From 4f1c7a600bc3f5b6210b0b2a16c45d22c9fd5e15 Mon Sep 17 00:00:00 2001 From: Lucas Fryzek Date: Mon, 11 Sep 2023 22:43:42 -0400 Subject: Get normal generation working Normals do seem a bit buggy, so likely needs more tweaking --- materials/terrain.tres | 1 + scenes/main.tscn | 2 +- scripts/level_gen.gd | 33 ++++++++++++++++++++++++--------- scripts/marching_cubes.gd | 22 +++++++++++++++++++--- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/materials/terrain.tres b/materials/terrain.tres index f8d2b6a..bcfee7c 100644 --- a/materials/terrain.tres +++ b/materials/terrain.tres @@ -1,3 +1,4 @@ [gd_resource type="StandardMaterial3D" format=3 uid="uid://uiwp0gpofjrx"] [resource] +vertex_color_use_as_albedo = true diff --git a/scenes/main.tscn b/scenes/main.tscn index 2b25fc4..0f2600b 100644 --- a/scenes/main.tscn +++ b/scenes/main.tscn @@ -20,7 +20,7 @@ transform = Transform3D(1, 0, 0, 0, 0.805763, 0.592238, 0, -0.592238, 0.805763, script = ExtResource("2_ba28s") [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] -transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 30, 0) +transform = Transform3D(1, 0, 0, 0, -0.878192, 0.478309, 0, -0.478309, -0.878192, 0, 30, 0) [node name="MeshInstance3D" type="MeshInstance3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 10, 0) 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)) -- cgit