Skip to content

Instantly share code, notes, and snippets.

@TheNeikos
Last active May 5, 2021 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheNeikos/3d18ddf811cc0aea1877eff4aa4b9195 to your computer and use it in GitHub Desktop.
Save TheNeikos/3d18ddf811cc0aea1877eff4aa4b9195 to your computer and use it in GitHub Desktop.
Ambient Occlusion as shown by 0FPS
// This code is licensed under CC0
//
//
// The code assumes that your meshing goes in clockwise order, starting at the top left
//
// TL -> TR
// ^ v
// BL <- BR
// `alt_axis` and `alt2_axis` are `u` and `v` respectively
fn face_visible_on_axis(block_pos: BlockPos, face: GlobalBlockFace) -> bool {
// Check if the face of the given block at that position occludes the given face
}
// We look for sides, starting with the top
let side_presence: [bool; 4] = let mut side_presence = [
{
let block_pos = block_pos.add_axis(alt_axis, 1);
face_visible_on_axis(block_pos, face_direction)
},
{
let block_pos = block_pos.sub_axis(alt2_axis, 1);
face_visible_on_axis(block_pos, face_direction)
},
{
let block_pos = block_pos.sub_axis(alt_axis, 1);
face_visible_on_axis(block_pos, face_direction)
},
{
let block_pos = block_pos.add_axis(alt2_axis, 1);
face_visible_on_axis(block_pos, face_direction)
},
];
let corner_presence = [ /* Do the same thing for corners, starting with the top left corner */ ]
match face_direction {
GlobalBlockFace::NegativeX => {
side_presence.mirror_vertically();
corner_presence.swap_bottom().swap_top();
}
GlobalBlockFace::NegativeY => {
side_presence.mirror_vertically();
corner_presence.swap_bottom().swap_top();
}
GlobalBlockFace::NegativeZ => {
side_presence.mirror_vertically();
corner_presence.swap_bottom().swap_top();
}
GlobalBlockFace::PositiveX => {
side_presence.rotate_clockwise();
corner_presence.rotate_clockwise();
}
GlobalBlockFace::PositiveY => {
side_presence.rotate_clockwise();
corner_presence.rotate_clockwise();
}
GlobalBlockFace::PositiveZ => {
side_presence.rotate_clockwise();
corner_presence.rotate_clockwise();
}
}
let mut occlusion: [usize; 4] = [3; 4];
for i in 0..4usize {
occlusion[i] = occlusion_factor(
side_presence[(i + 3) % 4],
side_presence[i],
corner_presence[i],
);
}
let occlusion_factor = |side1, side2, corner| {
if side1 && side2 {
return 0;
}
return 3 - (side1 as u32 + side2 as u32 + corner as u32);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment