| 1 | //------------------------------------------------------------------------------ |
| 2 | // Shader code for texcube-sapp sample. |
| 3 | // |
| 4 | // NOTE: This source file also uses the '#pragma sokol' form of the |
| 5 | // custom tags. |
| 6 | //------------------------------------------------------------------------------ |
| 7 | //#pragma sokol @ctype mat4 hmm_mat4 |
| 8 | |
| 9 | #pragma sokol @vs vs_p |
| 10 | uniform vs_params_p { |
| 11 | mat4 mvp; |
| 12 | }; |
| 13 | |
| 14 | in vec4 pos; |
| 15 | in vec4 color0; |
| 16 | in vec2 texcoord0; |
| 17 | |
| 18 | out vec4 color; |
| 19 | out vec2 uv; |
| 20 | |
| 21 | void main() { |
| 22 | gl_Position = mvp * pos; |
| 23 | color = color0; |
| 24 | uv = texcoord0; |
| 25 | } |
| 26 | #pragma sokol @end |
| 27 | |
| 28 | #pragma sokol @fs fs_p |
| 29 | uniform texture2D tex; |
| 30 | uniform sampler smp; |
| 31 | |
| 32 | uniform fs_params_p { |
| 33 | vec2 iResolution; |
| 34 | vec2 iMouse; |
| 35 | float iTime; |
| 36 | float iFrame; |
| 37 | }; |
| 38 | |
| 39 | in vec4 color; |
| 40 | in vec2 uv; |
| 41 | out vec4 frag_color; |
| 42 | |
| 43 | // change to 0 to 4 to increment the AntiAliasing, |
| 44 | // increase AA will SLOW the rendering!! |
| 45 | #define AA 1 |
| 46 | |
| 47 | //********************************************************* |
| 48 | // Ray Marching |
| 49 | // original code from: https://www.shadertoy.com/view/Xds3zN |
| 50 | //********************************************************* |
| 51 | // Created by inigo quilez - iq/2019 |
| 52 | // I share this piece (art and code) here in Shadertoy and through its Public API, only for educational purposes. |
| 53 | // You cannot use, share or host this piece or modifications of it as part of your own commercial or non-commercial product, website or project. |
| 54 | // You can share a link to it or an unmodified screenshot of it provided you attribute "by Inigo Quilez, @iquilezles and iquilezles.org". |
| 55 | // If you are a teacher, lecturer, educator or similar and these conditions are too restrictive for your needs, please contact me and we'll work it out. |
| 56 | |
| 57 | |
| 58 | // An animation test - a happy and blobby creature |
| 59 | // jumping and looking around. It gets off-model very |
| 60 | // often, but it looks good enough I think. |
| 61 | // |
| 62 | // Making-of with math/shader/art explanations (6 hours |
| 63 | // long): https://www.youtube.com/watch?v=Cfe5UQ-1L9Q |
| 64 | // |
| 65 | // Video capture: https://www.youtube.com/watch?v=s_UOFo2IULQ |
| 66 | // |
| 67 | // Buy a metal print here: https://www.redbubble.com/i/metal-print/Happy-Jumping-by-InigoQuilez/43594745.0JXQP |
| 68 | |
| 69 | //------------------------------------------------------------------ |
| 70 | |
| 71 | |
| 72 | // http://iquilezles.org/www/articles/smin/smin.htm |
| 73 | float smin( float a, float b, float k ) |
| 74 | { |
| 75 | float h = max(k-abs(a-b),0.0); |
| 76 | return min(a, b) - h*h*0.25/k; |
| 77 | } |
| 78 | |
| 79 | // http://iquilezles.org/www/articles/smin/smin.htm |
| 80 | vec2 smin( vec2 a, vec2 b, float k ) |
| 81 | { |
| 82 | float h = clamp( 0.5+0.5*(b.x-a.x)/k, 0.0, 1.0 ); |
| 83 | return mix( b, a, h ) - k*h*(1.0-h); |
| 84 | } |
| 85 | |
| 86 | // http://iquilezles.org/www/articles/smin/smin.htm |
| 87 | float smax( float a, float b, float k ) |
| 88 | { |
| 89 | float h = max(k-abs(a-b),0.0); |
| 90 | return max(a, b) + h*h*0.25/k; |
| 91 | } |
| 92 | |
| 93 | // http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm |
| 94 | float sdSphere( vec3 p, float s ) |
| 95 | { |
| 96 | return length(p)-s; |
| 97 | } |
| 98 | |
| 99 | // http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm |
| 100 | float sdEllipsoid( in vec3 p, in vec3 r ) // approximated |
| 101 | { |
| 102 | float k0 = length(p/r); |
| 103 | float k1 = length(p/(r*r)); |
| 104 | return k0*(k0-1.0)/k1; |
| 105 | } |
| 106 | |
| 107 | vec2 sdStick(vec3 p, vec3 a, vec3 b, float r1, float r2) // approximated |
| 108 | { |
| 109 | vec3 pa = p-a, ba = b-a; |
| 110 | float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); |
| 111 | return vec2( length( pa - ba*h ) - mix(r1,r2,h*h*(3.0-2.0*h)), h ); |
| 112 | } |
| 113 | |
| 114 | // http://iquilezles.org/www/articles/smin/smin.htm |
| 115 | vec4 opU( vec4 d1, vec4 d2 ) |
| 116 | { |
| 117 | return (d1.x<d2.x) ? d1 : d2; |
| 118 | } |
| 119 | |
| 120 | //------------------------------------------------------------------ |
| 121 | |
| 122 | #define ZERO (min(int(iFrame),0)) |
| 123 | |
| 124 | //------------------------------------------------------------------ |
| 125 | |
| 126 | float href; |
| 127 | float hsha; |
| 128 | |
| 129 | vec4 map( in vec3 pos, float atime ) |
| 130 | { |
| 131 | hsha = 1.0; |
| 132 | |
| 133 | float t1 = fract(atime); |
| 134 | float t4 = abs(fract(atime*0.5)-0.5)/0.5; |
| 135 | |
| 136 | float p = 4.0*t1*(1.0-t1); |
| 137 | float pp = 4.0*(1.0-2.0*t1); // derivative of p |
| 138 | |
| 139 | vec3 cen = vec3( 0.5*(-1.0 + 2.0*t4), |
| 140 | pow(p,2.0-p) + 0.1, |
| 141 | floor(atime) + pow(t1,0.7) -1.0 ); |
| 142 | |
| 143 | // body |
| 144 | vec2 uu = normalize(vec2( 1.0, -pp )); |
| 145 | vec2 vv = vec2(-uu.y, uu.x); |
| 146 | |
| 147 | float sy = 0.5 + 0.5*p; |
| 148 | float compress = 1.0-smoothstep(0.0,0.4,p); |
| 149 | sy = sy*(1.0-compress) + compress; |
| 150 | float sz = 1.0/sy; |
| 151 | |
| 152 | vec3 q = pos - cen; |
| 153 | float rot = -0.25*(-1.0 + 2.0*t4); |
| 154 | float rc = cos(rot); |
| 155 | float rs = sin(rot); |
| 156 | q.xy = mat2x2(rc,rs,-rs,rc)*q.xy; |
| 157 | vec3 r = q; |
| 158 | href = q.y; |
| 159 | q.yz = vec2( dot(uu,q.yz), dot(vv,q.yz) ); |
| 160 | |
| 161 | vec4 res = vec4( sdEllipsoid( q, vec3(0.25, 0.25*sy, 0.25*sz) ), 2.0, 0.0, 1.0 ); |
| 162 | |
| 163 | if( res.x-1.0 < pos.y ) // bounding volume |
| 164 | { |
| 165 | float t2 = fract(atime+0.8); |
| 166 | float p2 = 0.5-0.5*cos(6.2831*t2); |
| 167 | r.z += 0.05-0.2*p2; |
| 168 | r.y += 0.2*sy-0.2; |
| 169 | vec3 sq = vec3( abs(r.x), r.yz ); |
| 170 | |
| 171 | // head |
| 172 | vec3 h = r; |
| 173 | float hr = sin(0.791*atime); |
| 174 | hr = 0.7*sign(hr)*smoothstep(0.5,0.7,abs(hr)); |
| 175 | h.xz = mat2x2(cos(hr),sin(hr),-sin(hr),cos(hr))*h.xz; |
| 176 | vec3 hq = vec3( abs(h.x), h.yz ); |
| 177 | float d = sdEllipsoid( h-vec3(0.0,0.20,0.02), vec3(0.08,0.2,0.15) ); |
| 178 | float d2 = sdEllipsoid( h-vec3(0.0,0.21,-0.1), vec3(0.20,0.2,0.20) ); |
| 179 | d = smin( d, d2, 0.1 ); |
| 180 | res.x = smin( res.x, d, 0.1 ); |
| 181 | |
| 182 | // belly wrinkles |
| 183 | { |
| 184 | float yy = r.y-0.02-2.5*r.x*r.x; |
| 185 | res.x += 0.001*sin(yy*120.0)*(1.0-smoothstep(0.0,0.1,abs(yy))); |
| 186 | } |
| 187 | |
| 188 | // arms |
| 189 | { |
| 190 | vec2 arms = sdStick( sq, vec3(0.18-0.06*hr*sign(r.x),0.2,-0.05), vec3(0.3+0.1*p2,-0.2+0.3*p2,-0.15), 0.03, 0.06 ); |
| 191 | res.xz = smin( res.xz, arms, 0.01+0.04*(1.0-arms.y)*(1.0-arms.y)*(1.0-arms.y) ); |
| 192 | } |
| 193 | |
| 194 | // ears |
| 195 | { |
| 196 | float t3 = fract(atime+0.9); |
| 197 | float p3 = 4.0*t3*(1.0-t3); |
| 198 | vec2 ear = sdStick( hq, vec3(0.15,0.32,-0.05), vec3(0.2+0.05*p3,0.2+0.2*p3,-0.07), 0.01, 0.04 ); |
| 199 | res.xz = smin( res.xz, ear, 0.01 ); |
| 200 | } |
| 201 | |
| 202 | // mouth |
| 203 | { |
| 204 | d = sdEllipsoid( h-vec3(0.0,0.15+4.0*hq.x*hq.x,0.15), vec3(0.1,0.04,0.2) ); |
| 205 | res.w = 0.3+0.7*clamp( d*150.0,0.0,1.0); |
| 206 | res.x = smax( res.x, -d, 0.03 ); |
| 207 | } |
| 208 | |
| 209 | // legs |
| 210 | { |
| 211 | float t6 = cos(6.2831*(atime*0.5+0.25)); |
| 212 | float ccc = cos(1.57*t6*sign(r.x)); |
| 213 | float sss = sin(1.57*t6*sign(r.x)); |
| 214 | vec3 base = vec3(0.12,-0.07,-0.1); base.y -= 0.1/sy; |
| 215 | vec2 legs = sdStick( sq, base, base + vec3(0.2,-ccc,sss)*0.2, 0.04, 0.07 ); |
| 216 | res.xz = smin( res.xz, legs, 0.07 ); |
| 217 | } |
| 218 | |
| 219 | // eye |
| 220 | { |
| 221 | float blink = pow(0.5+0.5*sin(2.1*iTime),20.0); |
| 222 | float eyeball = sdSphere(hq-vec3(0.08,0.27,0.06),0.065+0.02*blink); |
| 223 | res.x = smin( res.x, eyeball, 0.03 ); |
| 224 | |
| 225 | vec3 cq = hq-vec3(0.1,0.34,0.08); |
| 226 | cq.xy = mat2x2(0.8,0.6,-0.6,0.8)*cq.xy; |
| 227 | d = sdEllipsoid( cq, vec3(0.06,0.03,0.03) ); |
| 228 | res.x = smin( res.x, d, 0.03 ); |
| 229 | |
| 230 | float eo = 1.0-0.5*smoothstep(0.01,0.04,length((hq.xy-vec2(0.095,0.285))*vec2(1.0,1.1))); |
| 231 | res = opU( res, vec4(sdSphere(hq-vec3(0.08,0.28,0.08),0.060),3.0,0.0,eo)); |
| 232 | res = opU( res, vec4(sdSphere(hq-vec3(0.075,0.28,0.102),0.0395),4.0,0.0,1.0)); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // ground |
| 237 | float fh = -0.1 - 0.05*(sin(pos.x*2.0)+sin(pos.z*2.0)); |
| 238 | float t5f = fract(atime+0.05); |
| 239 | float t5i = floor(atime+0.05); |
| 240 | float bt4 = abs(fract(t5i*0.5)-0.5)/0.5; |
| 241 | vec2 bcen = vec2( 0.5*(-1.0+2.0*bt4),t5i+pow(t5f,0.7)-1.0 ); |
| 242 | |
| 243 | float k = length(pos.xz-bcen); |
| 244 | float tt = t5f*15.0-6.2831 - k*3.0; |
| 245 | fh -= 0.1*exp(-k*k)*sin(tt)*exp(-max(tt,0.0)/2.0)*smoothstep(0.0,0.01,t5f); |
| 246 | float d = pos.y - fh; |
| 247 | |
| 248 | // bubbles |
| 249 | { |
| 250 | vec3 vp = vec3( mod(abs(pos.x),3.0)-1.5,pos.y,mod(pos.z+1.5,3.0)-1.5); |
| 251 | vec2 id = vec2( floor(pos.x/3.0), floor((pos.z+1.5)/3.0) ); |
| 252 | float fid = id.x*11.1 + id.y*31.7; |
| 253 | float fy = fract(fid*1.312+atime*0.1); |
| 254 | float y = -1.0+4.0*fy; |
| 255 | vec3 rad = vec3(0.7,1.0+0.5*sin(fid),0.7); |
| 256 | rad -= 0.1*(sin(pos.x*3.0)+sin(pos.y*4.0)+sin(pos.z*5.0)); |
| 257 | float siz = 4.0*fy*(1.0-fy); |
| 258 | float d2 = sdEllipsoid( vp-vec3(0.5,y,0.0), siz*rad ); |
| 259 | |
| 260 | d2 -= 0.03*smoothstep(-1.0,1.0,sin(18.0*pos.x)+sin(18.0*pos.y)+sin(18.0*pos.z)); |
| 261 | d2 *= 0.6; |
| 262 | d2 = min(d2,2.0); |
| 263 | d = smin( d, d2, 0.32 ); |
| 264 | if( d<res.x ) { res = vec4(d,1.0,0.0,1.0); hsha=sqrt(siz); } |
| 265 | } |
| 266 | |
| 267 | // candy |
| 268 | { |
| 269 | float fs = 5.0; |
| 270 | vec3 qos = fs*vec3(pos.x, pos.y-fh, pos.z ); |
| 271 | vec2 id = vec2( floor(qos.x+0.5), floor(qos.z+0.5) ); |
| 272 | vec3 vp = vec3( fract(qos.x+0.5)-0.5,qos.y,fract(qos.z+0.5)-0.5); |
| 273 | vp.xz += 0.1*cos( id.x*130.143 + id.y*120.372 + vec2(0.0,2.0) ); |
| 274 | float den = sin(id.x*0.1+sin(id.y*0.091))+sin(id.y*0.1); |
| 275 | float fid = id.x*0.143 + id.y*0.372; |
| 276 | float ra = smoothstep(0.0,0.1,den*0.1+fract(fid)-0.95); |
| 277 | d = sdSphere( vp, 0.35*ra )/fs; |
| 278 | if( d<res.x ) res = vec4(d,5.0,qos.y,1.0); |
| 279 | } |
| 280 | |
| 281 | return res; |
| 282 | } |
| 283 | |
| 284 | vec4 raycast( in vec3 ro, in vec3 rd, float time ) |
| 285 | { |
| 286 | vec4 res = vec4(-1.0,-1.0,0.0,1.0); |
| 287 | |
| 288 | float tmin = 0.5; |
| 289 | float tmax = 20.0; |
| 290 | |
| 291 | #if 1 |
| 292 | // raytrace bounding plane |
| 293 | float tp = (3.5-ro.y)/rd.y; |
| 294 | if( tp>0.0 ) tmax = min( tmax, tp ); |
| 295 | #endif |
| 296 | |
| 297 | // raymarch scene |
| 298 | float t = tmin; |
| 299 | for( int i=0; i<256 && t<tmax; i++ ) |
| 300 | { |
| 301 | vec4 h = map( ro+rd*t, time ); |
| 302 | if( abs(h.x)<(0.0005*t) ) |
| 303 | { |
| 304 | res = vec4(t,h.yzw); |
| 305 | break; |
| 306 | } |
| 307 | t += h.x; |
| 308 | } |
| 309 | |
| 310 | return res; |
| 311 | } |
| 312 | |
| 313 | // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm |
| 314 | float calcSoftshadow( in vec3 ro, in vec3 rd, float time ) |
| 315 | { |
| 316 | float res = 1.0; |
| 317 | |
| 318 | float tmax = 12.0; |
| 319 | #if 1 |
| 320 | float tp = (3.5-ro.y)/rd.y; // raytrace bounding plane |
| 321 | if( tp>0.0 ) tmax = min( tmax, tp ); |
| 322 | #endif |
| 323 | |
| 324 | float t = 0.02; |
| 325 | for( int i=0; i<50; i++ ) |
| 326 | { |
| 327 | float h = map( ro + rd*t, time ).x; |
| 328 | res = min( res, mix(1.0,16.0*h/t, hsha) ); |
| 329 | t += clamp( h, 0.05, 0.40 ); |
| 330 | if( res<0.005 || t>tmax ) break; |
| 331 | } |
| 332 | return clamp( res, 0.0, 1.0 ); |
| 333 | } |
| 334 | |
| 335 | // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm |
| 336 | vec3 calcNormal( in vec3 pos, float time ) |
| 337 | { |
| 338 | |
| 339 | #if 0 |
| 340 | vec2 e = vec2(1.0,-1.0)*0.5773*0.001; |
| 341 | return normalize( e.xyy*map( pos + e.xyy, time ).x + |
| 342 | e.yyx*map( pos + e.yyx, time ).x + |
| 343 | e.yxy*map( pos + e.yxy, time ).x + |
| 344 | e.xxx*map( pos + e.xxx, time ).x ); |
| 345 | #else |
| 346 | // inspired by tdhooper and klems - a way to prevent the compiler from inlining map() 4 times |
| 347 | vec3 n = vec3(0.0); |
| 348 | for( int i=ZERO; i<4; i++ ) |
| 349 | { |
| 350 | vec3 e = 0.5773*(2.0*vec3((((i+3)>>1)&1),((i>>1)&1),(i&1))-1.0); |
| 351 | n += e*map(pos+0.001*e,time).x; |
| 352 | } |
| 353 | return normalize(n); |
| 354 | #endif |
| 355 | } |
| 356 | |
| 357 | float calcOcclusion( in vec3 pos, in vec3 nor, float time ) |
| 358 | { |
| 359 | float occ = 0.0; |
| 360 | float sca = 1.0; |
| 361 | for( int i=ZERO; i<5; i++ ) |
| 362 | { |
| 363 | float h = 0.01 + 0.11*float(i)/4.0; |
| 364 | vec3 opos = pos + h*nor; |
| 365 | float d = map( opos, time ).x; |
| 366 | occ += (h-d)*sca; |
| 367 | sca *= 0.95; |
| 368 | } |
| 369 | return clamp( 1.0 - 2.0*occ, 0.0, 1.0 ); |
| 370 | } |
| 371 | |
| 372 | vec3 render( in vec3 ro, in vec3 rd, float time ) |
| 373 | { |
| 374 | // sky dome |
| 375 | vec3 col = vec3(0.5, 0.8, 0.9) - max(rd.y,0.0)*0.5; |
| 376 | // sky clouds |
| 377 | vec2 uv = 1.5*rd.xz/rd.y; |
| 378 | float cl = 1.0*(sin(uv.x)+sin(uv.y)); uv *= mat2(0.8,0.6,-0.6,0.8)*2.1; |
| 379 | cl += 0.5*(sin(uv.x)+sin(uv.y)); |
| 380 | col += 0.1*(-1.0+2.0*smoothstep(-0.1,0.1,cl-0.4)); |
| 381 | // sky horizon |
| 382 | col = mix( col, vec3(0.5, 0.7, .9), exp(-10.0*max(rd.y,0.0)) ); |
| 383 | |
| 384 | |
| 385 | // scene geometry |
| 386 | vec4 res = raycast(ro,rd, time); |
| 387 | if( res.y>-0.5 ) |
| 388 | { |
| 389 | float t = res.x; |
| 390 | vec3 pos = ro + t*rd; |
| 391 | vec3 nor = calcNormal( pos, time ); |
| 392 | vec3 ref = reflect( rd, nor ); |
| 393 | float focc = res.w; |
| 394 | |
| 395 | // material |
| 396 | col = vec3(0.2); |
| 397 | float ks = 1.0; |
| 398 | |
| 399 | if( res.y>4.5 ) // candy |
| 400 | { |
| 401 | col = vec3(0.14,0.048,0.0); |
| 402 | vec2 id = floor(5.0*pos.xz+0.5); |
| 403 | col += 0.036*cos((id.x*11.1+id.y*37.341) + vec3(0.0,1.0,2.0) ); |
| 404 | col = max(col,0.0); |
| 405 | focc = clamp(4.0*res.z,0.0,1.0); |
| 406 | } |
| 407 | else if( res.y>3.5 ) // eyeball |
| 408 | { |
| 409 | col = vec3(0.0); |
| 410 | } |
| 411 | else if( res.y>2.5 ) // iris |
| 412 | { |
| 413 | col = vec3(0.4); |
| 414 | } |
| 415 | else if( res.y>1.5 ) // body |
| 416 | { |
| 417 | col = mix(vec3(0.144,0.09,0.0036),vec3(0.36,0.1,0.04),res.z*res.z); |
| 418 | col = mix(col,vec3(0.14,0.09,0.06)*2.0, (1.0-res.z)*smoothstep(-0.15, 0.15, -href)); |
| 419 | } |
| 420 | else // terrain |
| 421 | { |
| 422 | // base green |
| 423 | col = vec3(0.05,0.09,0.02); |
| 424 | float f = 0.2*(-1.0+2.0*smoothstep(-0.2,0.2,sin(18.0*pos.x)+sin(18.0*pos.y)+sin(18.0*pos.z))); |
| 425 | col += f*vec3(0.06,0.06,0.02); |
| 426 | ks = 0.5 + pos.y*0.15; |
| 427 | |
| 428 | // footprints |
| 429 | vec2 mp = vec2(pos.x-0.5*(mod(floor(pos.z+0.5),2.0)*2.0-1.0), fract(pos.z+0.5)-0.5 ); |
| 430 | float mark = 1.0-smoothstep(0.1, 0.5, length(mp)); |
| 431 | mark *= smoothstep(0.0, 0.1, floor(time) - floor(pos.z+0.5) ); |
| 432 | col *= mix( vec3(1.0), vec3(0.5,0.5,0.4), mark ); |
| 433 | ks *= 1.0-0.5*mark; |
| 434 | } |
| 435 | |
| 436 | // lighting (sun, sky, bounce, back, sss) |
| 437 | float occ = calcOcclusion( pos, nor, time )*focc; |
| 438 | float fre = clamp(1.0+dot(nor,rd),0.0,1.0); |
| 439 | |
| 440 | vec3 sun_lig = normalize( vec3(0.6, 0.35, 0.5) ); |
| 441 | float sun_dif = clamp(dot( nor, sun_lig ), 0.0, 1.0 ); |
| 442 | vec3 sun_hal = normalize( sun_lig-rd ); |
| 443 | float sun_sha = calcSoftshadow( pos, sun_lig, time ); |
| 444 | float sun_spe = ks*pow(clamp(dot(nor,sun_hal),0.0,1.0),8.0)*sun_dif*(0.04+0.96*pow(clamp(1.0+dot(sun_hal,rd),0.0,1.0),5.0)); |
| 445 | float sky_dif = sqrt(clamp( 0.5+0.5*nor.y, 0.0, 1.0 )); |
| 446 | float sky_spe = ks*smoothstep( 0.0, 0.5, ref.y )*(0.04+0.96*pow(fre,4.0)); |
| 447 | float bou_dif = sqrt(clamp( 0.1-0.9*nor.y, 0.0, 1.0 ))*clamp(1.0-0.1*pos.y,0.0,1.0); |
| 448 | float bac_dif = clamp(0.1+0.9*dot( nor, normalize(vec3(-sun_lig.x,0.0,-sun_lig.z))), 0.0, 1.0 ); |
| 449 | float sss_dif = fre*sky_dif*(0.25+0.75*sun_dif*sun_sha); |
| 450 | |
| 451 | vec3 lin = vec3(0.0); |
| 452 | lin += sun_dif*vec3(8.10,6.00,4.20)*vec3(sun_sha,sun_sha*sun_sha*0.5+0.5*sun_sha,sun_sha*sun_sha); |
| 453 | lin += sky_dif*vec3(0.50,0.70,1.00)*occ; |
| 454 | lin += bou_dif*vec3(0.20,0.70,0.10)*occ; |
| 455 | lin += bac_dif*vec3(0.45,0.35,0.25)*occ; |
| 456 | lin += sss_dif*vec3(3.25,2.75,2.50)*occ; |
| 457 | col = col*lin; |
| 458 | col += sun_spe*vec3(9.90,8.10,6.30)*sun_sha; |
| 459 | col += sky_spe*vec3(0.20,0.30,0.65)*occ*occ; |
| 460 | |
| 461 | col = pow(col,vec3(0.8,0.9,1.0) ); |
| 462 | |
| 463 | // fog |
| 464 | col = mix( col, vec3(0.5,0.7,0.9), 1.0-exp( -0.0001*t*t*t ) ); |
| 465 | } |
| 466 | |
| 467 | return col; |
| 468 | } |
| 469 | |
| 470 | mat3 setCamera( in vec3 ro, in vec3 ta, float cr ) |
| 471 | { |
| 472 | vec3 cw = normalize(ta-ro); |
| 473 | vec3 cp = vec3(sin(cr), cos(cr),0.0); |
| 474 | vec3 cu = normalize( cross(cw,cp) ); |
| 475 | vec3 cv = ( cross(cu,cw) ); |
| 476 | return mat3( cu, cv, cw ); |
| 477 | } |
| 478 | |
| 479 | //void mainImage( out vec4 fragColor, in vec2 fragCoord ) |
| 480 | vec4 mainImage( vec2 fragCoord ) |
| 481 | { |
| 482 | vec3 tot = vec3(0.0); |
| 483 | #if AA>1 |
| 484 | for( int m=ZERO; m<AA; m++ ) |
| 485 | for( int n=ZERO; n<AA; n++ ) |
| 486 | { |
| 487 | // pixel coordinates |
| 488 | vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; |
| 489 | vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y; |
| 490 | // time coordinate (motion blurred, shutter=0.5) |
| 491 | float d = 0.5+0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0); |
| 492 | float time = iTime - 0.5*(1.0/24.0)*(float(m*AA+n)+d)/float(AA*AA); |
| 493 | #else |
| 494 | vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y; |
| 495 | float time = iTime; |
| 496 | #endif |
| 497 | time += -2.6; |
| 498 | time *= 0.9; |
| 499 | |
| 500 | // camera |
| 501 | float cl = sin(0.5*time); |
| 502 | float an = 1.57 + 0.7*sin(0.15*time); |
| 503 | vec3 ta = vec3( 0.0, 0.65, -0.6+time*1.0 - 0.4*cl); |
| 504 | vec3 ro = ta + vec3( 1.3*cos(an), -0.250, 1.3*sin(an) ); |
| 505 | float ti = fract(time-0.15); |
| 506 | ti = 4.0*ti*(1.0-ti); |
| 507 | ta.y += 0.15*ti*ti*(3.0-2.0*ti)*smoothstep(0.4,0.9,cl); |
| 508 | |
| 509 | // camera bounce |
| 510 | float t4 = abs(fract(time*0.5)-0.5)/0.5; |
| 511 | float bou = -1.0 + 2.0*t4; |
| 512 | ro += 0.06*sin(time*12.0+vec3(0.0,2.0,4.0))*smoothstep( 0.85, 1.0, abs(bou) ); |
| 513 | |
| 514 | // camera-to-world rotation |
| 515 | mat3 ca = setCamera( ro, ta, 0.0 ); |
| 516 | |
| 517 | // ray direction |
| 518 | vec3 rd = ca * normalize( vec3(p,1.8) ); |
| 519 | |
| 520 | // render |
| 521 | vec3 col = render( ro, rd, time ); |
| 522 | |
| 523 | // color grading |
| 524 | col = col*vec3(1.11,0.89,0.79); |
| 525 | |
| 526 | // compress |
| 527 | col = 1.35*col/(1.0+col); |
| 528 | |
| 529 | // gamma |
| 530 | col = pow( col, vec3(0.4545) ); |
| 531 | |
| 532 | tot += col; |
| 533 | #if AA>1 |
| 534 | } |
| 535 | tot /= float(AA*AA); |
| 536 | #endif |
| 537 | |
| 538 | // s-surve |
| 539 | tot = clamp(tot,0.0,1.0); |
| 540 | tot = tot*tot*(3.0-2.0*tot); |
| 541 | |
| 542 | // vignetting |
| 543 | vec2 q = fragCoord/iResolution.xy; |
| 544 | tot *= 0.5 + 0.5*pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.25); |
| 545 | |
| 546 | // output |
| 547 | //fragColor = vec4( tot, 1.0 ); |
| 548 | return vec4( tot, 1.0 ); |
| 549 | } |
| 550 | |
| 551 | //********************************************************* |
| 552 | // END Ray Marching |
| 553 | //********************************************************* |
| 554 | |
| 555 | void main() { |
| 556 | vec4 c = color; |
| 557 | vec4 txt = texture(sampler2D(tex, smp), uv); |
| 558 | c = txt * c; |
| 559 | vec2 uv1 = uv * iResolution; |
| 560 | vec4 col_ray = mainImage(uv1); |
| 561 | |
| 562 | // use this to mix the chessboart texture with the ray marching |
| 563 | //frag_color = clamp(c*iMouse.y/512.0,0.0,1.0) * col_ray ; |
| 564 | |
| 565 | frag_color = c*0.00001 + col_ray ; |
| 566 | } |
| 567 | |
| 568 | #pragma sokol @end |
| 569 | |
| 570 | #pragma sokol @program rt_puppy vs_p fs_p |
| 571 | |