Now About Social Code
summaryrefslogtreecommitdiff
path: root/scripts/level_gen.gd
diff options
context:
space:
mode:
authorLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2023-09-11 22:43:42 -0400
committerLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2023-09-11 22:43:42 -0400
commit4f1c7a600bc3f5b6210b0b2a16c45d22c9fd5e15 (patch)
tree83cccd8ec912e8412ad2d6a7eb1224034287fe95 /scripts/level_gen.gd
parent1e694fce7e4ba915861a5be543e40667b9da44ed (diff)
Get normal generation working
Normals do seem a bit buggy, so likely needs more tweaking
Diffstat (limited to 'scripts/level_gen.gd')
-rw-r--r--scripts/level_gen.gd33
1 files changed, 24 insertions, 9 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