- // Decide which sides to traverse next or to block away
-
- // First, find the near sides that would occlude the far sides
- // * A near side can itself be occluded by a nearby block (the test above ^^)
- // * A near side can be visible but fully opaque by itself (e.g. ground at the 0 level)
-
- // mesh solid sides are +Z-Z+Y-Y+X-X
- // if we are inside the block's coordinates on an axis,
- // treat these sides as opaque, as they should not allow to reach the far sides
- u8 block_inner_sides = (look.X == 0 ? 3 : 0) |
- (look.Y == 0 ? 12 : 0) |
- (look.Z == 0 ? 48 : 0);
-
- // get the mask for the sides that are relevant based on the direction
- u8 near_inner_sides = (look.X > 0 ? 1 : 2) |
- (look.Y > 0 ? 4 : 8) |
- (look.Z > 0 ? 16 : 32);
-
- // This bitset is +Z-Z+Y-Y+X-X (See MapBlockMesh), and axis is XYZ.
- // Get he block's transparent sides
- u8 transparent_sides = (occlusion_culling_enabled && block) ? ~block->solid_sides : 0x3F;
-
- // compress block transparent sides to ZYX mask of see-through axes
- u8 near_transparency = (block_inner_sides == 0x3F) ? near_inner_sides : (transparent_sides & near_inner_sides);
-
- // when we are inside the camera block, do not block any sides
- if (block_inner_sides == 0x3F)
- block_inner_sides = 0;
-
- near_transparency &= ~block_inner_sides & 0x3F;
-
- near_transparency |= (near_transparency >> 1);
- near_transparency = (near_transparency & 1) |
- ((near_transparency >> 1) & 2) |
- ((near_transparency >> 2) & 4);
-
- // combine with known visible sides that matter
- near_transparency &= visible_outer_sides;
-
- // The rule for any far side to be visible:
- // * Any of the adjacent near sides is transparent (different axes)
- // * The opposite near side (same axis) is transparent, if it is the dominant axis of the look vector
-
- // Calculate vector from camera to mapblock center. Because we only need relation between
- // coordinates we scale by 2 to avoid precision loss.
- v3s16 precise_look = 2 * (block_pos_nodes - cam_pos_nodes) + MAP_BLOCKSIZE - 1;
-
- // dominant axis flag
- u8 dominant_axis = (abs(precise_look.X) > abs(precise_look.Y) && abs(precise_look.X) > abs(precise_look.Z)) |
- ((abs(precise_look.Y) > abs(precise_look.Z) && abs(precise_look.Y) > abs(precise_look.X)) << 1) |
- ((abs(precise_look.Z) > abs(precise_look.X) && abs(precise_look.Z) > abs(precise_look.Y)) << 2);
-
- // Queue next blocks for processing:
- // - Examine "far" sides of the current blocks, i.e. never move towards the camera
- // - Only traverse the sides that are not occluded
- // - Only traverse the sides that are not opaque
- // When queueing, mark the relevant side on the next block as 'visible'
- for (s16 axis = 0; axis < 3; axis++) {
-
- // Select a bit from transparent_sides for the side
- u8 far_side_mask = 1 << (2 * axis);
-
- // axis flag
- u8 my_side = 1 << axis;
- u8 adjacent_sides = my_side ^ 0x07;
-
- auto traverse_far_side = [&](s8 next_pos_offset) {
- // far side is visible if adjacent near sides are transparent, or if opposite side on dominant axis is transparent
- bool side_visible = ((near_transparency & adjacent_sides) | (near_transparency & my_side & dominant_axis)) != 0;
- side_visible = side_visible && ((far_side_mask & transparent_sides) != 0);
-
- v3s16 next_pos = block_coord;
- next_pos[axis] += next_pos_offset;
-
- // If a side is a see-through, mark the next block's side as visible, and queue
- if (side_visible) {
- auto &next_flags = blocks_seen.getChunk(next_pos).getBits(next_pos);
- next_flags |= my_side;
- blocks_to_consider.push(next_pos);
- }
- else {
- sides_skipped++;
- }
- };