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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use std::{sync::Arc, time::Instant};

use crossbeam_channel::{unbounded, Receiver, Sender, TryRecvError};
use hashbrown::HashMap;
use itertools::izip;
use log::info;
use rayon::{ThreadPool, ThreadPoolBuilder};

use crate::{
    Block, BlockFace, BlockRotation, Chunk, CornerData, Geometry, LightUtils, MeshProtocol,
    Registry, Space, Vec3, VoxelAccess, WorldConfig, UV,
};

use super::lights::Lights;

fn vertex_ao(side1: bool, side2: bool, corner: bool) -> i32 {
    let num_s1 = !side1 as i32;
    let num_s2 = !side2 as i32;
    let num_c = !corner as i32;

    if num_s1 == 1 && num_s2 == 1 {
        0
    } else {
        3 - (num_s1 + num_s2 + num_c)
    }
}

fn get_block_by_voxel<'a>(
    vx: i32,
    vy: i32,
    vz: i32,
    space: &Space,
    registry: &'a Registry,
) -> &'a Block {
    registry.get_block_by_id(space.get_voxel(vx, vy, vz))
}

/// A meshing helper to mesh chunks.
pub struct Mesher {
    sender: Arc<Sender<Vec<Chunk>>>,
    receiver: Arc<Receiver<Vec<Chunk>>>,
    pool: ThreadPool,
}

impl Mesher {
    /// Create a new chunk meshing system.
    pub fn new() -> Self {
        let (sender, receiver) = unbounded();

        Self {
            sender: Arc::new(sender),
            receiver: Arc::new(receiver),
            pool: ThreadPoolBuilder::new()
                .thread_name(|index| format!("chunk-meshing-{index}"))
                .build()
                .unwrap(),
        }
    }

    pub fn process(
        &mut self,
        processes: Vec<(Chunk, Space)>,
        registry: &Registry,
        config: &WorldConfig,
    ) {
        let sender = Arc::clone(&self.sender);

        let registry = registry.to_owned();
        let config = config.to_owned();

        self.pool.spawn(move || {
            let chunks: Vec<Chunk> = processes
                .into_iter()
                .map(|(mut chunk, mut space)| {
                    if chunk.meshes.is_none() {
                        let min = space.min.to_owned();
                        let coords = space.coords.to_owned();
                        let shape = space.shape.to_owned();

                        chunk.lights = Lights::propagate(
                            &mut space, &min, &coords, &shape, &registry, &config,
                        );
                    }

                    let sub_chunks = chunk.updated_levels.to_owned();

                    space.updated_levels.clear();
                    chunk.updated_levels.clear();

                    let Vec3(min_x, min_y, min_z) = chunk.min;
                    let Vec3(max_x, _, max_z) = chunk.max;

                    let blocks_per_sub_chunk =
                        (space.params.max_height / space.params.sub_chunks) as i32;

                    sub_chunks.into_iter().for_each(|level| {
                        let level = level as i32;

                        let min = Vec3(min_x, min_y + level * blocks_per_sub_chunk, min_z);
                        let max = Vec3(max_x, min_y + (level + 1) * blocks_per_sub_chunk, max_z);

                        let opaque = Self::mesh_space(&min, &max, &space, &registry, false);
                        let transparent = Self::mesh_space(&min, &max, &space, &registry, true);

                        if chunk.meshes.is_none() {
                            chunk.meshes = Some(HashMap::new());
                        }

                        chunk.meshes.as_mut().unwrap().insert(
                            level as u32,
                            MeshProtocol {
                                level,
                                opaque,
                                transparent,
                            },
                        );
                    });

                    chunk
                })
                .collect();

            sender.send(chunks).unwrap();
        })
    }

    /// Attempt to retrieve the results from `pipeline.process`
    pub fn results(&self) -> Result<Vec<Chunk>, TryRecvError> {
        self.receiver.try_recv()
    }

    /// Mesh a Space struct from specified voxel coordinates, generating the 3D data needed
    /// to render a chunk/space.
    // #[allow(clippy::all)]
    pub fn mesh_space(
        min: &Vec3<i32>,
        max: &Vec3<i32>,
        space: &Space,
        registry: &Registry,
        transparent: bool,
    ) -> Option<Geometry> {
        let mut positions = Vec::<f32>::new();
        let mut indices = Vec::<i32>::new();
        let mut uvs = Vec::<f32>::new();
        let mut lights = Vec::<i32>::new();

        let &Vec3(min_x, min_y, min_z) = min;
        let &Vec3(max_x, max_y, max_z) = max;

        for vx in min_x..max_x {
            for vz in min_z..max_z {
                let height = space.get_max_height(vx, vz) as i32;

                if min_y > height {
                    continue;
                }

                let mut process_face =
                    |vx: i32,
                     vy: i32,
                     vz: i32,
                     voxel_id: u32,
                     rotation: &BlockRotation,
                     face: &BlockFace,
                     block: &Block,
                     uv_map: &HashMap<String, &UV>| {
                        let &Block {
                            is_fluid,
                            is_transparent,
                            is_full_block,
                            rotatable,
                            ..
                        } = block;
                        let BlockFace { dir, corners, .. } = face;

                        let mut dir = [dir[0] as f32, dir[1] as f32, dir[2] as f32];

                        if rotatable {
                            rotation.rotate(&mut dir, false);
                        }

                        let dir = [
                            dir[0].round() as i32,
                            dir[1].round() as i32,
                            dir[2].round() as i32,
                        ];

                        let nvx = vx + dir[0];
                        let nvy = vy + dir[1];
                        let nvz = vz + dir[2];

                        let is_void = !space.contains(nvx, nvy, nvz);
                        let neighbor_id = space.get_voxel(nvx, nvy, nvz);
                        let n_block_type = registry.get_block_by_id(neighbor_id);

                        if is_void
                            || (!is_full_block && !is_transparent)
                            || (!n_block_type.is_full_block && !n_block_type.is_transparent)
                            || ((n_block_type.is_transparent && !n_block_type.is_fluid)
                                || (n_block_type.is_fluid && !is_fluid))
                                && (!transparent
                                    || n_block_type.is_empty
                                    || neighbor_id != voxel_id
                                    || (n_block_type.transparent_standalone
                                        && (dir[0] + dir[1] + dir[2]) as i32 >= 1))
                        {
                            let UV {
                                start_u,
                                end_u,
                                start_v,
                                end_v,
                            } = uv_map.get(&face.name).unwrap();

                            let ndx = (positions.len() as f32 / 3.0).floor() as i32;
                            let mut face_aos = vec![];

                            let mut four_sunlights = vec![];
                            let mut four_red_lights = vec![];
                            let mut four_green_lights = vec![];
                            let mut four_blue_lights = vec![];

                            for CornerData { mut pos, uv } in corners.iter() {
                                if rotatable {
                                    rotation.rotate(&mut pos, true);
                                }

                                let pos_x = pos[0] + vx as f32;
                                let pos_y = pos[1] + vy as f32;
                                let pos_z = pos[2] + vz as f32;

                                let scale = if !is_full_block { 0.0001 } else { 0.0 };
                                positions.push(pos_x - min_x as f32 - dir[0] as f32 * scale);
                                positions.push(pos_y - dir[1] as f32 * scale);
                                positions.push(pos_z - min_z as f32 - dir[2] as f32 * scale);

                                uvs.push(uv[0] * (end_u - start_u) + start_u);
                                uvs.push(uv[1] * (end_v - start_v) + start_v);

                                // calculating the 8 voxels around this vertex
                                let dx = pos[0].round() as i32;
                                let dy = pos[1].round() as i32;
                                let dz = pos[2].round() as i32;

                                let dx = if dx == 0 {
                                    -1
                                } else if dx == 1 {
                                    1
                                } else {
                                    0
                                };
                                let dy = if dy == 0 {
                                    -1
                                } else if dy == 1 {
                                    1
                                } else {
                                    0
                                };
                                let dz = if dz == 0 {
                                    -1
                                } else if dz == 1 {
                                    1
                                } else {
                                    0
                                };

                                let mut sum_sunlight = vec![];
                                let mut sum_red_lights = vec![];
                                let mut sum_green_lights = vec![];
                                let mut sum_blue_lights = vec![];

                                let b011 =
                                    get_block_by_voxel(vx, vy + dy, vz + dz, space, registry);
                                let b011 = b011.is_transparent || !b011.is_full_block;
                                let b101 =
                                    get_block_by_voxel(vx + dx, vy, vz + dz, space, registry);
                                let b101 = b101.is_transparent || !b101.is_full_block;
                                let b110 =
                                    get_block_by_voxel(vx + dx, vy + dy, vz, space, registry);
                                let b110 = b110.is_transparent || !b110.is_full_block;
                                let b111 =
                                    get_block_by_voxel(vx + dx, vy + dy, vz + dz, space, registry);
                                let b111 = b111.is_transparent || !b111.is_full_block;

                                if is_transparent {
                                    face_aos.push(3)
                                } else if dir[0].abs() == 1 {
                                    face_aos.push(vertex_ao(b110, b101, b111));
                                } else if dir[1].abs() == 1 {
                                    face_aos.push(vertex_ao(b110, b011, b111));
                                } else {
                                    face_aos.push(vertex_ao(b011, b101, b111));
                                }

                                if is_transparent {
                                    let [dx, dy, dz] = dir;

                                    sum_sunlight.push(space.get_sunlight(
                                        vx + dx,
                                        vy + dy,
                                        vz + dz,
                                    ));
                                    sum_red_lights.push(space.get_raw_light(
                                        vx + dx,
                                        vy + dy,
                                        vz + dz,
                                    ));
                                    sum_green_lights.push(space.get_green_light(
                                        vx + dx,
                                        vy + dy,
                                        vz + dz,
                                    ));
                                    sum_blue_lights.push(space.get_blue_light(
                                        vx + dx,
                                        vy + dy,
                                        vz + dz,
                                    ));
                                } else {
                                    // Loop through all 8 neighbors of this vertex.
                                    for ddx in if dx > 0 { 0..=dx } else { dx..=0 } {
                                        for ddy in if dy > 0 { 0..=dy } else { dy..=0 } {
                                            for ddz in if dz > 0 { 0..=dz } else { dz..=0 } {
                                                // The block that we're checking is on the side that the face is facing, so
                                                // check if the diagonal block would be blocking this block's potential light.
                                                // Perpendicular check done by crossing the vectors.
                                                if dir[0] * ddx + dir[1] * ddy + dir[2] * ddz == 0 {
                                                    let facing = get_block_by_voxel(
                                                        vx + ddx * dir[0],
                                                        vy + ddy * dir[1],
                                                        vz + ddz * dir[2],
                                                        space,
                                                        registry,
                                                    );

                                                    if !facing.is_transparent
                                                        && facing.is_full_block
                                                    {
                                                        continue;
                                                    }
                                                }

                                                // Diagonal light leaking fix.
                                                if ddx.abs() + ddy.abs() + ddz.abs() == 3 {
                                                    let diagonal_yz = get_block_by_voxel(
                                                        vx,
                                                        vy + ddy,
                                                        vz + ddz,
                                                        space,
                                                        registry,
                                                    );
                                                    let diagonal_xz = get_block_by_voxel(
                                                        vx + ddx,
                                                        vy,
                                                        vz + ddz,
                                                        space,
                                                        registry,
                                                    );
                                                    let diagonal_xy = get_block_by_voxel(
                                                        vx + ddx,
                                                        vy + ddy,
                                                        vz,
                                                        space,
                                                        registry,
                                                    );

                                                    if (!diagonal_yz.is_transparent
                                                        && diagonal_yz.is_full_block)
                                                        && (!diagonal_xz.is_transparent
                                                            && diagonal_xz.is_full_block)
                                                        && (!diagonal_xy.is_transparent
                                                            && diagonal_xy.is_full_block)
                                                    {
                                                        continue;
                                                    }
                                                }

                                                let diagonal4 = get_block_by_voxel(
                                                    vx + ddx,
                                                    vy + ddy,
                                                    vz + ddz,
                                                    space,
                                                    registry,
                                                );

                                                let is_transparent = diagonal4.is_transparent
                                                    || !diagonal4.is_full_block;

                                                if is_transparent {
                                                    sum_sunlight.push(space.get_sunlight(
                                                        vx + ddx,
                                                        vy + ddy,
                                                        vz + ddz,
                                                    ));
                                                    sum_red_lights.push(space.get_red_light(
                                                        vx + ddx,
                                                        vy + ddy,
                                                        vz + ddz,
                                                    ));
                                                    sum_green_lights.push(space.get_green_light(
                                                        vx + ddx,
                                                        vy + ddy,
                                                        vz + ddz,
                                                    ));
                                                    sum_blue_lights.push(space.get_blue_light(
                                                        vx + ddx,
                                                        vy + ddy,
                                                        vz + ddz,
                                                    ));
                                                }
                                            }
                                        }
                                    }
                                }

                                four_sunlights.push(
                                    (sum_sunlight.iter().sum::<u32>() as f32
                                        / sum_sunlight.len() as f32)
                                        as i32,
                                );

                                four_red_lights.push(
                                    (sum_red_lights.iter().sum::<u32>() as f32
                                        / sum_red_lights.len() as f32)
                                        as i32,
                                );

                                four_green_lights.push(
                                    (sum_green_lights.iter().sum::<u32>() as f32
                                        / sum_green_lights.len() as f32)
                                        as i32,
                                );

                                four_blue_lights.push(
                                    (sum_blue_lights.iter().sum::<u32>() as f32
                                        / sum_blue_lights.len() as f32)
                                        as i32,
                                );
                            }

                            let a_rt = four_red_lights[0];
                            let b_rt = four_red_lights[1];
                            let c_rt = four_red_lights[2];
                            let d_rt = four_red_lights[3];

                            let a_gt = four_green_lights[0];
                            let b_gt = four_green_lights[1];
                            let c_gt = four_green_lights[2];
                            let d_gt = four_green_lights[3];

                            let a_bt = four_blue_lights[0];
                            let b_bt = four_blue_lights[1];
                            let c_bt = four_blue_lights[2];
                            let d_bt = four_blue_lights[3];

                            let threshold = 0;

                            /* -------------------------------------------------------------------------- */
                            /*                     I KNOW THIS IS UGLY, BUT IT WORKS!                     */
                            /* -------------------------------------------------------------------------- */
                            // at least one zero
                            let one_tr0 = a_rt <= threshold
                                || b_rt <= threshold
                                || c_rt <= threshold
                                || d_rt <= threshold;
                            let one_tg0 = a_gt <= threshold
                                || b_gt <= threshold
                                || c_gt <= threshold
                                || d_gt <= threshold;
                            let one_tb0 = a_bt <= threshold
                                || b_bt <= threshold
                                || c_bt <= threshold
                                || d_bt <= threshold;
                            // one is zero, and ao rule, but only for zero AO's
                            let fequals =
                                (face_aos[0] + face_aos[3]) == (face_aos[1] + face_aos[2]);
                            let ozao_r = a_rt + d_rt < b_rt + c_rt && fequals;
                            let ozao_g = a_gt + d_gt < b_gt + c_gt && fequals;
                            let ozao_b = a_bt + d_bt < b_bt + c_bt && fequals;
                            // all not zero, 4 parts
                            let anzp1_r = (b_rt as f32 > (a_rt + d_rt) as f32 / 2.0
                                && (a_rt + d_rt) as f32 / 2.0 > c_rt as f32)
                                || (c_rt as f32 > (a_rt + d_rt) as f32 / 2.0
                                    && (a_rt + d_rt) as f32 / 2.0 > b_rt as f32);
                            let anzp1_g = (b_gt as f32 > (a_gt + d_gt) as f32 / 2.0
                                && (a_gt + d_gt) as f32 / 2.0 > c_gt as f32)
                                || (c_gt as f32 > (a_gt + d_gt) as f32 / 2.0
                                    && (a_gt + d_gt) as f32 / 2.0 > b_gt as f32);
                            let anzp1_b = (b_bt as f32 > (a_bt + d_bt) as f32 / 2.0
                                && (a_bt + d_bt) as f32 / 2.0 > c_bt as f32)
                                || (c_bt as f32 > (a_bt + d_bt) as f32 / 2.0
                                    && (a_bt + d_bt) as f32 / 2.0 > b_bt as f32);
                            // fixed two light sources colliding
                            let anz_r = one_tr0 && anzp1_r;
                            let anz_g = one_tg0 && anzp1_g;
                            let anz_b = one_tb0 && anzp1_b;

                            // common starting indices
                            indices.push(ndx);
                            indices.push(ndx + 1);

                            if face_aos[0] + face_aos[3] > face_aos[1] + face_aos[2]
                                || (ozao_r || ozao_g || ozao_b)
                                || (anz_r || anz_g || anz_b)
                            {
                                // generate flipped quad
                                indices.push(ndx + 3);
                                indices.push(ndx + 3);
                                indices.push(ndx + 2);
                                indices.push(ndx);
                            } else {
                                indices.push(ndx + 2);
                                indices.push(ndx + 2);
                                indices.push(ndx + 1);
                                indices.push(ndx + 3);
                            }

                            let mut ao_i = 0;
                            for (s, r, g, b) in izip!(
                                &four_sunlights,
                                &four_red_lights,
                                &four_green_lights,
                                &four_blue_lights
                            ) {
                                let mut light = 0;
                                light = LightUtils::insert_red_light(light, *r as u32);
                                light = LightUtils::insert_green_light(light, *g as u32);
                                light = LightUtils::insert_blue_light(light, *b as u32);
                                light = LightUtils::insert_sunlight(light, *s as u32);
                                lights.push(light as i32 | face_aos[ao_i] << 16);
                                ao_i += 1;
                            }
                        }
                    };

                for vy in (min_y..=max_y.min(height) as i32).rev() {
                    let voxel_id = space.get_voxel(vx, vy, vz);
                    let rotation = space.get_voxel_rotation(vx, vy, vz);
                    let block = registry.get_block_by_id(voxel_id);

                    let Block {
                        is_transparent,
                        is_block,
                        ..
                    } = block.to_owned();

                    if if transparent {
                        is_transparent
                    } else {
                        !is_transparent
                    } {
                        if is_block {
                            let Block { faces, .. } = block.to_owned();

                            let uv_map = registry.get_uv_map(block);

                            faces.iter().for_each(|face| {
                                process_face(vx, vy, vz, voxel_id, &rotation, face, block, &uv_map)
                            });
                        }
                    }
                }
            }
        }

        if indices.is_empty() {
            return None;
        }

        Some(Geometry {
            positions,
            indices,
            uvs,
            lights,
        })
    }
}