diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/level_gen.gd | 53 | ||||
-rw-r--r-- | scripts/marching_cubes.gd | 16 |
2 files changed, 53 insertions, 16 deletions
diff --git a/scripts/level_gen.gd b/scripts/level_gen.gd index ea636f8..91a4813 100644 --- a/scripts/level_gen.gd +++ b/scripts/level_gen.gd @@ -35,6 +35,28 @@ func gen_map_grid(): for y in gen_h: map_grid[z*height*width + y*width + x] = -1.0 +func add_normal(p: Vector3, tri_index: int, triangles: PackedInt32Array, vertices: PackedVector3Array, normal_list: Array[Vector4]): + var new_tri_index = 0 + while new_tri_index < len(triangles): + if floor(new_tri_index/3) == floor(tri_index/3): + new_tri_index += 3 + continue + + var orig_vert = vertices[triangles[tri_index]] + var verts = [ + new_tri_index+0, + new_tri_index+1, + new_tri_index+2] + + for i in verts: + var vert = vertices[triangles[i]] + if i == tri_index: + continue + if vert == orig_vert: + normal_list[triangles[tri_index]] += normal_list[triangles[i]] + + new_tri_index += 3 + # Called when the node enters the scene tree for the first time. func _ready(): gen_map_grid() @@ -72,8 +94,37 @@ func _ready(): cell.value[i] = map_grid_get(grid[i][0], grid[i][1], grid[i][2]) cell.position[i] = Vector3(grid[i][0]*scale_x, grid[i][1]*scale_y, grid[i][2]*scale_z) - MarchingCubes.march_cube(cell, triangles, vertices, normals) + MarchingCubes.march_cube(cell, triangles, vertices) + + var tri_index = 0 + var normal_list: Array[Vector4] = [] + normal_list.resize(len(vertices)) + normal_list.fill(Vector4(0,0,0,0)) + while tri_index < len(triangles): + var p1 = vertices[triangles[tri_index + 0]] + var p2 = vertices[triangles[tri_index + 1]] + var p3 = vertices[triangles[tri_index + 2]] + var normal = (p2 - p1).cross(p3 - p1).normalized(); + var normal_4 = Vector4(normal.x, normal.y, normal.z, 1) + normal_list[triangles[tri_index + 0]] += normal_4 + normal_list[triangles[tri_index + 1]] += normal_4 + normal_list[triangles[tri_index + 2]] += normal_4 + tri_index += 3 + + tri_index = 0 + while tri_index < len(triangles): + var p1 = vertices[triangles[tri_index + 0]] + var p2 = vertices[triangles[tri_index + 1]] + var p3 = vertices[triangles[tri_index + 2]] + add_normal(p1, tri_index+0, triangles, vertices, normal_list) + add_normal(p2, tri_index+1, triangles, vertices, normal_list) + add_normal(p3, tri_index+2, triangles, vertices, normal_list) + tri_index += 3 + + for normal in normal_list: + var real_normal = Vector3(normal.x, normal.y, normal.z) / normal.w + normals.push_back(real_normal) # Create the Mesh print("Lengths are ", len(vertices), " ", len(normals)) arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays) diff --git a/scripts/marching_cubes.gd b/scripts/marching_cubes.gd index 984f63d..6c5296c 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, normals: PackedVector3Array): +func march_cube(cell: Cell, triangles: PackedInt32Array, verticies: PackedVector3Array): var cube_index : int = 0 var vertex_list: Array[Vector3] = [] @@ -71,27 +71,13 @@ func march_cube(cell: Cell, triangles: PackedInt32Array, verticies: PackedVector 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)) |