id
stringlengths 6
6
| author
stringclasses 55
values | date
timestamp[ns] | image_code
stringlengths 746
52.3k
| license
stringclasses 7
values | func_bytes
sequencelengths 5
5
| functions
sequencelengths 1
32
| comment
stringlengths 7
1.29k
| header
stringlengths 18
169
| body
stringlengths 18
2.14k
| model_inp
stringlengths 30
1.35k
| function_frequency
int64 1
176
| header_frequency
int64 1
16.3k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
lsfXWH | iq | 2014-05-15T00:14:43 | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org
// Four bands of Spherical Harmonics functions (or atomic orbitals if you want). For
// reference and fun.
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // antialias level (try 1, 2, 3, ...)
#endif
//#define SHOW_SPHERES
//---------------------------------------------------------------------------------
// Constants, see here: http://en.wikipedia.org/wiki/Table_of_spherical_harmonics
#define k01 0.2820947918 // sqrt( 1/PI)/2
#define k02 0.4886025119 // sqrt( 3/PI)/2
#define k03 1.0925484306 // sqrt( 15/PI)/2
#define k04 0.3153915652 // sqrt( 5/PI)/4
#define k05 0.5462742153 // sqrt( 15/PI)/4
#define k06 0.5900435860 // sqrt( 70/PI)/8
#define k07 2.8906114210 // sqrt(105/PI)/2
#define k08 0.4570214810 // sqrt( 42/PI)/8
#define k09 0.3731763300 // sqrt( 7/PI)/4
#define k10 1.4453057110 // sqrt(105/PI)/4
// Y_l_m(s), where l is the band and m the range in [-l..l]
float SH( in int l, in int m, in vec3 s )
{
vec3 n = s.zxy;
//----------------------------------------------------------
if( l==0 ) return k01;
//----------------------------------------------------------
if( l==1 && m==-1 ) return -k02*n.y;
if( l==1 && m== 0 ) return k02*n.z;
if( l==1 && m== 1 ) return -k02*n.x;
//----------------------------------------------------------
if( l==2 && m==-2 ) return k03*n.x*n.y;
if( l==2 && m==-1 ) return -k03*n.y*n.z;
if( l==2 && m== 0 ) return k04*(3.0*n.z*n.z-1.0);
if( l==2 && m== 1 ) return -k03*n.x*n.z;
if( l==2 && m== 2 ) return k05*(n.x*n.x-n.y*n.y);
//----------------------------------------------------------
if( l==3 && m==-3 ) return -k06*n.y*(3.0*n.x*n.x-n.y*n.y);
if( l==3 && m==-2 ) return k07*n.z*n.y*n.x;
if( l==3 && m==-1 ) return -k08*n.y*(5.0*n.z*n.z-1.0);
if( l==3 && m== 0 ) return k09*n.z*(5.0*n.z*n.z-3.0);
if( l==3 && m== 1 ) return -k08*n.x*(5.0*n.z*n.z-1.0);
if( l==3 && m== 2 ) return k10*n.z*(n.x*n.x-n.y*n.y);
if( l==3 && m== 3 ) return -k06*n.x*(n.x*n.x-3.0*n.y*n.y);
//----------------------------------------------------------
return 0.0;
}
// unrolled version of the above
float SH_0_0( in vec3 s ) { vec3 n = s.zxy; return k01; }
float SH_1_0( in vec3 s ) { vec3 n = s.zxy; return -k02*n.y; }
float SH_1_1( in vec3 s ) { vec3 n = s.zxy; return k02*n.z; }
float SH_1_2( in vec3 s ) { vec3 n = s.zxy; return -k02*n.x; }
float SH_2_0( in vec3 s ) { vec3 n = s.zxy; return k03*n.x*n.y; }
float SH_2_1( in vec3 s ) { vec3 n = s.zxy; return -k03*n.y*n.z; }
float SH_2_2( in vec3 s ) { vec3 n = s.zxy; return k04*(3.0*n.z*n.z-1.0); }
float SH_2_3( in vec3 s ) { vec3 n = s.zxy; return -k03*n.x*n.z; }
float SH_2_4( in vec3 s ) { vec3 n = s.zxy; return k05*(n.x*n.x-n.y*n.y); }
float SH_3_0( in vec3 s ) { vec3 n = s.zxy; return -k06*n.y*(3.0*n.x*n.x-n.y*n.y); }
float SH_3_1( in vec3 s ) { vec3 n = s.zxy; return k07*n.z*n.y*n.x; }
float SH_3_2( in vec3 s ) { vec3 n = s.zxy; return -k08*n.y*(5.0*n.z*n.z-1.0); }
float SH_3_3( in vec3 s ) { vec3 n = s.zxy; return k09*n.z*(5.0*n.z*n.z-3.0); }
float SH_3_4( in vec3 s ) { vec3 n = s.zxy; return -k08*n.x*(5.0*n.z*n.z-1.0); }
float SH_3_5( in vec3 s ) { vec3 n = s.zxy; return k10*n.z*(n.x*n.x-n.y*n.y); }
float SH_3_6( in vec3 s ) { vec3 n = s.zxy; return -k06*n.x*(n.x*n.x-3.0*n.y*n.y); }
vec3 map( in vec3 p )
{
vec3 p00 = p - vec3( 0.00, 2.5,0.0);
vec3 p01 = p - vec3(-1.25, 1.0,0.0);
vec3 p02 = p - vec3( 0.00, 1.0,0.0);
vec3 p03 = p - vec3( 1.25, 1.0,0.0);
vec3 p04 = p - vec3(-2.50,-0.5,0.0);
vec3 p05 = p - vec3(-1.25,-0.5,0.0);
vec3 p06 = p - vec3( 0.00,-0.5,0.0);
vec3 p07 = p - vec3( 1.25,-0.5,0.0);
vec3 p08 = p - vec3( 2.50,-0.5,0.0);
vec3 p09 = p - vec3(-3.75,-2.0,0.0);
vec3 p10 = p - vec3(-2.50,-2.0,0.0);
vec3 p11 = p - vec3(-1.25,-2.0,0.0);
vec3 p12 = p - vec3( 0.00,-2.0,0.0);
vec3 p13 = p - vec3( 1.25,-2.0,0.0);
vec3 p14 = p - vec3( 2.50,-2.0,0.0);
vec3 p15 = p - vec3( 3.75,-2.0,0.0);
float r, d; vec3 n, s, res;
#ifdef SHOW_SPHERES
#define SHAPE (vec3(d-0.35, -1.0+2.0*clamp(0.5 + 16.0*r,0.0,1.0),d))
#else
#define SHAPE (vec3(d-abs(r), sign(r),d))
#endif
d=length(p00); n=p00/d; r = SH_0_0( n ); s = SHAPE; res = s;
d=length(p01); n=p01/d; r = SH_1_0( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p02); n=p02/d; r = SH_1_1( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p03); n=p03/d; r = SH_1_2( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p04); n=p04/d; r = SH_2_0( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p05); n=p05/d; r = SH_2_1( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p06); n=p06/d; r = SH_2_2( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p07); n=p07/d; r = SH_2_3( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p08); n=p08/d; r = SH_2_4( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p09); n=p09/d; r = SH_3_0( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p10); n=p10/d; r = SH_3_1( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p11); n=p11/d; r = SH_3_2( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p12); n=p12/d; r = SH_3_3( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p13); n=p13/d; r = SH_3_4( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p14); n=p14/d; r = SH_3_5( n ); s = SHAPE; if( s.x<res.x ) res=s;
d=length(p15); n=p15/d; r = SH_3_6( n ); s = SHAPE; if( s.x<res.x ) res=s;
return vec3( res.x, 0.5+0.5*res.y, res.z );
}
vec3 intersect( in vec3 ro, in vec3 rd )
{
vec3 res = vec3(1e10,-1.0, 1.0);
float maxd = 10.0;
float h = 1.0;
float t = 0.0;
vec2 m = vec2(-1.0);
for( int i=0; i<200; i++ )
{
if( h<0.001||t>maxd ) break;
vec3 res = map( ro+rd*t );
h = res.x;
m = res.yz;
t += h*0.3;
}
if( t<maxd && t<res.x ) res=vec3(t,m);
return res;
}
vec3 calcNormal( in vec3 pos )
{
vec3 eps = vec3(0.001,0.0,0.0);
return normalize( vec3(
map(pos+eps.xyy).x - map(pos-eps.xyy).x,
map(pos+eps.yxy).x - map(pos-eps.yxy).x,
map(pos+eps.yyx).x - map(pos-eps.yyx).x ) );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera
float an = 0.314*iTime - 10.0*iMouse.x/iResolution.x;
vec3 ro = vec3(6.0*sin(an),0.0,6.0*cos(an));
vec3 ta = vec3(0.0,0.0,0.0);
// camera matrix
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
vec3 tot = vec3(0.0);
#define ZERO min(iFrame,0)
for( int m=ZERO; m<AA; m++ )
for( int n=ZERO; n<AA; n++ )
{
vec2 p = (-iResolution.xy + 2.0*(fragCoord+vec2(float(m),float(n))/float(AA))) / iResolution.y;
// create view ray
vec3 rd = normalize( p.x*uu + p.y*vv + 2.0*ww );
// background
vec3 col = vec3(0.3) * clamp(1.0-length(p)*0.45,0.0,1.0);
// raymarch
vec3 tmat = intersect(ro,rd);
if( tmat.y>-0.5 )
{
// geometry
vec3 pos = ro + tmat.x*rd;
vec3 nor = calcNormal(pos);
vec3 ref = reflect( rd, nor );
// material
vec3 mate = 0.5*mix( vec3(1.0,0.6,0.15), vec3(0.2,0.4,0.5), tmat.y );
float occ = clamp( 2.0*tmat.z, 0.0, 1.0 );
float sss = pow( clamp( 1.0 + dot(nor,rd), 0.0, 1.0 ), 1.0 );
// lights
vec3 lin = 2.5*occ*vec3(1.0,1.00,1.00)*(0.6+0.4*nor.y);
lin += 1.0*sss*vec3(1.0,0.95,0.70)*occ;
// surface-light interacion
col = mate.xyz * lin;
}
// gamma
col = pow( clamp(col,0.0,1.0), vec3(0.4545) );
tot += col;
}
tot /= float(AA*AA);
// bad dither
tot += (1.0/255.0)*fract(sin(fragCoord.x+1111.0*fragCoord.y)*1111.0);
fragColor = vec4( tot, 1.0 );
}
| mit | [
3256,
3289,
3316,
3316,
3347
] | [
[
1978,
2039,
2083,
2083,
3254
],
[
3256,
3289,
3316,
3316,
3347
],
[
3348,
3348,
3375,
3375,
3410
],
[
3411,
3411,
3438,
3438,
3473
],
[
3474,
3474,
3501,
3501,
3536
],
[
3537,
3537,
3564,
3564,
3603
],
[
3604,
3604,
3631,
3631,
3670
],
[
3671,
3671,
3698,
3698,
3747
],
[
3748,
3748,
3775,
3775,
3814
],
[
3815,
3815,
3842,
3842,
3891
],
[
3892,
3892,
3919,
3919,
3976
],
[
3977,
3977,
4004,
4004,
4047
],
[
4048,
4048,
4075,
4075,
4128
],
[
4129,
4129,
4156,
4156,
4209
],
[
4210,
4210,
4237,
4237,
4290
],
[
4291,
4291,
4318,
4318,
4371
],
[
4372,
4372,
4399,
4399,
4456
],
[
4458,
4458,
4481,
4481,
6528
],
[
6530,
6530,
6572,
6572,
6914
],
[
6916,
6916,
6948,
6948,
7172
],
[
7174,
7174,
7231,
7245,
8939
]
] | // unrolled version of the above
| float SH_0_0( in vec3 s ) { | vec3 n = s.zxy; return k01; } | // unrolled version of the above
float SH_0_0( in vec3 s ) { | 3 | 3 |
4ssSRl | iq | 2014-07-18T08:21:44 | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org
// distance to a line (can't get simpler than this)
float line( in vec2 a, in vec2 b, in vec2 p )
{
vec2 pa = p - a;
vec2 ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (-iResolution.xy + 2.0*fragCoord.xy) / iResolution.yy;
vec2 q = p;
vec2 c = vec2(0.0);
if( iMouse.z>0.0 ) c=(-iResolution.xy + 2.0*iMouse.xy) / iResolution.yy;
// background
vec3 col = vec3(0.5,0.85,0.9)*(1.0-0.2*length(p));
if( q.x>c.x && q.y>c.y ) col = pow(col,vec3(2.2));
// zoom in and out
p *= 1.0 + 0.2*sin(iTime*0.4);
// compute distance to a set of lines
float d = 1e20;
for( int i=0; i<7; i++ )
{
float anA = 6.2831*float(i+0)/7.0 + 0.15*iTime;
float anB = 6.2831*float(i+3)/7.0 + 0.20*iTime;
vec2 pA = 0.95*vec2( cos(anA), sin(anA) );
vec2 pB = 0.95*vec2( cos(anB), sin(anB) );
float h = line( pA, pB, p );
d = min( d, h );
}
// lines/start, left side of screen : not filtered
if( q.x<c.x )
{
if( d<0.12 ) col = vec3(0.0,0.0,0.0); // black
if( d<0.04 ) col = vec3(1.0,0.6,0.0); // orange
}
// lines/start, right side of the screen: filtered
else
{
float w = 0.5*fwidth(d);
w *= 1.5; // extra blur
if( q.y<c.y )
{
col = mix( vec3(0.0,0.0,0.0), col, smoothstep(-w,w,d-0.12) ); // black
col = mix( vec3(1.0,0.6,0.0), col, smoothstep(-w,w,d-0.04) ); // orange
}
else
{
col = mix( pow(vec3(0.0,0.0,0.0),vec3(2.2)), col, smoothstep(-w,w,d-0.12) ); // black
col = mix( pow(vec3(1.0,0.6,0.0),vec3(2.2)), col, smoothstep(-w,w,d-0.04) ); // orange
}
}
if( q.x>c.x && q.y>c.y )
col = pow( col, vec3(1.0/2.2) );
// draw left/right separating line
col = mix( vec3(0.0), col, smoothstep(0.007,0.008,abs(q.x-c.x)) );
col = mix( col, vec3(0.0), (1.0-smoothstep(0.007,0.008,abs(q.y-c.y)))*step(0.0,q.x-c.x) );
fragColor = vec4( col, 1.0 );
} | mit | [
0,
1196,
1243,
1243,
1363
] | [
[
0,
1196,
1243,
1243,
1363
],
[
1365,
1365,
1422,
1422,
3103
]
] | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org
// distance to a line (can't get simpler than this)
| float line( in vec2 a, in vec2 b, in vec2 p )
{ |
vec2 pa = p - a;
vec2 ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
} | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org
// distance to a line (can't get simpler than this)
float line( in vec2 a, in vec2 b, in vec2 p )
{ | 3 | 5 |
4djXzz | otaviogood | 2014-08-21T06:53:07 | /*--------------------------------------------------------------------------------------
License CC0 - http://creativecommons.org/publicdomain/zero/1.0/
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
----------------------------------------------------------------------------------------
^ This means do ANYTHING YOU WANT with this code. Because we are programmers, not lawyers.
-Otavio Good
*/
// various noise functions
float Hash2d(vec2 uv)
{
float f = uv.x + uv.y * 47.0;
return fract(cos(f*3.333)*100003.9);
}
float Hash3d(vec3 uv)
{
float f = uv.x + uv.y * 37.0 + uv.z * 521.0;
return fract(cos(f*3.333)*100003.9);
}
float mixP(float f0, float f1, float a)
{
return mix(f0, f1, a*a*(3.0-2.0*a));
}
const vec2 zeroOne = vec2(0.0, 1.0);
float noise2d(vec2 uv)
{
vec2 fr = fract(uv.xy);
vec2 fl = floor(uv.xy);
float h00 = Hash2d(fl);
float h10 = Hash2d(fl + zeroOne.yx);
float h01 = Hash2d(fl + zeroOne);
float h11 = Hash2d(fl + zeroOne.yy);
return mixP(mixP(h00, h10, fr.x), mixP(h01, h11, fr.x), fr.y);
}
float noise2dT(vec2 uv)
{
vec2 fr = fract(uv);
vec2 smoothv = fr*fr*(3.0-2.0*fr);
vec2 fl = floor(uv);
uv = smoothv + fl;
return textureLod(iChannel0, (uv + 0.5)/iChannelResolution[0].xy, 0.0).y; // use constant here instead?
}
float noise(vec3 uv)
{
vec3 fr = fract(uv.xyz);
vec3 fl = floor(uv.xyz);
float h000 = Hash3d(fl);
float h100 = Hash3d(fl + zeroOne.yxx);
float h010 = Hash3d(fl + zeroOne.xyx);
float h110 = Hash3d(fl + zeroOne.yyx);
float h001 = Hash3d(fl + zeroOne.xxy);
float h101 = Hash3d(fl + zeroOne.yxy);
float h011 = Hash3d(fl + zeroOne.xyy);
float h111 = Hash3d(fl + zeroOne.yyy);
return mixP(
mixP(mixP(h000, h100, fr.x), mixP(h010, h110, fr.x), fr.y),
mixP(mixP(h001, h101, fr.x), mixP(h011, h111, fr.x), fr.y)
, fr.z);
}
float PI=3.14159265;
vec3 saturate(vec3 a)
{
return clamp(a, 0.0, 1.0);
}
vec2 saturate(vec2 a)
{
return clamp(a, 0.0, 1.0);
}
float saturate(float a)
{
return clamp(a, 0.0, 1.0);
}
float Density(vec3 p)
{
//float ws = 0.06125*0.125;
//vec3 warp = vec3(noise(p*ws), noise(p*ws + 111.11), noise(p*ws + 7111.11));
float final = noise(p*0.06125);// + sin(iTime)*0.5-1.95 + warp.x*4.0;
float other = noise(p*0.06125 + 1234.567);
other -= 0.5;
final -= 0.5;
final = 0.1/(abs(final*final*other));
final += 0.5;
return final*0.0001;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// ---------------- First, set up the camera rays for ray marching ----------------
vec2 uv = fragCoord.xy/iResolution.xy * 2.0 - 1.0;// - 0.5;
// Camera up vector.
vec3 camUp=vec3(0,1,0); // vuv
// Camera lookat.
vec3 camLookat=vec3(0,0.0,0); // vrp
float mx=iMouse.x/iResolution.x*PI*2.0 + iTime * 0.01;
float my=-iMouse.y/iResolution.y*10.0 + sin(iTime * 0.03)*0.2+0.2;//*PI/2.01;
vec3 camPos=vec3(cos(my)*cos(mx),sin(my),cos(my)*sin(mx))*(200.2); // prp
// Camera setup.
vec3 camVec=normalize(camLookat - camPos);//vpn
vec3 sideNorm=normalize(cross(camUp, camVec)); // u
vec3 upNorm=cross(camVec, sideNorm);//v
vec3 worldFacing=(camPos + camVec);//vcv
vec3 worldPix = worldFacing + uv.x * sideNorm * (iResolution.x/iResolution.y) + uv.y * upNorm;//scrCoord
vec3 relVec = normalize(worldPix - camPos);//scp
// --------------------------------------------------------------------------------
float t = 0.0;
float inc = 0.02;
float maxDepth = 70.0;
vec3 pos = vec3(0,0,0);
float density = 0.0;
// ray marching time
for (int i = 0; i < 37; i++) // This is the count of how many times the ray actually marches.
{
if ((t > maxDepth)) break;
pos = camPos + relVec * t;
float temp = Density(pos);
//temp *= saturate(t-1.0);
inc = 1.9 + temp*0.05; // add temp because this makes it look extra crazy!
density += temp * inc;
t += inc;
}
// --------------------------------------------------------------------------------
// Now that we have done our ray marching, let's put some color on this.
vec3 finalColor = vec3(0.01,0.1,1.0)* density*0.2;
// output the final color with sqrt for "gamma correction"
fragColor = vec4(sqrt(clamp(finalColor, 0.0, 1.0)),1.0);
}
| cc0-1.0 | [
562,
589,
612,
612,
689
] | [
[
562,
589,
612,
612,
689
],
[
690,
690,
713,
713,
805
],
[
806,
806,
847,
847,
890
],
[
928,
928,
952,
952,
1225
],
[
1226,
1226,
1251,
1251,
1473
],
[
1474,
1474,
1496,
1496,
2055
],
[
2079,
2079,
2102,
2102,
2132
],
[
2133,
2133,
2156,
2156,
2186
],
[
2187,
2187,
2212,
2212,
2242
],
[
2244,
2244,
2267,
2381,
2625
],
[
2627,
2627,
2684,
2769,
4448
]
] | // various noise functions
| float Hash2d(vec2 uv)
{ |
float f = uv.x + uv.y * 47.0;
return fract(cos(f*3.333)*100003.9);
} | // various noise functions
float Hash2d(vec2 uv)
{ | 4 | 17 |
4dBXz3 | iq | 2014-10-24T08:55:07 | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// A useful trick to avoid certain type of discontinuities
// during rendering and procedural content generation. More info:
//
// https://iquilezles.org/www/articles/dontflip/dontflip.htm
// Flip v if in the negative half plane defined by r (this works in 3D too)
vec2 flipIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : -v;
}
// Reflect v if in the negative half plane defined by r (this works in 3D too)
vec2 reflIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : v-2.0*r*k;
}
// Clip v if in the negative half plane defined by r (this works in 3D too)
vec2 clipIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : (v-r*k)*inversesqrt(1.0-k*k/dot(v,v));
}
//===============================================================
float sdLine( in vec2 p, in vec2 a, in vec2 b )
{
vec2 pa = p - a;
vec2 ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
// https://www.shadertoy.com/view/slj3Dd
float sdArrow( in vec2 p, vec2 a, vec2 b, float w1, float w2 )
{
const float k = 3.0;
vec2 ba = b - a;
float l2 = dot(ba,ba);
float l = sqrt(l2);
p = p-a;
p = mat2(ba.x,-ba.y,ba.y,ba.x)*p/l;
p.y = abs(p.y);
vec2 pz = p-vec2(l-w2*k,w2);
vec2 q = p;
q.x -= clamp( q.x, 0.0, l-w2*k );
q.y -= w1;
float di = dot(q,q);
q = pz;
q.y -= clamp( q.y, w1-w2, 0.0 );
di = min( di, dot(q,q) );
if( p.x<w1 )
{
q = p;
q.y -= clamp( q.y, 0.0, w1 );
di = min( di, dot(q,q) );
}
if( pz.x>0.0 )
{
q = pz;
q -= vec2(k,-1.0)*clamp( (q.x*k-q.y)/(k*k+1.0), 0.0, w2 );
di = min( di, dot(q,q) );
}
float si = 1.0;
float z = l - p.x;
if( min(p.x,z)>0.0 )
{
float h = (pz.x<0.0) ? w1 : z/k;
if( p.y<h ) si = -1.0;
}
return si*sqrt(di);
}
//===============================================================
float line( in vec2 p, in vec2 a, in vec2 b, float w , float e)
{
return 1.0 - smoothstep( -e, e, sdLine( p, a, b ) - w );
}
float arrow( in vec2 p, in vec2 a, in vec2 b, float w1, float w2, float e )
{
return 1.0 - smoothstep( -e, e, sdArrow( p, a, b, w1, w2) );
}
//===============================================================
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord/iResolution.x;
vec2 q = p;
p.x = mod(p.x,1.0/3.0) - 1.0/6.0;
p.y -= 0.5*iResolution.y/iResolution.x;
p.y += 0.04;
float e = 1.0/iResolution.x;
float time = iTime;
//time = mod( time, 8.0 );
float an = 0.3*(1.0-smoothstep(-0.1,0.1,sin(0.125*6.283185*(time+1.0/2.0))));
vec2 r = vec2( sin(an), cos(an) );
vec2 pe = r.yx*vec2(-1.0,1.0);
vec3 col = vec3(0.15);
col = vec3(21,32,43)/255.0;
float wi = 0.0015;
float s = dot(p,r);
if( s>0.0 )
{
float r = length(p);
if( r<0.12 )
{
float nr = r/0.12;
col += 0.25*nr*nr;
}
col = mix(col,vec3(0.7), 1.0-smoothstep(-e,e,abs(r-0.12)-wi));
}
col = mix( col, vec3(0.7), arrow(p, vec2(0.0), r*0.18, wi, 0.01, e) );
col = mix( col, vec3(0.7), line(p, -0.12*pe, 0.12*pe, wi, e) );
{
float an = cos(0.5*6.283185*time);
vec2 v = vec2( -cos(an), sin(an) )*0.12;
vec2 f;
if( q.x<0.333 ) f = flipIfNeg( v, r );
else if( q.x<0.666 ) f = reflIfNeg( v, r );
else f = clipIfNeg( v, r );
col = mix( col, col+0.2, arrow(p, vec2(0.0), v, wi, 5.0*wi, e) );
col = mix( col, vec3(1.0,0.7,0.2), arrow(p, vec2(0.0), f, wi, 5.0*wi, e) );
}
fragColor = vec4( col, 1.0 );
} | mit | [
1272,
1348,
1388,
1388,
1443
] | [
[
1272,
1348,
1388,
1388,
1443
],
[
1445,
1524,
1564,
1564,
1626
],
[
1628,
1704,
1744,
1744,
1834
],
[
1903,
1903,
1952,
1952,
2072
],
[
2074,
2115,
2179,
2179,
2977
],
[
3046,
3046,
3111,
3111,
3174
],
[
3176,
3176,
3253,
3253,
3320
],
[
3389,
3389,
3446,
3446,
4797
]
] | // Flip v if in the negative half plane defined by r (this works in 3D too)
| vec2 flipIfNeg( in vec2 v, in vec2 r )
{ |
float k = dot(v,r);
return (k>0.0) ? v : -v;
} | // Flip v if in the negative half plane defined by r (this works in 3D too)
vec2 flipIfNeg( in vec2 v, in vec2 r )
{ | 1 | 1 |
4dBXz3 | iq | 2014-10-24T08:55:07 | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// A useful trick to avoid certain type of discontinuities
// during rendering and procedural content generation. More info:
//
// https://iquilezles.org/www/articles/dontflip/dontflip.htm
// Flip v if in the negative half plane defined by r (this works in 3D too)
vec2 flipIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : -v;
}
// Reflect v if in the negative half plane defined by r (this works in 3D too)
vec2 reflIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : v-2.0*r*k;
}
// Clip v if in the negative half plane defined by r (this works in 3D too)
vec2 clipIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : (v-r*k)*inversesqrt(1.0-k*k/dot(v,v));
}
//===============================================================
float sdLine( in vec2 p, in vec2 a, in vec2 b )
{
vec2 pa = p - a;
vec2 ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
// https://www.shadertoy.com/view/slj3Dd
float sdArrow( in vec2 p, vec2 a, vec2 b, float w1, float w2 )
{
const float k = 3.0;
vec2 ba = b - a;
float l2 = dot(ba,ba);
float l = sqrt(l2);
p = p-a;
p = mat2(ba.x,-ba.y,ba.y,ba.x)*p/l;
p.y = abs(p.y);
vec2 pz = p-vec2(l-w2*k,w2);
vec2 q = p;
q.x -= clamp( q.x, 0.0, l-w2*k );
q.y -= w1;
float di = dot(q,q);
q = pz;
q.y -= clamp( q.y, w1-w2, 0.0 );
di = min( di, dot(q,q) );
if( p.x<w1 )
{
q = p;
q.y -= clamp( q.y, 0.0, w1 );
di = min( di, dot(q,q) );
}
if( pz.x>0.0 )
{
q = pz;
q -= vec2(k,-1.0)*clamp( (q.x*k-q.y)/(k*k+1.0), 0.0, w2 );
di = min( di, dot(q,q) );
}
float si = 1.0;
float z = l - p.x;
if( min(p.x,z)>0.0 )
{
float h = (pz.x<0.0) ? w1 : z/k;
if( p.y<h ) si = -1.0;
}
return si*sqrt(di);
}
//===============================================================
float line( in vec2 p, in vec2 a, in vec2 b, float w , float e)
{
return 1.0 - smoothstep( -e, e, sdLine( p, a, b ) - w );
}
float arrow( in vec2 p, in vec2 a, in vec2 b, float w1, float w2, float e )
{
return 1.0 - smoothstep( -e, e, sdArrow( p, a, b, w1, w2) );
}
//===============================================================
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord/iResolution.x;
vec2 q = p;
p.x = mod(p.x,1.0/3.0) - 1.0/6.0;
p.y -= 0.5*iResolution.y/iResolution.x;
p.y += 0.04;
float e = 1.0/iResolution.x;
float time = iTime;
//time = mod( time, 8.0 );
float an = 0.3*(1.0-smoothstep(-0.1,0.1,sin(0.125*6.283185*(time+1.0/2.0))));
vec2 r = vec2( sin(an), cos(an) );
vec2 pe = r.yx*vec2(-1.0,1.0);
vec3 col = vec3(0.15);
col = vec3(21,32,43)/255.0;
float wi = 0.0015;
float s = dot(p,r);
if( s>0.0 )
{
float r = length(p);
if( r<0.12 )
{
float nr = r/0.12;
col += 0.25*nr*nr;
}
col = mix(col,vec3(0.7), 1.0-smoothstep(-e,e,abs(r-0.12)-wi));
}
col = mix( col, vec3(0.7), arrow(p, vec2(0.0), r*0.18, wi, 0.01, e) );
col = mix( col, vec3(0.7), line(p, -0.12*pe, 0.12*pe, wi, e) );
{
float an = cos(0.5*6.283185*time);
vec2 v = vec2( -cos(an), sin(an) )*0.12;
vec2 f;
if( q.x<0.333 ) f = flipIfNeg( v, r );
else if( q.x<0.666 ) f = reflIfNeg( v, r );
else f = clipIfNeg( v, r );
col = mix( col, col+0.2, arrow(p, vec2(0.0), v, wi, 5.0*wi, e) );
col = mix( col, vec3(1.0,0.7,0.2), arrow(p, vec2(0.0), f, wi, 5.0*wi, e) );
}
fragColor = vec4( col, 1.0 );
} | mit | [
1445,
1524,
1564,
1564,
1626
] | [
[
1272,
1348,
1388,
1388,
1443
],
[
1445,
1524,
1564,
1564,
1626
],
[
1628,
1704,
1744,
1744,
1834
],
[
1903,
1903,
1952,
1952,
2072
],
[
2074,
2115,
2179,
2179,
2977
],
[
3046,
3046,
3111,
3111,
3174
],
[
3176,
3176,
3253,
3253,
3320
],
[
3389,
3389,
3446,
3446,
4797
]
] | // Reflect v if in the negative half plane defined by r (this works in 3D too)
| vec2 reflIfNeg( in vec2 v, in vec2 r )
{ |
float k = dot(v,r);
return (k>0.0) ? v : v-2.0*r*k;
} | // Reflect v if in the negative half plane defined by r (this works in 3D too)
vec2 reflIfNeg( in vec2 v, in vec2 r )
{ | 1 | 1 |
4dBXz3 | iq | 2014-10-24T08:55:07 | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// A useful trick to avoid certain type of discontinuities
// during rendering and procedural content generation. More info:
//
// https://iquilezles.org/www/articles/dontflip/dontflip.htm
// Flip v if in the negative half plane defined by r (this works in 3D too)
vec2 flipIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : -v;
}
// Reflect v if in the negative half plane defined by r (this works in 3D too)
vec2 reflIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : v-2.0*r*k;
}
// Clip v if in the negative half plane defined by r (this works in 3D too)
vec2 clipIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : (v-r*k)*inversesqrt(1.0-k*k/dot(v,v));
}
//===============================================================
float sdLine( in vec2 p, in vec2 a, in vec2 b )
{
vec2 pa = p - a;
vec2 ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
// https://www.shadertoy.com/view/slj3Dd
float sdArrow( in vec2 p, vec2 a, vec2 b, float w1, float w2 )
{
const float k = 3.0;
vec2 ba = b - a;
float l2 = dot(ba,ba);
float l = sqrt(l2);
p = p-a;
p = mat2(ba.x,-ba.y,ba.y,ba.x)*p/l;
p.y = abs(p.y);
vec2 pz = p-vec2(l-w2*k,w2);
vec2 q = p;
q.x -= clamp( q.x, 0.0, l-w2*k );
q.y -= w1;
float di = dot(q,q);
q = pz;
q.y -= clamp( q.y, w1-w2, 0.0 );
di = min( di, dot(q,q) );
if( p.x<w1 )
{
q = p;
q.y -= clamp( q.y, 0.0, w1 );
di = min( di, dot(q,q) );
}
if( pz.x>0.0 )
{
q = pz;
q -= vec2(k,-1.0)*clamp( (q.x*k-q.y)/(k*k+1.0), 0.0, w2 );
di = min( di, dot(q,q) );
}
float si = 1.0;
float z = l - p.x;
if( min(p.x,z)>0.0 )
{
float h = (pz.x<0.0) ? w1 : z/k;
if( p.y<h ) si = -1.0;
}
return si*sqrt(di);
}
//===============================================================
float line( in vec2 p, in vec2 a, in vec2 b, float w , float e)
{
return 1.0 - smoothstep( -e, e, sdLine( p, a, b ) - w );
}
float arrow( in vec2 p, in vec2 a, in vec2 b, float w1, float w2, float e )
{
return 1.0 - smoothstep( -e, e, sdArrow( p, a, b, w1, w2) );
}
//===============================================================
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord/iResolution.x;
vec2 q = p;
p.x = mod(p.x,1.0/3.0) - 1.0/6.0;
p.y -= 0.5*iResolution.y/iResolution.x;
p.y += 0.04;
float e = 1.0/iResolution.x;
float time = iTime;
//time = mod( time, 8.0 );
float an = 0.3*(1.0-smoothstep(-0.1,0.1,sin(0.125*6.283185*(time+1.0/2.0))));
vec2 r = vec2( sin(an), cos(an) );
vec2 pe = r.yx*vec2(-1.0,1.0);
vec3 col = vec3(0.15);
col = vec3(21,32,43)/255.0;
float wi = 0.0015;
float s = dot(p,r);
if( s>0.0 )
{
float r = length(p);
if( r<0.12 )
{
float nr = r/0.12;
col += 0.25*nr*nr;
}
col = mix(col,vec3(0.7), 1.0-smoothstep(-e,e,abs(r-0.12)-wi));
}
col = mix( col, vec3(0.7), arrow(p, vec2(0.0), r*0.18, wi, 0.01, e) );
col = mix( col, vec3(0.7), line(p, -0.12*pe, 0.12*pe, wi, e) );
{
float an = cos(0.5*6.283185*time);
vec2 v = vec2( -cos(an), sin(an) )*0.12;
vec2 f;
if( q.x<0.333 ) f = flipIfNeg( v, r );
else if( q.x<0.666 ) f = reflIfNeg( v, r );
else f = clipIfNeg( v, r );
col = mix( col, col+0.2, arrow(p, vec2(0.0), v, wi, 5.0*wi, e) );
col = mix( col, vec3(1.0,0.7,0.2), arrow(p, vec2(0.0), f, wi, 5.0*wi, e) );
}
fragColor = vec4( col, 1.0 );
} | mit | [
1628,
1704,
1744,
1744,
1834
] | [
[
1272,
1348,
1388,
1388,
1443
],
[
1445,
1524,
1564,
1564,
1626
],
[
1628,
1704,
1744,
1744,
1834
],
[
1903,
1903,
1952,
1952,
2072
],
[
2074,
2115,
2179,
2179,
2977
],
[
3046,
3046,
3111,
3111,
3174
],
[
3176,
3176,
3253,
3253,
3320
],
[
3389,
3389,
3446,
3446,
4797
]
] | // Clip v if in the negative half plane defined by r (this works in 3D too)
| vec2 clipIfNeg( in vec2 v, in vec2 r )
{ |
float k = dot(v,r);
return (k>0.0) ? v : (v-r*k)*inversesqrt(1.0-k*k/dot(v,v));
} | // Clip v if in the negative half plane defined by r (this works in 3D too)
vec2 clipIfNeg( in vec2 v, in vec2 r )
{ | 1 | 1 |
4dBXz3 | iq | 2014-10-24T08:55:07 | // The MIT License
// Copyright © 2014 Inigo Quilez
// 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.
// A useful trick to avoid certain type of discontinuities
// during rendering and procedural content generation. More info:
//
// https://iquilezles.org/www/articles/dontflip/dontflip.htm
// Flip v if in the negative half plane defined by r (this works in 3D too)
vec2 flipIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : -v;
}
// Reflect v if in the negative half plane defined by r (this works in 3D too)
vec2 reflIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : v-2.0*r*k;
}
// Clip v if in the negative half plane defined by r (this works in 3D too)
vec2 clipIfNeg( in vec2 v, in vec2 r )
{
float k = dot(v,r);
return (k>0.0) ? v : (v-r*k)*inversesqrt(1.0-k*k/dot(v,v));
}
//===============================================================
float sdLine( in vec2 p, in vec2 a, in vec2 b )
{
vec2 pa = p - a;
vec2 ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
// https://www.shadertoy.com/view/slj3Dd
float sdArrow( in vec2 p, vec2 a, vec2 b, float w1, float w2 )
{
const float k = 3.0;
vec2 ba = b - a;
float l2 = dot(ba,ba);
float l = sqrt(l2);
p = p-a;
p = mat2(ba.x,-ba.y,ba.y,ba.x)*p/l;
p.y = abs(p.y);
vec2 pz = p-vec2(l-w2*k,w2);
vec2 q = p;
q.x -= clamp( q.x, 0.0, l-w2*k );
q.y -= w1;
float di = dot(q,q);
q = pz;
q.y -= clamp( q.y, w1-w2, 0.0 );
di = min( di, dot(q,q) );
if( p.x<w1 )
{
q = p;
q.y -= clamp( q.y, 0.0, w1 );
di = min( di, dot(q,q) );
}
if( pz.x>0.0 )
{
q = pz;
q -= vec2(k,-1.0)*clamp( (q.x*k-q.y)/(k*k+1.0), 0.0, w2 );
di = min( di, dot(q,q) );
}
float si = 1.0;
float z = l - p.x;
if( min(p.x,z)>0.0 )
{
float h = (pz.x<0.0) ? w1 : z/k;
if( p.y<h ) si = -1.0;
}
return si*sqrt(di);
}
//===============================================================
float line( in vec2 p, in vec2 a, in vec2 b, float w , float e)
{
return 1.0 - smoothstep( -e, e, sdLine( p, a, b ) - w );
}
float arrow( in vec2 p, in vec2 a, in vec2 b, float w1, float w2, float e )
{
return 1.0 - smoothstep( -e, e, sdArrow( p, a, b, w1, w2) );
}
//===============================================================
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord/iResolution.x;
vec2 q = p;
p.x = mod(p.x,1.0/3.0) - 1.0/6.0;
p.y -= 0.5*iResolution.y/iResolution.x;
p.y += 0.04;
float e = 1.0/iResolution.x;
float time = iTime;
//time = mod( time, 8.0 );
float an = 0.3*(1.0-smoothstep(-0.1,0.1,sin(0.125*6.283185*(time+1.0/2.0))));
vec2 r = vec2( sin(an), cos(an) );
vec2 pe = r.yx*vec2(-1.0,1.0);
vec3 col = vec3(0.15);
col = vec3(21,32,43)/255.0;
float wi = 0.0015;
float s = dot(p,r);
if( s>0.0 )
{
float r = length(p);
if( r<0.12 )
{
float nr = r/0.12;
col += 0.25*nr*nr;
}
col = mix(col,vec3(0.7), 1.0-smoothstep(-e,e,abs(r-0.12)-wi));
}
col = mix( col, vec3(0.7), arrow(p, vec2(0.0), r*0.18, wi, 0.01, e) );
col = mix( col, vec3(0.7), line(p, -0.12*pe, 0.12*pe, wi, e) );
{
float an = cos(0.5*6.283185*time);
vec2 v = vec2( -cos(an), sin(an) )*0.12;
vec2 f;
if( q.x<0.333 ) f = flipIfNeg( v, r );
else if( q.x<0.666 ) f = reflIfNeg( v, r );
else f = clipIfNeg( v, r );
col = mix( col, col+0.2, arrow(p, vec2(0.0), v, wi, 5.0*wi, e) );
col = mix( col, vec3(1.0,0.7,0.2), arrow(p, vec2(0.0), f, wi, 5.0*wi, e) );
}
fragColor = vec4( col, 1.0 );
} | mit | [
2074,
2115,
2179,
2179,
2977
] | [
[
1272,
1348,
1388,
1388,
1443
],
[
1445,
1524,
1564,
1564,
1626
],
[
1628,
1704,
1744,
1744,
1834
],
[
1903,
1903,
1952,
1952,
2072
],
[
2074,
2115,
2179,
2179,
2977
],
[
3046,
3046,
3111,
3111,
3174
],
[
3176,
3176,
3253,
3253,
3320
],
[
3389,
3389,
3446,
3446,
4797
]
] | // https://www.shadertoy.com/view/slj3Dd
| float sdArrow( in vec2 p, vec2 a, vec2 b, float w1, float w2 )
{ |
const float k = 3.0;
vec2 ba = b - a;
float l2 = dot(ba,ba);
float l = sqrt(l2);
p = p-a;
p = mat2(ba.x,-ba.y,ba.y,ba.x)*p/l;
p.y = abs(p.y);
vec2 pz = p-vec2(l-w2*k,w2);
vec2 q = p;
q.x -= clamp( q.x, 0.0, l-w2*k );
q.y -= w1;
float di = dot(q,q);
q = pz;
q.y -= clamp( q.y, w1-w2, 0.0 );
di = min( di, dot(q,q) );
if( p.x<w1 )
{
q = p;
q.y -= clamp( q.y, 0.0, w1 );
di = min( di, dot(q,q) );
}
if( pz.x>0.0 )
{
q = pz;
q -= vec2(k,-1.0)*clamp( (q.x*k-q.y)/(k*k+1.0), 0.0, w2 );
di = min( di, dot(q,q) );
}
float si = 1.0;
float z = l - p.x;
if( min(p.x,z)>0.0 )
{
float h = (pz.x<0.0) ? w1 : z/k;
if( p.y<h ) si = -1.0;
}
return si*sqrt(di);
} | // https://www.shadertoy.com/view/slj3Dd
float sdArrow( in vec2 p, vec2 a, vec2 b, float w1, float w2 )
{ | 1 | 2 |
ld2SzK | otaviogood | 2014-10-27T05:24:30 | /*--------------------------------------------------------------------------------------
License CC0 - http://creativecommons.org/publicdomain/zero/1.0/
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
----------------------------------------------------------------------------------------
-Otavio Good
*/
// The noise function in this was inspired by IQ's "Terrain Tubes" shader. I never really figured out
// his function completely, so I'm not sure of the exact similarities. It's nice though because it
// works the same on all computers (I think). It's not based on a hash that changes from computer to
// computer. That means I can finally rely on the terrain being the same and make a camera path. :)
// It's also a much faster noise function, although it can look a bit repetitive.
#define MOTION_BLUR
#define MOVING_SUN
float Hash2d(vec2 uv)
{
float f = uv.x + uv.y * 47.0;
return fract(cos(f*3.333)*100003.9);
}
float Hash3d(vec3 uv)
{
float f = uv.x + uv.y * 37.0 + uv.z * 521.0;
return fract(cos(f*3.333)*100003.9);
}
float PI=3.14159265;
vec3 saturate(vec3 a) { return clamp(a, 0.0, 1.0); }
vec2 saturate(vec2 a) { return clamp(a, 0.0, 1.0); }
float saturate(float a) { return clamp(a, 0.0, 1.0); }
vec3 RotateX(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(v.x, cos * v.y + sin * v.z, -sin * v.y + cos * v.z);
//else return new float3(x, cos * y - sin * z, sin * y + cos * z);
}
vec3 RotateY(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(cos * v.x - sin * v.z, v.y, sin * v.x + cos * v.z);
//else return new float3(cos * x + sin * z, y, -sin * x + cos * z);
}
vec3 RotateZ(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(cos * v.x + sin * v.y, -sin * v.x + cos * v.y, v.z);
}
// This function basically is a procedural environment map that makes the sun
vec3 sunCol = vec3(258.0, 208.0, 100.0) / 4255.0;//unfortunately, i seem to have 2 different sun colors. :(
vec3 GetSunColorReflection(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.015 / dist;
sunIntensity = pow(sunIntensity, 0.3)*100.0;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.0425;
}
vec3 GetSunColorSmall(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.05 / dist;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.025;
}
// This is a spline used for the camera path
vec4 CatmullRom(vec4 p0, vec4 p1, vec4 p2, vec4 p3, float t)
{
float t2 = t*t;
float t3 = t*t*t;
return 0.5 *((2.0 * p1) +
(-p0 + p2) * t +
(2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 +
(-p0 + 3.0 * p1- 3.0 * p2 + p3) * t3);
}
// This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.739513; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
float SpiralNoiseC(vec3 p)
{
float n = 0.0; // noise amount
float iter = 1.0;
for (int i = 0; i < 8; i++)
{
// add sin and cos scaled inverse with the frequency
n += -abs(sin(p.y*iter) + cos(p.x*iter)) / iter; // abs for a ridged look
// rotate by adding perpendicular and scaling down
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
// rotate on other axis
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
// increase the frequency
iter *= 1.733733;
}
return n;
}
float SpiralNoiseD(vec3 p)
{
float n = 0.0;
float iter = 1.0;
for (int i = 0; i < 6; i++)
{
n += abs(sin(p.y*iter) + cos(p.x*iter)) / iter; // abs for a ridged look
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.733733;
}
return n;
}
float SpiralNoise3D(vec3 p)
{
float n = 0.0;
float iter = 1.0;
for (int i = 0; i < 5; i++)
{
n += (sin(p.y*iter) + cos(p.x*iter)) / iter;
//p.xy += vec2(p.y, -p.x) * nudge;
//p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.33733;
}
return n;
}
// These are the xyz camera positions and a left/right facing angle relative to the path line
// I think webgl glsl can only access arrays using a constant, so I'm writing all these out.
// Someone please tell me if I'm wrong.
vec4 c00 = vec4(3.5, 2.0, 13.1, 0.0); // start point
vec4 c01 = vec4(12.5, 2.2, 17.0, 0.0); // run up to canyon 2 before hole in large rock face
vec4 c02 = vec4(21.5, 4.0, 8.1, 0.0); // canyon 2 before hole in large rock face
vec4 c03 = vec4(21.0, 5.0, 1.1, -0.5); // before hole in large rock face
vec4 c04 = vec4(17.8, 5.4, -0.2, 0.0); // hole in large rock face
vec4 c05 = vec4(14.7, 2.5, 1.4, 0.0); // after hole in large rock face
vec4 c06 = vec4(7.9, 2.3, -2.1, 0.0);
vec4 c07 = vec4(0.5, -0.7, -3.5, 1.0);
vec4 c08 = vec4(-3.0, -1.0, -3.5, 1.3);
vec4 c09 = vec4(-3.5, -1.0, 4.0, 1.3);
vec4 c10 = vec4(3.0, -0.7, 3.3, 0.8);
vec4 c11 = vec4(3.5, -1.0, -4.75, 0.0);
vec4 c12 = vec4(-6.0, -0.2, 1.0, 3.14);
vec4 c13 = vec4(-6.0, -1.0, 5.5, 0.0);
vec4 cXX = vec4(0.0, 3.0, 0.0, 0.0);
float camPathOffset = 0.0; // where to start on the camera path - parametric t var for catmull-rom spline
vec3 camPos = vec3(0.0), camFacing;
vec3 camLookat=vec3(0,0.0,0);
float waterLevel = 1.5;
// from a time t, this finds where in the camera path you are.
// It uses Catmull-Rom splines
vec4 CamPos(float t)
{
t = mod(t, 14.0); // repeat after 14 time units
float bigTime = floor(t);
float smallTime = fract(t);
// Can't do arrays right, so write this all out.
if (bigTime == 0.0) return CatmullRom(c00, c01, c02, c03, smallTime);
if (bigTime == 1.0) return CatmullRom(c01, c02, c03, c04, smallTime);
if (bigTime == 2.0) return CatmullRom(c02, c03, c04, c05, smallTime);
if (bigTime == 3.0) return CatmullRom(c03, c04, c05, c06, smallTime);
if (bigTime == 4.0) return CatmullRom(c04, c05, c06, c07, smallTime);
if (bigTime == 5.0) return CatmullRom(c05, c06, c07, c08, smallTime);
if (bigTime == 6.0) return CatmullRom(c06, c07, c08, c09, smallTime);
if (bigTime == 7.0) return CatmullRom(c07, c08, c09, c10, smallTime);
if (bigTime == 8.0) return CatmullRom(c08, c09, c10, c11, smallTime);
if (bigTime == 9.0) return CatmullRom(c09, c10, c11, c12, smallTime);
if (bigTime == 10.0) return CatmullRom(c10, c11, c12, c13, smallTime);
if (bigTime == 11.0) return CatmullRom(c11, c12, c13, c00, smallTime);
if (bigTime == 12.0) return CatmullRom(c12, c13, c00, c01, smallTime);
if (bigTime == 13.0) return CatmullRom(c13, c00, c01, c02, smallTime);
return vec4(0.0);
}
float DistanceToObject(vec3 p)
{
float final = p.y + 4.5;
final -= SpiralNoiseC(p.xyz); // mid-range noise
final += SpiralNoiseC(p.zxy*0.123+100.0)*3.0; // large scale terrain features
final -= SpiralNoise3D(p); // more large scale features, but 3d, so not just a height map.
final -= SpiralNoise3D(p*49.0)*0.0625*0.125; // small scale noise for variation
final = min(final, length(p) - 1.99); // sphere in center
final = min(final, p.y + waterLevel); // water
//final = min(final, length(p-camLookat) - 0.3);
return final;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// ---------------- First, set up the camera rays for ray marching ----------------
vec2 uv = fragCoord.xy/iResolution.xy * 2.0 - 1.0;
// Camera up vector.
vec3 camUp=vec3(0,1,0); // vuv
// Camera lookat.
camLookat=vec3(0,0.0,0); // vrp
/* if (iTime == 0.0) // for debugging with manual camera
{
camPos = cXX.xyz;
camLookat = vec3(0.0)*cXX.xyz;
}*/
// debugging camera
float mx=iMouse.x/iResolution.x*PI*2.0;// + iTime * 0.1;
float my=-iMouse.y/iResolution.y*10.0;// + sin(iTime * 0.3)*0.2+0.2;//*PI/2.01;
camPos += vec3(cos(my)*cos(mx),sin(my),cos(my)*sin(mx))*(5.2); // prp
// set time for moving camera along path
float timeLine = iTime*0.2 + camPathOffset;
camFacing = camLookat + camPos;
// without this if condition, the mac doesn't work. mysterious. :(
if (iTime != -1.0)
{
vec4 catmullA = CamPos(timeLine);
// get a smoother derivative even though the spline is not C2 continuous.
// Also look ahead a bit so the camera leads the motion
vec4 catmullB = CamPos(timeLine + 0.3);
#ifdef MOTION_BLUR
vec4 catmullC = CamPos(timeLine + 0.004); // adjust for camera motion blur
vec4 catmullBlur = mix(catmullA, catmullC, Hash2d(uv)); // motion blur along camera path
camPos = catmullBlur.xyz;
// face camera along derivate of motion path
camFacing = normalize(catmullB.xyz - catmullA.xyz);
// rotate camera based on w component of camera path vectors
camFacing = RotateY(camFacing, -catmullBlur.w);
#else
camPos = catmullA.xyz;
// face camera along derivate of motion path
camFacing = normalize(catmullB.xyz - catmullA.xyz);
// rotate camera based on w component of camera path vectors
camFacing = RotateY(camFacing, -catmullA.w);
#endif
camFacing = RotateY(camFacing, -mx);
camLookat = camPos + camFacing;
}
// add randomness to camera for depth-of-field look close up.
//camPos += vec3(Hash2d(uv)*0.91, Hash2d(uv+37.0), Hash2d(uv+47.0))*0.01;
// Camera setup.
vec3 camVec=normalize(camLookat - camPos);//vpn
vec3 sideNorm=normalize(cross(camUp, camVec)); // u
vec3 upNorm=cross(camVec, sideNorm);//v
vec3 worldFacing=(camPos + camVec);//vcv
vec3 worldPix = worldFacing + uv.x * sideNorm * (iResolution.x/iResolution.y) + uv.y * upNorm;//scrCoord
vec3 relVec = normalize(worldPix - camPos);//scp
// --------------------------------------------------------------------------------
float dist = 0.05;
float t = 0.0;
float inc = 0.02;
float maxDepth = 110.0;
vec3 pos = vec3(0,0,0);
// ray marching time
for (int i = 0; i < 200; i++) // This is the count of the max times the ray actually marches.
{
if ((t > maxDepth) || (abs(dist) < 0.0075)) break;
pos = camPos + relVec * t;
// *******************************************************
// This is _the_ function that defines the "distance field".
// It's really what makes the scene geometry.
// *******************************************************
dist = DistanceToObject(pos);
t += dist * 0.25; // because deformations mess up distance function.
}
// --------------------------------------------------------------------------------
// Now that we have done our ray marching, let's put some color on this geometry.
#ifdef MOVING_SUN
vec3 sunDir = normalize(vec3(sin(iTime*0.047-1.5), cos(iTime*0.047-1.5), -0.5));
#else
vec3 sunDir = normalize(vec3(0.93, 1.0, -1.5));
#endif
// This makes the sky fade at sunset
float skyMultiplier = saturate(sunDir.y+0.7);
vec3 finalColor = vec3(0.0);
// If a ray actually hit the object, let's light it.
if (abs(dist) < 0.75)
//if (t <= maxDepth)
{
// calculate the normal from the distance field. The distance field is a volume, so if you
// sample the current point and neighboring points, you can use the difference to get
// the normal.
vec3 smallVec = vec3(0.005, 0, 0);
vec3 normal = vec3(dist - DistanceToObject(pos - smallVec.xyy),
dist - DistanceToObject(pos - smallVec.yxy),
dist - DistanceToObject(pos - smallVec.yyx));
/*if (pos.y <= waterLevel-2.995) // water waves?
{
normal += SpiralNoise3D(pos*32.0+vec3(iTime*8.0,0.0,0.0))*0.0001;
normal += SpiralNoise3D(pos*27.0+vec3(0.0,0.0, iTime* 10.333))*0.0001;
normal += SpiralNoiseD(pos*37.0+vec3(0.0,iTime* 14.333,0.0))*0.0002;
}*/
normal = normalize(normal);
// calculate 2 ambient occlusion values. One for global stuff and one
// for local stuff - so the green sphere light source can also have ambient.
float ambientS = 1.0;
//ambient *= saturate(DistanceToObject(pos + normal * 0.1)*10.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.2)*5.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.4)*2.5);
ambientS *= saturate(DistanceToObject(pos + normal * 0.8)*1.25);
float ambient = ambientS * saturate(DistanceToObject(pos + normal * 1.6)*1.25*0.5);
ambient *= saturate(DistanceToObject(pos + normal * 3.2)*1.25*0.25);
ambient *= saturate(DistanceToObject(pos + normal * 6.4)*1.25*0.125);
//ambient = max(0.05, pow(ambient, 0.3)); // tone down ambient with a pow and min clamp it.
ambient = saturate(ambient);
// Trace a ray toward the sun for sun shadows
float sunShadow = 1.0;
float iter = 0.2;
for (int i = 0; i < 10; i++)
{
float tempDist = DistanceToObject(pos + sunDir * iter);
sunShadow *= saturate(tempDist*10.0);
if (tempDist <= 0.0) break;
iter *= 1.5; // constant is more reliable than distance-based
//iter += max(0.2, tempDist)*1.2;
}
float sunSet = saturate(sunDir.y*4.0); // sunset dims the sun
sunShadow = saturate(sunShadow) * sunSet;
// calculate the reflection vector for highlights
vec3 ref = reflect(relVec, normal);
// pulse the ball light source
vec3 ballGlow = vec3(0.1, 0.97, 0.1) * abs(SpiralNoise3D(vec3(iTime*1.3)));
// ------ Calculate texture color of the rock ------
// basic orange and white blended together with noise
vec3 texColor = mix(vec3(0.95, 1.0, 1.0), vec3(0.9, 0.7, 0.5), pow(abs(SpiralNoise3D(pos*1.0)-1.0), 0.6) );
// make the undersides darker greenish
texColor = mix(vec3(0.2, 0.2, 0.1), texColor, saturate(normal.y));
// fade to reddish/orange closer to the water level
texColor = mix(texColor, vec3(0.64, 0.2, 0.1) , saturate(-0.4-pos.y));
// some more variation to the color vertically
texColor = mix(texColor, vec3(0.2, 0.13, 0.02) , pow(saturate(pos.y*0.125+0.5), 2.0));
// give the rock a stratified, layered look
float rockLayers = abs(cos(pos.y*1.5+ SpiralNoiseD(pos*vec3(1.0, 2.0, 1.0)*4.0)*0.2 ));
texColor += vec3(0.7, 0.4, 0.3)*(1.0-pow(rockLayers, 0.3));
// make the water orange. I'm trying for that "nickel tailings" look.
texColor = mix(texColor, vec3(1.4, 0.15, 0.05) + SpiralNoise3D(pos)*0.025, saturate((-pos.y-1.45)*17.0));
// make the sphere white
if (length(pos) <= 2.01) texColor = vec3(1.0);
// don't let it get too saturated or dark
texColor = max(texColor, 0.05);
// ------ Calculate lighting color ------
// Start with sun color, standard lighting equation, and shadow
vec3 lightColor = vec3(1.0, 0.75, 0.75) * saturate(dot(sunDir, normal)) * sunShadow*1.5;
// sky color, hemisphere light equation approximation, anbient occlusion, sunset multiplier
lightColor += vec3(1.0,0.3,0.6) * ( dot(sunDir, normal) * 0.5 + 0.5 ) * ambient * 0.25 * skyMultiplier;
// Make the ball cast light. Distance to the 4th light falloff looked best. Use local ambient occlusion.
float lp = length(pos) - 1.0;
lightColor += ambientS*(ballGlow*1.2 * saturate(dot(normal, -pos)*0.5+0.5) / (lp*lp*lp*lp));
// finally, apply the light to the texture.
finalColor = texColor * lightColor;
// Make the water reflect the sun (leaving out sky reflection for no good reason)
vec3 refColor = GetSunColorReflection(ref, sunDir)*0.68;
finalColor += refColor * sunShadow * saturate(normal.y*normal.y) * saturate(-(pos.y+1.35)*16.0);
// make the ball itself glow
finalColor += pow(saturate(1.0 - length(pos)*0.4925), 0.65) * ballGlow*6.1;
// fog that fades to reddish plus the sun color so that fog is brightest towards sun
finalColor = mix(vec3(1.0, 0.41, 0.41)*skyMultiplier + min(vec3(0.25),GetSunColorSmall(relVec, sunDir))*2.0*sunSet, finalColor, exp(-t*0.03));
}
else
{
// Our ray trace hit nothing, so draw sky.
// fade the sky color, multiply sunset dimming
finalColor = mix(vec3(1.0, 0.5, 0.5), vec3(0.40, 0.25, 0.91), saturate(relVec.y))*skyMultiplier;
// add the sun
finalColor += GetSunColorSmall(relVec, sunDir);// + vec3(0.1, 0.1, 0.1);
}
//finalColor = vec3(Hash2d(uv)*0.91, Hash2d(uv+47.0)*0.91, 0.0);
// vignette?
finalColor *= vec3(1.0) * saturate(1.0 - length(uv/2.5));
finalColor *= 1.3;
// output the final color with sqrt for "gamma correction"
fragColor = vec4(sqrt(clamp(finalColor, 0.0, 1.0)),1.0);
}
| cc0-1.0 | [
2076,
2262,
2316,
2316,
2619
] | [
[
997,
997,
1020,
1020,
1097
],
[
1098,
1098,
1121,
1121,
1213
],
[
1237,
1237,
1260,
1260,
1289
],
[
1290,
1290,
1313,
1313,
1342
],
[
1343,
1343,
1368,
1368,
1397
],
[
1399,
1399,
1432,
1432,
1646
],
[
1647,
1647,
1680,
1680,
1894
],
[
1895,
1895,
1928,
1928,
2073
],
[
2076,
2262,
2316,
2316,
2619
],
[
2620,
2620,
2669,
2669,
2923
],
[
2925,
2970,
3032,
3032,
3240
],
[
3242,
3713,
3741,
3741,
4290
],
[
4291,
4291,
4319,
4319,
4665
],
[
4666,
4666,
4695,
4695,
5016
],
[
6229,
6323,
6345,
6345,
7577
],
[
7579,
7579,
7611,
7611,
8131
],
[
8133,
8133,
8190,
8275,
17740
]
] | // This function basically is a procedural environment map that makes the sun
vec3 sunCol = vec3(258.0, 208.0, 100.0) / 4255.0;//unfortunately, i seem to have 2 different sun colors. :(
| vec3 GetSunColorReflection(vec3 rayDir, vec3 sunDir)
{ |
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.015 / dist;
sunIntensity = pow(sunIntensity, 0.3)*100.0;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.0425;
} | // This function basically is a procedural environment map that makes the sun
vec3 sunCol = vec3(258.0, 208.0, 100.0) / 4255.0;//unfortunately, i seem to have 2 different sun colors. :(
vec3 GetSunColorReflection(vec3 rayDir, vec3 sunDir)
{ | 2 | 8 |
ld2SzK | otaviogood | 2014-10-27T05:24:30 | /*--------------------------------------------------------------------------------------
License CC0 - http://creativecommons.org/publicdomain/zero/1.0/
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
----------------------------------------------------------------------------------------
-Otavio Good
*/
// The noise function in this was inspired by IQ's "Terrain Tubes" shader. I never really figured out
// his function completely, so I'm not sure of the exact similarities. It's nice though because it
// works the same on all computers (I think). It's not based on a hash that changes from computer to
// computer. That means I can finally rely on the terrain being the same and make a camera path. :)
// It's also a much faster noise function, although it can look a bit repetitive.
#define MOTION_BLUR
#define MOVING_SUN
float Hash2d(vec2 uv)
{
float f = uv.x + uv.y * 47.0;
return fract(cos(f*3.333)*100003.9);
}
float Hash3d(vec3 uv)
{
float f = uv.x + uv.y * 37.0 + uv.z * 521.0;
return fract(cos(f*3.333)*100003.9);
}
float PI=3.14159265;
vec3 saturate(vec3 a) { return clamp(a, 0.0, 1.0); }
vec2 saturate(vec2 a) { return clamp(a, 0.0, 1.0); }
float saturate(float a) { return clamp(a, 0.0, 1.0); }
vec3 RotateX(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(v.x, cos * v.y + sin * v.z, -sin * v.y + cos * v.z);
//else return new float3(x, cos * y - sin * z, sin * y + cos * z);
}
vec3 RotateY(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(cos * v.x - sin * v.z, v.y, sin * v.x + cos * v.z);
//else return new float3(cos * x + sin * z, y, -sin * x + cos * z);
}
vec3 RotateZ(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(cos * v.x + sin * v.y, -sin * v.x + cos * v.y, v.z);
}
// This function basically is a procedural environment map that makes the sun
vec3 sunCol = vec3(258.0, 208.0, 100.0) / 4255.0;//unfortunately, i seem to have 2 different sun colors. :(
vec3 GetSunColorReflection(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.015 / dist;
sunIntensity = pow(sunIntensity, 0.3)*100.0;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.0425;
}
vec3 GetSunColorSmall(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.05 / dist;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.025;
}
// This is a spline used for the camera path
vec4 CatmullRom(vec4 p0, vec4 p1, vec4 p2, vec4 p3, float t)
{
float t2 = t*t;
float t3 = t*t*t;
return 0.5 *((2.0 * p1) +
(-p0 + p2) * t +
(2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 +
(-p0 + 3.0 * p1- 3.0 * p2 + p3) * t3);
}
// This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.739513; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
float SpiralNoiseC(vec3 p)
{
float n = 0.0; // noise amount
float iter = 1.0;
for (int i = 0; i < 8; i++)
{
// add sin and cos scaled inverse with the frequency
n += -abs(sin(p.y*iter) + cos(p.x*iter)) / iter; // abs for a ridged look
// rotate by adding perpendicular and scaling down
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
// rotate on other axis
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
// increase the frequency
iter *= 1.733733;
}
return n;
}
float SpiralNoiseD(vec3 p)
{
float n = 0.0;
float iter = 1.0;
for (int i = 0; i < 6; i++)
{
n += abs(sin(p.y*iter) + cos(p.x*iter)) / iter; // abs for a ridged look
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.733733;
}
return n;
}
float SpiralNoise3D(vec3 p)
{
float n = 0.0;
float iter = 1.0;
for (int i = 0; i < 5; i++)
{
n += (sin(p.y*iter) + cos(p.x*iter)) / iter;
//p.xy += vec2(p.y, -p.x) * nudge;
//p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.33733;
}
return n;
}
// These are the xyz camera positions and a left/right facing angle relative to the path line
// I think webgl glsl can only access arrays using a constant, so I'm writing all these out.
// Someone please tell me if I'm wrong.
vec4 c00 = vec4(3.5, 2.0, 13.1, 0.0); // start point
vec4 c01 = vec4(12.5, 2.2, 17.0, 0.0); // run up to canyon 2 before hole in large rock face
vec4 c02 = vec4(21.5, 4.0, 8.1, 0.0); // canyon 2 before hole in large rock face
vec4 c03 = vec4(21.0, 5.0, 1.1, -0.5); // before hole in large rock face
vec4 c04 = vec4(17.8, 5.4, -0.2, 0.0); // hole in large rock face
vec4 c05 = vec4(14.7, 2.5, 1.4, 0.0); // after hole in large rock face
vec4 c06 = vec4(7.9, 2.3, -2.1, 0.0);
vec4 c07 = vec4(0.5, -0.7, -3.5, 1.0);
vec4 c08 = vec4(-3.0, -1.0, -3.5, 1.3);
vec4 c09 = vec4(-3.5, -1.0, 4.0, 1.3);
vec4 c10 = vec4(3.0, -0.7, 3.3, 0.8);
vec4 c11 = vec4(3.5, -1.0, -4.75, 0.0);
vec4 c12 = vec4(-6.0, -0.2, 1.0, 3.14);
vec4 c13 = vec4(-6.0, -1.0, 5.5, 0.0);
vec4 cXX = vec4(0.0, 3.0, 0.0, 0.0);
float camPathOffset = 0.0; // where to start on the camera path - parametric t var for catmull-rom spline
vec3 camPos = vec3(0.0), camFacing;
vec3 camLookat=vec3(0,0.0,0);
float waterLevel = 1.5;
// from a time t, this finds where in the camera path you are.
// It uses Catmull-Rom splines
vec4 CamPos(float t)
{
t = mod(t, 14.0); // repeat after 14 time units
float bigTime = floor(t);
float smallTime = fract(t);
// Can't do arrays right, so write this all out.
if (bigTime == 0.0) return CatmullRom(c00, c01, c02, c03, smallTime);
if (bigTime == 1.0) return CatmullRom(c01, c02, c03, c04, smallTime);
if (bigTime == 2.0) return CatmullRom(c02, c03, c04, c05, smallTime);
if (bigTime == 3.0) return CatmullRom(c03, c04, c05, c06, smallTime);
if (bigTime == 4.0) return CatmullRom(c04, c05, c06, c07, smallTime);
if (bigTime == 5.0) return CatmullRom(c05, c06, c07, c08, smallTime);
if (bigTime == 6.0) return CatmullRom(c06, c07, c08, c09, smallTime);
if (bigTime == 7.0) return CatmullRom(c07, c08, c09, c10, smallTime);
if (bigTime == 8.0) return CatmullRom(c08, c09, c10, c11, smallTime);
if (bigTime == 9.0) return CatmullRom(c09, c10, c11, c12, smallTime);
if (bigTime == 10.0) return CatmullRom(c10, c11, c12, c13, smallTime);
if (bigTime == 11.0) return CatmullRom(c11, c12, c13, c00, smallTime);
if (bigTime == 12.0) return CatmullRom(c12, c13, c00, c01, smallTime);
if (bigTime == 13.0) return CatmullRom(c13, c00, c01, c02, smallTime);
return vec4(0.0);
}
float DistanceToObject(vec3 p)
{
float final = p.y + 4.5;
final -= SpiralNoiseC(p.xyz); // mid-range noise
final += SpiralNoiseC(p.zxy*0.123+100.0)*3.0; // large scale terrain features
final -= SpiralNoise3D(p); // more large scale features, but 3d, so not just a height map.
final -= SpiralNoise3D(p*49.0)*0.0625*0.125; // small scale noise for variation
final = min(final, length(p) - 1.99); // sphere in center
final = min(final, p.y + waterLevel); // water
//final = min(final, length(p-camLookat) - 0.3);
return final;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// ---------------- First, set up the camera rays for ray marching ----------------
vec2 uv = fragCoord.xy/iResolution.xy * 2.0 - 1.0;
// Camera up vector.
vec3 camUp=vec3(0,1,0); // vuv
// Camera lookat.
camLookat=vec3(0,0.0,0); // vrp
/* if (iTime == 0.0) // for debugging with manual camera
{
camPos = cXX.xyz;
camLookat = vec3(0.0)*cXX.xyz;
}*/
// debugging camera
float mx=iMouse.x/iResolution.x*PI*2.0;// + iTime * 0.1;
float my=-iMouse.y/iResolution.y*10.0;// + sin(iTime * 0.3)*0.2+0.2;//*PI/2.01;
camPos += vec3(cos(my)*cos(mx),sin(my),cos(my)*sin(mx))*(5.2); // prp
// set time for moving camera along path
float timeLine = iTime*0.2 + camPathOffset;
camFacing = camLookat + camPos;
// without this if condition, the mac doesn't work. mysterious. :(
if (iTime != -1.0)
{
vec4 catmullA = CamPos(timeLine);
// get a smoother derivative even though the spline is not C2 continuous.
// Also look ahead a bit so the camera leads the motion
vec4 catmullB = CamPos(timeLine + 0.3);
#ifdef MOTION_BLUR
vec4 catmullC = CamPos(timeLine + 0.004); // adjust for camera motion blur
vec4 catmullBlur = mix(catmullA, catmullC, Hash2d(uv)); // motion blur along camera path
camPos = catmullBlur.xyz;
// face camera along derivate of motion path
camFacing = normalize(catmullB.xyz - catmullA.xyz);
// rotate camera based on w component of camera path vectors
camFacing = RotateY(camFacing, -catmullBlur.w);
#else
camPos = catmullA.xyz;
// face camera along derivate of motion path
camFacing = normalize(catmullB.xyz - catmullA.xyz);
// rotate camera based on w component of camera path vectors
camFacing = RotateY(camFacing, -catmullA.w);
#endif
camFacing = RotateY(camFacing, -mx);
camLookat = camPos + camFacing;
}
// add randomness to camera for depth-of-field look close up.
//camPos += vec3(Hash2d(uv)*0.91, Hash2d(uv+37.0), Hash2d(uv+47.0))*0.01;
// Camera setup.
vec3 camVec=normalize(camLookat - camPos);//vpn
vec3 sideNorm=normalize(cross(camUp, camVec)); // u
vec3 upNorm=cross(camVec, sideNorm);//v
vec3 worldFacing=(camPos + camVec);//vcv
vec3 worldPix = worldFacing + uv.x * sideNorm * (iResolution.x/iResolution.y) + uv.y * upNorm;//scrCoord
vec3 relVec = normalize(worldPix - camPos);//scp
// --------------------------------------------------------------------------------
float dist = 0.05;
float t = 0.0;
float inc = 0.02;
float maxDepth = 110.0;
vec3 pos = vec3(0,0,0);
// ray marching time
for (int i = 0; i < 200; i++) // This is the count of the max times the ray actually marches.
{
if ((t > maxDepth) || (abs(dist) < 0.0075)) break;
pos = camPos + relVec * t;
// *******************************************************
// This is _the_ function that defines the "distance field".
// It's really what makes the scene geometry.
// *******************************************************
dist = DistanceToObject(pos);
t += dist * 0.25; // because deformations mess up distance function.
}
// --------------------------------------------------------------------------------
// Now that we have done our ray marching, let's put some color on this geometry.
#ifdef MOVING_SUN
vec3 sunDir = normalize(vec3(sin(iTime*0.047-1.5), cos(iTime*0.047-1.5), -0.5));
#else
vec3 sunDir = normalize(vec3(0.93, 1.0, -1.5));
#endif
// This makes the sky fade at sunset
float skyMultiplier = saturate(sunDir.y+0.7);
vec3 finalColor = vec3(0.0);
// If a ray actually hit the object, let's light it.
if (abs(dist) < 0.75)
//if (t <= maxDepth)
{
// calculate the normal from the distance field. The distance field is a volume, so if you
// sample the current point and neighboring points, you can use the difference to get
// the normal.
vec3 smallVec = vec3(0.005, 0, 0);
vec3 normal = vec3(dist - DistanceToObject(pos - smallVec.xyy),
dist - DistanceToObject(pos - smallVec.yxy),
dist - DistanceToObject(pos - smallVec.yyx));
/*if (pos.y <= waterLevel-2.995) // water waves?
{
normal += SpiralNoise3D(pos*32.0+vec3(iTime*8.0,0.0,0.0))*0.0001;
normal += SpiralNoise3D(pos*27.0+vec3(0.0,0.0, iTime* 10.333))*0.0001;
normal += SpiralNoiseD(pos*37.0+vec3(0.0,iTime* 14.333,0.0))*0.0002;
}*/
normal = normalize(normal);
// calculate 2 ambient occlusion values. One for global stuff and one
// for local stuff - so the green sphere light source can also have ambient.
float ambientS = 1.0;
//ambient *= saturate(DistanceToObject(pos + normal * 0.1)*10.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.2)*5.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.4)*2.5);
ambientS *= saturate(DistanceToObject(pos + normal * 0.8)*1.25);
float ambient = ambientS * saturate(DistanceToObject(pos + normal * 1.6)*1.25*0.5);
ambient *= saturate(DistanceToObject(pos + normal * 3.2)*1.25*0.25);
ambient *= saturate(DistanceToObject(pos + normal * 6.4)*1.25*0.125);
//ambient = max(0.05, pow(ambient, 0.3)); // tone down ambient with a pow and min clamp it.
ambient = saturate(ambient);
// Trace a ray toward the sun for sun shadows
float sunShadow = 1.0;
float iter = 0.2;
for (int i = 0; i < 10; i++)
{
float tempDist = DistanceToObject(pos + sunDir * iter);
sunShadow *= saturate(tempDist*10.0);
if (tempDist <= 0.0) break;
iter *= 1.5; // constant is more reliable than distance-based
//iter += max(0.2, tempDist)*1.2;
}
float sunSet = saturate(sunDir.y*4.0); // sunset dims the sun
sunShadow = saturate(sunShadow) * sunSet;
// calculate the reflection vector for highlights
vec3 ref = reflect(relVec, normal);
// pulse the ball light source
vec3 ballGlow = vec3(0.1, 0.97, 0.1) * abs(SpiralNoise3D(vec3(iTime*1.3)));
// ------ Calculate texture color of the rock ------
// basic orange and white blended together with noise
vec3 texColor = mix(vec3(0.95, 1.0, 1.0), vec3(0.9, 0.7, 0.5), pow(abs(SpiralNoise3D(pos*1.0)-1.0), 0.6) );
// make the undersides darker greenish
texColor = mix(vec3(0.2, 0.2, 0.1), texColor, saturate(normal.y));
// fade to reddish/orange closer to the water level
texColor = mix(texColor, vec3(0.64, 0.2, 0.1) , saturate(-0.4-pos.y));
// some more variation to the color vertically
texColor = mix(texColor, vec3(0.2, 0.13, 0.02) , pow(saturate(pos.y*0.125+0.5), 2.0));
// give the rock a stratified, layered look
float rockLayers = abs(cos(pos.y*1.5+ SpiralNoiseD(pos*vec3(1.0, 2.0, 1.0)*4.0)*0.2 ));
texColor += vec3(0.7, 0.4, 0.3)*(1.0-pow(rockLayers, 0.3));
// make the water orange. I'm trying for that "nickel tailings" look.
texColor = mix(texColor, vec3(1.4, 0.15, 0.05) + SpiralNoise3D(pos)*0.025, saturate((-pos.y-1.45)*17.0));
// make the sphere white
if (length(pos) <= 2.01) texColor = vec3(1.0);
// don't let it get too saturated or dark
texColor = max(texColor, 0.05);
// ------ Calculate lighting color ------
// Start with sun color, standard lighting equation, and shadow
vec3 lightColor = vec3(1.0, 0.75, 0.75) * saturate(dot(sunDir, normal)) * sunShadow*1.5;
// sky color, hemisphere light equation approximation, anbient occlusion, sunset multiplier
lightColor += vec3(1.0,0.3,0.6) * ( dot(sunDir, normal) * 0.5 + 0.5 ) * ambient * 0.25 * skyMultiplier;
// Make the ball cast light. Distance to the 4th light falloff looked best. Use local ambient occlusion.
float lp = length(pos) - 1.0;
lightColor += ambientS*(ballGlow*1.2 * saturate(dot(normal, -pos)*0.5+0.5) / (lp*lp*lp*lp));
// finally, apply the light to the texture.
finalColor = texColor * lightColor;
// Make the water reflect the sun (leaving out sky reflection for no good reason)
vec3 refColor = GetSunColorReflection(ref, sunDir)*0.68;
finalColor += refColor * sunShadow * saturate(normal.y*normal.y) * saturate(-(pos.y+1.35)*16.0);
// make the ball itself glow
finalColor += pow(saturate(1.0 - length(pos)*0.4925), 0.65) * ballGlow*6.1;
// fog that fades to reddish plus the sun color so that fog is brightest towards sun
finalColor = mix(vec3(1.0, 0.41, 0.41)*skyMultiplier + min(vec3(0.25),GetSunColorSmall(relVec, sunDir))*2.0*sunSet, finalColor, exp(-t*0.03));
}
else
{
// Our ray trace hit nothing, so draw sky.
// fade the sky color, multiply sunset dimming
finalColor = mix(vec3(1.0, 0.5, 0.5), vec3(0.40, 0.25, 0.91), saturate(relVec.y))*skyMultiplier;
// add the sun
finalColor += GetSunColorSmall(relVec, sunDir);// + vec3(0.1, 0.1, 0.1);
}
//finalColor = vec3(Hash2d(uv)*0.91, Hash2d(uv+47.0)*0.91, 0.0);
// vignette?
finalColor *= vec3(1.0) * saturate(1.0 - length(uv/2.5));
finalColor *= 1.3;
// output the final color with sqrt for "gamma correction"
fragColor = vec4(sqrt(clamp(finalColor, 0.0, 1.0)),1.0);
}
| cc0-1.0 | [
2925,
2970,
3032,
3032,
3240
] | [
[
997,
997,
1020,
1020,
1097
],
[
1098,
1098,
1121,
1121,
1213
],
[
1237,
1237,
1260,
1260,
1289
],
[
1290,
1290,
1313,
1313,
1342
],
[
1343,
1343,
1368,
1368,
1397
],
[
1399,
1399,
1432,
1432,
1646
],
[
1647,
1647,
1680,
1680,
1894
],
[
1895,
1895,
1928,
1928,
2073
],
[
2076,
2262,
2316,
2316,
2619
],
[
2620,
2620,
2669,
2669,
2923
],
[
2925,
2970,
3032,
3032,
3240
],
[
3242,
3713,
3741,
3741,
4290
],
[
4291,
4291,
4319,
4319,
4665
],
[
4666,
4666,
4695,
4695,
5016
],
[
6229,
6323,
6345,
6345,
7577
],
[
7579,
7579,
7611,
7611,
8131
],
[
8133,
8133,
8190,
8275,
17740
]
] | // This is a spline used for the camera path
| vec4 CatmullRom(vec4 p0, vec4 p1, vec4 p2, vec4 p3, float t)
{ |
float t2 = t*t;
float t3 = t*t*t;
return 0.5 *((2.0 * p1) +
(-p0 + p2) * t +
(2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 +
(-p0 + 3.0 * p1- 3.0 * p2 + p3) * t3);
} | // This is a spline used for the camera path
vec4 CatmullRom(vec4 p0, vec4 p1, vec4 p2, vec4 p3, float t)
{ | 1 | 1 |
ld2SzK | otaviogood | 2014-10-27T05:24:30 | /*--------------------------------------------------------------------------------------
License CC0 - http://creativecommons.org/publicdomain/zero/1.0/
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
----------------------------------------------------------------------------------------
-Otavio Good
*/
// The noise function in this was inspired by IQ's "Terrain Tubes" shader. I never really figured out
// his function completely, so I'm not sure of the exact similarities. It's nice though because it
// works the same on all computers (I think). It's not based on a hash that changes from computer to
// computer. That means I can finally rely on the terrain being the same and make a camera path. :)
// It's also a much faster noise function, although it can look a bit repetitive.
#define MOTION_BLUR
#define MOVING_SUN
float Hash2d(vec2 uv)
{
float f = uv.x + uv.y * 47.0;
return fract(cos(f*3.333)*100003.9);
}
float Hash3d(vec3 uv)
{
float f = uv.x + uv.y * 37.0 + uv.z * 521.0;
return fract(cos(f*3.333)*100003.9);
}
float PI=3.14159265;
vec3 saturate(vec3 a) { return clamp(a, 0.0, 1.0); }
vec2 saturate(vec2 a) { return clamp(a, 0.0, 1.0); }
float saturate(float a) { return clamp(a, 0.0, 1.0); }
vec3 RotateX(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(v.x, cos * v.y + sin * v.z, -sin * v.y + cos * v.z);
//else return new float3(x, cos * y - sin * z, sin * y + cos * z);
}
vec3 RotateY(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(cos * v.x - sin * v.z, v.y, sin * v.x + cos * v.z);
//else return new float3(cos * x + sin * z, y, -sin * x + cos * z);
}
vec3 RotateZ(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(cos * v.x + sin * v.y, -sin * v.x + cos * v.y, v.z);
}
// This function basically is a procedural environment map that makes the sun
vec3 sunCol = vec3(258.0, 208.0, 100.0) / 4255.0;//unfortunately, i seem to have 2 different sun colors. :(
vec3 GetSunColorReflection(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.015 / dist;
sunIntensity = pow(sunIntensity, 0.3)*100.0;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.0425;
}
vec3 GetSunColorSmall(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.05 / dist;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.025;
}
// This is a spline used for the camera path
vec4 CatmullRom(vec4 p0, vec4 p1, vec4 p2, vec4 p3, float t)
{
float t2 = t*t;
float t3 = t*t*t;
return 0.5 *((2.0 * p1) +
(-p0 + p2) * t +
(2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 +
(-p0 + 3.0 * p1- 3.0 * p2 + p3) * t3);
}
// This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.739513; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
float SpiralNoiseC(vec3 p)
{
float n = 0.0; // noise amount
float iter = 1.0;
for (int i = 0; i < 8; i++)
{
// add sin and cos scaled inverse with the frequency
n += -abs(sin(p.y*iter) + cos(p.x*iter)) / iter; // abs for a ridged look
// rotate by adding perpendicular and scaling down
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
// rotate on other axis
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
// increase the frequency
iter *= 1.733733;
}
return n;
}
float SpiralNoiseD(vec3 p)
{
float n = 0.0;
float iter = 1.0;
for (int i = 0; i < 6; i++)
{
n += abs(sin(p.y*iter) + cos(p.x*iter)) / iter; // abs for a ridged look
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.733733;
}
return n;
}
float SpiralNoise3D(vec3 p)
{
float n = 0.0;
float iter = 1.0;
for (int i = 0; i < 5; i++)
{
n += (sin(p.y*iter) + cos(p.x*iter)) / iter;
//p.xy += vec2(p.y, -p.x) * nudge;
//p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.33733;
}
return n;
}
// These are the xyz camera positions and a left/right facing angle relative to the path line
// I think webgl glsl can only access arrays using a constant, so I'm writing all these out.
// Someone please tell me if I'm wrong.
vec4 c00 = vec4(3.5, 2.0, 13.1, 0.0); // start point
vec4 c01 = vec4(12.5, 2.2, 17.0, 0.0); // run up to canyon 2 before hole in large rock face
vec4 c02 = vec4(21.5, 4.0, 8.1, 0.0); // canyon 2 before hole in large rock face
vec4 c03 = vec4(21.0, 5.0, 1.1, -0.5); // before hole in large rock face
vec4 c04 = vec4(17.8, 5.4, -0.2, 0.0); // hole in large rock face
vec4 c05 = vec4(14.7, 2.5, 1.4, 0.0); // after hole in large rock face
vec4 c06 = vec4(7.9, 2.3, -2.1, 0.0);
vec4 c07 = vec4(0.5, -0.7, -3.5, 1.0);
vec4 c08 = vec4(-3.0, -1.0, -3.5, 1.3);
vec4 c09 = vec4(-3.5, -1.0, 4.0, 1.3);
vec4 c10 = vec4(3.0, -0.7, 3.3, 0.8);
vec4 c11 = vec4(3.5, -1.0, -4.75, 0.0);
vec4 c12 = vec4(-6.0, -0.2, 1.0, 3.14);
vec4 c13 = vec4(-6.0, -1.0, 5.5, 0.0);
vec4 cXX = vec4(0.0, 3.0, 0.0, 0.0);
float camPathOffset = 0.0; // where to start on the camera path - parametric t var for catmull-rom spline
vec3 camPos = vec3(0.0), camFacing;
vec3 camLookat=vec3(0,0.0,0);
float waterLevel = 1.5;
// from a time t, this finds where in the camera path you are.
// It uses Catmull-Rom splines
vec4 CamPos(float t)
{
t = mod(t, 14.0); // repeat after 14 time units
float bigTime = floor(t);
float smallTime = fract(t);
// Can't do arrays right, so write this all out.
if (bigTime == 0.0) return CatmullRom(c00, c01, c02, c03, smallTime);
if (bigTime == 1.0) return CatmullRom(c01, c02, c03, c04, smallTime);
if (bigTime == 2.0) return CatmullRom(c02, c03, c04, c05, smallTime);
if (bigTime == 3.0) return CatmullRom(c03, c04, c05, c06, smallTime);
if (bigTime == 4.0) return CatmullRom(c04, c05, c06, c07, smallTime);
if (bigTime == 5.0) return CatmullRom(c05, c06, c07, c08, smallTime);
if (bigTime == 6.0) return CatmullRom(c06, c07, c08, c09, smallTime);
if (bigTime == 7.0) return CatmullRom(c07, c08, c09, c10, smallTime);
if (bigTime == 8.0) return CatmullRom(c08, c09, c10, c11, smallTime);
if (bigTime == 9.0) return CatmullRom(c09, c10, c11, c12, smallTime);
if (bigTime == 10.0) return CatmullRom(c10, c11, c12, c13, smallTime);
if (bigTime == 11.0) return CatmullRom(c11, c12, c13, c00, smallTime);
if (bigTime == 12.0) return CatmullRom(c12, c13, c00, c01, smallTime);
if (bigTime == 13.0) return CatmullRom(c13, c00, c01, c02, smallTime);
return vec4(0.0);
}
float DistanceToObject(vec3 p)
{
float final = p.y + 4.5;
final -= SpiralNoiseC(p.xyz); // mid-range noise
final += SpiralNoiseC(p.zxy*0.123+100.0)*3.0; // large scale terrain features
final -= SpiralNoise3D(p); // more large scale features, but 3d, so not just a height map.
final -= SpiralNoise3D(p*49.0)*0.0625*0.125; // small scale noise for variation
final = min(final, length(p) - 1.99); // sphere in center
final = min(final, p.y + waterLevel); // water
//final = min(final, length(p-camLookat) - 0.3);
return final;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// ---------------- First, set up the camera rays for ray marching ----------------
vec2 uv = fragCoord.xy/iResolution.xy * 2.0 - 1.0;
// Camera up vector.
vec3 camUp=vec3(0,1,0); // vuv
// Camera lookat.
camLookat=vec3(0,0.0,0); // vrp
/* if (iTime == 0.0) // for debugging with manual camera
{
camPos = cXX.xyz;
camLookat = vec3(0.0)*cXX.xyz;
}*/
// debugging camera
float mx=iMouse.x/iResolution.x*PI*2.0;// + iTime * 0.1;
float my=-iMouse.y/iResolution.y*10.0;// + sin(iTime * 0.3)*0.2+0.2;//*PI/2.01;
camPos += vec3(cos(my)*cos(mx),sin(my),cos(my)*sin(mx))*(5.2); // prp
// set time for moving camera along path
float timeLine = iTime*0.2 + camPathOffset;
camFacing = camLookat + camPos;
// without this if condition, the mac doesn't work. mysterious. :(
if (iTime != -1.0)
{
vec4 catmullA = CamPos(timeLine);
// get a smoother derivative even though the spline is not C2 continuous.
// Also look ahead a bit so the camera leads the motion
vec4 catmullB = CamPos(timeLine + 0.3);
#ifdef MOTION_BLUR
vec4 catmullC = CamPos(timeLine + 0.004); // adjust for camera motion blur
vec4 catmullBlur = mix(catmullA, catmullC, Hash2d(uv)); // motion blur along camera path
camPos = catmullBlur.xyz;
// face camera along derivate of motion path
camFacing = normalize(catmullB.xyz - catmullA.xyz);
// rotate camera based on w component of camera path vectors
camFacing = RotateY(camFacing, -catmullBlur.w);
#else
camPos = catmullA.xyz;
// face camera along derivate of motion path
camFacing = normalize(catmullB.xyz - catmullA.xyz);
// rotate camera based on w component of camera path vectors
camFacing = RotateY(camFacing, -catmullA.w);
#endif
camFacing = RotateY(camFacing, -mx);
camLookat = camPos + camFacing;
}
// add randomness to camera for depth-of-field look close up.
//camPos += vec3(Hash2d(uv)*0.91, Hash2d(uv+37.0), Hash2d(uv+47.0))*0.01;
// Camera setup.
vec3 camVec=normalize(camLookat - camPos);//vpn
vec3 sideNorm=normalize(cross(camUp, camVec)); // u
vec3 upNorm=cross(camVec, sideNorm);//v
vec3 worldFacing=(camPos + camVec);//vcv
vec3 worldPix = worldFacing + uv.x * sideNorm * (iResolution.x/iResolution.y) + uv.y * upNorm;//scrCoord
vec3 relVec = normalize(worldPix - camPos);//scp
// --------------------------------------------------------------------------------
float dist = 0.05;
float t = 0.0;
float inc = 0.02;
float maxDepth = 110.0;
vec3 pos = vec3(0,0,0);
// ray marching time
for (int i = 0; i < 200; i++) // This is the count of the max times the ray actually marches.
{
if ((t > maxDepth) || (abs(dist) < 0.0075)) break;
pos = camPos + relVec * t;
// *******************************************************
// This is _the_ function that defines the "distance field".
// It's really what makes the scene geometry.
// *******************************************************
dist = DistanceToObject(pos);
t += dist * 0.25; // because deformations mess up distance function.
}
// --------------------------------------------------------------------------------
// Now that we have done our ray marching, let's put some color on this geometry.
#ifdef MOVING_SUN
vec3 sunDir = normalize(vec3(sin(iTime*0.047-1.5), cos(iTime*0.047-1.5), -0.5));
#else
vec3 sunDir = normalize(vec3(0.93, 1.0, -1.5));
#endif
// This makes the sky fade at sunset
float skyMultiplier = saturate(sunDir.y+0.7);
vec3 finalColor = vec3(0.0);
// If a ray actually hit the object, let's light it.
if (abs(dist) < 0.75)
//if (t <= maxDepth)
{
// calculate the normal from the distance field. The distance field is a volume, so if you
// sample the current point and neighboring points, you can use the difference to get
// the normal.
vec3 smallVec = vec3(0.005, 0, 0);
vec3 normal = vec3(dist - DistanceToObject(pos - smallVec.xyy),
dist - DistanceToObject(pos - smallVec.yxy),
dist - DistanceToObject(pos - smallVec.yyx));
/*if (pos.y <= waterLevel-2.995) // water waves?
{
normal += SpiralNoise3D(pos*32.0+vec3(iTime*8.0,0.0,0.0))*0.0001;
normal += SpiralNoise3D(pos*27.0+vec3(0.0,0.0, iTime* 10.333))*0.0001;
normal += SpiralNoiseD(pos*37.0+vec3(0.0,iTime* 14.333,0.0))*0.0002;
}*/
normal = normalize(normal);
// calculate 2 ambient occlusion values. One for global stuff and one
// for local stuff - so the green sphere light source can also have ambient.
float ambientS = 1.0;
//ambient *= saturate(DistanceToObject(pos + normal * 0.1)*10.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.2)*5.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.4)*2.5);
ambientS *= saturate(DistanceToObject(pos + normal * 0.8)*1.25);
float ambient = ambientS * saturate(DistanceToObject(pos + normal * 1.6)*1.25*0.5);
ambient *= saturate(DistanceToObject(pos + normal * 3.2)*1.25*0.25);
ambient *= saturate(DistanceToObject(pos + normal * 6.4)*1.25*0.125);
//ambient = max(0.05, pow(ambient, 0.3)); // tone down ambient with a pow and min clamp it.
ambient = saturate(ambient);
// Trace a ray toward the sun for sun shadows
float sunShadow = 1.0;
float iter = 0.2;
for (int i = 0; i < 10; i++)
{
float tempDist = DistanceToObject(pos + sunDir * iter);
sunShadow *= saturate(tempDist*10.0);
if (tempDist <= 0.0) break;
iter *= 1.5; // constant is more reliable than distance-based
//iter += max(0.2, tempDist)*1.2;
}
float sunSet = saturate(sunDir.y*4.0); // sunset dims the sun
sunShadow = saturate(sunShadow) * sunSet;
// calculate the reflection vector for highlights
vec3 ref = reflect(relVec, normal);
// pulse the ball light source
vec3 ballGlow = vec3(0.1, 0.97, 0.1) * abs(SpiralNoise3D(vec3(iTime*1.3)));
// ------ Calculate texture color of the rock ------
// basic orange and white blended together with noise
vec3 texColor = mix(vec3(0.95, 1.0, 1.0), vec3(0.9, 0.7, 0.5), pow(abs(SpiralNoise3D(pos*1.0)-1.0), 0.6) );
// make the undersides darker greenish
texColor = mix(vec3(0.2, 0.2, 0.1), texColor, saturate(normal.y));
// fade to reddish/orange closer to the water level
texColor = mix(texColor, vec3(0.64, 0.2, 0.1) , saturate(-0.4-pos.y));
// some more variation to the color vertically
texColor = mix(texColor, vec3(0.2, 0.13, 0.02) , pow(saturate(pos.y*0.125+0.5), 2.0));
// give the rock a stratified, layered look
float rockLayers = abs(cos(pos.y*1.5+ SpiralNoiseD(pos*vec3(1.0, 2.0, 1.0)*4.0)*0.2 ));
texColor += vec3(0.7, 0.4, 0.3)*(1.0-pow(rockLayers, 0.3));
// make the water orange. I'm trying for that "nickel tailings" look.
texColor = mix(texColor, vec3(1.4, 0.15, 0.05) + SpiralNoise3D(pos)*0.025, saturate((-pos.y-1.45)*17.0));
// make the sphere white
if (length(pos) <= 2.01) texColor = vec3(1.0);
// don't let it get too saturated or dark
texColor = max(texColor, 0.05);
// ------ Calculate lighting color ------
// Start with sun color, standard lighting equation, and shadow
vec3 lightColor = vec3(1.0, 0.75, 0.75) * saturate(dot(sunDir, normal)) * sunShadow*1.5;
// sky color, hemisphere light equation approximation, anbient occlusion, sunset multiplier
lightColor += vec3(1.0,0.3,0.6) * ( dot(sunDir, normal) * 0.5 + 0.5 ) * ambient * 0.25 * skyMultiplier;
// Make the ball cast light. Distance to the 4th light falloff looked best. Use local ambient occlusion.
float lp = length(pos) - 1.0;
lightColor += ambientS*(ballGlow*1.2 * saturate(dot(normal, -pos)*0.5+0.5) / (lp*lp*lp*lp));
// finally, apply the light to the texture.
finalColor = texColor * lightColor;
// Make the water reflect the sun (leaving out sky reflection for no good reason)
vec3 refColor = GetSunColorReflection(ref, sunDir)*0.68;
finalColor += refColor * sunShadow * saturate(normal.y*normal.y) * saturate(-(pos.y+1.35)*16.0);
// make the ball itself glow
finalColor += pow(saturate(1.0 - length(pos)*0.4925), 0.65) * ballGlow*6.1;
// fog that fades to reddish plus the sun color so that fog is brightest towards sun
finalColor = mix(vec3(1.0, 0.41, 0.41)*skyMultiplier + min(vec3(0.25),GetSunColorSmall(relVec, sunDir))*2.0*sunSet, finalColor, exp(-t*0.03));
}
else
{
// Our ray trace hit nothing, so draw sky.
// fade the sky color, multiply sunset dimming
finalColor = mix(vec3(1.0, 0.5, 0.5), vec3(0.40, 0.25, 0.91), saturate(relVec.y))*skyMultiplier;
// add the sun
finalColor += GetSunColorSmall(relVec, sunDir);// + vec3(0.1, 0.1, 0.1);
}
//finalColor = vec3(Hash2d(uv)*0.91, Hash2d(uv+47.0)*0.91, 0.0);
// vignette?
finalColor *= vec3(1.0) * saturate(1.0 - length(uv/2.5));
finalColor *= 1.3;
// output the final color with sqrt for "gamma correction"
fragColor = vec4(sqrt(clamp(finalColor, 0.0, 1.0)),1.0);
}
| cc0-1.0 | [
3242,
3713,
3741,
3741,
4290
] | [
[
997,
997,
1020,
1020,
1097
],
[
1098,
1098,
1121,
1121,
1213
],
[
1237,
1237,
1260,
1260,
1289
],
[
1290,
1290,
1313,
1313,
1342
],
[
1343,
1343,
1368,
1368,
1397
],
[
1399,
1399,
1432,
1432,
1646
],
[
1647,
1647,
1680,
1680,
1894
],
[
1895,
1895,
1928,
1928,
2073
],
[
2076,
2262,
2316,
2316,
2619
],
[
2620,
2620,
2669,
2669,
2923
],
[
2925,
2970,
3032,
3032,
3240
],
[
3242,
3713,
3741,
3741,
4290
],
[
4291,
4291,
4319,
4319,
4665
],
[
4666,
4666,
4695,
4695,
5016
],
[
6229,
6323,
6345,
6345,
7577
],
[
7579,
7579,
7611,
7611,
8131
],
[
8133,
8133,
8190,
8275,
17740
]
] | // This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.739513; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
| float SpiralNoiseC(vec3 p)
{ |
float n = 0.0; // noise amount
float iter = 1.0;
for (int i = 0; i < 8; i++)
{
// add sin and cos scaled inverse with the frequency
n += -abs(sin(p.y*iter) + cos(p.x*iter)) / iter; // abs for a ridged look
// rotate by adding perpendicular and scaling down
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
// rotate on other axis
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
// increase the frequency
iter *= 1.733733;
}
return n;
} | // This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.739513; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
float SpiralNoiseC(vec3 p)
{ | 7 | 17 |
ld2SzK | otaviogood | 2014-10-27T05:24:30 | /*--------------------------------------------------------------------------------------
License CC0 - http://creativecommons.org/publicdomain/zero/1.0/
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
----------------------------------------------------------------------------------------
-Otavio Good
*/
// The noise function in this was inspired by IQ's "Terrain Tubes" shader. I never really figured out
// his function completely, so I'm not sure of the exact similarities. It's nice though because it
// works the same on all computers (I think). It's not based on a hash that changes from computer to
// computer. That means I can finally rely on the terrain being the same and make a camera path. :)
// It's also a much faster noise function, although it can look a bit repetitive.
#define MOTION_BLUR
#define MOVING_SUN
float Hash2d(vec2 uv)
{
float f = uv.x + uv.y * 47.0;
return fract(cos(f*3.333)*100003.9);
}
float Hash3d(vec3 uv)
{
float f = uv.x + uv.y * 37.0 + uv.z * 521.0;
return fract(cos(f*3.333)*100003.9);
}
float PI=3.14159265;
vec3 saturate(vec3 a) { return clamp(a, 0.0, 1.0); }
vec2 saturate(vec2 a) { return clamp(a, 0.0, 1.0); }
float saturate(float a) { return clamp(a, 0.0, 1.0); }
vec3 RotateX(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(v.x, cos * v.y + sin * v.z, -sin * v.y + cos * v.z);
//else return new float3(x, cos * y - sin * z, sin * y + cos * z);
}
vec3 RotateY(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(cos * v.x - sin * v.z, v.y, sin * v.x + cos * v.z);
//else return new float3(cos * x + sin * z, y, -sin * x + cos * z);
}
vec3 RotateZ(vec3 v, float rad)
{
float cos = cos(rad);
float sin = sin(rad);
//if (RIGHT_HANDED_COORD)
return vec3(cos * v.x + sin * v.y, -sin * v.x + cos * v.y, v.z);
}
// This function basically is a procedural environment map that makes the sun
vec3 sunCol = vec3(258.0, 208.0, 100.0) / 4255.0;//unfortunately, i seem to have 2 different sun colors. :(
vec3 GetSunColorReflection(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.015 / dist;
sunIntensity = pow(sunIntensity, 0.3)*100.0;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.0425;
}
vec3 GetSunColorSmall(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.05 / dist;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.025;
}
// This is a spline used for the camera path
vec4 CatmullRom(vec4 p0, vec4 p1, vec4 p2, vec4 p3, float t)
{
float t2 = t*t;
float t3 = t*t*t;
return 0.5 *((2.0 * p1) +
(-p0 + p2) * t +
(2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 +
(-p0 + 3.0 * p1- 3.0 * p2 + p3) * t3);
}
// This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.739513; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
float SpiralNoiseC(vec3 p)
{
float n = 0.0; // noise amount
float iter = 1.0;
for (int i = 0; i < 8; i++)
{
// add sin and cos scaled inverse with the frequency
n += -abs(sin(p.y*iter) + cos(p.x*iter)) / iter; // abs for a ridged look
// rotate by adding perpendicular and scaling down
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
// rotate on other axis
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
// increase the frequency
iter *= 1.733733;
}
return n;
}
float SpiralNoiseD(vec3 p)
{
float n = 0.0;
float iter = 1.0;
for (int i = 0; i < 6; i++)
{
n += abs(sin(p.y*iter) + cos(p.x*iter)) / iter; // abs for a ridged look
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.733733;
}
return n;
}
float SpiralNoise3D(vec3 p)
{
float n = 0.0;
float iter = 1.0;
for (int i = 0; i < 5; i++)
{
n += (sin(p.y*iter) + cos(p.x*iter)) / iter;
//p.xy += vec2(p.y, -p.x) * nudge;
//p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.33733;
}
return n;
}
// These are the xyz camera positions and a left/right facing angle relative to the path line
// I think webgl glsl can only access arrays using a constant, so I'm writing all these out.
// Someone please tell me if I'm wrong.
vec4 c00 = vec4(3.5, 2.0, 13.1, 0.0); // start point
vec4 c01 = vec4(12.5, 2.2, 17.0, 0.0); // run up to canyon 2 before hole in large rock face
vec4 c02 = vec4(21.5, 4.0, 8.1, 0.0); // canyon 2 before hole in large rock face
vec4 c03 = vec4(21.0, 5.0, 1.1, -0.5); // before hole in large rock face
vec4 c04 = vec4(17.8, 5.4, -0.2, 0.0); // hole in large rock face
vec4 c05 = vec4(14.7, 2.5, 1.4, 0.0); // after hole in large rock face
vec4 c06 = vec4(7.9, 2.3, -2.1, 0.0);
vec4 c07 = vec4(0.5, -0.7, -3.5, 1.0);
vec4 c08 = vec4(-3.0, -1.0, -3.5, 1.3);
vec4 c09 = vec4(-3.5, -1.0, 4.0, 1.3);
vec4 c10 = vec4(3.0, -0.7, 3.3, 0.8);
vec4 c11 = vec4(3.5, -1.0, -4.75, 0.0);
vec4 c12 = vec4(-6.0, -0.2, 1.0, 3.14);
vec4 c13 = vec4(-6.0, -1.0, 5.5, 0.0);
vec4 cXX = vec4(0.0, 3.0, 0.0, 0.0);
float camPathOffset = 0.0; // where to start on the camera path - parametric t var for catmull-rom spline
vec3 camPos = vec3(0.0), camFacing;
vec3 camLookat=vec3(0,0.0,0);
float waterLevel = 1.5;
// from a time t, this finds where in the camera path you are.
// It uses Catmull-Rom splines
vec4 CamPos(float t)
{
t = mod(t, 14.0); // repeat after 14 time units
float bigTime = floor(t);
float smallTime = fract(t);
// Can't do arrays right, so write this all out.
if (bigTime == 0.0) return CatmullRom(c00, c01, c02, c03, smallTime);
if (bigTime == 1.0) return CatmullRom(c01, c02, c03, c04, smallTime);
if (bigTime == 2.0) return CatmullRom(c02, c03, c04, c05, smallTime);
if (bigTime == 3.0) return CatmullRom(c03, c04, c05, c06, smallTime);
if (bigTime == 4.0) return CatmullRom(c04, c05, c06, c07, smallTime);
if (bigTime == 5.0) return CatmullRom(c05, c06, c07, c08, smallTime);
if (bigTime == 6.0) return CatmullRom(c06, c07, c08, c09, smallTime);
if (bigTime == 7.0) return CatmullRom(c07, c08, c09, c10, smallTime);
if (bigTime == 8.0) return CatmullRom(c08, c09, c10, c11, smallTime);
if (bigTime == 9.0) return CatmullRom(c09, c10, c11, c12, smallTime);
if (bigTime == 10.0) return CatmullRom(c10, c11, c12, c13, smallTime);
if (bigTime == 11.0) return CatmullRom(c11, c12, c13, c00, smallTime);
if (bigTime == 12.0) return CatmullRom(c12, c13, c00, c01, smallTime);
if (bigTime == 13.0) return CatmullRom(c13, c00, c01, c02, smallTime);
return vec4(0.0);
}
float DistanceToObject(vec3 p)
{
float final = p.y + 4.5;
final -= SpiralNoiseC(p.xyz); // mid-range noise
final += SpiralNoiseC(p.zxy*0.123+100.0)*3.0; // large scale terrain features
final -= SpiralNoise3D(p); // more large scale features, but 3d, so not just a height map.
final -= SpiralNoise3D(p*49.0)*0.0625*0.125; // small scale noise for variation
final = min(final, length(p) - 1.99); // sphere in center
final = min(final, p.y + waterLevel); // water
//final = min(final, length(p-camLookat) - 0.3);
return final;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// ---------------- First, set up the camera rays for ray marching ----------------
vec2 uv = fragCoord.xy/iResolution.xy * 2.0 - 1.0;
// Camera up vector.
vec3 camUp=vec3(0,1,0); // vuv
// Camera lookat.
camLookat=vec3(0,0.0,0); // vrp
/* if (iTime == 0.0) // for debugging with manual camera
{
camPos = cXX.xyz;
camLookat = vec3(0.0)*cXX.xyz;
}*/
// debugging camera
float mx=iMouse.x/iResolution.x*PI*2.0;// + iTime * 0.1;
float my=-iMouse.y/iResolution.y*10.0;// + sin(iTime * 0.3)*0.2+0.2;//*PI/2.01;
camPos += vec3(cos(my)*cos(mx),sin(my),cos(my)*sin(mx))*(5.2); // prp
// set time for moving camera along path
float timeLine = iTime*0.2 + camPathOffset;
camFacing = camLookat + camPos;
// without this if condition, the mac doesn't work. mysterious. :(
if (iTime != -1.0)
{
vec4 catmullA = CamPos(timeLine);
// get a smoother derivative even though the spline is not C2 continuous.
// Also look ahead a bit so the camera leads the motion
vec4 catmullB = CamPos(timeLine + 0.3);
#ifdef MOTION_BLUR
vec4 catmullC = CamPos(timeLine + 0.004); // adjust for camera motion blur
vec4 catmullBlur = mix(catmullA, catmullC, Hash2d(uv)); // motion blur along camera path
camPos = catmullBlur.xyz;
// face camera along derivate of motion path
camFacing = normalize(catmullB.xyz - catmullA.xyz);
// rotate camera based on w component of camera path vectors
camFacing = RotateY(camFacing, -catmullBlur.w);
#else
camPos = catmullA.xyz;
// face camera along derivate of motion path
camFacing = normalize(catmullB.xyz - catmullA.xyz);
// rotate camera based on w component of camera path vectors
camFacing = RotateY(camFacing, -catmullA.w);
#endif
camFacing = RotateY(camFacing, -mx);
camLookat = camPos + camFacing;
}
// add randomness to camera for depth-of-field look close up.
//camPos += vec3(Hash2d(uv)*0.91, Hash2d(uv+37.0), Hash2d(uv+47.0))*0.01;
// Camera setup.
vec3 camVec=normalize(camLookat - camPos);//vpn
vec3 sideNorm=normalize(cross(camUp, camVec)); // u
vec3 upNorm=cross(camVec, sideNorm);//v
vec3 worldFacing=(camPos + camVec);//vcv
vec3 worldPix = worldFacing + uv.x * sideNorm * (iResolution.x/iResolution.y) + uv.y * upNorm;//scrCoord
vec3 relVec = normalize(worldPix - camPos);//scp
// --------------------------------------------------------------------------------
float dist = 0.05;
float t = 0.0;
float inc = 0.02;
float maxDepth = 110.0;
vec3 pos = vec3(0,0,0);
// ray marching time
for (int i = 0; i < 200; i++) // This is the count of the max times the ray actually marches.
{
if ((t > maxDepth) || (abs(dist) < 0.0075)) break;
pos = camPos + relVec * t;
// *******************************************************
// This is _the_ function that defines the "distance field".
// It's really what makes the scene geometry.
// *******************************************************
dist = DistanceToObject(pos);
t += dist * 0.25; // because deformations mess up distance function.
}
// --------------------------------------------------------------------------------
// Now that we have done our ray marching, let's put some color on this geometry.
#ifdef MOVING_SUN
vec3 sunDir = normalize(vec3(sin(iTime*0.047-1.5), cos(iTime*0.047-1.5), -0.5));
#else
vec3 sunDir = normalize(vec3(0.93, 1.0, -1.5));
#endif
// This makes the sky fade at sunset
float skyMultiplier = saturate(sunDir.y+0.7);
vec3 finalColor = vec3(0.0);
// If a ray actually hit the object, let's light it.
if (abs(dist) < 0.75)
//if (t <= maxDepth)
{
// calculate the normal from the distance field. The distance field is a volume, so if you
// sample the current point and neighboring points, you can use the difference to get
// the normal.
vec3 smallVec = vec3(0.005, 0, 0);
vec3 normal = vec3(dist - DistanceToObject(pos - smallVec.xyy),
dist - DistanceToObject(pos - smallVec.yxy),
dist - DistanceToObject(pos - smallVec.yyx));
/*if (pos.y <= waterLevel-2.995) // water waves?
{
normal += SpiralNoise3D(pos*32.0+vec3(iTime*8.0,0.0,0.0))*0.0001;
normal += SpiralNoise3D(pos*27.0+vec3(0.0,0.0, iTime* 10.333))*0.0001;
normal += SpiralNoiseD(pos*37.0+vec3(0.0,iTime* 14.333,0.0))*0.0002;
}*/
normal = normalize(normal);
// calculate 2 ambient occlusion values. One for global stuff and one
// for local stuff - so the green sphere light source can also have ambient.
float ambientS = 1.0;
//ambient *= saturate(DistanceToObject(pos + normal * 0.1)*10.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.2)*5.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.4)*2.5);
ambientS *= saturate(DistanceToObject(pos + normal * 0.8)*1.25);
float ambient = ambientS * saturate(DistanceToObject(pos + normal * 1.6)*1.25*0.5);
ambient *= saturate(DistanceToObject(pos + normal * 3.2)*1.25*0.25);
ambient *= saturate(DistanceToObject(pos + normal * 6.4)*1.25*0.125);
//ambient = max(0.05, pow(ambient, 0.3)); // tone down ambient with a pow and min clamp it.
ambient = saturate(ambient);
// Trace a ray toward the sun for sun shadows
float sunShadow = 1.0;
float iter = 0.2;
for (int i = 0; i < 10; i++)
{
float tempDist = DistanceToObject(pos + sunDir * iter);
sunShadow *= saturate(tempDist*10.0);
if (tempDist <= 0.0) break;
iter *= 1.5; // constant is more reliable than distance-based
//iter += max(0.2, tempDist)*1.2;
}
float sunSet = saturate(sunDir.y*4.0); // sunset dims the sun
sunShadow = saturate(sunShadow) * sunSet;
// calculate the reflection vector for highlights
vec3 ref = reflect(relVec, normal);
// pulse the ball light source
vec3 ballGlow = vec3(0.1, 0.97, 0.1) * abs(SpiralNoise3D(vec3(iTime*1.3)));
// ------ Calculate texture color of the rock ------
// basic orange and white blended together with noise
vec3 texColor = mix(vec3(0.95, 1.0, 1.0), vec3(0.9, 0.7, 0.5), pow(abs(SpiralNoise3D(pos*1.0)-1.0), 0.6) );
// make the undersides darker greenish
texColor = mix(vec3(0.2, 0.2, 0.1), texColor, saturate(normal.y));
// fade to reddish/orange closer to the water level
texColor = mix(texColor, vec3(0.64, 0.2, 0.1) , saturate(-0.4-pos.y));
// some more variation to the color vertically
texColor = mix(texColor, vec3(0.2, 0.13, 0.02) , pow(saturate(pos.y*0.125+0.5), 2.0));
// give the rock a stratified, layered look
float rockLayers = abs(cos(pos.y*1.5+ SpiralNoiseD(pos*vec3(1.0, 2.0, 1.0)*4.0)*0.2 ));
texColor += vec3(0.7, 0.4, 0.3)*(1.0-pow(rockLayers, 0.3));
// make the water orange. I'm trying for that "nickel tailings" look.
texColor = mix(texColor, vec3(1.4, 0.15, 0.05) + SpiralNoise3D(pos)*0.025, saturate((-pos.y-1.45)*17.0));
// make the sphere white
if (length(pos) <= 2.01) texColor = vec3(1.0);
// don't let it get too saturated or dark
texColor = max(texColor, 0.05);
// ------ Calculate lighting color ------
// Start with sun color, standard lighting equation, and shadow
vec3 lightColor = vec3(1.0, 0.75, 0.75) * saturate(dot(sunDir, normal)) * sunShadow*1.5;
// sky color, hemisphere light equation approximation, anbient occlusion, sunset multiplier
lightColor += vec3(1.0,0.3,0.6) * ( dot(sunDir, normal) * 0.5 + 0.5 ) * ambient * 0.25 * skyMultiplier;
// Make the ball cast light. Distance to the 4th light falloff looked best. Use local ambient occlusion.
float lp = length(pos) - 1.0;
lightColor += ambientS*(ballGlow*1.2 * saturate(dot(normal, -pos)*0.5+0.5) / (lp*lp*lp*lp));
// finally, apply the light to the texture.
finalColor = texColor * lightColor;
// Make the water reflect the sun (leaving out sky reflection for no good reason)
vec3 refColor = GetSunColorReflection(ref, sunDir)*0.68;
finalColor += refColor * sunShadow * saturate(normal.y*normal.y) * saturate(-(pos.y+1.35)*16.0);
// make the ball itself glow
finalColor += pow(saturate(1.0 - length(pos)*0.4925), 0.65) * ballGlow*6.1;
// fog that fades to reddish plus the sun color so that fog is brightest towards sun
finalColor = mix(vec3(1.0, 0.41, 0.41)*skyMultiplier + min(vec3(0.25),GetSunColorSmall(relVec, sunDir))*2.0*sunSet, finalColor, exp(-t*0.03));
}
else
{
// Our ray trace hit nothing, so draw sky.
// fade the sky color, multiply sunset dimming
finalColor = mix(vec3(1.0, 0.5, 0.5), vec3(0.40, 0.25, 0.91), saturate(relVec.y))*skyMultiplier;
// add the sun
finalColor += GetSunColorSmall(relVec, sunDir);// + vec3(0.1, 0.1, 0.1);
}
//finalColor = vec3(Hash2d(uv)*0.91, Hash2d(uv+47.0)*0.91, 0.0);
// vignette?
finalColor *= vec3(1.0) * saturate(1.0 - length(uv/2.5));
finalColor *= 1.3;
// output the final color with sqrt for "gamma correction"
fragColor = vec4(sqrt(clamp(finalColor, 0.0, 1.0)),1.0);
}
| cc0-1.0 | [
6229,
6323,
6345,
6345,
7577
] | [
[
997,
997,
1020,
1020,
1097
],
[
1098,
1098,
1121,
1121,
1213
],
[
1237,
1237,
1260,
1260,
1289
],
[
1290,
1290,
1313,
1313,
1342
],
[
1343,
1343,
1368,
1368,
1397
],
[
1399,
1399,
1432,
1432,
1646
],
[
1647,
1647,
1680,
1680,
1894
],
[
1895,
1895,
1928,
1928,
2073
],
[
2076,
2262,
2316,
2316,
2619
],
[
2620,
2620,
2669,
2669,
2923
],
[
2925,
2970,
3032,
3032,
3240
],
[
3242,
3713,
3741,
3741,
4290
],
[
4291,
4291,
4319,
4319,
4665
],
[
4666,
4666,
4695,
4695,
5016
],
[
6229,
6323,
6345,
6345,
7577
],
[
7579,
7579,
7611,
7611,
8131
],
[
8133,
8133,
8190,
8275,
17740
]
] | // from a time t, this finds where in the camera path you are.
// It uses Catmull-Rom splines
| vec4 CamPos(float t)
{ |
t = mod(t, 14.0); // repeat after 14 time units
float bigTime = floor(t);
float smallTime = fract(t);
// Can't do arrays right, so write this all out.
if (bigTime == 0.0) return CatmullRom(c00, c01, c02, c03, smallTime);
if (bigTime == 1.0) return CatmullRom(c01, c02, c03, c04, smallTime);
if (bigTime == 2.0) return CatmullRom(c02, c03, c04, c05, smallTime);
if (bigTime == 3.0) return CatmullRom(c03, c04, c05, c06, smallTime);
if (bigTime == 4.0) return CatmullRom(c04, c05, c06, c07, smallTime);
if (bigTime == 5.0) return CatmullRom(c05, c06, c07, c08, smallTime);
if (bigTime == 6.0) return CatmullRom(c06, c07, c08, c09, smallTime);
if (bigTime == 7.0) return CatmullRom(c07, c08, c09, c10, smallTime);
if (bigTime == 8.0) return CatmullRom(c08, c09, c10, c11, smallTime);
if (bigTime == 9.0) return CatmullRom(c09, c10, c11, c12, smallTime);
if (bigTime == 10.0) return CatmullRom(c10, c11, c12, c13, smallTime);
if (bigTime == 11.0) return CatmullRom(c11, c12, c13, c00, smallTime);
if (bigTime == 12.0) return CatmullRom(c12, c13, c00, c01, smallTime);
if (bigTime == 13.0) return CatmullRom(c13, c00, c01, c02, smallTime);
return vec4(0.0);
} | // from a time t, this finds where in the camera path you are.
// It uses Catmull-Rom splines
vec4 CamPos(float t)
{ | 1 | 1 |
4dSXDd | otaviogood | 2014-11-27T22:26:38 | /*--------------------------------------------------------------------------------------
License CC0 - http://creativecommons.org/publicdomain/zero/1.0/
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
----------------------------------------------------------------------------------------
^ This means do ANYTHING YOU WANT with this code. Because we are programmers, not lawyers.
-Otavio Good
*/
// This will lower the framerate, but looks kinda cool
//#define TOO_MUCH_FRACTAL
//#define MOVING_SUN
float outerSphereRad = 3.5;
// noise functions
float Hash1d(float u)
{
return fract(sin(u)*143.9); // scale this down to kill the jitters
}
float Hash2d(vec2 uv)
{
float f = uv.x + uv.y * 37.0;
return fract(sin(f)*104003.9);
}
float Hash3d(vec3 uv)
{
float f = uv.x + uv.y * 37.0 + uv.z * 521.0;
return fract(sin(f)*110003.9);
}
float mixP(float f0, float f1, float a)
{
return mix(f0, f1, a*a*(3.0-2.0*a));
}
const vec2 zeroOne = vec2(0.0, 1.0);
float noise2d(vec2 uv)
{
vec2 fr = fract(uv.xy);
vec2 fl = floor(uv.xy);
float h00 = Hash2d(fl);
float h10 = Hash2d(fl + zeroOne.yx);
float h01 = Hash2d(fl + zeroOne);
float h11 = Hash2d(fl + zeroOne.yy);
return mixP(mixP(h00, h10, fr.x), mixP(h01, h11, fr.x), fr.y);
}
float noise(vec3 uv)
{
vec3 fr = fract(uv.xyz);
vec3 fl = floor(uv.xyz);
float h000 = Hash3d(fl);
float h100 = Hash3d(fl + zeroOne.yxx);
float h010 = Hash3d(fl + zeroOne.xyx);
float h110 = Hash3d(fl + zeroOne.yyx);
float h001 = Hash3d(fl + zeroOne.xxy);
float h101 = Hash3d(fl + zeroOne.yxy);
float h011 = Hash3d(fl + zeroOne.xyy);
float h111 = Hash3d(fl + zeroOne.yyy);
return mixP(
mixP(mixP(h000, h100, fr.x),
mixP(h010, h110, fr.x), fr.y),
mixP(mixP(h001, h101, fr.x),
mixP(h011, h111, fr.x), fr.y)
, fr.z);
}
float PI=3.14159265;
// Variables for animating and rotating the sides of the object
float chunkAnim = 0.0;
mat3 rotMat;
vec3 rotDir;
float rotAmount;
vec3 saturate(vec3 a) { return clamp(a, 0.0, 1.0); }
vec2 saturate(vec2 a) { return clamp(a, 0.0, 1.0); }
float saturate(float a) { return clamp(a, 0.0, 1.0); }
// This function basically is a procedural environment map that makes the sun
vec3 sunCol = vec3(258.0, 208.0, 100.0) / 4255.0;
vec3 GetSunColorReflection(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.015 / dist;
sunIntensity = pow(sunIntensity, 0.3)*100.0;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.0425;
}
vec3 GetSunColorSmall(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.05 / dist;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.025;
}
// This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.71; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
// Total hack of the spiral noise function to get a rust look
float RustNoise3D(vec3 p)
{
float n = 0.0;
float iter = 1.0;
float pn = noise(p*0.125);
pn += noise(p*0.25)*0.5;
pn += noise(p*0.5)*0.25;
pn += noise(p*1.0)*0.125;
for (int i = 0; i < 7; i++)
{
//n += (sin(p.y*iter) + cos(p.x*iter)) / iter;
float wave = saturate(cos(p.y*0.25 + pn) - 0.998);
// wave *= noise(p * 0.125)*1016.0;
n += wave;
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.4733;
}
return n*500.0;
}
vec3 camPos = vec3(0.0), camFacing;
vec3 camLookat=vec3(0,0.0,0);
// This is the big money function that makes the crazy fractally shape
float DistanceToObject(vec3 p)
{
//p += (1.0/p.y)*0.6;
// Rotate, but only the part that is on the side of rotDir
if (dot(p, rotDir) > 1.0) p *= rotMat;
// Repeat our position so we can carve out many cylindrical-like things from our solid
vec3 rep = fract(p)-0.5;
//final = max(final, -(length(rep.xz*rep.xz)*1.0 - 0.0326));
float final = -(length(rep.xy*rep.xz) - 0.109);
final = max(final, -(length(rep.zy) - 0.33));
//final = max(final, -(length(rep.xz*rep.xz) - 0.03));
//final = max(final, -(length(rep.yz*rep.yz) - 0.03));
//final = max(final, -(length(rep.xy*rep.xy) - 0.030266));
// Repeat the process of carving things out for smaller scales
vec3 rep2 = fract(rep*2.0)-0.5;
final = max(final, -(length(rep2.xz)*0.5 - 0.125));
final = max(final, -(length(rep2.xy)*0.5 - 0.125));
final = max(final, -(length(rep2.zy)*0.5 - 0.125));
vec3 rep3 = fract(rep2*3.0)-0.5;
final = max(final, -(length(rep3.xz)*0.1667 - 0.25*0.1667));
final = max(final, -(length(rep3.xy)*0.1667 - 0.25*0.1667));
final = max(final, -(length(rep3.zy)*0.1667 - 0.25*0.1667));
#ifdef TOO_MUCH_FRACTAL
vec3 rep4 = fract(rep3*3.0)-0.5;
final = max(final, -(length(rep4.xz)*0.0555 - 0.25*0.0555));
final = max(final, -(length(rep4.xy)*0.0555 - 0.25*0.0555));
final = max(final, -(length(rep4.yz)*0.0555 - 0.25*0.0555));
vec3 rep5 = fract(rep4*3.0)-0.5;
final = max(final, -(length(rep5.xz)*0.0185 - 0.25*0.0185));
final = max(final, -(length(rep5.xy)*0.0185 - 0.25*0.0185));
final = max(final, -(length(rep5.yz)*0.0185 - 0.25*0.0185));
#endif
// Cut out stuff outside of outer sphere
final = max(final, (length(p) - outerSphereRad));
// Carve out inner sphere
final = max(final, -(length(p) - 2.8));
//final = max(final, abs(p.x) - 2.0); // for that space station look
//final = (length(p) - outerSphereRad); // for debugging texture and lighting
// Slice the object in a 3d grid so it can rotate like a rubik's cube
float slice = 0.02;
vec3 grid = -abs(fract(p.xyz)) + slice;
final = max(final, grid.x);
final = max(final, grid.y);
final = max(final, grid.z);
//final = min(final, abs(p.y));
return final;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// ---------------- First, set up the camera rays for ray marching ----------------
vec2 uv = fragCoord.xy/iResolution.xy * 2.0 - 1.0;
// Camera up vector.
vec3 camUp=vec3(0,1,0);
// Camera lookat.
camLookat=vec3(0,0.0,0);
// debugging camera
float mx=iMouse.x/iResolution.x*PI*2.0 + iTime * 0.166;
float my=-iMouse.y/iResolution.y*10.0 + sin(iTime * 0.3)*0.8+0.1;//*PI/2.01;
// move camera in and out of the sphere
float smallTime = iTime*0.2;
float inOut = pow(abs(-cos(smallTime)), 0.6)* sign(-cos(smallTime));
camPos += vec3(cos(my)*cos(mx),sin(my),cos(my)*sin(mx))*(3.35+inOut*2.0);
// add randomness to camera for depth-of-field look close up.
//camPos += vec3(Hash2d(uv)*0.91, Hash2d(uv+37.0), Hash2d(uv+47.0))*0.01;
// Camera setup.
vec3 camVec=normalize(camLookat - camPos);
vec3 sideNorm=normalize(cross(camUp, camVec));
vec3 upNorm=cross(camVec, sideNorm);
vec3 worldFacing=(camPos + camVec);
vec3 worldPix = worldFacing + uv.x * sideNorm * (iResolution.x/iResolution.y) + uv.y * upNorm;
vec3 relVec = normalize(worldPix - camPos);
// -------------------------------- animate ---------------------------------------
float localTime = iTime*0.5;
float floorTime = floor(localTime);
float zeroToOne = max(0.0,fract(localTime)*1.0-0.0);// *4.0-3.0);
// This is the 0..1 for the rotation
chunkAnim = smoothstep(0.0, 1.0, zeroToOne);
// This is for brightening the outer sphere when a rotation happens
float pulse = saturate(-log(zeroToOne*30.0)+2.0);
//float mft = mod(floorTime, 6.0);
// Let's make it rotate a random part every time
float mft = Hash1d(floorTime * 2.34567);
mft = floor(mft * 5.9999); // get a random [0..6) integer
// randomize where the rotation slice is
float uglyRand = Hash1d(floorTime*1.234567);
uglyRand = floor(uglyRand*2.999); // get a random [0..3) integer
uglyRand = 1.0 / (uglyRand + 1.0);
// Check which axis we should rotate on and make a matrix for it.
if (mft <= 1.0)
{
rotAmount = PI;
float cos = cos(chunkAnim * rotAmount);
float sin = sin(chunkAnim * rotAmount);
rotMat = mat3(1.0, 0.0, 0.0,
0.0, cos, sin,
0.0, -sin, cos);
rotDir = vec3(uglyRand, 0.0, 0.0);
}
else if (mft <= 3.0)
{
rotAmount = PI;
float cos = cos(chunkAnim * rotAmount);
float sin = sin(chunkAnim * rotAmount);
rotMat = mat3(cos, 0.0, -sin,
0.0, 1.0, 0.0,
sin, 0.0, cos);
rotDir = vec3(0.0, uglyRand, 0.0);
}
else
{
rotAmount = PI;
float cos = cos(chunkAnim * rotAmount);
float sin = sin(chunkAnim * rotAmount);
rotMat = mat3(cos, sin, 0.0,
-sin, cos, 0.0,
0.0, 0.0, 1.0);
rotDir = vec3(0.0, 0.0, uglyRand);
}
if (mod(floorTime, 2.0) == 0.0) rotDir = -rotDir;
// --------------------------------------------------------------------------------
float dist = 0.15;
float t = 0.2 + Hash2d(uv)*0.1; // fade things close to the camera
float inc = 0.02;
float maxDepth = 11.0;
vec3 pos = vec3(0,0,0);
float glow = 0.0;
// ray marching time
for (int i = 0; i < 110; i++) // This is the count of the max times the ray actually marches.
{
if ((t > maxDepth) || (abs(dist) < 0.001)) break;
pos = camPos + relVec * t;
// *******************************************************
// This is _the_ function that defines the "distance field".
// It's really what makes the scene geometry.
// *******************************************************
dist = DistanceToObject(pos);
// Do some tricks for marching so that we can march the inner glow sphere
float lp = length(pos);
//if (lp > outerSphereRad + 0.9) break;
float inv = max(0.0, 0.1*dist / lp - 0.1);
dist = min(max(0.15,lp*0.6 - 0.1), dist);
glow += inv;//0.001
glow += 0.0025;
// no deformations messing up the distance function this time. Hurray for getting the math right!
t += dist;//*0.9995; // because deformations mess up distance function.
}
// --------------------------------------------------------------------------------
// Now that we have done our ray marching, let's put some color on this geometry.
#ifdef MOVING_SUN
vec3 sunDir = normalize(vec3(sin(iTime*0.047-1.5), cos(iTime*0.047-1.5), -0.5));
#else
vec3 sunDir = normalize(vec3(0.93, 1.0, -1.5));
#endif
vec3 finalColor = vec3(0.0);
// If a ray actually hit the object, let's light it.
if (abs(dist) < 0.75)
//if (t <= maxDepth)
{
// calculate the normal from the distance field. The distance field is a volume, so if you
// sample the current point and neighboring points, you can use the difference to get
// the normal.
vec3 smallVec = vec3(0.0025, 0, 0);
vec3 normalU = vec3(dist - DistanceToObject(pos - smallVec.xyy),
dist - DistanceToObject(pos - smallVec.yxy),
dist - DistanceToObject(pos - smallVec.yyx));
vec3 normal = normalize(normalU);
// calculate 2 ambient occlusion values. One for global stuff and one
// for local stuff
float ambientS = 1.0;
//ambientS *= saturate(DistanceToObject(pos + normal * 0.1)*10.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.2)*5.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.4)*2.5);
ambientS *= saturate(DistanceToObject(pos + normal * 0.8)*1.25);
float ambient = ambientS * saturate(DistanceToObject(pos + normal * 1.6)*1.25*0.5);
ambient *= saturate(DistanceToObject(pos + normal * 3.2)*1.25*0.25);
ambient *= saturate(DistanceToObject(pos + normal * 6.4)*1.25*0.125);
//ambient = max(0.05, pow(ambient, 0.3)); // tone down ambient with a pow and min clamp it.
ambient = saturate(ambient);
// Trace a ray toward the sun for sun shadows
float sunShadow = 1.0;
float iter = 0.05;
for (int i = 0; i < 30; i++)
{
vec3 tempPos = pos + sunDir * iter;
//if (dot(tempPos, tempPos) > outerSphereRad*outerSphereRad+0.8) break;
if (iter > outerSphereRad + outerSphereRad) break;
float tempDist = DistanceToObject(tempPos);
sunShadow *= saturate(tempDist*50.0);
if (tempDist <= 0.0) break;
//iter *= 1.5; // constant is more reliable than distance-based???
iter += max(0.01, tempDist)*1.0;
}
sunShadow = saturate(sunShadow);
// calculate the reflection vector for highlights
vec3 ref = reflect(relVec, normal);
// make sure the texture gets rotated along with the geometry.
vec3 posTex = pos;
if (dot(pos, rotDir) > 1.0) posTex = pos * rotMat;
posTex = abs(posTex); // make texture symetric so it doesn't pop after rotation
// make a few frequencies of noise to give it some texture
float n =0.0;
n += noise(posTex*32.0);
n += noise(posTex*64.0);
n += noise(posTex*128.0);
n += noise(posTex*256.0);
n += noise(posTex*512.0);
n *= 0.8;
normal = normalize(normal + n*0.1);
// ------ Calculate texture color ------
vec3 texColor = vec3(0.95, 1.0, 1.0);
vec3 rust = vec3(0.65, 0.25, 0.1) - noise(posTex*128.0);
texColor *= smoothstep(texColor, rust, vec3(saturate(RustNoise3D(posTex*8.0))-0.2));
// make outer edge a little brighter
texColor += (1.0 - vec3(19.0, 5.0, 2.0) * length(normalU))*ambientS;
// apply noise
texColor *= vec3(1.0)*n*0.05;
texColor *= 0.7;
texColor = saturate(texColor);
// ------ Calculate lighting color ------
// Start with sun color, standard lighting equation, and shadow
vec3 lightColor = vec3(0.6) * saturate(dot(sunDir, normal)) * sunShadow;
// weighted average the near ambient occlusion with the far for just the right look
float ambientAvg = (ambient*3.0 + ambientS) * 0.25;
// a red and blue light coming from different directions
lightColor += (vec3(1.0, 0.2, 0.4) * saturate(-normal.z *0.5+0.5))*pow(ambientAvg, 0.5);
lightColor += (vec3(0.1, 0.5, 0.99) * saturate(normal.y *0.5+0.5))*pow(ambientAvg, 0.5);
// blue glow light coming from the glow in the middle of the sphere
lightColor += vec3(0.3, 0.5, 0.9) * saturate(dot(-pos, normal))*pow(ambientS, 0.3);
// lightColor *= ambient;
lightColor *= 4.0;
// finally, apply the light to the texture.
finalColor = texColor * lightColor;
// sun reflection to make it look metal
finalColor += vec3(1.0)*pow(n,4.0)* GetSunColorSmall(ref, sunDir) * sunShadow;// * ambientS;
// fog that fades to reddish plus the sun color so that fog is brightest towards sun
//finalColor = mix(vec3(1.0, 0.41, 0.41)*skyMultiplier + min(vec3(0.25),GetSunColorSmall(relVec, sunDir))*2.0*sunSet, finalColor, exp(-t*0.003));
// pulse the outer edge color when something is about to rotate
if (dot(pos, rotDir) > 1.0) finalColor += vec3(0.2, 1.4, 0.8)*pulse*saturate(0.000001 / pow(abs(length(pos)-outerSphereRad), 2.0))*2.0;
}
else
{
// Our ray trace hit nothing, so draw sky.
//finalColor = saturate(GetSunColorSmall(relVec, sunDir)*0.95-0.01);
}
// add the ray marching glow
finalColor += vec3(0.3, 0.5, 0.9) * glow;
// vignette?
finalColor *= vec3(1.0) * saturate(1.0 - length(uv/2.5));
finalColor *= 1.3;
// output the final color with sqrt for "gamma correction"
fragColor = vec4(sqrt(clamp(finalColor, 0.0, 1.0)),1.0);
}
| cc0-1.0 | [
695,
714,
737,
737,
810
] | [
[
695,
714,
737,
737,
810
],
[
811,
811,
834,
834,
905
],
[
906,
906,
929,
929,
1015
],
[
1016,
1016,
1057,
1057,
1100
],
[
1138,
1138,
1162,
1162,
1435
],
[
1436,
1436,
1458,
1458,
2043
],
[
2198,
2198,
2221,
2221,
2250
],
[
2251,
2251,
2274,
2274,
2303
],
[
2304,
2304,
2329,
2329,
2358
],
[
2489,
2489,
2543,
2543,
2846
],
[
2847,
2847,
2896,
2896,
3150
],
[
3152,
3681,
3708,
3708,
4272
],
[
4341,
4412,
4444,
4534,
6672
],
[
6674,
6674,
6731,
6816,
16639
]
] | // noise functions
| float Hash1d(float u)
{ |
return fract(sin(u)*143.9); // scale this down to kill the jitters
} | // noise functions
float Hash1d(float u)
{ | 6 | 6 |
4dSXDd | otaviogood | 2014-11-27T22:26:38 | /*--------------------------------------------------------------------------------------
License CC0 - http://creativecommons.org/publicdomain/zero/1.0/
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
----------------------------------------------------------------------------------------
^ This means do ANYTHING YOU WANT with this code. Because we are programmers, not lawyers.
-Otavio Good
*/
// This will lower the framerate, but looks kinda cool
//#define TOO_MUCH_FRACTAL
//#define MOVING_SUN
float outerSphereRad = 3.5;
// noise functions
float Hash1d(float u)
{
return fract(sin(u)*143.9); // scale this down to kill the jitters
}
float Hash2d(vec2 uv)
{
float f = uv.x + uv.y * 37.0;
return fract(sin(f)*104003.9);
}
float Hash3d(vec3 uv)
{
float f = uv.x + uv.y * 37.0 + uv.z * 521.0;
return fract(sin(f)*110003.9);
}
float mixP(float f0, float f1, float a)
{
return mix(f0, f1, a*a*(3.0-2.0*a));
}
const vec2 zeroOne = vec2(0.0, 1.0);
float noise2d(vec2 uv)
{
vec2 fr = fract(uv.xy);
vec2 fl = floor(uv.xy);
float h00 = Hash2d(fl);
float h10 = Hash2d(fl + zeroOne.yx);
float h01 = Hash2d(fl + zeroOne);
float h11 = Hash2d(fl + zeroOne.yy);
return mixP(mixP(h00, h10, fr.x), mixP(h01, h11, fr.x), fr.y);
}
float noise(vec3 uv)
{
vec3 fr = fract(uv.xyz);
vec3 fl = floor(uv.xyz);
float h000 = Hash3d(fl);
float h100 = Hash3d(fl + zeroOne.yxx);
float h010 = Hash3d(fl + zeroOne.xyx);
float h110 = Hash3d(fl + zeroOne.yyx);
float h001 = Hash3d(fl + zeroOne.xxy);
float h101 = Hash3d(fl + zeroOne.yxy);
float h011 = Hash3d(fl + zeroOne.xyy);
float h111 = Hash3d(fl + zeroOne.yyy);
return mixP(
mixP(mixP(h000, h100, fr.x),
mixP(h010, h110, fr.x), fr.y),
mixP(mixP(h001, h101, fr.x),
mixP(h011, h111, fr.x), fr.y)
, fr.z);
}
float PI=3.14159265;
// Variables for animating and rotating the sides of the object
float chunkAnim = 0.0;
mat3 rotMat;
vec3 rotDir;
float rotAmount;
vec3 saturate(vec3 a) { return clamp(a, 0.0, 1.0); }
vec2 saturate(vec2 a) { return clamp(a, 0.0, 1.0); }
float saturate(float a) { return clamp(a, 0.0, 1.0); }
// This function basically is a procedural environment map that makes the sun
vec3 sunCol = vec3(258.0, 208.0, 100.0) / 4255.0;
vec3 GetSunColorReflection(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.015 / dist;
sunIntensity = pow(sunIntensity, 0.3)*100.0;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.0425;
}
vec3 GetSunColorSmall(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.05 / dist;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.025;
}
// This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.71; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
// Total hack of the spiral noise function to get a rust look
float RustNoise3D(vec3 p)
{
float n = 0.0;
float iter = 1.0;
float pn = noise(p*0.125);
pn += noise(p*0.25)*0.5;
pn += noise(p*0.5)*0.25;
pn += noise(p*1.0)*0.125;
for (int i = 0; i < 7; i++)
{
//n += (sin(p.y*iter) + cos(p.x*iter)) / iter;
float wave = saturate(cos(p.y*0.25 + pn) - 0.998);
// wave *= noise(p * 0.125)*1016.0;
n += wave;
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.4733;
}
return n*500.0;
}
vec3 camPos = vec3(0.0), camFacing;
vec3 camLookat=vec3(0,0.0,0);
// This is the big money function that makes the crazy fractally shape
float DistanceToObject(vec3 p)
{
//p += (1.0/p.y)*0.6;
// Rotate, but only the part that is on the side of rotDir
if (dot(p, rotDir) > 1.0) p *= rotMat;
// Repeat our position so we can carve out many cylindrical-like things from our solid
vec3 rep = fract(p)-0.5;
//final = max(final, -(length(rep.xz*rep.xz)*1.0 - 0.0326));
float final = -(length(rep.xy*rep.xz) - 0.109);
final = max(final, -(length(rep.zy) - 0.33));
//final = max(final, -(length(rep.xz*rep.xz) - 0.03));
//final = max(final, -(length(rep.yz*rep.yz) - 0.03));
//final = max(final, -(length(rep.xy*rep.xy) - 0.030266));
// Repeat the process of carving things out for smaller scales
vec3 rep2 = fract(rep*2.0)-0.5;
final = max(final, -(length(rep2.xz)*0.5 - 0.125));
final = max(final, -(length(rep2.xy)*0.5 - 0.125));
final = max(final, -(length(rep2.zy)*0.5 - 0.125));
vec3 rep3 = fract(rep2*3.0)-0.5;
final = max(final, -(length(rep3.xz)*0.1667 - 0.25*0.1667));
final = max(final, -(length(rep3.xy)*0.1667 - 0.25*0.1667));
final = max(final, -(length(rep3.zy)*0.1667 - 0.25*0.1667));
#ifdef TOO_MUCH_FRACTAL
vec3 rep4 = fract(rep3*3.0)-0.5;
final = max(final, -(length(rep4.xz)*0.0555 - 0.25*0.0555));
final = max(final, -(length(rep4.xy)*0.0555 - 0.25*0.0555));
final = max(final, -(length(rep4.yz)*0.0555 - 0.25*0.0555));
vec3 rep5 = fract(rep4*3.0)-0.5;
final = max(final, -(length(rep5.xz)*0.0185 - 0.25*0.0185));
final = max(final, -(length(rep5.xy)*0.0185 - 0.25*0.0185));
final = max(final, -(length(rep5.yz)*0.0185 - 0.25*0.0185));
#endif
// Cut out stuff outside of outer sphere
final = max(final, (length(p) - outerSphereRad));
// Carve out inner sphere
final = max(final, -(length(p) - 2.8));
//final = max(final, abs(p.x) - 2.0); // for that space station look
//final = (length(p) - outerSphereRad); // for debugging texture and lighting
// Slice the object in a 3d grid so it can rotate like a rubik's cube
float slice = 0.02;
vec3 grid = -abs(fract(p.xyz)) + slice;
final = max(final, grid.x);
final = max(final, grid.y);
final = max(final, grid.z);
//final = min(final, abs(p.y));
return final;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// ---------------- First, set up the camera rays for ray marching ----------------
vec2 uv = fragCoord.xy/iResolution.xy * 2.0 - 1.0;
// Camera up vector.
vec3 camUp=vec3(0,1,0);
// Camera lookat.
camLookat=vec3(0,0.0,0);
// debugging camera
float mx=iMouse.x/iResolution.x*PI*2.0 + iTime * 0.166;
float my=-iMouse.y/iResolution.y*10.0 + sin(iTime * 0.3)*0.8+0.1;//*PI/2.01;
// move camera in and out of the sphere
float smallTime = iTime*0.2;
float inOut = pow(abs(-cos(smallTime)), 0.6)* sign(-cos(smallTime));
camPos += vec3(cos(my)*cos(mx),sin(my),cos(my)*sin(mx))*(3.35+inOut*2.0);
// add randomness to camera for depth-of-field look close up.
//camPos += vec3(Hash2d(uv)*0.91, Hash2d(uv+37.0), Hash2d(uv+47.0))*0.01;
// Camera setup.
vec3 camVec=normalize(camLookat - camPos);
vec3 sideNorm=normalize(cross(camUp, camVec));
vec3 upNorm=cross(camVec, sideNorm);
vec3 worldFacing=(camPos + camVec);
vec3 worldPix = worldFacing + uv.x * sideNorm * (iResolution.x/iResolution.y) + uv.y * upNorm;
vec3 relVec = normalize(worldPix - camPos);
// -------------------------------- animate ---------------------------------------
float localTime = iTime*0.5;
float floorTime = floor(localTime);
float zeroToOne = max(0.0,fract(localTime)*1.0-0.0);// *4.0-3.0);
// This is the 0..1 for the rotation
chunkAnim = smoothstep(0.0, 1.0, zeroToOne);
// This is for brightening the outer sphere when a rotation happens
float pulse = saturate(-log(zeroToOne*30.0)+2.0);
//float mft = mod(floorTime, 6.0);
// Let's make it rotate a random part every time
float mft = Hash1d(floorTime * 2.34567);
mft = floor(mft * 5.9999); // get a random [0..6) integer
// randomize where the rotation slice is
float uglyRand = Hash1d(floorTime*1.234567);
uglyRand = floor(uglyRand*2.999); // get a random [0..3) integer
uglyRand = 1.0 / (uglyRand + 1.0);
// Check which axis we should rotate on and make a matrix for it.
if (mft <= 1.0)
{
rotAmount = PI;
float cos = cos(chunkAnim * rotAmount);
float sin = sin(chunkAnim * rotAmount);
rotMat = mat3(1.0, 0.0, 0.0,
0.0, cos, sin,
0.0, -sin, cos);
rotDir = vec3(uglyRand, 0.0, 0.0);
}
else if (mft <= 3.0)
{
rotAmount = PI;
float cos = cos(chunkAnim * rotAmount);
float sin = sin(chunkAnim * rotAmount);
rotMat = mat3(cos, 0.0, -sin,
0.0, 1.0, 0.0,
sin, 0.0, cos);
rotDir = vec3(0.0, uglyRand, 0.0);
}
else
{
rotAmount = PI;
float cos = cos(chunkAnim * rotAmount);
float sin = sin(chunkAnim * rotAmount);
rotMat = mat3(cos, sin, 0.0,
-sin, cos, 0.0,
0.0, 0.0, 1.0);
rotDir = vec3(0.0, 0.0, uglyRand);
}
if (mod(floorTime, 2.0) == 0.0) rotDir = -rotDir;
// --------------------------------------------------------------------------------
float dist = 0.15;
float t = 0.2 + Hash2d(uv)*0.1; // fade things close to the camera
float inc = 0.02;
float maxDepth = 11.0;
vec3 pos = vec3(0,0,0);
float glow = 0.0;
// ray marching time
for (int i = 0; i < 110; i++) // This is the count of the max times the ray actually marches.
{
if ((t > maxDepth) || (abs(dist) < 0.001)) break;
pos = camPos + relVec * t;
// *******************************************************
// This is _the_ function that defines the "distance field".
// It's really what makes the scene geometry.
// *******************************************************
dist = DistanceToObject(pos);
// Do some tricks for marching so that we can march the inner glow sphere
float lp = length(pos);
//if (lp > outerSphereRad + 0.9) break;
float inv = max(0.0, 0.1*dist / lp - 0.1);
dist = min(max(0.15,lp*0.6 - 0.1), dist);
glow += inv;//0.001
glow += 0.0025;
// no deformations messing up the distance function this time. Hurray for getting the math right!
t += dist;//*0.9995; // because deformations mess up distance function.
}
// --------------------------------------------------------------------------------
// Now that we have done our ray marching, let's put some color on this geometry.
#ifdef MOVING_SUN
vec3 sunDir = normalize(vec3(sin(iTime*0.047-1.5), cos(iTime*0.047-1.5), -0.5));
#else
vec3 sunDir = normalize(vec3(0.93, 1.0, -1.5));
#endif
vec3 finalColor = vec3(0.0);
// If a ray actually hit the object, let's light it.
if (abs(dist) < 0.75)
//if (t <= maxDepth)
{
// calculate the normal from the distance field. The distance field is a volume, so if you
// sample the current point and neighboring points, you can use the difference to get
// the normal.
vec3 smallVec = vec3(0.0025, 0, 0);
vec3 normalU = vec3(dist - DistanceToObject(pos - smallVec.xyy),
dist - DistanceToObject(pos - smallVec.yxy),
dist - DistanceToObject(pos - smallVec.yyx));
vec3 normal = normalize(normalU);
// calculate 2 ambient occlusion values. One for global stuff and one
// for local stuff
float ambientS = 1.0;
//ambientS *= saturate(DistanceToObject(pos + normal * 0.1)*10.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.2)*5.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.4)*2.5);
ambientS *= saturate(DistanceToObject(pos + normal * 0.8)*1.25);
float ambient = ambientS * saturate(DistanceToObject(pos + normal * 1.6)*1.25*0.5);
ambient *= saturate(DistanceToObject(pos + normal * 3.2)*1.25*0.25);
ambient *= saturate(DistanceToObject(pos + normal * 6.4)*1.25*0.125);
//ambient = max(0.05, pow(ambient, 0.3)); // tone down ambient with a pow and min clamp it.
ambient = saturate(ambient);
// Trace a ray toward the sun for sun shadows
float sunShadow = 1.0;
float iter = 0.05;
for (int i = 0; i < 30; i++)
{
vec3 tempPos = pos + sunDir * iter;
//if (dot(tempPos, tempPos) > outerSphereRad*outerSphereRad+0.8) break;
if (iter > outerSphereRad + outerSphereRad) break;
float tempDist = DistanceToObject(tempPos);
sunShadow *= saturate(tempDist*50.0);
if (tempDist <= 0.0) break;
//iter *= 1.5; // constant is more reliable than distance-based???
iter += max(0.01, tempDist)*1.0;
}
sunShadow = saturate(sunShadow);
// calculate the reflection vector for highlights
vec3 ref = reflect(relVec, normal);
// make sure the texture gets rotated along with the geometry.
vec3 posTex = pos;
if (dot(pos, rotDir) > 1.0) posTex = pos * rotMat;
posTex = abs(posTex); // make texture symetric so it doesn't pop after rotation
// make a few frequencies of noise to give it some texture
float n =0.0;
n += noise(posTex*32.0);
n += noise(posTex*64.0);
n += noise(posTex*128.0);
n += noise(posTex*256.0);
n += noise(posTex*512.0);
n *= 0.8;
normal = normalize(normal + n*0.1);
// ------ Calculate texture color ------
vec3 texColor = vec3(0.95, 1.0, 1.0);
vec3 rust = vec3(0.65, 0.25, 0.1) - noise(posTex*128.0);
texColor *= smoothstep(texColor, rust, vec3(saturate(RustNoise3D(posTex*8.0))-0.2));
// make outer edge a little brighter
texColor += (1.0 - vec3(19.0, 5.0, 2.0) * length(normalU))*ambientS;
// apply noise
texColor *= vec3(1.0)*n*0.05;
texColor *= 0.7;
texColor = saturate(texColor);
// ------ Calculate lighting color ------
// Start with sun color, standard lighting equation, and shadow
vec3 lightColor = vec3(0.6) * saturate(dot(sunDir, normal)) * sunShadow;
// weighted average the near ambient occlusion with the far for just the right look
float ambientAvg = (ambient*3.0 + ambientS) * 0.25;
// a red and blue light coming from different directions
lightColor += (vec3(1.0, 0.2, 0.4) * saturate(-normal.z *0.5+0.5))*pow(ambientAvg, 0.5);
lightColor += (vec3(0.1, 0.5, 0.99) * saturate(normal.y *0.5+0.5))*pow(ambientAvg, 0.5);
// blue glow light coming from the glow in the middle of the sphere
lightColor += vec3(0.3, 0.5, 0.9) * saturate(dot(-pos, normal))*pow(ambientS, 0.3);
// lightColor *= ambient;
lightColor *= 4.0;
// finally, apply the light to the texture.
finalColor = texColor * lightColor;
// sun reflection to make it look metal
finalColor += vec3(1.0)*pow(n,4.0)* GetSunColorSmall(ref, sunDir) * sunShadow;// * ambientS;
// fog that fades to reddish plus the sun color so that fog is brightest towards sun
//finalColor = mix(vec3(1.0, 0.41, 0.41)*skyMultiplier + min(vec3(0.25),GetSunColorSmall(relVec, sunDir))*2.0*sunSet, finalColor, exp(-t*0.003));
// pulse the outer edge color when something is about to rotate
if (dot(pos, rotDir) > 1.0) finalColor += vec3(0.2, 1.4, 0.8)*pulse*saturate(0.000001 / pow(abs(length(pos)-outerSphereRad), 2.0))*2.0;
}
else
{
// Our ray trace hit nothing, so draw sky.
//finalColor = saturate(GetSunColorSmall(relVec, sunDir)*0.95-0.01);
}
// add the ray marching glow
finalColor += vec3(0.3, 0.5, 0.9) * glow;
// vignette?
finalColor *= vec3(1.0) * saturate(1.0 - length(uv/2.5));
finalColor *= 1.3;
// output the final color with sqrt for "gamma correction"
fragColor = vec4(sqrt(clamp(finalColor, 0.0, 1.0)),1.0);
}
| cc0-1.0 | [
3152,
3681,
3708,
3708,
4272
] | [
[
695,
714,
737,
737,
810
],
[
811,
811,
834,
834,
905
],
[
906,
906,
929,
929,
1015
],
[
1016,
1016,
1057,
1057,
1100
],
[
1138,
1138,
1162,
1162,
1435
],
[
1436,
1436,
1458,
1458,
2043
],
[
2198,
2198,
2221,
2221,
2250
],
[
2251,
2251,
2274,
2274,
2303
],
[
2304,
2304,
2329,
2329,
2358
],
[
2489,
2489,
2543,
2543,
2846
],
[
2847,
2847,
2896,
2896,
3150
],
[
3152,
3681,
3708,
3708,
4272
],
[
4341,
4412,
4444,
4534,
6672
],
[
6674,
6674,
6731,
6816,
16639
]
] | // This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.71; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
// Total hack of the spiral noise function to get a rust look
| float RustNoise3D(vec3 p)
{ |
float n = 0.0;
float iter = 1.0;
float pn = noise(p*0.125);
pn += noise(p*0.25)*0.5;
pn += noise(p*0.5)*0.25;
pn += noise(p*1.0)*0.125;
for (int i = 0; i < 7; i++)
{
//n += (sin(p.y*iter) + cos(p.x*iter)) / iter;
float wave = saturate(cos(p.y*0.25 + pn) - 0.998);
// wave *= noise(p * 0.125)*1016.0;
n += wave;
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.4733;
}
return n*500.0;
} | // This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.71; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
// Total hack of the spiral noise function to get a rust look
float RustNoise3D(vec3 p)
{ | 1 | 2 |
4dSXDd | otaviogood | 2014-11-27T22:26:38 | /*--------------------------------------------------------------------------------------
License CC0 - http://creativecommons.org/publicdomain/zero/1.0/
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
----------------------------------------------------------------------------------------
^ This means do ANYTHING YOU WANT with this code. Because we are programmers, not lawyers.
-Otavio Good
*/
// This will lower the framerate, but looks kinda cool
//#define TOO_MUCH_FRACTAL
//#define MOVING_SUN
float outerSphereRad = 3.5;
// noise functions
float Hash1d(float u)
{
return fract(sin(u)*143.9); // scale this down to kill the jitters
}
float Hash2d(vec2 uv)
{
float f = uv.x + uv.y * 37.0;
return fract(sin(f)*104003.9);
}
float Hash3d(vec3 uv)
{
float f = uv.x + uv.y * 37.0 + uv.z * 521.0;
return fract(sin(f)*110003.9);
}
float mixP(float f0, float f1, float a)
{
return mix(f0, f1, a*a*(3.0-2.0*a));
}
const vec2 zeroOne = vec2(0.0, 1.0);
float noise2d(vec2 uv)
{
vec2 fr = fract(uv.xy);
vec2 fl = floor(uv.xy);
float h00 = Hash2d(fl);
float h10 = Hash2d(fl + zeroOne.yx);
float h01 = Hash2d(fl + zeroOne);
float h11 = Hash2d(fl + zeroOne.yy);
return mixP(mixP(h00, h10, fr.x), mixP(h01, h11, fr.x), fr.y);
}
float noise(vec3 uv)
{
vec3 fr = fract(uv.xyz);
vec3 fl = floor(uv.xyz);
float h000 = Hash3d(fl);
float h100 = Hash3d(fl + zeroOne.yxx);
float h010 = Hash3d(fl + zeroOne.xyx);
float h110 = Hash3d(fl + zeroOne.yyx);
float h001 = Hash3d(fl + zeroOne.xxy);
float h101 = Hash3d(fl + zeroOne.yxy);
float h011 = Hash3d(fl + zeroOne.xyy);
float h111 = Hash3d(fl + zeroOne.yyy);
return mixP(
mixP(mixP(h000, h100, fr.x),
mixP(h010, h110, fr.x), fr.y),
mixP(mixP(h001, h101, fr.x),
mixP(h011, h111, fr.x), fr.y)
, fr.z);
}
float PI=3.14159265;
// Variables for animating and rotating the sides of the object
float chunkAnim = 0.0;
mat3 rotMat;
vec3 rotDir;
float rotAmount;
vec3 saturate(vec3 a) { return clamp(a, 0.0, 1.0); }
vec2 saturate(vec2 a) { return clamp(a, 0.0, 1.0); }
float saturate(float a) { return clamp(a, 0.0, 1.0); }
// This function basically is a procedural environment map that makes the sun
vec3 sunCol = vec3(258.0, 208.0, 100.0) / 4255.0;
vec3 GetSunColorReflection(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.015 / dist;
sunIntensity = pow(sunIntensity, 0.3)*100.0;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.0425;
}
vec3 GetSunColorSmall(vec3 rayDir, vec3 sunDir)
{
vec3 localRay = normalize(rayDir);
float dist = 1.0 - (dot(localRay, sunDir) * 0.5 + 0.5);
float sunIntensity = 0.05 / dist;
sunIntensity += exp(-dist*12.0)*300.0;
sunIntensity = min(sunIntensity, 40000.0);
return sunCol * sunIntensity*0.025;
}
// This spiral noise works by successively adding and rotating sin waves while increasing frequency.
// It should work the same on all computers since it's not based on a hash function like some other noises.
// It can be much faster than other noise functions if you're ok with some repetition.
const float nudge = 0.71; // size of perpendicular vector
float normalizer = 1.0 / sqrt(1.0 + nudge*nudge); // pythagorean theorem on that perpendicular to maintain scale
// Total hack of the spiral noise function to get a rust look
float RustNoise3D(vec3 p)
{
float n = 0.0;
float iter = 1.0;
float pn = noise(p*0.125);
pn += noise(p*0.25)*0.5;
pn += noise(p*0.5)*0.25;
pn += noise(p*1.0)*0.125;
for (int i = 0; i < 7; i++)
{
//n += (sin(p.y*iter) + cos(p.x*iter)) / iter;
float wave = saturate(cos(p.y*0.25 + pn) - 0.998);
// wave *= noise(p * 0.125)*1016.0;
n += wave;
p.xy += vec2(p.y, -p.x) * nudge;
p.xy *= normalizer;
p.xz += vec2(p.z, -p.x) * nudge;
p.xz *= normalizer;
iter *= 1.4733;
}
return n*500.0;
}
vec3 camPos = vec3(0.0), camFacing;
vec3 camLookat=vec3(0,0.0,0);
// This is the big money function that makes the crazy fractally shape
float DistanceToObject(vec3 p)
{
//p += (1.0/p.y)*0.6;
// Rotate, but only the part that is on the side of rotDir
if (dot(p, rotDir) > 1.0) p *= rotMat;
// Repeat our position so we can carve out many cylindrical-like things from our solid
vec3 rep = fract(p)-0.5;
//final = max(final, -(length(rep.xz*rep.xz)*1.0 - 0.0326));
float final = -(length(rep.xy*rep.xz) - 0.109);
final = max(final, -(length(rep.zy) - 0.33));
//final = max(final, -(length(rep.xz*rep.xz) - 0.03));
//final = max(final, -(length(rep.yz*rep.yz) - 0.03));
//final = max(final, -(length(rep.xy*rep.xy) - 0.030266));
// Repeat the process of carving things out for smaller scales
vec3 rep2 = fract(rep*2.0)-0.5;
final = max(final, -(length(rep2.xz)*0.5 - 0.125));
final = max(final, -(length(rep2.xy)*0.5 - 0.125));
final = max(final, -(length(rep2.zy)*0.5 - 0.125));
vec3 rep3 = fract(rep2*3.0)-0.5;
final = max(final, -(length(rep3.xz)*0.1667 - 0.25*0.1667));
final = max(final, -(length(rep3.xy)*0.1667 - 0.25*0.1667));
final = max(final, -(length(rep3.zy)*0.1667 - 0.25*0.1667));
#ifdef TOO_MUCH_FRACTAL
vec3 rep4 = fract(rep3*3.0)-0.5;
final = max(final, -(length(rep4.xz)*0.0555 - 0.25*0.0555));
final = max(final, -(length(rep4.xy)*0.0555 - 0.25*0.0555));
final = max(final, -(length(rep4.yz)*0.0555 - 0.25*0.0555));
vec3 rep5 = fract(rep4*3.0)-0.5;
final = max(final, -(length(rep5.xz)*0.0185 - 0.25*0.0185));
final = max(final, -(length(rep5.xy)*0.0185 - 0.25*0.0185));
final = max(final, -(length(rep5.yz)*0.0185 - 0.25*0.0185));
#endif
// Cut out stuff outside of outer sphere
final = max(final, (length(p) - outerSphereRad));
// Carve out inner sphere
final = max(final, -(length(p) - 2.8));
//final = max(final, abs(p.x) - 2.0); // for that space station look
//final = (length(p) - outerSphereRad); // for debugging texture and lighting
// Slice the object in a 3d grid so it can rotate like a rubik's cube
float slice = 0.02;
vec3 grid = -abs(fract(p.xyz)) + slice;
final = max(final, grid.x);
final = max(final, grid.y);
final = max(final, grid.z);
//final = min(final, abs(p.y));
return final;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// ---------------- First, set up the camera rays for ray marching ----------------
vec2 uv = fragCoord.xy/iResolution.xy * 2.0 - 1.0;
// Camera up vector.
vec3 camUp=vec3(0,1,0);
// Camera lookat.
camLookat=vec3(0,0.0,0);
// debugging camera
float mx=iMouse.x/iResolution.x*PI*2.0 + iTime * 0.166;
float my=-iMouse.y/iResolution.y*10.0 + sin(iTime * 0.3)*0.8+0.1;//*PI/2.01;
// move camera in and out of the sphere
float smallTime = iTime*0.2;
float inOut = pow(abs(-cos(smallTime)), 0.6)* sign(-cos(smallTime));
camPos += vec3(cos(my)*cos(mx),sin(my),cos(my)*sin(mx))*(3.35+inOut*2.0);
// add randomness to camera for depth-of-field look close up.
//camPos += vec3(Hash2d(uv)*0.91, Hash2d(uv+37.0), Hash2d(uv+47.0))*0.01;
// Camera setup.
vec3 camVec=normalize(camLookat - camPos);
vec3 sideNorm=normalize(cross(camUp, camVec));
vec3 upNorm=cross(camVec, sideNorm);
vec3 worldFacing=(camPos + camVec);
vec3 worldPix = worldFacing + uv.x * sideNorm * (iResolution.x/iResolution.y) + uv.y * upNorm;
vec3 relVec = normalize(worldPix - camPos);
// -------------------------------- animate ---------------------------------------
float localTime = iTime*0.5;
float floorTime = floor(localTime);
float zeroToOne = max(0.0,fract(localTime)*1.0-0.0);// *4.0-3.0);
// This is the 0..1 for the rotation
chunkAnim = smoothstep(0.0, 1.0, zeroToOne);
// This is for brightening the outer sphere when a rotation happens
float pulse = saturate(-log(zeroToOne*30.0)+2.0);
//float mft = mod(floorTime, 6.0);
// Let's make it rotate a random part every time
float mft = Hash1d(floorTime * 2.34567);
mft = floor(mft * 5.9999); // get a random [0..6) integer
// randomize where the rotation slice is
float uglyRand = Hash1d(floorTime*1.234567);
uglyRand = floor(uglyRand*2.999); // get a random [0..3) integer
uglyRand = 1.0 / (uglyRand + 1.0);
// Check which axis we should rotate on and make a matrix for it.
if (mft <= 1.0)
{
rotAmount = PI;
float cos = cos(chunkAnim * rotAmount);
float sin = sin(chunkAnim * rotAmount);
rotMat = mat3(1.0, 0.0, 0.0,
0.0, cos, sin,
0.0, -sin, cos);
rotDir = vec3(uglyRand, 0.0, 0.0);
}
else if (mft <= 3.0)
{
rotAmount = PI;
float cos = cos(chunkAnim * rotAmount);
float sin = sin(chunkAnim * rotAmount);
rotMat = mat3(cos, 0.0, -sin,
0.0, 1.0, 0.0,
sin, 0.0, cos);
rotDir = vec3(0.0, uglyRand, 0.0);
}
else
{
rotAmount = PI;
float cos = cos(chunkAnim * rotAmount);
float sin = sin(chunkAnim * rotAmount);
rotMat = mat3(cos, sin, 0.0,
-sin, cos, 0.0,
0.0, 0.0, 1.0);
rotDir = vec3(0.0, 0.0, uglyRand);
}
if (mod(floorTime, 2.0) == 0.0) rotDir = -rotDir;
// --------------------------------------------------------------------------------
float dist = 0.15;
float t = 0.2 + Hash2d(uv)*0.1; // fade things close to the camera
float inc = 0.02;
float maxDepth = 11.0;
vec3 pos = vec3(0,0,0);
float glow = 0.0;
// ray marching time
for (int i = 0; i < 110; i++) // This is the count of the max times the ray actually marches.
{
if ((t > maxDepth) || (abs(dist) < 0.001)) break;
pos = camPos + relVec * t;
// *******************************************************
// This is _the_ function that defines the "distance field".
// It's really what makes the scene geometry.
// *******************************************************
dist = DistanceToObject(pos);
// Do some tricks for marching so that we can march the inner glow sphere
float lp = length(pos);
//if (lp > outerSphereRad + 0.9) break;
float inv = max(0.0, 0.1*dist / lp - 0.1);
dist = min(max(0.15,lp*0.6 - 0.1), dist);
glow += inv;//0.001
glow += 0.0025;
// no deformations messing up the distance function this time. Hurray for getting the math right!
t += dist;//*0.9995; // because deformations mess up distance function.
}
// --------------------------------------------------------------------------------
// Now that we have done our ray marching, let's put some color on this geometry.
#ifdef MOVING_SUN
vec3 sunDir = normalize(vec3(sin(iTime*0.047-1.5), cos(iTime*0.047-1.5), -0.5));
#else
vec3 sunDir = normalize(vec3(0.93, 1.0, -1.5));
#endif
vec3 finalColor = vec3(0.0);
// If a ray actually hit the object, let's light it.
if (abs(dist) < 0.75)
//if (t <= maxDepth)
{
// calculate the normal from the distance field. The distance field is a volume, so if you
// sample the current point and neighboring points, you can use the difference to get
// the normal.
vec3 smallVec = vec3(0.0025, 0, 0);
vec3 normalU = vec3(dist - DistanceToObject(pos - smallVec.xyy),
dist - DistanceToObject(pos - smallVec.yxy),
dist - DistanceToObject(pos - smallVec.yyx));
vec3 normal = normalize(normalU);
// calculate 2 ambient occlusion values. One for global stuff and one
// for local stuff
float ambientS = 1.0;
//ambientS *= saturate(DistanceToObject(pos + normal * 0.1)*10.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.2)*5.0);
ambientS *= saturate(DistanceToObject(pos + normal * 0.4)*2.5);
ambientS *= saturate(DistanceToObject(pos + normal * 0.8)*1.25);
float ambient = ambientS * saturate(DistanceToObject(pos + normal * 1.6)*1.25*0.5);
ambient *= saturate(DistanceToObject(pos + normal * 3.2)*1.25*0.25);
ambient *= saturate(DistanceToObject(pos + normal * 6.4)*1.25*0.125);
//ambient = max(0.05, pow(ambient, 0.3)); // tone down ambient with a pow and min clamp it.
ambient = saturate(ambient);
// Trace a ray toward the sun for sun shadows
float sunShadow = 1.0;
float iter = 0.05;
for (int i = 0; i < 30; i++)
{
vec3 tempPos = pos + sunDir * iter;
//if (dot(tempPos, tempPos) > outerSphereRad*outerSphereRad+0.8) break;
if (iter > outerSphereRad + outerSphereRad) break;
float tempDist = DistanceToObject(tempPos);
sunShadow *= saturate(tempDist*50.0);
if (tempDist <= 0.0) break;
//iter *= 1.5; // constant is more reliable than distance-based???
iter += max(0.01, tempDist)*1.0;
}
sunShadow = saturate(sunShadow);
// calculate the reflection vector for highlights
vec3 ref = reflect(relVec, normal);
// make sure the texture gets rotated along with the geometry.
vec3 posTex = pos;
if (dot(pos, rotDir) > 1.0) posTex = pos * rotMat;
posTex = abs(posTex); // make texture symetric so it doesn't pop after rotation
// make a few frequencies of noise to give it some texture
float n =0.0;
n += noise(posTex*32.0);
n += noise(posTex*64.0);
n += noise(posTex*128.0);
n += noise(posTex*256.0);
n += noise(posTex*512.0);
n *= 0.8;
normal = normalize(normal + n*0.1);
// ------ Calculate texture color ------
vec3 texColor = vec3(0.95, 1.0, 1.0);
vec3 rust = vec3(0.65, 0.25, 0.1) - noise(posTex*128.0);
texColor *= smoothstep(texColor, rust, vec3(saturate(RustNoise3D(posTex*8.0))-0.2));
// make outer edge a little brighter
texColor += (1.0 - vec3(19.0, 5.0, 2.0) * length(normalU))*ambientS;
// apply noise
texColor *= vec3(1.0)*n*0.05;
texColor *= 0.7;
texColor = saturate(texColor);
// ------ Calculate lighting color ------
// Start with sun color, standard lighting equation, and shadow
vec3 lightColor = vec3(0.6) * saturate(dot(sunDir, normal)) * sunShadow;
// weighted average the near ambient occlusion with the far for just the right look
float ambientAvg = (ambient*3.0 + ambientS) * 0.25;
// a red and blue light coming from different directions
lightColor += (vec3(1.0, 0.2, 0.4) * saturate(-normal.z *0.5+0.5))*pow(ambientAvg, 0.5);
lightColor += (vec3(0.1, 0.5, 0.99) * saturate(normal.y *0.5+0.5))*pow(ambientAvg, 0.5);
// blue glow light coming from the glow in the middle of the sphere
lightColor += vec3(0.3, 0.5, 0.9) * saturate(dot(-pos, normal))*pow(ambientS, 0.3);
// lightColor *= ambient;
lightColor *= 4.0;
// finally, apply the light to the texture.
finalColor = texColor * lightColor;
// sun reflection to make it look metal
finalColor += vec3(1.0)*pow(n,4.0)* GetSunColorSmall(ref, sunDir) * sunShadow;// * ambientS;
// fog that fades to reddish plus the sun color so that fog is brightest towards sun
//finalColor = mix(vec3(1.0, 0.41, 0.41)*skyMultiplier + min(vec3(0.25),GetSunColorSmall(relVec, sunDir))*2.0*sunSet, finalColor, exp(-t*0.003));
// pulse the outer edge color when something is about to rotate
if (dot(pos, rotDir) > 1.0) finalColor += vec3(0.2, 1.4, 0.8)*pulse*saturate(0.000001 / pow(abs(length(pos)-outerSphereRad), 2.0))*2.0;
}
else
{
// Our ray trace hit nothing, so draw sky.
//finalColor = saturate(GetSunColorSmall(relVec, sunDir)*0.95-0.01);
}
// add the ray marching glow
finalColor += vec3(0.3, 0.5, 0.9) * glow;
// vignette?
finalColor *= vec3(1.0) * saturate(1.0 - length(uv/2.5));
finalColor *= 1.3;
// output the final color with sqrt for "gamma correction"
fragColor = vec4(sqrt(clamp(finalColor, 0.0, 1.0)),1.0);
}
| cc0-1.0 | [
4341,
4412,
4444,
4534,
6672
] | [
[
695,
714,
737,
737,
810
],
[
811,
811,
834,
834,
905
],
[
906,
906,
929,
929,
1015
],
[
1016,
1016,
1057,
1057,
1100
],
[
1138,
1138,
1162,
1162,
1435
],
[
1436,
1436,
1458,
1458,
2043
],
[
2198,
2198,
2221,
2221,
2250
],
[
2251,
2251,
2274,
2274,
2303
],
[
2304,
2304,
2329,
2329,
2358
],
[
2489,
2489,
2543,
2543,
2846
],
[
2847,
2847,
2896,
2896,
3150
],
[
3152,
3681,
3708,
3708,
4272
],
[
4341,
4412,
4444,
4534,
6672
],
[
6674,
6674,
6731,
6816,
16639
]
] | // This is the big money function that makes the crazy fractally shape
| float DistanceToObject(vec3 p)
{ |
if (dot(p, rotDir) > 1.0) p *= rotMat;
// Repeat our position so we can carve out many cylindrical-like things from our solid
vec3 rep = fract(p)-0.5;
//final = max(final, -(length(rep.xz*rep.xz)*1.0 - 0.0326));
float final = -(length(rep.xy*rep.xz) - 0.109);
final = max(final, -(length(rep.zy) - 0.33));
//final = max(final, -(length(rep.xz*rep.xz) - 0.03));
//final = max(final, -(length(rep.yz*rep.yz) - 0.03));
//final = max(final, -(length(rep.xy*rep.xy) - 0.030266));
// Repeat the process of carving things out for smaller scales
vec3 rep2 = fract(rep*2.0)-0.5;
final = max(final, -(length(rep2.xz)*0.5 - 0.125));
final = max(final, -(length(rep2.xy)*0.5 - 0.125));
final = max(final, -(length(rep2.zy)*0.5 - 0.125));
vec3 rep3 = fract(rep2*3.0)-0.5;
final = max(final, -(length(rep3.xz)*0.1667 - 0.25*0.1667));
final = max(final, -(length(rep3.xy)*0.1667 - 0.25*0.1667));
final = max(final, -(length(rep3.zy)*0.1667 - 0.25*0.1667));
#ifdef TOO_MUCH_FRACTAL
vec3 rep4 = fract(rep3*3.0)-0.5;
final = max(final, -(length(rep4.xz)*0.0555 - 0.25*0.0555));
final = max(final, -(length(rep4.xy)*0.0555 - 0.25*0.0555));
final = max(final, -(length(rep4.yz)*0.0555 - 0.25*0.0555));
vec3 rep5 = fract(rep4*3.0)-0.5;
final = max(final, -(length(rep5.xz)*0.0185 - 0.25*0.0185));
final = max(final, -(length(rep5.xy)*0.0185 - 0.25*0.0185));
final = max(final, -(length(rep5.yz)*0.0185 - 0.25*0.0185));
#endif
// Cut out stuff outside of outer sphere
final = max(final, (length(p) - outerSphereRad));
// Carve out inner sphere
final = max(final, -(length(p) - 2.8));
//final = max(final, abs(p.x) - 2.0); // for that space station look
//final = (length(p) - outerSphereRad); // for debugging texture and lighting
// Slice the object in a 3d grid so it can rotate like a rubik's cube
float slice = 0.02;
vec3 grid = -abs(fract(p.xyz)) + slice;
final = max(final, grid.x);
final = max(final, grid.y);
final = max(final, grid.z);
//final = min(final, abs(p.y));
return final;
} | // This is the big money function that makes the crazy fractally shape
float DistanceToObject(vec3 p)
{ | 1 | 6 |
End of preview. Expand
in Dataset Viewer.
experiements
repository: https://github.com/Vipitis/shadertoys-dataset
not meant to be used by anyone else just yet - not guarantee to keep this public or even static.... but I need to run a lot of experiemtns on different machines so it's public for a bit.
revisions
v0.3 contains 394 functions gathers from shaders20k (years 2013-2021) and the Shadertoy.com API (years 2022-2023). All programs went through a assemble of filters. Then parsed to functions and additional filters were applied.
All functions are run with wgpu-shadertoy using the wgpu-py PR branch that updates to wgpu-native 22.1 ref, and then confirmed to be needed (it errors when the funciton is omitted).
the columns model_inp
contains the comment just before the function as well as the header.
- Downloads last month
- 214
Size of downloaded dataset files:
719 kB
Size of the auto-converted Parquet files:
719 kB
Number of rows:
467