|
| 1 | +// The MIT License |
| 2 | +// Copyright © 2013 Inigo Quilez |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 4 | + |
| 5 | + |
| 6 | +#ifdef GL_ES |
| 7 | +precision mediump float; |
| 8 | +precision mediump int; |
| 9 | +#endif |
| 10 | + |
| 11 | +// ---------------------- |
| 12 | +// SHADERTOY UNIFORMS - |
| 13 | +// ---------------------- |
| 14 | + |
| 15 | +uniform vec3 iResolution; // viewport resolution (in pixels) |
| 16 | +uniform float iTime; // shader playback time (in seconds) |
| 17 | +uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click |
| 18 | + |
| 19 | +void mainImage( out vec4 fragColor, in vec2 fragCoord ); |
| 20 | + |
| 21 | +void main() { |
| 22 | + mainImage(gl_FragColor,gl_FragCoord.xy); |
| 23 | +} |
| 24 | + |
| 25 | +const vec3 va = vec3( 0.0, 0.57735, 0.0 ); |
| 26 | +const vec3 vb = vec3( 0.0, -1.0, 1.15470 ); |
| 27 | +const vec3 vc = vec3( 1.0, -1.0, -0.57735 ); |
| 28 | +const vec3 vd = vec3( -1.0, -1.0, -0.57735 ); |
| 29 | + |
| 30 | +// return distance and address |
| 31 | +vec2 map( vec3 p ) |
| 32 | +{ |
| 33 | + float a = 0.0; |
| 34 | + float s = 1.0; |
| 35 | + float r = 1.0; |
| 36 | + float dm; |
| 37 | + vec3 v; |
| 38 | + for( int i=0; i<8; i++ ) |
| 39 | + { |
| 40 | + float d, t; |
| 41 | + d = dot(p-va,p-va); v=va; dm=d; t=0.0; |
| 42 | + d = dot(p-vb,p-vb); if( d<dm ) { v=vb; dm=d; t=1.0; } |
| 43 | + d = dot(p-vc,p-vc); if( d<dm ) { v=vc; dm=d; t=2.0; } |
| 44 | + d = dot(p-vd,p-vd); if( d<dm ) { v=vd; dm=d; t=3.0; } |
| 45 | + p = v + 2.0*(p - v); r*= 2.0; |
| 46 | + a = t + 4.0*a; s*= 4.0; |
| 47 | + } |
| 48 | + |
| 49 | + return vec2( (sqrt(dm)-1.0)/r, a/s ); |
| 50 | +} |
| 51 | + |
| 52 | +const float precis = 0.0002; |
| 53 | + |
| 54 | +vec3 intersect( in vec3 ro, in vec3 rd ) |
| 55 | +{ |
| 56 | + vec3 res = vec3( 1e20, 0.0, 0.0 ); |
| 57 | + |
| 58 | + float maxd = 5.0; |
| 59 | + |
| 60 | + // sierpinski |
| 61 | + float h = 1.0; |
| 62 | + float t = 0.5; |
| 63 | + float m = 0.0; |
| 64 | + vec2 r; |
| 65 | + for( int i=0; i<100; i++ ) |
| 66 | + { |
| 67 | + r = map( ro+rd*t ); |
| 68 | + if( r.x<precis || t>maxd ) break; |
| 69 | + m = r.y; |
| 70 | + t += r.x; |
| 71 | + } |
| 72 | + |
| 73 | + if( t<maxd && r.x<precis ) |
| 74 | + res = vec3( t, 2.0, m ); |
| 75 | + |
| 76 | + return res; |
| 77 | +} |
| 78 | + |
| 79 | +vec3 calcNormal( in vec3 pos ) |
| 80 | +{ |
| 81 | + vec3 eps = vec3(precis,0.0,0.0); |
| 82 | + return normalize( vec3( |
| 83 | + map(pos+eps.xyy).x - map(pos-eps.xyy).x, |
| 84 | + map(pos+eps.yxy).x - map(pos-eps.yxy).x, |
| 85 | + map(pos+eps.yyx).x - map(pos-eps.yyx).x ) ); |
| 86 | +} |
| 87 | + |
| 88 | +float calcOcclusion( in vec3 pos, in vec3 nor ) |
| 89 | +{ |
| 90 | + float ao = 0.0; |
| 91 | + float sca = 1.0; |
| 92 | + for( int i=0; i<8; i++ ) |
| 93 | + { |
| 94 | + float h = 0.001 + 0.5*pow(float(i)/7.0,1.5); |
| 95 | + float d = map( pos + h*nor ).x; |
| 96 | + ao += -(d-h)*sca; |
| 97 | + sca *= 0.95; |
| 98 | + } |
| 99 | + return clamp( 1.0 - 0.8*ao, 0.0, 1.0 ); |
| 100 | +} |
| 101 | + |
| 102 | +vec3 lig = normalize(vec3(1.0,0.7,0.9)); |
| 103 | + |
| 104 | +vec3 render( in vec3 ro, in vec3 rd ) |
| 105 | +{ |
| 106 | + vec3 col = vec3(0.0); |
| 107 | + |
| 108 | + // raymarch |
| 109 | + vec3 tm = intersect(ro,rd); |
| 110 | + if( tm.y>0.5 ) |
| 111 | + { |
| 112 | + // geometry |
| 113 | + vec3 pos = ro + tm.x*rd; |
| 114 | + vec3 nor = calcNormal( pos ); |
| 115 | + vec3 maa = vec3( 0.0 ); |
| 116 | + |
| 117 | + maa = 0.5 + 0.5*cos( 6.2831*tm.z + vec3(0.0,1.0,2.0) ); |
| 118 | + |
| 119 | + float occ = calcOcclusion( pos, nor ); |
| 120 | + |
| 121 | + // lighting |
| 122 | + float amb = (0.5 + 0.5*nor.y); |
| 123 | + float dif = max(dot(nor,lig),0.0); |
| 124 | + |
| 125 | + // lights |
| 126 | + vec3 lin = 1.5*amb*vec3(1.0) * occ; |
| 127 | + |
| 128 | + // surface-light interacion |
| 129 | + col = maa * lin; |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + // gamma |
| 134 | + col = pow( clamp(col,0.0,1.0), vec3(0.45) ); |
| 135 | + |
| 136 | + return col; |
| 137 | +} |
| 138 | +void mainImage( out vec4 fragColor, in vec2 fragCoord ) |
| 139 | +{ |
| 140 | + vec2 q = fragCoord.xy / iResolution.xy; |
| 141 | + vec2 p = -1.0 + 2.0 * q; |
| 142 | + p.x *= iResolution.x/iResolution.y; |
| 143 | + vec2 m = vec2(0.5); |
| 144 | + if( iMouse.z>0.0 ) m = iMouse.xy/iResolution.xy; |
| 145 | + |
| 146 | + //----------------------------------------------------- |
| 147 | + // camera |
| 148 | + //----------------------------------------------------- |
| 149 | + |
| 150 | + float an = 3.2 + 0.5*iTime - 6.2831*(m.x-0.5); |
| 151 | + |
| 152 | + vec3 ro = vec3(2.5*sin(an),0.0,2.5*cos(an)); |
| 153 | + vec3 ta = vec3(0.0,-0.5,0.0); |
| 154 | + vec3 ww = normalize( ta - ro ); |
| 155 | + vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) ); |
| 156 | + vec3 vv = normalize( cross(uu,ww)); |
| 157 | + vec3 rd = normalize( p.x*uu + p.y*vv + 5.0*ww*m.y ); |
| 158 | + |
| 159 | + vec3 col = render( ro, rd ); |
| 160 | + |
| 161 | + fragColor = vec4( col, 1.0 ); |
| 162 | +} |
| 163 | + |
| 164 | +void mainVR( out vec4 fragColor, in vec2 fragCoord, in vec3 fragRayOri, in vec3 fragRayDir ) |
| 165 | +{ |
| 166 | + vec3 col = render( fragRayOri + vec3(0.0,-0.1,2.0), fragRayDir ); |
| 167 | + |
| 168 | + fragColor = vec4( col, 1.0 ); |
| 169 | +} |
| 170 | +// ---------------------------- |
| 171 | +// SHADERTOY CODE ENDS HERE - |
| 172 | +// ---------------------------- |
0 commit comments