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.gd65
1 files changed, 25 insertions, 40 deletions
diff --git a/scripts/level_gen.gd b/scripts/level_gen.gd
index 184ed26..66f465a 100644
--- a/scripts/level_gen.gd
+++ b/scripts/level_gen.gd
@@ -24,7 +24,8 @@ func gen_map_grid():
var noise = FastNoiseLite.new()
noise.noise_type = FastNoiseLite.TYPE_SIMPLEX
noise.frequency = 0.06
- noise.set_seed(0xDEADBEEF)
+ #noise.set_seed(0xDEADBEEF)
+ noise.set_seed(random.randi())
for z in depth:
for x in width:
var mid_x = x - width/2.0
@@ -39,31 +40,19 @@ func gen_map_grid():
for y in gen_h:
map_grid[z*height*width + y*width + x] = -100.0
-func add_normal(tri_index: int, triangles: PackedInt32Array, vertices: PackedVector3Array, normal_list: Array[Vector4]):
- var new_tri_index: int = 0
- while new_tri_index < len(triangles):
- @warning_ignore("integer_division")
- var tri_index_1:int = int(new_tri_index/3)
- @warning_ignore("integer_division")
- var tri_index_2:int = int(tri_index/3)
- if tri_index_1 == tri_index_2:
- new_tri_index += 3
+func add_normal(tri_index: int, vertices: PackedVector3Array, normal_list: Array[Vector4]):
+ var vertex_index: int = 0
+ while vertex_index < len(vertices):
+ if vertex_index == tri_index:
+ vertex_index += 1
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]]
+ var orig_vert = vertices[tri_index]
+ var vert = vertices[vertex_index]
+ if orig_vert.is_equal_approx(vert):
+ normal_list[tri_index] += normal_list[vertex_index]
- new_tri_index += 3
+ vertex_index += 1
func place_objects(num_objects: int, object: PackedScene):
var space = get_world_3d().direct_space_state
@@ -95,14 +84,12 @@ func _ready():
gen_map_grid()
var vertices = PackedVector3Array()
var normals = PackedVector3Array()
- var triangles = PackedInt32Array()
# Initialize the ArrayMesh.
var arr_mesh = ArrayMesh.new()
var arrays = []
arrays.resize(Mesh.ARRAY_MAX)
arrays[Mesh.ARRAY_VERTEX] = vertices
arrays[Mesh.ARRAY_NORMAL] = normals
- arrays[Mesh.ARRAY_INDEX] = triangles
var cell = MarchingCubes.Cell.new()
@@ -127,31 +114,29 @@ 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)
+ MarchingCubes.march_cube(cell, 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]]
+ while tri_index < len(vertices):
+ var p1 = vertices[tri_index + 0]
+ var p2 = vertices[tri_index + 1]
+ var p3 = vertices[tri_index + 2]
var normal = (p2 - p3).cross(p1 - p3).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
+ normal_list[tri_index + 0] += normal_4
+ normal_list[tri_index + 1] += normal_4
+ normal_list[tri_index + 2] += normal_4
tri_index += 3
- if smooth_normal:
- tri_index = 0
- while tri_index < len(triangles):
- add_normal(tri_index+0, triangles, vertices, normal_list)
- add_normal(tri_index+1, triangles, vertices, normal_list)
- add_normal(tri_index+2, triangles, vertices, normal_list)
- tri_index += 3
+ if smooth_normal:
+ var vert_index = 0
+ while vert_index < len(vertices):
+ add_normal(vert_index, vertices, normal_list)
+ vert_index += 1
for normal in normal_list:
var real_normal = Vector3(normal.x, normal.y, normal.z) / normal.w