Last active
August 29, 2015 14:02
-
-
Save doxas/0892d1d0ba9e9b569997 to your computer and use it in GitHub Desktop.
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
precision mediump float; | |
uniform float time; | |
uniform vec2 mouse; | |
uniform vec2 resolution; | |
const float sphereSize = 1.0; | |
float distanceFunc(vec3 p){ | |
return length(p) - sphereSize; | |
} | |
void main(void){ | |
// fragment position | |
vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y); | |
// camera | |
vec3 cPos = vec3(0.0, 0.0, 3.0); | |
vec3 cDir = vec3(0.0, 0.0, -1.0); | |
vec3 cUp = vec3(0.0, 1.0, 0.0); | |
vec3 cSide = cross(cDir, cUp); | |
float targetDepth = 0.1; | |
// ray | |
vec3 ray = normalize(cSide * p.x + cUp * p.y + cDir * targetDepth); | |
// marching loop | |
float distance = 0.0; // レイとオブジェクト間の最短距離 | |
float rLen = 0.0; // レイに継ぎ足す長さ | |
vec3 rPos = cPos; // レイの先端位置 | |
for(int i = 0; i < 16; i++){ | |
distance = distanceFunc(rPos); | |
rLen += distance; | |
rPos = cPos + ray * rLen; | |
} | |
// hit check | |
if(abs(distance) < 0.001){ | |
gl_FragColor = vec4(vec3(1.0), 1.0); | |
}else{ | |
gl_FragColor = vec4(vec3(0.0), 1.0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment