material-maker/material_maker/windows/sdf_builder/preview_3d.tscn

155 lines
4.3 KiB
Plaintext

[gd_scene load_steps=7 format=2]
[ext_resource path="res://material_maker/windows/sdf_builder/preview_3d.gd" type="Script" id=1]
[ext_resource path="res://material_maker/windows/sdf_builder/gizmo.tscn" type="PackedScene" id=2]
[sub_resource type="QuadMesh" id=2]
size = Vector2( 100, 100 )
[sub_resource type="Shader" id=3]
code = "shader_type spatial;
varying float elapsed_time;
varying vec3 world_camera;
varying vec3 world_position;
const int MAX_STEPS = 100;
const float MAX_DIST = 100.0;
const float SURF_DIST = 1e-3;
float box3d(vec3 uv, vec3 s) {
vec3 q = abs(uv) - s;
return length(max(q, 0.0))+min(max(q.x, max(q.y, q.z)), 0.0);
}
vec2 GetDist(vec3 p) {
float _seed_variation_ = 0.0;
vec2 d = vec2(box3d(p, vec3(0.5)), 0.0);
return d;
}
vec2 RayMarch(vec3 ro, vec3 rd) {
float dO = 0.0;
float color = 0.0;
vec2 dS;
for (int i = 0; i < MAX_STEPS; i++)
{
vec3 p = ro + dO * rd;
dS = GetDist(p);
dO += dS.x;
if (dS.x < SURF_DIST || dO > MAX_DIST) {
color = dS.y;
break;
}
}
return vec2(dO, color);
}
vec3 GetNormal(vec3 p) {
vec2 e = vec2(1e-2, 0);
vec3 n = GetDist(p).x - vec3(
GetDist(p - e.xyy).x,
GetDist(p - e.yxy).x,
GetDist(p - e.yyx).x
);
return normalize(n);
}
void vertex() {
elapsed_time = TIME;
vec4 world_position_xyzw = WORLD_MATRIX*vec4(VERTEX, 1.0);
world_position = world_position_xyzw.xyz/world_position_xyzw.w;
vec4 world_camera_xyzw = CAMERA_MATRIX * vec4(0, 0, 0, 1);
world_camera = world_camera_xyzw.xyz/world_camera_xyzw.w;
}
void fragment() {
float _seed_variation_ = 0.0;
vec3 ro = world_camera;
vec3 rd = normalize(world_position - ro);
vec2 rm = RayMarch(ro, rd);
float d = rm.x;
if (d >= MAX_DIST) {
discard;
} else {
vec3 p = ro + rd * d;
ALBEDO = vec3(1.0);
ROUGHNESS = 1.0;
METALLIC = 0.0;
NORMAL = (INV_CAMERA_MATRIX*WORLD_MATRIX*vec4(GetNormal(p), 0.0)).xyz;
}
}
"
[sub_resource type="ShaderMaterial" id=4]
shader = SubResource( 3 )
[sub_resource type="PlaneShape" id=5]
margin = 1.0
plane = Plane( 0, 0, 1, 0 )
[node name="Preview3D" type="ViewportContainer" groups=["preview"]]
margin_right = 1280.0
margin_bottom = 720.0
mouse_filter = 1
size_flags_horizontal = 3
size_flags_vertical = 3
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Viewport" type="Viewport" parent="."]
handle_input_locally = false
render_target_update_mode = 3
physics_object_picking = true
[node name="Gizmo" parent="Viewport" instance=ExtResource( 2 )]
[node name="CameraPosition" type="Spatial" parent="Viewport"]
[node name="CameraRotation1" type="Spatial" parent="Viewport/CameraPosition"]
[node name="CameraRotation2" type="Spatial" parent="Viewport/CameraPosition/CameraRotation1"]
[node name="Camera" type="Camera" parent="Viewport/CameraPosition/CameraRotation1/CameraRotation2"]
transform = Transform( 1, 0, 0, 0, 0.866025, 0.5, 0, -0.5, 0.866025, 0, 2, 4 )
current = true
[node name="Plane" type="MeshInstance" parent="Viewport/CameraPosition/CameraRotation1/CameraRotation2/Camera"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -50 )
mesh = SubResource( 2 )
material/0 = SubResource( 4 )
[node name="Area" type="Area" parent="Viewport/CameraPosition/CameraRotation1/CameraRotation2/Camera/Plane"]
input_capture_on_drag = true
[node name="CollisionShape" type="CollisionShape" parent="Viewport/CameraPosition/CameraRotation1/CameraRotation2/Camera/Plane/Area"]
shape = SubResource( 5 )
[node name="OmniLight" type="DirectionalLight" parent="Viewport"]
transform = Transform( 0.999941, -0.00199387, -0.01067, 0.0106636, -0.00320327, 0.999938, -0.00202793, -0.999993, -0.00318182, 0, 3.40578, 0 )
[node name="Buttons" type="HBoxContainer" parent="."]
margin_right = 40.0
margin_bottom = 40.0
[node name="GizmoButton" type="Button" parent="Buttons"]
margin_right = 54.0
margin_bottom = 40.0
text = "Gizmo"
[connection signal="resized" from="." to="." method="_on_Preview3D_resized"]
[connection signal="rotated" from="Viewport/Gizmo" to="." method="_on_Gizmo_rotated"]
[connection signal="translated" from="Viewport/Gizmo" to="." method="_on_Gizmo_translated"]
[connection signal="input_event" from="Viewport/CameraPosition/CameraRotation1/CameraRotation2/Camera/Plane/Area" to="." method="_on_Background_input_event"]
[connection signal="pressed" from="Buttons/GizmoButton" to="." method="_on_GizmoButton_pressed"]