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
|
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
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)
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
# Called when the node enters the scene tree for the first time.
func _ready():
gen_map_grid()
var vertices = 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_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)
# 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.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
|