extends Node3D @export var width: int = 16 @export var height: int = 16 @export var depth: int = 16 @export var scale_x: int = 10 @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): return map_grid[z*height*width + y*width + x] 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 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 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() 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() for z in range(-1, depth): for y in range(-1, height): for x in range(-1, width): var grid = [ [x, y, z], [x + 1, y, z], [x + 1, y + 1, z], [x, y + 1, z], [x, y, z + 1], [x + 1, y, z + 1], [x + 1, y + 1, z + 1], [x, y + 1, z + 1], ] for i in grid.size(): if grid[i][0] >= width or grid[i][1] >= height or grid[i][2] >= depth or \ grid[i][0] < 0 or grid[i][1] < 0 or grid[i][2] < 0: cell.value[i] = 10.0 else: 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) 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) var material = preload("res://materials/terrain.tres") #arr_mesh.surface_set_material(0, material) var m = MeshInstance3D.new() m.mesh = arr_mesh var col_mesh = arr_mesh.create_trimesh_shape() var collision_shape = CollisionShape3D.new() collision_shape.set_shape(col_mesh) var static_body = StaticBody3D.new() static_body.add_child(collision_shape) static_body.add_child(m) add_child(static_body) var tree_prefab = preload("res://prefab/tree.tscn") var space = get_world_3d().direct_space_state var random = RandomNumberGenerator.new() random.randomize() var num_trees = random.randi_range(5, 15) for i in num_trees: var cast_pos = Vector3(random.randf_range(0, width*scale_x), height*scale_y, random.randf_range(0, depth*scale_z)) var cast_dest = Vector3(cast_pos.x, 0, cast_pos.z) var query = PhysicsRayQueryParameters3D.create(cast_pos, cast_dest) var result = space.intersect_ray(query) if result: var pos = result.position var new_tree = tree_prefab.instantiate() new_tree.position = pos add_child(new_tree) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(_delta): pass