Last active
October 11, 2024 00:25
-
-
Save tmpvar/437363604061ac0cfd1bdd8c41dc5bbe to your computer and use it in GitHub Desktop.
Branchless 3D DDA cursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <glm/glm.hpp> | |
using namespace glm; | |
struct DDACursor { | |
vec3 mask; | |
vec3 mapPos; | |
vec3 rayStep; | |
vec3 rayDir; | |
vec3 rayPos; | |
vec3 sideDist; | |
vec3 deltaDist; | |
DDACursor(){} | |
DDACursor(vec3 pos, vec3 rayDir) { | |
rayPos = pos; | |
rayDir = rayDir; | |
mapPos = glm::floor(pos); | |
deltaDist = glm::abs(vec3(glm::length(rayDir)) / rayDir); | |
rayStep = glm::sign(rayDir); | |
vec3 p = (mapPos - pos); | |
sideDist = ( | |
rayStep * p + (rayStep * 0.5f) + 0.5f | |
) * deltaDist; | |
compute_mask(); | |
} | |
void compute_mask() { | |
mask = glm::step(sideDist, vec3(sideDist.y, sideDist.z, sideDist.x)) * | |
glm::step(sideDist, vec3(sideDist.z, sideDist.x, sideDist.y)); | |
} | |
void step() { | |
compute_mask(); | |
sideDist += mask * deltaDist; | |
mapPos += mask * rayStep; | |
rayPos += mask * rayStep; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice snippet! Guessing this is sourced by: https://www.shadertoy.com/view/4dX3zl?fbclid=IwAR2nfmGTTCNqLIm23aUXtXJvPEwZ4SOQufoGSnBwlmDocOaCmf9cOhQNA_U
?