Now About Social Code
summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rw-r--r--scripts/level_gen.gd33
-rw-r--r--scripts/marching_cubes.gd22
2 files changed, 43 insertions, 12 deletions
diff --git a/scripts/level_gen.gd b/scripts/level_gen.gd
index b86da73..ea636f8 100644
--- a/scripts/level_gen.gd
+++ b/scripts/level_gen.gd
@@ -8,6 +8,8 @@ extends Node3D
@export var scale_y: int = 10
@export var scale_z: int = 10
+@export var radius: int = 4
+
var map_grid: Array[float] = []
func map_grid_get(x: int, y: int, z: int):
@@ -15,24 +17,36 @@ func map_grid_get(x: int, y: int, z: int):
func gen_map_grid():
map_grid.resize(width*height*depth)
+ map_grid.fill(10.0)
+ var noise = FastNoiseLite.new()
+ noise.noise_type = FastNoiseLite.TYPE_SIMPLEX
+ noise.frequency = 0.04
for z in depth:
- for y in height:
- for x in width:
- if y <= 2:
- map_grid[z*height*width + y*width + x] = -1.0
- else:
- map_grid[z*height*width + y*width + x] = 10.0
+ for x in width:
+ var mid_x = x - width/2.0
+ var mid_z = z - depth/2.0
+ var pos_vec = Vector2(mid_x, mid_z)
+ var pos_radius = pos_vec.length()
+ var multiplier = 1.0 / (max(1, pos_radius - radius))
+ var gen_h = noise.get_noise_2d(x, z)
+ gen_h = remap(gen_h, -1, 1, 0, height/2.0)
+ gen_h = int(gen_h*multiplier)
+
+ for y in gen_h:
+ map_grid[z*height*width + y*width + x] = -1.0
# Called when the node enters the scene tree for the first time.
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()
@@ -58,12 +72,13 @@ 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, triangles, vertices, normals)
- # Create the Mesh.
+ # Create the Mesh
+ print("Lengths are ", len(vertices), " ", len(normals))
arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
var material = preload("res://materials/terrain.tres")
- arr_mesh.surface_set_material(0, material)
+ #arr_mesh.surface_set_material(0, material)
var m = MeshInstance3D.new()
m.mesh = arr_mesh
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))