@@ -164,6 +164,14 @@ pub(crate) mod threadpool {
164
164
}
165
165
}
166
166
167
+ // https://0fps.net/2013/07/03/ambient-occlusion-for-minecraft-like-worlds/
168
+ fn calc_vertex_ao ( side1 : bool , side2 : bool , up : bool ) -> u8 {
169
+ if side1 && side2 {
170
+ return 0 ;
171
+ }
172
+ return 3 - ( side1 as u8 + side2 as u8 + up as u8 ) ;
173
+ }
174
+
167
175
/* Utility traits */
168
176
pub trait ChunkFromPosition {
169
177
fn get_chunk_from_position_absolute ( & self ) -> ( i32 , i32 ) ;
@@ -193,7 +201,52 @@ impl ChunkFromPosition for glam::Vec3 {
193
201
}
194
202
195
203
mod tests {
196
- use crate :: utils:: { ChunkFromPosition , RelativeFromAbsolute } ;
204
+ use crate :: utils:: { calc_vertex_ao, ChunkFromPosition , RelativeFromAbsolute } ;
205
+ use glam:: vec3;
206
+ #[ test]
207
+ fn should_calculate_the_correct_ao ( ) {
208
+ let vertex_position = vec3 ( 0.5 , 0.5 , 0.5 ) ; // Belongs to voxel 0,0,0
209
+ let neighbour_voxels = [ vec3 ( 1.0 , 1.0 , 0.0 ) , vec3 ( 1.0 , 1.0 , 1.0 ) ] ;
210
+
211
+ let has_side1 = neighbour_voxels. contains ( & vec3 (
212
+ f32:: floor ( vertex_position. x + 1.0 ) ,
213
+ f32:: floor ( vertex_position. y + 1.0 ) ,
214
+ f32:: floor ( vertex_position. z + 0.0 ) ,
215
+ ) ) ;
216
+ let has_side2 = neighbour_voxels. contains ( & vec3 (
217
+ f32:: floor ( vertex_position. x + 0.0 ) ,
218
+ f32:: floor ( vertex_position. y + 1.0 ) ,
219
+ f32:: floor ( vertex_position. z + 1.0 ) ,
220
+ ) ) ;
221
+ let has_corner = neighbour_voxels. contains ( & vec3 (
222
+ f32:: floor ( vertex_position. x + 1.0 ) ,
223
+ f32:: floor ( vertex_position. y + 1.0 ) ,
224
+ f32:: floor ( vertex_position. z + 1.0 ) ,
225
+ ) ) ;
226
+ let vao = calc_vertex_ao ( has_side1, has_side2, has_corner) ;
227
+
228
+ assert_eq ! ( vao, 1 ) ;
229
+
230
+ let vertex_position = vec3 ( 0.5 , 0.5 , -0.5 ) ; // Belongs to voxel 0,0,0
231
+
232
+ let has_side1 = neighbour_voxels. contains ( & vec3 (
233
+ f32:: floor ( vertex_position. x + 1.0 ) ,
234
+ f32:: floor ( vertex_position. y + 1.0 ) ,
235
+ f32:: floor ( vertex_position. z + 0.0 ) ,
236
+ ) ) ;
237
+ let has_side2 = neighbour_voxels. contains ( & vec3 (
238
+ f32:: floor ( vertex_position. x + 0.0 ) ,
239
+ f32:: floor ( vertex_position. y + 1.0 ) ,
240
+ f32:: floor ( vertex_position. z + 1.0 ) ,
241
+ ) ) ;
242
+ let has_corner = neighbour_voxels. contains ( & vec3 (
243
+ f32:: floor ( vertex_position. x + 1.0 ) ,
244
+ f32:: floor ( vertex_position. y + 1.0 ) ,
245
+ f32:: floor ( vertex_position. z + 1.0 ) ,
246
+ ) ) ;
247
+ let vao = calc_vertex_ao ( has_side1, has_side2, has_corner) ;
248
+ assert_eq ! ( vao, 2 )
249
+ }
197
250
198
251
#[ test]
199
252
fn should_get_the_correct_chunk_from_position_absolute ( ) {
0 commit comments