@@ -9,14 +9,17 @@ const TEXTURE_SHEET_WIDTH = 8
9
9
10
10
const CHUNK_LAST_INDEX = CHUNK_SIZE - 1
11
11
const TEXTURE_TILE_SIZE = 1.0 / TEXTURE_SHEET_WIDTH
12
+ const DIRECTIONS : Array [Vector3i ] = [Vector3i .LEFT , Vector3i .RIGHT , Vector3i .DOWN , Vector3i .UP , Vector3i .FORWARD , Vector3i .BACK ]
12
13
13
14
var data := {}
14
15
var chunk_position := Vector3i ()
16
+ var is_initial_mesh_generated : bool = false
15
17
16
18
var _thread : Thread
17
19
18
20
@onready var voxel_world := get_parent ()
19
21
22
+
20
23
func _ready () -> void :
21
24
transform .origin = Vector3 (chunk_position * CHUNK_SIZE )
22
25
name = str (chunk_position )
@@ -27,7 +30,14 @@ func _ready() -> void:
27
30
28
31
# We can only add colliders in the main thread due to physics limitations.
29
32
_generate_chunk_collider ()
30
- # However, we can use a thread for mesh generation.
33
+
34
+
35
+ func try_initial_generate_mesh (all_chunks : Dictionary [Vector3i , Chunk ]) -> void :
36
+ # We can use a thread for mesh generation.
37
+ for dir in DIRECTIONS :
38
+ if not all_chunks .has (chunk_position + dir ):
39
+ return
40
+ is_initial_mesh_generated = true
31
41
_thread = Thread .new ()
32
42
_thread .start (_generate_chunk_mesh )
33
43
@@ -73,7 +83,6 @@ func _generate_chunk_mesh() -> void:
73
83
_draw_block_mesh (surface_tool , block_position , block_id )
74
84
75
85
# Create the chunk's mesh from the SurfaceTool data.
76
- surface_tool .generate_normals ()
77
86
surface_tool .generate_tangents ()
78
87
surface_tool .index ()
79
88
var array_mesh := surface_tool .commit ()
@@ -91,10 +100,10 @@ func _draw_block_mesh(surface_tool: SurfaceTool, block_sub_position: Vector3i, b
91
100
92
101
# Bush blocks get drawn in their own special way.
93
102
if block_id == 27 or block_id == 28 :
94
- _draw_block_face (surface_tool , [verts [2 ], verts [0 ], verts [7 ], verts [5 ]], uvs )
95
- _draw_block_face (surface_tool , [verts [7 ], verts [5 ], verts [2 ], verts [0 ]], uvs )
96
- _draw_block_face (surface_tool , [verts [3 ], verts [1 ], verts [6 ], verts [4 ]], uvs )
97
- _draw_block_face (surface_tool , [verts [6 ], verts [4 ], verts [3 ], verts [1 ]], uvs )
103
+ _draw_block_face (surface_tool , [verts [2 ], verts [0 ], verts [7 ], verts [5 ]], uvs , Vector3 ( - 1 , 0 , 1 ). normalized () )
104
+ _draw_block_face (surface_tool , [verts [7 ], verts [5 ], verts [2 ], verts [0 ]], uvs , Vector3 ( 1 , 0 , - 1 ). normalized () )
105
+ _draw_block_face (surface_tool , [verts [3 ], verts [1 ], verts [6 ], verts [4 ]], uvs , Vector3 ( 1 , 0 , 1 ). normalized () )
106
+ _draw_block_face (surface_tool , [verts [6 ], verts [4 ], verts [3 ], verts [1 ]], uvs , Vector3 ( - 1 , 0 , - 1 ). normalized () )
98
107
return
99
108
100
109
# Allow some blocks to have different top/bottom textures.
@@ -112,62 +121,76 @@ func _draw_block_mesh(surface_tool: SurfaceTool, block_sub_position: Vector3i, b
112
121
bottom_uvs = top_uvs
113
122
114
123
# Main rendering code for normal blocks.
115
- var other_block_position := block_sub_position + Vector3i . LEFT
124
+ # var other_block_position := block_sub_position
116
125
var other_block_id := 0
117
- if other_block_position .x == - 1 :
118
- other_block_id = voxel_world .get_block_global_position (other_block_position + chunk_position * CHUNK_SIZE )
119
- elif data .has (other_block_position ):
120
- other_block_id = data [other_block_position ]
126
+ if block_sub_position .x == 0 :
127
+ var other_sub_pos : Vector3i = Vector3i (15 , block_sub_position .y , block_sub_position .z )
128
+ other_block_id = voxel_world .get_block_in_chunk (chunk_position + Vector3i .LEFT , other_sub_pos )
129
+ else :
130
+ var other_block_sub_pos : Vector3i = block_sub_position + Vector3i .LEFT
131
+ if data .has (other_block_sub_pos ):
132
+ other_block_id = data [other_block_sub_pos ]
121
133
if block_id != other_block_id and Chunk .is_block_transparent (other_block_id ):
122
- _draw_block_face (surface_tool , [verts [2 ], verts [0 ], verts [3 ], verts [1 ]], uvs )
134
+ _draw_block_face (surface_tool , [verts [2 ], verts [0 ], verts [3 ], verts [1 ]], uvs , Vector3 . LEFT )
123
135
124
- other_block_position = block_sub_position + Vector3i .RIGHT
125
136
other_block_id = 0
126
- if other_block_position .x == CHUNK_SIZE :
127
- other_block_id = voxel_world .get_block_global_position (other_block_position + chunk_position * CHUNK_SIZE )
128
- elif data .has (other_block_position ):
129
- other_block_id = data [other_block_position ]
137
+ if block_sub_position .x == CHUNK_SIZE - 1 :
138
+ var other_sub_pos : Vector3i = Vector3i (0 , block_sub_position .y , block_sub_position .z )
139
+ other_block_id = voxel_world .get_block_in_chunk (chunk_position + Vector3i .RIGHT , other_sub_pos )
140
+ else :
141
+ var other_block_sub_pos : Vector3i = block_sub_position + Vector3i .RIGHT
142
+ if data .has (other_block_sub_pos ):
143
+ other_block_id = data [other_block_sub_pos ]
130
144
if block_id != other_block_id and Chunk .is_block_transparent (other_block_id ):
131
- _draw_block_face (surface_tool , [verts [7 ], verts [5 ], verts [6 ], verts [4 ]], uvs )
145
+ _draw_block_face (surface_tool , [verts [7 ], verts [5 ], verts [6 ], verts [4 ]], uvs , Vector3 . RIGHT )
132
146
133
- other_block_position = block_sub_position + Vector3i .FORWARD
134
147
other_block_id = 0
135
- if other_block_position .z == - 1 :
136
- other_block_id = voxel_world .get_block_global_position (other_block_position + chunk_position * CHUNK_SIZE )
137
- elif data .has (other_block_position ):
138
- other_block_id = data [other_block_position ]
148
+ if block_sub_position .z == 0 :
149
+ var other_sub_pos : Vector3i = Vector3i (block_sub_position .x , block_sub_position .y , CHUNK_SIZE - 1 )
150
+ other_block_id = voxel_world .get_block_in_chunk (chunk_position + Vector3i .FORWARD , other_sub_pos )
151
+ else :
152
+ var other_block_sub_pos : Vector3i = block_sub_position + Vector3i .FORWARD
153
+ if data .has (other_block_sub_pos ):
154
+ other_block_id = data [other_block_sub_pos ]
139
155
if block_id != other_block_id and Chunk .is_block_transparent (other_block_id ):
140
- _draw_block_face (surface_tool , [verts [6 ], verts [4 ], verts [2 ], verts [0 ]], uvs )
156
+ _draw_block_face (surface_tool , [verts [6 ], verts [4 ], verts [2 ], verts [0 ]], uvs , Vector3 . FORWARD )
141
157
142
- other_block_position = block_sub_position + Vector3i .BACK
143
158
other_block_id = 0
144
- if other_block_position .z == CHUNK_SIZE :
145
- other_block_id = voxel_world .get_block_global_position (other_block_position + chunk_position * CHUNK_SIZE )
146
- elif data .has (other_block_position ):
147
- other_block_id = data [other_block_position ]
159
+ if block_sub_position .z == CHUNK_SIZE - 1 :
160
+ var other_sub_pos : Vector3i = Vector3i (block_sub_position .x , block_sub_position .y , 0 )
161
+ other_block_id = voxel_world .get_block_in_chunk (chunk_position + Vector3i .BACK , other_sub_pos )
162
+ else :
163
+ var other_block_sub_pos : Vector3i = block_sub_position + Vector3i .BACK
164
+ if data .has (other_block_sub_pos ):
165
+ other_block_id = data [other_block_sub_pos ]
148
166
if block_id != other_block_id and Chunk .is_block_transparent (other_block_id ):
149
- _draw_block_face (surface_tool , [verts [3 ], verts [1 ], verts [7 ], verts [5 ]], uvs )
167
+ _draw_block_face (surface_tool , [verts [3 ], verts [1 ], verts [7 ], verts [5 ]], uvs , Vector3 . BACK )
150
168
151
- other_block_position = block_sub_position + Vector3i .DOWN
152
169
other_block_id = 0
153
- if other_block_position .y == - 1 :
154
- other_block_id = voxel_world .get_block_global_position (other_block_position + chunk_position * CHUNK_SIZE )
155
- elif data .has (other_block_position ):
156
- other_block_id = data [other_block_position ]
170
+ if block_sub_position .y == 0 :
171
+ var other_sub_pos : Vector3i = Vector3i (block_sub_position .x , CHUNK_SIZE - 1 , block_sub_position .z )
172
+ other_block_id = voxel_world .get_block_in_chunk (chunk_position + Vector3i .DOWN , other_sub_pos )
173
+ else :
174
+ var other_block_sub_pos : Vector3i = block_sub_position + Vector3i .DOWN
175
+ if data .has (other_block_sub_pos ):
176
+ other_block_id = data [other_block_sub_pos ]
157
177
if block_id != other_block_id and Chunk .is_block_transparent (other_block_id ):
158
- _draw_block_face (surface_tool , [verts [4 ], verts [5 ], verts [0 ], verts [1 ]], bottom_uvs )
178
+ _draw_block_face (surface_tool , [verts [4 ], verts [5 ], verts [0 ], verts [1 ]], bottom_uvs , Vector3 . DOWN )
159
179
160
- other_block_position = block_sub_position + Vector3i .UP
161
180
other_block_id = 0
162
- if other_block_position .y == CHUNK_SIZE :
163
- other_block_id = voxel_world .get_block_global_position (other_block_position + chunk_position * CHUNK_SIZE )
164
- elif data .has (other_block_position ):
165
- other_block_id = data [other_block_position ]
181
+ if block_sub_position .y == CHUNK_SIZE - 1 :
182
+ var other_sub_pos : Vector3i = Vector3i (block_sub_position .x , 0 , block_sub_position .z )
183
+ other_block_id = voxel_world .get_block_in_chunk (chunk_position + Vector3i .UP , other_sub_pos )
184
+ else :
185
+ var other_block_sub_pos : Vector3i = block_sub_position + Vector3i .UP
186
+ if data .has (other_block_sub_pos ):
187
+ other_block_id = data [other_block_sub_pos ]
166
188
if block_id != other_block_id and Chunk .is_block_transparent (other_block_id ):
167
- _draw_block_face (surface_tool , [verts [2 ], verts [3 ], verts [6 ], verts [7 ]], top_uvs )
189
+ _draw_block_face (surface_tool , [verts [2 ], verts [3 ], verts [6 ], verts [7 ]], top_uvs , Vector3 . UP )
168
190
169
191
170
- func _draw_block_face (surface_tool : SurfaceTool , verts : Array [Vector3 ], uvs : Array [Vector2 ]) -> void :
192
+ func _draw_block_face (surface_tool : SurfaceTool , verts : Array [Vector3 ], uvs : Array [Vector2 ], normal : Vector3 ) -> void :
193
+ surface_tool .set_normal (normal )
171
194
surface_tool .set_uv (uvs [1 ]); surface_tool .add_vertex (verts [1 ])
172
195
surface_tool .set_uv (uvs [2 ]); surface_tool .add_vertex (verts [2 ])
173
196
surface_tool .set_uv (uvs [3 ]); surface_tool .add_vertex (verts [3 ])
0 commit comments