Last active
February 24, 2018 21:23
-
-
Save tdhooper/a2dd3a51c8ff3e389ae69927fadf02cb to your computer and use it in GitHub Desktop.
SDF Normal calculation
This file contains hidden or 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
| // 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