1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
extends Node3D
@export var width: int = 20
@export var height: int = 10
@export var depth: int = 20
@export var scale_x: int = 10
@export var scale_y: int = 6
@export var scale_z: int = 10
@export var radius: int = 5
@export var smooth_normal: bool = false
var map_grid: Array[float] = []
var random = RandomNumberGenerator.new()
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(1.0)
var noise = FastNoiseLite.new()
noise.noise_type = FastNoiseLite.TYPE_SIMPLEX
noise.frequency = 0.06
#noise.set_seed(0xDEADBEEF)
noise.set_seed(random.randi())
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)
gen_h = int(gen_h*multiplier*0.5)
for y in gen_h:
map_grid[z*height*width + y*width + x] = -100.0
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[tri_index]
var vert = vertices[vertex_index]
if orig_vert.is_equal_approx(vert):
normal_list[tri_index] += normal_list[vertex_index]
vertex_index += 1
func place_objects(num_objects: int, object: PackedScene):
var space = get_world_3d().direct_space_state
var object_num: int = 0
while object_num < num_objects:
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:
# If the object is water, don't place anything
if result.collider == $Water:
continue
# Don't spawn objects on hills that are too steep
if result.normal.dot(Vector3(0, 1, 0)) < 0.8:
continue
var pos = result.position
var new_tree = object.instantiate()
new_tree.position = pos
add_child(new_tree)
object_num += 1
# Called when the node enters the scene tree for the first time.
func _ready():
random.randomize()
gen_map_grid()
var vertices = PackedVector3Array()
var normals = PackedVector3Array()
# 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
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] = 1.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, 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(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[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:
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
normals.push_back(real_normal.normalized())
# Create the Mesh
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.collision_layer = 0b101
static_body.add_child(collision_shape)
static_body.add_child(m)
add_child(static_body)
var tree_prefab = preload("res://prefab/tree.tscn")
var num_trees = random.randi_range(5, 15)
var rock_prefab = preload("res://prefab/rock.tscn")
var num_rocks = random.randi_range(5, 15)
place_objects(num_trees, tree_prefab)
place_objects(num_rocks, rock_prefab)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
|