Skip to content

Instantly share code, notes, and snippets.

@tdhooper
Last active February 24, 2018 21:23
Show Gist options
  • Save tdhooper/a2dd3a51c8ff3e389ae69927fadf02cb to your computer and use it in GitHub Desktop.
Save tdhooper/a2dd3a51c8ff3e389ae69927fadf02cb to your computer and use it in GitHub Desktop.
SDF Normal calculation
// Slow to compile
vec3 calcNormalA(vec3 pos){
vec3 eps = vec3(.0001,0,0);
vec3 nor = vec3(
map(pos+eps.xyy).dist - map(pos-eps.xyy).dist,
map(pos+eps.yxy).dist - map(pos-eps.yxy).dist,
map(pos+eps.yyx).dist - map(pos-eps.yyx).dist
);
return normalize(nor);
}
// Fast to compile
const int NORMAL_STEPS = 6;
vec3 calcNormalB(vec3 pos){
vec3 eps = vec3(.0001,0,0);
vec3 nor = vec3(0);
float invert = 1.;
for (int i = 0; i < NORMAL_STEPS; i++){
nor += map(pos + eps * invert).dist * eps * invert;
eps = eps.zxy;
invert *= -1.;
}
return normalize(nor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment