diff options
Diffstat (limited to 'scripts/marching_cubes.gd')
-rw-r--r-- | scripts/marching_cubes.gd | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/scripts/marching_cubes.gd b/scripts/marching_cubes.gd index 09eaa0d..984f63d 100644 --- a/scripts/marching_cubes.gd +++ b/scripts/marching_cubes.gd @@ -12,7 +12,7 @@ class Cell: position.resize(8) value.resize(8) -func march_cube(cell: Cell, triangles: PackedInt32Array, verticies: PackedVector3Array): +func march_cube(cell: Cell, triangles: PackedInt32Array, verticies: PackedVector3Array, normals: PackedVector3Array): var cube_index : int = 0 var vertex_list: Array[Vector3] = [] @@ -67,15 +67,31 @@ func march_cube(cell: Cell, triangles: PackedInt32Array, verticies: PackedVector new_vertex_count += 1 tri_index += 1 - for vert in new_vertex_list: + for i in new_vertex_count: + var vert = new_vertex_list[i] verticies.push_back(vert) - + + var normal_list: Array[Vector4] = [] + normal_list.resize(new_vertex_count) + normal_list.fill(Vector4(0,0,0,0)) tri_index = 0 while mc._tri_table[cube_index][tri_index] != -1: + var p1: Vector3 = new_vertex_list[local_remap[mc._tri_table[cube_index][tri_index + 0]]] + var p2: Vector3 = new_vertex_list[local_remap[mc._tri_table[cube_index][tri_index + 1]]] + var p3: Vector3 = new_vertex_list[local_remap[mc._tri_table[cube_index][tri_index + 2]]] + var normal = (p2 - p1).cross(p3 - p1).normalized(); + var normal_4 = Vector4(normal.x, normal.y, normal.z, 1) + normal_list[local_remap[mc._tri_table[cube_index][tri_index + 0]]] += normal_4 + normal_list[local_remap[mc._tri_table[cube_index][tri_index + 1]]] += normal_4 + normal_list[local_remap[mc._tri_table[cube_index][tri_index + 2]]] += normal_4 triangles.push_back(local_remap[mc._tri_table[cube_index][tri_index + 0]] + starting_vertex_count) triangles.push_back(local_remap[mc._tri_table[cube_index][tri_index + 1]] + starting_vertex_count) triangles.push_back(local_remap[mc._tri_table[cube_index][tri_index + 2]] + starting_vertex_count) tri_index += 3 + + for normal in normal_list: + var new_normal = Vector3(normal.x, normal.y, normal.z) / normal.w + normals.push_back(new_normal.normalized()) func vertex_interpolate(p1: Vector3, p2: Vector3, val1: float, val2: float): return (p1 + (-val1 / (val2 - val1)) * (p2 - p1)) |