diff --git a/compute_samples.gd b/compute_samples.gd index 76b99ca..1844a62 100644 --- a/compute_samples.gd +++ b/compute_samples.gd @@ -1,17 +1,23 @@ var rd: RenderingDevice -var shader_file: Resource -var shader_spirv: RDShaderSPIRV -var shader: RID +var shader_file1: Resource +var shader_file2: Resource +var shader_spirv1: RDShaderSPIRV +var shader_spirv2: RDShaderSPIRV +var shader_pass1: RID +var shader_pass2: RID var start_time := Time.get_ticks_msec() / 1000.0 func create_device(): rd = RenderingServer.create_local_rendering_device() - shader_file = load("res://sdf_shader.glsl") - shader_spirv = shader_file.get_spirv() - shader = rd.shader_create_from_spirv(shader_spirv) + shader_file1 = load("res://sdf_shader.glsl") + shader_spirv1 = shader_file1.get_spirv() + shader_pass1 = rd.shader_create_from_spirv(shader_spirv1) + shader_file2 = load("res://sdf_mesh_generation.glsl") + shader_spirv2 = shader_file2.get_spirv() + shader_pass2 = rd.shader_create_from_spirv(shader_spirv2) -func compute(world_size: int, threshold: float): +func compute_mesh(world_size: int, threshold: float) -> ArrayMesh: # Prepare our data. We use floats in the shader, so we need 32 bit. var total = world_size * world_size * world_size @@ -40,11 +46,53 @@ func compute(world_size: int, threshold: float): # Create a uniform to assign the buffer to the rendering device var surface_uniform_buf := RDUniform.new() surface_uniform_buf.uniform_type = RenderingDevice.UNIFORM_TYPE_STORAGE_BUFFER - surface_uniform_buf.binding = 2 # this needs to match the "binding" in our shader file + surface_uniform_buf.binding = 2 surface_uniform_buf.add_id(surface_buffer) + # Binding 3: Normals ( 3 floats per voxel) + var normal_total = total * 3 + var normal_input := PackedFloat32Array() + normal_input.resize(normal_total) + # Ensure we pass the byte size correctly: total_elements * 4 bytes per float + var normal_buffer = rd.storage_buffer_create(normal_input.size() * 4, normal_input.to_byte_array()) + + var normal_uniform = RDUniform.new() + normal_uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_STORAGE_BUFFER + normal_uniform.binding = 3 + normal_uniform.add_id(normal_buffer) + + # Binding 4: UVs (vec2 = 2 floats per voxel) + var uv_bytes = PackedFloat32Array() + uv_bytes.resize(total * 2) + var uv_buffer = rd.storage_buffer_create(uv_bytes.size() * 4, uv_bytes.to_byte_array()) + var uv_uniform = RDUniform.new() + uv_uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_STORAGE_BUFFER + uv_uniform.binding = 4 + uv_uniform.add_id(uv_buffer) + + # Index buffer + # 1. Use a safer max_indices for testing (18 is the absolute max, 6 is usually enough) + var safe_max = world_size * world_size * world_size * 6 + # 2. Pre-calculate byte array to avoid double-allocation spikes + var index_bytes := PackedInt32Array() + index_bytes.resize(safe_max) + var index_raw_bytes = index_bytes.to_byte_array() + # 3. Explicitly create the buffer + var idx_buffer = rd.storage_buffer_create(index_raw_bytes.size(), index_raw_bytes) + var idx_uniform := RDUniform.new() + idx_uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_STORAGE_BUFFER + idx_uniform.binding = 5 + idx_uniform.add_id(idx_buffer) + + # Atomic counter + var counter_bytes = PackedInt32Array([0]).to_byte_array() + var counter_buffer = rd.storage_buffer_create(4, counter_bytes) + var counter_uniform := RDUniform.new() + counter_uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_STORAGE_BUFFER + counter_uniform.binding = 6 + counter_uniform.add_id(counter_buffer) + var u_time := Time.get_ticks_msec() / 1000.0 - start_time - print("utime: ", u_time) var peer := StreamPeerBuffer.new() peer.put_32(world_size) @@ -60,40 +108,92 @@ func compute(world_size: int, threshold: float): uniform_params.binding = 1 uniform_params.add_id(params_buffer) - var uniform_set := rd.uniform_set_create([uniform_params, uniform_buf, surface_uniform_buf], shader, 0) # the last parameter (the 0) needs to match the "set" in our shader file + var uniform_set1 := rd.uniform_set_create([uniform_buf, uniform_params, surface_uniform_buf, normal_uniform, uv_uniform, idx_uniform, counter_uniform], shader_pass1, 0) # the last parameter (the 0) needs to match the "set" in our shader file + var uniform_set2 := rd.uniform_set_create([uniform_buf, uniform_params, surface_uniform_buf, normal_uniform, uv_uniform, idx_uniform, counter_uniform], shader_pass2, 0) - # Create a compute pipeline - var pipeline := rd.compute_pipeline_create(shader) - var compute_list := rd.compute_list_begin() - rd.compute_list_bind_compute_pipeline(compute_list, pipeline) - rd.compute_list_bind_uniform_set(compute_list, uniform_set, 0) - var dispatch_x = int(ceil(world_size / 8.0)) - var dispatch_y = int(ceil(world_size / 8.0)) - var dispatch_z = int(ceil(world_size / 1.0)) # local_size_z = 1 - rd.compute_list_dispatch(compute_list, dispatch_x, dispatch_y, dispatch_z) + var dispatch_count = int(ceil(world_size / 4.0)) + + # 1. Dispatch PASS 1 (Calculate Points) + var pipeline1 := rd.compute_pipeline_create(shader_pass1) # Points only + var compute_list = rd.compute_list_begin() + rd.compute_list_bind_compute_pipeline(compute_list, pipeline1) + rd.compute_list_bind_uniform_set(compute_list, uniform_set1, 0) + rd.compute_list_dispatch(compute_list, dispatch_count, dispatch_count, dispatch_count) + rd.compute_list_end() + + # This is the "Magic" command that fixes the stripes/holes + # It forces the GPU to finish all writes to the surface_buffer + # before any other shader starts reading it. + rd.barrier(RenderingDevice.BARRIER_MASK_ALL_BARRIERS) + + # 2. Dispatch PASS 2 (Generate Indices) + var pipeline2 := rd.compute_pipeline_create(shader_pass2) # Indices only + compute_list = rd.compute_list_begin() + rd.compute_list_bind_compute_pipeline(compute_list, pipeline2) + rd.compute_list_bind_uniform_set(compute_list, uniform_set2, 0) + rd.compute_list_dispatch(compute_list, dispatch_count, dispatch_count, dispatch_count) rd.compute_list_end() # Submit to GPU and wait for sync rd.submit() rd.sync() # Read back the data from the buffer - var output_bytes := rd.buffer_get_data(surface_buffer) - var output := output_bytes.to_float32_array() - - var output_sample_bytes := rd.buffer_get_data(buffer) - var output_sample := output_sample_bytes.to_float32_array() - + var out_verts = rd.buffer_get_data(surface_buffer).to_float32_array() + var out_norms = rd.buffer_get_data(normal_buffer).to_float32_array() + var out_indices = rd.buffer_get_data(idx_buffer).to_int32_array() + var final_count = rd.buffer_get_data(counter_buffer).to_int32_array()[0] + rd.free_rid(buffer) rd.free_rid(params_buffer) rd.free_rid(surface_buffer) + rd.free_rid(normal_buffer) + rd.free_rid(uv_buffer) + rd.free_rid(idx_buffer) + rd.free_rid(counter_buffer) - var surface_points_dict := build_surface_dict(world_size, output) - var sample_points_dict := build_sample_dict(world_size, output_sample) + # 5. Build the Mesh + var mesh = ArrayMesh.new() + var arrays = [] + arrays.resize(Mesh.ARRAY_MAX) + + # We need to reshape the flat float array into Vector3s + var verts := PackedVector3Array() + var normals := PackedVector3Array() + # Instead of blindly appending every voxel, we check if the voxel was "active" + # Or, even better, map the original voxel indices to new packed indices + var active_map = {} + var packed_verts := PackedVector3Array() + var packed_normals := PackedVector3Array() + var packed_indices := PackedInt32Array() + + var offset = Vector3(world_size / 2.0, world_size / 2.0, world_size / 2.0) + var final_indices = out_indices.slice(0, final_count) + + for old_idx in final_indices: + var v_base = old_idx * 3 + + # 1. Skip if the shader marked this as an empty voxel + if out_verts[v_base] < -0.5: + continue + + if not active_map.has(old_idx): + active_map[old_idx] = packed_verts.size() + # 2. Re-center the vertex so the mesh isn't floating in the corner + var pos = Vector3(out_verts[v_base], out_verts[v_base+1], out_verts[v_base+2]) - offset + packed_verts.append(pos) + packed_normals.append(Vector3(out_norms[v_base], out_norms[v_base+1], out_norms[v_base+2])) + + packed_indices.append(active_map[old_idx]) + + + if packed_verts.size() > 0: + arrays[Mesh.ARRAY_VERTEX] = packed_verts + arrays[Mesh.ARRAY_NORMAL] = packed_normals + arrays[Mesh.ARRAY_INDEX] = packed_indices + mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays) + + return mesh - return { - "surface_points_dict": surface_points_dict, - "sample_points_dict": sample_points_dict - } func build_sample_dict(world_size: int, flat_buffer: PackedFloat32Array) -> Dictionary: var dict := {} diff --git a/default_env.tres b/default_env.tres index 85b532b..f9fa256 100644 --- a/default_env.tres +++ b/default_env.tres @@ -1,4 +1,4 @@ -[gd_resource type="Environment" load_steps=2 format=3 uid="uid://ti46fffblpei"] +[gd_resource type="Environment" format=3 uid="uid://ti46fffblpei"] [sub_resource type="Sky" id="1"] diff --git a/generate_mesh.tres b/generate_mesh.tres index 097cfc3..80663c1 100644 --- a/generate_mesh.tres +++ b/generate_mesh.tres @@ -1,26 +1,9 @@ -[gd_resource type="VisualShader" load_steps=2 format=3 uid="uid://bose286qacwdl"] +[gd_resource type="VisualShader" format=3 uid="uid://bose286qacwdl"] [sub_resource type="VisualShaderNodeColorConstant" id="VisualShaderNodeColorConstant_sxi40"] constant = Color(0.30202293, 0.6060653, 0.9109474, 1) [resource] -code = "shader_type spatial; -render_mode blend_mix, depth_draw_opaque, depth_test_default, cull_back, diffuse_lambert, specular_schlick_ggx; - - - - -void fragment() { -// ColorConstant:2 - vec4 n_out2p0 = vec4(0.302023, 0.606065, 0.910947, 1.000000); - - -// Output:0 - ALBEDO = vec3(n_out2p0.xyz); - - -} -" nodes/vertex/0/position = Vector2(360, 220) nodes/fragment/0/position = Vector2(660, 140) nodes/fragment/2/node = SubResource("VisualShaderNodeColorConstant_sxi40") diff --git a/project.godot b/project.godot index 2b0866b..fa8e03d 100644 --- a/project.godot +++ b/project.godot @@ -8,11 +8,15 @@ config_version=5 +[animation] + +compatibility/default_parent_skeleton_in_mesh_instance_3d=true + [application] config/name="GammaShade" run/main_scene="res://world.tscn" -config/features=PackedStringArray("4.5") +config/features=PackedStringArray("4.6") config/icon="res://icon.png" [debug] diff --git a/sample_point.tscn b/sample_point.tscn index 2dfc2ba..b9925a5 100644 --- a/sample_point.tscn +++ b/sample_point.tscn @@ -1,8 +1,7 @@ -[gd_scene load_steps=2 format=3 uid="uid://bm0qtcmn8rna5"] +[gd_scene format=3 uid="uid://bm0qtcmn8rna5"] [ext_resource type="Script" uid="uid://likm24qx18tm" path="res://sample_point.gd" id="1_x8428"] -[node name="SamplePoint" type="Node3D"] -visible = null +[node name="SamplePoint" type="Node3D" unique_id=1719920532] +visible = false script = ExtResource("1_x8428") -visible = null diff --git a/sdf_mesh_generation.glsl b/sdf_mesh_generation.glsl new file mode 100644 index 0000000..a7bf065 --- /dev/null +++ b/sdf_mesh_generation.glsl @@ -0,0 +1,86 @@ +#[compute] +#version 450 + +layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in; + +layout(set = 0, binding = 0, std430) buffer DataBuffer { float sample_points[]; } voxels; +layout(set = 0, binding = 1) uniform Params { int world_size; float threshold; float u_time; } params; +layout(set = 0, binding = 2, std430) buffer SurfaceBuffer { float surface_points[]; } surface; +layout(set = 0, binding = 5, std430) buffer IndexBuffer { uint indices[]; } mesh_indices; +layout(set = 0, binding = 6, std430) buffer Counter { uint count; } index_count; + +uint index3(uint x, uint y, uint z) { + return x + y * params.world_size + z * params.world_size * params.world_size; +} + +bool has_vertex(ivec3 p) { + // Boundary check for the voxel itself + if (any(lessThan(p, ivec3(0))) || any(greaterThanEqual(p, ivec3(params.world_size)))) return false; + uint idx = index3(uint(p.x), uint(p.y), uint(p.z)); + return surface.surface_points[idx * 3u] > -0.5; +} + +// Translated QUAD_POINTS from your GDScript +const ivec3 QUAD_OFFSETS[3][4] = ivec3[3][4]( + // X-Axis Edges + ivec3[](ivec3(0,0,-1), ivec3(0,-1,-1), ivec3(0,-1,0), ivec3(0,0,0)), + // Y-Axis Edges + ivec3[](ivec3(0,0,-1), ivec3(0,0,0), ivec3(-1,0,0), ivec3(-1,0,-1)), + // Z-Axis Edges + ivec3[](ivec3(0,0,0), ivec3(0,-1,0), ivec3(-1,-1,0), ivec3(-1,0,0)) +); + +void main() { + ivec3 id = ivec3(gl_GlobalInvocationID); + if (id.x >= params.world_size || id.y >= params.world_size || id.z >= params.world_size) return; + + uint idx = index3(id.x, id.y, id.z); + + // Check the 3 primary axes (forward edges) + const ivec3 AXIS[3] = ivec3[](ivec3(1,0,0), ivec3(0,1,0), ivec3(0,0,1)); + + for (int i = 0; i < 3; i++) { + ivec3 neighbor_id = id + AXIS[i]; + + // Ensure neighbor is in bounds before sampling noise + if (any(greaterThanEqual(neighbor_id, ivec3(params.world_size)))) continue; + + float d1 = voxels.sample_points[idx]; + float d2 = voxels.sample_points[index3(neighbor_id.x, neighbor_id.y, neighbor_id.z)]; + + // Sign change check (Crossing detection) + if ((d1 < 0.0) != (d2 < 0.0)) { + ivec3 p0 = id + QUAD_OFFSETS[i][0]; + ivec3 p1 = id + QUAD_OFFSETS[i][1]; + ivec3 p2 = id + QUAD_OFFSETS[i][2]; + ivec3 p3 = id + QUAD_OFFSETS[i][3]; + + // Verify all 4 surrounding voxels have valid vertices + if (has_vertex(p0) && has_vertex(p1) && has_vertex(p2) && has_vertex(p3)) { + uint v0 = index3(p0.x, p0.y, p0.z); + uint v1 = index3(p1.x, p1.y, p1.z); + uint v2 = index3(p2.x, p2.y, p2.z); + uint v3 = index3(p3.x, p3.y, p3.z); + + uint start = atomicAdd(index_count.count, 6); + + // Use the same winding logic as your add_quad / add_reversed_quad + if (d1 < 0.0) { + mesh_indices.indices[start + 0] = v0; + mesh_indices.indices[start + 1] = v1; + mesh_indices.indices[start + 2] = v2; + mesh_indices.indices[start + 3] = v0; + mesh_indices.indices[start + 4] = v2; + mesh_indices.indices[start + 5] = v3; + } else { + mesh_indices.indices[start + 0] = v0; + mesh_indices.indices[start + 1] = v2; + mesh_indices.indices[start + 2] = v1; + mesh_indices.indices[start + 3] = v0; + mesh_indices.indices[start + 4] = v3; + mesh_indices.indices[start + 5] = v2; + } + } + } + } +} diff --git a/sdf_mesh_generation.glsl.import b/sdf_mesh_generation.glsl.import new file mode 100644 index 0000000..7c8e2d7 --- /dev/null +++ b/sdf_mesh_generation.glsl.import @@ -0,0 +1,14 @@ +[remap] + +importer="glsl" +type="RDShaderFile" +uid="uid://d348vk1vnsbps" +path="res://.godot/imported/sdf_mesh_generation.glsl-debc521de09c2e8ab62d3825224df785.res" + +[deps] + +source_file="res://sdf_mesh_generation.glsl" +dest_files=["res://.godot/imported/sdf_mesh_generation.glsl-debc521de09c2e8ab62d3825224df785.res"] + +[params] + diff --git a/sdf_shader.glsl b/sdf_shader.glsl index c298353..87aba07 100644 --- a/sdf_shader.glsl +++ b/sdf_shader.glsl @@ -2,33 +2,48 @@ #version 450 // Workgroup size -layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; +layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in; // Storage buffer layout(set = 0, binding = 0, std430) buffer DataBuffer { - float sample_points[]; + float sample_points[]; } voxels; layout(set = 0, binding = 1) uniform Params { - int world_size; - float threshold; - float u_time; + int world_size; + float threshold; + float u_time; } params; layout(set = 0, binding = 2, std430) buffer SurfaceBuffer { - float surface_points[]; + float surface_points[]; } surface; +layout(set = 0, binding = 3, std430) buffer NormalsBuffer { + float normals[]; +} normal; + +layout(set = 0, binding = 4, std430) buffer UVBuffer { + vec2 UVs[]; +} UV; + +layout(set = 0, binding = 5, std430) buffer IndexBuffer { + uint indices[]; +} mesh_indices; + +layout(set = 0, binding = 6, std430) buffer Counter { + uint count; +} index_count; uint index3(uint x, uint y, uint z) { - return x + y * params.world_size + z * params.world_size * params.world_size; + return x + y * params.world_size + z * params.world_size * params.world_size; } void store_surface_point(uint idx, vec3 pos) { - uint base = idx * 3u; - surface.surface_points[base + 0u] = pos.x; - surface.surface_points[base + 1u] = pos.y; - surface.surface_points[base + 2u] = pos.z; + uint base = idx * 3u; + surface.surface_points[base + 0u] = pos.x; + surface.surface_points[base + 1u] = pos.y; + surface.surface_points[base + 2u] = pos.z; } // @@ -44,156 +59,195 @@ void store_surface_point(uint idx, vec3 pos) { // vec3 mod289(vec3 x) { - return x - floor(x * (1.0 / 289.0)) * 289.0; + return x - floor(x * (1.0 / 289.0)) * 289.0; } vec4 mod289(vec4 x) { - return x - floor(x * (1.0 / 289.0)) * 289.0; + return x - floor(x * (1.0 / 289.0)) * 289.0; } vec4 permute(vec4 x) { - return mod289(((x*34.0)+10.0)*x); + return mod289(((x*34.0)+10.0)*x); } vec4 taylorInvSqrt(vec4 r) { - return 1.79284291400159 - 0.85373472095314 * r; + return 1.79284291400159 - 0.85373472095314 * r; } float snoise(vec3 v) { - const vec2 C = vec2(1.0/6.0, 1.0/3.0) ; - const vec4 D = vec4(0.0, 0.5, 1.0, 2.0); + const vec2 C = vec2(1.0/6.0, 1.0/3.0) ; + const vec4 D = vec4(0.0, 0.5, 1.0, 2.0); - // First corner - vec3 i = floor(v + dot(v, C.yyy) ); - vec3 x0 = v - i + dot(i, C.xxx) ; + // First corner + vec3 i = floor(v + dot(v, C.yyy) ); + vec3 x0 = v - i + dot(i, C.xxx) ; - // Other corners - vec3 g = step(x0.yzx, x0.xyz); - vec3 l = 1.0 - g; - vec3 i1 = min( g.xyz, l.zxy ); - vec3 i2 = max( g.xyz, l.zxy ); + // Other corners + vec3 g = step(x0.yzx, x0.xyz); + vec3 l = 1.0 - g; + vec3 i1 = min( g.xyz, l.zxy ); + vec3 i2 = max( g.xyz, l.zxy ); - // x0 = x0 - 0.0 + 0.0 * C.xxx; - // x1 = x0 - i1 + 1.0 * C.xxx; - // x2 = x0 - i2 + 2.0 * C.xxx; - // x3 = x0 - 1.0 + 3.0 * C.xxx; - vec3 x1 = x0 - i1 + C.xxx; - vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y - vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y + // x0 = x0 - 0.0 + 0.0 * C.xxx; + // x1 = x0 - i1 + 1.0 * C.xxx; + // x2 = x0 - i2 + 2.0 * C.xxx; + // x3 = x0 - 1.0 + 3.0 * C.xxx; + vec3 x1 = x0 - i1 + C.xxx; + vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y + vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y - // Permutations - i = mod289(i); - vec4 p = permute( permute( permute( - i.z + vec4(0.0, i1.z, i2.z, 1.0 )) - + i.y + vec4(0.0, i1.y, i2.y, 1.0 )) - + i.x + vec4(0.0, i1.x, i2.x, 1.0 )); + // Permutations + i = mod289(i); + vec4 p = permute( permute( permute( + i.z + vec4(0.0, i1.z, i2.z, 1.0 )) + + i.y + vec4(0.0, i1.y, i2.y, 1.0 )) + + i.x + vec4(0.0, i1.x, i2.x, 1.0 )); - // Gradients: 7x7 points over a square, mapped onto an octahedron. - // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) - float n_ = 0.142857142857; // 1.0/7.0 - vec3 ns = n_ * D.wyz - D.xzx; + // Gradients: 7x7 points over a square, mapped onto an octahedron. + // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) + float n_ = 0.142857142857; // 1.0/7.0 + vec3 ns = n_ * D.wyz - D.xzx; - vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7) + vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7) - vec4 x_ = floor(j * ns.z); - vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N) + vec4 x_ = floor(j * ns.z); + vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N) - vec4 x = x_ *ns.x + ns.yyyy; - vec4 y = y_ *ns.x + ns.yyyy; - vec4 h = 1.0 - abs(x) - abs(y); + vec4 x = x_ *ns.x + ns.yyyy; + vec4 y = y_ *ns.x + ns.yyyy; + vec4 h = 1.0 - abs(x) - abs(y); - vec4 b0 = vec4( x.xy, y.xy ); - vec4 b1 = vec4( x.zw, y.zw ); + vec4 b0 = vec4( x.xy, y.xy ); + vec4 b1 = vec4( x.zw, y.zw ); - //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0; - //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0; - vec4 s0 = floor(b0)*2.0 + 1.0; - vec4 s1 = floor(b1)*2.0 + 1.0; - vec4 sh = -step(h, vec4(0.0)); + //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0; + //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0; + vec4 s0 = floor(b0)*2.0 + 1.0; + vec4 s1 = floor(b1)*2.0 + 1.0; + vec4 sh = -step(h, vec4(0.0)); - vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ; - vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ; + vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ; + vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ; - vec3 p0 = vec3(a0.xy,h.x); - vec3 p1 = vec3(a0.zw,h.y); - vec3 p2 = vec3(a1.xy,h.z); - vec3 p3 = vec3(a1.zw,h.w); + vec3 p0 = vec3(a0.xy,h.x); + vec3 p1 = vec3(a0.zw,h.y); + vec3 p2 = vec3(a1.xy,h.z); + vec3 p3 = vec3(a1.zw,h.w); - //Normalise gradients - vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3))); - p0 *= norm.x; - p1 *= norm.y; - p2 *= norm.z; - p3 *= norm.w; + //Normalise gradients + vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3))); + p0 *= norm.x; + p1 *= norm.y; + p2 *= norm.z; + p3 *= norm.w; - // Mix final noise value - vec4 m = max(0.5 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0); - m = m * m; - return 105.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1), - dot(p2,x2), dot(p3,x3) ) ); + // Mix final noise value + vec4 m = max(0.5 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0); + m = m * m; + return 105.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1), + dot(p2,x2), dot(p3,x3) ) ); } +const ivec3 AXIS[3] = ivec3[]( + ivec3(1,0,0), + ivec3(0,1,0), + ivec3(0,0,1) +); + +const ivec3 SURFACE_AXIS[8] = ivec3[]( + ivec3(1,0,0), + ivec3(0,1,0), + ivec3(0,0,1), + ivec3(1,0,1), + ivec3(0,1,1), + ivec3(1,1,0), + ivec3(1,1,1), + ivec3(0,0,0) +); + +// The 12 edges of a cube (pairs of corner indices 0-7) +const ivec2 EDGE_CORNERS[12] = ivec2[]( + ivec2(0,1), ivec2(1,2), ivec2(2,3), ivec2(3,0), // Bottom face edges + ivec2(4,5), ivec2(5,6), ivec2(6,7), ivec2(7,4), // Top face edges + ivec2(0,4), ivec2(1,5), ivec2(2,6), ivec2(3,7) // Vertical edges +); + +// The 8 corners of a cube +const ivec3 CORNERS[8] = ivec3[]( + ivec3(0,0,0), ivec3(1,0,0), ivec3(1,1,0), ivec3(0,1,0), + ivec3(0,0,1), ivec3(1,0,1), ivec3(1,1,1), ivec3(0,1,1) +); + +float get_noise_at(vec3 p) { + return snoise((p / 10.0) + vec3(params.u_time * .5)) * 5.0; +} + +// Calculate normal using central difference +vec3 calculate_normal(vec3 p) { + float e = 0.01; // Small epsilon + float dx = get_noise_at(p + vec3(e, 0, 0)) - get_noise_at(p - vec3(e, 0, 0)); + float dy = get_noise_at(p + vec3(0, e, 0)) - get_noise_at(p - vec3(0, e, 0)); + float dz = get_noise_at(p + vec3(0, 0, e)) - get_noise_at(p - vec3(0, 0, e)); + return normalize(vec3(dx, dy, dz)); +} void main() { - uvec3 id = gl_GlobalInvocationID; - if (id.x >= uint(params.world_size) || - id.y >= uint(params.world_size) || - id.z >= uint(params.world_size)) - return; + uvec3 id = gl_GlobalInvocationID; + if (id.x >= uint(params.world_size) || + id.y >= uint(params.world_size) || + id.z >= uint(params.world_size)) + return; - const ivec3 SURFACE_AXIS[8] = ivec3[]( - ivec3(1,0,0), - ivec3(0,1,0), - ivec3(0,0,1), - ivec3(1,0,1), - ivec3(0,1,1), - ivec3(1,1,0), - ivec3(1,1,1), - ivec3(0,0,0) - ); + vec3 p = vec3(id) - params.world_size / 2; + uint idx = index3(id.x, id.y, id.z); + voxels.sample_points[idx] = get_noise_at(p); - vec3 p = vec3(id) - params.world_size / 2; - uint idx = index3(id.x, id.y, id.z); - voxels.sample_points[idx] = snoise((p / 10.0) + vec3(params.u_time * .5, params.u_time * .5, params.u_time * .5)) *5.0; + barrier(); + memoryBarrierBuffer(); - int previous_sign = 0; - vec3 previous_sample_point_coords = vec3(id.x, id.y, id.z); - float previous_sample_point_distance = 0.0; - vec3 intersection_points_sum = vec3(0.0, 0.0, 0.0); - uint intersection_points_count = 0u; - for (int sample_point_to_check_index = 0; sample_point_to_check_index < 8; sample_point_to_check_index++){ - ivec3 sample_point_to_check = ivec3(id) + SURFACE_AXIS[sample_point_to_check_index]; - - // bounds check - //if (sample_point_to_check.x < 0 || sample_point_to_check.y < 0 || sample_point_to_check.z < 0) continue; - if (uint(sample_point_to_check.x) >= params.world_size || uint(sample_point_to_check.y) >= params.world_size || uint(sample_point_to_check.z) >= params.world_size) continue; - - uint buffer_index = index3(uint(sample_point_to_check.x), uint(sample_point_to_check.y), uint(sample_point_to_check.z)); - float sample_point_distance_from_sdf = voxels.sample_points[buffer_index]; - - int current_sign = (sample_point_distance_from_sdf >= 0.0 ? 1 : -1); - - if (previous_sign != 0 && current_sign != previous_sign) { - float t = previous_sample_point_distance / (previous_sample_point_distance - sample_point_distance_from_sdf); - vec3 intersect = mix(vec3(previous_sample_point_coords), vec3(sample_point_to_check), t); - intersection_points_sum += intersect; - intersection_points_count++; - } + vec3 intersection_sum = vec3(0.0); + uint count = 0; - previous_sign = current_sign; - previous_sample_point_distance = sample_point_distance_from_sdf; - previous_sample_point_coords = sample_point_to_check; - } + // Dual Contouring requires checking all 12 edges of the voxel. + // If ANY of these 12 edges has a sign change, this voxel MUST have a vertex. + for (int i = 0; i < 12; i++) { + ivec3 c1_off = CORNERS[EDGE_CORNERS[i].x]; + ivec3 c2_off = CORNERS[EDGE_CORNERS[i].y]; - if (intersection_points_count > 0u) { - vec3 avg = intersection_points_sum / float(intersection_points_count); - store_surface_point(idx, avg); - } else { - store_surface_point(idx, vec3(0.0)); // no surface - } + uvec3 c1 = id + uvec3(c1_off); + uvec3 c2 = id + uvec3(c2_off); + // Bounds check to prevent sampling noise outside the allocated buffer + if (any(greaterThanEqual(c1, uvec3(params.world_size))) || + any(greaterThanEqual(c2, uvec3(params.world_size)))) continue; + float d1 = voxels.sample_points[index3(c1.x, c1.y, c1.z)]; + float d2 = voxels.sample_points[index3(c2.x, c2.y, c2.z)]; + // Standard sign-change test + if ((d1 < 0.0) != (d2 < 0.0)) { + // Linear interpolation to find the exact crossing point on the edge + float t = d1 / (d1 - d2); + intersection_sum += mix(vec3(c1), vec3(c2), t); + count++; + } + } + + if (count > 0) { + vec3 avg_pos = intersection_sum / float(count); + store_surface_point(idx, avg_pos); + + // Normals should be calculated at the exact averaged surface point + vec3 world_p = avg_pos - params.world_size / 2.0; + vec3 n = calculate_normal(world_p); + uint base = idx * 3u; + normal.normals[base + 0] = n.x; + normal.normals[base + 1] = n.y; + normal.normals[base + 2] = n.z; + } else { + // Explicitly mark empty voxels to prevent them from being used in Pass 2 + store_surface_point(idx, vec3(-1.0)); + } } diff --git a/smooth_world.gd b/smooth_world.gd index 7d939ff..7fe0b19 100644 --- a/smooth_world.gd +++ b/smooth_world.gd @@ -38,29 +38,24 @@ func _ready() -> void: add_child(meshinstance) gpu_sdf = ComputeSdf.new() gpu_sdf.create_device() - var dictionaries = gpu_sdf.compute(world_size, RADIUS) - surface_points = dictionaries.surface_points_dict - sample_points = dictionaries.sample_points_dict - create_surface_mesh() + meshinstance.mesh = gpu_sdf.compute_mesh(world_size, RADIUS) func _input(event): if event is InputEventKey and event.is_action_released("RegenerateMesh"): - regenerate_mesh = !regenerate_mesh + if regenerate_mesh: + regenerate_mesh = false + else: + regenerate_mesh = true func _process(_delta: float) -> void: + if show_surface && regenerate_mesh: + #regenerate_mesh = false + clear() + meshinstance.mesh = gpu_sdf.compute_mesh(world_size, RADIUS) if !surface_points.is_empty(): - #create_surface_points_gpu() - #create_surface_points() if show_surface_points: draw_surface_points() - if show_surface && regenerate_mesh: - #regenerate_mesh = false - clear() - var dictionaries = gpu_sdf.compute(world_size, RADIUS) - surface_points = dictionaries.surface_points_dict - sample_points = dictionaries.sample_points_dict - create_surface_mesh() if show_sample_points: draw_sample_points() @@ -87,101 +82,3 @@ func draw_surface_points(): if surface_points[surface_point] != Vector3(0, 0, 0): DebugDraw3D.draw_square(surface_points[surface_point] , 0.3, surface_point_color) #DebugDraw3D.draw_text(surface_points[surface_point], str(surface_points[surface_point])) - -func create_surface_mesh(): - surface_tool = SurfaceTool.new() - surface_tool.begin(Mesh.PRIMITIVE_TRIANGLES) - for x in range(world_size): - for y in range(world_size): - for z in range(world_size): - if (surface_points.has(Vector3i(x, y, z))): - create_surface_mesh_quad(Vector3i(x,y,z)); - surface_tool.generate_normals() - mesh = surface_tool.commit() - meshinstance.mesh = mesh - -func create_surface_mesh_quad(index: Vector3i): - for axis_index in range(AXIS.size()): - var axis = AXIS[axis_index] - if sample_points.has(index + axis): - var sample_value1 = sample_points[index] - var sample_value2 = sample_points[index + axis] - - if sample_value1 < 0 and sample_value2 >= 0: - add_quad(index, axis_index) - elif sample_value1 >= 0 and sample_value2 < 0: - add_reversed_quad(index, axis_index) - -const AXIS := [ - Vector3i(1,0,0), - Vector3i(0,1,0), - Vector3i(0,0,1), -] - -const SURFACE_AXIS := [ - Vector3i(1,0,0), - Vector3i(0,1,0), - Vector3i(0,0,1), - Vector3i(1,0,1), - Vector3i(0,1,1), - Vector3i(1,1,0), - Vector3i(1,1,1), - Vector3i(0,0,0), -] - -func add_quad(index: Vector3i, axis_index: int): - var points = get_quad_points(index, axis_index) - if points != null: - surface_tool.add_vertex(points[0]) - surface_tool.add_vertex(points[1]) - surface_tool.add_vertex(points[2]) - - surface_tool.add_vertex(points[0]) - surface_tool.add_vertex(points[2]) - surface_tool.add_vertex(points[3]) - -func add_reversed_quad(index: Vector3i, axis_index: int): - var points = get_quad_points(index, axis_index) - - if points != null: - surface_tool.add_vertex(points[0]) - surface_tool.add_vertex(points[2]) - surface_tool.add_vertex(points[1]) - - surface_tool.add_vertex(points[0]) - surface_tool.add_vertex(points[3]) - surface_tool.add_vertex(points[2]) - -func get_quad_points(index: Vector3i, axis_index: int): - if surface_points.has(index + QUAD_POINTS[axis_index][0]) and surface_points.has(index + QUAD_POINTS[axis_index][1]) and surface_points.has(index + QUAD_POINTS[axis_index][2]) and surface_points.has(index + QUAD_POINTS[axis_index][3]) and surface_points.get(index + QUAD_POINTS[axis_index][0]) != Vector3.ZERO and surface_points.get(index + QUAD_POINTS[axis_index][1]) != Vector3.ZERO and surface_points.get(index + QUAD_POINTS[axis_index][2]) != Vector3.ZERO and surface_points.get(index + QUAD_POINTS[axis_index][3]) != Vector3.ZERO: - return [ - surface_points[index + QUAD_POINTS[axis_index][0]], - surface_points[index + QUAD_POINTS[axis_index][1]], - surface_points[index + QUAD_POINTS[axis_index][2]], - surface_points[index + QUAD_POINTS[axis_index][3]], - ] - - -const QUAD_POINTS := [ - # x axis - [ - Vector3i(0,0,-1), - Vector3i(0,-1,-1), - Vector3i(0,-1,0), - Vector3i(0,0,0) - ], - # y axis - [ - Vector3i(0,0,-1), - Vector3i(0,0,0), - Vector3i(-1,0,0), - Vector3i(-1,0,-1) - ], - # z axis - [ - Vector3i(0,0,0), - Vector3i(0,-1,0), - Vector3i(-1,-1,0), - Vector3i(-1,0,0) - ], -] diff --git a/smooth_world.tscn b/smooth_world.tscn index 6a18ed7..69da829 100644 --- a/smooth_world.tscn +++ b/smooth_world.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=4 format=3 uid="uid://llggsd0qmn4p"] +[gd_scene format=3 uid="uid://llggsd0qmn4p"] [ext_resource type="Script" uid="uid://bdfq22we54eul" path="res://smooth_world.gd" id="1_4h467"] [ext_resource type="Shader" uid="uid://bose286qacwdl" path="res://generate_mesh.tres" id="2_an62b"] @@ -7,16 +7,10 @@ render_priority = 0 shader = ExtResource("2_an62b") -[node name="SmoothWorld" type="Node3D"] +[node name="SmoothWorld" type="Node3D" unique_id=1592820568] script = ExtResource("1_4h467") RADIUS = 18.524 -regenerate_mesh = null -world_size = 30 -show_sample_points = null -show_surface_points = null -show_surface = null -do_the_thing = null -clear_the_thing = null +world_size = 64 -[node name="MeshInstance3D" type="MeshInstance3D" parent="."] +[node name="MeshInstance3D" type="MeshInstance3D" parent="." unique_id=1739626442] material_override = SubResource("ShaderMaterial_iutkt")