Last active
July 6, 2019 17:43
-
-
Save baixiaohub/a12a8dc29a19013d2a9e3bcb49aab335 to your computer and use it in GitHub Desktop.
ShaderToy raymarching template
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
| //shadertoy raymarching boilerplate code for testing various shading effects. | |
| //TODO: | |
| //1. color. | |
| //2. indirect lighting. | |
| //3. ao. | |
| //4. soft shadow. | |
| //5. effective calcNormal by tetrahedron and value/vector noise and derivative. | |
| //6. pbr shading and IBL. //ref: https://github.com/wdas/brdf/blob/master/src/brdfs/disney.brdf | |
| //7. volumetric. | |
| //8. AA. | |
| //9. refactor modules. | |
| //10. atomsphere. | |
| //11. bokeh, lens effects. | |
| //12. npr effects. | |
| #define SHADING_MODE_PHONE 1 | |
| #define SHADING_MODE_BLINN_PHONE 2 | |
| #define SHADING_MODE 2 | |
| #define ENABLE_SHADOW 1 | |
| float sphere(in vec3 p, in vec3 center, in float r) | |
| { | |
| return length(p - center) - r; | |
| } | |
| float map(in vec3 p) | |
| { | |
| float d = sphere(p, vec3(-1.0, 0.0, -5.0), 1.0); | |
| d = min(d, sphere(p, vec3(2.0, 0.0, -3.0), 1.0)); | |
| d = min(d, sphere(p, vec3(-2.0, 0.0, -2.0), 1.0)); | |
| d = min(d, p.y + 1.0); | |
| return d; | |
| } | |
| vec3 calcNormal(in vec3 p) | |
| { | |
| vec2 e = vec2(1.0, -1.0) * 0.0005; | |
| return normalize( | |
| e.xyy * map(p + e.xyy) + | |
| e.yxy * map(p + e.yxy) + | |
| e.yyx * map(p + e.yyx)); | |
| } | |
| float calcShadow(in vec3 p, in vec3 lightPos) | |
| { | |
| vec3 ro = p; | |
| vec3 rd = normalize(lightPos - p); | |
| float h, t = 0.02; | |
| for(int i = 0; i < 256; ++i) | |
| { | |
| h = map(ro + rd * t); | |
| t += h; | |
| if(h < 0.001) break; | |
| } | |
| return h < 0.001 ? 0.0 : 1.0; | |
| } | |
| vec3 scene(in vec3 ro, in vec3 rd) | |
| { | |
| float h, t = 1.0; | |
| for(int i = 0; i < 512; ++i) | |
| { | |
| h = map(ro + rd * t); | |
| t += h; | |
| if(h < 0.00001) break; | |
| } | |
| vec3 color = vec3(0.2); | |
| if(h < 0.00001) | |
| { | |
| vec3 p = ro + rd * t; | |
| vec3 lightPos = vec3(0.0, 2.0, 0.0); | |
| vec3 N = calcNormal(p); | |
| vec3 L = normalize(lightPos - p); | |
| vec3 V = normalize(-rd); | |
| vec3 H = normalize(L + V); | |
| vec3 R = reflect(-L, N); | |
| float Ldist = dot(lightPos - p, lightPos - p); | |
| {//lighting | |
| float ambient = 0.02; | |
| float diffuse = clamp(dot(L, N), 0.0, 1.0); | |
| diffuse *= 5.0 / Ldist;//squared light attenuation. | |
| float specular = 0.0; | |
| if(SHADING_MODE == SHADING_MODE_BLINN_PHONE) | |
| { | |
| //blinn-phone. | |
| specular = clamp(dot(N, H), 0.0, 1.0); | |
| } | |
| else if(SHADING_MODE == SHADING_MODE_PHONE) | |
| { | |
| //phone | |
| specular = clamp(dot(R, V), 0.0, 1.0); | |
| } | |
| specular = pow(specular, 4.0); | |
| specular *= 5.0 / Ldist; | |
| color = vec3(diffuse + specular + ambient); | |
| #if ENABLE_SHADOW | |
| color *= calcShadow(p, lightPos); | |
| #endif | |
| } | |
| } | |
| return color; | |
| } | |
| vec3 gamma(in vec3 color) | |
| { | |
| return pow(color, vec3(0.4545)); | |
| } | |
| void mainImage( out vec4 fragColor, in vec2 fragCoord ) | |
| { | |
| vec2 uv = (-iResolution.xy + 2.0*fragCoord.xy)/iResolution.y; | |
| vec3 ro = vec3(0.0, 0.0, 1.0); | |
| vec3 rd = normalize(vec3(uv, 0.0) - ro); | |
| vec3 color = scene(ro, rd); | |
| fragColor = vec4(gamma(color), 1.0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment