Skip to content

Instantly share code, notes, and snippets.

@doxas
Last active August 29, 2015 14:02
Show Gist options
  • Save doxas/0892d1d0ba9e9b569997 to your computer and use it in GitHub Desktop.
Save doxas/0892d1d0ba9e9b569997 to your computer and use it in GitHub Desktop.
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