|
| 1 | + |
| 2 | +#version 150 |
| 3 | + |
| 4 | +#define SAMPLER0 sampler2D // sampler2D, sampler3D, samplerCube |
| 5 | +#define SAMPLER1 sampler2D // sampler2D, sampler3D, samplerCube |
| 6 | +#define SAMPLER2 sampler2D // sampler2D, sampler3D, samplerCube |
| 7 | +#define SAMPLER3 sampler2D // sampler2D, sampler3D, samplerCube |
| 8 | + |
| 9 | +uniform SAMPLER0 iChannel0; // image/buffer/sound Sampler for input textures 0 |
| 10 | +uniform SAMPLER1 iChannel1; // image/buffer/sound Sampler for input textures 1 |
| 11 | +uniform SAMPLER2 iChannel2; // image/buffer/sound Sampler for input textures 2 |
| 12 | +uniform SAMPLER3 iChannel3; // image/buffer/sound Sampler for input textures 3 |
| 13 | + |
| 14 | +uniform vec3 iResolution; // image/buffer The viewport resolution (z is pixel aspect ratio, usually 1.0) |
| 15 | +uniform float iTime; // image/sound/buffer Current time in seconds |
| 16 | +uniform float iTimeDelta; // image/buffer Time it takes to render a frame, in seconds |
| 17 | +uniform int iFrame; // image/buffer Current frame |
| 18 | +uniform float iFrameRate; // image/buffer Number of frames rendered per second |
| 19 | +uniform vec4 iMouse; // image/buffer xy = current pixel coords (if LMB is down). zw = click pixel |
| 20 | +uniform vec4 iDate; // image/buffer/sound Year, month, day, time in seconds in .xyzw |
| 21 | +uniform float iSampleRate; // image/buffer/sound The sound sample rate (typically 44100) |
| 22 | +uniform float iChannelTime[4]; // image/buffer Time for channel (if video or sound), in seconds |
| 23 | +uniform vec3 iChannelResolution[4]; // image/buffer/sound Input texture resolution for each channel |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +// https://www.shadertoy.com/view/4slyRs |
| 28 | + |
| 29 | +float EPSILON = 0.002; |
| 30 | +vec2 twist = vec2(2.0,7.0); |
| 31 | +float planesDistance = 0.3; |
| 32 | +vec4 bumpMapParams1 = vec4(2.0,7.0,0.01,-0.01); |
| 33 | +vec4 bumpMapParams2 = vec4(2.0,3.0,-0.01,0.01); |
| 34 | +vec4 heightMapParams = vec4(3.0,1.0,0.0,0.01); |
| 35 | +vec4 heightInfluence = vec4(-0.025,-0.05,0.8,1.8); |
| 36 | +float fogDensity = 0.2; |
| 37 | +float fogDistance = 0.1; |
| 38 | +vec3 groundColor1 = vec3(0.2,0.3,0.3); |
| 39 | +vec3 groundColor2 = vec3(0.4,0.8,0.4); |
| 40 | +vec3 columnColors = vec3(0.9,0.3,0.3); |
| 41 | +vec4 ambient = vec4(0.2,0.3,0.4,0.0); |
| 42 | +vec3 lightColor = vec3(0.4,0.7,0.7); |
| 43 | +vec4 fogColor = vec4(0.0,0.1,0.5,1.0); |
| 44 | +vec3 rimColor = vec3(1.0,0.75,0.75); |
| 45 | + |
| 46 | +float pi = 3.14159265359; |
| 47 | + |
| 48 | +mat2 rot(float a) |
| 49 | +{ |
| 50 | + vec2 s = sin(vec2(a, a + pi/2.0)); |
| 51 | + return mat2(s.y,s.x,-s.x,s.y); |
| 52 | +} |
| 53 | + |
| 54 | +float smin( float a, float b, float k ) |
| 55 | +{ |
| 56 | + float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 ); |
| 57 | + return mix( b, a, h ) - k*h*(1.0-h); |
| 58 | +} |
| 59 | + |
| 60 | +float sphere(vec3 pos, float radius, vec3 scale) |
| 61 | +{ |
| 62 | + return length(pos*scale)-radius; |
| 63 | +} |
| 64 | + |
| 65 | +float heightmap(vec2 uv) |
| 66 | +{ |
| 67 | + return heightMapParams.x*texture(iChannel0, (uv + iTime*heightMapParams.zw)*heightMapParams.y).x; |
| 68 | +} |
| 69 | + |
| 70 | +float bumpmap(vec2 uv) |
| 71 | +{ |
| 72 | + float b1 = bumpMapParams1.x*(1.0 - texture(iChannel0, (uv + iTime*bumpMapParams1.zw)*bumpMapParams1.y).x); |
| 73 | + float b2 = bumpMapParams2.x*(1.0-texture(iChannel0, (uv + iTime*bumpMapParams2.zw)*bumpMapParams2.x).x); |
| 74 | + return b1+b2; |
| 75 | +} |
| 76 | + |
| 77 | +float distfunc(vec3 pos) |
| 78 | +{ |
| 79 | + vec3 p2 = pos; |
| 80 | + p2.x += sin(p2.z*3.0 + p2.y*5.0)*0.15; |
| 81 | + p2.xy *= rot(floor(p2.z*2.0)*twist.y); |
| 82 | + pos.xy *= rot(pos.z*twist.x); |
| 83 | + |
| 84 | + float h = heightmap(pos.xz)*heightInfluence.x; |
| 85 | + |
| 86 | + vec3 columnsrep = vec3(0.75,1.0,0.5); |
| 87 | + vec3 reppos = (mod(p2 + vec3(iTime*0.01 + sin(pos.z*0.5),0.0,0.0),columnsrep)-0.5*columnsrep); |
| 88 | + |
| 89 | + float columnsScaleX = 1.0 + sin(p2.y*20.0*sin(p2.z) + iTime*5.0 + pos.z)*0.15; |
| 90 | + float columnsScaleY = (sin(iTime + pos.z*4.0)*0.5+0.5); |
| 91 | + |
| 92 | + float columns = sphere(vec3(reppos.x, pos.y+0.25, reppos.z), 0.035, vec3(columnsScaleX,columnsScaleY,columnsScaleX)); |
| 93 | + float corridor = planesDistance - abs(pos.y) + h; |
| 94 | + float d = smin(corridor, columns, 0.25); |
| 95 | + |
| 96 | + return d; |
| 97 | +} |
| 98 | + |
| 99 | +float rayMarch(vec3 rayDir, vec3 cameraOrigin) |
| 100 | +{ |
| 101 | + const int MAX_ITER = 50; |
| 102 | + const float MAX_DIST = 30.0; |
| 103 | + |
| 104 | + float totalDist = 0.0; |
| 105 | + float totalDist2 = 0.0; |
| 106 | + vec3 pos = cameraOrigin; |
| 107 | + float dist = EPSILON; |
| 108 | + vec3 col = vec3(0.0); |
| 109 | + float glow = 0.0; |
| 110 | + |
| 111 | + for(int j = 0; j < MAX_ITER; j++) |
| 112 | + { |
| 113 | + dist = distfunc(pos); |
| 114 | + totalDist = totalDist + dist; |
| 115 | + pos += dist*rayDir; |
| 116 | + |
| 117 | + if(dist < EPSILON || totalDist > MAX_DIST) |
| 118 | + { |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return totalDist ; |
| 124 | +} |
| 125 | + |
| 126 | +//Taken from https://www.shadertoy.com/view/Xds3zN |
| 127 | +mat3 setCamera( in vec3 ro, in vec3 ta, float cr ) |
| 128 | +{ |
| 129 | + vec3 cw = normalize(ta-ro); |
| 130 | + vec3 cp = vec3(sin(cr), cos(cr),0.0); |
| 131 | + vec3 cu = normalize( cross(cw,cp) ); |
| 132 | + vec3 cv = normalize( cross(cu,cw) ); |
| 133 | + return mat3( cu, cv, cw ); |
| 134 | +} |
| 135 | + |
| 136 | +vec3 calculateNormals(vec3 pos) |
| 137 | +{ |
| 138 | + vec2 eps = vec2(0.0, EPSILON*1.0); |
| 139 | + vec3 n = normalize(vec3( |
| 140 | + distfunc(pos + eps.yxx) - distfunc(pos - eps.yxx), |
| 141 | + distfunc(pos + eps.xyx) - distfunc(pos - eps.xyx), |
| 142 | + distfunc(pos + eps.xxy) - distfunc(pos - eps.xxy))); |
| 143 | + |
| 144 | + return n; |
| 145 | +} |
| 146 | + |
| 147 | +//Taken from https://www.shadertoy.com/view/XlXXWj |
| 148 | +vec3 doBumpMap(vec2 uv, vec3 nor, float bumpfactor) |
| 149 | +{ |
| 150 | + |
| 151 | + const float eps = 0.001; |
| 152 | + float ref = bumpmap(uv); |
| 153 | + |
| 154 | + vec3 grad = vec3(bumpmap(vec2(uv.x-eps, uv.y))-ref, 0.0, bumpmap(vec2(uv.x, uv.y-eps))-ref); |
| 155 | + |
| 156 | + grad -= nor*dot(nor, grad); |
| 157 | + |
| 158 | + return normalize( nor + grad*bumpfactor ); |
| 159 | +} |
| 160 | + |
| 161 | +void mainImage( out vec4 fragColor, in vec2 fragCoord ) |
| 162 | +{ |
| 163 | + vec3 cameraOrigin = vec3(0.0, 0.0, iTime*-0.1); |
| 164 | + vec3 cameraTarget = cameraOrigin + vec3(0.0, 0.0, 1.0);; |
| 165 | + |
| 166 | + vec2 screenPos = (fragCoord.xy/iResolution.xy)*2.0-1.0; |
| 167 | + screenPos.x *= iResolution.x/iResolution.y; |
| 168 | + |
| 169 | + mat3 cam = setCamera(cameraOrigin, cameraTarget, 0.0 ); |
| 170 | + |
| 171 | + vec3 rayDir = cam* normalize( vec3(screenPos.xy,2.0) ); |
| 172 | + rayDir.xy *= rot(iTime*0.1); |
| 173 | + float dist = rayMarch(rayDir, cameraOrigin); |
| 174 | + |
| 175 | + vec3 pos = cameraOrigin + dist*rayDir; |
| 176 | + vec2 uv = pos.xy * rot(pos.z*twist.x); |
| 177 | + float h = heightmap(vec2(uv.x, pos.z)); |
| 178 | + vec3 n = calculateNormals(pos); |
| 179 | + vec3 bump = doBumpMap(vec2(uv.x, pos.z), n, 3.0); |
| 180 | + float m = smoothstep(-0.15,0.2, planesDistance - abs(uv.y) + h*heightInfluence.y + sin(iTime)*0.05); |
| 181 | + vec3 color = mix(mix(groundColor1, groundColor2, smoothstep(heightInfluence.z,heightInfluence.w,h)), columnColors, m); |
| 182 | + float fog = dist*fogDensity-fogDistance; |
| 183 | + float heightfog = pos.y; |
| 184 | + float rim = (1.0-max(0.0, dot(-normalize(rayDir), bump))); |
| 185 | + vec3 lightPos = pos - (cameraOrigin + vec3(0.0,0.0,1.0)); |
| 186 | + vec3 lightDir = -normalize(lightPos); |
| 187 | + float lightdist = length(lightPos); |
| 188 | + float atten = 1.0 / (1.0 + lightdist*lightdist*3.0); |
| 189 | + float light = max(0.0, dot(lightDir, bump)); |
| 190 | + vec3 r = reflect(normalize(rayDir), bump); |
| 191 | + float spec = clamp (dot (r, lightDir),0.0,1.0); |
| 192 | + float specpow = pow(spec,20.0); |
| 193 | + vec3 c = color*(ambient.xyz + mix(rim*rim*rim, rim*0.35+0.65, m)*rimColor + lightColor*(light*atten*2.0 + specpow*1.5)); |
| 194 | + vec4 res = mix(vec4(c, rim), fogColor, clamp(fog+heightfog,0.0,1.0)); |
| 195 | + |
| 196 | + |
| 197 | + fragColor = res; |
| 198 | +} |
0 commit comments