Now About Social Code
summaryrefslogtreecommitdiff
path: root/scripts/level_gen.gd
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/level_gen.gd')
-rw-r--r--scripts/level_gen.gd53
1 files changed, 52 insertions, 1 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)