From 915132eb9329066982a117a9b23e63d9e9f266d2 Mon Sep 17 00:00:00 2001 From: mudlord Date: Fri, 29 Jul 2016 16:10:55 +1000 Subject: [PATCH] Added 2 more example raymarch shaders. --- procedural/mudlord-emeraldenvy4.slang | 103 +++++++++ procedural/shane-latticetutorial.slang | 304 +++++++++++++++++++++++++ 2 files changed, 407 insertions(+) create mode 100644 procedural/mudlord-emeraldenvy4.slang create mode 100644 procedural/shane-latticetutorial.slang diff --git a/procedural/mudlord-emeraldenvy4.slang b/procedural/mudlord-emeraldenvy4.slang new file mode 100644 index 0000000..9920ab7 --- /dev/null +++ b/procedural/mudlord-emeraldenvy4.slang @@ -0,0 +1,103 @@ +#version 450 +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; + uint FrameCount; +} global; + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; +const vec2 madd = vec2(0.5, 0.5); +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = gl_Position.xy; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; + +float time = float(global.FrameCount)*0.0325; +vec2 resolution = global.OutputSize.xy; +float fade = 0.45; + +const float PI=3.14159265358979323846; + +float speed=time*0.25; +float ground_x=0.125-0.25*cos(PI*speed*0.25); +float ground_y=0.125+0.25*sin(PI*speed*0.25); +float ground_z=speed*0.125; + +vec2 rotate(vec2 k,float t) + { + return vec2(cos(t)*k.x-sin(t)*k.y,sin(t)*k.x+cos(t)*k.y); + } + + +float scene(vec3 p) + { + float ball_p=0.25; + float ball_w=ball_p*1.0; + float ball=length(mod(p.xyz,ball_p)-ball_p*0.5)-ball_w; + float hole_w=ball_p*0.55; + float hole=length(mod(p.xyz,ball_p)-ball_p*0.5)-hole_w; + float pipe_p=0.015; + float pipe_w=pipe_p*0.42;//-0.00375*sync; + float pipe_y=length(max(abs(mod(p.xy,pipe_p)-pipe_p*0.5)-pipe_w,0.0)); + float pipe_z=length(max(abs(mod(p.xz,pipe_p)-pipe_p*0.5)-pipe_w,0.0)); + return max(max(ball,-hole),max(pipe_y,max(pipe_y,pipe_z))); + } + + vec3 getNormal(vec3 pos) + { vec3 e = vec3(0.0,0.0001,0.0); + return normalize(vec3( scene(pos+e.yxx)-scene(pos-e.yxx),scene(pos+e.xyx)-scene(pos-e.xyx),scene(pos+e.xxy)-scene(pos-e.xxy) ) ); + } +float render_scene(vec3 ray_origin, vec3 ray_dir,float t) +{ + const int ray_n=96; + for(int i=0;i31. || dot(col, vec3(.299, .587, .114)) > 1. || t>16.) break; + + // Current ray postion. Slightly redundant here, but sometimes you may wish to reuse + // it during the accumulation stage. + sp = ro+rd*t; + + d = map(sp); // Distance to nearest point on the noise surface. + + // If we get within a certain distance of the surface, accumulate some surface values. + // Values further away have less influence on the total. + // + // aD - Accumulated distance. You could smoothly interpolate it, if you wanted. + // + // 1/.(1. + t*t*0.1) - Basic distance attenuation. Feel free to substitute your own. + + // Normalized distance from the surface threshold value to our current isosurface value. + aD = (thD-abs(d)*31./32.)/thD; + + // If we're within the surface threshold, accumulate some color. + // Two "if" statements in a shader loop makes me nervous. I don't suspect there'll be any + // problems, but if there are, let us know. + if(aD>0.) { + + + // Add the accumulated surface distance value, along with some basic falloff using the + // camera to light distance, "lDist." There's a bit of color jitter there, too. + + sn = calcNormal(sp, d)*sign(d); + ld = (lp - sp); //vec3(.5773) + lDist = max(length(ld), .001); + ld /= lDist; + s = pow(max(dot(reflect(-ld, sn), -rd), 0.), 8.); + l = max(dot(ld, sn), 0.); + + + //float c = dot(sin(sp*128. - cos(sp.yzx*64.)), vec3(.166))+.5; + col += ((l + .1) + vec3(.5, .7, 1)*s)*aD/(1. + lDist*0.25 + lDist*lDist*0.05)*.2; + // Failed experiment with color jitter to take out more banding. + //col += ((l + .05 + fract(rnd + i*27.)*.1) + vec3(.5, .7, 1)*s)*aD/(1. + lDist*0.25 + lDist*lDist*0.05)*.2; + + + // The layer number is worth noting. Accumulating more layers gives a bit more glow. + // Lower layer numbers allow a quicker bailout. A lot of it is guess work. + layers++; + + } + + // Kind of weird the way this works. I think not allowing the ray to hone in properly is + // the very thing that gives an even spread of values. The figures are based on a bit + // of knowledge versus trial and error. If you have a faster computer, feel free to tweak + // them a bit. + t += max(abs(d)*.75, thD*.25); + + + } + + t = min(t, 16.); + + + col = mix(col, vec3(0), 1.-exp(-0.025*t*t));////1.-exp(-0.01*t*t) 1.-1./(1. + t*t*.1) + + // Mixing the greytone color with a firey orange vignette. There's no meaning + // behind it. I just thought the artsy greyscale was a little too artsy. + uv = abs(fragCoord.xy/iResolution.xy - .5); // Wasteful, but the GPU can handle it. + col = mix(col, vec3(min(col.x*1.5, 1.), pow(col.x, 2.5), pow(col.x, 12.)).yxz, + min( dot(pow(uv, vec2(4.)), vec2(1))*8., 1.)); + + //col = vec3(min(col.z*1.5, 1.), pow(col.z, 2.5), pow(col.z, 12.)); + + + // Mixing the vignette colors up a bit more. + col = mix(col, col.zxy, dot(sin(rd*5.), vec3(.166)) + 0.166); + + + // Presenting the color to the screen. + fragColor = vec4( sqrt(clamp(col, 0., 1.)), 1.0 ); + + + } + + void main(void) +{ + //just some shit to wrap shadertoy's stuff + vec2 FragCoord = vTexCoord.xy*global.OutputSize.xy; + FragCoord.y = -FragCoord.y; + mainImage(FragColor,FragCoord); +} \ No newline at end of file