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
tsBfRD
iq
2020-05-16T00:31:58
// The MIT License // Copyright © 2017 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. // Analytical computation of the exact bounding box for a cubic bezier segment // // See http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm // Other bounding box functions: // // Disk - 3D BBox : https://www.shadertoy.com/view/ll3Xzf // Cylinder - 3D BBox : https://www.shadertoy.com/view/MtcXRf // Ellipse - 3D BBox : https://www.shadertoy.com/view/Xtjczw // Cone boundong - 3D BBox : https://www.shadertoy.com/view/WdjSRK // Cubic Bezier - 2D BBox : https://www.shadertoy.com/view/XdVBWd // Quadratic Bezier - 3D BBox : https://www.shadertoy.com/view/ldj3Wh // Quadratic Bezier - 2D BBox : https://www.shadertoy.com/view/lsyfWc #define AA 3 struct bound3 { vec3 mMin; vec3 mMax; }; //--------------------------------------------------------------------------------------- // bounding box for a bezier (http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm) //--------------------------------------------------------------------------------------- bound3 BezierAABB( in vec3 p0, in vec3 p1, in vec3 p2 ) { // extremes vec3 mi = min(p0,p2); vec3 ma = max(p0,p2); // p = (1-t)^2*p0 + 2(1-t)t*p1 + t^2*p2 // dp/dt = 2(t-1)*p0 + 2(1-2t)*p1 + 2t*p2 = t*(2*p0-4*p1+2*p2) + 2*(p1-p0) // dp/dt = 0 -> t*(p0-2*p1+p2) = (p0-p1); vec3 t = clamp((p0-p1)/(p0-2.0*p1+p2),0.0,1.0); vec3 s = 1.0 - t; vec3 q = s*s*p0 + 2.0*s*t*p1 + t*t*p2; mi = min(mi,q); ma = max(ma,q); return bound3( mi, ma ); } // ray-ellipse intersection float iEllipse( in vec3 ro, in vec3 rd, // ray: origin, direction in vec3 c, in vec3 u, in vec3 v ) // disk: center, 1st axis, 2nd axis { vec3 q = ro - c; vec3 r = vec3( dot( cross(u,v), q ), dot( cross(q,u), rd ), dot( cross(v,q), rd ) ) / dot( cross(v,u), rd ); return (dot(r.yz,r.yz)<1.0) ? r.x : -1.0; } // ray-box intersection (simplified) vec2 iBox( in vec3 ro, in vec3 rd, in vec3 cen, in vec3 rad ) { // ray-box intersection in box space vec3 m = 1.0/rd; vec3 n = m*(ro-cen); vec3 k = abs(m)*rad; vec3 t1 = -n - k; vec3 t2 = -n + k; float tN = max( max( t1.x, t1.y ), t1.z ); float tF = min( min( t2.x, t2.y ), t2.z ); if( tN > tF || tF < 0.0) return vec2(-1.0); return vec2( tN, tF ); } float length2( in vec3 v ) { return dot(v,v); } vec3 iSegment( in vec3 ro, in vec3 rd, in vec3 a, in vec3 b ) { vec3 ba = b - a; vec3 oa = ro - a; float oad = dot( oa, rd ); float dba = dot( rd, ba ); float baba = dot( ba, ba ); float oaba = dot( oa, ba ); vec2 th = vec2( -oad*baba + dba*oaba, oaba - oad*dba ) / (baba - dba*dba); th.x = max( th.x, 0.0 ); th.y = clamp( th.y, 0.0, 1.0 ); vec3 p = a + ba*th.y; vec3 q = ro + rd*th.x; return vec3( th, length2( p-q ) ); } float iBezier( in vec3 ro, in vec3 rd, in vec3 p0, in vec3 p1, in vec3 p2, in float width) { const int kNum = 50; float hit = -1.0; float res = 1e10; vec3 a = p0; for( int i=1; i<kNum; i++ ) { float t = float(i)/float(kNum-1); vec3 b = mix(mix(p0,p1,t),mix(p1,p2,t),t); vec3 r = iSegment( ro, rd, a, b ); if( r.z<width*width ) { res = min( res, r.x ); hit = 1.0; } a = b; } return res*hit; } float hash1( in vec2 p ) { return fract(sin(dot(p, vec2(12.9898, 78.233)))*43758.5453); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y; #else vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y; #endif // camera position vec3 ro = vec3( -0.5, 0.4, 1.5 ); 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)); // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // bezier animation float time = iTime*0.5; vec3 p0 = vec3(0.8,0.6,0.8)*sin( time*0.7 + vec3(3.0,1.0,2.0) ); vec3 p1 = vec3(0.8,0.6,0.8)*sin( time*1.1 + vec3(0.0,6.0,1.0) ); vec3 p2 = vec3(0.8,0.6,0.8)*sin( time*1.3 + vec3(4.0,2.0,3.0) ); float thickness = 0.01; // render vec3 col = vec3(0.4)*(1.0-0.3*length(p)); // raytrace bezier float t = iBezier( ro, rd, p0, p1, p2, thickness); float tmin = 1e10; if( t>0.0 ) { tmin = t; col = vec3(1.0,0.75,0.3); } // compute bounding box for bezier bound3 bbox = BezierAABB( p0, p1, p2 ); bbox.mMin -= thickness; bbox.mMax += thickness; // raytrace bounding box vec3 bcen = 0.5*(bbox.mMin+bbox.mMax); vec3 brad = 0.5*(bbox.mMax-bbox.mMin); vec2 tbox = iBox( ro, rd, bcen, brad ); if( tbox.x>0.0 ) { // back face if( tbox.y < tmin ) { vec3 pos = ro + rd*tbox.y; vec3 e = smoothstep( brad-0.03, brad-0.02, abs(pos-bcen) ); float al = 1.0 - (1.0-e.x*e.y)*(1.0-e.y*e.z)*(1.0-e.z*e.x); col = mix( col, vec3(0.0), 0.25 + 0.75*al ); } // front face if( tbox.x < tmin ) { vec3 pos = ro + rd*tbox.x; vec3 e = smoothstep( brad-0.03, brad-0.02, abs(pos-bcen) ); float al = 1.0 - (1.0-e.x*e.y)*(1.0-e.y*e.z)*(1.0-e.z*e.x); col = mix( col, vec3(0.0), 0.15 + 0.85*al ); } } tot += col; #if AA>1 } tot /= float(AA*AA); #endif // dithering tot += ((hash1(fragCoord.xy)+hash1(fragCoord.yx+13.1))/2.0-0.5)/256.0; fragColor = vec4( tot, 1.0 ); }
mit
[ 1825, 2097, 2154, 2170, 2587 ]
[ [ 1825, 2097, 2154, 2170, 2587 ], [ 2590, 2618, 2777, 2777, 2979 ], [ 2982, 3019, 3083, 3121, 3401 ], [ 3403, 3403, 3431, 3431, 3450 ], [ 3452, 3452, 3515, 3515, 3906 ], [ 3909, 3909, 4001, 4001, 4432 ], [ 4434, 4434, 4460, 4460, 4527 ] ]
//--------------------------------------------------------------------------------------- // bounding box for a bezier (http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm) //---------------------------------------------------------------------------------------
bound3 BezierAABB( in vec3 p0, in vec3 p1, in vec3 p2 ) {
vec3 mi = min(p0,p2); vec3 ma = max(p0,p2); // p = (1-t)^2*p0 + 2(1-t)t*p1 + t^2*p2 // dp/dt = 2(t-1)*p0 + 2(1-2t)*p1 + 2t*p2 = t*(2*p0-4*p1+2*p2) + 2*(p1-p0) // dp/dt = 0 -> t*(p0-2*p1+p2) = (p0-p1); vec3 t = clamp((p0-p1)/(p0-2.0*p1+p2),0.0,1.0); vec3 s = 1.0 - t; vec3 q = s*s*p0 + 2.0*s*t*p1 + t*t*p2; mi = min(mi,q); ma = max(ma,q); return bound3( mi, ma ); }
//--------------------------------------------------------------------------------------- // bounding box for a bezier (http://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm) //--------------------------------------------------------------------------------------- bound3 BezierAABB( in vec3 p0, in vec3 p1, in vec3 p2 ) {
1
1
tsjfRw
blackle
2020-05-17T22:24:19
//CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/ //To the extent possible under law, Blackle Mori has waived all copyright and related or neighboring rights to this work. //antialising #define AA_SAMPLES 1 //percentage of domains filled #define DENSITY 0.35 //returns a vector pointing in the direction of the closest neighbouring cell vec3 quadrant(vec3 p) { vec3 ap = abs(p); if (ap.x >= max(ap.y, ap.z)) return vec3(sign(p.x),0.,0.); if (ap.y >= max(ap.x, ap.z)) return vec3(0.,sign(p.y),0.); if (ap.z >= max(ap.x, ap.y)) return vec3(0.,0.,sign(p.z)); return vec3(0); } float hash(float a, float b) { return fract(sin(a*1.2664745 + b*.9560333 + 3.) * 14958.5453); } bool domain_enabled(vec3 id) { //repeat random number along z axis so every active cell has at least one active neighbour id.z = floor(id.z/2.); return hash(id.x, hash(id.y, id.z)) < DENSITY; } float linedist(vec3 p, vec3 a, vec3 b) { float k = dot(p-a,b-a)/dot(b-a,b-a); return distance(p, mix(a,b,clamp(k,0.,1.))); } float ball; float scene(vec3 p) { float scale = 5.; vec3 id = floor(p/scale); p = (fract(p/scale)-.5)*scale; if (!domain_enabled(id)) { //return distance to sphere in adjacent domain p = abs(p); if (p.x > p.y) p.xy = p.yx; if (p.y > p.z) p.yz = p.zy; if (p.x > p.y) p.xy = p.yx; p.z -= scale; return length(p)-1.; } float dist = length(p)-1.; ball = dist; vec3 quad = quadrant(p); if (domain_enabled(id+quad)) { //add pipe dist = min(dist, linedist(p, vec3(0), quad*scale)-.2); } return dist; } vec3 norm(vec3 p) { mat3 k = mat3(p,p,p)-mat3(0.01); return normalize(scene(p) - vec3( scene(k[0]),scene(k[1]),scene(k[2]) )); } vec3 erot(vec3 p, vec3 ax, float ro) { return mix(dot(ax,p)*ax, p, cos(ro)) + sin(ro)*cross(ax,p); } vec3 srgb(float r, float g, float b) { return vec3(r*r,g*g,b*b); } float smoothstairs(float p, float scale) { p *= scale; p = smoothstep(0.9, 1., fract(p)) + floor(p); return p/scale; } const float PI = acos(-1.); vec3 pixel_color(vec2 uv) { vec2 mouse = (iMouse.xy-0.5*iResolution.xy)/iResolution.y; vec3 cam = normalize(vec3(1,uv)); vec3 init = vec3(iTime,0,0); float yrot = 0.; float zrot = 0.; if (iMouse.z > 0.) { yrot += smoothstep(-PI/2., PI/2., -4.*mouse.y)*PI-PI/2.; zrot += 4.*mouse.x; } else { yrot += cos(iTime*.2)*.6; zrot += sin(iTime*.2)*.6; } cam = erot(cam, vec3(0,1,0), yrot); cam = erot(cam, vec3(0,0,1), zrot); vec3 p = init; bool hit = false; bool triggered = false; bool outline = false; bool type = false; float dist; //ray marching for (int i = 0; i < 150 && !hit; i++) { dist = scene(p); float outline_radius = 0.1*sqrt(distance(p,init))/3.; if (dist < outline_radius*.9 && !triggered) { triggered = true; type = dist == ball; } if (triggered) { float line = (outline_radius-dist); outline = line < dist || type != (dist == ball); dist = min(line, dist); } hit = dist*dist < 1e-6; p+=dist*cam; if (distance(p,init)>90.) break; } if (!hit) return vec3(0.4); bool is_ball = dist == ball; vec3 n = norm(p); vec3 r = reflect(cam, n); //add outline to sharp edges outline = outline || scene(p+n*.1) < 0.09; float fog = smoothstep(80.,60., distance(p,init)); //shading float ao = smoothstep(.0, .5, scene(p+n*.5)); float fact = ao*length(sin(r*vec3(3.,-2.,2.))*.5+.5)/sqrt(3.); float lod = smoothstep(90.,50.,distance(p,init))*5.; //make the shading simpler in the distance fact = smoothstairs(fact, lod)+.1; vec3 ballcol = abs(erot(srgb(0.6,0.7,0.8), normalize(cos(p*.5)), .3)); vec3 matcol = is_ball ? ballcol : srgb(0.6,0.65,0.7); vec3 col = matcol*fact + mix(vec3(1), matcol, .4)*pow(fact, 10.)*1.5; col *= smoothstep(0.,.25,abs(dot(cam, n))); col = mix(vec3(.6), outline ? vec3(0.) : col, fog); if (isnan(length(col))) return vec3(.6); //i have no idea where this nan is coming from return col; } vec2 weyl_2d(int n) { return fract(vec2(n*12664745, n*9560333)/exp2(24.)); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (fragCoord-.5*iResolution.xy)/iResolution.y; fragColor = vec4(0); for (int i = 0; i < AA_SAMPLES; i++) { vec2 uv2 = uv + weyl_2d(i)/iResolution.y*1.25; fragColor += vec4(pixel_color(uv2), 1.); } fragColor.xyz = sqrt(fragColor.xyz/fragColor.w); }
cc0-1.0
[ 282, 360, 383, 383, 616 ]
[ [ 282, 360, 383, 383, 616 ], [ 618, 618, 648, 648, 717 ], [ 719, 719, 749, 844, 925 ], [ 927, 927, 967, 967, 1059 ], [ 1073, 1073, 1094, 1094, 1671 ], [ 1673, 1673, 1692, 1692, 1809 ], [ 1811, 1811, 1849, 1849, 1915 ], [ 1917, 1917, 1955, 1955, 1987 ], [ 1989, 1989, 2031, 2031, 2119 ], [ 2149, 2149, 2176, 2176, 4269 ], [ 4271, 4271, 4292, 4292, 4351 ], [ 4353, 4353, 4410, 4410, 4699 ] ]
//returns a vector pointing in the direction of the closest neighbouring cell
vec3 quadrant(vec3 p) {
vec3 ap = abs(p); if (ap.x >= max(ap.y, ap.z)) return vec3(sign(p.x),0.,0.); if (ap.y >= max(ap.x, ap.z)) return vec3(0.,sign(p.y),0.); if (ap.z >= max(ap.x, ap.y)) return vec3(0.,0.,sign(p.z)); return vec3(0); }
//returns a vector pointing in the direction of the closest neighbouring cell vec3 quadrant(vec3 p) {
2
2
WsBBRw
blackle
2020-05-16T20:41:42
//CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/ //To the extent possible under law, Blackle Mori has waived all copyright and related or neighboring rights to this work. //this is the technique in https://www.shadertoy.com/view/td2fRD //but a 3d SDF mapping to a 2d sdf, instead of a 4d to a 3d. //should be easier to understand. #define FK(k) floatBitsToInt(k)^floatBitsToInt(cos(k)) float hash(float a, float b) { int x = FK(a); int y = FK(b); return float((x*x-y)*(y*y+x)+x)/2.14e9; } //return the SDF for a sphere, or the SDF for an empty region surrounded by spheres float gated_domain(vec3 p, float scale, bool gated) { if (!gated) { p.xy = abs(p.xy); if (p.x > p.y) p.xy = p.yx; p.y -= 1./scale; } return length(p)-.2; } float scene3d(vec3 p) { float scale = 2.; vec2 id = floor(p.xy*scale); p.xy = (fract(p.xy*scale)-0.5)/scale; bool gated = hash(id.x, id.y) > 0.; return gated_domain(p, scale, gated); } vec3 erot(vec3 p, vec3 ax, float ro) { return mix(dot(ax,p)*ax,p,cos(ro)) + sin(ro)*cross(ax,p); } int pittingtype; float scene2d(vec2 p) { float circle = length(p)-1.; float top = circle; float last = circle; for (int i = 0; i < 5; i++) { float scale = 1./float(i+1); //map 3d coordinates to 4d using the distance to the SDF vec3 p3d = vec3(p, last)/scale; //cut out mapped spheres from SDF float holes = scene3d(p3d)*scale; top = max(top, -holes); if (pittingtype == 0) last = holes; //add pitting to existing pits if (pittingtype == 1) last = top; //add pitting everywhere if (pittingtype == 2) last = circle; //add pitting only to original surface } return top; } vec3 norm(vec3 p) { mat3 k = mat3(p,p,p)-mat3(0.001); return normalize(scene3d(p)-vec3(scene3d(k[0]),scene3d(k[1]),scene3d(k[2]))); } vec3 render3d(vec2 uv) { vec3 cam = normalize(vec3(2,uv)); vec3 init = vec3(-5,0,2); cam = erot(cam,vec3(0,1,0), .3); cam = erot(cam,vec3(0,0,1), iTime*.1); vec3 p = init; bool hit = false; //raymarch for (int i = 0; i < 100 && !hit; i++) { float dist = scene3d(p); hit = dist*dist < 1e-6; p+=cam*dist*.9; if (distance(p,init) > 100.) break; } //shading vec3 n = norm(p); return hit ? sin(n)*.5+.5 : vec3(0.1); } vec3 shadeDistance(float d) { float dist = d*150.0; float banding = max(sin(dist), 0.0); float strength = sqrt(1.-exp(-abs(d)*2.)); float pattern = mix(strength, banding, (0.6-abs(strength-0.5))*0.3); vec3 color = vec3(pattern); color *= d > 0.0 ? vec3(1.0,0.56,0.4) : vec3(0.4,0.9,1.0); return color; } float antialias(float x) { float pixelsize = 3./iResolution.y; return smoothstep(-pixelsize, pixelsize, x); } vec3 render2d(vec2 uv, vec2 mouse) { uv *= 3.; mouse *= 3.; vec3 col = shadeDistance(scene2d(uv)); float mousedist = scene2d(mouse); if (iMouse.z > 0.) { col *= antialias(distance(mouse, uv) - abs(mousedist))*0.5+0.5; } return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (fragCoord-.5*iResolution.xy)/iResolution.y; vec2 mouse = (iMouse.xy-.5*iResolution.xy)/iResolution.y; pittingtype = int(iTime)%3; if (uv.x > .0) { fragColor.xyz = render3d(uv-vec2(0.45,0.)); } else { fragColor.xyz = render2d(uv+vec2(0.45,0.), mouse+vec2(0.45,0.)); } }
cc0-1.0
[ 522, 606, 659, 659, 797 ]
[ [ 410, 410, 440, 440, 520 ], [ 522, 606, 659, 659, 797 ], [ 799, 799, 822, 822, 1003 ], [ 1005, 1005, 1043, 1043, 1107 ], [ 1126, 1126, 1149, 1149, 1763 ], [ 1765, 1765, 1784, 1784, 1906 ], [ 1908, 1908, 1932, 1932, 2416 ], [ 2418, 2418, 2447, 2447, 2755 ], [ 2757, 2757, 2783, 2783, 2874 ], [ 2876, 2876, 2912, 2912, 3146 ], [ 3148, 3148, 3205, 3205, 3530 ] ]
//return the SDF for a sphere, or the SDF for an empty region surrounded by spheres
float gated_domain(vec3 p, float scale, bool gated) {
if (!gated) { p.xy = abs(p.xy); if (p.x > p.y) p.xy = p.yx; p.y -= 1./scale; } return length(p)-.2; }
//return the SDF for a sphere, or the SDF for an empty region surrounded by spheres float gated_domain(vec3 p, float scale, bool gated) {
1
1
3lByRK
blackle
2020-07-14T20:22:45
//CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/ //To the extent possible under law, Blackle Mori has waived all copyright and related or neighboring rights to this work. //shitty way to prevent division by zero. if b is zero, add a little bit to it. vec3 div(vec3 a, vec3 b) { b += vec3(equal(b,vec3(0)))*.01; return a/b; } float antiderivative(float x, vec3 origin, vec3 dir) { //antiderivative for pow(dot(sin(origin + x*dir), vec3(1), 2.); mat3 A = mat3(1,1,0,-1,0,1,0,-1,-1); mat3 B = mat3(1,1,0,1,0,1,0,1,1); vec3 Q = origin + dir*x; vec3 integral = div(sin(A*Q),(A*dir)) - div(sin(B*Q),(B*dir)) + div((2.*Q-sin(2.*Q)),(4.*dir)); return dot(integral, vec3(1)); } float lineintegral(vec3 a, vec3 b) { float len = distance(a, b); vec3 dir = (b-a)/len; return antiderivative(len,a,dir) - antiderivative(0.,a,dir); } float scene(vec3 p) { p = asin(sin(p+1.)); return length(p)-1.; } vec3 erot(vec3 p, vec3 ax, float ro) { return mix(dot(ax,p)*ax,p,cos(ro))+sin(ro)*cross(ax,p); } #define FK(k) floatBitsToInt(k*k/7.)^floatBitsToInt(k) float hash(float a, float b) { int x = FK(a), y = FK(b); return float((x*x-y)*(y*y+x)-x)/2.14e9; } vec3 hash3(float a, float b) { float s1 = hash(a, b); float s2 = hash(s1, b); float s3 = hash(s2, b); return vec3(s1,s2,s3); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (fragCoord-.5*iResolution.xy)/iResolution.y; vec3 cam = normalize(vec3(1,uv)); vec3 init = vec3(-4,0,0); cam = erot(cam, vec3(0,1,0), .2); init = erot(init, vec3(0,1,0), .2); cam = erot(cam, vec3(0,0,1), iTime/5.); init = erot(init, vec3(0,0,1), iTime/5.); init.x += iTime; vec3 p = init; bool hit = false; for (int i = 0; i < 100 && !hit; i++) { float dist = scene(p); hit = dist*dist < 1e-6; p += dist * cam; if (distance(p,init)>50.) break; } vec3 a = p; vec3 b = init; float scale = 1.; float fog = lineintegral(a,b)/20.; if (uv.x > 0.) { //sum up multiple different versions of the fog for (int i = 0; i < 50; i++) { //random rotation vec3 ax = normalize(tan(hash3(float(i),14353.))); float ro = hash(float(i),66123.)*10.; a = erot(a,ax,ro); b = erot(b,ax,ro); fog += lineintegral(a*scale,b*scale)/sqrt(scale); scale *= 1.06; } fog /= 2500.; } fragColor = sqrt(vec4(fog)); }
cc0-1.0
[ 194, 274, 300, 300, 355 ]
[ [ 194, 274, 300, 300, 355 ], [ 357, 357, 411, 479, 724 ], [ 726, 726, 762, 762, 887 ], [ 889, 889, 910, 910, 962 ], [ 964, 964, 1002, 1002, 1064 ], [ 1121, 1121, 1151, 1151, 1227 ], [ 1229, 1229, 1259, 1259, 1371 ], [ 1373, 1373, 1430, 1430, 2504 ] ]
//shitty way to prevent division by zero. if b is zero, add a little bit to it.
vec3 div(vec3 a, vec3 b) {
b += vec3(equal(b,vec3(0)))*.01; return a/b; }
//shitty way to prevent division by zero. if b is zero, add a little bit to it. vec3 div(vec3 a, vec3 b) {
1
1
WtScDt
iq
2020-07-27T06:52:31
// The MIT License // Copyright © 2020 Inigo Quilez // https://www.youtube.com/c/InigoQuilez // https://iquilezles.org/ // 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 simple way to prevent aliasing of cosine functions (the color // palette in this case is made of 8 layers) by attenuating them // when their oscillations become smaller than a pixel. Left is // direct use of cos(x), right is band-limited cos(x). // // Box-filtering of cos(x): // // (1/w)∫cos(t)dt with t ∈ (x-½w, x+½w) // = [sin(x+½w) - sin(x-½w)]/w // = cos(x)·sin(½w)/(½w) // // Can approximate smoothstep(2π,0,w) ≈ sin(w/2)/(w/2), // which you can also see as attenuating cos(x) when it // oscilates more than once per pixel. More info: // // https://iquilezles.org/www/articles/bandlimiting/bandlimiting.htm // // Related Shader: // https://www.shadertoy.com/view/WtScDt // https://www.shadertoy.com/view/wtXfRH // https://www.shadertoy.com/view/3tScWd // box-filted cos(x) vec3 fcos( in vec3 x ) { vec3 w = fwidth(x); #if 1 return cos(x) * sin(0.5*w)/(0.5*w); // exact #else return cos(x) * smoothstep(6.2832,0.0,w); // approx #endif } // pick raw cosine, or band-limited cosine bool mode = false; vec3 mcos( vec3 x){return mode?cos(x):fcos(x);} // color palette, made of 8 cos functions // (see https://iquilezles.org/www/articles/palettes/palettes.htm) vec3 getColor( in float t ) { vec3 col = vec3(0.6,0.5,0.4); col += 0.14*mcos(6.2832*t* 1.0+vec3(0.0,0.5,0.6)); col += 0.13*mcos(6.2832*t* 3.1+vec3(0.5,0.6,1.0)); col += 0.12*mcos(6.2832*t* 5.1+vec3(0.1,0.7,1.1)); col += 0.11*mcos(6.2832*t* 9.1+vec3(0.1,0.5,1.2)); col += 0.10*mcos(6.2832*t* 17.1+vec3(0.0,0.3,0.9)); col += 0.09*mcos(6.2832*t* 31.1+vec3(0.1,0.5,1.3)); col += 0.08*mcos(6.2832*t* 65.1+vec3(0.1,0.5,1.3)); col += 0.07*mcos(6.2832*t*131.1+vec3(0.3,0.2,0.8)); return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord ) { // coordiantes vec2 q = (2.0*fragCoord-iResolution.xy)/iResolution.y; // separation float th = (iMouse.z>0.001) ? (2.0*iMouse.x-iResolution.x)/iResolution.y : 1.8*sin(iTime); mode = (q.x<th); // deformation vec2 p = 2.0*q/dot(q,q); // animation p.xy += 0.05*iTime; // texture vec3 col = min(getColor(p.x),getColor(p.y)); // vignetting col *= 1.5 - 0.2*length(q); // separation col *= smoothstep(0.005,0.010,abs(q.x-th)); // palette if( q.y<-0.9 ) col = getColor( fragCoord.x/iResolution.x ); fragColor = vec4( col, 1.0 ); }
mit
[ 1932, 1953, 1977, 1977, 2140 ]
[ [ 1932, 1953, 1977, 1977, 2140 ], [ 2205, 2205, 2225, 2225, 2253 ], [ 2255, 2364, 2393, 2393, 2893 ], [ 2895, 2895, 2951, 2970, 3564 ] ]
// box-filted cos(x)
vec3 fcos( in vec3 x ) {
vec3 w = fwidth(x); #if 1 return cos(x) * sin(0.5*w)/(0.5*w); // exact #else return cos(x) * smoothstep(6.2832,0.0,w); // approx #endif }
// box-filted cos(x) vec3 fcos( in vec3 x ) {
1
4
WtScDt
iq
2020-07-27T06:52:31
// The MIT License // Copyright © 2020 Inigo Quilez // https://www.youtube.com/c/InigoQuilez // https://iquilezles.org/ // 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 simple way to prevent aliasing of cosine functions (the color // palette in this case is made of 8 layers) by attenuating them // when their oscillations become smaller than a pixel. Left is // direct use of cos(x), right is band-limited cos(x). // // Box-filtering of cos(x): // // (1/w)∫cos(t)dt with t ∈ (x-½w, x+½w) // = [sin(x+½w) - sin(x-½w)]/w // = cos(x)·sin(½w)/(½w) // // Can approximate smoothstep(2π,0,w) ≈ sin(w/2)/(w/2), // which you can also see as attenuating cos(x) when it // oscilates more than once per pixel. More info: // // https://iquilezles.org/www/articles/bandlimiting/bandlimiting.htm // // Related Shader: // https://www.shadertoy.com/view/WtScDt // https://www.shadertoy.com/view/wtXfRH // https://www.shadertoy.com/view/3tScWd // box-filted cos(x) vec3 fcos( in vec3 x ) { vec3 w = fwidth(x); #if 1 return cos(x) * sin(0.5*w)/(0.5*w); // exact #else return cos(x) * smoothstep(6.2832,0.0,w); // approx #endif } // pick raw cosine, or band-limited cosine bool mode = false; vec3 mcos( vec3 x){return mode?cos(x):fcos(x);} // color palette, made of 8 cos functions // (see https://iquilezles.org/www/articles/palettes/palettes.htm) vec3 getColor( in float t ) { vec3 col = vec3(0.6,0.5,0.4); col += 0.14*mcos(6.2832*t* 1.0+vec3(0.0,0.5,0.6)); col += 0.13*mcos(6.2832*t* 3.1+vec3(0.5,0.6,1.0)); col += 0.12*mcos(6.2832*t* 5.1+vec3(0.1,0.7,1.1)); col += 0.11*mcos(6.2832*t* 9.1+vec3(0.1,0.5,1.2)); col += 0.10*mcos(6.2832*t* 17.1+vec3(0.0,0.3,0.9)); col += 0.09*mcos(6.2832*t* 31.1+vec3(0.1,0.5,1.3)); col += 0.08*mcos(6.2832*t* 65.1+vec3(0.1,0.5,1.3)); col += 0.07*mcos(6.2832*t*131.1+vec3(0.3,0.2,0.8)); return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord ) { // coordiantes vec2 q = (2.0*fragCoord-iResolution.xy)/iResolution.y; // separation float th = (iMouse.z>0.001) ? (2.0*iMouse.x-iResolution.x)/iResolution.y : 1.8*sin(iTime); mode = (q.x<th); // deformation vec2 p = 2.0*q/dot(q,q); // animation p.xy += 0.05*iTime; // texture vec3 col = min(getColor(p.x),getColor(p.y)); // vignetting col *= 1.5 - 0.2*length(q); // separation col *= smoothstep(0.005,0.010,abs(q.x-th)); // palette if( q.y<-0.9 ) col = getColor( fragCoord.x/iResolution.x ); fragColor = vec4( col, 1.0 ); }
mit
[ 2255, 2364, 2393, 2393, 2893 ]
[ [ 1932, 1953, 1977, 1977, 2140 ], [ 2205, 2205, 2225, 2225, 2253 ], [ 2255, 2364, 2393, 2393, 2893 ], [ 2895, 2895, 2951, 2970, 3564 ] ]
// color palette, made of 8 cos functions // (see https://iquilezles.org/www/articles/palettes/palettes.htm)
vec3 getColor( in float t ) {
vec3 col = vec3(0.6,0.5,0.4); col += 0.14*mcos(6.2832*t* 1.0+vec3(0.0,0.5,0.6)); col += 0.13*mcos(6.2832*t* 3.1+vec3(0.5,0.6,1.0)); col += 0.12*mcos(6.2832*t* 5.1+vec3(0.1,0.7,1.1)); col += 0.11*mcos(6.2832*t* 9.1+vec3(0.1,0.5,1.2)); col += 0.10*mcos(6.2832*t* 17.1+vec3(0.0,0.3,0.9)); col += 0.09*mcos(6.2832*t* 31.1+vec3(0.1,0.5,1.3)); col += 0.08*mcos(6.2832*t* 65.1+vec3(0.1,0.5,1.3)); col += 0.07*mcos(6.2832*t*131.1+vec3(0.3,0.2,0.8)); return col; }
// color palette, made of 8 cos functions // (see https://iquilezles.org/www/articles/palettes/palettes.htm) vec3 getColor( in float t ) {
1
8
wtsBzS
mrange
2020-08-10T18:35:18
// License CC0: Double Ended Truchet Experiment // Been looking at some double ended truchets by BigWings and Shane. // After some experiments I got something I felt was interesting enough to share. #define TIME iTime #define RESOLUTION iResolution #define PI 3.141592654 #define TAU (2.0*PI) const vec2 coords[8] = vec2[8]( 0.5*vec2(-1.0, -0.5), 0.5*vec2(-1.0, +0.5), 0.5*vec2(-0.5, +1.0), 0.5*vec2(+0.5, +1.0), 0.5*vec2(+1.0, +0.5), 0.5*vec2(+1.0, -0.5), 0.5*vec2(+0.5, -1.0), 0.5*vec2(-0.5, -1.0) ); const vec2 dcoords[8] = vec2[8]( vec2(+1.0, +0.0), vec2(+1.0, +0.0), vec2(+0.0, -1.0), vec2(+0.0, -1.0), vec2(-1.0, +0.0), vec2(-1.0, +0.0), vec2(+0.0, +1.0), vec2(+0.0, +1.0) ); const int noCorners = 105; // Using symmetries and reflections should be possible to reduce this // array alot, but that is hard ;) const int corners[105*8] = int[105*8]( 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 6, 5, 7, 0, 1, 2, 3, 4, 7, 5, 6, 0, 1, 2, 4, 3, 5, 6, 7, 0, 1, 2, 4, 3, 6, 5, 7, 0, 1, 2, 4, 3, 7, 5, 6, 0, 1, 2, 5, 3, 4, 6, 7, 0, 1, 2, 5, 3, 6, 4, 7, 0, 1, 2, 5, 3, 7, 4, 6, 0, 1, 2, 6, 3, 4, 5, 7, 0, 1, 2, 6, 3, 5, 4, 7, 0, 1, 2, 6, 3, 7, 4, 5, 0, 1, 2, 7, 3, 4, 5, 6, 0, 1, 2, 7, 3, 5, 4, 6, 0, 1, 2, 7, 3, 6, 4, 5, 0, 2, 1, 3, 4, 5, 6, 7, 0, 2, 1, 3, 4, 6, 5, 7, 0, 2, 1, 3, 4, 7, 5, 6, 0, 2, 1, 4, 3, 5, 6, 7, 0, 2, 1, 4, 3, 6, 5, 7, 0, 2, 1, 4, 3, 7, 5, 6, 0, 2, 1, 5, 3, 4, 6, 7, 0, 2, 1, 5, 3, 6, 4, 7, 0, 2, 1, 5, 3, 7, 4, 6, 0, 2, 1, 6, 3, 4, 5, 7, 0, 2, 1, 6, 3, 5, 4, 7, 0, 2, 1, 6, 3, 7, 4, 5, 0, 2, 1, 7, 3, 4, 5, 6, 0, 2, 1, 7, 3, 5, 4, 6, 0, 2, 1, 7, 3, 6, 4, 5, 0, 3, 1, 2, 4, 5, 6, 7, 0, 3, 1, 2, 4, 6, 5, 7, 0, 3, 1, 2, 4, 7, 5, 6, 0, 3, 1, 4, 2, 5, 6, 7, 0, 3, 1, 4, 2, 6, 5, 7, 0, 3, 1, 4, 2, 7, 5, 6, 0, 3, 1, 5, 2, 4, 6, 7, 0, 3, 1, 5, 2, 6, 4, 7, 0, 3, 1, 5, 2, 7, 4, 6, 0, 3, 1, 6, 2, 4, 5, 7, 0, 3, 1, 6, 2, 5, 4, 7, 0, 3, 1, 6, 2, 7, 4, 5, 0, 3, 1, 7, 2, 4, 5, 6, 0, 3, 1, 7, 2, 5, 4, 6, 0, 3, 1, 7, 2, 6, 4, 5, 0, 4, 1, 2, 3, 5, 6, 7, 0, 4, 1, 2, 3, 6, 5, 7, 0, 4, 1, 2, 3, 7, 5, 6, 0, 4, 1, 3, 2, 5, 6, 7, 0, 4, 1, 3, 2, 6, 5, 7, 0, 4, 1, 3, 2, 7, 5, 6, 0, 4, 1, 5, 2, 3, 6, 7, 0, 4, 1, 5, 2, 6, 3, 7, 0, 4, 1, 5, 2, 7, 3, 6, 0, 4, 1, 6, 2, 3, 5, 7, 0, 4, 1, 6, 2, 5, 3, 7, 0, 4, 1, 6, 2, 7, 3, 5, 0, 4, 1, 7, 2, 3, 5, 6, 0, 4, 1, 7, 2, 5, 3, 6, 0, 4, 1, 7, 2, 6, 3, 5, 0, 5, 1, 2, 3, 4, 6, 7, 0, 5, 1, 2, 3, 6, 4, 7, 0, 5, 1, 2, 3, 7, 4, 6, 0, 5, 1, 3, 2, 4, 6, 7, 0, 5, 1, 3, 2, 6, 4, 7, 0, 5, 1, 3, 2, 7, 4, 6, 0, 5, 1, 4, 2, 3, 6, 7, 0, 5, 1, 4, 2, 6, 3, 7, 0, 5, 1, 4, 2, 7, 3, 6, 0, 5, 1, 6, 2, 3, 4, 7, 0, 5, 1, 6, 2, 4, 3, 7, 0, 5, 1, 6, 2, 7, 3, 4, 0, 5, 1, 7, 2, 3, 4, 6, 0, 5, 1, 7, 2, 4, 3, 6, 0, 5, 1, 7, 2, 6, 3, 4, 0, 6, 1, 2, 3, 4, 5, 7, 0, 6, 1, 2, 3, 5, 4, 7, 0, 6, 1, 2, 3, 7, 4, 5, 0, 6, 1, 3, 2, 4, 5, 7, 0, 6, 1, 3, 2, 5, 4, 7, 0, 6, 1, 3, 2, 7, 4, 5, 0, 6, 1, 4, 2, 3, 5, 7, 0, 6, 1, 4, 2, 5, 3, 7, 0, 6, 1, 4, 2, 7, 3, 5, 0, 6, 1, 5, 2, 3, 4, 7, 0, 6, 1, 5, 2, 4, 3, 7, 0, 6, 1, 5, 2, 7, 3, 4, 0, 6, 1, 7, 2, 3, 4, 5, 0, 6, 1, 7, 2, 4, 3, 5, 0, 6, 1, 7, 2, 5, 3, 4, 0, 7, 1, 2, 3, 4, 5, 6, 0, 7, 1, 2, 3, 5, 4, 6, 0, 7, 1, 2, 3, 6, 4, 5, 0, 7, 1, 3, 2, 4, 5, 6, 0, 7, 1, 3, 2, 5, 4, 6, 0, 7, 1, 3, 2, 6, 4, 5, 0, 7, 1, 4, 2, 3, 5, 6, 0, 7, 1, 4, 2, 5, 3, 6, 0, 7, 1, 4, 2, 6, 3, 5, 0, 7, 1, 5, 2, 3, 4, 6, 0, 7, 1, 5, 2, 4, 3, 6, 0, 7, 1, 5, 2, 6, 3, 4, 0, 7, 1, 6, 2, 3, 4, 5, 0, 7, 1, 6, 2, 4, 3, 5, 0, 7, 1, 6, 2, 5, 3, 4 ); vec2 mod2_1(inout vec2 p) { vec2 c = floor(p + 0.5); p = fract(p + 0.5) - 0.5; return c; } float hash(vec2 co) { return fract(sin(dot(co.xy ,vec2(12.9898,58.233))) * 13758.5453); } float psin(float a) { return 0.5 + 0.5*sin(a); } float dot2(vec2 v) { return dot(v,v); } vec3 alphaBlend(vec3 back, vec4 front) { vec3 colb = back.xyz; vec3 colf = front.xyz; vec3 xyz = mix(colb, colf.xyz, front.w); return xyz; } // IQ Bezier: https://www.shadertoy.com/view/MlKcDD float bezier(vec2 pos, vec2 A, vec2 B, vec2 C) { const float sqrt3 = sqrt(3.0); vec2 a = B - A; vec2 b = A - 2.0*B + C; vec2 c = a * 2.0; vec2 d = A - pos; float kk = 1.0/dot(b,b); float kx = kk * dot(a,b); float ky = kk * (2.0*dot(a,a)+dot(d,b))/3.0; float kz = kk * dot(d,a); float res = 0.0; float p = ky - kx*kx; float p3 = p*p*p; float q = kx*(2.0*kx*kx - 3.0*ky) + kz; float h = q*q + 4.0*p3; if(h>=0.0) { // 1 root h = sqrt(h); vec2 x = (vec2(h,-h)-q)/2.0; vec2 uv = sign(x)*pow(abs(x), vec2(1.0/3.0)); float t = clamp(uv.x+uv.y-kx, 0.0, 1.0); res = dot2(d+(c+b*t)*t); } else { // 3 roots float z = sqrt(-p); float v = acos(q/(p*z*2.0))/3.0; float m = cos(v); float n = sin(v)*sqrt3; vec3 t = clamp(vec3(m+m,-n-m,n-m)*z-kx, 0.0, 1.0); res = min(dot2(d+(c+b*t.x)*t.x), dot2(d+(c+b*t.y)*t.y)); // the third root cannot be the closest. See https://www.shadertoy.com/view/4dsfRS // res = min(res,dot2(d+(c+b*t.z)*t.z)); } return sqrt(res); } float bezier2(vec2 p, float f, vec2 off, vec2 p0, vec2 dp0, vec2 p1, vec2 dp1) { float dist = length(p0 - p1); float hdist = 0.5*f*dist; vec2 mp0 = p0 + hdist*dp0; vec2 mp1 = p1 + hdist*dp1; vec2 jp = (mp0 + mp1)*0.5+off; float d0 = bezier(p, p0, mp0, jp); float d1 = bezier(p, p1, mp1, jp); float d = d0; d = min(d, d1); return d; } vec3 color(vec2 p, float s, float aa, vec3 col) { p /= s; vec2 cp = p; vec2 cn = mod2_1(cp); float rr = hash(cn); int sel = int(float(noCorners)*rr); int off = sel*8; const vec3 scol = vec3(0.25); const vec3 bcol = vec3(1.0); const float sw = 0.05; for (int i = 0; i < 4; ++i) { int c0 = corners[off + i*2 + 0]; int c1 = corners[off + i*2 + 1]; int odd = min(c0, c1) & 1; float r = fract(rr*13.0*float(i+1)); int l = abs(c0 - c1) + odd*8; float f = 0.71; vec2 off = vec2(0.0, 0.0); vec2 p0 = coords[c0]; vec2 p1 = coords[c1]; vec2 dp0 = dcoords[c0]; vec2 dp1 = dcoords[c1]; vec2 dp = mix(dp0, dp1, r); switch(l) { // Mid shape case 1: case 15: f = mix(0.75, 2.5, r); break; // L - shape case 2: case 6: case 10: case 14: f = r > 0.5 ? 0.35 : 1.25; break; // Big corner shape case 3: case 13: f = mix(0.5, 1.0, r); break; // Cross line case 4: case 12: f = r>0.5 ? 0.5 : 1.5; break; // Straight line case 5: case 11: f = 1.5; off = (r > 0.5 ? 1.0 : -1.0)*0.15*vec2(dp0.y, -dp0.x); break; // Small corner shape case 7: case 9: f = r>0.5 ? 0.75 : 2.75; break; default: f = 0.5; break; } float dd = (bezier2(cp, f, off, p0, dp0, p1, dp1)-0.025)*s; vec4 sc = vec4(scol, smoothstep(-sw, sw, -dd)); vec4 bc = vec4(bcol, smoothstep(-aa, aa, -dd)); col = alphaBlend(col, sc); col = alphaBlend(col, bc); } return col; } void mainImage(out vec4 fragColor, vec2 fragCoord) { vec2 q = fragCoord/RESOLUTION.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; p += vec2(0.5, sqrt(0.5))*TIME*0.1; float aa = 2.0/RESOLUTION.y; float s = 0.25; vec3 col = vec3(0.1); col = color(p, s, aa, col); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 4214, 4266, 4314, 4314, 5346 ]
[ [ 3776, 3776, 3803, 3803, 3872 ], [ 3874, 3874, 3895, 3895, 3965 ], [ 3967, 3967, 3988, 3988, 4017 ], [ 4019, 4019, 4039, 4039, 4058 ], [ 4064, 4064, 4104, 4104, 4212 ], [ 4214, 4266, 4314, 4314, 5346 ], [ 5348, 5348, 5428, 5428, 5704 ], [ 5706, 5706, 5755, 5755, 7331 ], [ 7333, 7333, 7385, 7385, 7663 ] ]
// IQ Bezier: https://www.shadertoy.com/view/MlKcDD
float bezier(vec2 pos, vec2 A, vec2 B, vec2 C) {
const float sqrt3 = sqrt(3.0); vec2 a = B - A; vec2 b = A - 2.0*B + C; vec2 c = a * 2.0; vec2 d = A - pos; float kk = 1.0/dot(b,b); float kx = kk * dot(a,b); float ky = kk * (2.0*dot(a,a)+dot(d,b))/3.0; float kz = kk * dot(d,a); float res = 0.0; float p = ky - kx*kx; float p3 = p*p*p; float q = kx*(2.0*kx*kx - 3.0*ky) + kz; float h = q*q + 4.0*p3; if(h>=0.0) { // 1 root h = sqrt(h); vec2 x = (vec2(h,-h)-q)/2.0; vec2 uv = sign(x)*pow(abs(x), vec2(1.0/3.0)); float t = clamp(uv.x+uv.y-kx, 0.0, 1.0); res = dot2(d+(c+b*t)*t); } else { // 3 roots float z = sqrt(-p); float v = acos(q/(p*z*2.0))/3.0; float m = cos(v); float n = sin(v)*sqrt3; vec3 t = clamp(vec3(m+m,-n-m,n-m)*z-kx, 0.0, 1.0); res = min(dot2(d+(c+b*t.x)*t.x), dot2(d+(c+b*t.y)*t.y)); // the third root cannot be the closest. See https://www.shadertoy.com/view/4dsfRS // res = min(res,dot2(d+(c+b*t.z)*t.z)); } return sqrt(res); }
// IQ Bezier: https://www.shadertoy.com/view/MlKcDD float bezier(vec2 pos, vec2 A, vec2 B, vec2 C) {
2
6
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 950, 964, 995, 995, 1108 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Transforms
vec3 RotateY(vec3 p, float a) {
float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); }
// Transforms vec3 RotateY(vec3 p, float a) {
1
3
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 1110, 1155, 1188, 1188, 1282 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Cubic falloff // x: distance // R: radius
float Falloff(float x, float R) {
float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; }
// Cubic falloff // x: distance // R: radius float Falloff(float x, float R) {
1
1
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 1284, 1373, 1407, 1407, 1443 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Computes the global lipschitz bound of the falloff function // e: energy // R: radius
float FalloffK(float e, float R) {
return e * 1.72 * abs(e) / R; }
// Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) {
1
1
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 1445, 1588, 1640, 1640, 1961 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy
float FalloffK(float a, float b, float R, float e) {
if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); }
// Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) {
1
1
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 1963, 2068, 2116, 2116, 2159 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy
float Vertex(vec3 p, vec3 c, float R, float e) {
return e * Falloff(length(p - c), R); }
// Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) {
1
1
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 2161, 2320, 2384, 2384, 2944 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end
float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) {
vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; }
// Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) {
1
1
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 2946, 2959, 2981, 2981, 3189 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Tree root
float Object(vec3 p) {
float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; }
// Tree root float Object(vec3 p) {
1
4
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 3191, 3201, 3233, 3233, 3470 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// K root
float KSegment(vec3 a, vec3 b) {
float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; }
// K root float KSegment(vec3 a, vec3 b) {
1
1
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 3532, 3553, 3584, 3584, 3819 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Normal evaluation
vec3 ObjectNormal(in vec3 p ) {
float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); }
// Normal evaluation vec3 ObjectNormal(in vec3 p ) {
1
28
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 3821, 3928, 3988, 3988, 4463 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps
float SphereTracing(vec3 o, vec3 u, out bool h, out int s) {
float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; }
// Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) {
1
1
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 4465, 4570, 4631, 4631, 5512 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps
float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) {
float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; }
// Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) {
1
1
WdVyDW
Moon519
2020-10-16T16:33:14
// Shadertoy implementation of "Segment Tracing using Local Lipschitz Bounds" - Eurographics 2020 // Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie // Paper: https://hal.archives-ouvertes.fr/hal-02507361/document // Video: https://www.youtube.com/watch?v=NOinlrHyieE&feature=youtu.be // Talk: https://www.youtube.com/watch?v=KIOSbWNu-Ms&feature=youtu.be // Github: https://github.com/aparis69/Segment-Tracing // // Sphere tracing on the left - Segment tracing on the right. // You can move the sliders with the mouse. // MIT License const int StepsMax = 150; // Maximum step count for sphere & segment tracing const float Epsilon = 0.1; // Marching epsilon const float T = 0.5; // Surface threshold. const float ra = 20.0; // Ray start interval const float rb = 60.0; // Ray end interval const float radius = 8.0; // Primitive radius const float kappa = 2.0; // Segment tracing factor for next candidate segment // Transforms vec3 RotateY(vec3 p, float a) { float sa = sin(a); float ca = cos(a); return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z); } // Cubic falloff // x: distance // R: radius float Falloff(float x, float R) { float xx = clamp(x / R, 0.0, 1.0); float y = (1.0 - xx * xx); return y * y * y; } // Computes the global lipschitz bound of the falloff function // e: energy // R: radius float FalloffK(float e, float R) { return e * 1.72 * abs(e) / R; } // Computes the local lipschitz bound of the falloff function // a: value at first bound // b: value at second bound // R: radius // e: energy float FalloffK(float a, float b, float R, float e) { if (a > R) return 0.0; if (b < R / 5.0) { float t = (1.0 - b / R); return abs(e) * 6.0 * (sqrt(b) / R) * (t * t); } else if (a > (R * R) / 5.0) { float t = (1.0 - a / R); return abs(e) * 6.0 * (sqrt(a) / R) * (t * t); } else return FalloffK(e, R); } // Primitives // Point primitive field function // p: world point // c: center // R: radius // e: energy float Vertex(vec3 p, vec3 c, float R, float e) { return e * Falloff(length(p - c), R); } // Evaluates the local lipschitz bound of a point primitive over a segment [a, b] // c: center // R: radius // e: energy // a: segment start // b: segment end float VertexKSegment(vec3 c, float R, float e, vec3 a, vec3 b) { vec3 axis = normalize(b - a); float l = dot((c - a), axis); float kk = 0.0; if (l < 0.0) { kk = FalloffK(length(c - a), length(c - b), R, e); } else if (length(b - a) < l) { kk = FalloffK(length(c - b), length(c - a), R, e); } else { float dd = length(c - a) - (l * l); vec3 pc = a + axis * l; kk = FalloffK(dd, max(length(c - b), length(c - a)), R, e); } float grad = max(abs(dot(axis, normalize(c - a))), abs(dot(axis, normalize(c - b)))); return kk * grad; } // Tree root float Object(vec3 p) { float I = Vertex(p, vec3(-radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 2.0, 0, 0), radius, 1.0); I += Vertex(p, vec3(radius / 3.0, radius, 0), radius, 1.0); return I - T; } // K root float KSegment(vec3 a, vec3 b) { float K = VertexKSegment(vec3(-radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 2.0, 0, 0), radius, 1.0, a, b); K += VertexKSegment(vec3(radius / 3.0, radius, 0), radius, 1.0, a, b); return K; } float KGlobal() { return FalloffK(1.0, radius) * 3.0; } // Normal evaluation vec3 ObjectNormal(in vec3 p ) { float eps = 0.001; float v = Object(p); vec3 n; n.x = Object(vec3(p.x + eps, p.y, p.z)) - v; n.y = Object(vec3(p.x, p.y + eps, p.z)) - v; n.z = Object(vec3(p.x, p.y, p.z + eps)) - v; return normalize(n); } // Trace ray using sphere tracing // o : ray origin // u : ray direction // h : hit // s : Number of steps float SphereTracing(vec3 o, vec3 u, out bool h, out int s) { float kGlobal = KGlobal(); float t = ra; h = false; s = 0; for(int i = 0; i < StepsMax; i++) { vec3 p = o + t * u; float v = Object(p); s++; // Hit object if (v > 0.0) { h = true; break; } // Move along ray t += max(Epsilon, abs(v) / kGlobal); // Escape marched far away if (t > rb) break; } return t; } // Trace ray using ray marching // o : ray origin // u : ray direction // h : hit // s : Number of steps float SegmentTracing(vec3 o, vec3 u, out bool h, out int s) { float t = ra; h = false; float candidate = 1.0; for(int i = 0; i < StepsMax; i++) { s++; vec3 p = o + t * u; float v = Object(p); // Hit object if (v > 0.0) { h = true; break; } // Lipschitz constant on a segment float lipschitzSeg = KSegment(p, o + (t + candidate) * u); // Lipschitz marching distance float step = abs(v) / lipschitzSeg; // No further than the segment length step = min(step, candidate); // But at least, Epsilon step = max(Epsilon, step); // Move along ray t += step; // Escape marched far away if (t > rb) break; candidate = kappa * step; } return t; } // Shading functions vec3 Background(vec3 rd) { const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); } vec3 Shade(vec3 p, vec3 n) { const vec3 l1 = normalize(vec3(-2.0, -1.0, -1.0)); const vec3 l2 = normalize(vec3(2.0, 0.0, 1.0)); float d1 = pow(0.5 * (1.0 + dot(n, l1)), 2.0); float d2 = pow(0.5 * (1.0 + dot(n, l2)), 2.0); return vec3(0.6) + 0.2 * (d1 + d2)* Background(n); } vec3 ShadeSteps(int n) { const vec3 a = vec3(97, 130, 234) / vec3(255.0); const vec3 b = vec3(220, 94, 75) / vec3(255.0); const vec3 c = vec3(221, 220, 219) / vec3(255.0); float t = float(n) / float(StepsMax); if (t < 0.5) return mix(a, c, 2.0 * t); else return mix(c, b, 2.0 * t - 1.0); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { // Compute ray origin and direction vec2 pixel = (gl_FragCoord.xy / iResolution.xy) * 2.0 - 1.0; float asp = iResolution.x / iResolution.y; vec3 rd = normalize(vec3(asp * pixel.x, pixel.y - 1.5, -4.0)); vec3 ro = vec3(0.0, 18, 40.0); vec2 mouse = (iMouse.xy / iResolution.xy) * 2.0 - 1.0; if (mouse.y <= -0.9999) // show cost at frame 0 mouse.xy = vec2(0.0); float a = (iTime * 0.25); ro = RotateY(ro, a); rd = RotateY(rd, a); // Trace ray bool hit; // Ray hit flag int s; // Number of steps float t; // Ray hit position float sep = mouse.x; // Sphere tracing on the left if (pixel.x < sep) t = SphereTracing(ro, rd, hit, s); // Segment tracing on the right else t = SegmentTracing(ro, rd, hit, s); // Shade this with object vec3 rgb = Background(rd); if (pixel.y > mouse.y) { if (hit) { vec3 pos = ro + t * rd; vec3 n = ObjectNormal(pos); rgb = Shade(pos, n); } } else { rgb = ShadeSteps(s); } rgb *= smoothstep(1.0, 2.0, abs(pixel.x-sep)/(2.0 / iResolution.x)); rgb *= smoothstep(1.0, 2.0, abs(pixel.y-mouse.y)/(2.0 / iResolution.y)); fragColor = vec4(rgb, 1.0); }
mit
[ 5514, 5535, 5561, 5561, 5683 ]
[ [ 950, 964, 995, 995, 1108 ], [ 1110, 1155, 1188, 1188, 1282 ], [ 1284, 1373, 1407, 1407, 1443 ], [ 1445, 1588, 1640, 1640, 1961 ], [ 1963, 2068, 2116, 2116, 2159 ], [ 2161, 2320, 2384, 2384, 2944 ], [ 2946, 2959, 2981, 2981, 3189 ], [ 3191, 3201, 3233, 3233, 3470 ], [ 3471, 3471, 3488, 3488, 3530 ], [ 3532, 3553, 3584, 3584, 3819 ], [ 3821, 3928, 3988, 3988, 4463 ], [ 4465, 4570, 4631, 4631, 5512 ], [ 5514, 5535, 5561, 5561, 5683 ], [ 5684, 5684, 5712, 5712, 5978 ], [ 5979, 5979, 6003, 6003, 6311 ], [ 6313, 6313, 6368, 6417, 7688 ] ]
// Shading functions
vec3 Background(vec3 rd) {
const vec3 C1 = vec3(0.8, 0.8, 0.9); const vec3 C2 = vec3(0.6, 0.8, 1.0); return mix(C1, C2, rd.y * 1.0 + 0.25); }
// Shading functions vec3 Background(vec3 rd) {
1
6
WsVcRd
pikmin2010
2020-10-25T01:49:01
// The MIT License // Copyright © 2013 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 list of useful distance function to simple primitives. All // these functions (except for ellipsoid) return an exact // euclidean distance, meaning they produce a better SDF than // what you'd get if you were constructing them from boolean // operations. // // More info here: // // https://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm //------------------------------------------------------------------ #define March_Quality 32 #define Shadow_Quality 4 #define AO_Quality 8 #define Lighting 1 #define Epsilon 0.00001 float dot2( in vec2 v ) { return dot(v,v); } float dot2( in vec3 v ) { return dot(v,v); } float ndot( in vec2 a, in vec2 b ) { return a.x*b.x - a.y*b.y; } /** * Rotation matrix around the X axis. */ mat3 rotateX(float theta) { float c = cos(theta); float s = sin(theta); return mat3( vec3(1, 0, 0), vec3(0, c, -s), vec3(0, s, c) ); } /** * Rotation matrix around the Y axis. */ mat3 rotateY(float theta) { float c = cos(theta); float s = sin(theta); return mat3( vec3(c, 0, s), vec3(0, 1, 0), vec3(-s, 0, c) ); } /** * Rotation matrix around the Z axis. */ mat3 rotateZ(float theta) { float c = cos(theta); float s = sin(theta); return mat3( vec3(c, -s, 0), vec3(s, c, 0), vec3(0, 0, 1) ); } float sdSphere( vec3 p, float s ) { return length(p)-s; } float sdBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); } float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) { vec3 pa = p-a, ba = b-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ) - r; } // vertical float sdCylinder( vec3 p, vec2 h ) { vec2 d = abs(vec2(length(p.xz),p.y)) - h; return min(max(d.x,d.y),0.0) + length(max(d,0.0)); } // arbitrary orientation float sdCylinder(vec3 p, vec3 a, vec3 b, float r) { vec3 pa = p - a; vec3 ba = b - a; float baba = dot(ba,ba); float paba = dot(pa,ba); float x = length(pa*baba-ba*paba) - r*baba; float y = abs(paba-baba*0.5)-baba*0.5; float x2 = x*x; float y2 = y*y*baba; float d = (max(x,y)<0.0)?-min(x2,y2):(((x>0.0)?x2:0.0)+((y>0.0)?y2:0.0)); return sign(d)*sqrt(abs(d))/baba; } //------------------------------------------------------------------ vec2 opU( vec2 d1, vec2 d2 ) { return (d1.x<d2.x) ? d1 : d2; } vec2 opB( vec2 d1, vec2 d2 ) { return (d1.x>d2.x) ? d1 : d2; } vec3 opRepLim( vec3 p, float c, vec3 l) { vec3 q = p-c*clamp(round(p/c),-l,l); return ( q ); } //------------------------------------------------------------------ #define ZERO (min(iFrame,0)) //------------------------------------------------------------------ vec2 map( in vec3 pos ) { pos = opRepLim(pos,3.,vec3(1,1,1)); pos.y +=0.2; pos.y *=0.9; vec2 belly = vec2( sdSphere( pos-vec3(.0,0., 0.2), 0.5 ), 11 ); vec2 brownBelly = vec2(sdSphere( pos-vec3(.0,0., 0.0), 0.6), 12 ); vec2 brownHead = vec2(sdSphere( pos-vec3(.0,0.6, 0.0), 0.4), 12 ); vec3 eyePos = pos; eyePos.x *=1.3; vec2 whiteEyeL = vec2(sdSphere( eyePos-vec3(.15,0.65, 0.25),0.15), 11 ); vec2 whiteEyeR = vec2(sdSphere( eyePos-vec3(-.15,0.65, 0.25), 0.15), 11 ); vec2 brownEyeL = vec2(sdSphere( eyePos-vec3(.15,0.73, 0.22),0.15), 12 ); vec2 brownEyeR = vec2(sdSphere( eyePos-vec3(-.15,0.73, 0.22), 0.15), 12 ); vec2 pupilEyeL = vec2(sdSphere( eyePos-vec3(.15,0.65, 0.37),0.05), 13 ); vec2 pupilEyeR = vec2(sdSphere( eyePos-vec3(-.15,0.65, 0.37), 0.05), 13 ); vec3 furPos = pos; furPos.x *=0.5; vec2 mouthFur = vec2(sdSphere( furPos-vec3(0.,0.44,0.35),0.11),11); vec2 mouthFur2 = vec2(sdSphere( furPos-vec3(0.,0.5,0.35),0.09),11); vec2 mouthFur3 = vec2(sdSphere( furPos-vec3(0.,0.56,0.42),0.04),11); vec3 earPos = pos; earPos.z *=1.6; earPos.x *=1.7; earPos.y *=0.5; vec2 brownEarL = vec2(sdSphere(earPos-vec3(0.27,0.5,0.),0.25),12); vec2 brownEarR = vec2(sdSphere(earPos-vec3(-0.27,0.5,0.),0.25),12); vec2 redEarL = vec2(sdSphere(earPos-vec3(0.27,0.5,0.1),0.18),14); vec2 redEarR = vec2(sdSphere(earPos-vec3(-0.27,0.5,0.1),0.18),14); vec3 mouthPos = pos; mouthPos.y -= 0.5; mouthPos.z +=0.14; vec2 mouthSub = vec2(sdBox(mouthPos-vec3(0,-0.05,.5),vec3(0.1,0.1,0.1)),15); vec2 redMouth = vec2(sdSphere(mouthPos-vec3(0.,0.,.5),0.1),15); redMouth = opB(redMouth,mouthSub); vec2 teeth = opU(vec2(sdBox(mouthPos-vec3(0.035,0.031,0.59),vec3(0.025,0.03,0.005)),11),vec2(sdBox(mouthPos-vec3(-0.035,0.031,0.59),vec3(0.025,0.03,0.005)),11)); vec3 feetPos =pos; feetPos.y += 0.4; feetPos.x *=0.7; vec2 leftFoot = vec2(sdSphere(feetPos-vec3(-0.30,0,0),0.25),12); vec2 rightFoot = vec2(sdSphere(feetPos-vec3(0.30,0,0),0.25),12); feetPos.y *=1.1; vec2 leftFootWhite = vec2(sdSphere(feetPos-vec3(-0.40,-0.05,0),0.25),11); vec2 rightFootWhite = vec2(sdSphere(feetPos-vec3(0.40,-0.05,0),0.25),11); vec2 feetWhite = opU(leftFootWhite,rightFootWhite); vec2 feet = opU(leftFoot,rightFoot); feet = opU(feetWhite,feet); vec2 rightArm = vec2(sdCylinder(pos-vec3(0.5,0.25,0.),vec3(0,0,0),vec3(0.5,0,0),0.1),12); vec2 leftArm = vec2(sdCylinder(pos-vec3(-0.5,0.25,0.),vec3(0,0,0),vec3(-0.5,0,0),0.1),12); vec2 leftHand = vec2(sdSphere(pos-vec3(-1.0,0.25,0),0.15),11); vec2 rightHand = vec2(sdSphere(pos-vec3(1.0,0.25,0),0.15),11); vec2 res =opU(brownBelly,belly); res = opU(res,brownHead); res = opU(res,whiteEyeL); res = opU(res,whiteEyeR); res = opU(res,brownEyeR); res = opU(res,brownEyeL); res = opU(res,pupilEyeR); res = opU(res,pupilEyeL); res = opU(res,mouthFur); res = opU(res,mouthFur2); res = opU(res,mouthFur3); res = opU(res,brownEarL); res = opU(res,brownEarR); res = opU(res,redEarL); res = opU(res,redEarR); res = opU(res,redMouth); res = opU(res,teeth); res = opU(res,feet); res = opU(res,rightArm); res = opU(res,leftArm); res = opU(res,leftHand); res = opU(res,rightHand); return res; } vec2 iBox( in vec3 ro, in vec3 rd, in vec3 rad ) { vec3 m = 1.0/rd; vec3 n = m*ro; vec3 k = abs(m)*rad; vec3 t1 = -n - k; vec3 t2 = -n + k; return vec2( max( max( t1.x, t1.y ), t1.z ), min( min( t2.x, t2.y ), t2.z ) ); } vec2 raycast( in vec3 ro, in vec3 rd ) { vec2 res = vec2(-1.0,-1.0); float tmin = 1.0; float tmax = 20.0; //else return res; // raymarch primitives float t = tmin; for( int i=0; i<March_Quality && t<tmax; i++ ) { vec2 h = map( ro+rd*t ); if( abs(h.x)<(Epsilon*t) ) { res = vec2(t,h.y); break; } t += h.x; } return res; } // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax ) { // bounding volume float tp = (0.8-ro.y)/rd.y; if( tp>0.0 ) tmax = min( tmax, tp ); float res = 1.0; float t = mint; for( int i=ZERO; i<Shadow_Quality; i++ ) { float h = map( ro + rd*t ).x; float s = clamp(8.0*h/t,0.0,1.0); res = min( res, s*s*(3.0-2.0*s) ); t += clamp( h, 0.02, 0.2 ); if( res<0.004 || t>tmax ) break; } return clamp( res, 0.0, 1.0 ); } // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm vec3 calcNormal( in vec3 pos ) { #if 0 vec2 e = vec2(1.0,-1.0)*0.5773*0.0005; return normalize( e.xyy*map( pos + e.xyy ).x + e.yyx*map( pos + e.yyx ).x + e.yxy*map( pos + e.yxy ).x + e.xxx*map( pos + e.xxx ).x ); #else // inspired by tdhooper and klems - a way to prevent the compiler from inlining map() 4 times vec3 n = vec3(0.0); for( int i=ZERO; i<4; i++ ) { vec3 e = 0.5773*(2.0*vec3((((i+3)>>1)&1),((i>>1)&1),(i&1))-1.0); n += e*map(pos+0.0005*e).x; //if( n.x+n.y+n.z>100.0 ) break; } return normalize(n); #endif } float calcAO( in vec3 pos, in vec3 nor ) { float occ = 0.0; float sca = 1.0; for( int i=ZERO; i<AO_Quality; i++ ) { float h = 0.01 + 0.12*float(i)/4.0; float d = map( pos + h*nor ).x; occ += (h-d)*sca; sca *= 0.95; if( occ>0.35 ) break; } return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) * (0.5+0.5*nor.y); } // http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm vec3 render( in vec3 ro, in vec3 rd, in vec3 rdx, in vec3 rdy ) { // background vec3 col = vec3(0.7, 0.7, 0.9) - max(rd.y,0.0)*0.3; // raycast scene vec2 res = raycast(ro,rd); float t = res.x; float m = res.y; if( m>-0.5 ) { vec3 pos = ro + t*rd; vec3 nor = (m<1.5) ? vec3(0.0,1.0,0.0) : calcNormal( pos ); vec3 ref = reflect( rd, nor ); // material col = 0.2 + 0.2*sin( m*2.0 + vec3(0.0,1.0,2.0) ); float ks = 1.0; if(m>=10.){ if(m<=11.){ col = vec3(1,1,1); }else if(m<=12.){ col = vec3(0.45,0.4,0.4); }else if(m<=13.){ col = vec3(0.,0.,0.); }else if(m<=14.){ col = vec3(0.85,0.54,0.50); }else if(m<=15.){ col = vec3(0.75,0.47,0.63); } // project pixel footprint into the plane vec3 dpdx = ro.y*(rd/rd.y-rdx/rdx.y); vec3 dpdy = ro.y*(rd/rd.y-rdy/rdy.y); ks = 0.4; } if(Lighting ==1){ // lighting float occ = calcAO( pos, nor ); vec3 lin = vec3(0.0); // sun { vec3 lig = normalize( vec3(1.5, 0.8, 0.6) ); vec3 hal = normalize( lig-rd ); float dif = clamp( dot( nor, lig ), 0.0, 1.0 ); //if( dif>0.0001 ) dif *= calcSoftshadow( pos, lig, 0.02, 2.5 ); float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0); spe *= dif; spe *= 0.04+0.96*pow(clamp(1.0-dot(hal,lig),0.0,1.0),5.0); lin += col*2.20*dif*vec3(1.30,1.00,0.70); lin += 5.00*spe*vec3(1.30,1.00,0.70)*ks; } // sky { float dif = sqrt(clamp( 0.5+0.5*nor.y, 0.0, 1.0 )); dif *= occ; float spe = smoothstep( -0.2, 0.2, ref.y ); spe *= dif; spe *= 0.04+0.96*pow(clamp(1.0+dot(nor,rd),0.0,1.0), 5.0 ); //if( spe>0.001 ) spe *= calcSoftshadow( pos, ref, 0.02, 2.5 ); lin += col*0.60*dif*vec3(0.40,0.60,1.15); lin += 2.00*spe*vec3(0.40,0.60,1.30)*ks; } // back { float dif = clamp( dot( nor, normalize(vec3(0.5,0.0,0.6))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0); dif *= occ; lin += col*0.55*dif*vec3(0.25,0.25,0.25); } // sss { float dif = pow(clamp(1.0+dot(nor,rd),0.0,1.0),2.0); dif *= occ; lin += col*0.25*dif*vec3(1.00,1.00,1.00); } col = lin; } col = mix( col, vec3(0.7,0.7,0.9), 1.0-exp( -0.0001*t*t*t ) ); } return vec3( clamp(col,0.0,1.0) ); } mat3 setCamera( in vec3 ro, in vec3 ta, float cr ) { vec3 cw = normalize(ta-ro); vec3 cp = vec3(sin(cr), cos(cr),0.0); vec3 cu = normalize( cross(cw,cp) ); vec3 cv = ( cross(cu,cw) ); return mat3( cu, cv, cw ); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 mo = iMouse.xy/iResolution.xy; float time = 32.0 + iTime*1.5; // camera vec3 ta = vec3( 0., -0., -0. ); vec3 ro = ta + vec3( 4.5*cos(0.1*time + 7.0*mo.x), 1.3 + 2.0*mo.y, 4.5*sin(0.1*time + 7.0*mo.x) ); // camera-to-world transformation mat3 ca = setCamera( ro, ta, 0.0 ); vec3 tot = vec3(0.1); vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; // ray direction vec3 rd = ca * normalize( vec3(p,2.5) ); // ray differentials vec2 px = (2.0*(fragCoord+vec2(1.0,0.0))-iResolution.xy)/iResolution.y; vec2 py = (2.0*(fragCoord+vec2(0.0,1.0))-iResolution.xy)/iResolution.y; vec3 rdx = ca * normalize( vec3(px,2.5) ); vec3 rdy = ca * normalize( vec3(py,2.5) ); // render vec3 col = render( ro, rd, rdx, rdy ); // gain // col = col*3.0/(2.5+col); // gamma col = pow( col, vec3(0.45) ); tot += col; fragColor = vec4( tot, 1.0 ); }
mit
[ 2956, 2981, 3032, 3032, 3387 ]
[ [ 1622, 1622, 1647, 1647, 1666 ], [ 1667, 1667, 1692, 1692, 1711 ], [ 1712, 1712, 1748, 1748, 1776 ], [ 1780, 1826, 1853, 1853, 2000 ], [ 2002, 2048, 2075, 2075, 2222 ], [ 2224, 2270, 2297, 2297, 2444 ], [ 2446, 2446, 2481, 2481, 2507 ], [ 2509, 2509, 2540, 2540, 2631 ], [ 2634, 2634, 2686, 2686, 2800 ], [ 2803, 2815, 2851, 2851, 2954 ], [ 2956, 2981, 3032, 3032, 3387 ], [ 3461, 3461, 3491, 3491, 3524 ], [ 3526, 3526, 3556, 3556, 3589 ], [ 3591, 3591, 3634, 3634, 3695 ], [ 3866, 3866, 3891, 3891, 7296 ], [ 7298, 7298, 7349, 7349, 7554 ], [ 7556, 7556, 7596, 7596, 8051 ], [ 8053, 8115, 8193, 8216, 8615 ], [ 8617, 8681, 8713, 8713, 9283 ], [ 9285, 9285, 9327, 9327, 9645 ], [ 9725, 9725, 9790, 9809, 12550 ], [ 12552, 12552, 12604, 12604, 12781 ], [ 12783, 12783, 12840, 12840, 13853 ] ]
// arbitrary orientation
float sdCylinder(vec3 p, vec3 a, vec3 b, float r) {
vec3 pa = p - a; vec3 ba = b - a; float baba = dot(ba,ba); float paba = dot(pa,ba); float x = length(pa*baba-ba*paba) - r*baba; float y = abs(paba-baba*0.5)-baba*0.5; float x2 = x*x; float y2 = y*y*baba; float d = (max(x,y)<0.0)?-min(x2,y2):(((x>0.0)?x2:0.0)+((y>0.0)?y2:0.0)); return sign(d)*sqrt(abs(d))/baba; }
// arbitrary orientation float sdCylinder(vec3 p, vec3 a, vec3 b, float r) {
13
23
wdGfzD
IWBTShyGuy
2020-11-29T18:44:50
// Copyright © 2020 IWBTShyGuy // Attribution 4.0 International (CC BY 4.0) const float PI = 3.141592653; const float PI2 = 2.0 * PI; // square const int N = 4; // the circumradius of polygon const float R_POLY = 0.4; const float SCREW_THICKNESS = 0.02; // the half of thickness of polygon edges const float THICKNESS = 0.025; // Good Colors!! const vec3 COLOR[N] = vec3[]( vec3(226.0, 133.0, 27.0) / 255.0, vec3(126.0, 107.0, 152.0) / 255.0, vec3(238.0, 200.0, 80.0) / 255.0, vec3(136.0, 175.0, 34.0) / 255.0 ); // the radius of the vertex of square const float R_DOT = 0.04; // normalized fragment coordinate vec2 uv_coord(vec2 coord) { int max_idx = iResolution.x > iResolution.y ? 0 : 1; int min_idx = 1 - max_idx; vec2 aspect_vec = vec2(1.0, 1.0); aspect_vec[max_idx] = iResolution[max_idx] / iResolution[min_idx]; return 2.0 * coord / iResolution[min_idx] - aspect_vec; } // Creates vertices of polygon vec2[N] createVertex() { vec2 vertex[N]; for (int i = 0; i < N; i++) { float theta = float(i) / float(N) * PI2; vertex[i] = vec2(cos(theta), sin(theta)) * R_POLY; } return vertex; } float get_angle(in vec2 uv) { float theta = acos(uv.x / length(uv)); if (uv.y < 0.0) theta = 2.0 * PI - theta; return theta; } float torus_distance(in float x, in float y) { float a = abs(x - y); float b = abs(PI2 + x - y); float c = abs(x - y - PI2); return min(a, min(b, c)); } vec4 renderScrew(in vec2 uv) { float len = length(uv); float theta = get_angle(uv); float c = 0.0; int idx = 0; for (int i = 0; i < N; i++) { if (len < R_POLY) continue; float delta = float(i) / float(N); float phase = fract((iTime - PI2 * len + PI2 * delta) / PI2) * PI2; float dist = smoothstep(0.0, 1.0, (torus_distance(phase, theta) / PI2) / SCREW_THICKNESS); if (c < 1.0 - dist * dist * dist) { c = 1.0 - dist * dist * dist; idx = i % N; } } return vec4(c * COLOR[idx], 1.0); } vec4 renderSquare(in vec4 fragColor, in vec2 uv, in vec2 vertex[N]) { float theta = iTime - 2.0 * PI * R_POLY; uv = mat2(cos(theta), -sin(theta), sin(theta), cos(theta)) * uv; float plus = abs(uv.x + uv.y); float minus = abs(uv.x - uv.y); if (plus < R_POLY - THICKNESS && minus < R_POLY - THICKNESS) fragColor = vec4(0.0, 0.0, 0.0, 1.0); else if (plus < R_POLY + THICKNESS && minus < R_POLY + THICKNESS) { float k = 1.0 - abs(plus - R_POLY) / THICKNESS; k = max(k, 1.0 - abs(minus - R_POLY) / THICKNESS); k = 1.0 - pow(1.0 - k, 5.0); vec3 col = vec3(0.0); for (int i = 0; i < N; i++) { float c = distance(vertex[i], uv) / R_POLY; c = smoothstep(0.0, 1.0, c); c = 1.0 - pow(c, 3.0); col += c * COLOR[i] * k; } fragColor = vec4(col, 1.0); } return fragColor; } vec4 renderVertices(in vec4 fragColor, in vec2 uv, in vec2 vertex[N]) { float theta = iTime - 2.0 * PI * R_POLY; uv = mat2(cos(theta), -sin(theta), sin(theta), cos(theta)) * uv; for (int i = 0; i < N; i++) { if (distance(uv, vertex[i]) < R_DOT) { float c = distance(uv, vertex[i]) / R_DOT; c = 1.0 - pow(c, 5.0); fragColor = vec4(COLOR[i] * c, 1.0); } } return fragColor; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = uv_coord(fragCoord); vec2 vertex[N] = createVertex(); fragColor = renderScrew(uv); fragColor = renderSquare(fragColor, uv, vertex); fragColor = renderVertices(fragColor, uv, vertex); }
cc-by-4.0
[ 602, 636, 663, 663, 922 ]
[ [ 602, 636, 663, 663, 922 ], [ 1170, 1170, 1199, 1199, 1308 ], [ 1310, 1310, 1356, 1356, 1478 ], [ 1480, 1480, 1510, 1510, 2066 ], [ 2068, 2068, 2137, 2137, 2970 ], [ 2972, 2972, 3043, 3043, 3417 ], [ 3419, 3419, 3474, 3474, 3689 ] ]
// normalized fragment coordinate
vec2 uv_coord(vec2 coord) {
int max_idx = iResolution.x > iResolution.y ? 0 : 1; int min_idx = 1 - max_idx; vec2 aspect_vec = vec2(1.0, 1.0); aspect_vec[max_idx] = iResolution[max_idx] / iResolution[min_idx]; return 2.0 * coord / iResolution[min_idx] - aspect_vec; }
// normalized fragment coordinate vec2 uv_coord(vec2 coord) {
1
1
3dyfDd
iq
2020-12-17T12:19:02
// The MIT License // Copyright © 2020 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. // Signed distance and analytic gradient to an isosceles // triangle. More accurate than central differences and // faster to compute than automatic differentiation/duals. // List of other 2D distances+gradients: // // https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm // // and // // https://www.shadertoy.com/playlist/M3dSRf // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 vec3 sdgTriangleIsosceles( in vec2 p, in vec2 q ) { float w = sign(p.x); p.x = abs(p.x); vec2 a = p - q*clamp( dot(p,q)/dot(q,q), 0.0, 1.0 ); vec2 b = p - q*vec2( clamp( p.x/q.x, 0.0, 1.0 ), 1.0 ); float k = sign( q.y ); float l1 = dot(a,a); float l2 = dot(b,b); float d = sqrt((l1<l2)?l1:l2); vec2 g = (l1<l2)? a: b; float s = max( k*(p.x*q.y-p.y*q.x),k*(p.y-q.y) ); return vec3(d,vec2(w*g.x,g.y)/d)*sign(s); } #define AA 2 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y; #else vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y; #endif p.y -= 0.3; // size vec2 si = vec2(0.5,-0.5) + vec2(0.3,-0.3)*cos( iTime + vec2(0.0,1.57) + 0.0 ); // sdf(p) and gradient(sdf(p)) vec3 dg = sdgTriangleIsosceles(p,si); float d = dg.x; vec2 g = dg.yz; // central differenes based gradient, for comparison // g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y); // coloring vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85); col *= 1.0 + vec3(0.5*g,0.0); //col = vec3(0.5+0.5*g,1.0); col *= 1.0 - 0.5*exp(-16.0*abs(d)); col *= 0.9 + 0.1*cos(150.0*d); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 1438, 1533, 1584, 1584, 1992 ]
[ [ 1438, 1533, 1584, 1584, 1992 ] ]
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgTriangleIsosceles( in vec2 p, in vec2 q ) {
float w = sign(p.x); p.x = abs(p.x); vec2 a = p - q*clamp( dot(p,q)/dot(q,q), 0.0, 1.0 ); vec2 b = p - q*vec2( clamp( p.x/q.x, 0.0, 1.0 ), 1.0 ); float k = sign( q.y ); float l1 = dot(a,a); float l2 = dot(b,b); float d = sqrt((l1<l2)?l1:l2); vec2 g = (l1<l2)? a: b; float s = max( k*(p.x*q.y-p.y*q.x),k*(p.y-q.y) ); return vec3(d,vec2(w*g.x,g.y)/d)*sign(s); }
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 vec3 sdgTriangleIsosceles( in vec2 p, in vec2 q ) {
1
1
3lGXRc
iq
2021-01-18T05:50:56
// The MIT License // Copyright © 2021 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. // Signed distance and gradient to a Vesica. Probably // faster than central differences or automatic // differentiation/dual numbers. // List of other 2D distances+gradients: // // https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm // // and // // https://www.shadertoy.com/playlist/M3dSRf // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 vec3 sdgVesica(vec2 p, float r, float d) { vec2 s = sign(p); p = abs(p); float b = sqrt(r*r-d*d); // can delay this sqrt by rewriting the comparison vec3 res; if( (p.y-b)*d > p.x*b ) { vec2 q = vec2(p.x,p.y-b); float l = length(q)*sign(d); res = vec3( l, q/l ); } else { vec2 q = vec2(p.x+d,p.y); float l = length(q); res = vec3( l-r, q/l ); } return vec3(res.x, res.yz*s ); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; // animate float time = iTime; float r1 = 0.5*cos(time+12.0); float r2 = 0.2*sin(time*1.4); // sdf(p) and gradient(sdf(p)) vec3 dg = sdgVesica( p, 0.7, r1 ); float d = dg.x + r2; vec2 g = dg.yz; // central differenes based gradient, for comparison // g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y); // coloring vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85); col *= 1.0 + vec3(0.5*g,0.0); //col = vec3(0.5+0.5*g,1.0); col *= 1.0 - 0.7*exp(-8.0*abs(d)); col *= 0.9 + 0.1*cos(150.0*d); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) ); fragColor = vec4(col,1.0); }
mit
[ 1402, 1497, 1539, 1539, 1970 ]
[ [ 1402, 1497, 1539, 1539, 1970 ], [ 1972, 1972, 2029, 2029, 2745 ] ]
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgVesica(vec2 p, float r, float d) {
vec2 s = sign(p); p = abs(p); float b = sqrt(r*r-d*d); // can delay this sqrt by rewriting the comparison vec3 res; if( (p.y-b)*d > p.x*b ) { vec2 q = vec2(p.x,p.y-b); float l = length(q)*sign(d); res = vec3( l, q/l ); } else { vec2 q = vec2(p.x+d,p.y); float l = length(q); res = vec3( l-r, q/l ); } return vec3(res.x, res.yz*s ); }
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 vec3 sdgVesica(vec2 p, float r, float d) {
2
2
3tGXRc
iq
2021-01-18T05:35:51
// The MIT License // Copyright © 2021 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. // Signed distance and gradient to a pie. Probably // faster than central differences or automatic // differentiation/dual numbers. // List of other 2D distances+gradients: // // https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm // // and // // https://www.shadertoy.com/playlist/M3dSRf // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 // c is the sin/cos of the angle. r is the radius vec3 sdgPie( in vec2 p, in vec2 c, in float r ) { float s = sign(p.x); p.x = abs(p.x); float l = length(p); float n = l - r; vec2 q = p - c*clamp(dot(p,c),0.0,r); float m = length(q)* sign(c.y*p.x-c.x*p.y); vec3 res = (n>m) ? vec3(n,p/l) : vec3(m,q/m); return vec3(res.x,s*res.y,res.z); } #define AA 2 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y; #else vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y; #endif // animation float t = 3.14*(0.5+0.5*cos(iTime*0.52)); vec2 w = vec2(0.50,0.25)*(0.5+0.5*cos(iTime*vec2(1.1,1.3)+vec2(0.0,2.0))); // sdf(p) and gradient(sdf(p)) vec3 dg = sdgPie(p,vec2(sin(t),cos(t)), 0.65); float d = dg.x; vec2 g = dg.yz; // central differenes based gradient, for comparison // g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y); // coloring vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85); col *= 1.0 + vec3(0.5*g,0.0); //col = vec3(0.5+0.5*g,1.0); col *= 1.0 - 0.7*exp(-8.0*abs(d)); col *= 0.9 + 0.1*cos(150.0*d); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 1398, 1543, 1592, 1592, 1868 ]
[ [ 1398, 1543, 1592, 1592, 1868 ] ]
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 // c is the sin/cos of the angle. r is the radius
vec3 sdgPie( in vec2 p, in vec2 c, in float r ) {
float s = sign(p.x); p.x = abs(p.x); float l = length(p); float n = l - r; vec2 q = p - c*clamp(dot(p,c),0.0,r); float m = length(q)* sign(c.y*p.x-c.x*p.y); vec3 res = (n>m) ? vec3(n,p/l) : vec3(m,q/m); return vec3(res.x,s*res.y,res.z); }
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 // c is the sin/cos of the angle. r is the radius vec3 sdgPie( in vec2 p, in vec2 c, in float r ) {
1
1
tlVyWh
iq
2021-01-18T04:33:35
// The MIT License // Copyright © 2021 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. // Signed distance and gradient to a triangle. Probably // faster than central differences or automatic // differentiation/dual numbers. // List of other 2D distances+gradients: // https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm // // Circle: https://www.shadertoy.com/view/WltSDj // Pie: https://www.shadertoy.com/view/3tGXRc // Arc: https://www.shadertoy.com/view/WtGXRc // Isosceles Triangle: https://www.shadertoy.com/view/3dyfDd // Triangle: https://www.shadertoy.com/view/tlVyWh // Box: https://www.shadertoy.com/view/wlcXD2 // Quad: https://www.shadertoy.com/view/WtVcD1 // Cross: https://www.shadertoy.com/view/WtdXWj // Segment: https://www.shadertoy.com/view/WtdSDj // Hexagon: https://www.shadertoy.com/view/WtySRc // Vesica: https://www.shadertoy.com/view/3lGXRc // Smooth-Minimum: https://www.shadertoy.com/view/tdGBDt // Parallelogram: https://www.shadertoy.com/view/sssGzX float cro( in vec2 a, in vec2 b ) { return a.x*b.y - a.y*b.x; } // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 vec3 sdgTriangle( in vec2 p, in vec2 v[3] ) { float gs = cro(v[0]-v[2],v[1]-v[0]); vec4 res; // edge 0 { vec2 e = v[1]-v[0]; vec2 w = p-v[0]; vec2 q = w-e*clamp(dot(w,e)/dot(e,e),0.0,1.0); float d = dot(q,q); float s = gs*cro(w,e); res = vec4(d,q,s); } // edge 1 { vec2 e = v[2]-v[1]; vec2 w = p-v[1]; vec2 q = w-e*clamp(dot(w,e)/dot(e,e),0.0,1.0); float d = dot(q,q); float s = gs*cro(w,e); res = vec4( (d<res.x) ? vec3(d,q) : res.xyz, (s>res.w) ? s : res.w ); } // edge 2 { vec2 e = v[0]-v[2]; vec2 w = p-v[2]; vec2 q = w-e*clamp(dot(w,e)/dot(e,e),0.0,1.0); float d = dot(q,q); float s = gs*cro(w,e); res = vec4( (d<res.x) ? vec3(d,q) : res.xyz, (s>res.w) ? s : res.w ); } // distance and sign float d = sqrt(res.x)*sign(res.w); return vec3(d,res.yz/d); } #define AA 2 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y; #else vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y; #endif // animate float time = iTime; vec2 v[3] = vec2[3]( vec2(-0.8,-0.3) + 0.5*cos( 0.5*time + vec2(0.0,1.9) + 4.0 ), vec2( 0.8,-0.3) + 0.5*cos( 0.7*time + vec2(0.0,1.7) + 2.0 ), vec2( 0.0, 0.3) + 0.5*cos( 0.9*time + vec2(0.0,1.3) + 1.0 ) ); // corner radious float ra = 0.1*(0.5+0.5*sin(iTime*1.2)); // sdf(p) and gradient(sdf(p)) vec3 dg = sdgTriangle(p,v); float d = dg.x-ra; vec2 g = dg.yz; // central differenes based gradient, for comparison // g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y); // coloring vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85); col *= 1.0 + vec3(0.5*g,0.0); //col = vec3(0.5+0.5*g,1.0); col *= 1.0 - 0.7*exp(-8.0*abs(d)); col *= 0.9 + 0.1*cos(150.0*d); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 2203, 2298, 2343, 2343, 3260 ]
[ [ 2138, 2138, 2173, 2173, 2201 ], [ 2203, 2298, 2343, 2343, 3260 ] ]
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgTriangle( in vec2 p, in vec2 v[3] ) {
float gs = cro(v[0]-v[2],v[1]-v[0]); vec4 res; // edge 0 { vec2 e = v[1]-v[0]; vec2 w = p-v[0]; vec2 q = w-e*clamp(dot(w,e)/dot(e,e),0.0,1.0); float d = dot(q,q); float s = gs*cro(w,e); res = vec4(d,q,s); } // edge 1 { vec2 e = v[2]-v[1]; vec2 w = p-v[1]; vec2 q = w-e*clamp(dot(w,e)/dot(e,e),0.0,1.0); float d = dot(q,q); float s = gs*cro(w,e); res = vec4( (d<res.x) ? vec3(d,q) : res.xyz, (s>res.w) ? s : res.w ); } // edge 2 { vec2 e = v[0]-v[2]; vec2 w = p-v[2]; vec2 q = w-e*clamp(dot(w,e)/dot(e,e),0.0,1.0); float d = dot(q,q); float s = gs*cro(w,e); res = vec4( (d<res.x) ? vec3(d,q) : res.xyz, (s>res.w) ? s : res.w ); } // distance and sign float d = sqrt(res.x)*sign(res.w); return vec3(d,res.yz/d); }
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 vec3 sdgTriangle( in vec2 p, in vec2 v[3] ) {
1
1
wlccR2
butadiene
2021-01-11T04:43:22
// Description : GLSL 2D simplex noise function // Author : Ian McEwan, Ashima Arts // Maintainer : ijm // Lastmod : 20110822 (ijm) // License : // Copyright (C) 2011 Ashima Arts. All rights reserved. // Distributed under the MIT License. See LICENSE file. // https://github.com/ashima/webgl-noise // ////////////////////////////////////////////////////////////////////////////////////////////// // Some useful functions vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } vec2 mod289(vec2 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } vec3 permute(vec3 x) { return mod289(((x*34.0)+1.0)*x); } float snoise(vec2 v) { // Precompute values for skewed triangular grid const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0 0.366025403784439, // 0.5*(sqrt(3.0)-1.0) -0.577350269189626, // -1.0 + 2.0 * C.x 0.024390243902439); // 1.0 / 41.0 // First corner (x0) vec2 i = floor(v + dot(v, C.yy)); vec2 x0 = v - i + dot(i, C.xx); // Other two corners (x1, x2) vec2 i1 = vec2(0.0); i1 = (x0.x > x0.y)? vec2(1.0, 0.0):vec2(0.0, 1.0); vec2 x1 = x0.xy + C.xx - i1; vec2 x2 = x0.xy + C.zz; // Do some permutations to avoid // truncation effects in permutation i = mod289(i); vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0)) + i.x + vec3(0.0, i1.x, 1.0 )); vec3 m = max(0.5 - vec3( dot(x0,x0), dot(x1,x1), dot(x2,x2) ), 0.0); m = m*m ; m = m*m ; // Gradients: // 41 pts uniformly over a line, mapped onto a diamond // The ring size 17*17 = 289 is close to a multiple // of 41 (41*7 = 287) vec3 x = 2.0 * fract(p * C.www) - 1.0; vec3 h = abs(x) - 0.5; vec3 ox = floor(x + 0.5); vec3 a0 = x - ox; // Normalise gradients implicitly by scaling m // Approximation of: m *= inversesqrt(a0*a0 + h*h); m *= 1.79284291400159 - 0.85373472095314 * (a0*a0+h*h); // Compute final noise value at P vec3 g = vec3(0.0); g.x = a0.x * x0.x + h.x * x0.y; g.yz = a0.yz * vec2(x1.x,x2.x) + h.yz * vec2(x1.y,x2.y); return 130.0 * dot(m, g); } #define OCTAVES 2 // Ridged multifractal // See "Texturing & Modeling, A Procedural Approach", Chapter 12 float ridge(float h, float offset) { h = abs(h); // create creases h = offset - h; // invert so creases are at top h = h * h; // sharpen creases return h; } float ridgedMF(vec2 p) { float lacunarity = 2.0; float gain = 0.5; float offset = 0.9; float sum = 0.0; float freq = 1.0, amp = 0.5; float prev = 1.0; for(int i=0; i < OCTAVES; i++) { float n = ridge(snoise(p*freq), offset); sum += n*amp; sum += n*amp*prev; // scale by previous octave prev = n; freq *= lacunarity; amp *= gain; } return sum; } ///////////////////////////////////////////////////////////////////////////////////////////// float PI = 3.1415926535; vec3 MoonDirection = normalize(vec3(-0.5,0.4,-0.3)); vec3 MoonColor = vec3(0.6,0.7,1.2); float random (vec2 st) { return fract(sin(dot(st.xy, vec2(12.9898,78.233)))* 43758.5453123); } mat2 rot(float r){ return mat2(cos(r),sin(r),-sin(r),cos(r)); } vec4 dist(vec3 p){ //p.z *= 0.7; p.y -= 0.3; float d = 0.009; float no = ridgedMF(p.xz+0.3*snoise(p.xz+0.1*iTime)); vec3 col = vec3(1,1,1)*0.02*exp(-no*3.); float thredy = 0.5; float thx = p.y-thredy; vec3 highems = vec3(1.3,1.0,1.0)*max(thx*8.0*exp(-3.5*vec3(2.,1.2,1.5)*thx),0.); col *= highems; return vec4(col,d); } vec4 ground(vec3 p){ p.y -= 0.3; p.x -= -0.; float d = p.y - smoothstep(0.0,1.0,length(p.xz-vec2(-0.4,0.))*1.)*0.23*ridgedMF(vec2(0.9,1.)*(p.xz-vec2(-0.1,0.02*iTime))); //d = max(d,-(length(p-vec3(-0.4,0.65,-0.6))-0.8)); vec3 col = vec3(0); return vec4(col,d); } vec3 getnormal(vec3 p) { const vec2 e = vec2(0.5773,-0.5773)*0.0001; vec3 nor = normalize( e.xyy*ground(p+e.xyy).w + e.yyx*ground(p+e.yyx).w + e.yxy*ground(p+e.yxy).w + e.xxx*ground(p+e.xxx).w); nor = normalize(vec3(nor)); return nor ; } vec3 star(vec2 s){ vec3 c = vec3(snoise(s)); c = pow(c,vec3(5.)); c = clamp(7.*clamp(c-0.7,0.0,1.0),0.0,1000.0); return c; } vec3 background(vec3 rd){ vec2 rs = vec2(atan(length(rd.xy),rd.z),atan(rd.x,rd.y)); vec3 moon = 0.5*clamp(MoonColor*0.07/length(MoonDirection-rd),0.0,1.0); return moon+star(rs*50.)*vec3(0.5)+vec3(0.7,0.5,0.5)*star(rs*50.+20.)+vec3(0.5,0.5,0.7)*star(rs*50.+70.); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // Normalized pixel coordinates (from 0 to 1) vec2 uv = fragCoord/iResolution.xy; vec2 p = uv; p = 2.0*(p-0.5); p.x *= iResolution.x/iResolution.y; vec3 offset = vec3(0,-0.4 ,0); vec3 ro = vec3(0,0,0)-offset; vec3 ta = vec3(0,1.6,-2)-offset; vec3 cdir = normalize(ta-ro); vec3 side = cross(cdir,vec3(0,1,0)); vec3 up = cross(side,cdir); float fov = 0.4; vec3 rd = normalize(side*p.x+up*p.y+cdir*fov); //rd.xz *= rot(iTime); float d,t=0.; float gd = 0.; vec3 ac = vec3(0.); vec4 disres; float kset = 0.3; float sen = (1.0+1.5*pow(abs(sin(iTime*kset))*(1.0-fract(iTime*kset/(0.5*PI))),1.)); for(int i = 0;i<139;i++){ disres = 2.0*dist(ro+rd*t)*sen; d = disres.w; gd = 0.5*ground(ro+rd*t).w; d = min(gd,d); t += d; ac += disres.xyz; if((ro+rd*t).z<-1.5)break; } vec3 col = vec3(0.); col += ac; col += background(rd); if(gd<0.01){ vec3 sp = ro+rd*t; vec3 normal = getnormal(sp); float snk = 1.; vec3 cnormal = normal + 0.1*(vec3(random(snk*sp.yz),random(snk*sp.zx),random(snk*sp.xy))-0.5); cnormal = normalize(cnormal); col = 1.5*vec3(193,157,121)/255.*MoonColor*vec3(max(dot(cnormal,MoonDirection),0.)); col += MoonColor*0.02; ac = vec3(0.0); vec3 snormal; vec3 rrd; for(int i =0; i<8; i++){ snormal =normal + 1.0*(vec3(random(snk*sp.yz+float(i)*100.),random(snk*sp.zx+float(i)*100.),random(snk*sp.xy+float(i)*100.))-0.5); snormal = normalize(snormal); t = 0.4; ro = sp; rrd =snormal;// reflect(rd,snormal); for(int i = 0;i<10;i++){ disres = 6.0*dist(ro+rrd*t)*sen; d = disres.w; t += d; ac += disres.xyz; } } col += 0.1*ac; } // Output to screen col = pow(col,vec3(0.8)); fragColor = vec4(col,1.0); }
mit
[ 1, 439, 460, 460, 507 ]
[ [ 1, 439, 460, 460, 507 ], [ 508, 508, 529, 529, 576 ], [ 577, 577, 599, 599, 634 ], [ 636, 636, 658, 711, 2408 ], [ 2429, 2517, 2553, 2553, 2698 ], [ 2700, 2700, 2724, 2724, 3130 ], [ 3340, 3340, 3364, 3364, 3471 ], [ 3473, 3473, 3491, 3491, 3540 ], [ 3542, 3542, 3560, 3578, 3898 ], [ 3900, 3900, 3920, 3920, 4186 ], [ 4188, 4188, 4212, 4212, 4432 ], [ 4435, 4435, 4453, 4453, 4578 ], [ 4580, 4580, 4605, 4605, 4855 ], [ 4857, 4857, 4914, 4964, 6971 ] ]
// Description : GLSL 2D simplex noise function // Author : Ian McEwan, Ashima Arts // Maintainer : ijm // Lastmod : 20110822 (ijm) // License : // Copyright (C) 2011 Ashima Arts. All rights reserved. // Distributed under the MIT License. See LICENSE file. // https://github.com/ashima/webgl-noise // ////////////////////////////////////////////////////////////////////////////////////////////// // Some useful functions
vec3 mod289(vec3 x) {
return x - floor(x * (1.0 / 289.0)) * 289.0; }
// Description : GLSL 2D simplex noise function // Author : Ian McEwan, Ashima Arts // Maintainer : ijm // Lastmod : 20110822 (ijm) // License : // Copyright (C) 2011 Ashima Arts. All rights reserved. // Distributed under the MIT License. See LICENSE file. // https://github.com/ashima/webgl-noise // ////////////////////////////////////////////////////////////////////////////////////////////// // Some useful functions vec3 mod289(vec3 x) {
56
179
wlccR2
butadiene
2021-01-11T04:43:22
// Description : GLSL 2D simplex noise function // Author : Ian McEwan, Ashima Arts // Maintainer : ijm // Lastmod : 20110822 (ijm) // License : // Copyright (C) 2011 Ashima Arts. All rights reserved. // Distributed under the MIT License. See LICENSE file. // https://github.com/ashima/webgl-noise // ////////////////////////////////////////////////////////////////////////////////////////////// // Some useful functions vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } vec2 mod289(vec2 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } vec3 permute(vec3 x) { return mod289(((x*34.0)+1.0)*x); } float snoise(vec2 v) { // Precompute values for skewed triangular grid const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0 0.366025403784439, // 0.5*(sqrt(3.0)-1.0) -0.577350269189626, // -1.0 + 2.0 * C.x 0.024390243902439); // 1.0 / 41.0 // First corner (x0) vec2 i = floor(v + dot(v, C.yy)); vec2 x0 = v - i + dot(i, C.xx); // Other two corners (x1, x2) vec2 i1 = vec2(0.0); i1 = (x0.x > x0.y)? vec2(1.0, 0.0):vec2(0.0, 1.0); vec2 x1 = x0.xy + C.xx - i1; vec2 x2 = x0.xy + C.zz; // Do some permutations to avoid // truncation effects in permutation i = mod289(i); vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0)) + i.x + vec3(0.0, i1.x, 1.0 )); vec3 m = max(0.5 - vec3( dot(x0,x0), dot(x1,x1), dot(x2,x2) ), 0.0); m = m*m ; m = m*m ; // Gradients: // 41 pts uniformly over a line, mapped onto a diamond // The ring size 17*17 = 289 is close to a multiple // of 41 (41*7 = 287) vec3 x = 2.0 * fract(p * C.www) - 1.0; vec3 h = abs(x) - 0.5; vec3 ox = floor(x + 0.5); vec3 a0 = x - ox; // Normalise gradients implicitly by scaling m // Approximation of: m *= inversesqrt(a0*a0 + h*h); m *= 1.79284291400159 - 0.85373472095314 * (a0*a0+h*h); // Compute final noise value at P vec3 g = vec3(0.0); g.x = a0.x * x0.x + h.x * x0.y; g.yz = a0.yz * vec2(x1.x,x2.x) + h.yz * vec2(x1.y,x2.y); return 130.0 * dot(m, g); } #define OCTAVES 2 // Ridged multifractal // See "Texturing & Modeling, A Procedural Approach", Chapter 12 float ridge(float h, float offset) { h = abs(h); // create creases h = offset - h; // invert so creases are at top h = h * h; // sharpen creases return h; } float ridgedMF(vec2 p) { float lacunarity = 2.0; float gain = 0.5; float offset = 0.9; float sum = 0.0; float freq = 1.0, amp = 0.5; float prev = 1.0; for(int i=0; i < OCTAVES; i++) { float n = ridge(snoise(p*freq), offset); sum += n*amp; sum += n*amp*prev; // scale by previous octave prev = n; freq *= lacunarity; amp *= gain; } return sum; } ///////////////////////////////////////////////////////////////////////////////////////////// float PI = 3.1415926535; vec3 MoonDirection = normalize(vec3(-0.5,0.4,-0.3)); vec3 MoonColor = vec3(0.6,0.7,1.2); float random (vec2 st) { return fract(sin(dot(st.xy, vec2(12.9898,78.233)))* 43758.5453123); } mat2 rot(float r){ return mat2(cos(r),sin(r),-sin(r),cos(r)); } vec4 dist(vec3 p){ //p.z *= 0.7; p.y -= 0.3; float d = 0.009; float no = ridgedMF(p.xz+0.3*snoise(p.xz+0.1*iTime)); vec3 col = vec3(1,1,1)*0.02*exp(-no*3.); float thredy = 0.5; float thx = p.y-thredy; vec3 highems = vec3(1.3,1.0,1.0)*max(thx*8.0*exp(-3.5*vec3(2.,1.2,1.5)*thx),0.); col *= highems; return vec4(col,d); } vec4 ground(vec3 p){ p.y -= 0.3; p.x -= -0.; float d = p.y - smoothstep(0.0,1.0,length(p.xz-vec2(-0.4,0.))*1.)*0.23*ridgedMF(vec2(0.9,1.)*(p.xz-vec2(-0.1,0.02*iTime))); //d = max(d,-(length(p-vec3(-0.4,0.65,-0.6))-0.8)); vec3 col = vec3(0); return vec4(col,d); } vec3 getnormal(vec3 p) { const vec2 e = vec2(0.5773,-0.5773)*0.0001; vec3 nor = normalize( e.xyy*ground(p+e.xyy).w + e.yyx*ground(p+e.yyx).w + e.yxy*ground(p+e.yxy).w + e.xxx*ground(p+e.xxx).w); nor = normalize(vec3(nor)); return nor ; } vec3 star(vec2 s){ vec3 c = vec3(snoise(s)); c = pow(c,vec3(5.)); c = clamp(7.*clamp(c-0.7,0.0,1.0),0.0,1000.0); return c; } vec3 background(vec3 rd){ vec2 rs = vec2(atan(length(rd.xy),rd.z),atan(rd.x,rd.y)); vec3 moon = 0.5*clamp(MoonColor*0.07/length(MoonDirection-rd),0.0,1.0); return moon+star(rs*50.)*vec3(0.5)+vec3(0.7,0.5,0.5)*star(rs*50.+20.)+vec3(0.5,0.5,0.7)*star(rs*50.+70.); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // Normalized pixel coordinates (from 0 to 1) vec2 uv = fragCoord/iResolution.xy; vec2 p = uv; p = 2.0*(p-0.5); p.x *= iResolution.x/iResolution.y; vec3 offset = vec3(0,-0.4 ,0); vec3 ro = vec3(0,0,0)-offset; vec3 ta = vec3(0,1.6,-2)-offset; vec3 cdir = normalize(ta-ro); vec3 side = cross(cdir,vec3(0,1,0)); vec3 up = cross(side,cdir); float fov = 0.4; vec3 rd = normalize(side*p.x+up*p.y+cdir*fov); //rd.xz *= rot(iTime); float d,t=0.; float gd = 0.; vec3 ac = vec3(0.); vec4 disres; float kset = 0.3; float sen = (1.0+1.5*pow(abs(sin(iTime*kset))*(1.0-fract(iTime*kset/(0.5*PI))),1.)); for(int i = 0;i<139;i++){ disres = 2.0*dist(ro+rd*t)*sen; d = disres.w; gd = 0.5*ground(ro+rd*t).w; d = min(gd,d); t += d; ac += disres.xyz; if((ro+rd*t).z<-1.5)break; } vec3 col = vec3(0.); col += ac; col += background(rd); if(gd<0.01){ vec3 sp = ro+rd*t; vec3 normal = getnormal(sp); float snk = 1.; vec3 cnormal = normal + 0.1*(vec3(random(snk*sp.yz),random(snk*sp.zx),random(snk*sp.xy))-0.5); cnormal = normalize(cnormal); col = 1.5*vec3(193,157,121)/255.*MoonColor*vec3(max(dot(cnormal,MoonDirection),0.)); col += MoonColor*0.02; ac = vec3(0.0); vec3 snormal; vec3 rrd; for(int i =0; i<8; i++){ snormal =normal + 1.0*(vec3(random(snk*sp.yz+float(i)*100.),random(snk*sp.zx+float(i)*100.),random(snk*sp.xy+float(i)*100.))-0.5); snormal = normalize(snormal); t = 0.4; ro = sp; rrd =snormal;// reflect(rd,snormal); for(int i = 0;i<10;i++){ disres = 6.0*dist(ro+rrd*t)*sen; d = disres.w; t += d; ac += disres.xyz; } } col += 0.1*ac; } // Output to screen col = pow(col,vec3(0.8)); fragColor = vec4(col,1.0); }
mit
[ 2429, 2517, 2553, 2553, 2698 ]
[ [ 1, 439, 460, 460, 507 ], [ 508, 508, 529, 529, 576 ], [ 577, 577, 599, 599, 634 ], [ 636, 636, 658, 711, 2408 ], [ 2429, 2517, 2553, 2553, 2698 ], [ 2700, 2700, 2724, 2724, 3130 ], [ 3340, 3340, 3364, 3364, 3471 ], [ 3473, 3473, 3491, 3491, 3540 ], [ 3542, 3542, 3560, 3578, 3898 ], [ 3900, 3900, 3920, 3920, 4186 ], [ 4188, 4188, 4212, 4212, 4432 ], [ 4435, 4435, 4453, 4453, 4578 ], [ 4580, 4580, 4605, 4605, 4855 ], [ 4857, 4857, 4914, 4964, 6971 ] ]
// Ridged multifractal // See "Texturing & Modeling, A Procedural Approach", Chapter 12
float ridge(float h, float offset) {
h = abs(h); // create creases h = offset - h; // invert so creases are at top h = h * h; // sharpen creases return h; }
// Ridged multifractal // See "Texturing & Modeling, A Procedural Approach", Chapter 12 float ridge(float h, float offset) {
2
2
WtGXRc
iq
2021-01-20T05:53:42
// The MIT License // Copyright © 2020 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. // Signed distance and gradient to a circle art. // Probably faster than central differences or automatic // differentiation/dual numbers. // List of other 2D distances+gradients: // https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm // // Circle: https://www.shadertoy.com/view/WltSDj // Pie: https://www.shadertoy.com/view/3tGXRc // Arc: https://www.shadertoy.com/view/WtGXRc // Isosceles Triangle: https://www.shadertoy.com/view/3dyfDd // Triangle: https://www.shadertoy.com/view/tlVyWh // Box: https://www.shadertoy.com/view/wlcXD2 // Quad: https://www.shadertoy.com/view/WtVcD1 // Cross: https://www.shadertoy.com/view/WtdXWj // Segment: https://www.shadertoy.com/view/WtdSDj // Hexagon: https://www.shadertoy.com/view/WtySRc // Vesica: https://www.shadertoy.com/view/3lGXRc // Smooth-Minimum: https://www.shadertoy.com/view/tdGBDt // Parallelogram: https://www.shadertoy.com/view/sssGzX // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 // sca is the sin/cos of the orientation // scb is the sin/cos of the aperture vec3 sdgArc( in vec2 p, in vec2 sca, in vec2 scb, in float ra, in float rb ) { vec2 q = p; mat2 ma = mat2(sca.x,-sca.y,sca.y,sca.x); p = ma*p; float s = sign(p.x); p.x = abs(p.x); if( scb.y*p.x > scb.x*p.y ) { vec2 w = p - ra*scb; float d = length(w); return vec3( d-rb, vec2(s*w.x,w.y)*ma/d ); } else { float l = length(q); float w = l - ra; return vec3( abs(w)-rb, sign(w)*q/l ); } } #define AA 2 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y; #else vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y; #endif // animation float ta = 3.14*(0.5+0.5*cos(iTime*0.52+2.0)); float tb = 3.14*(0.5+0.5*cos(iTime*0.31+2.0)); float rb = 0.15*(0.5+0.5*cos(iTime*0.41+1.0)); // sdf(p) and gradient(sdf(p)) vec3 dg = sdgArc(p,vec2(sin(ta),cos(ta)),vec2(sin(tb),cos(tb)), 0.5, rb); float d = dg.x; vec2 g = dg.yz; // central differenes based gradient, for comparison // g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y); // coloring vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85); col *= 1.0 + vec3(0.5*g,0.0); //col = vec3(0.5+0.5*g,1.0); col *= 1.0 - 0.5*exp(-16.0*abs(d)); col *= 0.9 + 0.1*cos(150.0*d); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 2139, 2313, 2391, 2391, 2794 ]
[ [ 2139, 2313, 2391, 2391, 2794 ] ]
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 // sca is the sin/cos of the orientation // scb is the sin/cos of the aperture
vec3 sdgArc( in vec2 p, in vec2 sca, in vec2 scb, in float ra, in float rb ) {
vec2 q = p; mat2 ma = mat2(sca.x,-sca.y,sca.y,sca.x); p = ma*p; float s = sign(p.x); p.x = abs(p.x); if( scb.y*p.x > scb.x*p.y ) { vec2 w = p - ra*scb; float d = length(w); return vec3( d-rb, vec2(s*w.x,w.y)*ma/d ); } else { float l = length(q); float w = l - ra; return vec3( abs(w)-rb, sign(w)*q/l ); } }
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 // sca is the sin/cos of the orientation // scb is the sin/cos of the aperture vec3 sdgArc( in vec2 p, in vec2 sca, in vec2 scb, in float ra, in float rb ) {
1
1
WtVcD1
iq
2021-01-18T04:35:08
// The MIT License // Copyright © 2021 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. // Signed distance and gradient to a quad. Probably // faster than central differences or automatic // differentiation/dual numbers. // List of other 2D distances+gradients: // // https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm // // and // // https://www.shadertoy.com/playlist/M3dSRf // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 float cro( in vec2 a, in vec2 b ) { return a.x*b.y - a.y*b.x; } vec3 sdgQuad( in vec2 p, in vec2 v[4] ) { float gs = cro(v[0]-v[3],v[1]-v[0]); vec4 res; // edge 0 { vec2 e = v[1]-v[0]; vec2 w = p-v[0]; vec2 q = w-e*clamp(dot(w,e)/dot(e,e),0.0,1.0); float d = dot(q,q); float s = gs*cro(w,e); res = vec4(d,q,s); } // edge 1 { vec2 e = v[2]-v[1]; vec2 w = p-v[1]; vec2 q = w-e*clamp(dot(w,e)/dot(e,e),0.0,1.0); float d = dot(q,q); float s = gs*cro(w,e); res = vec4( (d<res.x) ? vec3(d,q) : res.xyz, (s>res.w) ? s : res.w ); } // edge 2 { vec2 e = v[3]-v[2]; vec2 w = p-v[2]; vec2 q = w-e*clamp(dot(w,e)/dot(e,e),0.0,1.0); float d = dot(q,q); float s = gs*cro(w,e); res = vec4( (d<res.x) ? vec3(d,q) : res.xyz, (s>res.w) ? s : res.w ); } // edge 3 { vec2 e = v[0]-v[3]; vec2 w = p-v[3]; vec2 q = w-e*clamp(dot(w,e)/dot(e,e),0.0,1.0); float d = dot(q,q); float s = gs*cro(w,e); res = vec4( (d<res.x) ? vec3(d,q) : res.xyz, (s>res.w) ? s : res.w ); } // distance and sign float d = sqrt(res.x)*sign(res.w); return vec3(d,res.yz/d); } #define AA 2 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y; #else vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y; #endif // animate float time = iTime; vec2 v[4] = vec2[4]( vec2(-0.9,-0.5) + 0.3*cos( 0.5*time + vec2(0.0,1.9) + 4.0 ), vec2( 0.9,-0.5) + 0.3*cos( 0.7*time + vec2(0.0,1.7) + 2.0 ), vec2( 0.9, 0.5) + 0.3*cos( 0.9*time + vec2(0.0,1.3) + 1.0 ), vec2(-0.9, 0.5) + 0.3*cos( 1.1*time + vec2(0.0,1.5) + 0.0 ) ); // corner radious float ra = 0.1*(0.5+0.5*sin(iTime*1.2)); // sdf(p) and gradient(sdf(p)) vec3 dg = sdgQuad(p,v); float d = dg.x-ra; vec2 g = dg.yz; // central differenes based gradient, for comparison // g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y); // coloring vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85); col *= 1.0 + vec3(0.5*g,0.0); //col = vec3(0.5+0.5*g,1.0); col *= 1.0 - 0.7*exp(-8.0*abs(d)); col *= 0.9 + 0.1*cos(150.0*d); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 1401, 1496, 1531, 1531, 1559 ]
[ [ 1401, 1496, 1531, 1531, 1559 ], [ 1560, 1560, 1601, 1601, 2796 ] ]
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1
float cro( in vec2 a, in vec2 b ) {
return a.x*b.y - a.y*b.x; }
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 float cro( in vec2 a, in vec2 b ) {
8
8
3lcfR8
iq
2021-02-01T13:38:00
// The MIT License // Copyright © 2021 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. // Signed distance and gradient to an ellipse. Probably // faster than central differences or automatic // differentiation/dual numbers. // // It uses 4 iterations of Newton's root solver, but could need more for // very eccentric ellipses (see line 46). For an analytic solver see // https://www.shadertoy.com/view/4sS3zz // List of other 2D distances+gradients: // https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm // // Circle: https://www.shadertoy.com/view/WltSDj // Pie: https://www.shadertoy.com/view/3tGXRc // Arc: https://www.shadertoy.com/view/WtGXRc // Isosceles Triangle: https://www.shadertoy.com/view/3dyfDd // Triangle: https://www.shadertoy.com/view/tlVyWh // Box: https://www.shadertoy.com/view/wlcXD2 // Quad: https://www.shadertoy.com/view/WtVcD1 // Cross: https://www.shadertoy.com/view/WtdXWj // Segment: https://www.shadertoy.com/view/WtdSDj // Hexagon: https://www.shadertoy.com/view/WtySRc // Vesica: https://www.shadertoy.com/view/3lGXRc // Ellipse: https://www.shadertoy.com/view/3lcfR8 // Smooth-Minimum: https://www.shadertoy.com/view/tdGBDt // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 vec3 sdgEllipse( vec2 p, in vec2 ab ) { // symmetry vec2 sp = sign(p); p = abs( p ); // determine in/out and initial value bool s = dot(p/ab,p/ab)>1.0; float w = atan(p.y*ab.x, p.x*ab.y); if(!s) w=(ab.x*(p.x-ab.x)<ab.y*(p.y-ab.y))? 1.570796327 : 0.0; // Newton root solver for( int i=0; i<4; i++ ) { vec2 cs = vec2(cos(w),sin(w)); vec2 u = ab*vec2( cs.x,cs.y); vec2 v = ab*vec2(-cs.y,cs.x); w = w + dot(p-u,v)/(dot(p-u,u)+dot(v,v)); } vec2 q = ab*vec2(cos(w),sin(w)); // compute distance and gradient (everything above // could probably be replaced by something better) float d = length(p-q); return vec3( d, sp*(p-q)/d ) * (s?1.0:-1.0); } #define AA 2 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y; #else vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y; #endif // animation vec2 ab = 0.7 + vec2(0.4,0.2)*cos(iTime*0.52+vec2(1,2)); // sdf(p) and gradient(sdf(p)) vec3 dg = sdgEllipse(p,ab); float d = dg.x; vec2 g = dg.yz; // central differenes based gradient, for comparison //g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y); // coloring vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85); col *= 1.0 + vec3(0.5*g,0.0); //col = vec3(0.5+0.5*g,1.0); col *= 1.0 - 0.5*exp(-16.0*abs(d)); col *= 0.9 + 0.1*cos(150.0*d); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) ); // draw gradient discontinuty if( sin(3.1415927*iTime)>0.0 ) { float f2 = ab.x*ab.x - ab.y*ab.y; if( ab.x>ab.y ) { float foc = f2/ab.x; p.x -= clamp(p.x,-foc,foc); } else { float foc = -f2/ab.y; p.y -= clamp(p.y,-foc,foc); } d = length(p); col = mix( col, vec3(1.0), 1.0-smoothstep(0.005,0.010,abs(d)) ); } tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 2323, 2418, 2457, 2473, 3159 ]
[ [ 2323, 2418, 2457, 2473, 3159 ] ]
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgEllipse( vec2 p, in vec2 ab ) {
vec2 sp = sign(p); p = abs( p ); // determine in/out and initial value bool s = dot(p/ab,p/ab)>1.0; float w = atan(p.y*ab.x, p.x*ab.y); if(!s) w=(ab.x*(p.x-ab.x)<ab.y*(p.y-ab.y))? 1.570796327 : 0.0; // Newton root solver for( int i=0; i<4; i++ ) { vec2 cs = vec2(cos(w),sin(w)); vec2 u = ab*vec2( cs.x,cs.y); vec2 v = ab*vec2(-cs.y,cs.x); w = w + dot(p-u,v)/(dot(p-u,u)+dot(v,v)); } vec2 q = ab*vec2(cos(w),sin(w)); // compute distance and gradient (everything above // could probably be replaced by something better) float d = length(p-q); return vec3( d, sp*(p-q)/d ) * (s?1.0:-1.0); }
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 vec3 sdgEllipse( vec2 p, in vec2 ab ) {
1
1
wlyBWm
PauloFalcao
2021-02-27T18:14:26
// Smooth Repetition // by @paulofalcao // // CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/ // // Twitter: @paulofalcao // https://twitter.com/paulofalcao/status/1365726720695934979 // // YouTube playing with this and Material Maker // https://www.youtube.com/watch?v=HoAQ7DFRzQE // // I was using smooth abs p=sqrt(p*p+a) introduced by omeometo // at https://shadertoy.com/view/wljXzh // and iteratively doing smooth abs and translations // the number of objects is exponencial // // But it's possible to use asin(sin(x)*S) with S between 0 and 1 // like blackle said in the comments! // Creates infinite repetitions and it's even faster! :) // Change asin_sin_mode to true to use this mode (this is now the default mode) // // Using IQ "Raymarching - Primitives" as sandbox // https://www.shadertoy.com/view/Xds3zN // #define asin_sin_mode true //Change asin_sin_mode to true to use this mode (default) //blackle mode asin(sin(x)*S) with S between 0 and 1 (higher values less smooth) vec2 smoothrepeat_asin_sin(vec2 p,float smooth_size,float size){ p/=size; p=asin(sin(p)*(1.0-smooth_size)); return p*size; } //Change asin_sin_mode to false to use this mode //6 iterations create 2^6 objects for each axis #define smoothrepeat_iterations 6 vec2 smoothrepeat(vec2 p,float smooth_size,float size){ size/=2.0; float w=pow(2.0,float(smoothrepeat_iterations)); for(int i=0;i<smoothrepeat_iterations;i++){ p=sqrt(p*p+smooth_size);//smooth abs p-=size*w;//translate w=w/2.0; } return p; } // // The code from now on is the same as IQ "Raymarching - Primitives" // with minor modifications and different map function // https://www.shadertoy.com/view/Xds3zN // #if HW_PERFORMANCE==1 #define AA 1 #else #define AA 2 // make this 2 or 3 for antialiasing #endif //------------------------------------------------------------------ // PRIMITIVES //------------------------------------------------------------------ float sdPlane( vec3 p ) { return p.y; } float sdRoundBox( vec3 p, vec3 b, float r ) { vec3 q = abs(p) - b; return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) - r; } float sdTorus( vec3 p, vec2 t ) { return length( vec2(length(p.xz)-t.x,p.y) )-t.y; } //------------------------------------------------------------------ vec2 opU( vec2 d1, vec2 d2 ) { return (d1.x<d2.x) ? d1 : d2; } vec2 rot(vec2 p, float r) { float s=sin(r);float c=cos(r); return p*mat2(c,-s,s,c); } //------------------------------------------------------------------ #define ZERO (min(iFrame,0)) //------------------------------------------------------------------ vec2 map( in vec3 pos ) { vec2 res = vec2( 1e10, 0.0 ); { float sm=(smoothstep(0.0,1.0,sin(iTime)+0.5)-0.5)*0.01+0.005; float dist=sin(iTime*0.35)*0.2+0.3; if (asin_sin_mode){ pos.xz=smoothrepeat_asin_sin(pos.xz,sm*10.0,dist); } else { pos.xz=smoothrepeat(pos.xz,sm,dist); } pos.xz=rot(pos.xz,sin(iTime*0.5)); pos.xy=rot(pos.xy,sin(iTime*0.7)*0.4); pos.yz=rot(pos.yz,sin(iTime)*0.3); pos-=vec3(0.0,0.2, 0.0); float b=sdRoundBox( pos, vec3(0.4,0.02,0.1),0.02); res = opU( res, vec2(b ,1.5) ); } return res; } vec2 raycast( in vec3 ro, in vec3 rd ) { vec2 res = vec2(-1.0,-1.0); float tmin = 0.1; float tmax = 20.0; // raytrace floor plane float tp1 = (0.0-ro.y)/rd.y; if( tp1>0.0 ) { tmax = min( tmax, tp1 ); res = vec2( tp1, 1.0 ); } //else return res; float t = tmin; for( int i=0; i<70 && t<tmax; i++ ) { vec2 h = map( ro+rd*t ); if( abs(h.x)<(0.0001*t) ) { res = vec2(t,h.y); break; } t += h.x; } return res; } // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax ) { // bounding volume float tp = (0.8-ro.y)/rd.y; if( tp>0.0 ) tmax = min( tmax, tp ); float res = 1.0; float t = mint; for( int i=ZERO; i<24; i++ ) { float h = map( ro + rd*t ).x; float s = clamp(8.0*h/t,0.0,1.0); res = min( res, s*s*(3.0-2.0*s) ); t += clamp( h, 0.02, 0.2 ); if( res<0.004 || t>tmax ) break; } return clamp( res, 0.0, 1.0 ); } // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm vec3 calcNormal( in vec3 pos ) { #if 0 vec2 e = vec2(1.0,-1.0)*0.5773*0.0005; return normalize( e.xyy*map( pos + e.xyy ).x + e.yyx*map( pos + e.yyx ).x + e.yxy*map( pos + e.yxy ).x + e.xxx*map( pos + e.xxx ).x ); #else // inspired by tdhooper and klems - a way to prevent the compiler from inlining map() 4 times vec3 n = vec3(0.0); for( int i=ZERO; i<4; i++ ) { vec3 e = 0.5773*(2.0*vec3((((i+3)>>1)&1),((i>>1)&1),(i&1))-1.0); n += e*map(pos+0.0005*e).x; //if( n.x+n.y+n.z>100.0 ) break; } return normalize(n); #endif } float calcAO( in vec3 pos, in vec3 nor ) { float occ = 0.0; float sca = 1.0; for( int i=ZERO; i<5; i++ ) { float h = 0.01 + 0.12*float(i)/4.0; float d = map( pos + h*nor ).x; occ += (h-d)*sca; sca *= 0.95; if( occ>0.35 ) break; } return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) * (0.5+0.5*nor.y); } // http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm float checkersGradBox( in vec2 p, in vec2 dpdx, in vec2 dpdy ) { // filter kernel vec2 w = abs(dpdx)+abs(dpdy) + 0.001; // analytical integral (box filter) vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w; // xor pattern return 0.5 - 0.5*i.x*i.y; } vec3 render( in vec3 ro, in vec3 rd, in vec3 rdx, in vec3 rdy ) { // background vec3 col = vec3(0.7, 0.7, 0.9) - max(rd.y,0.0)*0.3; // raycast scene vec2 res = raycast(ro,rd); float t = res.x; float m = res.y; if( m>-0.5 ) { vec3 pos = ro + t*rd; vec3 nor = (m<1.5) ? vec3(0.0,1.0,0.0) : calcNormal( pos ); vec3 ref = reflect( rd, nor ); // material col = 0.2 + 0.2*sin( m*2.0 + vec3(0.0,1.0,2.0) ); float ks = 1.0; if( m<1.5 ) { // project pixel footprint into the plane vec3 dpdx = ro.y*(rd/rd.y-rdx/rdx.y); vec3 dpdy = ro.y*(rd/rd.y-rdy/rdy.y); float f = checkersGradBox( 3.0*pos.xz, 3.0*dpdx.xz, 3.0*dpdy.xz ); col = 0.15 + f*vec3(0.05); ks = 0.4; } // lighting float occ = calcAO( pos, nor ); vec3 lin = vec3(0.0); // sun { vec3 lig = normalize( vec3(-0.5, 0.4, -0.6) ); vec3 hal = normalize( lig-rd ); float dif = clamp( dot( nor, lig ), 0.0, 1.0 ); //if( dif>0.0001 ) dif *= calcSoftshadow( pos, lig, 0.02, 2.5 ); float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0); spe *= dif; spe *= 0.04+0.96*pow(clamp(1.0-dot(hal,lig),0.0,1.0),5.0); lin += col*2.20*dif*vec3(1.30,1.00,0.70); lin += 10.00*spe*vec3(1.30,1.00,0.70)*ks; } // sky { float dif = sqrt(clamp( 0.5+0.5*nor.y, 0.0, 1.0 )); dif *= occ; float spe = smoothstep( -0.2, 0.2, ref.y ); spe *= dif; spe *= 0.04+0.96*pow(clamp(1.0+dot(nor,rd),0.0,1.0), 5.0 ); //if( spe>0.001 ) spe *= calcSoftshadow( pos, ref, 0.02, 2.5 ); lin += col*0.60*dif*vec3(0.40,0.60,1.15); lin += 2.00*spe*vec3(0.40,0.60,1.30)*ks; } // back { float dif = clamp( dot( nor, normalize(vec3(0.5,0.0,0.6))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0); dif *= occ; lin += col*0.55*dif*vec3(0.25,0.25,0.25); } // sss { float dif = pow(clamp(1.0+dot(nor,rd),0.0,1.0),2.0); dif *= occ; lin += col*0.25*dif*vec3(1.00,1.00,1.00); } col = lin; col = mix( col, vec3(0.7,0.7,0.9), 1.0-exp( -0.001*t*t*t ) ); } return vec3( clamp(col,0.0,1.0) ); } mat3 setCamera( in vec3 ro, in vec3 ta, float cr ) { vec3 cw = normalize(ta-ro); vec3 cp = vec3(sin(cr), cos(cr),0.0); vec3 cu = normalize( cross(cw,cp) ); vec3 cv = ( cross(cu,cw) ); return mat3( cu, cv, cw ); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 mo = iMouse.xy/iResolution.xy; float time = 32.0 + iTime*1.5; // camera vec3 ta = vec3( 0, 0, 0 ); vec3 ro = ta + vec3( 4.5*cos(0.1*time + 7.0*mo.x), 1.0 + 4.0*mo.y, 4.5*sin(0.1*time + 7.0*mo.x) ); // camera-to-world transformation mat3 ca = setCamera( ro, ta, 0.0 ); vec3 tot = vec3(0.0); #if AA>1 for( int m=ZERO; m<AA; m++ ) for( int n=ZERO; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; #else vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; #endif // focal length const float fl = 2.5; // ray direction vec3 rd = ca * normalize( vec3(p,fl) ); // ray differentials vec2 px = (2.0*(fragCoord+vec2(1.0,0.0))-iResolution.xy)/iResolution.y; vec2 py = (2.0*(fragCoord+vec2(0.0,1.0))-iResolution.xy)/iResolution.y; vec3 rdx = ca * normalize( vec3(px,fl) ); vec3 rdy = ca * normalize( vec3(py,fl) ); // render vec3 col = render( ro, rd, rdx, rdy ); // gain // col = col*3.0/(2.5+col); // gamma col = pow( col, vec3(0.4545) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
cc0-1.0
[ 873, 1012, 1076, 1076, 1148 ]
[ [ 873, 1012, 1076, 1076, 1148 ], [ 1281, 1281, 1336, 1336, 1545 ], [ 1974, 1974, 1999, 1999, 2014 ], [ 2016, 2016, 2061, 2061, 2152 ], [ 2154, 2154, 2187, 2187, 2242 ], [ 2315, 2315, 2345, 2345, 2378 ], [ 2380, 2380, 2407, 2407, 2467 ], [ 2639, 2639, 2664, 2664, 3279 ], [ 3281, 3281, 3321, 3321, 3837 ], [ 3839, 3901, 3979, 4002, 4389 ], [ 4391, 4455, 4487, 4487, 5057 ], [ 5059, 5059, 5101, 5101, 5410 ], [ 5412, 5488, 5552, 5573, 5806 ], [ 5808, 5808, 5873, 5892, 8372 ], [ 8374, 8374, 8426, 8426, 8603 ] ]
//Change asin_sin_mode to true to use this mode (default) //blackle mode asin(sin(x)*S) with S between 0 and 1 (higher values less smooth)
vec2 smoothrepeat_asin_sin(vec2 p,float smooth_size,float size){
p/=size; p=asin(sin(p)*(1.0-smooth_size)); return p*size; }
//Change asin_sin_mode to true to use this mode (default) //blackle mode asin(sin(x)*S) with S between 0 and 1 (higher values less smooth) vec2 smoothrepeat_asin_sin(vec2 p,float smooth_size,float size){
2
2
wtcfzM
iq
2021-02-04T12:38:04
// The MIT License // Copyright © 2021 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. // sdgTorus() returns the torus SDF and its gradient, by // computing it analytically. This means the normal to // the torus surface can be used during the raymarch loop // rather inexpensivelly (compared to sampling the SDF // multiple times to evaluate a normal for it) // Other SDF analytic gradients: // // Torus: https://www.shadertoy.com/view/wtcfzM // Capsule: https://www.shadertoy.com/view/WttfR7 // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .w = ∂f(p)/∂z // .yzw = ∇f(p) with ‖∇f(p)‖ = 1 vec4 sdgTorus( vec3 p, float ra, float rb ) { float h = length(p.xz); return vec4( length(vec2(h-ra,p.y))-rb, normalize(p*vec3(h-ra,h,h-ra)) ); } #define AA 3 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // camera movement float an = 0.5*(iTime-10.0); vec3 ro = 1.2*vec3( 1.0*cos(an), 0.65, 1.0*sin(an) ); vec3 ta = vec3( 0.0, -0.15, 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)); // animate torus float ra = 0.5; float rb = 0.2+0.1*sin(iTime); // render vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; #else vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; #endif // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raymarch const float tmax = 5.0; float t = 0.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; float h = sdgTorus(pos,ra,rb).x; if( h<0.0001 || t>tmax ) break; t += h; } // shading/lighting vec3 col = vec3(0.0); if( t<tmax ) { vec3 pos = ro + t*rd; vec3 nor = sdgTorus(pos,ra,rb).yzw; // compute normal numerically, for comparison // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm #if 0 const vec2 e = vec2(1,-1); const float eps = 0.0002; nor = normalize( e.xyy*sdgTorus( pos + e.xyy*eps, ra, rb ).x + e.yyx*sdgTorus( pos + e.yyx*eps, ra, rb ).x + e.yxy*sdgTorus( pos + e.yxy*eps, ra, rb ).x + e.xxx*sdgTorus( pos + e.xxx*eps, ra, rb ).x ); #endif float dif = clamp( dot(nor,vec3(0.57703)), 0.0, 1.0 ); float amb = 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0)); col = vec3(0.2,0.3,0.4)*amb + vec3(0.85,0.75,0.65)*dif; col *= (0.5+0.5*nor)*(0.5+0.5*nor); } // gamma col = sqrt( col ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 1490, 1607, 1652, 1652, 1777 ]
[ [ 1490, 1607, 1652, 1652, 1777 ] ]
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .w = ∂f(p)/∂z // .yzw = ∇f(p) with ‖∇f(p)‖ = 1
vec4 sdgTorus( vec3 p, float ra, float rb ) {
float h = length(p.xz); return vec4( length(vec2(h-ra,p.y))-rb, normalize(p*vec3(h-ra,h,h-ra)) ); }
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .w = ∂f(p)/∂z // .yzw = ∇f(p) with ‖∇f(p)‖ = 1 vec4 sdgTorus( vec3 p, float ra, float rb ) {
2
2
WttfR7
iq
2021-02-04T12:39:44
// The MIT License // Copyright © 2021 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. // sdgSegment() returns the segments/capsule SDF and its // analytical gradient. This means the normal to the // capsule's surface can be used during the raymarch loop // rather inexpensivelly (compared to sampling the SDF // multiple times to evaluate a normal for it) // Other SDF analytic gradients: // // Torus: https://www.shadertoy.com/view/wtcfzM // Capsule: https://www.shadertoy.com/view/WttfR7 // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .w = ∂f(p)/∂z // .yzw = ∇f(p) with ‖∇f(p)‖ = 1 vec4 sdgSegment( vec3 p, vec3 a, vec3 b, float r ) { vec3 ba = b-a; vec3 pa = p-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); vec3 q = pa-h*ba; float d = length(q); return vec4(d-r,q/d); } #define AA 3 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // camera movement float an = 0.5*(iTime-10.0); vec3 ro = 1.2*vec3( 1.0*cos(an), 0.65, 1.0*sin(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)); // animate torus vec3 pa = vec3(-0.5,-0.4,-0.3); vec3 pb = vec3( 0.2, 0.4, 0.1); float ra = 0.25+0.2*sin(iTime); // render vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; #else vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; #endif // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raymarch const float tmax = 5.0; float t = 0.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; float h = sdgSegment(pos,pa,pb,ra).x; if( h<0.0001 || t>tmax ) break; t += h; } // shading/lighting vec3 col = vec3(0.0); if( t<tmax ) { vec3 pos = ro + t*rd; vec3 nor = sdgSegment(pos,pa,pb,ra).yzw; // compute normal numerically, for comparison // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm #if 0 const vec2 e = vec2(1,-1); const float eps = 0.0002; nor = normalize( e.xyy*sdgSegment( pos + e.xyy*eps, pa, pb, ra ).x + e.yyx*sdgSegment( pos + e.yyx*eps, pa, pb, ra ).x + e.yxy*sdgSegment( pos + e.yxy*eps, pa, pb, ra ).x + e.xxx*sdgSegment( pos + e.xxx*eps, pa, pb, ra ).x ); #endif float dif = clamp( dot(nor,vec3(0.57703)), 0.0, 1.0 ); float amb = 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0)); col = vec3(0.2,0.3,0.4)*amb + vec3(0.85,0.75,0.65)*dif; col *= (0.5+0.5*nor)*(0.5+0.5*nor); } // gamma col = sqrt( col ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 1488, 1605, 1657, 1657, 1831 ]
[ [ 1488, 1605, 1657, 1657, 1831 ] ]
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .w = ∂f(p)/∂z // .yzw = ∇f(p) with ‖∇f(p)‖ = 1
vec4 sdgSegment( vec3 p, vec3 a, vec3 b, float r ) {
vec3 ba = b-a; vec3 pa = p-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); vec3 q = pa-h*ba; float d = length(q); return vec4(d-r,q/d); }
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .w = ∂f(p)/∂z // .yzw = ∇f(p) with ‖∇f(p)‖ = 1 vec4 sdgSegment( vec3 p, vec3 a, vec3 b, float r ) {
1
1
7dlGRf
iq
2021-03-22T09:36:51
// The MIT License // Copyright © 2021 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. // Distance to a parallelogram, I implemented three methods: // Method 1: computed by two edges, by symmetry, single square root // Method 2: computed by interior/exterior, optimization of Pentan's idea // Method 3: computed by zones #define METHOD 1 // List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf // // and www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm // signed distance to a 2D parallelogram (width, height, skew) float sdParallelogram_1( in vec2 p, float wi, float he, float sk ) { vec2 e = vec2(sk,he); float e2 = sk*sk + he*he; p = (p.y<0.0)?-p:p; // horizontal edge vec2 w = p - e; w.x -= clamp(w.x,-wi,wi); vec2 d = vec2(dot(w,w), -w.y); // vertical edge float s = p.x*e.y - p.y*e.x; p = (s<0.0)?-p:p; vec2 v = p - vec2(wi,0); v -= e*clamp(dot(v,e)/e2,-1.0,1.0); d = min( d, vec2(dot(v,v), wi*he-abs(s))); return sqrt(d.x)*sign(-d.y); } float sdParallelogram_2( in vec2 p, float wi, float he, float sk ) { vec2 e = vec2(sk,he); float e2 = sk*sk + he*he; float da = abs(p.x*e.y-p.y*e.x)-wi*he; float db = abs(p.y)-e.y; if( max(da,db)<0.0 ) // interior { return max( da*inversesqrt(e2), db ); } else // exterior { float f = clamp(p.y/e.y,-1.0,1.0); float g = clamp(p.x-e.x*f, -wi, wi); float h = clamp(((p.x-g)*e.x+p.y*e.y)/e2,-1.0,1.0); return length(p-vec2(g+e.x*h,e.y*h)); } } float sdParallelogram_3( in vec2 p, float wi, float he, float sk ) { // above float db = abs(p.y)-he; if( db>0.0 && abs(p.x-sk*sign(p.y))<wi ) return db; // inside float e2 = sk*sk + he*he; float h = p.x*he - p.y*sk; float da = (abs(h)-wi*he)*inversesqrt(e2); if( da<0.0 && db<0.0 ) return max( da, db ); // sides vec2 q = (h<0.0)?-p:p; q.x -= wi; float v = abs(q.x*sk+q.y*he); if( v<e2 ) return da; // exterior return sqrt( dot(q,q)+e2-2.0*v ); } float sdParallelogram( in vec2 p, float wi, float he, float sk ) { #if METHOD==1 return sdParallelogram_1(p,wi,he,sk); #endif #if METHOD==2 return sdParallelogram_2(p,wi,he,sk); #endif #if METHOD==3 return sdParallelogram_3(p,wi,he,sk); #endif } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y; // animate float sk = 0.5*sin(iTime); //p.x -= sk; // enable to lock base in place // distance float d = sdParallelogram(p,0.4,0.6,sk); // colorize vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7); col *= 1.0 - exp(-4.0*abs(d)); col *= 0.8 + 0.2*cos(120.0*d); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) ); if( iMouse.z>0.001 ) { d = sdParallelogram(m,0.4,0.6,sk); col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0025)); col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-m)-0.015)); } fragColor = vec4(col,1.0); }
mit
[ 1491, 1554, 1622, 1622, 2035 ]
[ [ 1491, 1554, 1622, 1622, 2035 ], [ 2037, 2037, 2105, 2105, 2572 ], [ 2574, 2574, 2642, 2655, 3117 ], [ 3119, 3119, 3185, 3185, 3400 ], [ 3402, 3402, 3459, 3459, 4236 ] ]
// signed distance to a 2D parallelogram (width, height, skew)
float sdParallelogram_1( in vec2 p, float wi, float he, float sk ) {
vec2 e = vec2(sk,he); float e2 = sk*sk + he*he; p = (p.y<0.0)?-p:p; // horizontal edge vec2 w = p - e; w.x -= clamp(w.x,-wi,wi); vec2 d = vec2(dot(w,w), -w.y); // vertical edge float s = p.x*e.y - p.y*e.x; p = (s<0.0)?-p:p; vec2 v = p - vec2(wi,0); v -= e*clamp(dot(v,e)/e2,-1.0,1.0); d = min( d, vec2(dot(v,v), wi*he-abs(s))); return sqrt(d.x)*sign(-d.y); }
// signed distance to a 2D parallelogram (width, height, skew) float sdParallelogram_1( in vec2 p, float wi, float he, float sk ) {
1
1
sssGzX
iq
2021-03-22T18:46:06
// The MIT License // Copyright © 2021 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. // Signed distance and gradient to a parallelogram. Probably // faster than central differences or automatic differentiation/dual // numbers. // List of other 2D distances+gradients: // // https://iquilezles.org/www/articles/distgradfunctions2d/distgradfunctions2d.htm // // and // // https://www.shadertoy.com/playlist/M3dSRf // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 vec3 sdgParallelogram( in vec2 p, float wi, float he, float sk ) { vec2 e = vec2(sk,he); float v = 1.0; if( p.y<0.0 ) { p=-p;v=-v;} // horizontal edge vec2 w = p - e; w.x -= clamp(w.x,-wi,wi); vec4 dsg = vec4(dot(w,w),v*w,w.y); // vertical edge float s = p.x*e.y - p.y*e.x; if( s<0.0 ) { p=-p; v=-v; } vec2 q = p - vec2(wi,0); q -= e*clamp(dot(q,e)/dot(e,e),-1.0,1.0); float d = dot(q,q); s = abs(s) - wi*he; dsg = vec4( (d<dsg.x) ? vec3(d,v*q) : dsg.xyz, (s>dsg.w) ? s : dsg.w ); // signed distance d = sqrt(dsg.x)*sign(dsg.w); // and gradient return vec3(d,dsg.yz/d); } #define AA 2 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; #else vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; #endif // animate float time = iTime; // sdf(p) and gradient(sdf(p)) float s = sin(iTime); vec3 dg = sdgParallelogram(p,0.4, 0.6,s); float d = dg.x; vec2 g = dg.yz; // central differenes based gradient, for comparison // g = vec2(dFdx(d),dFdy(d))/(2.0/iResolution.y); // coloring vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85); col *= 1.0 + vec3(0.5*g,0.0); //col = vec3(0.5+0.5*g,1.0); col *= 1.0 - 0.5*exp(-16.0*abs(d)); col *= 0.9 + 0.1*cos(150.0*d); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 1408, 1503, 1569, 1569, 2183 ]
[ [ 1408, 1503, 1569, 1569, 2183 ] ]
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgParallelogram( in vec2 p, float wi, float he, float sk ) {
vec2 e = vec2(sk,he); float v = 1.0; if( p.y<0.0 ) { p=-p;v=-v;} // horizontal edge vec2 w = p - e; w.x -= clamp(w.x,-wi,wi); vec4 dsg = vec4(dot(w,w),v*w,w.y); // vertical edge float s = p.x*e.y - p.y*e.x; if( s<0.0 ) { p=-p; v=-v; } vec2 q = p - vec2(wi,0); q -= e*clamp(dot(q,e)/dot(e,e),-1.0,1.0); float d = dot(q,q); s = abs(s) - wi*he; dsg = vec4( (d<dsg.x) ? vec3(d,v*q) : dsg.xyz, (s>dsg.w) ? s : dsg.w ); // signed distance d = sqrt(dsg.x)*sign(dsg.w); // and gradient return vec3(d,dsg.yz/d); }
// .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .yz = ∇f(p) with ‖∇f(p)‖ = 1 vec3 sdgParallelogram( in vec2 p, float wi, float he, float sk ) {
1
1
WlGfWc
jorge2017a1
2021-03-07T21:16:49
//Modificado por jorge2017a1 ----jorgeFloresP //Referencia de IQ https://www.shadertoy.com/view/wdBXRW // The MIT License // Copyright © 2019 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. // Distance to a regular pentagon, without trigonometric functions. // // // http://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm #define Rot(a) mat2(cos(a),-sin(a),sin(a),cos(a)) #define antialiasing(n) n/min(iResolution.y,iResolution.x) #define S(d,b) smoothstep(antialiasing(1.0),b,d) #define DF(a,b) length(a) * cos( mod( atan(a.y,a.x)+6.28/(b*8.0), 6.28/((b*8.0)*0.5))+(b-1.)*6.28/(b*8.0) + vec2(0,11) ) #define opU2(d1, d2) ( d1.x < d2.x ? d1 : d2 ) #define saturate(x) clamp(x, 0.0, 1.0) #define R iResolution.xy #define ss(a, b, t) smoothstep(a, b, t) #define SS(U) smoothstep(3./R.y,0.,U) float opU( float d1, float d2 ) { return min(d1,d2); } float opS( float d1, float d2 ) { return max(-d1,d2); } float opI( float d1, float d2 ) { return max(d1,d2); } //----------oPeraciones de Repeticion float opRep1D( float p, float c ) { float q = mod(p+0.5*c,c)-0.5*c; return q ;} //---------- float sdBox( in vec2 p, in vec2 b ) { vec2 d = abs(p)-b; return length(max(d,0.0)) + min(max(d.x,d.y),0.0); } float sdCircle( vec2 p, float r ) { return length(p) - r; } ////------------------- float dot2( in vec2 v ) { return dot(v,v); } float cross2d( in vec2 v0, in vec2 v1) { return v0.x*v1.y - v0.y*v1.x; } const int N1 =12; float sdPolygon( in vec2 p, in vec2[N1] v ) { const int num = v.length(); float d = dot(p-v[0],p-v[0]); float s = 1.0; for( int i=0, j=num-1; i<num; j=i, i++ ) { // distance vec2 e = v[j] - v[i]; vec2 w = p - v[i]; vec2 b = w - e*clamp( dot(w,e)/dot(e,e), 0.0, 1.0 ); d = min( d, dot(b,b) ); // winding number from http://geomalgorithms.com/a03-_inclusion.html bvec3 cond = bvec3( p.y>=v[i].y, p.y <v[j].y, e.x*w.y>e.y*w.x ); if( all(cond) || all(not(cond)) ) s=-s; } return s*sqrt(d); } //vec2[] polygon = vec2[](v0,v1,v2,v3,v4); vec2 pt1[12]=vec2[]( vec2(.32,.56), vec2(.24,.32), vec2(.34,.39), vec2(.4,.32), vec2(.43,.39), vec2(.47,.33), vec2(.49,.41), vec2(.55,.3), vec2(.56,.41), vec2(.61,.28), vec2(.59,.56), vec2(.32,.56) ); vec2 rotatev2(vec2 p, float ang) { float c = cos(ang); float s = sin(ang); return vec2(p.x*c - p.y*s, p.x*s + p.y*c); } vec3 CabezaConPelo(vec2 p, vec3 col ) { vec2 p2= rotatev2( p, radians(180.0)); //vec2 p3= rotatev2( p-vec2(0.03,0.10), radians(iTime*10.0)); vec2 p3= rotatev2( p-vec2(0.03,0.10), radians(185.0)); float d1 = sdPolygon(p2-vec2(-0.4,-0.5), pt1); float d2 = sdPolygon(p3-vec2(-0.4,-0.5), pt1); float s1= sdCircle( p-vec2(-0.05,-0.1), 0.15 ); float boca1= sdBox( p-vec2(-0.01,-0.2), vec2(0.05,0.01) ); float ojo1= sdBox( p-vec2(-0.05,-0.1), vec2(0.02,0.02) ); float ojo2= sdBox( p-vec2(0.05,-0.1), vec2(0.02,0.02) ); col = mix(col,vec3(1.0, 0.8,0.1)*1.2,S(s1,0.0)); col = mix(col,vec3(1.0, 0.2,0.1)*1.2,S(d2,0.0)); col = mix(col,vec3(1.0, 0.2,0.1)*1.2,S(boca1,0.0)); col = mix(col,vec3(0.0, 0.2,0.1)*1.2,S(ojo1,0.0)); col = mix(col,vec3(0.0, 0.2,0.1)*1.2,S(ojo2,0.0)); d1 = SS(d1); col=mix(col,vec3(0.0),d1); return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (2.0*fragCoord-iResolution.xy)/iResolution.y; float tt=iTime; uv=uv*2.0*abs(sin(tt)); vec2 p=uv*0.5+tt; vec2 p2=uv*2.0; //------------------------------- vec3 col=vec3(1.0); p.x= opRep1D( p.x, 0.8 ); p.y= opRep1D( p.y, 0.8 ); p2.x= opRep1D( p2.x, 1.6 ); p2.y= opRep1D( p2.y, 1.6 ); col= CabezaConPelo(p, col); col= CabezaConPelo(p2-vec2(0.5,-0.5), col); fragColor=vec4(col,1.0); }
mit
[ 1977, 2015, 2051, 2051, 2096 ]
[ [ 1809, 1809, 1842, 1842, 1864 ], [ 1865, 1865, 1898, 1898, 1920 ], [ 1921, 1921, 1954, 1954, 1975 ], [ 1977, 2015, 2051, 2051, 2096 ], [ 2097, 2110, 2147, 2147, 2227 ], [ 2230, 2230, 2265, 2265, 2293 ], [ 2321, 2321, 2346, 2346, 2365 ], [ 2366, 2366, 2406, 2406, 2438 ], [ 3362, 3362, 3396, 3396, 3493 ], [ 3497, 3497, 3536, 3536, 4422 ], [ 4428, 4428, 4485, 4485, 4960 ] ]
//----------oPeraciones de Repeticion
float opRep1D( float p, float c ) {
float q = mod(p+0.5*c,c)-0.5*c; return q ;}
//----------oPeraciones de Repeticion float opRep1D( float p, float c ) {
121
121
7d23DR
mrange
2021-04-01T09:24:32
// License CC0: 2D Amoebas // While messing around I stumbled on a simple "amoeba" lika effect. // Nothing complicated but nice IMHO so I shared #define RESOLUTION iResolution #define TIME iTime float circle(vec2 p, float r) { return length(p) - r; } // IQ's polynominal min float pmin(float a, float b, float k) { float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 ); return mix( b, a, h ) - k*h*(1.0-h); } // http://mercury.sexy/hg_sdf/ vec2 mod2(inout vec2 p, vec2 size) { vec2 c = floor((p + size*0.5)/size); p = mod(p + size*0.5,size) - size*0.5; return c; } float df(vec2 p) { // Generates a grid of dots vec2 dp = p; vec2 dn = mod2(dp, vec2(0.25)); float ddots = length(dp); // Blobs float dblobs = 1E6; for (int i = 0; i < 5; ++i) { float dd = circle(p-1.0*vec2(sin(TIME+float(i)), sin(float(i*i)+TIME*sqrt(0.5))), 0.1); dblobs = pmin(dblobs, dd, 0.35); } float d = 1E6; d = min(d, ddots); // Smooth min between blobs and dots makes it look somewhat amoeba like d = pmin(d, dblobs, 0.35); return d; } vec3 postProcess(vec3 col, vec2 q) { col=pow(clamp(col,0.0,1.0),vec3(1.0/2.2)); col=col*0.6+0.4*col*col*(3.0-2.0*col); // contrast col=mix(col, vec3(dot(col, vec3(0.33))), -0.4); // satuation col*=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); // vigneting return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord/RESOLUTION.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; float aa = 2.0/RESOLUTION.y; const float z = 1.4; float d = df(p/z)*z; vec3 col = vec3(0.33); col = mix(col, vec3(.0), smoothstep(-aa, aa, -d)); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 264, 288, 327, 327, 420 ]
[ [ 205, 205, 236, 236, 262 ], [ 264, 288, 327, 327, 420 ], [ 422, 453, 489, 489, 583 ], [ 586, 586, 604, 634, 1068 ], [ 1070, 1070, 1107, 1107, 1359 ], [ 1361, 1361, 1416, 1416, 1736 ] ]
// IQ's polynominal min
float pmin(float a, float b, float k) {
float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 ); return mix( b, a, h ) - k*h*(1.0-h); }
// IQ's polynominal min float pmin(float a, float b, float k) {
6
107
7d23DR
mrange
2021-04-01T09:24:32
// License CC0: 2D Amoebas // While messing around I stumbled on a simple "amoeba" lika effect. // Nothing complicated but nice IMHO so I shared #define RESOLUTION iResolution #define TIME iTime float circle(vec2 p, float r) { return length(p) - r; } // IQ's polynominal min float pmin(float a, float b, float k) { float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 ); return mix( b, a, h ) - k*h*(1.0-h); } // http://mercury.sexy/hg_sdf/ vec2 mod2(inout vec2 p, vec2 size) { vec2 c = floor((p + size*0.5)/size); p = mod(p + size*0.5,size) - size*0.5; return c; } float df(vec2 p) { // Generates a grid of dots vec2 dp = p; vec2 dn = mod2(dp, vec2(0.25)); float ddots = length(dp); // Blobs float dblobs = 1E6; for (int i = 0; i < 5; ++i) { float dd = circle(p-1.0*vec2(sin(TIME+float(i)), sin(float(i*i)+TIME*sqrt(0.5))), 0.1); dblobs = pmin(dblobs, dd, 0.35); } float d = 1E6; d = min(d, ddots); // Smooth min between blobs and dots makes it look somewhat amoeba like d = pmin(d, dblobs, 0.35); return d; } vec3 postProcess(vec3 col, vec2 q) { col=pow(clamp(col,0.0,1.0),vec3(1.0/2.2)); col=col*0.6+0.4*col*col*(3.0-2.0*col); // contrast col=mix(col, vec3(dot(col, vec3(0.33))), -0.4); // satuation col*=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); // vigneting return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord/RESOLUTION.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; float aa = 2.0/RESOLUTION.y; const float z = 1.4; float d = df(p/z)*z; vec3 col = vec3(0.33); col = mix(col, vec3(.0), smoothstep(-aa, aa, -d)); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 422, 453, 489, 489, 583 ]
[ [ 205, 205, 236, 236, 262 ], [ 264, 288, 327, 327, 420 ], [ 422, 453, 489, 489, 583 ], [ 586, 586, 604, 634, 1068 ], [ 1070, 1070, 1107, 1107, 1359 ], [ 1361, 1361, 1416, 1416, 1736 ] ]
// http://mercury.sexy/hg_sdf/
vec2 mod2(inout vec2 p, vec2 size) {
vec2 c = floor((p + size*0.5)/size); p = mod(p + size*0.5,size) - size*0.5; return c; }
// http://mercury.sexy/hg_sdf/ vec2 mod2(inout vec2 p, vec2 size) {
52
53
fdXXR4
mrange
2021-04-13T08:04:55
// License CC0: Starry background with nebula // Created for another shader but thought the background could be useful to others so extracted it // Controls how many layers of stars #define LAYERS 5.0 // QUINTIC or HERMITE interpolation? #define QUINTIC // How often to change the nebula #define PERIOD 15.0 #define PI 3.141592654 #define TAU (2.0*PI) #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PCOS(x) (0.5 + 0.5*cos(x)) #define TTIME (TAU*TIME) const mat2 rotSome = ROT(1.0); float tanh_approx(float x) { // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } float hash(float co) { return fract(sin(co*12.9898) * 13758.5453); } float hash(vec2 co) { co += 123.4; return fract(sin(dot(co, vec2(12.9898,58.233))) * 13758.5453); } vec2 hash2(vec2 p) { p = vec2 (dot (p, vec2 (127.1, 311.7)), dot (p, vec2 (269.5, 183.3))); return -1. + 2.*fract (sin (p)*43758.5453123); } // https://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl vec3 hsv2rgb(vec3 c) { const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } // http://mercury.sexy/hg_sdf/ vec2 mod2(inout vec2 p, vec2 size) { vec2 c = floor((p + size*0.5)/size); p = mod(p + size*0.5,size) - size*0.5; return c; } vec3 toSpherical(vec3 p) { float r = length(p); float t = acos(p.z/r); float ph = atan(p.y, p.x); return vec3(r, t, ph); } vec3 postProcess(vec3 col, vec2 q) { col=pow(clamp(col,0.0,1.0),vec3(1.0/2.2)); col=col*0.6+0.4*col*col*(3.0-2.0*col); // contrast col=mix(col, vec3(dot(col, vec3(0.33))), -0.4); // satuation col*=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); // vigneting return col; } // From one of IQ's value noise shaders float vnoise(vec2 x) { vec2 i = floor(x); vec2 w = fract(x); #ifdef QUINTIC // quintic interpolation vec2 u = w*w*w*(w*(w*6.0-15.0)+10.0); #else // cubic interpolation vec2 u = w*w*(3.0-2.0*w); #endif float a = hash(i+vec2(0.0,0.0)); float b = hash(i+vec2(1.0,0.0)); float c = hash(i+vec2(0.0,1.0)); float d = hash(i+vec2(1.0,1.0)); float k0 = a; float k1 = b - a; float k2 = c - a; float k3 = d - c + a - b; float aa = mix(a, b, u.x); float bb = mix(c, d, u.x); float cc = mix(aa, bb, u.y); return k0 + k1*u.x + k2*u.y + k3*u.x*u.y; } float globalCloudDensity(vec2 p, float off) { vec2 pp = p; p *= 3.33; float gcd = vnoise(p+off); gcd *= smoothstep(PI/2.0, PI/4.0, abs(pp.x)); gcd *= smoothstep(PI/6.0, PI/18.0, abs(pp.y)); return gcd; } float localCloudDensity(vec2 p, float off) { p *= 10.0; const float aa = -0.45; const mat2 pp = 2.03*rotSome; float a = 0.5; float s = 0.0; p += off; s += a*vnoise(p); a *= aa; p *= pp; s += a*vnoise(p); a *= aa; p *= pp; s += a*vnoise(p); a *= aa; p *= pp; s += a*vnoise(p); a *= aa; p *= pp; s += a*vnoise(p); a *= aa; p *= pp; return s*2.75; } vec3 clouds(vec3 ro, vec3 rd, out float cloudDensity) { vec3 srd = toSpherical(rd.zxy); float y = sin(srd.y); vec2 pp = srd.zy; pp.x *= y; pp.y -= PI/2.0; pp *= ROT(0.5); float h = hash(floor(2.0+TIME/PERIOD)); float off = 10.0*fract(123.0*h)+100.0; float gcd = globalCloudDensity(pp, off); float cd = gcd*localCloudDensity(pp, off); float cdo = gcd*localCloudDensity(pp+00.075*vec2(0.125, -0.25), off); cloudDensity = cd; // Basis for some very fake shading float cli = mix(-0.5, 1.0, 0.5 + 0.5*tanh_approx(12.0*(cd-cdo))); float tc = clamp(cd, 0.0, 1.0); float huec = (mix(-0.2, 0.05, tc)+0.05)-0.15*(h-0.5)-0.0; float satc = mix(0.9, 0.5, tc); float bric = 1.0; vec3 colc = hsv2rgb(vec3(huec, satc, bric))+cli*vec3(0.9, 0.7, 0.9); tc *= tc; vec4 cc = vec4(colc*0.66, tc); cc = clamp(cc, 0.0, 1.0); return cc.xyz*cc.w; } vec3 stars(vec3 ro, vec3 rd, float cloudDensity) { vec3 col = vec3(0.0); vec3 srd = toSpherical(rd.xzy); const float m = LAYERS; for (float i = 0.0; i < m; ++i) { vec2 pp = srd.yz+0.5*i; float s = i/(m-1.0); vec2 dim = vec2(mix(0.025, 0.003, s)*PI); vec2 np = mod2(pp, dim); vec2 h = hash2(np+127.0+i); vec2 o = -1.0+2.0*h; float y = sin(srd.y); pp += o*dim*0.5; pp.y *= y; float l = length(pp); float h1 = fract(h.x*109.0); float h2 = fract(h.x*113.0); float h3 = fract(h.x*127.0); vec3 hsv = vec3(fract(0.025-0.4*h1*h1), mix(0.5, 0.125, s), 1.0); vec3 scol = mix(8.0*h2, 0.25*h2*h2, s)*hsv2rgb(hsv); vec3 ccol = col+ exp(-(2000.0/mix(2.0, 0.25, s))*max(l-0.001, 0.0))*scol; float p = i < 3.0 ? mix(0.125, 2.0, cloudDensity)*y : y; p = clamp(p, 0.0, 1.0); col = h3 < p ? ccol : col; } return col; } vec3 grid(vec3 ro, vec3 rd) { vec3 srd = toSpherical(rd.xzy); const float m = 1.0; const vec2 dim = vec2(1.0/8.0*PI); vec2 pp = srd.yz; vec2 np = mod2(pp, dim); vec3 col = vec3(0.0); float y = sin(srd.y); float d = min(abs(pp.x), abs(pp.y*y)); float aa = 2.0/RESOLUTION.y; col += 2.0*vec3(0.5, 0.5, 1.0)*exp(-2000.0*max(d-0.00025, 0.0)); return 0.25*tanh(col); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord.xy/RESOLUTION.xy; vec2 p = -1.0 + 2.0*q; p.x *= RESOLUTION.x/RESOLUTION.y; vec3 ro = vec3(2.0, 0, 0.); ro.xy *= ROT(-0.33*sin(TTIME/12.0)); ro.xz *= ROT(1.5+0.33*sin(TTIME/12.0)); vec3 la = vec3(0.0, 0.0, 0.0); vec3 ww = normalize(la - ro); vec3 uu = normalize(cross( vec3(0.0,1.0,0.0), ww)); vec3 vv = normalize(cross(ww,uu)); const float rdd = 2.0; vec3 rd = normalize(p.x*uu + p.y*vv + rdd*ww); vec3 col = vec3(0.0); float cloudDensity; col += clouds(ro, rd, cloudDensity); col += stars(ro, rd, cloudDensity); col += grid(ro, rd); col = clamp(col, 0.0, 1.0); fragColor = vec4(postProcess(col, q),1.0); }
cc0-1.0
[ 1991, 2031, 2053, 2053, 2625 ]
[ [ 666, 666, 694, 714, 790 ], [ 792, 792, 814, 814, 862 ], [ 864, 864, 885, 885, 967 ], [ 969, 969, 989, 989, 1126 ], [ 1128, 1207, 1229, 1229, 1398 ], [ 1400, 1431, 1467, 1467, 1561 ], [ 1563, 1563, 1589, 1589, 1698 ], [ 1700, 1700, 1737, 1737, 1989 ], [ 1991, 2031, 2053, 2053, 2625 ], [ 2627, 2627, 2672, 2672, 2845 ], [ 2847, 2847, 2891, 2891, 3223 ], [ 3225, 3225, 3280, 3280, 4103 ], [ 4105, 4105, 4155, 4155, 5005 ], [ 5007, 5007, 5036, 5036, 5406 ], [ 5408, 5408, 5463, 5463, 6138 ] ]
// From one of IQ's value noise shaders
float vnoise(vec2 x) {
vec2 i = floor(x); vec2 w = fract(x); #ifdef QUINTIC // quintic interpolation vec2 u = w*w*w*(w*(w*6.0-15.0)+10.0); #else // cubic interpolation vec2 u = w*w*(3.0-2.0*w); #endif float a = hash(i+vec2(0.0,0.0)); float b = hash(i+vec2(1.0,0.0)); float c = hash(i+vec2(0.0,1.0)); float d = hash(i+vec2(1.0,1.0)); float k0 = a; float k1 = b - a; float k2 = c - a; float k3 = d - c + a - b; float aa = mix(a, b, u.x); float bb = mix(c, d, u.x); float cc = mix(aa, bb, u.y); return k0 + k1*u.x + k2*u.y + k3*u.x*u.y; }
// From one of IQ's value noise shaders float vnoise(vec2 x) {
1
5
NdS3WW
mrange
2021-04-02T13:52:21
// License CC0: The monolith, 1x4x9 #define TOLERANCE 0.0001 #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define L2(x) dot(x, x) #define PCOS(x) (0.5 + 0.5*cos(x)) #define SKYCOLOR(ro, rd) skyColor(ro, rd) const float miss = 1E4; const float refrIndex = 0.8; const vec3 lightPos = 2.0*vec3(1.5, 2.0, 1.0); const vec3 skyCol1 = vec3(0.2, 0.4, 0.6); const vec3 skyCol2 = vec3(0.4, 0.7, 1.0); const vec3 sunCol = vec3(8.0,7.0,6.0)/8.0; const vec3 boxDim = vec3(1.0, 9.0, 4.0)/18.0; const vec4 plane = vec4(vec3(0.0, 1.0, 0.0), 0.5); float tanh_approx(float x) { // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } // IQ's polynominal min float pmin(float a, float b, float k) { float h = clamp(0.5+0.5*(b-a)/k, 0.0, 1.0); return mix(b, a, h) - k*h*(1.0-h); } // https://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl vec3 hsv2rgb(vec3 c) { const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } // IQ's ray sphere intersection vec2 raySphere(vec3 ro, vec3 rd, vec4 s) { vec3 ce = s.xyz; float ra = s.w; vec3 oc = ro - ce; float b = dot( oc, rd ); float c = dot( oc, oc ) - ra*ra; float h = b*b - c; if( h<0.0 ) return vec2(miss); // no intersection h = sqrt( h ); return vec2( -b-h, -b+h ); } // IQ's ray box intersection vec2 rayBox(vec3 ro, vec3 rd, vec3 boxSize, out vec3 outNormal ) { vec3 m = 1.0/rd; // can precompute if traversing a set of aligned boxes vec3 n = m*ro; // can precompute if traversing a set of aligned boxes vec3 k = abs(m)*boxSize; vec3 t1 = -n - k; vec3 t2 = -n + k; float tN = max( max( t1.x, t1.y ), t1.z ); float tF = min( min( t2.x, t2.y ), t2.z ); if( tN>tF || tF<0.0) return vec2(miss); // no intersection outNormal = -sign(rd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz); return vec2( tN, tF ); } float rayPlane(vec3 ro, vec3 rd, vec4 p ) { return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz); } vec3 postProcess(vec3 col, vec2 q) { col=pow(clamp(col,0.0,1.0),vec3(1.0/2.2)); col=col*0.6+0.4*col*col*(3.0-2.0*col); // contrast col=mix(col, vec3(dot(col, vec3(0.33))), -0.4); // satuation col*=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); // vigneting return col; } vec3 skyColor(vec3 ro, vec3 rd) { const vec3 sunDir = normalize(lightPos); float sunDot = max(dot(rd, sunDir), 0.0); vec3 final = vec3(0.); final += mix(skyCol1, skyCol2, rd.y); final += 0.5*sunCol*pow(sunDot, 20.0); final += 4.0*sunCol*pow(sunDot, 400.0); float tp = rayPlane(ro, rd, plane); if (tp > 0.0) { vec3 pos = ro + tp*rd; vec3 ld = normalize(lightPos - pos); vec3 snor; vec2 rb = rayBox(pos, ld, boxDim, snor); vec3 spos = pos + ld*rb.x; float it = rb.y - rb.x; // Extremely fake soft shadows float sha = rb.x == miss ? 1.0 : (1.0-1.0*tanh_approx(it*6.0/(0.1+rb.x))); vec3 nor = vec3(0.0, 1.0, 0.0); vec3 icol = 1.5*skyCol1 + 4.0*sunCol*sha*dot(-rd, nor); vec2 ppos = pos.xz*0.75+0.23; ppos = fract(ppos+0.5)-0.5; float pd = min(abs(ppos.x), abs(ppos.y)); vec3 pcol= mix(vec3(0.4), vec3(0.3, 0.3, 0.3), exp(-60.0*pd)); vec3 col = icol*pcol; col = clamp(col, 0.0, 1.25); float f = exp(-10.0*(max(tp-10.0, 0.0) / 100.0)); return mix(final, col , f); } else{ return final; } } vec3 innerRender(vec3 ro, vec3 rd, vec3 enor) { const float spr = 0.25; vec3 spc = vec3(-enor*spr); vec4 sp = vec4(spc, spr); vec2 rs = raySphere(ro,rd, sp); vec3 bhsv = vec3(fract(0.05*TIME+dot(enor, rd)*2.0), 0.5, 1.0); vec3 bcol = hsv2rgb(bhsv); vec3 col = vec3(0.0); if (rs.x < miss) { float t = rs.x; vec3 pos = ro + rd*t; vec3 nor = normalize(pos - sp.xyz); vec3 ld = normalize(lightPos - pos); float dif = pow(max(dot(nor,ld),0.0), 2.0); float l = dif; float lin = mix(0.005, 1.0, l); float itd = rs.y - rs.x; col += lin*bcol; col = mix(col, vec3(0.0), tanh_approx(1E-3/(itd*itd))); } return col; } vec3 render(vec3 ro, vec3 rd) { vec3 skyCol = SKYCOLOR(ro, rd); vec3 col = vec3(0.0); float t = 1E6; vec3 nor; vec2 rb = rayBox(ro, rd, boxDim, nor); if (rb.x < miss) { t = rb.x; float itd = rb.y - rb.x; vec3 pos = ro + t*rd; vec3 anor = abs(nor); vec2 tp = anor.x == 1.0 ? pos.yz : (anor.y == 1.0 ? pos.xz : pos.xy); vec2 bd = anor.x == 1.0 ? boxDim.yz : (anor.y == 1.0 ? boxDim.xz : boxDim.xy); vec3 refr = refract(rd, nor, refrIndex); vec3 refl = reflect(rd, nor); vec3 rcol = SKYCOLOR(pos, refl); float fre = mix(0.0, 1.0, pow(1.0-dot(-rd, nor), 3.0)); vec3 ld = normalize(lightPos - pos); float dif = pow(max(dot(nor,ld),0.0), 3.0); float spe = pow(max(dot(reflect(-ld, nor), -rd), 0.), 50.); float lin = mix(0.0, 1.0, dif); vec3 lcol = 2.0*sqrt(sunCol); col = innerRender(pos, refr, nor); vec2 btp = (1.0*bd - abs(tp)); float bdd = pmin(btp.x, btp.y, 0.0125); float bddd = exp(-10000.0*bdd*bdd); // col += vec3(0.5, 0.5, 1.0)*bddd*10; col *= 1.0 - bddd; vec3 diff = hsv2rgb(vec3(0.7, fre, 0.075*lin))*lcol; col += fre*rcol+diff+spe*lcol; if (refr == vec3(0.0)) { // Not expected to happen as the refraction index < 1.0 col = vec3(1.0, 0.0, 0.0); } col = mix(col, skyCol, tanh_approx(1E-5/(itd*itd))); } else { // Ray intersected sky return skyCol; } return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord.xy/RESOLUTION.xy; vec2 p = -1.0 + 2.0*q; p.x *= RESOLUTION.x/RESOLUTION.y; const float mul = 0.05; float mm = mix(0.25, 0.5, PCOS(TIME*mul*sqrt(3.0))); vec3 ro = mm*vec3(2.0, 0, 0.2); ro.xz *= ROT((TIME*mul)); ro.yz *= ROT(sin(TIME*mul*sqrt(0.5))*0.5); ro += vec3(0.0, mm, 0.0); vec3 ww = normalize(vec3(0.0, 0.0, 0.0) - ro); vec3 uu = normalize(cross( vec3(0.0,1.0,0.0), ww)); vec3 vv = normalize(cross(ww,uu)); const float rdd = 2.00; vec3 rd = normalize( p.x*uu + p.y*vv + rdd*ww); vec3 col = render(ro, rd); fragColor = vec4(postProcess(col, q),1.0); }
cc0-1.0
[ 994, 1073, 1095, 1095, 1264 ]
[ [ 715, 715, 743, 763, 839 ], [ 841, 865, 904, 904, 992 ], [ 994, 1073, 1095, 1095, 1264 ], [ 1266, 1298, 1340, 1340, 1599 ], [ 1601, 1630, 1697, 1697, 2175 ], [ 2177, 2177, 2220, 2220, 2267 ], [ 2269, 2269, 2306, 2306, 2558 ], [ 2560, 2560, 2593, 2593, 3661 ], [ 3664, 3664, 3711, 3711, 4340 ], [ 4342, 4342, 4373, 4373, 5778 ], [ 5780, 5780, 5835, 5835, 6447 ] ]
// https://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl
vec3 hsv2rgb(vec3 c) {
const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); }
// https://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl vec3 hsv2rgb(vec3 c) {
22
237
NdS3WW
mrange
2021-04-02T13:52:21
// License CC0: The monolith, 1x4x9 #define TOLERANCE 0.0001 #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define L2(x) dot(x, x) #define PCOS(x) (0.5 + 0.5*cos(x)) #define SKYCOLOR(ro, rd) skyColor(ro, rd) const float miss = 1E4; const float refrIndex = 0.8; const vec3 lightPos = 2.0*vec3(1.5, 2.0, 1.0); const vec3 skyCol1 = vec3(0.2, 0.4, 0.6); const vec3 skyCol2 = vec3(0.4, 0.7, 1.0); const vec3 sunCol = vec3(8.0,7.0,6.0)/8.0; const vec3 boxDim = vec3(1.0, 9.0, 4.0)/18.0; const vec4 plane = vec4(vec3(0.0, 1.0, 0.0), 0.5); float tanh_approx(float x) { // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } // IQ's polynominal min float pmin(float a, float b, float k) { float h = clamp(0.5+0.5*(b-a)/k, 0.0, 1.0); return mix(b, a, h) - k*h*(1.0-h); } // https://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl vec3 hsv2rgb(vec3 c) { const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } // IQ's ray sphere intersection vec2 raySphere(vec3 ro, vec3 rd, vec4 s) { vec3 ce = s.xyz; float ra = s.w; vec3 oc = ro - ce; float b = dot( oc, rd ); float c = dot( oc, oc ) - ra*ra; float h = b*b - c; if( h<0.0 ) return vec2(miss); // no intersection h = sqrt( h ); return vec2( -b-h, -b+h ); } // IQ's ray box intersection vec2 rayBox(vec3 ro, vec3 rd, vec3 boxSize, out vec3 outNormal ) { vec3 m = 1.0/rd; // can precompute if traversing a set of aligned boxes vec3 n = m*ro; // can precompute if traversing a set of aligned boxes vec3 k = abs(m)*boxSize; vec3 t1 = -n - k; vec3 t2 = -n + k; float tN = max( max( t1.x, t1.y ), t1.z ); float tF = min( min( t2.x, t2.y ), t2.z ); if( tN>tF || tF<0.0) return vec2(miss); // no intersection outNormal = -sign(rd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz); return vec2( tN, tF ); } float rayPlane(vec3 ro, vec3 rd, vec4 p ) { return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz); } vec3 postProcess(vec3 col, vec2 q) { col=pow(clamp(col,0.0,1.0),vec3(1.0/2.2)); col=col*0.6+0.4*col*col*(3.0-2.0*col); // contrast col=mix(col, vec3(dot(col, vec3(0.33))), -0.4); // satuation col*=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); // vigneting return col; } vec3 skyColor(vec3 ro, vec3 rd) { const vec3 sunDir = normalize(lightPos); float sunDot = max(dot(rd, sunDir), 0.0); vec3 final = vec3(0.); final += mix(skyCol1, skyCol2, rd.y); final += 0.5*sunCol*pow(sunDot, 20.0); final += 4.0*sunCol*pow(sunDot, 400.0); float tp = rayPlane(ro, rd, plane); if (tp > 0.0) { vec3 pos = ro + tp*rd; vec3 ld = normalize(lightPos - pos); vec3 snor; vec2 rb = rayBox(pos, ld, boxDim, snor); vec3 spos = pos + ld*rb.x; float it = rb.y - rb.x; // Extremely fake soft shadows float sha = rb.x == miss ? 1.0 : (1.0-1.0*tanh_approx(it*6.0/(0.1+rb.x))); vec3 nor = vec3(0.0, 1.0, 0.0); vec3 icol = 1.5*skyCol1 + 4.0*sunCol*sha*dot(-rd, nor); vec2 ppos = pos.xz*0.75+0.23; ppos = fract(ppos+0.5)-0.5; float pd = min(abs(ppos.x), abs(ppos.y)); vec3 pcol= mix(vec3(0.4), vec3(0.3, 0.3, 0.3), exp(-60.0*pd)); vec3 col = icol*pcol; col = clamp(col, 0.0, 1.25); float f = exp(-10.0*(max(tp-10.0, 0.0) / 100.0)); return mix(final, col , f); } else{ return final; } } vec3 innerRender(vec3 ro, vec3 rd, vec3 enor) { const float spr = 0.25; vec3 spc = vec3(-enor*spr); vec4 sp = vec4(spc, spr); vec2 rs = raySphere(ro,rd, sp); vec3 bhsv = vec3(fract(0.05*TIME+dot(enor, rd)*2.0), 0.5, 1.0); vec3 bcol = hsv2rgb(bhsv); vec3 col = vec3(0.0); if (rs.x < miss) { float t = rs.x; vec3 pos = ro + rd*t; vec3 nor = normalize(pos - sp.xyz); vec3 ld = normalize(lightPos - pos); float dif = pow(max(dot(nor,ld),0.0), 2.0); float l = dif; float lin = mix(0.005, 1.0, l); float itd = rs.y - rs.x; col += lin*bcol; col = mix(col, vec3(0.0), tanh_approx(1E-3/(itd*itd))); } return col; } vec3 render(vec3 ro, vec3 rd) { vec3 skyCol = SKYCOLOR(ro, rd); vec3 col = vec3(0.0); float t = 1E6; vec3 nor; vec2 rb = rayBox(ro, rd, boxDim, nor); if (rb.x < miss) { t = rb.x; float itd = rb.y - rb.x; vec3 pos = ro + t*rd; vec3 anor = abs(nor); vec2 tp = anor.x == 1.0 ? pos.yz : (anor.y == 1.0 ? pos.xz : pos.xy); vec2 bd = anor.x == 1.0 ? boxDim.yz : (anor.y == 1.0 ? boxDim.xz : boxDim.xy); vec3 refr = refract(rd, nor, refrIndex); vec3 refl = reflect(rd, nor); vec3 rcol = SKYCOLOR(pos, refl); float fre = mix(0.0, 1.0, pow(1.0-dot(-rd, nor), 3.0)); vec3 ld = normalize(lightPos - pos); float dif = pow(max(dot(nor,ld),0.0), 3.0); float spe = pow(max(dot(reflect(-ld, nor), -rd), 0.), 50.); float lin = mix(0.0, 1.0, dif); vec3 lcol = 2.0*sqrt(sunCol); col = innerRender(pos, refr, nor); vec2 btp = (1.0*bd - abs(tp)); float bdd = pmin(btp.x, btp.y, 0.0125); float bddd = exp(-10000.0*bdd*bdd); // col += vec3(0.5, 0.5, 1.0)*bddd*10; col *= 1.0 - bddd; vec3 diff = hsv2rgb(vec3(0.7, fre, 0.075*lin))*lcol; col += fre*rcol+diff+spe*lcol; if (refr == vec3(0.0)) { // Not expected to happen as the refraction index < 1.0 col = vec3(1.0, 0.0, 0.0); } col = mix(col, skyCol, tanh_approx(1E-5/(itd*itd))); } else { // Ray intersected sky return skyCol; } return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord.xy/RESOLUTION.xy; vec2 p = -1.0 + 2.0*q; p.x *= RESOLUTION.x/RESOLUTION.y; const float mul = 0.05; float mm = mix(0.25, 0.5, PCOS(TIME*mul*sqrt(3.0))); vec3 ro = mm*vec3(2.0, 0, 0.2); ro.xz *= ROT((TIME*mul)); ro.yz *= ROT(sin(TIME*mul*sqrt(0.5))*0.5); ro += vec3(0.0, mm, 0.0); vec3 ww = normalize(vec3(0.0, 0.0, 0.0) - ro); vec3 uu = normalize(cross( vec3(0.0,1.0,0.0), ww)); vec3 vv = normalize(cross(ww,uu)); const float rdd = 2.00; vec3 rd = normalize( p.x*uu + p.y*vv + rdd*ww); vec3 col = render(ro, rd); fragColor = vec4(postProcess(col, q),1.0); }
cc0-1.0
[ 1266, 1298, 1340, 1340, 1599 ]
[ [ 715, 715, 743, 763, 839 ], [ 841, 865, 904, 904, 992 ], [ 994, 1073, 1095, 1095, 1264 ], [ 1266, 1298, 1340, 1340, 1599 ], [ 1601, 1630, 1697, 1697, 2175 ], [ 2177, 2177, 2220, 2220, 2267 ], [ 2269, 2269, 2306, 2306, 2558 ], [ 2560, 2560, 2593, 2593, 3661 ], [ 3664, 3664, 3711, 3711, 4340 ], [ 4342, 4342, 4373, 4373, 5778 ], [ 5780, 5780, 5835, 5835, 6447 ] ]
// IQ's ray sphere intersection
vec2 raySphere(vec3 ro, vec3 rd, vec4 s) {
vec3 ce = s.xyz; float ra = s.w; vec3 oc = ro - ce; float b = dot( oc, rd ); float c = dot( oc, oc ) - ra*ra; float h = b*b - c; if( h<0.0 ) return vec2(miss); // no intersection h = sqrt( h ); return vec2( -b-h, -b+h ); }
// IQ's ray sphere intersection vec2 raySphere(vec3 ro, vec3 rd, vec4 s) {
2
2
NdS3WW
mrange
2021-04-02T13:52:21
// License CC0: The monolith, 1x4x9 #define TOLERANCE 0.0001 #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define L2(x) dot(x, x) #define PCOS(x) (0.5 + 0.5*cos(x)) #define SKYCOLOR(ro, rd) skyColor(ro, rd) const float miss = 1E4; const float refrIndex = 0.8; const vec3 lightPos = 2.0*vec3(1.5, 2.0, 1.0); const vec3 skyCol1 = vec3(0.2, 0.4, 0.6); const vec3 skyCol2 = vec3(0.4, 0.7, 1.0); const vec3 sunCol = vec3(8.0,7.0,6.0)/8.0; const vec3 boxDim = vec3(1.0, 9.0, 4.0)/18.0; const vec4 plane = vec4(vec3(0.0, 1.0, 0.0), 0.5); float tanh_approx(float x) { // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } // IQ's polynominal min float pmin(float a, float b, float k) { float h = clamp(0.5+0.5*(b-a)/k, 0.0, 1.0); return mix(b, a, h) - k*h*(1.0-h); } // https://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl vec3 hsv2rgb(vec3 c) { const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } // IQ's ray sphere intersection vec2 raySphere(vec3 ro, vec3 rd, vec4 s) { vec3 ce = s.xyz; float ra = s.w; vec3 oc = ro - ce; float b = dot( oc, rd ); float c = dot( oc, oc ) - ra*ra; float h = b*b - c; if( h<0.0 ) return vec2(miss); // no intersection h = sqrt( h ); return vec2( -b-h, -b+h ); } // IQ's ray box intersection vec2 rayBox(vec3 ro, vec3 rd, vec3 boxSize, out vec3 outNormal ) { vec3 m = 1.0/rd; // can precompute if traversing a set of aligned boxes vec3 n = m*ro; // can precompute if traversing a set of aligned boxes vec3 k = abs(m)*boxSize; vec3 t1 = -n - k; vec3 t2 = -n + k; float tN = max( max( t1.x, t1.y ), t1.z ); float tF = min( min( t2.x, t2.y ), t2.z ); if( tN>tF || tF<0.0) return vec2(miss); // no intersection outNormal = -sign(rd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz); return vec2( tN, tF ); } float rayPlane(vec3 ro, vec3 rd, vec4 p ) { return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz); } vec3 postProcess(vec3 col, vec2 q) { col=pow(clamp(col,0.0,1.0),vec3(1.0/2.2)); col=col*0.6+0.4*col*col*(3.0-2.0*col); // contrast col=mix(col, vec3(dot(col, vec3(0.33))), -0.4); // satuation col*=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); // vigneting return col; } vec3 skyColor(vec3 ro, vec3 rd) { const vec3 sunDir = normalize(lightPos); float sunDot = max(dot(rd, sunDir), 0.0); vec3 final = vec3(0.); final += mix(skyCol1, skyCol2, rd.y); final += 0.5*sunCol*pow(sunDot, 20.0); final += 4.0*sunCol*pow(sunDot, 400.0); float tp = rayPlane(ro, rd, plane); if (tp > 0.0) { vec3 pos = ro + tp*rd; vec3 ld = normalize(lightPos - pos); vec3 snor; vec2 rb = rayBox(pos, ld, boxDim, snor); vec3 spos = pos + ld*rb.x; float it = rb.y - rb.x; // Extremely fake soft shadows float sha = rb.x == miss ? 1.0 : (1.0-1.0*tanh_approx(it*6.0/(0.1+rb.x))); vec3 nor = vec3(0.0, 1.0, 0.0); vec3 icol = 1.5*skyCol1 + 4.0*sunCol*sha*dot(-rd, nor); vec2 ppos = pos.xz*0.75+0.23; ppos = fract(ppos+0.5)-0.5; float pd = min(abs(ppos.x), abs(ppos.y)); vec3 pcol= mix(vec3(0.4), vec3(0.3, 0.3, 0.3), exp(-60.0*pd)); vec3 col = icol*pcol; col = clamp(col, 0.0, 1.25); float f = exp(-10.0*(max(tp-10.0, 0.0) / 100.0)); return mix(final, col , f); } else{ return final; } } vec3 innerRender(vec3 ro, vec3 rd, vec3 enor) { const float spr = 0.25; vec3 spc = vec3(-enor*spr); vec4 sp = vec4(spc, spr); vec2 rs = raySphere(ro,rd, sp); vec3 bhsv = vec3(fract(0.05*TIME+dot(enor, rd)*2.0), 0.5, 1.0); vec3 bcol = hsv2rgb(bhsv); vec3 col = vec3(0.0); if (rs.x < miss) { float t = rs.x; vec3 pos = ro + rd*t; vec3 nor = normalize(pos - sp.xyz); vec3 ld = normalize(lightPos - pos); float dif = pow(max(dot(nor,ld),0.0), 2.0); float l = dif; float lin = mix(0.005, 1.0, l); float itd = rs.y - rs.x; col += lin*bcol; col = mix(col, vec3(0.0), tanh_approx(1E-3/(itd*itd))); } return col; } vec3 render(vec3 ro, vec3 rd) { vec3 skyCol = SKYCOLOR(ro, rd); vec3 col = vec3(0.0); float t = 1E6; vec3 nor; vec2 rb = rayBox(ro, rd, boxDim, nor); if (rb.x < miss) { t = rb.x; float itd = rb.y - rb.x; vec3 pos = ro + t*rd; vec3 anor = abs(nor); vec2 tp = anor.x == 1.0 ? pos.yz : (anor.y == 1.0 ? pos.xz : pos.xy); vec2 bd = anor.x == 1.0 ? boxDim.yz : (anor.y == 1.0 ? boxDim.xz : boxDim.xy); vec3 refr = refract(rd, nor, refrIndex); vec3 refl = reflect(rd, nor); vec3 rcol = SKYCOLOR(pos, refl); float fre = mix(0.0, 1.0, pow(1.0-dot(-rd, nor), 3.0)); vec3 ld = normalize(lightPos - pos); float dif = pow(max(dot(nor,ld),0.0), 3.0); float spe = pow(max(dot(reflect(-ld, nor), -rd), 0.), 50.); float lin = mix(0.0, 1.0, dif); vec3 lcol = 2.0*sqrt(sunCol); col = innerRender(pos, refr, nor); vec2 btp = (1.0*bd - abs(tp)); float bdd = pmin(btp.x, btp.y, 0.0125); float bddd = exp(-10000.0*bdd*bdd); // col += vec3(0.5, 0.5, 1.0)*bddd*10; col *= 1.0 - bddd; vec3 diff = hsv2rgb(vec3(0.7, fre, 0.075*lin))*lcol; col += fre*rcol+diff+spe*lcol; if (refr == vec3(0.0)) { // Not expected to happen as the refraction index < 1.0 col = vec3(1.0, 0.0, 0.0); } col = mix(col, skyCol, tanh_approx(1E-5/(itd*itd))); } else { // Ray intersected sky return skyCol; } return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord.xy/RESOLUTION.xy; vec2 p = -1.0 + 2.0*q; p.x *= RESOLUTION.x/RESOLUTION.y; const float mul = 0.05; float mm = mix(0.25, 0.5, PCOS(TIME*mul*sqrt(3.0))); vec3 ro = mm*vec3(2.0, 0, 0.2); ro.xz *= ROT((TIME*mul)); ro.yz *= ROT(sin(TIME*mul*sqrt(0.5))*0.5); ro += vec3(0.0, mm, 0.0); vec3 ww = normalize(vec3(0.0, 0.0, 0.0) - ro); vec3 uu = normalize(cross( vec3(0.0,1.0,0.0), ww)); vec3 vv = normalize(cross(ww,uu)); const float rdd = 2.00; vec3 rd = normalize( p.x*uu + p.y*vv + rdd*ww); vec3 col = render(ro, rd); fragColor = vec4(postProcess(col, q),1.0); }
cc0-1.0
[ 1601, 1630, 1697, 1697, 2175 ]
[ [ 715, 715, 743, 763, 839 ], [ 841, 865, 904, 904, 992 ], [ 994, 1073, 1095, 1095, 1264 ], [ 1266, 1298, 1340, 1340, 1599 ], [ 1601, 1630, 1697, 1697, 2175 ], [ 2177, 2177, 2220, 2220, 2267 ], [ 2269, 2269, 2306, 2306, 2558 ], [ 2560, 2560, 2593, 2593, 3661 ], [ 3664, 3664, 3711, 3711, 4340 ], [ 4342, 4342, 4373, 4373, 5778 ], [ 5780, 5780, 5835, 5835, 6447 ] ]
// IQ's ray box intersection
vec2 rayBox(vec3 ro, vec3 rd, vec3 boxSize, out vec3 outNormal ) {
vec3 m = 1.0/rd; // can precompute if traversing a set of aligned boxes vec3 n = m*ro; // can precompute if traversing a set of aligned boxes vec3 k = abs(m)*boxSize; vec3 t1 = -n - k; vec3 t2 = -n + k; float tN = max( max( t1.x, t1.y ), t1.z ); float tF = min( min( t2.x, t2.y ), t2.z ); if( tN>tF || tF<0.0) return vec2(miss); // no intersection outNormal = -sign(rd)*step(t1.yzx,t1.xyz)*step(t1.zxy,t1.xyz); return vec2( tN, tF ); }
// IQ's ray box intersection vec2 rayBox(vec3 ro, vec3 rd, vec3 boxSize, out vec3 outNormal ) {
1
3
NdsSRM
Dain
2021-04-14T12:40:26
// The MIT License // 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. //Rendering code from IQs torus gradient shader https://www.shadertoy.com/view/wtcfzM // sdgOval returns the oval SDF and its gradient, by // computing it analytically. // Other SDF analytic gradients // Egg: https://www.shadertoy.com/view/7dXSz7 // Oval: https://www.shadertoy.com/view/NdsSRM // Disk: https://www.shadertoy.com/view/NdlSR7 // Box : https://www.shadertoy.com/view/NslSz7 // Other SDF analytic gradients(By IQ): // // Torus: https://www.shadertoy.com/view/wtcfzM // Capsule: https://www.shadertoy.com/view/WttfR7 //Set to 1 to show the finite difference gradient for comparison #define SHOW_NUMERIC_GRADIENT 0 //A Z up oval, similiar to a capsule with a customizable mid radius // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .w = ∂f(p)/∂z // .yzw = ∇f(p) with ‖∇f(p)‖ = 1 vec4 sdgOvalZ(vec3 pIn, float a, float b, float h) { //These first 4 lines can be precalculated once float r = a - b; //a must be greater than b! float l = (h * h - r * r) / (r+r); float sub2 = (a + l); float sub1 = sub2 - length(vec2(h, l)); vec2 p = vec2(length(pIn.xy), abs(pIn.z) ); bool isTop =((p.y-h)*l) > p.x * h; float y = isTop? h: 0.0; float x = isTop ? 0.0: l; vec2 p2 = vec2( p.x + x, p.y - y ); float d = length(p2)- (isTop ? sub1 : sub2); vec3 grad = vec3(pIn.xy, pIn.z-y)*vec3(p2.x, p2.x, p.x ); return vec4(d, normalize(grad)); } //This shader assumes Y is up, so wrapping it to call the Z up oval vec4 sdgOvalY(vec3 p, float a, float b, float h) { p.xyz = p.xzy; vec4 r= sdgOvalZ(p, a,b,h); r.yzw = r.ywz; return r; } #define AA 3 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // camera movement float an = 0.5*(iTime-10.0); vec3 ro = 1.2*vec3( 1.0*cos(an),1.30, 1.0*sin(an)); vec3 ta = vec3( 0.0, .5, 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)); // animate torus float ra = 0.5 + 0.4*cos(iTime); float rb = min(0.1+0.1*(sin(iTime)), ra*.9); float height = abs(cos(iTime*.5))*.5 +0.6; // render vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; #else vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; #endif // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raymarch const float tmax = 5.0; float t = 0.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; float h = sdgOvalY(pos,ra,rb, height).x; if( h<0.0001 || t>tmax ) break; t += h; } // shading/lighting vec3 col = vec3(0.0); if( t<tmax ) { vec3 pos = ro + t*rd; vec3 nor = sdgOvalY(pos,ra,rb, height).yzw; // compute normal numerically, for comparison // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm #if SHOW_NUMERIC_GRADIENT const vec2 e = vec2(1,-1); const float eps = 0.0002; nor = normalize( e.xyy*sdgOvalY( pos + e.xyy*eps, ra, rb,height ).x + e.yyx*sdgOvalY( pos + e.yyx*eps, ra, rb,height ).x + e.yxy*sdgOvalY( pos + e.yxy*eps, ra, rb,height ).x + e.xxx*sdgOvalY( pos + e.xxx*eps, ra, rb,height ).x ); #endif float dif = clamp( dot(nor,vec3(0.57703)), 0.0, 1.0 ); float amb = 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0)); col = vec3(0.2,0.3,0.4)*amb + vec3(0.85,0.75,0.65)*dif; col *= (0.5+0.5*nor)*(0.5+0.5*nor); } // gamma col = sqrt( col ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 1680, 1865, 1918, 1975, 2468 ]
[ [ 1680, 1865, 1918, 1975, 2468 ], [ 2470, 2538, 2589, 2589, 2675 ] ]
//A Z up oval, similiar to a capsule with a customizable mid radius // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .w = ∂f(p)/∂z // .yzw = ∇f(p) with ‖∇f(p)‖ = 1
vec4 sdgOvalZ(vec3 pIn, float a, float b, float h) {
float r = a - b; //a must be greater than b! float l = (h * h - r * r) / (r+r); float sub2 = (a + l); float sub1 = sub2 - length(vec2(h, l)); vec2 p = vec2(length(pIn.xy), abs(pIn.z) ); bool isTop =((p.y-h)*l) > p.x * h; float y = isTop? h: 0.0; float x = isTop ? 0.0: l; vec2 p2 = vec2( p.x + x, p.y - y ); float d = length(p2)- (isTop ? sub1 : sub2); vec3 grad = vec3(pIn.xy, pIn.z-y)*vec3(p2.x, p2.x, p.x ); return vec4(d, normalize(grad)); }
//A Z up oval, similiar to a capsule with a customizable mid radius // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .w = ∂f(p)/∂z // .yzw = ∇f(p) with ‖∇f(p)‖ = 1 vec4 sdgOvalZ(vec3 pIn, float a, float b, float h) {
1
1
NdsSRM
Dain
2021-04-14T12:40:26
// The MIT License // 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. //Rendering code from IQs torus gradient shader https://www.shadertoy.com/view/wtcfzM // sdgOval returns the oval SDF and its gradient, by // computing it analytically. // Other SDF analytic gradients // Egg: https://www.shadertoy.com/view/7dXSz7 // Oval: https://www.shadertoy.com/view/NdsSRM // Disk: https://www.shadertoy.com/view/NdlSR7 // Box : https://www.shadertoy.com/view/NslSz7 // Other SDF analytic gradients(By IQ): // // Torus: https://www.shadertoy.com/view/wtcfzM // Capsule: https://www.shadertoy.com/view/WttfR7 //Set to 1 to show the finite difference gradient for comparison #define SHOW_NUMERIC_GRADIENT 0 //A Z up oval, similiar to a capsule with a customizable mid radius // .x = f(p) // .y = ∂f(p)/∂x // .z = ∂f(p)/∂y // .w = ∂f(p)/∂z // .yzw = ∇f(p) with ‖∇f(p)‖ = 1 vec4 sdgOvalZ(vec3 pIn, float a, float b, float h) { //These first 4 lines can be precalculated once float r = a - b; //a must be greater than b! float l = (h * h - r * r) / (r+r); float sub2 = (a + l); float sub1 = sub2 - length(vec2(h, l)); vec2 p = vec2(length(pIn.xy), abs(pIn.z) ); bool isTop =((p.y-h)*l) > p.x * h; float y = isTop? h: 0.0; float x = isTop ? 0.0: l; vec2 p2 = vec2( p.x + x, p.y - y ); float d = length(p2)- (isTop ? sub1 : sub2); vec3 grad = vec3(pIn.xy, pIn.z-y)*vec3(p2.x, p2.x, p.x ); return vec4(d, normalize(grad)); } //This shader assumes Y is up, so wrapping it to call the Z up oval vec4 sdgOvalY(vec3 p, float a, float b, float h) { p.xyz = p.xzy; vec4 r= sdgOvalZ(p, a,b,h); r.yzw = r.ywz; return r; } #define AA 3 void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // camera movement float an = 0.5*(iTime-10.0); vec3 ro = 1.2*vec3( 1.0*cos(an),1.30, 1.0*sin(an)); vec3 ta = vec3( 0.0, .5, 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)); // animate torus float ra = 0.5 + 0.4*cos(iTime); float rb = min(0.1+0.1*(sin(iTime)), ra*.9); float height = abs(cos(iTime*.5))*.5 +0.6; // render vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; #else vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; #endif // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raymarch const float tmax = 5.0; float t = 0.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; float h = sdgOvalY(pos,ra,rb, height).x; if( h<0.0001 || t>tmax ) break; t += h; } // shading/lighting vec3 col = vec3(0.0); if( t<tmax ) { vec3 pos = ro + t*rd; vec3 nor = sdgOvalY(pos,ra,rb, height).yzw; // compute normal numerically, for comparison // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm #if SHOW_NUMERIC_GRADIENT const vec2 e = vec2(1,-1); const float eps = 0.0002; nor = normalize( e.xyy*sdgOvalY( pos + e.xyy*eps, ra, rb,height ).x + e.yyx*sdgOvalY( pos + e.yyx*eps, ra, rb,height ).x + e.yxy*sdgOvalY( pos + e.yxy*eps, ra, rb,height ).x + e.xxx*sdgOvalY( pos + e.xxx*eps, ra, rb,height ).x ); #endif float dif = clamp( dot(nor,vec3(0.57703)), 0.0, 1.0 ); float amb = 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0)); col = vec3(0.2,0.3,0.4)*amb + vec3(0.85,0.75,0.65)*dif; col *= (0.5+0.5*nor)*(0.5+0.5*nor); } // gamma col = sqrt( col ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = vec4( tot, 1.0 ); }
mit
[ 2470, 2538, 2589, 2589, 2675 ]
[ [ 1680, 1865, 1918, 1975, 2468 ], [ 2470, 2538, 2589, 2589, 2675 ] ]
//This shader assumes Y is up, so wrapping it to call the Z up oval
vec4 sdgOvalY(vec3 p, float a, float b, float h) {
p.xyz = p.xzy; vec4 r= sdgOvalZ(p, a,b,h); r.yzw = r.ywz; return r; }
//This shader assumes Y is up, so wrapping it to call the Z up oval vec4 sdgOvalY(vec3 p, float a, float b, float h) {
1
1
sdSSzz
mrange
2021-04-27T17:05:27
// License CC0: Crystal skull // Perhaps it's just me that sees a glowing skull captured in a crystal? // Result after continued experimenting with marble fractals and different kinds of trap functions #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PI 3.141592654 #define TAU (2.0*PI) #define L2(x) dot(x, x) #define PCOS(x) (0.5+0.5*cos(x)) #define RAYSHAPE(ro, rd) raySphere4(ro, rd, 0.5) #define IRAYSHAPE(ro, rd) iraySphere4(ro, rd, 0.5) const float miss = 1E4; const float refrIndex = 0.85; const vec3 lightPos = 2.0*vec3(1.5, 2.0, 1.0); const vec3 skyCol1 = pow(vec3(0.2, 0.4, 0.6), vec3(0.25))*1.0; const vec3 skyCol2 = pow(vec3(0.4, 0.7, 1.0), vec3(2.0))*1.0; const vec3 sunCol = vec3(8.0,7.0,6.0)/8.0; float tanh_approx(float x) { // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } // https://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl vec3 hsv2rgb(vec3 c) { const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } // Various ray object intersection from IQ: // https://www.iquilezles.org/www/articles/intersectors/intersectors.htm float raySphere4(vec3 ro, vec3 rd, float ra) { float r2 = ra*ra; vec3 d2 = rd*rd; vec3 d3 = d2*rd; vec3 o2 = ro*ro; vec3 o3 = o2*ro; float ka = 1.0/dot(d2,d2); float k3 = ka* dot(ro,d3); float k2 = ka* dot(o2,d2); float k1 = ka* dot(o3,rd); float k0 = ka*(dot(o2,o2) - r2*r2); float c2 = k2 - k3*k3; float c1 = k1 + 2.0*k3*k3*k3 - 3.0*k3*k2; float c0 = k0 - 3.0*k3*k3*k3*k3 + 6.0*k3*k3*k2 - 4.0*k3*k1; float p = c2*c2 + c0/3.0; float q = c2*c2*c2 - c2*c0 + c1*c1; float h = q*q - p*p*p; if (h<0.0) return miss; //no intersection float sh = sqrt(h); float s = sign(q+sh)*pow(abs(q+sh),1.0/3.0); // cuberoot float t = sign(q-sh)*pow(abs(q-sh),1.0/3.0); // cuberoot vec2 w = vec2( s+t,s-t ); vec2 v = vec2( w.x+c2*4.0, w.y*sqrt(3.0) )*0.5; float r = length(v); return -abs(v.y)/sqrt(r+v.x) - c1/r - k3; } vec3 sphere4Normal(vec3 pos) { return normalize( pos*pos*pos ); } float iraySphere4(vec3 ro, vec3 rd, float ra) { // Computes inner intersection by intersecting a reverse outer intersection vec3 rro = ro + rd*ra*4.0; vec3 rrd = -rd; float rt = raySphere4(rro, rrd, ra); if (rt == miss) return miss; vec3 rpos = rro + rrd*rt; return length(rpos - ro); } float rayPlane(vec3 ro, vec3 rd, vec4 p ) { return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz); } vec3 skyColor(vec3 ro, vec3 rd) { const vec3 sunDir = normalize(lightPos); float sunDot = max(dot(rd, sunDir), 0.0); vec3 final = vec3(0.); final += mix(skyCol1, skyCol2, rd.y); final += 0.5*sunCol*pow(sunDot, 20.0); final += 4.0*sunCol*pow(sunDot, 400.0); float tp = rayPlane(ro, rd, vec4(vec3(0.0, 1.0, 0.0), 0.505)); if (tp > 0.0) { vec3 pos = ro + tp*rd; vec3 ld = normalize(lightPos - pos); float ts4 = RAYSHAPE(pos, ld); vec3 spos = pos + ld*ts4; float its4= IRAYSHAPE(spos, ld); // Extremely fake soft shadows float sha = ts4 == miss ? 1.0 : (1.0-1.0*tanh_approx(its4*1.5/(0.5+.5*ts4))); vec3 nor = vec3(0.0, 1.0, 0.0); vec3 icol = 1.5*skyCol1 + 4.0*sunCol*sha*dot(-rd, nor); vec2 ppos = pos.xz*0.75; ppos = fract(ppos+0.5)-0.5; float pd = min(abs(ppos.x), abs(ppos.y)); vec3 pcol= mix(vec3(0.4), vec3(0.3), exp(-60.0*pd)); vec3 col = icol*pcol; col = clamp(col, 0.0, 1.25); float f = exp(-10.0*(max(tp-10.0, 0.0) / 100.0)); return mix(final, col , f); } else{ return final; } } // Marble fractal from https://www.shadertoy.com/view/MtX3Ws vec2 csqr(vec2 a) { return vec2(a.x*a.x - a.y*a.y, 2.*a.x*a.y); } float l0(vec3 v) { return abs(v.x) + abs(v.y) + abs(v.z); } float marble_df(vec3 p) { float res = 0.; vec3 c = p; const float scale = 0.72; const int max_iter = 8; for (int i = 0; i < max_iter; ++i) { p = scale*abs(p)/L2(p) - scale; p.yz = csqr(p.yz); p = p.zxy; res += exp(-2. * L2(p-c)); } return res; } vec3 marble_march(vec3 ro, vec3 rd, float d, float dist, vec2 tminmax) { float t = tminmax.x; float dt = mix(1.0, 0.02, d); vec3 col = vec3(0.0); float c = 0.; const int max_iter = 64; for(int i = 0; i < max_iter; ++i) { t += dt*exp(-2.0*c); if(t>tminmax.y) { break; } vec3 pos = ro+t*rd; c = marble_df(pos); c *= 0.5; vec3 dcol = vec3(c*c*c-c*dist, c*c-c, c); col = col + dcol; } const float scale = 0.005; float td = (t - tminmax.x)/(tminmax.y - tminmax.x); col *= exp(-10.0*td); col *= scale; return col; } vec3 render1(vec3 ro, vec3 rd, float d) { vec3 ipos = ro; vec3 ird = rd; float its4 = IRAYSHAPE(ipos, ird); float fi = smoothstep(8.75, 10.0, TIME); vec3 dpos = ipos; dpos.x = abs(dpos.x); float dm = mix(0.75, 1.1, (PCOS(-TIME*0.5+8.0*dpos.y))*fi); float dist = 0.0; dist += 0.5*mix(7.0, (length(dpos.xy-vec2(0.1, 0.055))-0.05)*30.0, dm); dist += 0.5*mix(7.0, (length(dpos.xy-vec2(0.0, -0.025))-0.05)*20.0, dm); ipos.z -= mix(0.3, 0.0, fi); ipos.z -= 0.0125*sin(ipos.y*5.0-TIME*sqrt(1.25)); ipos -= vec3(0.0, 0.2, mix(0.25, 0.0, d*d*d*d)); return marble_march(ipos, ird, d, dist, vec2(0.0, its4)); } vec3 render(vec3 ro, vec3 rd) { vec3 skyCol = skyColor(ro, rd); vec3 col = vec3(0.0); float t = 1E6; float ts4 = RAYSHAPE(ro, rd); if (ts4 < miss) { t = ts4; vec3 pos = ro + ts4*rd; vec3 nor = sphere4Normal(pos); vec3 refr = refract(rd, nor, refrIndex); vec3 refl = reflect(rd, nor); vec3 rcol = skyColor(pos, refl); float fre = mix(0.0, 1.0, pow(1.0-dot(-rd, nor), 4.0)); vec3 lv = lightPos - pos; float ll2 = L2(lv); float ll = sqrt(ll2); vec3 ld = lv / ll; float dm = min(1.0, 40.0/ll2); float dif = pow(max(dot(nor,ld),0.0), 8.0)*dm; float spe = pow(max(dot(reflect(-ld, nor), -rd), 0.), 100.); float l = dif; float d = dot(rd, refr); float lin = mix(0.0, 1.0, l); const vec3 lcol = 2.0*sqrt(sunCol); col = render1(pos, refr, d); vec3 diff = hsv2rgb(vec3(0.7, fre, 0.075*lin))*lcol; col += fre*rcol+diff+spe*lcol; if (refr == vec3(0.0)) { // Not expected to happen as the refraction index < 1.0 col = vec3(1.0, 0.0, 0.0); } } else { // Ray intersected sky return skyCol; } return col; } vec3 effect(vec2 p, vec2 q) { vec3 start = 1.5*vec3(0.5, 0.5, -2.0); vec3 end = mix(0.4, 0.6, PCOS(TIME*0.1))*vec3(0.0, 0.2, -2.0); float fi = smoothstep(1.0, 10.0, TIME); vec3 ro = mix(start, end, fi); ro.zy *= ROT(0.1*sin(TIME*sqrt(0.05))*fi); ro.xz *= ROT(0.1*sin(TIME*sqrt(0.02))*fi); vec3 ww = normalize(vec3(0.0, 0.0, 0.0) - ro); vec3 uu = normalize(cross( vec3(0.0,1.0,0.0), ww)); vec3 vv = normalize(cross(ww,uu)); float rdd = 2.0+0.5*tanh_approx(length(p)); vec3 rd = normalize( p.x*uu + p.y*vv + rdd*ww); vec3 col = render(ro, rd); return col; } vec3 postProcess(vec3 col, vec2 q) { col = clamp(col, 0.0, 1.0); col = pow(col, vec3(1.0/2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); col *=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord/RESOLUTION.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; vec3 col = effect(p, q); col = postProcess(col, q); col = mix(vec3(0.0), col, smoothstep(0.0, 3.0, TIME)); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 1304, 1422, 1468, 1468, 2313 ]
[ [ 906, 906, 934, 954, 1030 ], [ 1032, 1111, 1133, 1133, 1302 ], [ 1304, 1422, 1468, 1468, 2313 ], [ 2315, 2315, 2345, 2345, 2382 ], [ 2384, 2384, 2431, 2509, 2688 ], [ 2690, 2690, 2733, 2733, 2780 ], [ 2782, 2782, 2815, 2815, 3878 ], [ 3880, 3941, 3960, 3960, 4010 ], [ 4012, 4012, 4030, 4030, 4073 ], [ 4075, 4075, 4100, 4100, 4361 ], [ 4363, 4363, 4435, 4435, 4983 ], [ 4985, 4985, 5026, 5026, 5627 ], [ 5629, 5629, 5660, 5660, 6775 ], [ 6777, 6777, 6806, 6806, 7382 ], [ 7384, 7384, 7420, 7420, 7653 ], [ 7655, 7655, 7710, 7710, 7954 ] ]
// Various ray object intersection from IQ: // https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
float raySphere4(vec3 ro, vec3 rd, float ra) {
float r2 = ra*ra; vec3 d2 = rd*rd; vec3 d3 = d2*rd; vec3 o2 = ro*ro; vec3 o3 = o2*ro; float ka = 1.0/dot(d2,d2); float k3 = ka* dot(ro,d3); float k2 = ka* dot(o2,d2); float k1 = ka* dot(o3,rd); float k0 = ka*(dot(o2,o2) - r2*r2); float c2 = k2 - k3*k3; float c1 = k1 + 2.0*k3*k3*k3 - 3.0*k3*k2; float c0 = k0 - 3.0*k3*k3*k3*k3 + 6.0*k3*k3*k2 - 4.0*k3*k1; float p = c2*c2 + c0/3.0; float q = c2*c2*c2 - c2*c0 + c1*c1; float h = q*q - p*p*p; if (h<0.0) return miss; //no intersection float sh = sqrt(h); float s = sign(q+sh)*pow(abs(q+sh),1.0/3.0); // cuberoot float t = sign(q-sh)*pow(abs(q-sh),1.0/3.0); // cuberoot vec2 w = vec2( s+t,s-t ); vec2 v = vec2( w.x+c2*4.0, w.y*sqrt(3.0) )*0.5; float r = length(v); return -abs(v.y)/sqrt(r+v.x) - c1/r - k3; }
// Various ray object intersection from IQ: // https://www.iquilezles.org/www/articles/intersectors/intersectors.htm float raySphere4(vec3 ro, vec3 rd, float ra) {
2
3
sdSSzz
mrange
2021-04-27T17:05:27
// License CC0: Crystal skull // Perhaps it's just me that sees a glowing skull captured in a crystal? // Result after continued experimenting with marble fractals and different kinds of trap functions #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PI 3.141592654 #define TAU (2.0*PI) #define L2(x) dot(x, x) #define PCOS(x) (0.5+0.5*cos(x)) #define RAYSHAPE(ro, rd) raySphere4(ro, rd, 0.5) #define IRAYSHAPE(ro, rd) iraySphere4(ro, rd, 0.5) const float miss = 1E4; const float refrIndex = 0.85; const vec3 lightPos = 2.0*vec3(1.5, 2.0, 1.0); const vec3 skyCol1 = pow(vec3(0.2, 0.4, 0.6), vec3(0.25))*1.0; const vec3 skyCol2 = pow(vec3(0.4, 0.7, 1.0), vec3(2.0))*1.0; const vec3 sunCol = vec3(8.0,7.0,6.0)/8.0; float tanh_approx(float x) { // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } // https://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl vec3 hsv2rgb(vec3 c) { const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } // Various ray object intersection from IQ: // https://www.iquilezles.org/www/articles/intersectors/intersectors.htm float raySphere4(vec3 ro, vec3 rd, float ra) { float r2 = ra*ra; vec3 d2 = rd*rd; vec3 d3 = d2*rd; vec3 o2 = ro*ro; vec3 o3 = o2*ro; float ka = 1.0/dot(d2,d2); float k3 = ka* dot(ro,d3); float k2 = ka* dot(o2,d2); float k1 = ka* dot(o3,rd); float k0 = ka*(dot(o2,o2) - r2*r2); float c2 = k2 - k3*k3; float c1 = k1 + 2.0*k3*k3*k3 - 3.0*k3*k2; float c0 = k0 - 3.0*k3*k3*k3*k3 + 6.0*k3*k3*k2 - 4.0*k3*k1; float p = c2*c2 + c0/3.0; float q = c2*c2*c2 - c2*c0 + c1*c1; float h = q*q - p*p*p; if (h<0.0) return miss; //no intersection float sh = sqrt(h); float s = sign(q+sh)*pow(abs(q+sh),1.0/3.0); // cuberoot float t = sign(q-sh)*pow(abs(q-sh),1.0/3.0); // cuberoot vec2 w = vec2( s+t,s-t ); vec2 v = vec2( w.x+c2*4.0, w.y*sqrt(3.0) )*0.5; float r = length(v); return -abs(v.y)/sqrt(r+v.x) - c1/r - k3; } vec3 sphere4Normal(vec3 pos) { return normalize( pos*pos*pos ); } float iraySphere4(vec3 ro, vec3 rd, float ra) { // Computes inner intersection by intersecting a reverse outer intersection vec3 rro = ro + rd*ra*4.0; vec3 rrd = -rd; float rt = raySphere4(rro, rrd, ra); if (rt == miss) return miss; vec3 rpos = rro + rrd*rt; return length(rpos - ro); } float rayPlane(vec3 ro, vec3 rd, vec4 p ) { return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz); } vec3 skyColor(vec3 ro, vec3 rd) { const vec3 sunDir = normalize(lightPos); float sunDot = max(dot(rd, sunDir), 0.0); vec3 final = vec3(0.); final += mix(skyCol1, skyCol2, rd.y); final += 0.5*sunCol*pow(sunDot, 20.0); final += 4.0*sunCol*pow(sunDot, 400.0); float tp = rayPlane(ro, rd, vec4(vec3(0.0, 1.0, 0.0), 0.505)); if (tp > 0.0) { vec3 pos = ro + tp*rd; vec3 ld = normalize(lightPos - pos); float ts4 = RAYSHAPE(pos, ld); vec3 spos = pos + ld*ts4; float its4= IRAYSHAPE(spos, ld); // Extremely fake soft shadows float sha = ts4 == miss ? 1.0 : (1.0-1.0*tanh_approx(its4*1.5/(0.5+.5*ts4))); vec3 nor = vec3(0.0, 1.0, 0.0); vec3 icol = 1.5*skyCol1 + 4.0*sunCol*sha*dot(-rd, nor); vec2 ppos = pos.xz*0.75; ppos = fract(ppos+0.5)-0.5; float pd = min(abs(ppos.x), abs(ppos.y)); vec3 pcol= mix(vec3(0.4), vec3(0.3), exp(-60.0*pd)); vec3 col = icol*pcol; col = clamp(col, 0.0, 1.25); float f = exp(-10.0*(max(tp-10.0, 0.0) / 100.0)); return mix(final, col , f); } else{ return final; } } // Marble fractal from https://www.shadertoy.com/view/MtX3Ws vec2 csqr(vec2 a) { return vec2(a.x*a.x - a.y*a.y, 2.*a.x*a.y); } float l0(vec3 v) { return abs(v.x) + abs(v.y) + abs(v.z); } float marble_df(vec3 p) { float res = 0.; vec3 c = p; const float scale = 0.72; const int max_iter = 8; for (int i = 0; i < max_iter; ++i) { p = scale*abs(p)/L2(p) - scale; p.yz = csqr(p.yz); p = p.zxy; res += exp(-2. * L2(p-c)); } return res; } vec3 marble_march(vec3 ro, vec3 rd, float d, float dist, vec2 tminmax) { float t = tminmax.x; float dt = mix(1.0, 0.02, d); vec3 col = vec3(0.0); float c = 0.; const int max_iter = 64; for(int i = 0; i < max_iter; ++i) { t += dt*exp(-2.0*c); if(t>tminmax.y) { break; } vec3 pos = ro+t*rd; c = marble_df(pos); c *= 0.5; vec3 dcol = vec3(c*c*c-c*dist, c*c-c, c); col = col + dcol; } const float scale = 0.005; float td = (t - tminmax.x)/(tminmax.y - tminmax.x); col *= exp(-10.0*td); col *= scale; return col; } vec3 render1(vec3 ro, vec3 rd, float d) { vec3 ipos = ro; vec3 ird = rd; float its4 = IRAYSHAPE(ipos, ird); float fi = smoothstep(8.75, 10.0, TIME); vec3 dpos = ipos; dpos.x = abs(dpos.x); float dm = mix(0.75, 1.1, (PCOS(-TIME*0.5+8.0*dpos.y))*fi); float dist = 0.0; dist += 0.5*mix(7.0, (length(dpos.xy-vec2(0.1, 0.055))-0.05)*30.0, dm); dist += 0.5*mix(7.0, (length(dpos.xy-vec2(0.0, -0.025))-0.05)*20.0, dm); ipos.z -= mix(0.3, 0.0, fi); ipos.z -= 0.0125*sin(ipos.y*5.0-TIME*sqrt(1.25)); ipos -= vec3(0.0, 0.2, mix(0.25, 0.0, d*d*d*d)); return marble_march(ipos, ird, d, dist, vec2(0.0, its4)); } vec3 render(vec3 ro, vec3 rd) { vec3 skyCol = skyColor(ro, rd); vec3 col = vec3(0.0); float t = 1E6; float ts4 = RAYSHAPE(ro, rd); if (ts4 < miss) { t = ts4; vec3 pos = ro + ts4*rd; vec3 nor = sphere4Normal(pos); vec3 refr = refract(rd, nor, refrIndex); vec3 refl = reflect(rd, nor); vec3 rcol = skyColor(pos, refl); float fre = mix(0.0, 1.0, pow(1.0-dot(-rd, nor), 4.0)); vec3 lv = lightPos - pos; float ll2 = L2(lv); float ll = sqrt(ll2); vec3 ld = lv / ll; float dm = min(1.0, 40.0/ll2); float dif = pow(max(dot(nor,ld),0.0), 8.0)*dm; float spe = pow(max(dot(reflect(-ld, nor), -rd), 0.), 100.); float l = dif; float d = dot(rd, refr); float lin = mix(0.0, 1.0, l); const vec3 lcol = 2.0*sqrt(sunCol); col = render1(pos, refr, d); vec3 diff = hsv2rgb(vec3(0.7, fre, 0.075*lin))*lcol; col += fre*rcol+diff+spe*lcol; if (refr == vec3(0.0)) { // Not expected to happen as the refraction index < 1.0 col = vec3(1.0, 0.0, 0.0); } } else { // Ray intersected sky return skyCol; } return col; } vec3 effect(vec2 p, vec2 q) { vec3 start = 1.5*vec3(0.5, 0.5, -2.0); vec3 end = mix(0.4, 0.6, PCOS(TIME*0.1))*vec3(0.0, 0.2, -2.0); float fi = smoothstep(1.0, 10.0, TIME); vec3 ro = mix(start, end, fi); ro.zy *= ROT(0.1*sin(TIME*sqrt(0.05))*fi); ro.xz *= ROT(0.1*sin(TIME*sqrt(0.02))*fi); vec3 ww = normalize(vec3(0.0, 0.0, 0.0) - ro); vec3 uu = normalize(cross( vec3(0.0,1.0,0.0), ww)); vec3 vv = normalize(cross(ww,uu)); float rdd = 2.0+0.5*tanh_approx(length(p)); vec3 rd = normalize( p.x*uu + p.y*vv + rdd*ww); vec3 col = render(ro, rd); return col; } vec3 postProcess(vec3 col, vec2 q) { col = clamp(col, 0.0, 1.0); col = pow(col, vec3(1.0/2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); col *=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord/RESOLUTION.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; vec3 col = effect(p, q); col = postProcess(col, q); col = mix(vec3(0.0), col, smoothstep(0.0, 3.0, TIME)); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 3880, 3941, 3960, 3960, 4010 ]
[ [ 906, 906, 934, 954, 1030 ], [ 1032, 1111, 1133, 1133, 1302 ], [ 1304, 1422, 1468, 1468, 2313 ], [ 2315, 2315, 2345, 2345, 2382 ], [ 2384, 2384, 2431, 2509, 2688 ], [ 2690, 2690, 2733, 2733, 2780 ], [ 2782, 2782, 2815, 2815, 3878 ], [ 3880, 3941, 3960, 3960, 4010 ], [ 4012, 4012, 4030, 4030, 4073 ], [ 4075, 4075, 4100, 4100, 4361 ], [ 4363, 4363, 4435, 4435, 4983 ], [ 4985, 4985, 5026, 5026, 5627 ], [ 5629, 5629, 5660, 5660, 6775 ], [ 6777, 6777, 6806, 6806, 7382 ], [ 7384, 7384, 7420, 7420, 7653 ], [ 7655, 7655, 7710, 7710, 7954 ] ]
// Marble fractal from https://www.shadertoy.com/view/MtX3Ws
vec2 csqr(vec2 a) {
return vec2(a.x*a.x - a.y*a.y, 2.*a.x*a.y); }
// Marble fractal from https://www.shadertoy.com/view/MtX3Ws vec2 csqr(vec2 a) {
1
3
flsGDH
blackle
2021-05-26T07:32:11
//CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/ //To the extent possible under law, Blackle Mori has waived all copyright and related or neighboring rights to this work. //this is further explorations in how domain repetition works //and under what scenarios a naively domain repeated object might fail to be an SDF //this is a visualization of how the closest neighbour SDF inside a domain changes as those SDFs change //if we imagine a point within a domain, we can ask "what is the closest neighbouring SDF?" //in this case, we have 6 boxes in the immediate neighbouring domains. //as we move a point within the central domain around, the closest box to that point will change //we can visualze the boundaries at which a point stops being closest to one box, and starts being closest to a different box //inside the middle box of this shader is such a visualization //as the dimensions of the neighbouring boxes change, the shape of the boundaries change. //when the boxes are perfect cubes, we can model the boundaries with //the "face" function in this shader: https://www.shadertoy.com/view/Wl3fD2 //see also the youtube video: https://youtu.be/I8fmkLK1OKg vec3 erot(vec3 p, vec3 ax, float ro) { return mix(dot(ax,p)*ax,p,cos(ro)) + sin(ro)*cross(ax,p); } float box(vec3 p, vec3 d) { p = abs(p)-d; return length(max(p,0.)) + min(0.,max(max(p.x,p.y),p.z)); } float obj(vec3 p) { return box(p, vec3(.25,.25,.25+sin(iTime)*.24)) - .01; } bool sort(inout float a, inout float b) { if (b < a) { float tmp = a; a = b; b = tmp; return true; } return false; } int gid; float scene(vec3 p) { float u = obj(p - vec3(0,0,1)); float d = obj(p - vec3(0,0,-1)); float e = obj(p - vec3(0,1,0)); float w = obj(p - vec3(0,-1,0)); float n = obj(p - vec3(1,0,0)); float s = obj(p - vec3(-1,0,0)); gid = 0; if (sort(u,d)) gid = 1; sort(d,e); sort(e,w); sort(w,n); sort(n,s); if (sort(u,d)) gid = 2; sort(d,e); sort(e,w); sort(w,n); if (sort(u,d)) gid = 3; sort(d,e); sort(e,w); if (sort(u,d)) gid = 4; sort(d,e); if (sort(u,d)) gid = 5; float closest = u; float secondclosest = d; float boundary = (abs(closest-secondclosest)-.01)/2.; boundary = max(boundary, box(p, vec3(.5))); return min(closest,boundary); } vec3 norm(vec3 p) { mat3 k = mat3(p,p,p) - mat3(0.0001); return normalize(scene(p) - vec3(scene(k[0]),scene(k[1]),scene(k[2]))); } // https://iquilezles.org/www/articles/palettes/palettes.htm vec3 palette( float t ) { return cos(t+vec3(0,1.8,3.2))*.4+.6; } vec3 shade(vec3 p, vec3 cam) { float fid = float(gid); vec3 n = norm(p); vec3 r = reflect(cam,n); float fres = 1.-abs(dot(cam,n))*.98; float fact = length(sin(r*3.5)*.5+.5)/sqrt(3.0); return palette(fid)*(fact*.5 + pow(fact,5.)*4.*fres); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (fragCoord-iResolution.xy*.5)/iResolution.y; vec3 cam = normalize(vec3(.8,uv)); vec3 init = vec3(-2.5,0,0); float yrot = .2; float zrot = iTime/2.; cam = erot(cam,vec3(0,1,0),yrot); init = erot(init,vec3(0,1,0),yrot); cam = erot(cam,vec3(0,0,1),zrot); init = erot(init,vec3(0,0,1),zrot); vec3 p = init; bool hit = false; vec3 col = vec3(0); float atten = .7; float k = 1.; for (int i = 0; i < 200; i++ ) { float dist = scene(p); p += cam*dist*k; if (dist*dist < 1e-7) { col += shade(p, cam)*atten; atten *= .7; p += cam*.005; k = sign(scene(p)); } if(distance(p,init)>100.)break; } vec3 spec = shade(p, cam); fragColor = vec4(sqrt(smoothstep(0.,1.,col)),1.0); }
cc0-1.0
[ 2564, 2625, 2650, 2650, 2693 ]
[ [ 1190, 1190, 1228, 1228, 1292 ], [ 1294, 1294, 1321, 1321, 1403 ], [ 1405, 1405, 1424, 1424, 1485 ], [ 1487, 1487, 1528, 1528, 1639 ], [ 1650, 1650, 1671, 1671, 2422 ], [ 2424, 2424, 2443, 2443, 2562 ], [ 2564, 2625, 2650, 2650, 2693 ], [ 2695, 2695, 2725, 2725, 2958 ], [ 2960, 2960, 3017, 3017, 3865 ] ]
// https://iquilezles.org/www/articles/palettes/palettes.htm
vec3 palette( float t ) {
return cos(t+vec3(0,1.8,3.2))*.4+.6; }
// https://iquilezles.org/www/articles/palettes/palettes.htm vec3 palette( float t ) {
1
8
NtsGWH
blackle
2021-05-26T05:42:13
//CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/ //To the extent possible under law, Blackle Mori has waived all copyright and related or neighboring rights to this work. float nozerosgn(float x) { return step(0.,x)*2.-1.; } vec2 nozerosgn(vec2 x) { return step(0.,x)*2.-1.; } //returns the vectors pointing to each edge of the box with dimensions d, //ordered by closeness to the point p. only valid inside the rectangle void edge4(vec2 p, vec2 d, inout vec2 e1, inout vec2 e2, inout vec2 e3, inout vec2 e4) { //this probably has some really elegant underlying structure, but I'm too tired to figure it out vec3 p3 = vec3(nozerosgn(p), 0); //this lets us construct the edge vectors p = abs(p); float c2 = nozerosgn(p.x+p.y-d.x-d.y+min(d.x,d.y)*2.); e1 = (p.x-d.x < p.y-d.y) ? p3.zy : p3.xz; e2 = c2*((c2 < 0. == p.x-d.x < p.y-d.y) ? p3.zy : p3.xz); e3 = -c2*((c2 < 0. == p.x+d.x < p.y+d.y) ? p3.zy : p3.xz); e4 = (p.x+d.x < p.y+d.y) ? -p3.zy : -p3.xz; } //rest of this is visualization code //colours in box cycle between the boundaries for the 1st, 2nd, 3rd, and 4th closest edge. float linedist(vec2 p, vec2 a, vec2 b) { float k = dot(p-a,b-a)/dot(b-a,b-a); return length(p-mix(a,b,clamp(k,0.,1.))); } vec2 closestonline(vec2 p, vec2 a, vec2 b) { float k = dot(p-a,b-a)/dot(b-a,b-a); return mix(a,b,clamp(k,0.,1.)); } float aa(float x) { return smoothstep(0., 1.5/iResolution.y, x); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (fragCoord-iResolution.xy*.5)/iResolution.y; vec2 mouse = (iMouse.xy-iResolution.xy*.5)/iResolution.y; vec2 d = vec2(sin(iTime/3.)*.5+1., -sin(iTime/3.)*.5+1.)*.3; vec2 p = vec2(sin(iTime), cos(iTime*3./2.))*min(d.x,d.y); if (iMouse.z > 0.) p = mouse; vec2 e1, e2, e3, e4; edge4(p, d, e1, e2, e3, e4); float d1 = linedist(uv, d*e1 + d*e1.yx, d*e1 - d*e1.yx); float d2 = linedist(uv, d*e2 + d*e2.yx, d*e2 - d*e2.yx); float d3 = linedist(uv, d*e3 + d*e3.yx, d*e3 - d*e3.yx); float d4 = linedist(uv, d*e4 + d*e4.yx, d*e4 - d*e4.yx); vec2 c1 = closestonline(p, d*e1 + d*e1.yx, d*e1 - d*e1.yx); vec2 c2 = closestonline(p, d*e2 + d*e2.yx, d*e2 - d*e2.yx); vec2 c3 = closestonline(p, d*e3 + d*e3.yx, d*e3 - d*e3.yx); vec2 c4 = closestonline(p, d*e4 + d*e4.yx, d*e4 - d*e4.yx); float dd1 = linedist(uv, p, c1); float dd2 = linedist(uv, p, c2); float dd3 = linedist(uv, p, c3); float dd4 = linedist(uv, p, c4); edge4(uv, d, e1, e2, e3, e4); bool vb1 = sin(iTime/4.) < 0.; bool vb2 = sin(iTime/2.) < 0.; vec2 vis = vb1 ? (vb2 ? e1 : e3) : (vb2 ? e2 : e4); vec3 col = length(max(abs(uv)-d,0.)) > 0. ? vec3(1) : vec3(vis*.3+.7,1.); float dmin = min(min(min(d1,dd1),min(d2,dd2)),min(min(d3,dd3),min(d4,dd4))); col = mix(vec3(.00), col, aa(dmin-.007)); col = mix(vec3(.75), col, aa(min(d4,dd4)-.005)); col = mix(vec3(.50), col, aa(min(d3,dd3)-.005)); col = mix(vec3(.25), col, aa(min(d2,dd2)-.005)); col = mix(vec3(.00), col, aa(min(d1,dd1)-.005)); col = mix(vec3(.85,.05,.05), col, aa(distance(p,uv)-.020)); // Output to screen fragColor = vec4(sqrt(col),1.0); }
cc0-1.0
[ 303, 448, 536, 633, 1009 ]
[ [ 194, 194, 220, 220, 247 ], [ 248, 248, 274, 274, 301 ], [ 303, 448, 536, 633, 1009 ], [ 1011, 1139, 1179, 1179, 1268 ], [ 1269, 1269, 1313, 1313, 1392 ], [ 1394, 1394, 1413, 1413, 1464 ], [ 1466, 1466, 1523, 1523, 3230 ] ]
//returns the vectors pointing to each edge of the box with dimensions d, //ordered by closeness to the point p. only valid inside the rectangle
void edge4(vec2 p, vec2 d, inout vec2 e1, inout vec2 e2, inout vec2 e3, inout vec2 e4) {
vec3 p3 = vec3(nozerosgn(p), 0); //this lets us construct the edge vectors p = abs(p); float c2 = nozerosgn(p.x+p.y-d.x-d.y+min(d.x,d.y)*2.); e1 = (p.x-d.x < p.y-d.y) ? p3.zy : p3.xz; e2 = c2*((c2 < 0. == p.x-d.x < p.y-d.y) ? p3.zy : p3.xz); e3 = -c2*((c2 < 0. == p.x+d.x < p.y+d.y) ? p3.zy : p3.xz); e4 = (p.x+d.x < p.y+d.y) ? -p3.zy : -p3.xz; }
//returns the vectors pointing to each edge of the box with dimensions d, //ordered by closeness to the point p. only valid inside the rectangle void edge4(vec2 p, vec2 d, inout vec2 e1, inout vec2 e2, inout vec2 e3, inout vec2 e4) {
1
1
NtsGWH
blackle
2021-05-26T05:42:13
//CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/ //To the extent possible under law, Blackle Mori has waived all copyright and related or neighboring rights to this work. float nozerosgn(float x) { return step(0.,x)*2.-1.; } vec2 nozerosgn(vec2 x) { return step(0.,x)*2.-1.; } //returns the vectors pointing to each edge of the box with dimensions d, //ordered by closeness to the point p. only valid inside the rectangle void edge4(vec2 p, vec2 d, inout vec2 e1, inout vec2 e2, inout vec2 e3, inout vec2 e4) { //this probably has some really elegant underlying structure, but I'm too tired to figure it out vec3 p3 = vec3(nozerosgn(p), 0); //this lets us construct the edge vectors p = abs(p); float c2 = nozerosgn(p.x+p.y-d.x-d.y+min(d.x,d.y)*2.); e1 = (p.x-d.x < p.y-d.y) ? p3.zy : p3.xz; e2 = c2*((c2 < 0. == p.x-d.x < p.y-d.y) ? p3.zy : p3.xz); e3 = -c2*((c2 < 0. == p.x+d.x < p.y+d.y) ? p3.zy : p3.xz); e4 = (p.x+d.x < p.y+d.y) ? -p3.zy : -p3.xz; } //rest of this is visualization code //colours in box cycle between the boundaries for the 1st, 2nd, 3rd, and 4th closest edge. float linedist(vec2 p, vec2 a, vec2 b) { float k = dot(p-a,b-a)/dot(b-a,b-a); return length(p-mix(a,b,clamp(k,0.,1.))); } vec2 closestonline(vec2 p, vec2 a, vec2 b) { float k = dot(p-a,b-a)/dot(b-a,b-a); return mix(a,b,clamp(k,0.,1.)); } float aa(float x) { return smoothstep(0., 1.5/iResolution.y, x); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (fragCoord-iResolution.xy*.5)/iResolution.y; vec2 mouse = (iMouse.xy-iResolution.xy*.5)/iResolution.y; vec2 d = vec2(sin(iTime/3.)*.5+1., -sin(iTime/3.)*.5+1.)*.3; vec2 p = vec2(sin(iTime), cos(iTime*3./2.))*min(d.x,d.y); if (iMouse.z > 0.) p = mouse; vec2 e1, e2, e3, e4; edge4(p, d, e1, e2, e3, e4); float d1 = linedist(uv, d*e1 + d*e1.yx, d*e1 - d*e1.yx); float d2 = linedist(uv, d*e2 + d*e2.yx, d*e2 - d*e2.yx); float d3 = linedist(uv, d*e3 + d*e3.yx, d*e3 - d*e3.yx); float d4 = linedist(uv, d*e4 + d*e4.yx, d*e4 - d*e4.yx); vec2 c1 = closestonline(p, d*e1 + d*e1.yx, d*e1 - d*e1.yx); vec2 c2 = closestonline(p, d*e2 + d*e2.yx, d*e2 - d*e2.yx); vec2 c3 = closestonline(p, d*e3 + d*e3.yx, d*e3 - d*e3.yx); vec2 c4 = closestonline(p, d*e4 + d*e4.yx, d*e4 - d*e4.yx); float dd1 = linedist(uv, p, c1); float dd2 = linedist(uv, p, c2); float dd3 = linedist(uv, p, c3); float dd4 = linedist(uv, p, c4); edge4(uv, d, e1, e2, e3, e4); bool vb1 = sin(iTime/4.) < 0.; bool vb2 = sin(iTime/2.) < 0.; vec2 vis = vb1 ? (vb2 ? e1 : e3) : (vb2 ? e2 : e4); vec3 col = length(max(abs(uv)-d,0.)) > 0. ? vec3(1) : vec3(vis*.3+.7,1.); float dmin = min(min(min(d1,dd1),min(d2,dd2)),min(min(d3,dd3),min(d4,dd4))); col = mix(vec3(.00), col, aa(dmin-.007)); col = mix(vec3(.75), col, aa(min(d4,dd4)-.005)); col = mix(vec3(.50), col, aa(min(d3,dd3)-.005)); col = mix(vec3(.25), col, aa(min(d2,dd2)-.005)); col = mix(vec3(.00), col, aa(min(d1,dd1)-.005)); col = mix(vec3(.85,.05,.05), col, aa(distance(p,uv)-.020)); // Output to screen fragColor = vec4(sqrt(col),1.0); }
cc0-1.0
[ 1011, 1139, 1179, 1179, 1268 ]
[ [ 194, 194, 220, 220, 247 ], [ 248, 248, 274, 274, 301 ], [ 303, 448, 536, 633, 1009 ], [ 1011, 1139, 1179, 1179, 1268 ], [ 1269, 1269, 1313, 1313, 1392 ], [ 1394, 1394, 1413, 1413, 1464 ], [ 1466, 1466, 1523, 1523, 3230 ] ]
//rest of this is visualization code //colours in box cycle between the boundaries for the 1st, 2nd, 3rd, and 4th closest edge.
float linedist(vec2 p, vec2 a, vec2 b) {
float k = dot(p-a,b-a)/dot(b-a,b-a); return length(p-mix(a,b,clamp(k,0.,1.))); }
//rest of this is visualization code //colours in box cycle between the boundaries for the 1st, 2nd, 3rd, and 4th closest edge. float linedist(vec2 p, vec2 a, vec2 b) {
1
14
7l23zK
weasel
2021-06-22T13:35:25
// The MIT License // Copyright © 2021 Henrik Dick // 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. #define PI 3.14159265359 /*** math heavy part for spherical harmonics ***/ #define SQRT2PI 2.506628274631 // factorial float fac(int n) { float res = 1.0; for (int i = n; i > 1; i--) res *= float(i); return res; } // double factorial float dfac(int n) { float res = 1.0; for (int i = n; i > 1; i-=2) res *= float(i); return res; } // fac(l-m)/fac(l+m) but more stable float fac2(int l, int m) { int am = abs(m); if (am > l) return 0.0; float res = 1.0; for (int i = max(l-am+1,2); i <= l+am; i++) res *= float(i); if (m < 0) return res; return 1.0 / res; } // complex exponential vec2 cexp(vec2 c) { return exp(c.x)*vec2(cos(c.y), sin(c.y)); } // complex multiplication vec2 cmul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } // complex conjugation vec2 conj(vec2 c) { return vec2(c.x, -c.y); } // complex/real magnitude squared float sqr(float x) { return x*x; } float sqr(vec2 x) { return dot(x,x); } // associated legendre polynomials float legendre_poly(float x, int l, int m) { if (l < abs(m)) return 0.0; if (l == 0) return 1.0; float mul = m >= 0 ? 1.0 : float((~m&1)*2-1)*fac2(l,m); m = abs(m); // recursive calculation of legendre polynomial float lp1 = 0.0; float lp2 = float((~m&1)*2-1)*dfac(2*m-1)*pow(max(1.0-x*x, 1e-7), float(m)/2.0); for (int i = m+1; i <= l; i++) { float lp = (x*float(2*i-1)*lp2 - float(i+m-1)*lp1)/float(i-m); lp1 = lp2; lp2 = lp; } return lp2 / mul; } // spherical harmonics function vec2 sphere_harm(float theta, float phi, int l, int m) { float abs_value = 1.0/SQRT2PI*sqrt(float(2*l+1)/2.0*fac2(l,m)) *legendre_poly(cos(theta), l, m); return cexp(vec2(0.0,float(m)*phi))*abs_value; } // associated laguerre polynomial L_s^k(x) with k > 0, s >= 0 float laguerre_poly(float x, int s, int k) { if (s <= 0) return 1.0; float lp1 = 1.0; float lp2 = 1.0 - x + float(k); for (int n = 1; n < s; n++) { float lp = ((float(2*n + k + 1) - x) * lp2 - float(n+k)*lp1)/float(n+1); lp1 = lp2; lp2 = lp; } return lp2; } // radius dependent term of the 1/r potential eigenstates in atomic units float radius_term(float r, int n, int l) { float a0 = 1.0; // atomic radius float rr = r / a0; float n2 = 2.0 / float(n) / a0; float n3 = n2 * n2 * n2; float p1 = sqrt(n3 * fac2(n, l) * float(n-l)/float(n)); float p2 = exp(-rr/float(n)); float p3 = pow(n2*r, float(l)); float p4 = laguerre_poly(n2*r, n-l-1, 2*l+1); return p1 * p2 * p3 * p4; } vec2 hydrogen(vec3 pos, int n, int l, int m) { float r = length(pos); float sin_theta = length(pos.xy); float phi = sin_theta > 0.0 ? atan(pos.x, pos.y) : 0.0; float theta = atan(sin_theta, pos.z);//atan(sin_theta, pos.z); return sphere_harm(theta, phi, l, m) * radius_term(r, n, l); } /*** Now the rendering ***/ vec3 rotateX(vec3 pos, float angle) { return vec3(pos.x, cmul(pos.yz, cexp(vec2(0.,-angle)))); } #define SELECT_GRID 7.0 void get_nlm(out int n, out int l, out int m, in vec2 fragCoord) { vec2 mouse = iMouse.xy/iResolution.xy; int t; bool selection = false; if (mouse.x + mouse.y > 0.0) {// && iMouse.z > 0.5) { vec2 coord = iMouse.z > 0.5 ? fragCoord : iMouse.xy; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); //t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID); t = cell.x; n = cell.y + 1; selection = t < n*(n+1)/2 || iMouse.z > 0.5; } if (!selection) { /*vec2 coord = fragCoord; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); cell.x += 0; t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID);*/ t = int(iTime*0.5); if (t == 0) n = 1; else { float x = float(t); // see https://en.wikipedia.org/wiki/Tetrahedral_number n = int(ceil(pow(3.*x+sqrt(9.*x*x-1./27.), 1./3.) + pow(3.*x-sqrt(9.*x*x-1./27.), 1./3.) - 0.995)); } t -= ((n*(n-1)*(2*n-1))/6+(n*(n-1))/2)/2; } l = int(floor(sqrt(0.25 + float(2*t)) - 0.5)); m = t - int(floor(0.5*float(l + l*l))); } float spos(float x, float s) { return 0.5*(x*x/(s+abs(x))+x+s); } float smax(float a, float b, float s) { return a+spos(b-a,s); } #define SURFACE_LEVEL 0.3 float globalSdf(vec3 pos, out vec3 color, in vec2 fragCoord) { int n, m, l; get_nlm(n, l, m, fragCoord); // evaluate spherical harmonics vec2 off = cexp(vec2(0, iTime)); vec2 H = hydrogen(pos*float(n*n+1)*1.5, n, l, m); if (m != 0) H = cmul(H, off); H *= float((l+1)*l+n*n)*sqrt(float(n)); // visual rescaling float crit2 = 0.3*(length(pos)+0.05); color = H.x > 0. ? vec3(1.0,0.6,0.15) : vec3(0.2,0.4,0.5); //color = vec3(max(vec3(0.02),(sin(float(n) + vec3(0., 2.1, 4.2))))); float d = (SURFACE_LEVEL - abs(H.x))*crit2; if (m == 0) return smax(d, 0.707*(pos.x+pos.y), 0.02); return d; float arg = atan(H.x, H.y); color = vec3(max(vec3(0.02),(sin(arg + vec3(0., 2.1, 4.2))))); return (0.20 - length(H))*crit2; } vec3 calculate_normal(in vec3 world_point, float sd, in vec2 fragCoord) { const vec3 small_step = vec3(0.001, 0.0, 0.0); vec3 col; float gradient_x = globalSdf(world_point + small_step.xyy, col, fragCoord) - sd; float gradient_y = globalSdf(world_point + small_step.yxy, col, fragCoord) - sd; float gradient_z = globalSdf(world_point + small_step.yyx, col, fragCoord) - sd; vec3 normal = vec3(gradient_x, gradient_y, gradient_z); return normalize(normal); } vec4 lighting(vec3 cp, vec3 color, vec3 normal, vec3 rdir) { // from https://www.shadertoy.com/view/ts3XDj // geometry vec3 ref = reflect( rdir, normal ); // material vec3 mate = color.rgb; float occ = clamp(length(cp)*0.7, 0.2, 0.5);//min(color.g, 1.0);//clamp(2.0*tmat.z, 0.0, 1.0); float sss = -pow(clamp(1.0 + dot(normal, rdir), 0.0, 1.0), 1.0); // lights vec3 lin = 2.5*occ*vec3(1.0)*(0.6 + 0.4*normal.y); lin += 1.0*sss*vec3(1.0,0.95,0.70)*occ; // surface-light interacion vec3 col = mate.xyz * lin; return vec4(col, 1.0); } #define NUMBER_OF_STEPS 128 #define MINIMUM_HIT_DISTANCE 0.005 #define MAXIMUM_TRACE_DISTANCE 6.0 vec4 raymarch(in vec3 rpos, in vec3 rdir, in vec2 fragCoord) { float t = 0.0; float closest_t = 0.0; float closest_t_r = MAXIMUM_TRACE_DISTANCE; float closest_t_r2 = MAXIMUM_TRACE_DISTANCE; float closest_t_r3 = MAXIMUM_TRACE_DISTANCE; vec4 col = vec4(0,0,0,0); for (int i = 0; i < NUMBER_OF_STEPS; i++) { vec3 cp = rpos + t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); if (abs(sd) < 0.7*MINIMUM_HIT_DISTANCE) { vec3 normal = calculate_normal(cp, sd, fragCoord); col = lighting(cp, color, normal, rdir); break; } closest_t_r3 = closest_t_r2; closest_t_r2 = closest_t_r; if (sd < closest_t_r) { closest_t = t; closest_t_r = sd; } if (t > MAXIMUM_TRACE_DISTANCE) break; t += sd; } if (abs(closest_t_r3) > MINIMUM_HIT_DISTANCE) { return col; } vec3 cp = rpos + closest_t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); vec3 normal = calculate_normal(cp, sd, fragCoord); float a = 1.0-abs(closest_t_r3)/MINIMUM_HIT_DISTANCE; vec4 col2 = lighting(cp, color, normal, rdir); col2.a = a; return mix(col, col2, a); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = (2.0*fragCoord - iResolution.xy) / iResolution.y; float rot = 0.5*sin(iTime*0.5) * PI/3.0; if (iMouse.z > 0.5) { // selection on click uv = fract(fragCoord/iResolution.y*SELECT_GRID)*2.0-1.0; rot = -0.5; } // camera movement vec3 cam_pos = 3.0 * rotateX(vec3(0,1,0), rot); vec3 look_at = vec3(0); vec3 look_up = vec3(0,0,1); // camera matrix vec3 ww = normalize(look_at - cam_pos); vec3 uu = normalize(cross(ww, look_up)); vec3 vv = normalize(cross(uu, ww)); // create perspective view ray vec3 rpos = cam_pos; vec3 rdir = normalize( uv.x*uu + uv.y*vv + 2.0*ww ); vec4 col = raymarch(rpos, rdir, fragCoord); vec3 bg = vec3(0.3) * clamp(1.0-2.6*length(fragCoord/iResolution.xy-0.5)*0.5,0.0,1.0); col = vec4(mix(bg, col.rgb, col.a), 1.0); col = vec4(pow(clamp(col.rgb,0.0,1.0), vec3(0.4545)), 1.0); if (iMouse.z > 0.5) { // selection on click ivec2 select = abs(ivec2(fragCoord/iResolution.y*SELECT_GRID)-ivec2(iMouse.xy/iResolution.y*SELECT_GRID)); if (select.x + select.y == 0) { // draw selection box vec2 absuv = abs(uv); vec2 cmp = min(absuv, vec2(0.9)); float d = length(absuv - cmp); float fac = max(0.05 - abs(d - 0.05), 0.0)/0.025; ivec2 checkers = ivec2(round(uv * 3.5)); fac *= float((checkers.x + checkers.y + 1)&1); col = mix(col, vec4(1.0), fac); } } fragColor = col; }
mit
[ 1315, 1335, 1354, 1354, 1451 ]
[ [ 1186, 1199, 1217, 1217, 1313 ], [ 1315, 1335, 1354, 1354, 1451 ], [ 1453, 1490, 1516, 1516, 1726 ], [ 1728, 1751, 1770, 1770, 1818 ], [ 1820, 1846, 1873, 1873, 1938 ], [ 1940, 1963, 1982, 1982, 2008 ], [ 2010, 2044, 2064, 2064, 2078 ], [ 2079, 2079, 2098, 2098, 2117 ], [ 2119, 2154, 2198, 2198, 2675 ], [ 2677, 2709, 2765, 2765, 2943 ], [ 3314, 3388, 3430, 3430, 3767 ], [ 3769, 3769, 3815, 3815, 4079 ], [ 4110, 4110, 4147, 4147, 4210 ], [ 4236, 4236, 4302, 4302, 5431 ], [ 5433, 5433, 5463, 5463, 5502 ], [ 5503, 5503, 5542, 5542, 5570 ], [ 5598, 5598, 5660, 5660, 6407 ], [ 6409, 6409, 6482, 6482, 6894 ], [ 6896, 6896, 6956, 7022, 7491 ], [ 7591, 7591, 7653, 7653, 8927 ], [ 8929, 8929, 8984, 8984, 10528 ] ]
// double factorial
float dfac(int n) {
float res = 1.0; for (int i = n; i > 1; i-=2) res *= float(i); return res; }
// double factorial float dfac(int n) {
3
3
7l23zK
weasel
2021-06-22T13:35:25
// The MIT License // Copyright © 2021 Henrik Dick // 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. #define PI 3.14159265359 /*** math heavy part for spherical harmonics ***/ #define SQRT2PI 2.506628274631 // factorial float fac(int n) { float res = 1.0; for (int i = n; i > 1; i--) res *= float(i); return res; } // double factorial float dfac(int n) { float res = 1.0; for (int i = n; i > 1; i-=2) res *= float(i); return res; } // fac(l-m)/fac(l+m) but more stable float fac2(int l, int m) { int am = abs(m); if (am > l) return 0.0; float res = 1.0; for (int i = max(l-am+1,2); i <= l+am; i++) res *= float(i); if (m < 0) return res; return 1.0 / res; } // complex exponential vec2 cexp(vec2 c) { return exp(c.x)*vec2(cos(c.y), sin(c.y)); } // complex multiplication vec2 cmul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } // complex conjugation vec2 conj(vec2 c) { return vec2(c.x, -c.y); } // complex/real magnitude squared float sqr(float x) { return x*x; } float sqr(vec2 x) { return dot(x,x); } // associated legendre polynomials float legendre_poly(float x, int l, int m) { if (l < abs(m)) return 0.0; if (l == 0) return 1.0; float mul = m >= 0 ? 1.0 : float((~m&1)*2-1)*fac2(l,m); m = abs(m); // recursive calculation of legendre polynomial float lp1 = 0.0; float lp2 = float((~m&1)*2-1)*dfac(2*m-1)*pow(max(1.0-x*x, 1e-7), float(m)/2.0); for (int i = m+1; i <= l; i++) { float lp = (x*float(2*i-1)*lp2 - float(i+m-1)*lp1)/float(i-m); lp1 = lp2; lp2 = lp; } return lp2 / mul; } // spherical harmonics function vec2 sphere_harm(float theta, float phi, int l, int m) { float abs_value = 1.0/SQRT2PI*sqrt(float(2*l+1)/2.0*fac2(l,m)) *legendre_poly(cos(theta), l, m); return cexp(vec2(0.0,float(m)*phi))*abs_value; } // associated laguerre polynomial L_s^k(x) with k > 0, s >= 0 float laguerre_poly(float x, int s, int k) { if (s <= 0) return 1.0; float lp1 = 1.0; float lp2 = 1.0 - x + float(k); for (int n = 1; n < s; n++) { float lp = ((float(2*n + k + 1) - x) * lp2 - float(n+k)*lp1)/float(n+1); lp1 = lp2; lp2 = lp; } return lp2; } // radius dependent term of the 1/r potential eigenstates in atomic units float radius_term(float r, int n, int l) { float a0 = 1.0; // atomic radius float rr = r / a0; float n2 = 2.0 / float(n) / a0; float n3 = n2 * n2 * n2; float p1 = sqrt(n3 * fac2(n, l) * float(n-l)/float(n)); float p2 = exp(-rr/float(n)); float p3 = pow(n2*r, float(l)); float p4 = laguerre_poly(n2*r, n-l-1, 2*l+1); return p1 * p2 * p3 * p4; } vec2 hydrogen(vec3 pos, int n, int l, int m) { float r = length(pos); float sin_theta = length(pos.xy); float phi = sin_theta > 0.0 ? atan(pos.x, pos.y) : 0.0; float theta = atan(sin_theta, pos.z);//atan(sin_theta, pos.z); return sphere_harm(theta, phi, l, m) * radius_term(r, n, l); } /*** Now the rendering ***/ vec3 rotateX(vec3 pos, float angle) { return vec3(pos.x, cmul(pos.yz, cexp(vec2(0.,-angle)))); } #define SELECT_GRID 7.0 void get_nlm(out int n, out int l, out int m, in vec2 fragCoord) { vec2 mouse = iMouse.xy/iResolution.xy; int t; bool selection = false; if (mouse.x + mouse.y > 0.0) {// && iMouse.z > 0.5) { vec2 coord = iMouse.z > 0.5 ? fragCoord : iMouse.xy; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); //t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID); t = cell.x; n = cell.y + 1; selection = t < n*(n+1)/2 || iMouse.z > 0.5; } if (!selection) { /*vec2 coord = fragCoord; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); cell.x += 0; t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID);*/ t = int(iTime*0.5); if (t == 0) n = 1; else { float x = float(t); // see https://en.wikipedia.org/wiki/Tetrahedral_number n = int(ceil(pow(3.*x+sqrt(9.*x*x-1./27.), 1./3.) + pow(3.*x-sqrt(9.*x*x-1./27.), 1./3.) - 0.995)); } t -= ((n*(n-1)*(2*n-1))/6+(n*(n-1))/2)/2; } l = int(floor(sqrt(0.25 + float(2*t)) - 0.5)); m = t - int(floor(0.5*float(l + l*l))); } float spos(float x, float s) { return 0.5*(x*x/(s+abs(x))+x+s); } float smax(float a, float b, float s) { return a+spos(b-a,s); } #define SURFACE_LEVEL 0.3 float globalSdf(vec3 pos, out vec3 color, in vec2 fragCoord) { int n, m, l; get_nlm(n, l, m, fragCoord); // evaluate spherical harmonics vec2 off = cexp(vec2(0, iTime)); vec2 H = hydrogen(pos*float(n*n+1)*1.5, n, l, m); if (m != 0) H = cmul(H, off); H *= float((l+1)*l+n*n)*sqrt(float(n)); // visual rescaling float crit2 = 0.3*(length(pos)+0.05); color = H.x > 0. ? vec3(1.0,0.6,0.15) : vec3(0.2,0.4,0.5); //color = vec3(max(vec3(0.02),(sin(float(n) + vec3(0., 2.1, 4.2))))); float d = (SURFACE_LEVEL - abs(H.x))*crit2; if (m == 0) return smax(d, 0.707*(pos.x+pos.y), 0.02); return d; float arg = atan(H.x, H.y); color = vec3(max(vec3(0.02),(sin(arg + vec3(0., 2.1, 4.2))))); return (0.20 - length(H))*crit2; } vec3 calculate_normal(in vec3 world_point, float sd, in vec2 fragCoord) { const vec3 small_step = vec3(0.001, 0.0, 0.0); vec3 col; float gradient_x = globalSdf(world_point + small_step.xyy, col, fragCoord) - sd; float gradient_y = globalSdf(world_point + small_step.yxy, col, fragCoord) - sd; float gradient_z = globalSdf(world_point + small_step.yyx, col, fragCoord) - sd; vec3 normal = vec3(gradient_x, gradient_y, gradient_z); return normalize(normal); } vec4 lighting(vec3 cp, vec3 color, vec3 normal, vec3 rdir) { // from https://www.shadertoy.com/view/ts3XDj // geometry vec3 ref = reflect( rdir, normal ); // material vec3 mate = color.rgb; float occ = clamp(length(cp)*0.7, 0.2, 0.5);//min(color.g, 1.0);//clamp(2.0*tmat.z, 0.0, 1.0); float sss = -pow(clamp(1.0 + dot(normal, rdir), 0.0, 1.0), 1.0); // lights vec3 lin = 2.5*occ*vec3(1.0)*(0.6 + 0.4*normal.y); lin += 1.0*sss*vec3(1.0,0.95,0.70)*occ; // surface-light interacion vec3 col = mate.xyz * lin; return vec4(col, 1.0); } #define NUMBER_OF_STEPS 128 #define MINIMUM_HIT_DISTANCE 0.005 #define MAXIMUM_TRACE_DISTANCE 6.0 vec4 raymarch(in vec3 rpos, in vec3 rdir, in vec2 fragCoord) { float t = 0.0; float closest_t = 0.0; float closest_t_r = MAXIMUM_TRACE_DISTANCE; float closest_t_r2 = MAXIMUM_TRACE_DISTANCE; float closest_t_r3 = MAXIMUM_TRACE_DISTANCE; vec4 col = vec4(0,0,0,0); for (int i = 0; i < NUMBER_OF_STEPS; i++) { vec3 cp = rpos + t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); if (abs(sd) < 0.7*MINIMUM_HIT_DISTANCE) { vec3 normal = calculate_normal(cp, sd, fragCoord); col = lighting(cp, color, normal, rdir); break; } closest_t_r3 = closest_t_r2; closest_t_r2 = closest_t_r; if (sd < closest_t_r) { closest_t = t; closest_t_r = sd; } if (t > MAXIMUM_TRACE_DISTANCE) break; t += sd; } if (abs(closest_t_r3) > MINIMUM_HIT_DISTANCE) { return col; } vec3 cp = rpos + closest_t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); vec3 normal = calculate_normal(cp, sd, fragCoord); float a = 1.0-abs(closest_t_r3)/MINIMUM_HIT_DISTANCE; vec4 col2 = lighting(cp, color, normal, rdir); col2.a = a; return mix(col, col2, a); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = (2.0*fragCoord - iResolution.xy) / iResolution.y; float rot = 0.5*sin(iTime*0.5) * PI/3.0; if (iMouse.z > 0.5) { // selection on click uv = fract(fragCoord/iResolution.y*SELECT_GRID)*2.0-1.0; rot = -0.5; } // camera movement vec3 cam_pos = 3.0 * rotateX(vec3(0,1,0), rot); vec3 look_at = vec3(0); vec3 look_up = vec3(0,0,1); // camera matrix vec3 ww = normalize(look_at - cam_pos); vec3 uu = normalize(cross(ww, look_up)); vec3 vv = normalize(cross(uu, ww)); // create perspective view ray vec3 rpos = cam_pos; vec3 rdir = normalize( uv.x*uu + uv.y*vv + 2.0*ww ); vec4 col = raymarch(rpos, rdir, fragCoord); vec3 bg = vec3(0.3) * clamp(1.0-2.6*length(fragCoord/iResolution.xy-0.5)*0.5,0.0,1.0); col = vec4(mix(bg, col.rgb, col.a), 1.0); col = vec4(pow(clamp(col.rgb,0.0,1.0), vec3(0.4545)), 1.0); if (iMouse.z > 0.5) { // selection on click ivec2 select = abs(ivec2(fragCoord/iResolution.y*SELECT_GRID)-ivec2(iMouse.xy/iResolution.y*SELECT_GRID)); if (select.x + select.y == 0) { // draw selection box vec2 absuv = abs(uv); vec2 cmp = min(absuv, vec2(0.9)); float d = length(absuv - cmp); float fac = max(0.05 - abs(d - 0.05), 0.0)/0.025; ivec2 checkers = ivec2(round(uv * 3.5)); fac *= float((checkers.x + checkers.y + 1)&1); col = mix(col, vec4(1.0), fac); } } fragColor = col; }
mit
[ 1453, 1490, 1516, 1516, 1726 ]
[ [ 1186, 1199, 1217, 1217, 1313 ], [ 1315, 1335, 1354, 1354, 1451 ], [ 1453, 1490, 1516, 1516, 1726 ], [ 1728, 1751, 1770, 1770, 1818 ], [ 1820, 1846, 1873, 1873, 1938 ], [ 1940, 1963, 1982, 1982, 2008 ], [ 2010, 2044, 2064, 2064, 2078 ], [ 2079, 2079, 2098, 2098, 2117 ], [ 2119, 2154, 2198, 2198, 2675 ], [ 2677, 2709, 2765, 2765, 2943 ], [ 3314, 3388, 3430, 3430, 3767 ], [ 3769, 3769, 3815, 3815, 4079 ], [ 4110, 4110, 4147, 4147, 4210 ], [ 4236, 4236, 4302, 4302, 5431 ], [ 5433, 5433, 5463, 5463, 5502 ], [ 5503, 5503, 5542, 5542, 5570 ], [ 5598, 5598, 5660, 5660, 6407 ], [ 6409, 6409, 6482, 6482, 6894 ], [ 6896, 6896, 6956, 7022, 7491 ], [ 7591, 7591, 7653, 7653, 8927 ], [ 8929, 8929, 8984, 8984, 10528 ] ]
// fac(l-m)/fac(l+m) but more stable
float fac2(int l, int m) {
int am = abs(m); if (am > l) return 0.0; float res = 1.0; for (int i = max(l-am+1,2); i <= l+am; i++) res *= float(i); if (m < 0) return res; return 1.0 / res; }
// fac(l-m)/fac(l+m) but more stable float fac2(int l, int m) {
3
3
7l23zK
weasel
2021-06-22T13:35:25
// The MIT License // Copyright © 2021 Henrik Dick // 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. #define PI 3.14159265359 /*** math heavy part for spherical harmonics ***/ #define SQRT2PI 2.506628274631 // factorial float fac(int n) { float res = 1.0; for (int i = n; i > 1; i--) res *= float(i); return res; } // double factorial float dfac(int n) { float res = 1.0; for (int i = n; i > 1; i-=2) res *= float(i); return res; } // fac(l-m)/fac(l+m) but more stable float fac2(int l, int m) { int am = abs(m); if (am > l) return 0.0; float res = 1.0; for (int i = max(l-am+1,2); i <= l+am; i++) res *= float(i); if (m < 0) return res; return 1.0 / res; } // complex exponential vec2 cexp(vec2 c) { return exp(c.x)*vec2(cos(c.y), sin(c.y)); } // complex multiplication vec2 cmul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } // complex conjugation vec2 conj(vec2 c) { return vec2(c.x, -c.y); } // complex/real magnitude squared float sqr(float x) { return x*x; } float sqr(vec2 x) { return dot(x,x); } // associated legendre polynomials float legendre_poly(float x, int l, int m) { if (l < abs(m)) return 0.0; if (l == 0) return 1.0; float mul = m >= 0 ? 1.0 : float((~m&1)*2-1)*fac2(l,m); m = abs(m); // recursive calculation of legendre polynomial float lp1 = 0.0; float lp2 = float((~m&1)*2-1)*dfac(2*m-1)*pow(max(1.0-x*x, 1e-7), float(m)/2.0); for (int i = m+1; i <= l; i++) { float lp = (x*float(2*i-1)*lp2 - float(i+m-1)*lp1)/float(i-m); lp1 = lp2; lp2 = lp; } return lp2 / mul; } // spherical harmonics function vec2 sphere_harm(float theta, float phi, int l, int m) { float abs_value = 1.0/SQRT2PI*sqrt(float(2*l+1)/2.0*fac2(l,m)) *legendre_poly(cos(theta), l, m); return cexp(vec2(0.0,float(m)*phi))*abs_value; } // associated laguerre polynomial L_s^k(x) with k > 0, s >= 0 float laguerre_poly(float x, int s, int k) { if (s <= 0) return 1.0; float lp1 = 1.0; float lp2 = 1.0 - x + float(k); for (int n = 1; n < s; n++) { float lp = ((float(2*n + k + 1) - x) * lp2 - float(n+k)*lp1)/float(n+1); lp1 = lp2; lp2 = lp; } return lp2; } // radius dependent term of the 1/r potential eigenstates in atomic units float radius_term(float r, int n, int l) { float a0 = 1.0; // atomic radius float rr = r / a0; float n2 = 2.0 / float(n) / a0; float n3 = n2 * n2 * n2; float p1 = sqrt(n3 * fac2(n, l) * float(n-l)/float(n)); float p2 = exp(-rr/float(n)); float p3 = pow(n2*r, float(l)); float p4 = laguerre_poly(n2*r, n-l-1, 2*l+1); return p1 * p2 * p3 * p4; } vec2 hydrogen(vec3 pos, int n, int l, int m) { float r = length(pos); float sin_theta = length(pos.xy); float phi = sin_theta > 0.0 ? atan(pos.x, pos.y) : 0.0; float theta = atan(sin_theta, pos.z);//atan(sin_theta, pos.z); return sphere_harm(theta, phi, l, m) * radius_term(r, n, l); } /*** Now the rendering ***/ vec3 rotateX(vec3 pos, float angle) { return vec3(pos.x, cmul(pos.yz, cexp(vec2(0.,-angle)))); } #define SELECT_GRID 7.0 void get_nlm(out int n, out int l, out int m, in vec2 fragCoord) { vec2 mouse = iMouse.xy/iResolution.xy; int t; bool selection = false; if (mouse.x + mouse.y > 0.0) {// && iMouse.z > 0.5) { vec2 coord = iMouse.z > 0.5 ? fragCoord : iMouse.xy; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); //t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID); t = cell.x; n = cell.y + 1; selection = t < n*(n+1)/2 || iMouse.z > 0.5; } if (!selection) { /*vec2 coord = fragCoord; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); cell.x += 0; t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID);*/ t = int(iTime*0.5); if (t == 0) n = 1; else { float x = float(t); // see https://en.wikipedia.org/wiki/Tetrahedral_number n = int(ceil(pow(3.*x+sqrt(9.*x*x-1./27.), 1./3.) + pow(3.*x-sqrt(9.*x*x-1./27.), 1./3.) - 0.995)); } t -= ((n*(n-1)*(2*n-1))/6+(n*(n-1))/2)/2; } l = int(floor(sqrt(0.25 + float(2*t)) - 0.5)); m = t - int(floor(0.5*float(l + l*l))); } float spos(float x, float s) { return 0.5*(x*x/(s+abs(x))+x+s); } float smax(float a, float b, float s) { return a+spos(b-a,s); } #define SURFACE_LEVEL 0.3 float globalSdf(vec3 pos, out vec3 color, in vec2 fragCoord) { int n, m, l; get_nlm(n, l, m, fragCoord); // evaluate spherical harmonics vec2 off = cexp(vec2(0, iTime)); vec2 H = hydrogen(pos*float(n*n+1)*1.5, n, l, m); if (m != 0) H = cmul(H, off); H *= float((l+1)*l+n*n)*sqrt(float(n)); // visual rescaling float crit2 = 0.3*(length(pos)+0.05); color = H.x > 0. ? vec3(1.0,0.6,0.15) : vec3(0.2,0.4,0.5); //color = vec3(max(vec3(0.02),(sin(float(n) + vec3(0., 2.1, 4.2))))); float d = (SURFACE_LEVEL - abs(H.x))*crit2; if (m == 0) return smax(d, 0.707*(pos.x+pos.y), 0.02); return d; float arg = atan(H.x, H.y); color = vec3(max(vec3(0.02),(sin(arg + vec3(0., 2.1, 4.2))))); return (0.20 - length(H))*crit2; } vec3 calculate_normal(in vec3 world_point, float sd, in vec2 fragCoord) { const vec3 small_step = vec3(0.001, 0.0, 0.0); vec3 col; float gradient_x = globalSdf(world_point + small_step.xyy, col, fragCoord) - sd; float gradient_y = globalSdf(world_point + small_step.yxy, col, fragCoord) - sd; float gradient_z = globalSdf(world_point + small_step.yyx, col, fragCoord) - sd; vec3 normal = vec3(gradient_x, gradient_y, gradient_z); return normalize(normal); } vec4 lighting(vec3 cp, vec3 color, vec3 normal, vec3 rdir) { // from https://www.shadertoy.com/view/ts3XDj // geometry vec3 ref = reflect( rdir, normal ); // material vec3 mate = color.rgb; float occ = clamp(length(cp)*0.7, 0.2, 0.5);//min(color.g, 1.0);//clamp(2.0*tmat.z, 0.0, 1.0); float sss = -pow(clamp(1.0 + dot(normal, rdir), 0.0, 1.0), 1.0); // lights vec3 lin = 2.5*occ*vec3(1.0)*(0.6 + 0.4*normal.y); lin += 1.0*sss*vec3(1.0,0.95,0.70)*occ; // surface-light interacion vec3 col = mate.xyz * lin; return vec4(col, 1.0); } #define NUMBER_OF_STEPS 128 #define MINIMUM_HIT_DISTANCE 0.005 #define MAXIMUM_TRACE_DISTANCE 6.0 vec4 raymarch(in vec3 rpos, in vec3 rdir, in vec2 fragCoord) { float t = 0.0; float closest_t = 0.0; float closest_t_r = MAXIMUM_TRACE_DISTANCE; float closest_t_r2 = MAXIMUM_TRACE_DISTANCE; float closest_t_r3 = MAXIMUM_TRACE_DISTANCE; vec4 col = vec4(0,0,0,0); for (int i = 0; i < NUMBER_OF_STEPS; i++) { vec3 cp = rpos + t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); if (abs(sd) < 0.7*MINIMUM_HIT_DISTANCE) { vec3 normal = calculate_normal(cp, sd, fragCoord); col = lighting(cp, color, normal, rdir); break; } closest_t_r3 = closest_t_r2; closest_t_r2 = closest_t_r; if (sd < closest_t_r) { closest_t = t; closest_t_r = sd; } if (t > MAXIMUM_TRACE_DISTANCE) break; t += sd; } if (abs(closest_t_r3) > MINIMUM_HIT_DISTANCE) { return col; } vec3 cp = rpos + closest_t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); vec3 normal = calculate_normal(cp, sd, fragCoord); float a = 1.0-abs(closest_t_r3)/MINIMUM_HIT_DISTANCE; vec4 col2 = lighting(cp, color, normal, rdir); col2.a = a; return mix(col, col2, a); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = (2.0*fragCoord - iResolution.xy) / iResolution.y; float rot = 0.5*sin(iTime*0.5) * PI/3.0; if (iMouse.z > 0.5) { // selection on click uv = fract(fragCoord/iResolution.y*SELECT_GRID)*2.0-1.0; rot = -0.5; } // camera movement vec3 cam_pos = 3.0 * rotateX(vec3(0,1,0), rot); vec3 look_at = vec3(0); vec3 look_up = vec3(0,0,1); // camera matrix vec3 ww = normalize(look_at - cam_pos); vec3 uu = normalize(cross(ww, look_up)); vec3 vv = normalize(cross(uu, ww)); // create perspective view ray vec3 rpos = cam_pos; vec3 rdir = normalize( uv.x*uu + uv.y*vv + 2.0*ww ); vec4 col = raymarch(rpos, rdir, fragCoord); vec3 bg = vec3(0.3) * clamp(1.0-2.6*length(fragCoord/iResolution.xy-0.5)*0.5,0.0,1.0); col = vec4(mix(bg, col.rgb, col.a), 1.0); col = vec4(pow(clamp(col.rgb,0.0,1.0), vec3(0.4545)), 1.0); if (iMouse.z > 0.5) { // selection on click ivec2 select = abs(ivec2(fragCoord/iResolution.y*SELECT_GRID)-ivec2(iMouse.xy/iResolution.y*SELECT_GRID)); if (select.x + select.y == 0) { // draw selection box vec2 absuv = abs(uv); vec2 cmp = min(absuv, vec2(0.9)); float d = length(absuv - cmp); float fac = max(0.05 - abs(d - 0.05), 0.0)/0.025; ivec2 checkers = ivec2(round(uv * 3.5)); fac *= float((checkers.x + checkers.y + 1)&1); col = mix(col, vec4(1.0), fac); } } fragColor = col; }
mit
[ 1728, 1751, 1770, 1770, 1818 ]
[ [ 1186, 1199, 1217, 1217, 1313 ], [ 1315, 1335, 1354, 1354, 1451 ], [ 1453, 1490, 1516, 1516, 1726 ], [ 1728, 1751, 1770, 1770, 1818 ], [ 1820, 1846, 1873, 1873, 1938 ], [ 1940, 1963, 1982, 1982, 2008 ], [ 2010, 2044, 2064, 2064, 2078 ], [ 2079, 2079, 2098, 2098, 2117 ], [ 2119, 2154, 2198, 2198, 2675 ], [ 2677, 2709, 2765, 2765, 2943 ], [ 3314, 3388, 3430, 3430, 3767 ], [ 3769, 3769, 3815, 3815, 4079 ], [ 4110, 4110, 4147, 4147, 4210 ], [ 4236, 4236, 4302, 4302, 5431 ], [ 5433, 5433, 5463, 5463, 5502 ], [ 5503, 5503, 5542, 5542, 5570 ], [ 5598, 5598, 5660, 5660, 6407 ], [ 6409, 6409, 6482, 6482, 6894 ], [ 6896, 6896, 6956, 7022, 7491 ], [ 7591, 7591, 7653, 7653, 8927 ], [ 8929, 8929, 8984, 8984, 10528 ] ]
// complex exponential
vec2 cexp(vec2 c) {
return exp(c.x)*vec2(cos(c.y), sin(c.y)); }
// complex exponential vec2 cexp(vec2 c) {
3
3
7l23zK
weasel
2021-06-22T13:35:25
// The MIT License // Copyright © 2021 Henrik Dick // 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. #define PI 3.14159265359 /*** math heavy part for spherical harmonics ***/ #define SQRT2PI 2.506628274631 // factorial float fac(int n) { float res = 1.0; for (int i = n; i > 1; i--) res *= float(i); return res; } // double factorial float dfac(int n) { float res = 1.0; for (int i = n; i > 1; i-=2) res *= float(i); return res; } // fac(l-m)/fac(l+m) but more stable float fac2(int l, int m) { int am = abs(m); if (am > l) return 0.0; float res = 1.0; for (int i = max(l-am+1,2); i <= l+am; i++) res *= float(i); if (m < 0) return res; return 1.0 / res; } // complex exponential vec2 cexp(vec2 c) { return exp(c.x)*vec2(cos(c.y), sin(c.y)); } // complex multiplication vec2 cmul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } // complex conjugation vec2 conj(vec2 c) { return vec2(c.x, -c.y); } // complex/real magnitude squared float sqr(float x) { return x*x; } float sqr(vec2 x) { return dot(x,x); } // associated legendre polynomials float legendre_poly(float x, int l, int m) { if (l < abs(m)) return 0.0; if (l == 0) return 1.0; float mul = m >= 0 ? 1.0 : float((~m&1)*2-1)*fac2(l,m); m = abs(m); // recursive calculation of legendre polynomial float lp1 = 0.0; float lp2 = float((~m&1)*2-1)*dfac(2*m-1)*pow(max(1.0-x*x, 1e-7), float(m)/2.0); for (int i = m+1; i <= l; i++) { float lp = (x*float(2*i-1)*lp2 - float(i+m-1)*lp1)/float(i-m); lp1 = lp2; lp2 = lp; } return lp2 / mul; } // spherical harmonics function vec2 sphere_harm(float theta, float phi, int l, int m) { float abs_value = 1.0/SQRT2PI*sqrt(float(2*l+1)/2.0*fac2(l,m)) *legendre_poly(cos(theta), l, m); return cexp(vec2(0.0,float(m)*phi))*abs_value; } // associated laguerre polynomial L_s^k(x) with k > 0, s >= 0 float laguerre_poly(float x, int s, int k) { if (s <= 0) return 1.0; float lp1 = 1.0; float lp2 = 1.0 - x + float(k); for (int n = 1; n < s; n++) { float lp = ((float(2*n + k + 1) - x) * lp2 - float(n+k)*lp1)/float(n+1); lp1 = lp2; lp2 = lp; } return lp2; } // radius dependent term of the 1/r potential eigenstates in atomic units float radius_term(float r, int n, int l) { float a0 = 1.0; // atomic radius float rr = r / a0; float n2 = 2.0 / float(n) / a0; float n3 = n2 * n2 * n2; float p1 = sqrt(n3 * fac2(n, l) * float(n-l)/float(n)); float p2 = exp(-rr/float(n)); float p3 = pow(n2*r, float(l)); float p4 = laguerre_poly(n2*r, n-l-1, 2*l+1); return p1 * p2 * p3 * p4; } vec2 hydrogen(vec3 pos, int n, int l, int m) { float r = length(pos); float sin_theta = length(pos.xy); float phi = sin_theta > 0.0 ? atan(pos.x, pos.y) : 0.0; float theta = atan(sin_theta, pos.z);//atan(sin_theta, pos.z); return sphere_harm(theta, phi, l, m) * radius_term(r, n, l); } /*** Now the rendering ***/ vec3 rotateX(vec3 pos, float angle) { return vec3(pos.x, cmul(pos.yz, cexp(vec2(0.,-angle)))); } #define SELECT_GRID 7.0 void get_nlm(out int n, out int l, out int m, in vec2 fragCoord) { vec2 mouse = iMouse.xy/iResolution.xy; int t; bool selection = false; if (mouse.x + mouse.y > 0.0) {// && iMouse.z > 0.5) { vec2 coord = iMouse.z > 0.5 ? fragCoord : iMouse.xy; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); //t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID); t = cell.x; n = cell.y + 1; selection = t < n*(n+1)/2 || iMouse.z > 0.5; } if (!selection) { /*vec2 coord = fragCoord; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); cell.x += 0; t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID);*/ t = int(iTime*0.5); if (t == 0) n = 1; else { float x = float(t); // see https://en.wikipedia.org/wiki/Tetrahedral_number n = int(ceil(pow(3.*x+sqrt(9.*x*x-1./27.), 1./3.) + pow(3.*x-sqrt(9.*x*x-1./27.), 1./3.) - 0.995)); } t -= ((n*(n-1)*(2*n-1))/6+(n*(n-1))/2)/2; } l = int(floor(sqrt(0.25 + float(2*t)) - 0.5)); m = t - int(floor(0.5*float(l + l*l))); } float spos(float x, float s) { return 0.5*(x*x/(s+abs(x))+x+s); } float smax(float a, float b, float s) { return a+spos(b-a,s); } #define SURFACE_LEVEL 0.3 float globalSdf(vec3 pos, out vec3 color, in vec2 fragCoord) { int n, m, l; get_nlm(n, l, m, fragCoord); // evaluate spherical harmonics vec2 off = cexp(vec2(0, iTime)); vec2 H = hydrogen(pos*float(n*n+1)*1.5, n, l, m); if (m != 0) H = cmul(H, off); H *= float((l+1)*l+n*n)*sqrt(float(n)); // visual rescaling float crit2 = 0.3*(length(pos)+0.05); color = H.x > 0. ? vec3(1.0,0.6,0.15) : vec3(0.2,0.4,0.5); //color = vec3(max(vec3(0.02),(sin(float(n) + vec3(0., 2.1, 4.2))))); float d = (SURFACE_LEVEL - abs(H.x))*crit2; if (m == 0) return smax(d, 0.707*(pos.x+pos.y), 0.02); return d; float arg = atan(H.x, H.y); color = vec3(max(vec3(0.02),(sin(arg + vec3(0., 2.1, 4.2))))); return (0.20 - length(H))*crit2; } vec3 calculate_normal(in vec3 world_point, float sd, in vec2 fragCoord) { const vec3 small_step = vec3(0.001, 0.0, 0.0); vec3 col; float gradient_x = globalSdf(world_point + small_step.xyy, col, fragCoord) - sd; float gradient_y = globalSdf(world_point + small_step.yxy, col, fragCoord) - sd; float gradient_z = globalSdf(world_point + small_step.yyx, col, fragCoord) - sd; vec3 normal = vec3(gradient_x, gradient_y, gradient_z); return normalize(normal); } vec4 lighting(vec3 cp, vec3 color, vec3 normal, vec3 rdir) { // from https://www.shadertoy.com/view/ts3XDj // geometry vec3 ref = reflect( rdir, normal ); // material vec3 mate = color.rgb; float occ = clamp(length(cp)*0.7, 0.2, 0.5);//min(color.g, 1.0);//clamp(2.0*tmat.z, 0.0, 1.0); float sss = -pow(clamp(1.0 + dot(normal, rdir), 0.0, 1.0), 1.0); // lights vec3 lin = 2.5*occ*vec3(1.0)*(0.6 + 0.4*normal.y); lin += 1.0*sss*vec3(1.0,0.95,0.70)*occ; // surface-light interacion vec3 col = mate.xyz * lin; return vec4(col, 1.0); } #define NUMBER_OF_STEPS 128 #define MINIMUM_HIT_DISTANCE 0.005 #define MAXIMUM_TRACE_DISTANCE 6.0 vec4 raymarch(in vec3 rpos, in vec3 rdir, in vec2 fragCoord) { float t = 0.0; float closest_t = 0.0; float closest_t_r = MAXIMUM_TRACE_DISTANCE; float closest_t_r2 = MAXIMUM_TRACE_DISTANCE; float closest_t_r3 = MAXIMUM_TRACE_DISTANCE; vec4 col = vec4(0,0,0,0); for (int i = 0; i < NUMBER_OF_STEPS; i++) { vec3 cp = rpos + t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); if (abs(sd) < 0.7*MINIMUM_HIT_DISTANCE) { vec3 normal = calculate_normal(cp, sd, fragCoord); col = lighting(cp, color, normal, rdir); break; } closest_t_r3 = closest_t_r2; closest_t_r2 = closest_t_r; if (sd < closest_t_r) { closest_t = t; closest_t_r = sd; } if (t > MAXIMUM_TRACE_DISTANCE) break; t += sd; } if (abs(closest_t_r3) > MINIMUM_HIT_DISTANCE) { return col; } vec3 cp = rpos + closest_t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); vec3 normal = calculate_normal(cp, sd, fragCoord); float a = 1.0-abs(closest_t_r3)/MINIMUM_HIT_DISTANCE; vec4 col2 = lighting(cp, color, normal, rdir); col2.a = a; return mix(col, col2, a); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = (2.0*fragCoord - iResolution.xy) / iResolution.y; float rot = 0.5*sin(iTime*0.5) * PI/3.0; if (iMouse.z > 0.5) { // selection on click uv = fract(fragCoord/iResolution.y*SELECT_GRID)*2.0-1.0; rot = -0.5; } // camera movement vec3 cam_pos = 3.0 * rotateX(vec3(0,1,0), rot); vec3 look_at = vec3(0); vec3 look_up = vec3(0,0,1); // camera matrix vec3 ww = normalize(look_at - cam_pos); vec3 uu = normalize(cross(ww, look_up)); vec3 vv = normalize(cross(uu, ww)); // create perspective view ray vec3 rpos = cam_pos; vec3 rdir = normalize( uv.x*uu + uv.y*vv + 2.0*ww ); vec4 col = raymarch(rpos, rdir, fragCoord); vec3 bg = vec3(0.3) * clamp(1.0-2.6*length(fragCoord/iResolution.xy-0.5)*0.5,0.0,1.0); col = vec4(mix(bg, col.rgb, col.a), 1.0); col = vec4(pow(clamp(col.rgb,0.0,1.0), vec3(0.4545)), 1.0); if (iMouse.z > 0.5) { // selection on click ivec2 select = abs(ivec2(fragCoord/iResolution.y*SELECT_GRID)-ivec2(iMouse.xy/iResolution.y*SELECT_GRID)); if (select.x + select.y == 0) { // draw selection box vec2 absuv = abs(uv); vec2 cmp = min(absuv, vec2(0.9)); float d = length(absuv - cmp); float fac = max(0.05 - abs(d - 0.05), 0.0)/0.025; ivec2 checkers = ivec2(round(uv * 3.5)); fac *= float((checkers.x + checkers.y + 1)&1); col = mix(col, vec4(1.0), fac); } } fragColor = col; }
mit
[ 1820, 1846, 1873, 1873, 1938 ]
[ [ 1186, 1199, 1217, 1217, 1313 ], [ 1315, 1335, 1354, 1354, 1451 ], [ 1453, 1490, 1516, 1516, 1726 ], [ 1728, 1751, 1770, 1770, 1818 ], [ 1820, 1846, 1873, 1873, 1938 ], [ 1940, 1963, 1982, 1982, 2008 ], [ 2010, 2044, 2064, 2064, 2078 ], [ 2079, 2079, 2098, 2098, 2117 ], [ 2119, 2154, 2198, 2198, 2675 ], [ 2677, 2709, 2765, 2765, 2943 ], [ 3314, 3388, 3430, 3430, 3767 ], [ 3769, 3769, 3815, 3815, 4079 ], [ 4110, 4110, 4147, 4147, 4210 ], [ 4236, 4236, 4302, 4302, 5431 ], [ 5433, 5433, 5463, 5463, 5502 ], [ 5503, 5503, 5542, 5542, 5570 ], [ 5598, 5598, 5660, 5660, 6407 ], [ 6409, 6409, 6482, 6482, 6894 ], [ 6896, 6896, 6956, 7022, 7491 ], [ 7591, 7591, 7653, 7653, 8927 ], [ 8929, 8929, 8984, 8984, 10528 ] ]
// complex multiplication
vec2 cmul(vec2 a, vec2 b) {
return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
// complex multiplication vec2 cmul(vec2 a, vec2 b) {
5
15
7l23zK
weasel
2021-06-22T13:35:25
// The MIT License // Copyright © 2021 Henrik Dick // 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. #define PI 3.14159265359 /*** math heavy part for spherical harmonics ***/ #define SQRT2PI 2.506628274631 // factorial float fac(int n) { float res = 1.0; for (int i = n; i > 1; i--) res *= float(i); return res; } // double factorial float dfac(int n) { float res = 1.0; for (int i = n; i > 1; i-=2) res *= float(i); return res; } // fac(l-m)/fac(l+m) but more stable float fac2(int l, int m) { int am = abs(m); if (am > l) return 0.0; float res = 1.0; for (int i = max(l-am+1,2); i <= l+am; i++) res *= float(i); if (m < 0) return res; return 1.0 / res; } // complex exponential vec2 cexp(vec2 c) { return exp(c.x)*vec2(cos(c.y), sin(c.y)); } // complex multiplication vec2 cmul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } // complex conjugation vec2 conj(vec2 c) { return vec2(c.x, -c.y); } // complex/real magnitude squared float sqr(float x) { return x*x; } float sqr(vec2 x) { return dot(x,x); } // associated legendre polynomials float legendre_poly(float x, int l, int m) { if (l < abs(m)) return 0.0; if (l == 0) return 1.0; float mul = m >= 0 ? 1.0 : float((~m&1)*2-1)*fac2(l,m); m = abs(m); // recursive calculation of legendre polynomial float lp1 = 0.0; float lp2 = float((~m&1)*2-1)*dfac(2*m-1)*pow(max(1.0-x*x, 1e-7), float(m)/2.0); for (int i = m+1; i <= l; i++) { float lp = (x*float(2*i-1)*lp2 - float(i+m-1)*lp1)/float(i-m); lp1 = lp2; lp2 = lp; } return lp2 / mul; } // spherical harmonics function vec2 sphere_harm(float theta, float phi, int l, int m) { float abs_value = 1.0/SQRT2PI*sqrt(float(2*l+1)/2.0*fac2(l,m)) *legendre_poly(cos(theta), l, m); return cexp(vec2(0.0,float(m)*phi))*abs_value; } // associated laguerre polynomial L_s^k(x) with k > 0, s >= 0 float laguerre_poly(float x, int s, int k) { if (s <= 0) return 1.0; float lp1 = 1.0; float lp2 = 1.0 - x + float(k); for (int n = 1; n < s; n++) { float lp = ((float(2*n + k + 1) - x) * lp2 - float(n+k)*lp1)/float(n+1); lp1 = lp2; lp2 = lp; } return lp2; } // radius dependent term of the 1/r potential eigenstates in atomic units float radius_term(float r, int n, int l) { float a0 = 1.0; // atomic radius float rr = r / a0; float n2 = 2.0 / float(n) / a0; float n3 = n2 * n2 * n2; float p1 = sqrt(n3 * fac2(n, l) * float(n-l)/float(n)); float p2 = exp(-rr/float(n)); float p3 = pow(n2*r, float(l)); float p4 = laguerre_poly(n2*r, n-l-1, 2*l+1); return p1 * p2 * p3 * p4; } vec2 hydrogen(vec3 pos, int n, int l, int m) { float r = length(pos); float sin_theta = length(pos.xy); float phi = sin_theta > 0.0 ? atan(pos.x, pos.y) : 0.0; float theta = atan(sin_theta, pos.z);//atan(sin_theta, pos.z); return sphere_harm(theta, phi, l, m) * radius_term(r, n, l); } /*** Now the rendering ***/ vec3 rotateX(vec3 pos, float angle) { return vec3(pos.x, cmul(pos.yz, cexp(vec2(0.,-angle)))); } #define SELECT_GRID 7.0 void get_nlm(out int n, out int l, out int m, in vec2 fragCoord) { vec2 mouse = iMouse.xy/iResolution.xy; int t; bool selection = false; if (mouse.x + mouse.y > 0.0) {// && iMouse.z > 0.5) { vec2 coord = iMouse.z > 0.5 ? fragCoord : iMouse.xy; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); //t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID); t = cell.x; n = cell.y + 1; selection = t < n*(n+1)/2 || iMouse.z > 0.5; } if (!selection) { /*vec2 coord = fragCoord; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); cell.x += 0; t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID);*/ t = int(iTime*0.5); if (t == 0) n = 1; else { float x = float(t); // see https://en.wikipedia.org/wiki/Tetrahedral_number n = int(ceil(pow(3.*x+sqrt(9.*x*x-1./27.), 1./3.) + pow(3.*x-sqrt(9.*x*x-1./27.), 1./3.) - 0.995)); } t -= ((n*(n-1)*(2*n-1))/6+(n*(n-1))/2)/2; } l = int(floor(sqrt(0.25 + float(2*t)) - 0.5)); m = t - int(floor(0.5*float(l + l*l))); } float spos(float x, float s) { return 0.5*(x*x/(s+abs(x))+x+s); } float smax(float a, float b, float s) { return a+spos(b-a,s); } #define SURFACE_LEVEL 0.3 float globalSdf(vec3 pos, out vec3 color, in vec2 fragCoord) { int n, m, l; get_nlm(n, l, m, fragCoord); // evaluate spherical harmonics vec2 off = cexp(vec2(0, iTime)); vec2 H = hydrogen(pos*float(n*n+1)*1.5, n, l, m); if (m != 0) H = cmul(H, off); H *= float((l+1)*l+n*n)*sqrt(float(n)); // visual rescaling float crit2 = 0.3*(length(pos)+0.05); color = H.x > 0. ? vec3(1.0,0.6,0.15) : vec3(0.2,0.4,0.5); //color = vec3(max(vec3(0.02),(sin(float(n) + vec3(0., 2.1, 4.2))))); float d = (SURFACE_LEVEL - abs(H.x))*crit2; if (m == 0) return smax(d, 0.707*(pos.x+pos.y), 0.02); return d; float arg = atan(H.x, H.y); color = vec3(max(vec3(0.02),(sin(arg + vec3(0., 2.1, 4.2))))); return (0.20 - length(H))*crit2; } vec3 calculate_normal(in vec3 world_point, float sd, in vec2 fragCoord) { const vec3 small_step = vec3(0.001, 0.0, 0.0); vec3 col; float gradient_x = globalSdf(world_point + small_step.xyy, col, fragCoord) - sd; float gradient_y = globalSdf(world_point + small_step.yxy, col, fragCoord) - sd; float gradient_z = globalSdf(world_point + small_step.yyx, col, fragCoord) - sd; vec3 normal = vec3(gradient_x, gradient_y, gradient_z); return normalize(normal); } vec4 lighting(vec3 cp, vec3 color, vec3 normal, vec3 rdir) { // from https://www.shadertoy.com/view/ts3XDj // geometry vec3 ref = reflect( rdir, normal ); // material vec3 mate = color.rgb; float occ = clamp(length(cp)*0.7, 0.2, 0.5);//min(color.g, 1.0);//clamp(2.0*tmat.z, 0.0, 1.0); float sss = -pow(clamp(1.0 + dot(normal, rdir), 0.0, 1.0), 1.0); // lights vec3 lin = 2.5*occ*vec3(1.0)*(0.6 + 0.4*normal.y); lin += 1.0*sss*vec3(1.0,0.95,0.70)*occ; // surface-light interacion vec3 col = mate.xyz * lin; return vec4(col, 1.0); } #define NUMBER_OF_STEPS 128 #define MINIMUM_HIT_DISTANCE 0.005 #define MAXIMUM_TRACE_DISTANCE 6.0 vec4 raymarch(in vec3 rpos, in vec3 rdir, in vec2 fragCoord) { float t = 0.0; float closest_t = 0.0; float closest_t_r = MAXIMUM_TRACE_DISTANCE; float closest_t_r2 = MAXIMUM_TRACE_DISTANCE; float closest_t_r3 = MAXIMUM_TRACE_DISTANCE; vec4 col = vec4(0,0,0,0); for (int i = 0; i < NUMBER_OF_STEPS; i++) { vec3 cp = rpos + t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); if (abs(sd) < 0.7*MINIMUM_HIT_DISTANCE) { vec3 normal = calculate_normal(cp, sd, fragCoord); col = lighting(cp, color, normal, rdir); break; } closest_t_r3 = closest_t_r2; closest_t_r2 = closest_t_r; if (sd < closest_t_r) { closest_t = t; closest_t_r = sd; } if (t > MAXIMUM_TRACE_DISTANCE) break; t += sd; } if (abs(closest_t_r3) > MINIMUM_HIT_DISTANCE) { return col; } vec3 cp = rpos + closest_t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); vec3 normal = calculate_normal(cp, sd, fragCoord); float a = 1.0-abs(closest_t_r3)/MINIMUM_HIT_DISTANCE; vec4 col2 = lighting(cp, color, normal, rdir); col2.a = a; return mix(col, col2, a); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = (2.0*fragCoord - iResolution.xy) / iResolution.y; float rot = 0.5*sin(iTime*0.5) * PI/3.0; if (iMouse.z > 0.5) { // selection on click uv = fract(fragCoord/iResolution.y*SELECT_GRID)*2.0-1.0; rot = -0.5; } // camera movement vec3 cam_pos = 3.0 * rotateX(vec3(0,1,0), rot); vec3 look_at = vec3(0); vec3 look_up = vec3(0,0,1); // camera matrix vec3 ww = normalize(look_at - cam_pos); vec3 uu = normalize(cross(ww, look_up)); vec3 vv = normalize(cross(uu, ww)); // create perspective view ray vec3 rpos = cam_pos; vec3 rdir = normalize( uv.x*uu + uv.y*vv + 2.0*ww ); vec4 col = raymarch(rpos, rdir, fragCoord); vec3 bg = vec3(0.3) * clamp(1.0-2.6*length(fragCoord/iResolution.xy-0.5)*0.5,0.0,1.0); col = vec4(mix(bg, col.rgb, col.a), 1.0); col = vec4(pow(clamp(col.rgb,0.0,1.0), vec3(0.4545)), 1.0); if (iMouse.z > 0.5) { // selection on click ivec2 select = abs(ivec2(fragCoord/iResolution.y*SELECT_GRID)-ivec2(iMouse.xy/iResolution.y*SELECT_GRID)); if (select.x + select.y == 0) { // draw selection box vec2 absuv = abs(uv); vec2 cmp = min(absuv, vec2(0.9)); float d = length(absuv - cmp); float fac = max(0.05 - abs(d - 0.05), 0.0)/0.025; ivec2 checkers = ivec2(round(uv * 3.5)); fac *= float((checkers.x + checkers.y + 1)&1); col = mix(col, vec4(1.0), fac); } } fragColor = col; }
mit
[ 2119, 2154, 2198, 2198, 2675 ]
[ [ 1186, 1199, 1217, 1217, 1313 ], [ 1315, 1335, 1354, 1354, 1451 ], [ 1453, 1490, 1516, 1516, 1726 ], [ 1728, 1751, 1770, 1770, 1818 ], [ 1820, 1846, 1873, 1873, 1938 ], [ 1940, 1963, 1982, 1982, 2008 ], [ 2010, 2044, 2064, 2064, 2078 ], [ 2079, 2079, 2098, 2098, 2117 ], [ 2119, 2154, 2198, 2198, 2675 ], [ 2677, 2709, 2765, 2765, 2943 ], [ 3314, 3388, 3430, 3430, 3767 ], [ 3769, 3769, 3815, 3815, 4079 ], [ 4110, 4110, 4147, 4147, 4210 ], [ 4236, 4236, 4302, 4302, 5431 ], [ 5433, 5433, 5463, 5463, 5502 ], [ 5503, 5503, 5542, 5542, 5570 ], [ 5598, 5598, 5660, 5660, 6407 ], [ 6409, 6409, 6482, 6482, 6894 ], [ 6896, 6896, 6956, 7022, 7491 ], [ 7591, 7591, 7653, 7653, 8927 ], [ 8929, 8929, 8984, 8984, 10528 ] ]
// associated legendre polynomials
float legendre_poly(float x, int l, int m) {
if (l < abs(m)) return 0.0; if (l == 0) return 1.0; float mul = m >= 0 ? 1.0 : float((~m&1)*2-1)*fac2(l,m); m = abs(m); // recursive calculation of legendre polynomial float lp1 = 0.0; float lp2 = float((~m&1)*2-1)*dfac(2*m-1)*pow(max(1.0-x*x, 1e-7), float(m)/2.0); for (int i = m+1; i <= l; i++) { float lp = (x*float(2*i-1)*lp2 - float(i+m-1)*lp1)/float(i-m); lp1 = lp2; lp2 = lp; } return lp2 / mul; }
// associated legendre polynomials float legendre_poly(float x, int l, int m) {
1
3
7l23zK
weasel
2021-06-22T13:35:25
// The MIT License // Copyright © 2021 Henrik Dick // 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. #define PI 3.14159265359 /*** math heavy part for spherical harmonics ***/ #define SQRT2PI 2.506628274631 // factorial float fac(int n) { float res = 1.0; for (int i = n; i > 1; i--) res *= float(i); return res; } // double factorial float dfac(int n) { float res = 1.0; for (int i = n; i > 1; i-=2) res *= float(i); return res; } // fac(l-m)/fac(l+m) but more stable float fac2(int l, int m) { int am = abs(m); if (am > l) return 0.0; float res = 1.0; for (int i = max(l-am+1,2); i <= l+am; i++) res *= float(i); if (m < 0) return res; return 1.0 / res; } // complex exponential vec2 cexp(vec2 c) { return exp(c.x)*vec2(cos(c.y), sin(c.y)); } // complex multiplication vec2 cmul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } // complex conjugation vec2 conj(vec2 c) { return vec2(c.x, -c.y); } // complex/real magnitude squared float sqr(float x) { return x*x; } float sqr(vec2 x) { return dot(x,x); } // associated legendre polynomials float legendre_poly(float x, int l, int m) { if (l < abs(m)) return 0.0; if (l == 0) return 1.0; float mul = m >= 0 ? 1.0 : float((~m&1)*2-1)*fac2(l,m); m = abs(m); // recursive calculation of legendre polynomial float lp1 = 0.0; float lp2 = float((~m&1)*2-1)*dfac(2*m-1)*pow(max(1.0-x*x, 1e-7), float(m)/2.0); for (int i = m+1; i <= l; i++) { float lp = (x*float(2*i-1)*lp2 - float(i+m-1)*lp1)/float(i-m); lp1 = lp2; lp2 = lp; } return lp2 / mul; } // spherical harmonics function vec2 sphere_harm(float theta, float phi, int l, int m) { float abs_value = 1.0/SQRT2PI*sqrt(float(2*l+1)/2.0*fac2(l,m)) *legendre_poly(cos(theta), l, m); return cexp(vec2(0.0,float(m)*phi))*abs_value; } // associated laguerre polynomial L_s^k(x) with k > 0, s >= 0 float laguerre_poly(float x, int s, int k) { if (s <= 0) return 1.0; float lp1 = 1.0; float lp2 = 1.0 - x + float(k); for (int n = 1; n < s; n++) { float lp = ((float(2*n + k + 1) - x) * lp2 - float(n+k)*lp1)/float(n+1); lp1 = lp2; lp2 = lp; } return lp2; } // radius dependent term of the 1/r potential eigenstates in atomic units float radius_term(float r, int n, int l) { float a0 = 1.0; // atomic radius float rr = r / a0; float n2 = 2.0 / float(n) / a0; float n3 = n2 * n2 * n2; float p1 = sqrt(n3 * fac2(n, l) * float(n-l)/float(n)); float p2 = exp(-rr/float(n)); float p3 = pow(n2*r, float(l)); float p4 = laguerre_poly(n2*r, n-l-1, 2*l+1); return p1 * p2 * p3 * p4; } vec2 hydrogen(vec3 pos, int n, int l, int m) { float r = length(pos); float sin_theta = length(pos.xy); float phi = sin_theta > 0.0 ? atan(pos.x, pos.y) : 0.0; float theta = atan(sin_theta, pos.z);//atan(sin_theta, pos.z); return sphere_harm(theta, phi, l, m) * radius_term(r, n, l); } /*** Now the rendering ***/ vec3 rotateX(vec3 pos, float angle) { return vec3(pos.x, cmul(pos.yz, cexp(vec2(0.,-angle)))); } #define SELECT_GRID 7.0 void get_nlm(out int n, out int l, out int m, in vec2 fragCoord) { vec2 mouse = iMouse.xy/iResolution.xy; int t; bool selection = false; if (mouse.x + mouse.y > 0.0) {// && iMouse.z > 0.5) { vec2 coord = iMouse.z > 0.5 ? fragCoord : iMouse.xy; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); //t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID); t = cell.x; n = cell.y + 1; selection = t < n*(n+1)/2 || iMouse.z > 0.5; } if (!selection) { /*vec2 coord = fragCoord; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); cell.x += 0; t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID);*/ t = int(iTime*0.5); if (t == 0) n = 1; else { float x = float(t); // see https://en.wikipedia.org/wiki/Tetrahedral_number n = int(ceil(pow(3.*x+sqrt(9.*x*x-1./27.), 1./3.) + pow(3.*x-sqrt(9.*x*x-1./27.), 1./3.) - 0.995)); } t -= ((n*(n-1)*(2*n-1))/6+(n*(n-1))/2)/2; } l = int(floor(sqrt(0.25 + float(2*t)) - 0.5)); m = t - int(floor(0.5*float(l + l*l))); } float spos(float x, float s) { return 0.5*(x*x/(s+abs(x))+x+s); } float smax(float a, float b, float s) { return a+spos(b-a,s); } #define SURFACE_LEVEL 0.3 float globalSdf(vec3 pos, out vec3 color, in vec2 fragCoord) { int n, m, l; get_nlm(n, l, m, fragCoord); // evaluate spherical harmonics vec2 off = cexp(vec2(0, iTime)); vec2 H = hydrogen(pos*float(n*n+1)*1.5, n, l, m); if (m != 0) H = cmul(H, off); H *= float((l+1)*l+n*n)*sqrt(float(n)); // visual rescaling float crit2 = 0.3*(length(pos)+0.05); color = H.x > 0. ? vec3(1.0,0.6,0.15) : vec3(0.2,0.4,0.5); //color = vec3(max(vec3(0.02),(sin(float(n) + vec3(0., 2.1, 4.2))))); float d = (SURFACE_LEVEL - abs(H.x))*crit2; if (m == 0) return smax(d, 0.707*(pos.x+pos.y), 0.02); return d; float arg = atan(H.x, H.y); color = vec3(max(vec3(0.02),(sin(arg + vec3(0., 2.1, 4.2))))); return (0.20 - length(H))*crit2; } vec3 calculate_normal(in vec3 world_point, float sd, in vec2 fragCoord) { const vec3 small_step = vec3(0.001, 0.0, 0.0); vec3 col; float gradient_x = globalSdf(world_point + small_step.xyy, col, fragCoord) - sd; float gradient_y = globalSdf(world_point + small_step.yxy, col, fragCoord) - sd; float gradient_z = globalSdf(world_point + small_step.yyx, col, fragCoord) - sd; vec3 normal = vec3(gradient_x, gradient_y, gradient_z); return normalize(normal); } vec4 lighting(vec3 cp, vec3 color, vec3 normal, vec3 rdir) { // from https://www.shadertoy.com/view/ts3XDj // geometry vec3 ref = reflect( rdir, normal ); // material vec3 mate = color.rgb; float occ = clamp(length(cp)*0.7, 0.2, 0.5);//min(color.g, 1.0);//clamp(2.0*tmat.z, 0.0, 1.0); float sss = -pow(clamp(1.0 + dot(normal, rdir), 0.0, 1.0), 1.0); // lights vec3 lin = 2.5*occ*vec3(1.0)*(0.6 + 0.4*normal.y); lin += 1.0*sss*vec3(1.0,0.95,0.70)*occ; // surface-light interacion vec3 col = mate.xyz * lin; return vec4(col, 1.0); } #define NUMBER_OF_STEPS 128 #define MINIMUM_HIT_DISTANCE 0.005 #define MAXIMUM_TRACE_DISTANCE 6.0 vec4 raymarch(in vec3 rpos, in vec3 rdir, in vec2 fragCoord) { float t = 0.0; float closest_t = 0.0; float closest_t_r = MAXIMUM_TRACE_DISTANCE; float closest_t_r2 = MAXIMUM_TRACE_DISTANCE; float closest_t_r3 = MAXIMUM_TRACE_DISTANCE; vec4 col = vec4(0,0,0,0); for (int i = 0; i < NUMBER_OF_STEPS; i++) { vec3 cp = rpos + t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); if (abs(sd) < 0.7*MINIMUM_HIT_DISTANCE) { vec3 normal = calculate_normal(cp, sd, fragCoord); col = lighting(cp, color, normal, rdir); break; } closest_t_r3 = closest_t_r2; closest_t_r2 = closest_t_r; if (sd < closest_t_r) { closest_t = t; closest_t_r = sd; } if (t > MAXIMUM_TRACE_DISTANCE) break; t += sd; } if (abs(closest_t_r3) > MINIMUM_HIT_DISTANCE) { return col; } vec3 cp = rpos + closest_t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); vec3 normal = calculate_normal(cp, sd, fragCoord); float a = 1.0-abs(closest_t_r3)/MINIMUM_HIT_DISTANCE; vec4 col2 = lighting(cp, color, normal, rdir); col2.a = a; return mix(col, col2, a); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = (2.0*fragCoord - iResolution.xy) / iResolution.y; float rot = 0.5*sin(iTime*0.5) * PI/3.0; if (iMouse.z > 0.5) { // selection on click uv = fract(fragCoord/iResolution.y*SELECT_GRID)*2.0-1.0; rot = -0.5; } // camera movement vec3 cam_pos = 3.0 * rotateX(vec3(0,1,0), rot); vec3 look_at = vec3(0); vec3 look_up = vec3(0,0,1); // camera matrix vec3 ww = normalize(look_at - cam_pos); vec3 uu = normalize(cross(ww, look_up)); vec3 vv = normalize(cross(uu, ww)); // create perspective view ray vec3 rpos = cam_pos; vec3 rdir = normalize( uv.x*uu + uv.y*vv + 2.0*ww ); vec4 col = raymarch(rpos, rdir, fragCoord); vec3 bg = vec3(0.3) * clamp(1.0-2.6*length(fragCoord/iResolution.xy-0.5)*0.5,0.0,1.0); col = vec4(mix(bg, col.rgb, col.a), 1.0); col = vec4(pow(clamp(col.rgb,0.0,1.0), vec3(0.4545)), 1.0); if (iMouse.z > 0.5) { // selection on click ivec2 select = abs(ivec2(fragCoord/iResolution.y*SELECT_GRID)-ivec2(iMouse.xy/iResolution.y*SELECT_GRID)); if (select.x + select.y == 0) { // draw selection box vec2 absuv = abs(uv); vec2 cmp = min(absuv, vec2(0.9)); float d = length(absuv - cmp); float fac = max(0.05 - abs(d - 0.05), 0.0)/0.025; ivec2 checkers = ivec2(round(uv * 3.5)); fac *= float((checkers.x + checkers.y + 1)&1); col = mix(col, vec4(1.0), fac); } } fragColor = col; }
mit
[ 2677, 2709, 2765, 2765, 2943 ]
[ [ 1186, 1199, 1217, 1217, 1313 ], [ 1315, 1335, 1354, 1354, 1451 ], [ 1453, 1490, 1516, 1516, 1726 ], [ 1728, 1751, 1770, 1770, 1818 ], [ 1820, 1846, 1873, 1873, 1938 ], [ 1940, 1963, 1982, 1982, 2008 ], [ 2010, 2044, 2064, 2064, 2078 ], [ 2079, 2079, 2098, 2098, 2117 ], [ 2119, 2154, 2198, 2198, 2675 ], [ 2677, 2709, 2765, 2765, 2943 ], [ 3314, 3388, 3430, 3430, 3767 ], [ 3769, 3769, 3815, 3815, 4079 ], [ 4110, 4110, 4147, 4147, 4210 ], [ 4236, 4236, 4302, 4302, 5431 ], [ 5433, 5433, 5463, 5463, 5502 ], [ 5503, 5503, 5542, 5542, 5570 ], [ 5598, 5598, 5660, 5660, 6407 ], [ 6409, 6409, 6482, 6482, 6894 ], [ 6896, 6896, 6956, 7022, 7491 ], [ 7591, 7591, 7653, 7653, 8927 ], [ 8929, 8929, 8984, 8984, 10528 ] ]
// spherical harmonics function
vec2 sphere_harm(float theta, float phi, int l, int m) {
float abs_value = 1.0/SQRT2PI*sqrt(float(2*l+1)/2.0*fac2(l,m)) *legendre_poly(cos(theta), l, m); return cexp(vec2(0.0,float(m)*phi))*abs_value; }
// spherical harmonics function vec2 sphere_harm(float theta, float phi, int l, int m) {
3
3
7l23zK
weasel
2021-06-22T13:35:25
// The MIT License // Copyright © 2021 Henrik Dick // 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. #define PI 3.14159265359 /*** math heavy part for spherical harmonics ***/ #define SQRT2PI 2.506628274631 // factorial float fac(int n) { float res = 1.0; for (int i = n; i > 1; i--) res *= float(i); return res; } // double factorial float dfac(int n) { float res = 1.0; for (int i = n; i > 1; i-=2) res *= float(i); return res; } // fac(l-m)/fac(l+m) but more stable float fac2(int l, int m) { int am = abs(m); if (am > l) return 0.0; float res = 1.0; for (int i = max(l-am+1,2); i <= l+am; i++) res *= float(i); if (m < 0) return res; return 1.0 / res; } // complex exponential vec2 cexp(vec2 c) { return exp(c.x)*vec2(cos(c.y), sin(c.y)); } // complex multiplication vec2 cmul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } // complex conjugation vec2 conj(vec2 c) { return vec2(c.x, -c.y); } // complex/real magnitude squared float sqr(float x) { return x*x; } float sqr(vec2 x) { return dot(x,x); } // associated legendre polynomials float legendre_poly(float x, int l, int m) { if (l < abs(m)) return 0.0; if (l == 0) return 1.0; float mul = m >= 0 ? 1.0 : float((~m&1)*2-1)*fac2(l,m); m = abs(m); // recursive calculation of legendre polynomial float lp1 = 0.0; float lp2 = float((~m&1)*2-1)*dfac(2*m-1)*pow(max(1.0-x*x, 1e-7), float(m)/2.0); for (int i = m+1; i <= l; i++) { float lp = (x*float(2*i-1)*lp2 - float(i+m-1)*lp1)/float(i-m); lp1 = lp2; lp2 = lp; } return lp2 / mul; } // spherical harmonics function vec2 sphere_harm(float theta, float phi, int l, int m) { float abs_value = 1.0/SQRT2PI*sqrt(float(2*l+1)/2.0*fac2(l,m)) *legendre_poly(cos(theta), l, m); return cexp(vec2(0.0,float(m)*phi))*abs_value; } // associated laguerre polynomial L_s^k(x) with k > 0, s >= 0 float laguerre_poly(float x, int s, int k) { if (s <= 0) return 1.0; float lp1 = 1.0; float lp2 = 1.0 - x + float(k); for (int n = 1; n < s; n++) { float lp = ((float(2*n + k + 1) - x) * lp2 - float(n+k)*lp1)/float(n+1); lp1 = lp2; lp2 = lp; } return lp2; } // radius dependent term of the 1/r potential eigenstates in atomic units float radius_term(float r, int n, int l) { float a0 = 1.0; // atomic radius float rr = r / a0; float n2 = 2.0 / float(n) / a0; float n3 = n2 * n2 * n2; float p1 = sqrt(n3 * fac2(n, l) * float(n-l)/float(n)); float p2 = exp(-rr/float(n)); float p3 = pow(n2*r, float(l)); float p4 = laguerre_poly(n2*r, n-l-1, 2*l+1); return p1 * p2 * p3 * p4; } vec2 hydrogen(vec3 pos, int n, int l, int m) { float r = length(pos); float sin_theta = length(pos.xy); float phi = sin_theta > 0.0 ? atan(pos.x, pos.y) : 0.0; float theta = atan(sin_theta, pos.z);//atan(sin_theta, pos.z); return sphere_harm(theta, phi, l, m) * radius_term(r, n, l); } /*** Now the rendering ***/ vec3 rotateX(vec3 pos, float angle) { return vec3(pos.x, cmul(pos.yz, cexp(vec2(0.,-angle)))); } #define SELECT_GRID 7.0 void get_nlm(out int n, out int l, out int m, in vec2 fragCoord) { vec2 mouse = iMouse.xy/iResolution.xy; int t; bool selection = false; if (mouse.x + mouse.y > 0.0) {// && iMouse.z > 0.5) { vec2 coord = iMouse.z > 0.5 ? fragCoord : iMouse.xy; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); //t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID); t = cell.x; n = cell.y + 1; selection = t < n*(n+1)/2 || iMouse.z > 0.5; } if (!selection) { /*vec2 coord = fragCoord; ivec2 cell = ivec2(coord/iResolution.y*SELECT_GRID); cell.x += 0; t = (int(SELECT_GRID) - cell.y - 1) + cell.x * int(SELECT_GRID);*/ t = int(iTime*0.5); if (t == 0) n = 1; else { float x = float(t); // see https://en.wikipedia.org/wiki/Tetrahedral_number n = int(ceil(pow(3.*x+sqrt(9.*x*x-1./27.), 1./3.) + pow(3.*x-sqrt(9.*x*x-1./27.), 1./3.) - 0.995)); } t -= ((n*(n-1)*(2*n-1))/6+(n*(n-1))/2)/2; } l = int(floor(sqrt(0.25 + float(2*t)) - 0.5)); m = t - int(floor(0.5*float(l + l*l))); } float spos(float x, float s) { return 0.5*(x*x/(s+abs(x))+x+s); } float smax(float a, float b, float s) { return a+spos(b-a,s); } #define SURFACE_LEVEL 0.3 float globalSdf(vec3 pos, out vec3 color, in vec2 fragCoord) { int n, m, l; get_nlm(n, l, m, fragCoord); // evaluate spherical harmonics vec2 off = cexp(vec2(0, iTime)); vec2 H = hydrogen(pos*float(n*n+1)*1.5, n, l, m); if (m != 0) H = cmul(H, off); H *= float((l+1)*l+n*n)*sqrt(float(n)); // visual rescaling float crit2 = 0.3*(length(pos)+0.05); color = H.x > 0. ? vec3(1.0,0.6,0.15) : vec3(0.2,0.4,0.5); //color = vec3(max(vec3(0.02),(sin(float(n) + vec3(0., 2.1, 4.2))))); float d = (SURFACE_LEVEL - abs(H.x))*crit2; if (m == 0) return smax(d, 0.707*(pos.x+pos.y), 0.02); return d; float arg = atan(H.x, H.y); color = vec3(max(vec3(0.02),(sin(arg + vec3(0., 2.1, 4.2))))); return (0.20 - length(H))*crit2; } vec3 calculate_normal(in vec3 world_point, float sd, in vec2 fragCoord) { const vec3 small_step = vec3(0.001, 0.0, 0.0); vec3 col; float gradient_x = globalSdf(world_point + small_step.xyy, col, fragCoord) - sd; float gradient_y = globalSdf(world_point + small_step.yxy, col, fragCoord) - sd; float gradient_z = globalSdf(world_point + small_step.yyx, col, fragCoord) - sd; vec3 normal = vec3(gradient_x, gradient_y, gradient_z); return normalize(normal); } vec4 lighting(vec3 cp, vec3 color, vec3 normal, vec3 rdir) { // from https://www.shadertoy.com/view/ts3XDj // geometry vec3 ref = reflect( rdir, normal ); // material vec3 mate = color.rgb; float occ = clamp(length(cp)*0.7, 0.2, 0.5);//min(color.g, 1.0);//clamp(2.0*tmat.z, 0.0, 1.0); float sss = -pow(clamp(1.0 + dot(normal, rdir), 0.0, 1.0), 1.0); // lights vec3 lin = 2.5*occ*vec3(1.0)*(0.6 + 0.4*normal.y); lin += 1.0*sss*vec3(1.0,0.95,0.70)*occ; // surface-light interacion vec3 col = mate.xyz * lin; return vec4(col, 1.0); } #define NUMBER_OF_STEPS 128 #define MINIMUM_HIT_DISTANCE 0.005 #define MAXIMUM_TRACE_DISTANCE 6.0 vec4 raymarch(in vec3 rpos, in vec3 rdir, in vec2 fragCoord) { float t = 0.0; float closest_t = 0.0; float closest_t_r = MAXIMUM_TRACE_DISTANCE; float closest_t_r2 = MAXIMUM_TRACE_DISTANCE; float closest_t_r3 = MAXIMUM_TRACE_DISTANCE; vec4 col = vec4(0,0,0,0); for (int i = 0; i < NUMBER_OF_STEPS; i++) { vec3 cp = rpos + t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); if (abs(sd) < 0.7*MINIMUM_HIT_DISTANCE) { vec3 normal = calculate_normal(cp, sd, fragCoord); col = lighting(cp, color, normal, rdir); break; } closest_t_r3 = closest_t_r2; closest_t_r2 = closest_t_r; if (sd < closest_t_r) { closest_t = t; closest_t_r = sd; } if (t > MAXIMUM_TRACE_DISTANCE) break; t += sd; } if (abs(closest_t_r3) > MINIMUM_HIT_DISTANCE) { return col; } vec3 cp = rpos + closest_t * rdir; vec3 color = vec3(0.0); float sd = globalSdf(cp, color, fragCoord); vec3 normal = calculate_normal(cp, sd, fragCoord); float a = 1.0-abs(closest_t_r3)/MINIMUM_HIT_DISTANCE; vec4 col2 = lighting(cp, color, normal, rdir); col2.a = a; return mix(col, col2, a); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = (2.0*fragCoord - iResolution.xy) / iResolution.y; float rot = 0.5*sin(iTime*0.5) * PI/3.0; if (iMouse.z > 0.5) { // selection on click uv = fract(fragCoord/iResolution.y*SELECT_GRID)*2.0-1.0; rot = -0.5; } // camera movement vec3 cam_pos = 3.0 * rotateX(vec3(0,1,0), rot); vec3 look_at = vec3(0); vec3 look_up = vec3(0,0,1); // camera matrix vec3 ww = normalize(look_at - cam_pos); vec3 uu = normalize(cross(ww, look_up)); vec3 vv = normalize(cross(uu, ww)); // create perspective view ray vec3 rpos = cam_pos; vec3 rdir = normalize( uv.x*uu + uv.y*vv + 2.0*ww ); vec4 col = raymarch(rpos, rdir, fragCoord); vec3 bg = vec3(0.3) * clamp(1.0-2.6*length(fragCoord/iResolution.xy-0.5)*0.5,0.0,1.0); col = vec4(mix(bg, col.rgb, col.a), 1.0); col = vec4(pow(clamp(col.rgb,0.0,1.0), vec3(0.4545)), 1.0); if (iMouse.z > 0.5) { // selection on click ivec2 select = abs(ivec2(fragCoord/iResolution.y*SELECT_GRID)-ivec2(iMouse.xy/iResolution.y*SELECT_GRID)); if (select.x + select.y == 0) { // draw selection box vec2 absuv = abs(uv); vec2 cmp = min(absuv, vec2(0.9)); float d = length(absuv - cmp); float fac = max(0.05 - abs(d - 0.05), 0.0)/0.025; ivec2 checkers = ivec2(round(uv * 3.5)); fac *= float((checkers.x + checkers.y + 1)&1); col = mix(col, vec4(1.0), fac); } } fragColor = col; }
mit
[ 3314, 3388, 3430, 3430, 3767 ]
[ [ 1186, 1199, 1217, 1217, 1313 ], [ 1315, 1335, 1354, 1354, 1451 ], [ 1453, 1490, 1516, 1516, 1726 ], [ 1728, 1751, 1770, 1770, 1818 ], [ 1820, 1846, 1873, 1873, 1938 ], [ 1940, 1963, 1982, 1982, 2008 ], [ 2010, 2044, 2064, 2064, 2078 ], [ 2079, 2079, 2098, 2098, 2117 ], [ 2119, 2154, 2198, 2198, 2675 ], [ 2677, 2709, 2765, 2765, 2943 ], [ 3314, 3388, 3430, 3430, 3767 ], [ 3769, 3769, 3815, 3815, 4079 ], [ 4110, 4110, 4147, 4147, 4210 ], [ 4236, 4236, 4302, 4302, 5431 ], [ 5433, 5433, 5463, 5463, 5502 ], [ 5503, 5503, 5542, 5542, 5570 ], [ 5598, 5598, 5660, 5660, 6407 ], [ 6409, 6409, 6482, 6482, 6894 ], [ 6896, 6896, 6956, 7022, 7491 ], [ 7591, 7591, 7653, 7653, 8927 ], [ 8929, 8929, 8984, 8984, 10528 ] ]
// radius dependent term of the 1/r potential eigenstates in atomic units
float radius_term(float r, int n, int l) {
float a0 = 1.0; // atomic radius float rr = r / a0; float n2 = 2.0 / float(n) / a0; float n3 = n2 * n2 * n2; float p1 = sqrt(n3 * fac2(n, l) * float(n-l)/float(n)); float p2 = exp(-rr/float(n)); float p3 = pow(n2*r, float(l)); float p4 = laguerre_poly(n2*r, n-l-1, 2*l+1); return p1 * p2 * p3 * p4; }
// radius dependent term of the 1/r potential eigenstates in atomic units float radius_term(float r, int n, int l) {
1
1
7tj3DG
mrange
2021-06-25T18:20:12
// License CC0: Random friday fractal // Result after a bit of random coding on friday afternoon #define PI 3.141592654 #define TAU (2.0*PI) #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define TOLERANCE 0.00001 #define MAX_RAY_LENGTH 10.0 #define MAX_RAY_MARCHES 50 #define NORM_OFF 0.0001 const vec3 std_gamma = vec3(2.2); float tanh_approx(float x) { // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } // From: https://stackoverflow.com/a/17897228/418488 vec3 hsv2rgb(vec3 c) { const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } vec3 postProcess(vec3 col, vec2 q) { col = clamp(col, 0.0, 1.0); col = pow(col, 1.0/std_gamma); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); col *=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } float box(vec3 p, vec3 b) { vec3 q = abs(p) - b; return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0); } float pmin(float a, float b, float k) { float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 ); return mix( b, a, h ) - k*h*(1.0-h); } float pmax(float a, float b, float k) { return -pmin(-a, -b, k); } vec3 pmin(vec3 a, vec3 b, float k) { vec3 h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 ); return mix( b, a, h ) - k*h*(1.0-h); } vec3 pabs(vec3 a, float k) { return -pmin(a, -a, k); } float df(vec3 p) { float d = 1E6; const float zf = 2.; const vec3 nz = normalize(vec3(1.0, .0, -1.0)); const vec3 ny = normalize(vec3(1.0, -1., 0.0)); float z = 1.0; const float rsm = 0.125*0.25; const float a = 128.25; const mat2 rxy = ROT(a); const mat2 ryz = ROT(a*sqrt(0.5)); for (int i = 0; i < 7; ++i) { vec3 pp0 = p; vec3 pp1 = p; float dd0 = box(pp0, vec3(0.25))-0.01; float dd1 = length(pp1)-0.3; float dd = dd0; dd = pmax(dd, -dd1, 0.05); dd = min(dd, length(pp0)- 0.25); dd /= z; z *= zf; p *= zf; p.xy *= rxy; p.yz *= ryz; p = pabs(p, rsm); p -= nz*pmin(0.0, dot(p, nz), rsm)*2.0; p -= ny*pmin(0.0, dot(p, ny), rsm)*2.0; p -= vec3(0.85/zf, 0.0, 0.0); d = pmax(d, -(dd-0.1/z), 0.05/z); if(i > 2) d = min(d, dd); } return d; } float rayMarch(vec3 ro, vec3 rd, out int iter) { float t = 0.0; int i = 0; for (i = 0; i < MAX_RAY_MARCHES; i++) { float d = df(ro + rd*t); if (d < TOLERANCE || t > MAX_RAY_LENGTH) break; t += d; } iter = i; return t; } vec3 normal(vec3 pos) { vec2 eps = vec2(NORM_OFF,0.0); vec3 nor; nor.x = df(pos+eps.xyy) - df(pos-eps.xyy); nor.y = df(pos+eps.yxy) - df(pos-eps.yxy); nor.z = df(pos+eps.yyx) - df(pos-eps.yyx); return normalize(nor); } float softShadow(vec3 pos, vec3 ld, float ll, float mint, float k) { const float minShadow = 0.25; float res = 1.0; float t = mint; for (int i=0; i<24; i++) { float d = df(pos + ld*t); res = min(res, k*d/t); if (ll <= t) break; if(res <= minShadow) break; t += max(mint*0.2, d); } return clamp(res,minShadow,1.0); } vec3 render(vec3 ro, vec3 rd) { vec3 lightPos = vec3(1.0); float alpha = 0.05*TIME; const vec3 skyCol = vec3(0.0); int iter = 0; float t = rayMarch(ro, rd, iter); if (t >= MAX_RAY_LENGTH) { return vec3(0.0); } vec3 pos = ro + t*rd; vec3 nor = normal(pos); vec3 refl = reflect(rd, nor); float ii = float(iter)/float(MAX_RAY_MARCHES); float ifade= 1.0-tanh_approx(1.25*ii); vec3 hsv = vec3(1.25-t*0.5, mix(0.25, 1.0, ii), 1.0); vec3 color = hsv2rgb(hsv); vec3 lv = lightPos - pos; float ll2 = dot(lv, lv); float ll = sqrt(ll2); vec3 ld = lv / ll; float sha = softShadow(pos, ld, ll*0.95, 0.01, 10.0); float dm = 5.0/ll2; float dif = max(dot(nor,ld),0.0)*(dm+0.05); float spe = pow(max(dot(refl, ld), 0.), 20.); float ao = smoothstep(0.5, 0.1 , ii); float l = mix(0.2, 1.0, dif*sha*ao); vec3 col = l*color + 2.0*spe*ao*sha; return col*ifade; // return vec3(ao); } vec3 effect3d(vec2 p, vec2 q) { float z = TIME; vec3 cam = vec3(1.0, 0.5, 0.0); float rt = TAU*TIME/20.0;; cam.xy *= ROT(sin(rt*sqrt(0.5))*0.5+0.0); cam.xz *= ROT(sin(rt)*1.0-0.75); vec3 la = vec3(0.0); vec3 dcam = normalize(la - cam); vec3 ddcam= vec3(0.0); vec3 ro = cam; vec3 ww = normalize(dcam); vec3 uu = normalize(cross(vec3(0.0,1.0,0.0)+ddcam*2.0, ww )); vec3 vv = normalize(cross(ww,uu)); vec3 rd = normalize( p.x*uu + p.y*vv + 2.5*ww ); return render(ro, rd); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord/RESOLUTION.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; vec3 col = effect3d(p, q); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 584, 637, 659, 659, 828 ]
[ [ 458, 458, 486, 506, 582 ], [ 584, 637, 659, 659, 828 ], [ 830, 830, 866, 866, 1099 ], [ 1101, 1101, 1128, 1128, 1215 ], [ 1217, 1217, 1256, 1256, 1345 ], [ 1347, 1347, 1386, 1386, 1415 ], [ 1417, 1417, 1453, 1453, 1541 ], [ 1543, 1543, 1571, 1571, 1599 ], [ 1602, 1602, 1620, 1620, 2461 ], [ 2463, 2463, 2511, 2511, 2706 ], [ 2708, 2708, 2731, 2731, 2939 ], [ 2941, 2941, 3009, 3009, 3288 ], [ 3290, 3290, 3321, 3321, 4250 ], [ 4252, 4252, 4283, 4283, 4765 ], [ 4767, 4767, 4822, 4822, 5012 ] ]
// From: https://stackoverflow.com/a/17897228/418488
vec3 hsv2rgb(vec3 c) {
const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); }
// From: https://stackoverflow.com/a/17897228/418488 vec3 hsv2rgb(vec3 c) {
22
237
sdBSWc
iq
2021-06-21T05:20:04
// The MIT License // Copyright © 2021 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. // The left butterfly implements a naive and wrong way to // transition a stationary object into constant motion. The // butterfly to the right implements the integral of the // smoothstep() function in order to smoohtly transition // between the two states. // // More information here: // // https://iquilezles.org/www/articles/smoothstepintegral/smoothstepintegral.htm // Incorrect EaseInOut/Smoothstep velocity float position_bad( float t, in float T ) { return smoothstep(0.0,T,t)*t; //return (t<T) ? (t*t*t)/(T*T*T)*(3.0*T-2.0*t) : t; } // Correct integral of EaseInOut/Smoothstep float position_good( float t, in float T ) { if( t>=T ) return t - 0.5*T; float f = t/T; return f*f*f*(T-t*0.5); } // ======================================= vec3 trackMin( in vec3 v, in float d ) { if( d<v.x ) v=vec3(d,v.x,v.y); else if( d<v.y ) v=vec3(v.x,d,v.y); else if( d<v.z ) v=vec3(v.x,v.y,d); return v; } vec4 butterfly( in vec2 p ) { p.x = abs(p.x); p.y *= 0.9; vec4 col = vec4(0.0); float a = atan(p.x,p.y); float r = length(p); if( p.y<0.0 ) { float f = 0.6 + 0.01*sin( 24.0*a ); float w = 1.1*a-0.8; f *= sin(w)*sin(w); float th = f + 0.001; float th2 = th; vec3 wcol = mix( vec3(210,119,40)/255.0, vec3(232,79,12)/255.0, smoothstep( 0.0, 0.7, r ) ); wcol *= 1.5; wcol *= 1.0+0.1*sin(17.0*p.x+vec3(0,0,4))*sin(23.0*p.y+vec3(0,0,4)); vec2 q = p; q.xy += 0.02*sin(q.yx*12.0); q.y = min(q.y,0.0); vec3 v = vec3(10); v = trackMin(v,length(q-vec2(0.29,-0.20))); v = trackMin(v,length(q-vec2(0.10,-0.30))); v = trackMin(v,length(q-vec2(0.20,-0.26))); v = trackMin(v,length(q-vec2(0.28,-0.29))); v = trackMin(v,length(q-vec2(0.34,-0.27))); v = trackMin(v,length(q-vec2(0.38,-0.24))); v = trackMin(v,length(q-vec2(0.39,-0.20))); v = trackMin(v,length(q-vec2(0.38,-0.15))); v = trackMin(v,length(q-vec2(0.35,-0.08))); v.yz -= v.x; float g = 1.25*v.y*v.z/max(v.y+v.z,0.001); wcol *= smoothstep(0.0,0.01,g); th -= 0.05*(1.0-smoothstep(0.0,0.05,g))-0.02; wcol *= smoothstep(0.02,0.03,(th-r)*th); q = vec2( mod(a,0.1)-0.05, (r-th+0.025)*3.1415*0.5 ); float d = length( q )-0.015; wcol = mix( wcol, vec3(1,1,1), 1.0-smoothstep( 0.0, 0.005,d) ); wcol *= smoothstep(0.01,0.03,length(p-vec2(0.235,-0.2))); d = r-(th+th2)*0.5; col = vec4(wcol,smoothstep( 0.0,2.0*fwidth(d),-d) ); } if( a<2.2 ) { float f = 0.65 + 0.015*sin( 24.0*a ); float w = a*(3.1416/2.356); float th = f*sin(w)*sin(w) + 0.001; float th2 = th; th += 0.25*exp2( -50.0*(w-1.4)*(w-1.4) ); vec3 wcol = mix( vec3(0.7,0.5,0.2), vec3(0.8,0.2,0.0), smoothstep( 0.0, 1.0, r ) ); wcol *= 1.4; wcol *= 1.0+0.1*sin(13.0*p.x+vec3(0,0,4))*sin(19.0*p.y+vec3(0,0,4)); vec3 v = vec3(10); v = trackMin(v,length(p-vec2(0.25,0.2))); v = trackMin(v,length(p-vec2(0.35,0.0))); v = trackMin(v,length(p-vec2(0.4,0.1))); v = trackMin(v,length(p-vec2(0.45,0.2))); v = trackMin(v,length(p-vec2(0.45,0.3))); v.yz -= v.x; float g = 2.0*v.y*v.z/max(v.y+v.z,0.001); wcol *= smoothstep(0.0,0.02,g); th2 -= 0.05*(1.0-smoothstep(0.0,0.05,g)); float isblack = smoothstep(0.02,0.03,(th2-r)*th2); vec2 q = vec2( mod(a,0.1)-0.05, (r-th+0.025)*3.1415*0.5 ); float d = length( q )-0.015; float ww = 1.0-smoothstep( 0.0, 0.01,d); if( r>th2 ) { vec2 q = fract(p*18.0)-0.5; vec2 iq = floor(p*18.0); float id= iq.x*111.0+iq.y*13.0; q += 0.25*sin(id*vec2(15,17)+vec2(0,2)); float r = 1.0+0.75*sin(id*431.0); ww = max( ww, 1.0-smoothstep(0.0,0.01,length(q)-0.2*r)); } wcol = mix( wcol, vec3(ww), 1.0-isblack ); d = r-th; float al = smoothstep( 0.0,2.0*fwidth(d),-d); col.xyz = mix( col.xyz, wcol, al ); col.w = 1.0 - (1.0-col.w)*(1.0-al); } return col; } int hash( ivec2 z ) { int n = z.x+z.y*11111; n = (n<<13)^n; return (n*(n*n*15731+789221)+1376312589)>>16; } #if HW_PERFORMANCE==0 const int AA = 2; #else const int AA = 4; #endif void mainImage( out vec4 fragColor, in vec2 fragCoord ) { float stime = mod( iTime, 6.0 ); vec3 col = vec3(0.0); for( int j=0; j<AA; j++ ) for( int i=0; i<AA; i++ ) { vec2 of = vec2(i,j)/float(AA); vec2 p = (2.0*(fragCoord+of)-iResolution.xy)/iResolution.y; p *= 1.6; //p.y += 0.5; float di = float( hash(ivec2(fragCoord)*AA+ivec2(i,j))&255 )/255.0; float time = stime + ((float(j*AA+i)+di)/float(AA*AA))*(0.5/30.0) - 0.5*0.5/30.0; time += 0.07*sin( p.y ); float wing = (p.x<0.0)?position_bad(time, 2.0): position_good(time, 2.0); if( p.x>0.0 ) p.x=p.x-1.4; else p.x=p.x+1.4; float an = 1.55*(0.5-0.5*cos(2.0*6.283185*wing)); vec2 pl = vec2(sign(p.x)*sin(an),-cos(an)); vec3 ro = vec3(0.0,0.085,2.1); vec3 rd = vec3(p,-3.0); vec3 pos = ro - rd*dot(ro.xz,pl)/dot(rd.xz,pl); vec2 q = vec2( length(pos.xz), pos.y ); vec4 tmp = butterfly( q ); tmp = clamp(tmp,0.0,1.0); tmp.xyz *= 0.1+0.9*mix(1.0,abs(q.y)*0.5+min(q.x*2.0,1.0),pl.x*pl.x); tmp.xyz *= clamp(0.25+0.75*(pl.x-pl.y+1.0),0.0,1.0); col += mix( vec3(0.5), tmp.xyz, tmp.w ); } col /= float(AA*AA); vec2 q = fragCoord/iResolution.xy; col += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0; fragColor = vec4(col,1.0); }
mit
[ 1455, 1498, 1541, 1541, 1633 ]
[ [ 1455, 1498, 1541, 1541, 1633 ], [ 1635, 1679, 1723, 1723, 1805 ], [ 1852, 1852, 1892, 1892, 2029 ], [ 2031, 2031, 2060, 2060, 5437 ], [ 5439, 5439, 5460, 5460, 5558 ] ]
// Incorrect EaseInOut/Smoothstep velocity
float position_bad( float t, in float T ) {
return smoothstep(0.0,T,t)*t; //return (t<T) ? (t*t*t)/(T*T*T)*(3.0*T-2.0*t) : t; }
// Incorrect EaseInOut/Smoothstep velocity float position_bad( float t, in float T ) {
1
1
sdBSWc
iq
2021-06-21T05:20:04
// The MIT License // Copyright © 2021 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. // The left butterfly implements a naive and wrong way to // transition a stationary object into constant motion. The // butterfly to the right implements the integral of the // smoothstep() function in order to smoohtly transition // between the two states. // // More information here: // // https://iquilezles.org/www/articles/smoothstepintegral/smoothstepintegral.htm // Incorrect EaseInOut/Smoothstep velocity float position_bad( float t, in float T ) { return smoothstep(0.0,T,t)*t; //return (t<T) ? (t*t*t)/(T*T*T)*(3.0*T-2.0*t) : t; } // Correct integral of EaseInOut/Smoothstep float position_good( float t, in float T ) { if( t>=T ) return t - 0.5*T; float f = t/T; return f*f*f*(T-t*0.5); } // ======================================= vec3 trackMin( in vec3 v, in float d ) { if( d<v.x ) v=vec3(d,v.x,v.y); else if( d<v.y ) v=vec3(v.x,d,v.y); else if( d<v.z ) v=vec3(v.x,v.y,d); return v; } vec4 butterfly( in vec2 p ) { p.x = abs(p.x); p.y *= 0.9; vec4 col = vec4(0.0); float a = atan(p.x,p.y); float r = length(p); if( p.y<0.0 ) { float f = 0.6 + 0.01*sin( 24.0*a ); float w = 1.1*a-0.8; f *= sin(w)*sin(w); float th = f + 0.001; float th2 = th; vec3 wcol = mix( vec3(210,119,40)/255.0, vec3(232,79,12)/255.0, smoothstep( 0.0, 0.7, r ) ); wcol *= 1.5; wcol *= 1.0+0.1*sin(17.0*p.x+vec3(0,0,4))*sin(23.0*p.y+vec3(0,0,4)); vec2 q = p; q.xy += 0.02*sin(q.yx*12.0); q.y = min(q.y,0.0); vec3 v = vec3(10); v = trackMin(v,length(q-vec2(0.29,-0.20))); v = trackMin(v,length(q-vec2(0.10,-0.30))); v = trackMin(v,length(q-vec2(0.20,-0.26))); v = trackMin(v,length(q-vec2(0.28,-0.29))); v = trackMin(v,length(q-vec2(0.34,-0.27))); v = trackMin(v,length(q-vec2(0.38,-0.24))); v = trackMin(v,length(q-vec2(0.39,-0.20))); v = trackMin(v,length(q-vec2(0.38,-0.15))); v = trackMin(v,length(q-vec2(0.35,-0.08))); v.yz -= v.x; float g = 1.25*v.y*v.z/max(v.y+v.z,0.001); wcol *= smoothstep(0.0,0.01,g); th -= 0.05*(1.0-smoothstep(0.0,0.05,g))-0.02; wcol *= smoothstep(0.02,0.03,(th-r)*th); q = vec2( mod(a,0.1)-0.05, (r-th+0.025)*3.1415*0.5 ); float d = length( q )-0.015; wcol = mix( wcol, vec3(1,1,1), 1.0-smoothstep( 0.0, 0.005,d) ); wcol *= smoothstep(0.01,0.03,length(p-vec2(0.235,-0.2))); d = r-(th+th2)*0.5; col = vec4(wcol,smoothstep( 0.0,2.0*fwidth(d),-d) ); } if( a<2.2 ) { float f = 0.65 + 0.015*sin( 24.0*a ); float w = a*(3.1416/2.356); float th = f*sin(w)*sin(w) + 0.001; float th2 = th; th += 0.25*exp2( -50.0*(w-1.4)*(w-1.4) ); vec3 wcol = mix( vec3(0.7,0.5,0.2), vec3(0.8,0.2,0.0), smoothstep( 0.0, 1.0, r ) ); wcol *= 1.4; wcol *= 1.0+0.1*sin(13.0*p.x+vec3(0,0,4))*sin(19.0*p.y+vec3(0,0,4)); vec3 v = vec3(10); v = trackMin(v,length(p-vec2(0.25,0.2))); v = trackMin(v,length(p-vec2(0.35,0.0))); v = trackMin(v,length(p-vec2(0.4,0.1))); v = trackMin(v,length(p-vec2(0.45,0.2))); v = trackMin(v,length(p-vec2(0.45,0.3))); v.yz -= v.x; float g = 2.0*v.y*v.z/max(v.y+v.z,0.001); wcol *= smoothstep(0.0,0.02,g); th2 -= 0.05*(1.0-smoothstep(0.0,0.05,g)); float isblack = smoothstep(0.02,0.03,(th2-r)*th2); vec2 q = vec2( mod(a,0.1)-0.05, (r-th+0.025)*3.1415*0.5 ); float d = length( q )-0.015; float ww = 1.0-smoothstep( 0.0, 0.01,d); if( r>th2 ) { vec2 q = fract(p*18.0)-0.5; vec2 iq = floor(p*18.0); float id= iq.x*111.0+iq.y*13.0; q += 0.25*sin(id*vec2(15,17)+vec2(0,2)); float r = 1.0+0.75*sin(id*431.0); ww = max( ww, 1.0-smoothstep(0.0,0.01,length(q)-0.2*r)); } wcol = mix( wcol, vec3(ww), 1.0-isblack ); d = r-th; float al = smoothstep( 0.0,2.0*fwidth(d),-d); col.xyz = mix( col.xyz, wcol, al ); col.w = 1.0 - (1.0-col.w)*(1.0-al); } return col; } int hash( ivec2 z ) { int n = z.x+z.y*11111; n = (n<<13)^n; return (n*(n*n*15731+789221)+1376312589)>>16; } #if HW_PERFORMANCE==0 const int AA = 2; #else const int AA = 4; #endif void mainImage( out vec4 fragColor, in vec2 fragCoord ) { float stime = mod( iTime, 6.0 ); vec3 col = vec3(0.0); for( int j=0; j<AA; j++ ) for( int i=0; i<AA; i++ ) { vec2 of = vec2(i,j)/float(AA); vec2 p = (2.0*(fragCoord+of)-iResolution.xy)/iResolution.y; p *= 1.6; //p.y += 0.5; float di = float( hash(ivec2(fragCoord)*AA+ivec2(i,j))&255 )/255.0; float time = stime + ((float(j*AA+i)+di)/float(AA*AA))*(0.5/30.0) - 0.5*0.5/30.0; time += 0.07*sin( p.y ); float wing = (p.x<0.0)?position_bad(time, 2.0): position_good(time, 2.0); if( p.x>0.0 ) p.x=p.x-1.4; else p.x=p.x+1.4; float an = 1.55*(0.5-0.5*cos(2.0*6.283185*wing)); vec2 pl = vec2(sign(p.x)*sin(an),-cos(an)); vec3 ro = vec3(0.0,0.085,2.1); vec3 rd = vec3(p,-3.0); vec3 pos = ro - rd*dot(ro.xz,pl)/dot(rd.xz,pl); vec2 q = vec2( length(pos.xz), pos.y ); vec4 tmp = butterfly( q ); tmp = clamp(tmp,0.0,1.0); tmp.xyz *= 0.1+0.9*mix(1.0,abs(q.y)*0.5+min(q.x*2.0,1.0),pl.x*pl.x); tmp.xyz *= clamp(0.25+0.75*(pl.x-pl.y+1.0),0.0,1.0); col += mix( vec3(0.5), tmp.xyz, tmp.w ); } col /= float(AA*AA); vec2 q = fragCoord/iResolution.xy; col += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0; fragColor = vec4(col,1.0); }
mit
[ 1635, 1679, 1723, 1723, 1805 ]
[ [ 1455, 1498, 1541, 1541, 1633 ], [ 1635, 1679, 1723, 1723, 1805 ], [ 1852, 1852, 1892, 1892, 2029 ], [ 2031, 2031, 2060, 2060, 5437 ], [ 5439, 5439, 5460, 5460, 5558 ] ]
// Correct integral of EaseInOut/Smoothstep
float position_good( float t, in float T ) {
if( t>=T ) return t - 0.5*T; float f = t/T; return f*f*f*(T-t*0.5); }
// Correct integral of EaseInOut/Smoothstep float position_good( float t, in float T ) {
1
1
slj3Dd
iq
2021-06-30T05:08:46
// The MIT License // Copyright © 2021 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. // Distance to an arrow // List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf // // and www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm // The arrow goes from a to b. It's thickness is w1. The arrow // head's thickness is w2. float sdArrow( in vec2 p, vec2 a, vec2 b, float w1, float w2 ) { // return min(length(p-a)-w1,length(p-b)); for debugging // constant setup const float k = 3.0; // arrow head ratio vec2 ba = b - a; float l2 = dot(ba,ba); float l = sqrt(l2); // pixel setup 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); // === distance (four segments) === 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 ) // conditional is optional { q = p; q.y -= clamp( q.y, 0.0, w1 ); di = min( di, dot(q,q) ); } //---- if( pz.x>0.0 ) // conditional is optional { 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) ); } // === sign === float si = 1.0; float z = l - p.x; if( min(p.x,z)>0.0 ) //if( p.x>0.0 && z>0.0 ) { float h = (pz.x<0.0) ? w1 : z/k; if( p.y<h ) si = -1.0; } return si*sqrt(di); } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // normalized pixel coordinates vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y; // animation float time = iTime*0.25; vec2 a = vec2(-1.0,0.0)+vec2(0.4,0.6)*cos(time*vec2(1.1,1.3)+vec2(0.0,1.0)); vec2 b = vec2( 1.0,0.0)+vec2(0.4,0.6)*cos(time*vec2(1.2,1.5)+vec2(0.3,2.0)); float w1 = 0.2;//0.05*(0.8+0.2*cos(time*0.31+2.0)); float w2 = w1 + 0.15; // distance float d = sdArrow(p, a, b, w1, w2); // coloring vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7); col *= 1.0 - exp(-5.0*abs(d)); col *= 0.8 + 0.2*cos(128.0*abs(d)); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.015,abs(d)) ); if( iMouse.z>0.001 ) { d = sdArrow(m, a, b, w1, w2); col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0025)); col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-m)-0.015)); } fragColor = vec4(col, 1.0); }
mit
[ 1261, 1351, 1415, 1499, 2515 ]
[ [ 1261, 1351, 1415, 1499, 2515 ], [ 2517, 2517, 2574, 2610, 3581 ] ]
// The arrow goes from a to b. It's thickness is w1. The arrow // head's thickness is w2.
float sdArrow( in vec2 p, vec2 a, vec2 b, float w1, float w2 ) {
const float k = 3.0; // arrow head ratio vec2 ba = b - a; float l2 = dot(ba,ba); float l = sqrt(l2); // pixel setup 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); // === distance (four segments) === 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 ) // conditional is optional { q = p; q.y -= clamp( q.y, 0.0, w1 ); di = min( di, dot(q,q) ); } //---- if( pz.x>0.0 ) // conditional is optional { 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) ); } // === sign === float si = 1.0; float z = l - p.x; if( min(p.x,z)>0.0 ) //if( p.x>0.0 && z>0.0 ) { float h = (pz.x<0.0) ? w1 : z/k; if( p.y<h ) si = -1.0; } return si*sqrt(di); }
// The arrow goes from a to b. It's thickness is w1. The arrow // head's thickness is w2. float sdArrow( in vec2 p, vec2 a, vec2 b, float w1, float w2 ) {
1
2
4tsBD7
iq
2021-07-15T22:22:15
// The MIT License // Copyright © 2013 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. // Ray-disk intersection. For general planar shapes, please see the // "coord system intersector" at https://www.shadertoy.com/view/lsfGDB // // // List of other ray-surface intersectors: // https://www.shadertoy.com/playlist/l3dXRf // and // http://iquilezles.org/www/articles/intersectors/intersectors.htm // disk: center c, normal n, radius r float diskIntersect( in vec3 ro, in vec3 rd, vec3 c, vec3 n, float r ) { vec3 o = ro - c; float t = -dot(n,o)/dot(rd,n); vec3 q = o + rd*t; return (dot(q,q)<r*r) ? t : -1.0; } // disk: center c, normal n, radius r float diskIntersectWithBackFaceCulling( in vec3 ro, in vec3 rd, vec3 c, vec3 n, float r ) { float d = dot(rd,n); if( d>0.0 ) return -1.0; vec3 o = ro - c; float t = -dot(n,o)/d; vec3 q = o + rd*t; return (dot(q,q)<r*r) ? t : -1.0; } #if HW_PERFORMANCE==0 #define AA 1 #else #define AA 2 #endif void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; #else vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; #endif // camera vec3 ro = 1.5*vec3(cos(0.15*iTime),0.0,sin(0.15*iTime)); 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)); // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.0*ww ); // render background vec3 col = vec3(0.08)*(1.0-0.3*length(p)) + 0.02*rd.y; // render disks (raycast them) const int num = 64; // number of disks float tmin = 1e20; vec3 onor = vec3(0.0); for( int i=0; i<num; i++ ) { // fibonacci points on a sphere const float kInvPhi = (sqrt(5.0)-1.0)/2.0; // one over golden ratio float cb = 1.0-2.0*(float(i)+0.5)/float(num); float sb = sqrt(1.0-cb*cb); float aa = 6.283185*kInvPhi*float(i); vec3 cen = vec3( sb*sin(aa), sb*cos(aa), cb ); // orient disk tangent to sphere surface vec3 nor = normalize(cen); // for full coverage, each disk's area should be 4PI/num, // ie, their radius should be 2/sqrt(num) float rad = (2.0/sqrt(float(num))); // but we only want partial coverage, for aesthetic reasons rad *= 0.5; // test for intersection with disk float t = diskIntersect( ro, rd, cen, nor, rad ); // trak intersections if( t>0.0 && t<tmin ) { tmin = t; onor = nor; } } // shade disk, if one found if( tmin<1000.0 ) { float dif = clamp( dot(onor,vec3(0.8,0.6,0.4)), 0.0, 1.0 ); float amb = 0.5 + 0.5*onor.y; col = vec3(0.2,0.3,0.4)*amb + vec3(0.8,0.75,0.6)*dif; } // gamma col = sqrt( col ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif // dither to remove banding in the background tot += fract(sin(fragCoord.x*vec3(13,1,11)+fragCoord.y*vec3(1,7,5))*158.391832)/255.0; fragColor = vec4( tot, 1.0 ); }
mit
[ 1395, 1433, 1505, 1505, 1623 ]
[ [ 1395, 1433, 1505, 1505, 1623 ], [ 1625, 1663, 1754, 1754, 1918 ] ]
// disk: center c, normal n, radius r
float diskIntersect( in vec3 ro, in vec3 rd, vec3 c, vec3 n, float r ) {
vec3 o = ro - c; float t = -dot(n,o)/dot(rd,n); vec3 q = o + rd*t; return (dot(q,q)<r*r) ? t : -1.0; }
// disk: center c, normal n, radius r float diskIntersect( in vec3 ro, in vec3 rd, vec3 c, vec3 n, float r ) {
1
1
ftXXWX
mrange
2021-07-18T19:01:48
// License CC0: Cable nest // Result after a few hours programming sunday afternoon #define TOLERANCE 0.0001 #define NORMTOL 0.00125 #define MAX_RAY_LENGTH 20.0 #define MAX_RAY_MARCHES 90 #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PI 3.141592654 #define TAU (2.0*PI) // https://stackoverflow.com/a/17897228/418488 const vec4 hsv2rgb_K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 hsv2rgb(vec3 c) { vec3 p = abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www); return c.z * mix(hsv2rgb_K.xxx, clamp(p - hsv2rgb_K.xxx, 0.0, 1.0), c.y); } #define HSV2RGB(c) (c.z * mix(hsv2rgb_K.xxx, clamp(abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www) - hsv2rgb_K.xxx, 0.0, 1.0), c.y)) #define PATHA vec2(0.1147, 0.2093) #define PATHB vec2(13.0, 3.0) const float cam_amp = 1.0; vec4 g_state = vec4(0.0); float tanh_approx(float x) { // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } // https://iquilezles.org/www/articles/spherefunctions/spherefunctions.htm float sphered(vec3 ro, vec3 rd, vec4 sph, float dbuffer) { float ndbuffer = dbuffer/sph.w; vec3 rc = (ro - sph.xyz)/sph.w; float b = dot(rd,rc); float c = dot(rc,rc) - 1.0; float h = b*b - c; if( h<0.0 ) return 0.0; h = sqrt( h ); float t1 = -b - h; float t2 = -b + h; if( t2<0.0 || t1>ndbuffer ) return 0.0; t1 = max( t1, 0.0 ); t2 = min( t2, ndbuffer ); float i1 = -(c*t1 + b*t1*t1 + t1*t1*t1/3.0); float i2 = -(c*t2 + b*t2*t2 + t2*t2*t2/3.0); return (i2-i1)*(3.0/4.0); } float hash(float co) { return fract(sin(co*12.9898) * 13758.5453); } vec3 cam_path(float z) { return vec3(cam_amp*sin(z*PATHA)*PATHB, z); } vec3 dcam_path(float z) { return vec3(cam_amp*PATHA*PATHB*cos(PATHA*z), 1.0); } vec3 ddcam_path(float z) { return cam_amp*vec3(cam_amp*-PATHA*PATHA*PATHB*sin(PATHA*z), 0.0); } float df(vec3 p3, out vec4 state) { float cylr = 0.2; vec2 p = p3.xy; float t = p3.z; const float ss = 1.5; mat2 pp = ss*ROT(1.0+0.5*p3.z); p *= ROT(-0.2*TIME); float s = 1.0; float d = 1E6; float tt = 0.0; for (int i = 0; i < 3; ++i) { tt += sqrt(2.0)*float(1+i); p *= pp; vec2 sp = sign(p); p = abs(p); tt += dot(sp, vec2(0.25, 0.5))*s; p -= 1.35*s; s *= 1.0/ss; float dd = (length(p-vec2(0.0))-cylr)*s; if (dd < d) { d = dd; state = vec4(p, t, hash(tt+123.4)); } } return d; } float df(vec3 p) { // Found this world warping technique somewhere but forgot which shader :( vec3 cam = cam_path(p.z); vec3 dcam = normalize(dcam_path(p.z)); p.xy -= cam.xy; p -= dcam*dot(vec3(p.xy, 0), dcam)*0.5*vec3(1,1,-1); vec4 state; float d = df(p, state); g_state = state; return d; } float rayMarch(in vec3 ro, in vec3 rd, out int iter) { float t = 0.0; int i = 0; for (i = 0; i < MAX_RAY_MARCHES; i++) { float distance = df(ro + rd*t); if (distance < TOLERANCE || t > MAX_RAY_LENGTH) break; t += distance; } iter = i; return t; } vec3 normal(in vec3 pos) { vec3 eps = vec3(NORMTOL,0.0,0.0); vec3 nor; nor.x = df(pos+eps.xyy) - df(pos-eps.xyy); nor.y = df(pos+eps.yxy) - df(pos-eps.yxy); nor.z = df(pos+eps.yyx) - df(pos-eps.yyx); return normalize(nor); } float softShadow(in vec3 pos, in vec3 ld, in float ll, float mint, float k) { const float minShadow = 0.25; float res = 1.0; float t = mint; for (int i=0; i<25; ++i) { float distance = df(pos + ld*t); res = min(res, k*distance/t); if (ll <= t) break; if(res <= minShadow) break; t += max(mint*0.2, distance); } return clamp(res,minShadow,1.0); } vec3 postProcess(in vec3 col, in vec2 q) { col=pow(clamp(col,0.0,1.0),vec3(1.0/2.2)); col=col*0.6+0.4*col*col*(3.0-2.0*col); // contrast col=mix(col, vec3(dot(col, vec3(0.33))), -0.4); // satuation col*=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); // vigneting return col; } vec3 render(vec3 ro, vec3 rd) { vec3 lightPos0 = cam_path(TIME-0.5); vec3 lightPos1 = cam_path(TIME+6.5); vec3 skyCol = vec3(0.0); int iter = 0; float t = rayMarch(ro, rd, iter); vec4 state = g_state; float tt = float(iter)/float(MAX_RAY_MARCHES); float bs = 1.0-tt*tt*tt*tt; vec3 pos = ro + t*rd; float lsd1 = sphered(ro, rd, vec4(lightPos1, 2.5), t); float beat = smoothstep(0.25, 1.0, sin(TAU*TIME*120.0/60.0)); vec3 bcol = mix(1.5*vec3(2.25, 0.75, 0.5), 3.5*vec3(2.0, 1.0, 0.75), beat); vec3 gcol = lsd1*bcol; if (t >= MAX_RAY_LENGTH) { return skyCol+gcol; } vec3 nor = normal(pos); float sa = atan(state.y, state.x)+4.0*state.z*(0.5+0.5*state.w); float v = 0.9*smoothstep(-0.1, 0.1, sin(4.0*sa)); vec3 color = hsv2rgb(vec3(0.0+123.4*state.w, 0.66, 0.75*v)); vec3 lv0 = lightPos0 - pos; float ll20 = dot(lv0, lv0); float ll0 = sqrt(ll20); vec3 ld0 = lv0 / ll0; float dm0 = 8.0/ll20; float sha0 = softShadow(pos, ld0, ll0, 0.125, 32.0); float dif0 = max(dot(nor,ld0),0.0)*dm0; vec3 lv1 = lightPos1 - pos; float ll21 = dot(lv1, lv1); float ll1 = sqrt(ll21); vec3 ld1 = lv1 / ll1; float spe1 = pow(max(dot(reflect(ld1, nor), rd), 0.), 100.)*tanh_approx(3.0/ll21); vec3 col = vec3(0.0); col += dif0*sha0*color; col += spe1*bcol*bs; col += gcol; return col; } vec3 effect3d(vec2 p) { float tm = TIME; vec3 cam = cam_path(tm); vec3 dcam = dcam_path(tm); vec3 ddcam= ddcam_path(tm); vec3 ro = cam; vec3 ww = normalize(dcam); vec3 uu = normalize(cross(vec3(0.0,1.0,0.0)+ddcam*-2.0, ww )); vec3 vv = normalize(cross(ww,uu)); float rdd = (2.0+0.5*tanh_approx(length(p))); vec3 rd = normalize(p.x*uu + p.y*vv + rdd*ww); vec3 col = render(ro, rd); return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q=fragCoord.xy/RESOLUTION.xy; vec2 p = -1.0 + 2.0*q; p.x *= RESOLUTION.x/RESOLUTION.y; vec3 col = effect3d(p); fragColor = vec4(postProcess(col, q),1.0); }
cc0-1.0
[ 1069, 1144, 1202, 1202, 1683 ]
[ [ 510, 510, 532, 532, 678 ], [ 943, 943, 971, 991, 1067 ], [ 1069, 1144, 1202, 1202, 1683 ], [ 1685, 1685, 1707, 1707, 1755 ], [ 1757, 1757, 1781, 1781, 1829 ], [ 1831, 1831, 1856, 1856, 1912 ], [ 1914, 1914, 1940, 1940, 2011 ], [ 2013, 2013, 2048, 2048, 2588 ], [ 2590, 2590, 2608, 2685, 2907 ], [ 2910, 2910, 2964, 2964, 3180 ], [ 3182, 3182, 3208, 3208, 3419 ], [ 3421, 3421, 3498, 3498, 3798 ], [ 3800, 3800, 3843, 3843, 4095 ], [ 4097, 4097, 4128, 4128, 5496 ], [ 5498, 5498, 5521, 5521, 5919 ], [ 5921, 5921, 5976, 5976, 6150 ] ]
// https://iquilezles.org/www/articles/spherefunctions/spherefunctions.htm
float sphered(vec3 ro, vec3 rd, vec4 sph, float dbuffer) {
float ndbuffer = dbuffer/sph.w; vec3 rc = (ro - sph.xyz)/sph.w; float b = dot(rd,rc); float c = dot(rc,rc) - 1.0; float h = b*b - c; if( h<0.0 ) return 0.0; h = sqrt( h ); float t1 = -b - h; float t2 = -b + h; if( t2<0.0 || t1>ndbuffer ) return 0.0; t1 = max( t1, 0.0 ); t2 = min( t2, ndbuffer ); float i1 = -(c*t1 + b*t1*t1 + t1*t1*t1/3.0); float i2 = -(c*t2 + b*t2*t2 + t2*t2*t2/3.0); return (i2-i1)*(3.0/4.0); }
// https://iquilezles.org/www/articles/spherefunctions/spherefunctions.htm float sphered(vec3 ro, vec3 rd, vec4 sph, float dbuffer) {
8
11
NlXXzs
iq
2021-07-14T17:22:14
// The MIT License // Copyright © 2021 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. // Closest point on a 3D box. For closest points on other primitives, check // // https://www.shadertoy.com/playlist/wXsSzB // Returns the closest point o, a 3D box // p is the point we are at // b is the box radius (3 half side lengths) // The box is axis aligned and centered at the origin. For a box rotated // by M,you need to transform p and the returned point by inverse(M). vec3 closestPointToBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; float m = min(0.0,max(d.x,max(d.y,d.z))); return p - vec3(d.x>=m?d.x:0.0, d.y>=m?d.y:0.0, d.z>=m?d.z:0.0)*sign(p); } // Alternative implementation vec3 closestPointToBox2( vec3 p, vec3 b ) { vec3 d = abs(p) - b; vec3 s = sign(p); // interior vec3 q; float ma; { q=p; q.x=s.x*b.x; ma=d.x; } if( d.y>ma ) { q=p; q.y=s.y*b.y; ma=d.y; } if( d.z>ma ) { q=p; q.z=s.z*b.z; ma=d.z; } if( ma<0.0 ) return q; // exterior return p - s*max(d,0.0); } // If the point is guaranteed to be always outside of the box, you can // use closestPointToBoxExterior() instead. vec3 closestPointToBoxExterior( vec3 p, vec3 b ) { return p-sign(p)*max(abs(p)-b,0.0); } //------------------------------------------------------------ // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdSphere( vec3 p, vec3 cen, float rad ) { return length(p-cen)-rad; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) { vec3 pa = p-a, ba = b-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ) - r; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBoxFrame( vec3 p, vec3 b, float e ) { p = abs(p )-b; vec3 q = abs(p+e)-e; return min(min( length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); } //------------------------------------------------------------ vec3 gPoint; vec2 map( in vec3 pos, bool showSurface ) { const vec3 box_rad = vec3(1.1,0.5,0.6); // compute closest point to gPoint on the surace of the box vec3 closestPoint = closestPointToBox(gPoint, box_rad ); // point vec2 res = vec2( sdSphere( pos, gPoint, 0.06 ), 1.0 ); // closest point { float d = sdSphere( pos, closestPoint, 0.06 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box (semi-transparent) if( showSurface ) { float d = sdBox( pos, box_rad ); if( d<res.x ) res = vec2( d, 3.0 ); } // segment { float d = sdCapsule( pos, gPoint, closestPoint, 0.015 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box edges { float d = sdBoxFrame( pos, box_rad, 0.01 ); if( d<res.x ) res = vec2( d, 5.0 ); } return res; } // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm vec3 calcNormal( in vec3 pos, in bool showSurface ) { vec2 e = vec2(1.0,-1.0)*0.5773; const float eps = 0.0005; return normalize( e.xyy*map( pos + e.xyy*eps, showSurface ).x + e.yyx*map( pos + e.yyx*eps, showSurface ).x + e.yxy*map( pos + e.yxy*eps, showSurface ).x + e.xxx*map( pos + e.xxx*eps, showSurface ).x ); } // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm float calcSoftShadow( vec3 ro, vec3 rd, bool showSurface ) { float res = 1.0; const float tmax = 2.0; float t = 0.001; for( int i=0; i<64; i++ ) { float h = map(ro + t*rd, showSurface).x; res = min( res, 64.0*h/t ); t += clamp(h, 0.01,0.5); if( res<-1.0 || t>tmax ) break; } res = max(res,-1.0); return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep, in [-1,1] } #if HW_PERFORMANCE==0 #define AA 1 #else #define AA 2 #endif void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord)*AA + ivec2(m,n); // time sample float td = 0.5+0.5*sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1); float time = iTime - (1.0/60.0)*(td+float(m*AA+n))/float(AA*AA-1); #else // pixel coordinates vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord); // time sample float time = iTime; #endif // animate camera float an = 0.25*time + 6.283185*iMouse.x/iResolution.x; vec3 ro = vec3( 2.4*cos(an), 0.7, 2.4*sin(an) ); vec3 ta = vec3( 0.0, -0.15, 0.0 ); // camera matrix vec3 ww = normalize( ta - ro ); vec3 uu = normalize( cross(ww,vec3(0.2,1.0,0.0) ) ); vec3 vv = normalize( cross(uu,ww)); // animate point gPoint = -sin(time*0.8*vec3(1.0,1.1,1.2)+vec3(4.0,2.0,1.0)); // make box transparent bool showSurface = ((samp.x+samp.y)&1)==0; // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raycast const float tmax = 5.0; float t = 0.0; float m = -1.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; vec2 hm = map(pos,showSurface); m = hm.y; if( hm.x<0.0001 || t>tmax ) break; t += hm.x; } // shade background vec3 col = vec3(0.05)*(1.0-0.2*length(p)); // shade objects if( t<tmax ) { // geometry vec3 pos = ro + t*rd; vec3 nor = calcNormal(pos,showSurface); // color vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) ); // lighting col = vec3(0.0); { // key light vec3 lig = normalize(vec3(0.3,0.7,0.2)); float dif = clamp( dot(nor,lig), 0.0, 1.0 ); if( dif>0.001 ) dif *= calcSoftShadow(pos+nor*0.001,lig,showSurface); col += mate*vec3(1.0,0.9,0.8)*dif; } { // dome light float dif = 0.5 + 0.5*nor.y; col += mate*vec3(0.2,0.3,0.4)*dif; } } // gamma col = pow( col, vec3(0.4545) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif // cheap dithering tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0; fragColor = vec4( tot, 1.0 ); }
mit
[ 1209, 1475, 1517, 1517, 1710 ]
[ [ 1209, 1475, 1517, 1517, 1710 ], [ 1712, 1742, 1785, 1785, 2087 ], [ 2089, 2204, 2254, 2254, 2296 ], [ 2362, 2433, 2464, 2464, 2555 ], [ 2557, 2628, 2675, 2675, 2707 ], [ 2709, 2780, 2832, 2832, 2946 ], [ 2948, 3019, 3064, 3064, 3352 ], [ 3432, 3432, 3475, 3475, 4274 ], [ 4276, 4340, 4393, 4393, 4692 ], [ 4694, 4756, 4816, 4816, 5189 ] ]
// Returns the closest point o, a 3D box // p is the point we are at // b is the box radius (3 half side lengths) // The box is axis aligned and centered at the origin. For a box rotated // by M,you need to transform p and the returned point by inverse(M).
vec3 closestPointToBox( vec3 p, vec3 b ) {
vec3 d = abs(p) - b; float m = min(0.0,max(d.x,max(d.y,d.z))); return p - vec3(d.x>=m?d.x:0.0, d.y>=m?d.y:0.0, d.z>=m?d.z:0.0)*sign(p); }
// Returns the closest point o, a 3D box // p is the point we are at // b is the box radius (3 half side lengths) // The box is axis aligned and centered at the origin. For a box rotated // by M,you need to transform p and the returned point by inverse(M). vec3 closestPointToBox( vec3 p, vec3 b ) {
1
1
NlXXzs
iq
2021-07-14T17:22:14
// The MIT License // Copyright © 2021 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. // Closest point on a 3D box. For closest points on other primitives, check // // https://www.shadertoy.com/playlist/wXsSzB // Returns the closest point o, a 3D box // p is the point we are at // b is the box radius (3 half side lengths) // The box is axis aligned and centered at the origin. For a box rotated // by M,you need to transform p and the returned point by inverse(M). vec3 closestPointToBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; float m = min(0.0,max(d.x,max(d.y,d.z))); return p - vec3(d.x>=m?d.x:0.0, d.y>=m?d.y:0.0, d.z>=m?d.z:0.0)*sign(p); } // Alternative implementation vec3 closestPointToBox2( vec3 p, vec3 b ) { vec3 d = abs(p) - b; vec3 s = sign(p); // interior vec3 q; float ma; { q=p; q.x=s.x*b.x; ma=d.x; } if( d.y>ma ) { q=p; q.y=s.y*b.y; ma=d.y; } if( d.z>ma ) { q=p; q.z=s.z*b.z; ma=d.z; } if( ma<0.0 ) return q; // exterior return p - s*max(d,0.0); } // If the point is guaranteed to be always outside of the box, you can // use closestPointToBoxExterior() instead. vec3 closestPointToBoxExterior( vec3 p, vec3 b ) { return p-sign(p)*max(abs(p)-b,0.0); } //------------------------------------------------------------ // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdSphere( vec3 p, vec3 cen, float rad ) { return length(p-cen)-rad; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) { vec3 pa = p-a, ba = b-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ) - r; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBoxFrame( vec3 p, vec3 b, float e ) { p = abs(p )-b; vec3 q = abs(p+e)-e; return min(min( length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); } //------------------------------------------------------------ vec3 gPoint; vec2 map( in vec3 pos, bool showSurface ) { const vec3 box_rad = vec3(1.1,0.5,0.6); // compute closest point to gPoint on the surace of the box vec3 closestPoint = closestPointToBox(gPoint, box_rad ); // point vec2 res = vec2( sdSphere( pos, gPoint, 0.06 ), 1.0 ); // closest point { float d = sdSphere( pos, closestPoint, 0.06 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box (semi-transparent) if( showSurface ) { float d = sdBox( pos, box_rad ); if( d<res.x ) res = vec2( d, 3.0 ); } // segment { float d = sdCapsule( pos, gPoint, closestPoint, 0.015 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box edges { float d = sdBoxFrame( pos, box_rad, 0.01 ); if( d<res.x ) res = vec2( d, 5.0 ); } return res; } // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm vec3 calcNormal( in vec3 pos, in bool showSurface ) { vec2 e = vec2(1.0,-1.0)*0.5773; const float eps = 0.0005; return normalize( e.xyy*map( pos + e.xyy*eps, showSurface ).x + e.yyx*map( pos + e.yyx*eps, showSurface ).x + e.yxy*map( pos + e.yxy*eps, showSurface ).x + e.xxx*map( pos + e.xxx*eps, showSurface ).x ); } // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm float calcSoftShadow( vec3 ro, vec3 rd, bool showSurface ) { float res = 1.0; const float tmax = 2.0; float t = 0.001; for( int i=0; i<64; i++ ) { float h = map(ro + t*rd, showSurface).x; res = min( res, 64.0*h/t ); t += clamp(h, 0.01,0.5); if( res<-1.0 || t>tmax ) break; } res = max(res,-1.0); return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep, in [-1,1] } #if HW_PERFORMANCE==0 #define AA 1 #else #define AA 2 #endif void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord)*AA + ivec2(m,n); // time sample float td = 0.5+0.5*sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1); float time = iTime - (1.0/60.0)*(td+float(m*AA+n))/float(AA*AA-1); #else // pixel coordinates vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord); // time sample float time = iTime; #endif // animate camera float an = 0.25*time + 6.283185*iMouse.x/iResolution.x; vec3 ro = vec3( 2.4*cos(an), 0.7, 2.4*sin(an) ); vec3 ta = vec3( 0.0, -0.15, 0.0 ); // camera matrix vec3 ww = normalize( ta - ro ); vec3 uu = normalize( cross(ww,vec3(0.2,1.0,0.0) ) ); vec3 vv = normalize( cross(uu,ww)); // animate point gPoint = -sin(time*0.8*vec3(1.0,1.1,1.2)+vec3(4.0,2.0,1.0)); // make box transparent bool showSurface = ((samp.x+samp.y)&1)==0; // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raycast const float tmax = 5.0; float t = 0.0; float m = -1.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; vec2 hm = map(pos,showSurface); m = hm.y; if( hm.x<0.0001 || t>tmax ) break; t += hm.x; } // shade background vec3 col = vec3(0.05)*(1.0-0.2*length(p)); // shade objects if( t<tmax ) { // geometry vec3 pos = ro + t*rd; vec3 nor = calcNormal(pos,showSurface); // color vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) ); // lighting col = vec3(0.0); { // key light vec3 lig = normalize(vec3(0.3,0.7,0.2)); float dif = clamp( dot(nor,lig), 0.0, 1.0 ); if( dif>0.001 ) dif *= calcSoftShadow(pos+nor*0.001,lig,showSurface); col += mate*vec3(1.0,0.9,0.8)*dif; } { // dome light float dif = 0.5 + 0.5*nor.y; col += mate*vec3(0.2,0.3,0.4)*dif; } } // gamma col = pow( col, vec3(0.4545) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif // cheap dithering tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0; fragColor = vec4( tot, 1.0 ); }
mit
[ 2362, 2433, 2464, 2464, 2555 ]
[ [ 1209, 1475, 1517, 1517, 1710 ], [ 1712, 1742, 1785, 1785, 2087 ], [ 2089, 2204, 2254, 2254, 2296 ], [ 2362, 2433, 2464, 2464, 2555 ], [ 2557, 2628, 2675, 2675, 2707 ], [ 2709, 2780, 2832, 2832, 2946 ], [ 2948, 3019, 3064, 3064, 3352 ], [ 3432, 3432, 3475, 3475, 4274 ], [ 4276, 4340, 4393, 4393, 4692 ], [ 4694, 4756, 4816, 4816, 5189 ] ]
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdBox( vec3 p, vec3 b ) {
vec3 d = abs(p) - b; return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); }
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBox( vec3 p, vec3 b ) {
61
579
NlXXzs
iq
2021-07-14T17:22:14
// The MIT License // Copyright © 2021 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. // Closest point on a 3D box. For closest points on other primitives, check // // https://www.shadertoy.com/playlist/wXsSzB // Returns the closest point o, a 3D box // p is the point we are at // b is the box radius (3 half side lengths) // The box is axis aligned and centered at the origin. For a box rotated // by M,you need to transform p and the returned point by inverse(M). vec3 closestPointToBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; float m = min(0.0,max(d.x,max(d.y,d.z))); return p - vec3(d.x>=m?d.x:0.0, d.y>=m?d.y:0.0, d.z>=m?d.z:0.0)*sign(p); } // Alternative implementation vec3 closestPointToBox2( vec3 p, vec3 b ) { vec3 d = abs(p) - b; vec3 s = sign(p); // interior vec3 q; float ma; { q=p; q.x=s.x*b.x; ma=d.x; } if( d.y>ma ) { q=p; q.y=s.y*b.y; ma=d.y; } if( d.z>ma ) { q=p; q.z=s.z*b.z; ma=d.z; } if( ma<0.0 ) return q; // exterior return p - s*max(d,0.0); } // If the point is guaranteed to be always outside of the box, you can // use closestPointToBoxExterior() instead. vec3 closestPointToBoxExterior( vec3 p, vec3 b ) { return p-sign(p)*max(abs(p)-b,0.0); } //------------------------------------------------------------ // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdSphere( vec3 p, vec3 cen, float rad ) { return length(p-cen)-rad; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) { vec3 pa = p-a, ba = b-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ) - r; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBoxFrame( vec3 p, vec3 b, float e ) { p = abs(p )-b; vec3 q = abs(p+e)-e; return min(min( length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); } //------------------------------------------------------------ vec3 gPoint; vec2 map( in vec3 pos, bool showSurface ) { const vec3 box_rad = vec3(1.1,0.5,0.6); // compute closest point to gPoint on the surace of the box vec3 closestPoint = closestPointToBox(gPoint, box_rad ); // point vec2 res = vec2( sdSphere( pos, gPoint, 0.06 ), 1.0 ); // closest point { float d = sdSphere( pos, closestPoint, 0.06 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box (semi-transparent) if( showSurface ) { float d = sdBox( pos, box_rad ); if( d<res.x ) res = vec2( d, 3.0 ); } // segment { float d = sdCapsule( pos, gPoint, closestPoint, 0.015 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box edges { float d = sdBoxFrame( pos, box_rad, 0.01 ); if( d<res.x ) res = vec2( d, 5.0 ); } return res; } // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm vec3 calcNormal( in vec3 pos, in bool showSurface ) { vec2 e = vec2(1.0,-1.0)*0.5773; const float eps = 0.0005; return normalize( e.xyy*map( pos + e.xyy*eps, showSurface ).x + e.yyx*map( pos + e.yyx*eps, showSurface ).x + e.yxy*map( pos + e.yxy*eps, showSurface ).x + e.xxx*map( pos + e.xxx*eps, showSurface ).x ); } // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm float calcSoftShadow( vec3 ro, vec3 rd, bool showSurface ) { float res = 1.0; const float tmax = 2.0; float t = 0.001; for( int i=0; i<64; i++ ) { float h = map(ro + t*rd, showSurface).x; res = min( res, 64.0*h/t ); t += clamp(h, 0.01,0.5); if( res<-1.0 || t>tmax ) break; } res = max(res,-1.0); return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep, in [-1,1] } #if HW_PERFORMANCE==0 #define AA 1 #else #define AA 2 #endif void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord)*AA + ivec2(m,n); // time sample float td = 0.5+0.5*sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1); float time = iTime - (1.0/60.0)*(td+float(m*AA+n))/float(AA*AA-1); #else // pixel coordinates vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord); // time sample float time = iTime; #endif // animate camera float an = 0.25*time + 6.283185*iMouse.x/iResolution.x; vec3 ro = vec3( 2.4*cos(an), 0.7, 2.4*sin(an) ); vec3 ta = vec3( 0.0, -0.15, 0.0 ); // camera matrix vec3 ww = normalize( ta - ro ); vec3 uu = normalize( cross(ww,vec3(0.2,1.0,0.0) ) ); vec3 vv = normalize( cross(uu,ww)); // animate point gPoint = -sin(time*0.8*vec3(1.0,1.1,1.2)+vec3(4.0,2.0,1.0)); // make box transparent bool showSurface = ((samp.x+samp.y)&1)==0; // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raycast const float tmax = 5.0; float t = 0.0; float m = -1.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; vec2 hm = map(pos,showSurface); m = hm.y; if( hm.x<0.0001 || t>tmax ) break; t += hm.x; } // shade background vec3 col = vec3(0.05)*(1.0-0.2*length(p)); // shade objects if( t<tmax ) { // geometry vec3 pos = ro + t*rd; vec3 nor = calcNormal(pos,showSurface); // color vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) ); // lighting col = vec3(0.0); { // key light vec3 lig = normalize(vec3(0.3,0.7,0.2)); float dif = clamp( dot(nor,lig), 0.0, 1.0 ); if( dif>0.001 ) dif *= calcSoftShadow(pos+nor*0.001,lig,showSurface); col += mate*vec3(1.0,0.9,0.8)*dif; } { // dome light float dif = 0.5 + 0.5*nor.y; col += mate*vec3(0.2,0.3,0.4)*dif; } } // gamma col = pow( col, vec3(0.4545) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif // cheap dithering tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0; fragColor = vec4( tot, 1.0 ); }
mit
[ 2557, 2628, 2675, 2675, 2707 ]
[ [ 1209, 1475, 1517, 1517, 1710 ], [ 1712, 1742, 1785, 1785, 2087 ], [ 2089, 2204, 2254, 2254, 2296 ], [ 2362, 2433, 2464, 2464, 2555 ], [ 2557, 2628, 2675, 2675, 2707 ], [ 2709, 2780, 2832, 2832, 2946 ], [ 2948, 3019, 3064, 3064, 3352 ], [ 3432, 3432, 3475, 3475, 4274 ], [ 4276, 4340, 4393, 4393, 4692 ], [ 4694, 4756, 4816, 4816, 5189 ] ]
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdSphere( vec3 p, vec3 cen, float rad ) {
return length(p-cen)-rad; }
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdSphere( vec3 p, vec3 cen, float rad ) {
2
2
NlXXzs
iq
2021-07-14T17:22:14
// The MIT License // Copyright © 2021 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. // Closest point on a 3D box. For closest points on other primitives, check // // https://www.shadertoy.com/playlist/wXsSzB // Returns the closest point o, a 3D box // p is the point we are at // b is the box radius (3 half side lengths) // The box is axis aligned and centered at the origin. For a box rotated // by M,you need to transform p and the returned point by inverse(M). vec3 closestPointToBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; float m = min(0.0,max(d.x,max(d.y,d.z))); return p - vec3(d.x>=m?d.x:0.0, d.y>=m?d.y:0.0, d.z>=m?d.z:0.0)*sign(p); } // Alternative implementation vec3 closestPointToBox2( vec3 p, vec3 b ) { vec3 d = abs(p) - b; vec3 s = sign(p); // interior vec3 q; float ma; { q=p; q.x=s.x*b.x; ma=d.x; } if( d.y>ma ) { q=p; q.y=s.y*b.y; ma=d.y; } if( d.z>ma ) { q=p; q.z=s.z*b.z; ma=d.z; } if( ma<0.0 ) return q; // exterior return p - s*max(d,0.0); } // If the point is guaranteed to be always outside of the box, you can // use closestPointToBoxExterior() instead. vec3 closestPointToBoxExterior( vec3 p, vec3 b ) { return p-sign(p)*max(abs(p)-b,0.0); } //------------------------------------------------------------ // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdSphere( vec3 p, vec3 cen, float rad ) { return length(p-cen)-rad; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) { vec3 pa = p-a, ba = b-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ) - r; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBoxFrame( vec3 p, vec3 b, float e ) { p = abs(p )-b; vec3 q = abs(p+e)-e; return min(min( length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); } //------------------------------------------------------------ vec3 gPoint; vec2 map( in vec3 pos, bool showSurface ) { const vec3 box_rad = vec3(1.1,0.5,0.6); // compute closest point to gPoint on the surace of the box vec3 closestPoint = closestPointToBox(gPoint, box_rad ); // point vec2 res = vec2( sdSphere( pos, gPoint, 0.06 ), 1.0 ); // closest point { float d = sdSphere( pos, closestPoint, 0.06 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box (semi-transparent) if( showSurface ) { float d = sdBox( pos, box_rad ); if( d<res.x ) res = vec2( d, 3.0 ); } // segment { float d = sdCapsule( pos, gPoint, closestPoint, 0.015 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box edges { float d = sdBoxFrame( pos, box_rad, 0.01 ); if( d<res.x ) res = vec2( d, 5.0 ); } return res; } // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm vec3 calcNormal( in vec3 pos, in bool showSurface ) { vec2 e = vec2(1.0,-1.0)*0.5773; const float eps = 0.0005; return normalize( e.xyy*map( pos + e.xyy*eps, showSurface ).x + e.yyx*map( pos + e.yyx*eps, showSurface ).x + e.yxy*map( pos + e.yxy*eps, showSurface ).x + e.xxx*map( pos + e.xxx*eps, showSurface ).x ); } // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm float calcSoftShadow( vec3 ro, vec3 rd, bool showSurface ) { float res = 1.0; const float tmax = 2.0; float t = 0.001; for( int i=0; i<64; i++ ) { float h = map(ro + t*rd, showSurface).x; res = min( res, 64.0*h/t ); t += clamp(h, 0.01,0.5); if( res<-1.0 || t>tmax ) break; } res = max(res,-1.0); return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep, in [-1,1] } #if HW_PERFORMANCE==0 #define AA 1 #else #define AA 2 #endif void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord)*AA + ivec2(m,n); // time sample float td = 0.5+0.5*sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1); float time = iTime - (1.0/60.0)*(td+float(m*AA+n))/float(AA*AA-1); #else // pixel coordinates vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord); // time sample float time = iTime; #endif // animate camera float an = 0.25*time + 6.283185*iMouse.x/iResolution.x; vec3 ro = vec3( 2.4*cos(an), 0.7, 2.4*sin(an) ); vec3 ta = vec3( 0.0, -0.15, 0.0 ); // camera matrix vec3 ww = normalize( ta - ro ); vec3 uu = normalize( cross(ww,vec3(0.2,1.0,0.0) ) ); vec3 vv = normalize( cross(uu,ww)); // animate point gPoint = -sin(time*0.8*vec3(1.0,1.1,1.2)+vec3(4.0,2.0,1.0)); // make box transparent bool showSurface = ((samp.x+samp.y)&1)==0; // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raycast const float tmax = 5.0; float t = 0.0; float m = -1.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; vec2 hm = map(pos,showSurface); m = hm.y; if( hm.x<0.0001 || t>tmax ) break; t += hm.x; } // shade background vec3 col = vec3(0.05)*(1.0-0.2*length(p)); // shade objects if( t<tmax ) { // geometry vec3 pos = ro + t*rd; vec3 nor = calcNormal(pos,showSurface); // color vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) ); // lighting col = vec3(0.0); { // key light vec3 lig = normalize(vec3(0.3,0.7,0.2)); float dif = clamp( dot(nor,lig), 0.0, 1.0 ); if( dif>0.001 ) dif *= calcSoftShadow(pos+nor*0.001,lig,showSurface); col += mate*vec3(1.0,0.9,0.8)*dif; } { // dome light float dif = 0.5 + 0.5*nor.y; col += mate*vec3(0.2,0.3,0.4)*dif; } } // gamma col = pow( col, vec3(0.4545) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif // cheap dithering tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0; fragColor = vec4( tot, 1.0 ); }
mit
[ 2709, 2780, 2832, 2832, 2946 ]
[ [ 1209, 1475, 1517, 1517, 1710 ], [ 1712, 1742, 1785, 1785, 2087 ], [ 2089, 2204, 2254, 2254, 2296 ], [ 2362, 2433, 2464, 2464, 2555 ], [ 2557, 2628, 2675, 2675, 2707 ], [ 2709, 2780, 2832, 2832, 2946 ], [ 2948, 3019, 3064, 3064, 3352 ], [ 3432, 3432, 3475, 3475, 4274 ], [ 4276, 4340, 4393, 4393, 4692 ], [ 4694, 4756, 4816, 4816, 5189 ] ]
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) {
vec3 pa = p-a, ba = b-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ) - r; }
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) {
56
160
NlXXzs
iq
2021-07-14T17:22:14
// The MIT License // Copyright © 2021 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. // Closest point on a 3D box. For closest points on other primitives, check // // https://www.shadertoy.com/playlist/wXsSzB // Returns the closest point o, a 3D box // p is the point we are at // b is the box radius (3 half side lengths) // The box is axis aligned and centered at the origin. For a box rotated // by M,you need to transform p and the returned point by inverse(M). vec3 closestPointToBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; float m = min(0.0,max(d.x,max(d.y,d.z))); return p - vec3(d.x>=m?d.x:0.0, d.y>=m?d.y:0.0, d.z>=m?d.z:0.0)*sign(p); } // Alternative implementation vec3 closestPointToBox2( vec3 p, vec3 b ) { vec3 d = abs(p) - b; vec3 s = sign(p); // interior vec3 q; float ma; { q=p; q.x=s.x*b.x; ma=d.x; } if( d.y>ma ) { q=p; q.y=s.y*b.y; ma=d.y; } if( d.z>ma ) { q=p; q.z=s.z*b.z; ma=d.z; } if( ma<0.0 ) return q; // exterior return p - s*max(d,0.0); } // If the point is guaranteed to be always outside of the box, you can // use closestPointToBoxExterior() instead. vec3 closestPointToBoxExterior( vec3 p, vec3 b ) { return p-sign(p)*max(abs(p)-b,0.0); } //------------------------------------------------------------ // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdSphere( vec3 p, vec3 cen, float rad ) { return length(p-cen)-rad; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) { vec3 pa = p-a, ba = b-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ) - r; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBoxFrame( vec3 p, vec3 b, float e ) { p = abs(p )-b; vec3 q = abs(p+e)-e; return min(min( length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); } //------------------------------------------------------------ vec3 gPoint; vec2 map( in vec3 pos, bool showSurface ) { const vec3 box_rad = vec3(1.1,0.5,0.6); // compute closest point to gPoint on the surace of the box vec3 closestPoint = closestPointToBox(gPoint, box_rad ); // point vec2 res = vec2( sdSphere( pos, gPoint, 0.06 ), 1.0 ); // closest point { float d = sdSphere( pos, closestPoint, 0.06 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box (semi-transparent) if( showSurface ) { float d = sdBox( pos, box_rad ); if( d<res.x ) res = vec2( d, 3.0 ); } // segment { float d = sdCapsule( pos, gPoint, closestPoint, 0.015 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box edges { float d = sdBoxFrame( pos, box_rad, 0.01 ); if( d<res.x ) res = vec2( d, 5.0 ); } return res; } // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm vec3 calcNormal( in vec3 pos, in bool showSurface ) { vec2 e = vec2(1.0,-1.0)*0.5773; const float eps = 0.0005; return normalize( e.xyy*map( pos + e.xyy*eps, showSurface ).x + e.yyx*map( pos + e.yyx*eps, showSurface ).x + e.yxy*map( pos + e.yxy*eps, showSurface ).x + e.xxx*map( pos + e.xxx*eps, showSurface ).x ); } // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm float calcSoftShadow( vec3 ro, vec3 rd, bool showSurface ) { float res = 1.0; const float tmax = 2.0; float t = 0.001; for( int i=0; i<64; i++ ) { float h = map(ro + t*rd, showSurface).x; res = min( res, 64.0*h/t ); t += clamp(h, 0.01,0.5); if( res<-1.0 || t>tmax ) break; } res = max(res,-1.0); return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep, in [-1,1] } #if HW_PERFORMANCE==0 #define AA 1 #else #define AA 2 #endif void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord)*AA + ivec2(m,n); // time sample float td = 0.5+0.5*sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1); float time = iTime - (1.0/60.0)*(td+float(m*AA+n))/float(AA*AA-1); #else // pixel coordinates vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord); // time sample float time = iTime; #endif // animate camera float an = 0.25*time + 6.283185*iMouse.x/iResolution.x; vec3 ro = vec3( 2.4*cos(an), 0.7, 2.4*sin(an) ); vec3 ta = vec3( 0.0, -0.15, 0.0 ); // camera matrix vec3 ww = normalize( ta - ro ); vec3 uu = normalize( cross(ww,vec3(0.2,1.0,0.0) ) ); vec3 vv = normalize( cross(uu,ww)); // animate point gPoint = -sin(time*0.8*vec3(1.0,1.1,1.2)+vec3(4.0,2.0,1.0)); // make box transparent bool showSurface = ((samp.x+samp.y)&1)==0; // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raycast const float tmax = 5.0; float t = 0.0; float m = -1.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; vec2 hm = map(pos,showSurface); m = hm.y; if( hm.x<0.0001 || t>tmax ) break; t += hm.x; } // shade background vec3 col = vec3(0.05)*(1.0-0.2*length(p)); // shade objects if( t<tmax ) { // geometry vec3 pos = ro + t*rd; vec3 nor = calcNormal(pos,showSurface); // color vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) ); // lighting col = vec3(0.0); { // key light vec3 lig = normalize(vec3(0.3,0.7,0.2)); float dif = clamp( dot(nor,lig), 0.0, 1.0 ); if( dif>0.001 ) dif *= calcSoftShadow(pos+nor*0.001,lig,showSurface); col += mate*vec3(1.0,0.9,0.8)*dif; } { // dome light float dif = 0.5 + 0.5*nor.y; col += mate*vec3(0.2,0.3,0.4)*dif; } } // gamma col = pow( col, vec3(0.4545) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif // cheap dithering tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0; fragColor = vec4( tot, 1.0 ); }
mit
[ 2948, 3019, 3064, 3064, 3352 ]
[ [ 1209, 1475, 1517, 1517, 1710 ], [ 1712, 1742, 1785, 1785, 2087 ], [ 2089, 2204, 2254, 2254, 2296 ], [ 2362, 2433, 2464, 2464, 2555 ], [ 2557, 2628, 2675, 2675, 2707 ], [ 2709, 2780, 2832, 2832, 2946 ], [ 2948, 3019, 3064, 3064, 3352 ], [ 3432, 3432, 3475, 3475, 4274 ], [ 4276, 4340, 4393, 4393, 4692 ], [ 4694, 4756, 4816, 4816, 5189 ] ]
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdBoxFrame( vec3 p, vec3 b, float e ) {
p = abs(p )-b; vec3 q = abs(p+e)-e; return min(min( length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); }
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBoxFrame( vec3 p, vec3 b, float e ) {
1
20
NlXXzs
iq
2021-07-14T17:22:14
// The MIT License // Copyright © 2021 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. // Closest point on a 3D box. For closest points on other primitives, check // // https://www.shadertoy.com/playlist/wXsSzB // Returns the closest point o, a 3D box // p is the point we are at // b is the box radius (3 half side lengths) // The box is axis aligned and centered at the origin. For a box rotated // by M,you need to transform p and the returned point by inverse(M). vec3 closestPointToBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; float m = min(0.0,max(d.x,max(d.y,d.z))); return p - vec3(d.x>=m?d.x:0.0, d.y>=m?d.y:0.0, d.z>=m?d.z:0.0)*sign(p); } // Alternative implementation vec3 closestPointToBox2( vec3 p, vec3 b ) { vec3 d = abs(p) - b; vec3 s = sign(p); // interior vec3 q; float ma; { q=p; q.x=s.x*b.x; ma=d.x; } if( d.y>ma ) { q=p; q.y=s.y*b.y; ma=d.y; } if( d.z>ma ) { q=p; q.z=s.z*b.z; ma=d.z; } if( ma<0.0 ) return q; // exterior return p - s*max(d,0.0); } // If the point is guaranteed to be always outside of the box, you can // use closestPointToBoxExterior() instead. vec3 closestPointToBoxExterior( vec3 p, vec3 b ) { return p-sign(p)*max(abs(p)-b,0.0); } //------------------------------------------------------------ // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdSphere( vec3 p, vec3 cen, float rad ) { return length(p-cen)-rad; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) { vec3 pa = p-a, ba = b-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ) - r; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBoxFrame( vec3 p, vec3 b, float e ) { p = abs(p )-b; vec3 q = abs(p+e)-e; return min(min( length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); } //------------------------------------------------------------ vec3 gPoint; vec2 map( in vec3 pos, bool showSurface ) { const vec3 box_rad = vec3(1.1,0.5,0.6); // compute closest point to gPoint on the surace of the box vec3 closestPoint = closestPointToBox(gPoint, box_rad ); // point vec2 res = vec2( sdSphere( pos, gPoint, 0.06 ), 1.0 ); // closest point { float d = sdSphere( pos, closestPoint, 0.06 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box (semi-transparent) if( showSurface ) { float d = sdBox( pos, box_rad ); if( d<res.x ) res = vec2( d, 3.0 ); } // segment { float d = sdCapsule( pos, gPoint, closestPoint, 0.015 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box edges { float d = sdBoxFrame( pos, box_rad, 0.01 ); if( d<res.x ) res = vec2( d, 5.0 ); } return res; } // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm vec3 calcNormal( in vec3 pos, in bool showSurface ) { vec2 e = vec2(1.0,-1.0)*0.5773; const float eps = 0.0005; return normalize( e.xyy*map( pos + e.xyy*eps, showSurface ).x + e.yyx*map( pos + e.yyx*eps, showSurface ).x + e.yxy*map( pos + e.yxy*eps, showSurface ).x + e.xxx*map( pos + e.xxx*eps, showSurface ).x ); } // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm float calcSoftShadow( vec3 ro, vec3 rd, bool showSurface ) { float res = 1.0; const float tmax = 2.0; float t = 0.001; for( int i=0; i<64; i++ ) { float h = map(ro + t*rd, showSurface).x; res = min( res, 64.0*h/t ); t += clamp(h, 0.01,0.5); if( res<-1.0 || t>tmax ) break; } res = max(res,-1.0); return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep, in [-1,1] } #if HW_PERFORMANCE==0 #define AA 1 #else #define AA 2 #endif void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord)*AA + ivec2(m,n); // time sample float td = 0.5+0.5*sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1); float time = iTime - (1.0/60.0)*(td+float(m*AA+n))/float(AA*AA-1); #else // pixel coordinates vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord); // time sample float time = iTime; #endif // animate camera float an = 0.25*time + 6.283185*iMouse.x/iResolution.x; vec3 ro = vec3( 2.4*cos(an), 0.7, 2.4*sin(an) ); vec3 ta = vec3( 0.0, -0.15, 0.0 ); // camera matrix vec3 ww = normalize( ta - ro ); vec3 uu = normalize( cross(ww,vec3(0.2,1.0,0.0) ) ); vec3 vv = normalize( cross(uu,ww)); // animate point gPoint = -sin(time*0.8*vec3(1.0,1.1,1.2)+vec3(4.0,2.0,1.0)); // make box transparent bool showSurface = ((samp.x+samp.y)&1)==0; // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raycast const float tmax = 5.0; float t = 0.0; float m = -1.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; vec2 hm = map(pos,showSurface); m = hm.y; if( hm.x<0.0001 || t>tmax ) break; t += hm.x; } // shade background vec3 col = vec3(0.05)*(1.0-0.2*length(p)); // shade objects if( t<tmax ) { // geometry vec3 pos = ro + t*rd; vec3 nor = calcNormal(pos,showSurface); // color vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) ); // lighting col = vec3(0.0); { // key light vec3 lig = normalize(vec3(0.3,0.7,0.2)); float dif = clamp( dot(nor,lig), 0.0, 1.0 ); if( dif>0.001 ) dif *= calcSoftShadow(pos+nor*0.001,lig,showSurface); col += mate*vec3(1.0,0.9,0.8)*dif; } { // dome light float dif = 0.5 + 0.5*nor.y; col += mate*vec3(0.2,0.3,0.4)*dif; } } // gamma col = pow( col, vec3(0.4545) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif // cheap dithering tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0; fragColor = vec4( tot, 1.0 ); }
mit
[ 4276, 4340, 4393, 4393, 4692 ]
[ [ 1209, 1475, 1517, 1517, 1710 ], [ 1712, 1742, 1785, 1785, 2087 ], [ 2089, 2204, 2254, 2254, 2296 ], [ 2362, 2433, 2464, 2464, 2555 ], [ 2557, 2628, 2675, 2675, 2707 ], [ 2709, 2780, 2832, 2832, 2946 ], [ 2948, 3019, 3064, 3064, 3352 ], [ 3432, 3432, 3475, 3475, 4274 ], [ 4276, 4340, 4393, 4393, 4692 ], [ 4694, 4756, 4816, 4816, 5189 ] ]
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos, in bool showSurface ) {
vec2 e = vec2(1.0,-1.0)*0.5773; const float eps = 0.0005; return normalize( e.xyy*map( pos + e.xyy*eps, showSurface ).x + e.yyx*map( pos + e.yyx*eps, showSurface ).x + e.yxy*map( pos + e.yxy*eps, showSurface ).x + e.xxx*map( pos + e.xxx*eps, showSurface ).x ); }
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm vec3 calcNormal( in vec3 pos, in bool showSurface ) {
1
1
NlXXzs
iq
2021-07-14T17:22:14
// The MIT License // Copyright © 2021 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. // Closest point on a 3D box. For closest points on other primitives, check // // https://www.shadertoy.com/playlist/wXsSzB // Returns the closest point o, a 3D box // p is the point we are at // b is the box radius (3 half side lengths) // The box is axis aligned and centered at the origin. For a box rotated // by M,you need to transform p and the returned point by inverse(M). vec3 closestPointToBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; float m = min(0.0,max(d.x,max(d.y,d.z))); return p - vec3(d.x>=m?d.x:0.0, d.y>=m?d.y:0.0, d.z>=m?d.z:0.0)*sign(p); } // Alternative implementation vec3 closestPointToBox2( vec3 p, vec3 b ) { vec3 d = abs(p) - b; vec3 s = sign(p); // interior vec3 q; float ma; { q=p; q.x=s.x*b.x; ma=d.x; } if( d.y>ma ) { q=p; q.y=s.y*b.y; ma=d.y; } if( d.z>ma ) { q=p; q.z=s.z*b.z; ma=d.z; } if( ma<0.0 ) return q; // exterior return p - s*max(d,0.0); } // If the point is guaranteed to be always outside of the box, you can // use closestPointToBoxExterior() instead. vec3 closestPointToBoxExterior( vec3 p, vec3 b ) { return p-sign(p)*max(abs(p)-b,0.0); } //------------------------------------------------------------ // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBox( vec3 p, vec3 b ) { vec3 d = abs(p) - b; return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdSphere( vec3 p, vec3 cen, float rad ) { return length(p-cen)-rad; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) { vec3 pa = p-a, ba = b-a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ) - r; } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float sdBoxFrame( vec3 p, vec3 b, float e ) { p = abs(p )-b; vec3 q = abs(p+e)-e; return min(min( length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); } //------------------------------------------------------------ vec3 gPoint; vec2 map( in vec3 pos, bool showSurface ) { const vec3 box_rad = vec3(1.1,0.5,0.6); // compute closest point to gPoint on the surace of the box vec3 closestPoint = closestPointToBox(gPoint, box_rad ); // point vec2 res = vec2( sdSphere( pos, gPoint, 0.06 ), 1.0 ); // closest point { float d = sdSphere( pos, closestPoint, 0.06 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box (semi-transparent) if( showSurface ) { float d = sdBox( pos, box_rad ); if( d<res.x ) res = vec2( d, 3.0 ); } // segment { float d = sdCapsule( pos, gPoint, closestPoint, 0.015 ); if( d<res.x ) res = vec2( d, 4.0 ); } // box edges { float d = sdBoxFrame( pos, box_rad, 0.01 ); if( d<res.x ) res = vec2( d, 5.0 ); } return res; } // http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm vec3 calcNormal( in vec3 pos, in bool showSurface ) { vec2 e = vec2(1.0,-1.0)*0.5773; const float eps = 0.0005; return normalize( e.xyy*map( pos + e.xyy*eps, showSurface ).x + e.yyx*map( pos + e.yyx*eps, showSurface ).x + e.yxy*map( pos + e.yxy*eps, showSurface ).x + e.xxx*map( pos + e.xxx*eps, showSurface ).x ); } // http://iquilezles.org/www/articles/rmshadows/rmshadows.htm float calcSoftShadow( vec3 ro, vec3 rd, bool showSurface ) { float res = 1.0; const float tmax = 2.0; float t = 0.001; for( int i=0; i<64; i++ ) { float h = map(ro + t*rd, showSurface).x; res = min( res, 64.0*h/t ); t += clamp(h, 0.01,0.5); if( res<-1.0 || t>tmax ) break; } res = max(res,-1.0); return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep, in [-1,1] } #if HW_PERFORMANCE==0 #define AA 1 #else #define AA 2 #endif void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec3 tot = vec3(0.0); #if AA>1 for( int m=0; m<AA; m++ ) for( int n=0; n<AA; n++ ) { // pixel coordinates vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5; vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord)*AA + ivec2(m,n); // time sample float td = 0.5+0.5*sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1); float time = iTime - (1.0/60.0)*(td+float(m*AA+n))/float(AA*AA-1); #else // pixel coordinates vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; // pixel sample ivec2 samp = ivec2(fragCoord); // time sample float time = iTime; #endif // animate camera float an = 0.25*time + 6.283185*iMouse.x/iResolution.x; vec3 ro = vec3( 2.4*cos(an), 0.7, 2.4*sin(an) ); vec3 ta = vec3( 0.0, -0.15, 0.0 ); // camera matrix vec3 ww = normalize( ta - ro ); vec3 uu = normalize( cross(ww,vec3(0.2,1.0,0.0) ) ); vec3 vv = normalize( cross(uu,ww)); // animate point gPoint = -sin(time*0.8*vec3(1.0,1.1,1.2)+vec3(4.0,2.0,1.0)); // make box transparent bool showSurface = ((samp.x+samp.y)&1)==0; // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww ); // raycast const float tmax = 5.0; float t = 0.0; float m = -1.0; for( int i=0; i<256; i++ ) { vec3 pos = ro + t*rd; vec2 hm = map(pos,showSurface); m = hm.y; if( hm.x<0.0001 || t>tmax ) break; t += hm.x; } // shade background vec3 col = vec3(0.05)*(1.0-0.2*length(p)); // shade objects if( t<tmax ) { // geometry vec3 pos = ro + t*rd; vec3 nor = calcNormal(pos,showSurface); // color vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) ); // lighting col = vec3(0.0); { // key light vec3 lig = normalize(vec3(0.3,0.7,0.2)); float dif = clamp( dot(nor,lig), 0.0, 1.0 ); if( dif>0.001 ) dif *= calcSoftShadow(pos+nor*0.001,lig,showSurface); col += mate*vec3(1.0,0.9,0.8)*dif; } { // dome light float dif = 0.5 + 0.5*nor.y; col += mate*vec3(0.2,0.3,0.4)*dif; } } // gamma col = pow( col, vec3(0.4545) ); tot += col; #if AA>1 } tot /= float(AA*AA); #endif // cheap dithering tot += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0; fragColor = vec4( tot, 1.0 ); }
mit
[ 4694, 4756, 4816, 4816, 5189 ]
[ [ 1209, 1475, 1517, 1517, 1710 ], [ 1712, 1742, 1785, 1785, 2087 ], [ 2089, 2204, 2254, 2254, 2296 ], [ 2362, 2433, 2464, 2464, 2555 ], [ 2557, 2628, 2675, 2675, 2707 ], [ 2709, 2780, 2832, 2832, 2946 ], [ 2948, 3019, 3064, 3064, 3352 ], [ 3432, 3432, 3475, 3475, 4274 ], [ 4276, 4340, 4393, 4393, 4692 ], [ 4694, 4756, 4816, 4816, 5189 ] ]
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftShadow( vec3 ro, vec3 rd, bool showSurface ) {
float res = 1.0; const float tmax = 2.0; float t = 0.001; for( int i=0; i<64; i++ ) { float h = map(ro + t*rd, showSurface).x; res = min( res, 64.0*h/t ); t += clamp(h, 0.01,0.5); if( res<-1.0 || t>tmax ) break; } res = max(res,-1.0); return 0.25*(1.0+res)*(1.0+res)*(2.0-res); // smoothstep, in [-1,1] }
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm float calcSoftShadow( vec3 ro, vec3 rd, bool showSurface ) {
1
1
fd33zn
mrange
2021-08-14T13:49:10
// License CC0: Saturday Torus // Inspired by: https://www.istockphoto.com/photo/black-and-white-stripes-projection-on-torus-gm488221403-39181884 #define PI 3.141592654 #define TAU (2.0*PI) #define TIME iTime #define TTIME (TAU*TIME) #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PCOS(x) (0.5+0.5*cos(x)) // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm float rayTorus(vec3 ro, vec3 rd, vec2 tor) { float po = 1.0; float Ra2 = tor.x*tor.x; float ra2 = tor.y*tor.y; float m = dot(ro,ro); float n = dot(ro,rd); // bounding sphere { float h = n*n - m + (tor.x+tor.y)*(tor.x+tor.y); if(h<0.0) return -1.0; //float t = -n-sqrt(h); // could use this to compute intersections from ro+t*rd } // find quartic equation float k = (m - ra2 - Ra2)/2.0; float k3 = n; float k2 = n*n + Ra2*rd.z*rd.z + k; float k1 = k*n + Ra2*ro.z*rd.z; float k0 = k*k + Ra2*ro.z*ro.z - Ra2*ra2; #ifndef TORUS_REDUCE_PRECISION // prevent |c1| from being too close to zero if(abs(k3*(k3*k3 - k2) + k1) < 0.01) { po = -1.0; float tmp=k1; k1=k3; k3=tmp; k0 = 1.0/k0; k1 = k1*k0; k2 = k2*k0; k3 = k3*k0; } #endif float c2 = 2.0*k2 - 3.0*k3*k3; float c1 = k3*(k3*k3 - k2) + k1; float c0 = k3*(k3*(-3.0*k3*k3 + 4.0*k2) - 8.0*k1) + 4.0*k0; c2 /= 3.0; c1 *= 2.0; c0 /= 3.0; float Q = c2*c2 + c0; float R = 3.0*c0*c2 - c2*c2*c2 - c1*c1; float h = R*R - Q*Q*Q; float z = 0.0; if(h < 0.0) { // 4 intersections float sQ = sqrt(Q); z = 2.0*sQ*cos(acos(R/(sQ*Q)) / 3.0); } else { // 2 intersections float sQ = pow(sqrt(h) + abs(R), 1.0/3.0); z = sign(R)*abs(sQ + Q/sQ); } z = c2 - z; float d1 = z - 3.0*c2; float d2 = z*z - 3.0*c0; if(abs(d1) < 1.0e-4) { if(d2 < 0.0) return -1.0; d2 = sqrt(d2); } else { if(d1 < 0.0) return -1.0; d1 = sqrt(d1/2.0); d2 = c1/d1; } //---------------------------------- float result = 1e20; h = d1*d1 - z + d2; if(h > 0.0) { h = sqrt(h); float t1 = -d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1; float t2 = -d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2; if(t1 > 0.0) result=t1; if(t2 > 0.0) result=min(result,t2); } h = d1*d1 - z - d2; if(h > 0.0) { h = sqrt(h); float t1 = d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1; float t2 = d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2; if(t1 > 0.0) result=min(result,t1); if(t2 > 0.0) result=min(result,t2); } return result; } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm vec3 torusNormal(vec3 pos, vec2 tor) { return normalize(pos*(dot(pos,pos)- tor.y*tor.y - tor.x*tor.x*vec3(1.0,1.0,-1.0))); } // License: Unknown, author: Unknown, found: don't remember float tanh_approx(float x) { // Found this somewhere on the interwebs // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } vec3 color(vec2 p, vec2 q) { const float rdd = 2.0; vec3 ro = 1.*vec3(0., 0.75, -0.2); vec3 la = vec3(0.0, 0.0, 0.2); vec3 up = vec3(0.3, 0.0, 1.0); vec3 lp1 = ro; lp1.xy *= ROT(0.85); lp1.xz *= ROT(-0.5); vec3 ww = normalize(la - ro); vec3 uu = normalize(cross(up, ww)); vec3 vv = normalize(cross(ww,uu)); vec3 rd = normalize(p.x*uu + p.y*vv + rdd*ww); const vec2 tor = 0.55*vec2(1.0, 0.75); float td = rayTorus(ro, rd, tor); vec3 tpos = ro + rd*td; vec3 tnor = -torusNormal(tpos, tor); vec3 tref = reflect(rd, tnor); vec3 ldif1 = lp1 - tpos; float ldd1 = dot(ldif1, ldif1); float ldl1 = sqrt(ldd1); vec3 ld1 = ldif1/ldl1; vec3 sro = tpos+0.05*tnor; float sd = rayTorus(sro, ld1, tor); vec3 spos = sro+ld1*sd; vec3 snor = -torusNormal(spos, tor); float dif1 = max(dot(tnor, ld1), 0.0); float spe1 = pow(max(dot(tref, ld1), 0.0), 10.0); float r = length(tpos.xy); float a = atan(tpos.y, tpos.x)-PI*tpos.z/(r+0.5*abs(tpos.z))-TTIME/45.0; float s = mix(0.05, 0.5, tanh_approx(2.0*abs(td-0.75))); vec3 bcol0 = vec3(0.3); vec3 bcol1 = vec3(0.025); vec3 tcol = mix(bcol0, bcol1, smoothstep(-s, s, sin(9.0*a))); vec3 col = vec3(0.0); if (td > -1.0) { col += tcol*mix(0.2, 1.0, dif1/ldd1)+0.25*spe1; col *= sqrt(abs(dot(rd, tnor))); } if (sd < ldl1) { col *= mix(1.0, 0.0, pow(abs(dot(ld1, snor)), 3.0*tanh_approx(sd))); } return col; } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm vec3 postProcess(vec3 col, vec2 q) { col = clamp(col, 0.0, 1.0); col = pow(col, 1.0/vec3(2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); col *=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 q = fragCoord/iResolution.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; vec3 col = color(p, q); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 394, 510, 554, 554, 2612 ]
[ [ 394, 510, 554, 554, 2612 ], [ 2614, 2730, 2768, 2768, 2856 ], [ 2858, 2918, 2946, 3012, 3088 ], [ 3090, 3090, 3118, 3118, 4569 ], [ 4571, 4658, 4694, 4694, 4927 ], [ 4929, 4929, 4986, 4986, 5171 ] ]
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
float rayTorus(vec3 ro, vec3 rd, vec2 tor) {
float po = 1.0; float Ra2 = tor.x*tor.x; float ra2 = tor.y*tor.y; float m = dot(ro,ro); float n = dot(ro,rd); // bounding sphere { float h = n*n - m + (tor.x+tor.y)*(tor.x+tor.y); if(h<0.0) return -1.0; //float t = -n-sqrt(h); // could use this to compute intersections from ro+t*rd } // find quartic equation float k = (m - ra2 - Ra2)/2.0; float k3 = n; float k2 = n*n + Ra2*rd.z*rd.z + k; float k1 = k*n + Ra2*ro.z*rd.z; float k0 = k*k + Ra2*ro.z*ro.z - Ra2*ra2; #ifndef TORUS_REDUCE_PRECISION // prevent |c1| from being too close to zero if(abs(k3*(k3*k3 - k2) + k1) < 0.01) { po = -1.0; float tmp=k1; k1=k3; k3=tmp; k0 = 1.0/k0; k1 = k1*k0; k2 = k2*k0; k3 = k3*k0; } #endif float c2 = 2.0*k2 - 3.0*k3*k3; float c1 = k3*(k3*k3 - k2) + k1; float c0 = k3*(k3*(-3.0*k3*k3 + 4.0*k2) - 8.0*k1) + 4.0*k0; c2 /= 3.0; c1 *= 2.0; c0 /= 3.0; float Q = c2*c2 + c0; float R = 3.0*c0*c2 - c2*c2*c2 - c1*c1; float h = R*R - Q*Q*Q; float z = 0.0; if(h < 0.0) { // 4 intersections float sQ = sqrt(Q); z = 2.0*sQ*cos(acos(R/(sQ*Q)) / 3.0); } else { // 2 intersections float sQ = pow(sqrt(h) + abs(R), 1.0/3.0); z = sign(R)*abs(sQ + Q/sQ); } z = c2 - z; float d1 = z - 3.0*c2; float d2 = z*z - 3.0*c0; if(abs(d1) < 1.0e-4) { if(d2 < 0.0) return -1.0; d2 = sqrt(d2); } else { if(d1 < 0.0) return -1.0; d1 = sqrt(d1/2.0); d2 = c1/d1; } //---------------------------------- float result = 1e20; h = d1*d1 - z + d2; if(h > 0.0) { h = sqrt(h); float t1 = -d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1; float t2 = -d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2; if(t1 > 0.0) result=t1; if(t2 > 0.0) result=min(result,t2); } h = d1*d1 - z - d2; if(h > 0.0) { h = sqrt(h); float t1 = d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1; float t2 = d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2; if(t1 > 0.0) result=min(result,t1); if(t2 > 0.0) result=min(result,t2); } return result; }
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm float rayTorus(vec3 ro, vec3 rd, vec2 tor) {
2
2
fd33zn
mrange
2021-08-14T13:49:10
// License CC0: Saturday Torus // Inspired by: https://www.istockphoto.com/photo/black-and-white-stripes-projection-on-torus-gm488221403-39181884 #define PI 3.141592654 #define TAU (2.0*PI) #define TIME iTime #define TTIME (TAU*TIME) #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PCOS(x) (0.5+0.5*cos(x)) // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm float rayTorus(vec3 ro, vec3 rd, vec2 tor) { float po = 1.0; float Ra2 = tor.x*tor.x; float ra2 = tor.y*tor.y; float m = dot(ro,ro); float n = dot(ro,rd); // bounding sphere { float h = n*n - m + (tor.x+tor.y)*(tor.x+tor.y); if(h<0.0) return -1.0; //float t = -n-sqrt(h); // could use this to compute intersections from ro+t*rd } // find quartic equation float k = (m - ra2 - Ra2)/2.0; float k3 = n; float k2 = n*n + Ra2*rd.z*rd.z + k; float k1 = k*n + Ra2*ro.z*rd.z; float k0 = k*k + Ra2*ro.z*ro.z - Ra2*ra2; #ifndef TORUS_REDUCE_PRECISION // prevent |c1| from being too close to zero if(abs(k3*(k3*k3 - k2) + k1) < 0.01) { po = -1.0; float tmp=k1; k1=k3; k3=tmp; k0 = 1.0/k0; k1 = k1*k0; k2 = k2*k0; k3 = k3*k0; } #endif float c2 = 2.0*k2 - 3.0*k3*k3; float c1 = k3*(k3*k3 - k2) + k1; float c0 = k3*(k3*(-3.0*k3*k3 + 4.0*k2) - 8.0*k1) + 4.0*k0; c2 /= 3.0; c1 *= 2.0; c0 /= 3.0; float Q = c2*c2 + c0; float R = 3.0*c0*c2 - c2*c2*c2 - c1*c1; float h = R*R - Q*Q*Q; float z = 0.0; if(h < 0.0) { // 4 intersections float sQ = sqrt(Q); z = 2.0*sQ*cos(acos(R/(sQ*Q)) / 3.0); } else { // 2 intersections float sQ = pow(sqrt(h) + abs(R), 1.0/3.0); z = sign(R)*abs(sQ + Q/sQ); } z = c2 - z; float d1 = z - 3.0*c2; float d2 = z*z - 3.0*c0; if(abs(d1) < 1.0e-4) { if(d2 < 0.0) return -1.0; d2 = sqrt(d2); } else { if(d1 < 0.0) return -1.0; d1 = sqrt(d1/2.0); d2 = c1/d1; } //---------------------------------- float result = 1e20; h = d1*d1 - z + d2; if(h > 0.0) { h = sqrt(h); float t1 = -d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1; float t2 = -d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2; if(t1 > 0.0) result=t1; if(t2 > 0.0) result=min(result,t2); } h = d1*d1 - z - d2; if(h > 0.0) { h = sqrt(h); float t1 = d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1; float t2 = d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2; if(t1 > 0.0) result=min(result,t1); if(t2 > 0.0) result=min(result,t2); } return result; } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm vec3 torusNormal(vec3 pos, vec2 tor) { return normalize(pos*(dot(pos,pos)- tor.y*tor.y - tor.x*tor.x*vec3(1.0,1.0,-1.0))); } // License: Unknown, author: Unknown, found: don't remember float tanh_approx(float x) { // Found this somewhere on the interwebs // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } vec3 color(vec2 p, vec2 q) { const float rdd = 2.0; vec3 ro = 1.*vec3(0., 0.75, -0.2); vec3 la = vec3(0.0, 0.0, 0.2); vec3 up = vec3(0.3, 0.0, 1.0); vec3 lp1 = ro; lp1.xy *= ROT(0.85); lp1.xz *= ROT(-0.5); vec3 ww = normalize(la - ro); vec3 uu = normalize(cross(up, ww)); vec3 vv = normalize(cross(ww,uu)); vec3 rd = normalize(p.x*uu + p.y*vv + rdd*ww); const vec2 tor = 0.55*vec2(1.0, 0.75); float td = rayTorus(ro, rd, tor); vec3 tpos = ro + rd*td; vec3 tnor = -torusNormal(tpos, tor); vec3 tref = reflect(rd, tnor); vec3 ldif1 = lp1 - tpos; float ldd1 = dot(ldif1, ldif1); float ldl1 = sqrt(ldd1); vec3 ld1 = ldif1/ldl1; vec3 sro = tpos+0.05*tnor; float sd = rayTorus(sro, ld1, tor); vec3 spos = sro+ld1*sd; vec3 snor = -torusNormal(spos, tor); float dif1 = max(dot(tnor, ld1), 0.0); float spe1 = pow(max(dot(tref, ld1), 0.0), 10.0); float r = length(tpos.xy); float a = atan(tpos.y, tpos.x)-PI*tpos.z/(r+0.5*abs(tpos.z))-TTIME/45.0; float s = mix(0.05, 0.5, tanh_approx(2.0*abs(td-0.75))); vec3 bcol0 = vec3(0.3); vec3 bcol1 = vec3(0.025); vec3 tcol = mix(bcol0, bcol1, smoothstep(-s, s, sin(9.0*a))); vec3 col = vec3(0.0); if (td > -1.0) { col += tcol*mix(0.2, 1.0, dif1/ldd1)+0.25*spe1; col *= sqrt(abs(dot(rd, tnor))); } if (sd < ldl1) { col *= mix(1.0, 0.0, pow(abs(dot(ld1, snor)), 3.0*tanh_approx(sd))); } return col; } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm vec3 postProcess(vec3 col, vec2 q) { col = clamp(col, 0.0, 1.0); col = pow(col, 1.0/vec3(2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); col *=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 q = fragCoord/iResolution.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; vec3 col = color(p, q); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 2614, 2730, 2768, 2768, 2856 ]
[ [ 394, 510, 554, 554, 2612 ], [ 2614, 2730, 2768, 2768, 2856 ], [ 2858, 2918, 2946, 3012, 3088 ], [ 3090, 3090, 3118, 3118, 4569 ], [ 4571, 4658, 4694, 4694, 4927 ], [ 4929, 4929, 4986, 4986, 5171 ] ]
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
vec3 torusNormal(vec3 pos, vec2 tor) {
return normalize(pos*(dot(pos,pos)- tor.y*tor.y - tor.x*tor.x*vec3(1.0,1.0,-1.0))); }
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm vec3 torusNormal(vec3 pos, vec2 tor) {
2
2
fd33zn
mrange
2021-08-14T13:49:10
// License CC0: Saturday Torus // Inspired by: https://www.istockphoto.com/photo/black-and-white-stripes-projection-on-torus-gm488221403-39181884 #define PI 3.141592654 #define TAU (2.0*PI) #define TIME iTime #define TTIME (TAU*TIME) #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PCOS(x) (0.5+0.5*cos(x)) // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm float rayTorus(vec3 ro, vec3 rd, vec2 tor) { float po = 1.0; float Ra2 = tor.x*tor.x; float ra2 = tor.y*tor.y; float m = dot(ro,ro); float n = dot(ro,rd); // bounding sphere { float h = n*n - m + (tor.x+tor.y)*(tor.x+tor.y); if(h<0.0) return -1.0; //float t = -n-sqrt(h); // could use this to compute intersections from ro+t*rd } // find quartic equation float k = (m - ra2 - Ra2)/2.0; float k3 = n; float k2 = n*n + Ra2*rd.z*rd.z + k; float k1 = k*n + Ra2*ro.z*rd.z; float k0 = k*k + Ra2*ro.z*ro.z - Ra2*ra2; #ifndef TORUS_REDUCE_PRECISION // prevent |c1| from being too close to zero if(abs(k3*(k3*k3 - k2) + k1) < 0.01) { po = -1.0; float tmp=k1; k1=k3; k3=tmp; k0 = 1.0/k0; k1 = k1*k0; k2 = k2*k0; k3 = k3*k0; } #endif float c2 = 2.0*k2 - 3.0*k3*k3; float c1 = k3*(k3*k3 - k2) + k1; float c0 = k3*(k3*(-3.0*k3*k3 + 4.0*k2) - 8.0*k1) + 4.0*k0; c2 /= 3.0; c1 *= 2.0; c0 /= 3.0; float Q = c2*c2 + c0; float R = 3.0*c0*c2 - c2*c2*c2 - c1*c1; float h = R*R - Q*Q*Q; float z = 0.0; if(h < 0.0) { // 4 intersections float sQ = sqrt(Q); z = 2.0*sQ*cos(acos(R/(sQ*Q)) / 3.0); } else { // 2 intersections float sQ = pow(sqrt(h) + abs(R), 1.0/3.0); z = sign(R)*abs(sQ + Q/sQ); } z = c2 - z; float d1 = z - 3.0*c2; float d2 = z*z - 3.0*c0; if(abs(d1) < 1.0e-4) { if(d2 < 0.0) return -1.0; d2 = sqrt(d2); } else { if(d1 < 0.0) return -1.0; d1 = sqrt(d1/2.0); d2 = c1/d1; } //---------------------------------- float result = 1e20; h = d1*d1 - z + d2; if(h > 0.0) { h = sqrt(h); float t1 = -d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1; float t2 = -d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2; if(t1 > 0.0) result=t1; if(t2 > 0.0) result=min(result,t2); } h = d1*d1 - z - d2; if(h > 0.0) { h = sqrt(h); float t1 = d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1; float t2 = d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2; if(t1 > 0.0) result=min(result,t1); if(t2 > 0.0) result=min(result,t2); } return result; } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm vec3 torusNormal(vec3 pos, vec2 tor) { return normalize(pos*(dot(pos,pos)- tor.y*tor.y - tor.x*tor.x*vec3(1.0,1.0,-1.0))); } // License: Unknown, author: Unknown, found: don't remember float tanh_approx(float x) { // Found this somewhere on the interwebs // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } vec3 color(vec2 p, vec2 q) { const float rdd = 2.0; vec3 ro = 1.*vec3(0., 0.75, -0.2); vec3 la = vec3(0.0, 0.0, 0.2); vec3 up = vec3(0.3, 0.0, 1.0); vec3 lp1 = ro; lp1.xy *= ROT(0.85); lp1.xz *= ROT(-0.5); vec3 ww = normalize(la - ro); vec3 uu = normalize(cross(up, ww)); vec3 vv = normalize(cross(ww,uu)); vec3 rd = normalize(p.x*uu + p.y*vv + rdd*ww); const vec2 tor = 0.55*vec2(1.0, 0.75); float td = rayTorus(ro, rd, tor); vec3 tpos = ro + rd*td; vec3 tnor = -torusNormal(tpos, tor); vec3 tref = reflect(rd, tnor); vec3 ldif1 = lp1 - tpos; float ldd1 = dot(ldif1, ldif1); float ldl1 = sqrt(ldd1); vec3 ld1 = ldif1/ldl1; vec3 sro = tpos+0.05*tnor; float sd = rayTorus(sro, ld1, tor); vec3 spos = sro+ld1*sd; vec3 snor = -torusNormal(spos, tor); float dif1 = max(dot(tnor, ld1), 0.0); float spe1 = pow(max(dot(tref, ld1), 0.0), 10.0); float r = length(tpos.xy); float a = atan(tpos.y, tpos.x)-PI*tpos.z/(r+0.5*abs(tpos.z))-TTIME/45.0; float s = mix(0.05, 0.5, tanh_approx(2.0*abs(td-0.75))); vec3 bcol0 = vec3(0.3); vec3 bcol1 = vec3(0.025); vec3 tcol = mix(bcol0, bcol1, smoothstep(-s, s, sin(9.0*a))); vec3 col = vec3(0.0); if (td > -1.0) { col += tcol*mix(0.2, 1.0, dif1/ldd1)+0.25*spe1; col *= sqrt(abs(dot(rd, tnor))); } if (sd < ldl1) { col *= mix(1.0, 0.0, pow(abs(dot(ld1, snor)), 3.0*tanh_approx(sd))); } return col; } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm vec3 postProcess(vec3 col, vec2 q) { col = clamp(col, 0.0, 1.0); col = pow(col, 1.0/vec3(2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); col *=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 q = fragCoord/iResolution.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; vec3 col = color(p, q); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 2858, 2918, 2946, 3012, 3088 ]
[ [ 394, 510, 554, 554, 2612 ], [ 2614, 2730, 2768, 2768, 2856 ], [ 2858, 2918, 2946, 3012, 3088 ], [ 3090, 3090, 3118, 3118, 4569 ], [ 4571, 4658, 4694, 4694, 4927 ], [ 4929, 4929, 4986, 4986, 5171 ] ]
// License: Unknown, author: Unknown, found: don't remember
float tanh_approx(float x) {
float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); }
// License: Unknown, author: Unknown, found: don't remember float tanh_approx(float x) {
100
102
fd33zn
mrange
2021-08-14T13:49:10
// License CC0: Saturday Torus // Inspired by: https://www.istockphoto.com/photo/black-and-white-stripes-projection-on-torus-gm488221403-39181884 #define PI 3.141592654 #define TAU (2.0*PI) #define TIME iTime #define TTIME (TAU*TIME) #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PCOS(x) (0.5+0.5*cos(x)) // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm float rayTorus(vec3 ro, vec3 rd, vec2 tor) { float po = 1.0; float Ra2 = tor.x*tor.x; float ra2 = tor.y*tor.y; float m = dot(ro,ro); float n = dot(ro,rd); // bounding sphere { float h = n*n - m + (tor.x+tor.y)*(tor.x+tor.y); if(h<0.0) return -1.0; //float t = -n-sqrt(h); // could use this to compute intersections from ro+t*rd } // find quartic equation float k = (m - ra2 - Ra2)/2.0; float k3 = n; float k2 = n*n + Ra2*rd.z*rd.z + k; float k1 = k*n + Ra2*ro.z*rd.z; float k0 = k*k + Ra2*ro.z*ro.z - Ra2*ra2; #ifndef TORUS_REDUCE_PRECISION // prevent |c1| from being too close to zero if(abs(k3*(k3*k3 - k2) + k1) < 0.01) { po = -1.0; float tmp=k1; k1=k3; k3=tmp; k0 = 1.0/k0; k1 = k1*k0; k2 = k2*k0; k3 = k3*k0; } #endif float c2 = 2.0*k2 - 3.0*k3*k3; float c1 = k3*(k3*k3 - k2) + k1; float c0 = k3*(k3*(-3.0*k3*k3 + 4.0*k2) - 8.0*k1) + 4.0*k0; c2 /= 3.0; c1 *= 2.0; c0 /= 3.0; float Q = c2*c2 + c0; float R = 3.0*c0*c2 - c2*c2*c2 - c1*c1; float h = R*R - Q*Q*Q; float z = 0.0; if(h < 0.0) { // 4 intersections float sQ = sqrt(Q); z = 2.0*sQ*cos(acos(R/(sQ*Q)) / 3.0); } else { // 2 intersections float sQ = pow(sqrt(h) + abs(R), 1.0/3.0); z = sign(R)*abs(sQ + Q/sQ); } z = c2 - z; float d1 = z - 3.0*c2; float d2 = z*z - 3.0*c0; if(abs(d1) < 1.0e-4) { if(d2 < 0.0) return -1.0; d2 = sqrt(d2); } else { if(d1 < 0.0) return -1.0; d1 = sqrt(d1/2.0); d2 = c1/d1; } //---------------------------------- float result = 1e20; h = d1*d1 - z + d2; if(h > 0.0) { h = sqrt(h); float t1 = -d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1; float t2 = -d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2; if(t1 > 0.0) result=t1; if(t2 > 0.0) result=min(result,t2); } h = d1*d1 - z - d2; if(h > 0.0) { h = sqrt(h); float t1 = d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1; float t2 = d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2; if(t1 > 0.0) result=min(result,t1); if(t2 > 0.0) result=min(result,t2); } return result; } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm vec3 torusNormal(vec3 pos, vec2 tor) { return normalize(pos*(dot(pos,pos)- tor.y*tor.y - tor.x*tor.x*vec3(1.0,1.0,-1.0))); } // License: Unknown, author: Unknown, found: don't remember float tanh_approx(float x) { // Found this somewhere on the interwebs // return tanh(x); float x2 = x*x; return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); } vec3 color(vec2 p, vec2 q) { const float rdd = 2.0; vec3 ro = 1.*vec3(0., 0.75, -0.2); vec3 la = vec3(0.0, 0.0, 0.2); vec3 up = vec3(0.3, 0.0, 1.0); vec3 lp1 = ro; lp1.xy *= ROT(0.85); lp1.xz *= ROT(-0.5); vec3 ww = normalize(la - ro); vec3 uu = normalize(cross(up, ww)); vec3 vv = normalize(cross(ww,uu)); vec3 rd = normalize(p.x*uu + p.y*vv + rdd*ww); const vec2 tor = 0.55*vec2(1.0, 0.75); float td = rayTorus(ro, rd, tor); vec3 tpos = ro + rd*td; vec3 tnor = -torusNormal(tpos, tor); vec3 tref = reflect(rd, tnor); vec3 ldif1 = lp1 - tpos; float ldd1 = dot(ldif1, ldif1); float ldl1 = sqrt(ldd1); vec3 ld1 = ldif1/ldl1; vec3 sro = tpos+0.05*tnor; float sd = rayTorus(sro, ld1, tor); vec3 spos = sro+ld1*sd; vec3 snor = -torusNormal(spos, tor); float dif1 = max(dot(tnor, ld1), 0.0); float spe1 = pow(max(dot(tref, ld1), 0.0), 10.0); float r = length(tpos.xy); float a = atan(tpos.y, tpos.x)-PI*tpos.z/(r+0.5*abs(tpos.z))-TTIME/45.0; float s = mix(0.05, 0.5, tanh_approx(2.0*abs(td-0.75))); vec3 bcol0 = vec3(0.3); vec3 bcol1 = vec3(0.025); vec3 tcol = mix(bcol0, bcol1, smoothstep(-s, s, sin(9.0*a))); vec3 col = vec3(0.0); if (td > -1.0) { col += tcol*mix(0.2, 1.0, dif1/ldd1)+0.25*spe1; col *= sqrt(abs(dot(rd, tnor))); } if (sd < ldl1) { col *= mix(1.0, 0.0, pow(abs(dot(ld1, snor)), 3.0*tanh_approx(sd))); } return col; } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm vec3 postProcess(vec3 col, vec2 q) { col = clamp(col, 0.0, 1.0); col = pow(col, 1.0/vec3(2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); col *=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 q = fragCoord/iResolution.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; vec3 col = color(p, q); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 4571, 4658, 4694, 4694, 4927 ]
[ [ 394, 510, 554, 554, 2612 ], [ 2614, 2730, 2768, 2768, 2856 ], [ 2858, 2918, 2946, 3012, 3088 ], [ 3090, 3090, 3118, 3118, 4569 ], [ 4571, 4658, 4694, 4694, 4927 ], [ 4929, 4929, 4986, 4986, 5171 ] ]
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm
vec3 postProcess(vec3 col, vec2 q) {
col = clamp(col, 0.0, 1.0); col = pow(col, 1.0/vec3(2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); col *=0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; }
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm vec3 postProcess(vec3 col, vec2 q) {
6
43
sscGz4
unjello
2021-08-17T18:12:56
/// Exodus / Aberration Creations, a 4k intro /// 3rd place @ MAGFest 2019 /// License: CC0 /// /// Effects inspired by: /// Octahedral Voxel Tracing / fizzer: https://www.shadertoy.com/view/4lcfDB /// Swirly Strands / Plento: https://www.shadertoy.com/view/MtKfWy /// InFX.1 / patu: https://www.shadertoy.com/view/llSSRm /// /// Soundtrack: https://soundcloud.com/argasek/exodus-video /// Video: www.youtube.com/watch?v=HJA1xIevGl0 /// float MIN_DIST = 0.0; float MAX_DIST = 120.0; float EPSILON = 0.0001; vec3 K_a = vec3(1.); vec3 K_d = vec3(.6); vec3 K_s = vec3(0.5, 1.0, 0.5); vec3 lp = vec3(0.0, 1.0, -0.5); vec3 zero3 = vec3(0.); int MAX_STEPS = 80; int MODE_CROSS_CENTER = 1; int MODE_CROSS_JUMPING = 2; int MODE_METABALLS_CENTER = 3; int MODE_SWIRLS_CENTER = 5; int MODE_SWIRLS_SIDE = 6; // random took from // https://thebookofshaders.com/11/ float random (in vec2 st) { return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123); } float noise (vec2 st) { vec2 i = floor(st); vec2 f = fract(st); // Four corners in 2D of a tile float a = random(i); float b = random(i + vec2(1.0, 0.0)); float c = random(i + vec2(0.0, 1.0)); float d = random(i + vec2(1.0, 1.0)); // Smooth Interpolation // Cubic Hermine Curve. Same as SmoothStep() vec2 u = f*f*(3.0-2.0*f); // u = smoothstep(0.,1.,f); // Mix 4 coorners percentages return 0.4*(mix(a, b, u.x) + (c - a)* u.y * (1.0 - u.x) + (d - b) * u.x * u.y); } float sdfSphere(vec3 p, float r) { return length(p) - r; } // http://mercury.sexy/hg_sdf/ float sdfCubeCheap(vec3 p, vec3 size) { vec3 d = abs(p) - size; return max(d.x, max(d.y, d.z)); } float sdfOpUnion(float a, float b) { return min(a,b); } vec3 sdfOpMod(vec3 p, vec3 size) { vec3 halfsize = size * 0.5; p = mod(p + halfsize, size) - halfsize; return p; } vec3 opTwist( vec3 p, float r ) { float c = cos(r * p.y + r); float s = sin(r * p.y + r); mat2 m = mat2(c,-s,s,c); return vec3(m*p.xz,p.y); } float opBlob(float d1, float d2, float d3, float d4, float d5, float d6) { float k = 2.0; return -log(exp(-k*d1)+exp(-k*d2)+exp(-k*d3)+exp(-k*d4)+exp(-k*d5)+exp(-k*d6))/k; } // https://www1.udel.edu/biology/rosewc/kaap427627/notes/matrices_rotations.pdf mat3 fullRotate(vec3 theta) { float sx=sin(theta.x); float cx=cos(theta.x); float sy=sin(theta.y); float cy=cos(theta.y); float sz=sin(theta.z); float cz=cos(theta.z); return mat3( vec3(cy*cz, -cy*sz, sy), vec3(sx*sy*cz+cx*sz, -sx*sy*sz+cx*cz, -sx*cy), vec3(-cx*sy*cz+sx*sz, cx*sy*sz+sx*cz, cx*cy) ); } float sdf_metaballs(vec3 p) { float t = iTime / 4.; float p1 = sdfSphere(0.5*(p + vec3(cos(t*0.5),sin(t*0.3),cos(t))), 1.+0.5*cos(t*6.0)); float p2 = sdfSphere(2.0*(p + 3.0 * vec3(cos(t*1.1),cos(t*1.3),cos(t*1.7))), 3.+2.*sin(t))/2.0; float p3 = sdfSphere(2.0*(p + 5.0 * vec3(cos(t*0.7),cos(t*1.9),cos(t*2.3))), 3.)/2.0; float p4 = sdfSphere(2.0*(p + 3.0 * vec3(cos(t*0.3),cos(t*2.9),sin(t*1.1))), 3.+2.*sin(t))/2.0; float p5 = sdfSphere(2.0*(p + 6.0 * vec3(sin(t*1.3),sin(t*1.7),sin(t*0.7))), 3.0+1.5*cos(t))/2.0; float p6 = sdfSphere(2.0*(p + 3.0 * vec3(sin(t*2.3),sin(t*1.9),sin(t*2.9))), 3.0)/2.0; return opBlob(p1, p2, p3, p4, p5, p6); } float sdf_swirls(vec3 p, int mode) { p -= vec3(1.0, -0.25, 4.0); p *= fullRotate(vec3( 0.0, 0.0, mode == MODE_SWIRLS_CENTER ? p.z*0.06+0.2*sin(iTime) : p.z*.06+iTime*0.25 )); p.y += sin(p.z + iTime + p.x*1.0)*0.2; p.x += cos(p.y - p.z * 2.0 + iTime)*0.3; p = sdfOpMod(p, vec3(1.5, 1.5, 0.5+0.3*sin(iTime))); return sdfCubeCheap(p, vec3(0.033, 0.033, 2.0)); } float sdfCross(vec3 p, float w ) { float da = sdfCubeCheap(p.xyz,vec3(20., w, w)); float db = sdfCubeCheap(p.yzx,vec3(w, 20., w)); float dc = sdfCubeCheap(p.zxy,vec3(w, w , 20.)); return sdfOpUnion(sdfOpUnion(sdfOpUnion(db,dc), da), da); } float sdf_cross(vec3 p) { float t = iTime / 4.; float w = 1.7 - length(p) / 10.; p = opTwist(p, 0.1*sin(iTime*0.02))*fullRotate(vec3(iTime*0.01, 0.0, iTime*0.02)); p *= fullRotate(vec3(sin(iTime*0.1), 0.0, cos(iTime*0.02))); float res = sdfOpUnion( sdfCross(p, w), sdfCross(p * fullRotate(vec3(3.14/4.0, 0.0, 3.14/4.0)), w)); res = sdfOpUnion(res, sdfCross(p * fullRotate(vec3(3.14, 3.14/4.0, 3.14)), w)); return res; } vec2 render_raymarch(vec3 eye, vec3 dir, int mode) { float dist = MIN_DIST; float glow = 0.0; float minDist = MAX_DIST; for (int i = 0; i < MAX_STEPS; ++i) { vec3 v = eye + dist * dir; float step = 0.0; if (mode == MODE_METABALLS_CENTER) { step = sdf_metaballs(v); } if (mode == MODE_CROSS_CENTER || mode == MODE_CROSS_JUMPING) { step = sdf_cross(v); } if (mode == MODE_SWIRLS_CENTER || mode == MODE_SWIRLS_SIDE) { step = sdf_swirls(v, mode); } if (abs(step) < EPSILON) { return vec2(dist, glow); } dist += step; minDist = min(minDist, step * 4.); glow = pow( 1. / minDist, 0.4); if (dist >= MAX_DIST) { return vec2(dist, glow); } } return vec2(dist, glow); } vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) { vec2 xy = fragCoord - size / 2.0; float z = size.y / tan(radians(fieldOfView) / 2.0); return normalize(vec3(xy, -z)); } mat4 viewMatrix(vec3 eye, vec3 center, vec3 up) { // Based on gluLookAt man page vec3 f = normalize(center - eye); vec3 s = normalize(cross(f, up)); vec3 u = cross(s, f); return mat4( vec4(s, 0.0), vec4(u, 0.0), vec4(-f, 0.0), vec4(0.0, 0.0, 0.0, 1) ); } // http://learnwebgl.brown37.net/09_lights/lights_attenuation.html vec3 getSunLightColor(vec3 eye, vec3 dir, vec3 p, vec3 lp) { vec3 sun_pos = eye; vec3 L = sun_pos - p; float d = max(length(L), EPSILON); float atten = 1.0 / (1.0 + d*0.2 + d*d*0.1); vec3 c = (K_a + K_d + K_s)*atten; return c; } vec3 getFoggyColor(vec3 eye, float d, vec3 dir, vec3 lightPosition) { vec3 p = eye + d * dir; vec3 c = getSunLightColor(eye, dir, p, lightPosition); float fog = smoothstep(0.0, 0.68, d*0.005); return mix(c, zero3, fog); } vec4 effect_swirls(vec2 fragCoord, int mode) { vec2 uv = vec2(fragCoord.xy - 0.5*iResolution.xy)/iResolution.y; vec3 eye = vec3(mode == MODE_SWIRLS_CENTER ? 0.0 : 0.0, 0.0, (mode == MODE_SWIRLS_CENTER ? -17.0 : 2.0)*iTime); vec3 viewDir = rayDirection(mode == MODE_SWIRLS_CENTER ? 45.0 : 25.0, iResolution.xy, mode == MODE_SWIRLS_CENTER ? fragCoord : fragCoord.yx);//normalize(vec3(uv,2.0)); float d = render_raymarch(eye, viewDir, mode).x; if (d >= MAX_DIST) { return vec4(0.0); } else { return vec4(getFoggyColor(eye, d, viewDir, lp), 1.0); } } vec4 effect_raymarch(vec2 fragCoord, int mode) { float k = (iTime+150.)/ 2.0; vec3 eye = vec3( mode == MODE_METABALLS_CENTER ? 30. : sin(k) * 40., 1. , mode == MODE_METABALLS_CENTER ? -5.+sin(k) :cos(k) * -20.); vec3 viewDir = rayDirection(45.0, iResolution.xy, fragCoord); vec3 tt = vec3(10., mode == MODE_CROSS_CENTER ? 0. : 20. , 0.); if (mode == MODE_METABALLS_CENTER) { tt.x/=2.; tt.y = 2.5+sin(k); } vec2 uv = fragCoord.xy / iResolution.xy - 1.0; vec3 cc = vec3(1.0); if (mode == MODE_CROSS_CENTER) { uv.y += noise(uv)*sin(k*noise(uv*cos(k))); uv.x -= sin(k*noise(uv*sin(k))); float n = (ceil(uv.x * uv.y)); if (abs(n) < EPSILON) { tt.y += 2.0 * sin(iTime); cc = vec3(0.65); } } else { float n, n2, n3; float div = mode == MODE_CROSS_JUMPING ? 1. : -1.; n = (ceil(uv.x*2.5 + div*uv.y*2.5 + div*2.0 - div*sin(k+noise(uv)))); n2 = (ceil(uv.x*2.5 + div*uv.y*2.5 + 2.0*sin(k))); n3 = (ceil(uv.x*2.5 + div*uv.y*2.5 + div*2.0 - div*sin(k)*cos(k))); vec3 cc = vec3(1.0); if (abs(n) < EPSILON) { tt.y += 2.0 * sin(iTime); } if (abs(n2) < EPSILON) { tt.y += 3.0 * cos(iTime); } if (abs(n3) < EPSILON) { tt.y += 4.0 * cos(iTime); } cc = vec3(0.65); } vec3 up = vec3(0.2, 0.2, -1.); if (mode == MODE_CROSS_JUMPING) { up.z = -50.*cos(k); } else if (mode == MODE_CROSS_CENTER) { up.y = sin(k*5.); up.z = cos(k*5.); } mat4 viewToWorld = viewMatrix(eye, tt, up); vec3 worldDir = (viewToWorld * vec4(viewDir, 0.0)).xyz; vec2 dd = render_raymarch(eye, worldDir, mode); float d = dd.x; float glow = dd.y; vec3 c = zero3; if (d >= MAX_DIST) { float g = glow*glow; c += K_s*glow*0.2 + K_d*g; } else { c = getFoggyColor(eye, d, worldDir, mode == 4 ? vec3(0.0, -10., -15.) : lp); } return vec4(c*cc, 1.0); } vec4 intro(vec2 fragCoord) { if (iTime <= 4.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 9.6) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 16.1) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 19.1) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 25.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 28.7) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 33.7) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 38.3) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 43.1) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 47.9) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 57.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 76.8) { return effect_raymarch(fragCoord, MODE_CROSS_CENTER); } else { return effect_swirls(fragCoord, MODE_SWIRLS_CENTER); } } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (fragCoord.xy - iResolution.xy)/ iResolution.xy; fragColor = intro(fragCoord); // Vignette fragColor.rgb *= 1. - (pow(abs(uv.x), 5.) + pow(abs(uv.y), 5.)) * .4; // Tonemapping fragColor.rgb /= (fragColor.rgb + vec3(.5)) * .7; // Gamma fragColor.rgb = pow(fragColor.rgb, vec3(1. / 2.2)); if (iTime > 94.0) { fragColor /= (-93.9+iTime)*6.; } }
cc0-1.0
[ 800, 856, 883, 883, 958 ]
[ [ 800, 856, 883, 883, 958 ], [ 960, 960, 983, 983, 1506 ], [ 1508, 1508, 1542, 1542, 1570 ], [ 1572, 1603, 1642, 1642, 1708 ], [ 1710, 1710, 1746, 1746, 1766 ], [ 1768, 1768, 1802, 1802, 1894 ], [ 1896, 1896, 1929, 1929, 2057 ], [ 2059, 2059, 2133, 2133, 2237 ], [ 2240, 2320, 2349, 2349, 2682 ], [ 2684, 2684, 2713, 2713, 3355 ], [ 3357, 3357, 3393, 3393, 3765 ], [ 3767, 3767, 3801, 3801, 4014 ], [ 4017, 4017, 4042, 4042, 4481 ], [ 4484, 4484, 4536, 4536, 5364 ], [ 5367, 5367, 5432, 5432, 5564 ], [ 5566, 5566, 5615, 5650, 5876 ], [ 5878, 5945, 6005, 6005, 6192 ], [ 6194, 6194, 6263, 6263, 6453 ], [ 6458, 6458, 6504, 6504, 7085 ], [ 7087, 7087, 7135, 7135, 9229 ], [ 9232, 9232, 9260, 9260, 10458 ], [ 10461, 10461, 10518, 10518, 10925 ] ]
// random took from // https://thebookofshaders.com/11/
float random (in vec2 st) {
return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123); }
// random took from // https://thebookofshaders.com/11/ float random (in vec2 st) {
4
113
sscGz4
unjello
2021-08-17T18:12:56
/// Exodus / Aberration Creations, a 4k intro /// 3rd place @ MAGFest 2019 /// License: CC0 /// /// Effects inspired by: /// Octahedral Voxel Tracing / fizzer: https://www.shadertoy.com/view/4lcfDB /// Swirly Strands / Plento: https://www.shadertoy.com/view/MtKfWy /// InFX.1 / patu: https://www.shadertoy.com/view/llSSRm /// /// Soundtrack: https://soundcloud.com/argasek/exodus-video /// Video: www.youtube.com/watch?v=HJA1xIevGl0 /// float MIN_DIST = 0.0; float MAX_DIST = 120.0; float EPSILON = 0.0001; vec3 K_a = vec3(1.); vec3 K_d = vec3(.6); vec3 K_s = vec3(0.5, 1.0, 0.5); vec3 lp = vec3(0.0, 1.0, -0.5); vec3 zero3 = vec3(0.); int MAX_STEPS = 80; int MODE_CROSS_CENTER = 1; int MODE_CROSS_JUMPING = 2; int MODE_METABALLS_CENTER = 3; int MODE_SWIRLS_CENTER = 5; int MODE_SWIRLS_SIDE = 6; // random took from // https://thebookofshaders.com/11/ float random (in vec2 st) { return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123); } float noise (vec2 st) { vec2 i = floor(st); vec2 f = fract(st); // Four corners in 2D of a tile float a = random(i); float b = random(i + vec2(1.0, 0.0)); float c = random(i + vec2(0.0, 1.0)); float d = random(i + vec2(1.0, 1.0)); // Smooth Interpolation // Cubic Hermine Curve. Same as SmoothStep() vec2 u = f*f*(3.0-2.0*f); // u = smoothstep(0.,1.,f); // Mix 4 coorners percentages return 0.4*(mix(a, b, u.x) + (c - a)* u.y * (1.0 - u.x) + (d - b) * u.x * u.y); } float sdfSphere(vec3 p, float r) { return length(p) - r; } // http://mercury.sexy/hg_sdf/ float sdfCubeCheap(vec3 p, vec3 size) { vec3 d = abs(p) - size; return max(d.x, max(d.y, d.z)); } float sdfOpUnion(float a, float b) { return min(a,b); } vec3 sdfOpMod(vec3 p, vec3 size) { vec3 halfsize = size * 0.5; p = mod(p + halfsize, size) - halfsize; return p; } vec3 opTwist( vec3 p, float r ) { float c = cos(r * p.y + r); float s = sin(r * p.y + r); mat2 m = mat2(c,-s,s,c); return vec3(m*p.xz,p.y); } float opBlob(float d1, float d2, float d3, float d4, float d5, float d6) { float k = 2.0; return -log(exp(-k*d1)+exp(-k*d2)+exp(-k*d3)+exp(-k*d4)+exp(-k*d5)+exp(-k*d6))/k; } // https://www1.udel.edu/biology/rosewc/kaap427627/notes/matrices_rotations.pdf mat3 fullRotate(vec3 theta) { float sx=sin(theta.x); float cx=cos(theta.x); float sy=sin(theta.y); float cy=cos(theta.y); float sz=sin(theta.z); float cz=cos(theta.z); return mat3( vec3(cy*cz, -cy*sz, sy), vec3(sx*sy*cz+cx*sz, -sx*sy*sz+cx*cz, -sx*cy), vec3(-cx*sy*cz+sx*sz, cx*sy*sz+sx*cz, cx*cy) ); } float sdf_metaballs(vec3 p) { float t = iTime / 4.; float p1 = sdfSphere(0.5*(p + vec3(cos(t*0.5),sin(t*0.3),cos(t))), 1.+0.5*cos(t*6.0)); float p2 = sdfSphere(2.0*(p + 3.0 * vec3(cos(t*1.1),cos(t*1.3),cos(t*1.7))), 3.+2.*sin(t))/2.0; float p3 = sdfSphere(2.0*(p + 5.0 * vec3(cos(t*0.7),cos(t*1.9),cos(t*2.3))), 3.)/2.0; float p4 = sdfSphere(2.0*(p + 3.0 * vec3(cos(t*0.3),cos(t*2.9),sin(t*1.1))), 3.+2.*sin(t))/2.0; float p5 = sdfSphere(2.0*(p + 6.0 * vec3(sin(t*1.3),sin(t*1.7),sin(t*0.7))), 3.0+1.5*cos(t))/2.0; float p6 = sdfSphere(2.0*(p + 3.0 * vec3(sin(t*2.3),sin(t*1.9),sin(t*2.9))), 3.0)/2.0; return opBlob(p1, p2, p3, p4, p5, p6); } float sdf_swirls(vec3 p, int mode) { p -= vec3(1.0, -0.25, 4.0); p *= fullRotate(vec3( 0.0, 0.0, mode == MODE_SWIRLS_CENTER ? p.z*0.06+0.2*sin(iTime) : p.z*.06+iTime*0.25 )); p.y += sin(p.z + iTime + p.x*1.0)*0.2; p.x += cos(p.y - p.z * 2.0 + iTime)*0.3; p = sdfOpMod(p, vec3(1.5, 1.5, 0.5+0.3*sin(iTime))); return sdfCubeCheap(p, vec3(0.033, 0.033, 2.0)); } float sdfCross(vec3 p, float w ) { float da = sdfCubeCheap(p.xyz,vec3(20., w, w)); float db = sdfCubeCheap(p.yzx,vec3(w, 20., w)); float dc = sdfCubeCheap(p.zxy,vec3(w, w , 20.)); return sdfOpUnion(sdfOpUnion(sdfOpUnion(db,dc), da), da); } float sdf_cross(vec3 p) { float t = iTime / 4.; float w = 1.7 - length(p) / 10.; p = opTwist(p, 0.1*sin(iTime*0.02))*fullRotate(vec3(iTime*0.01, 0.0, iTime*0.02)); p *= fullRotate(vec3(sin(iTime*0.1), 0.0, cos(iTime*0.02))); float res = sdfOpUnion( sdfCross(p, w), sdfCross(p * fullRotate(vec3(3.14/4.0, 0.0, 3.14/4.0)), w)); res = sdfOpUnion(res, sdfCross(p * fullRotate(vec3(3.14, 3.14/4.0, 3.14)), w)); return res; } vec2 render_raymarch(vec3 eye, vec3 dir, int mode) { float dist = MIN_DIST; float glow = 0.0; float minDist = MAX_DIST; for (int i = 0; i < MAX_STEPS; ++i) { vec3 v = eye + dist * dir; float step = 0.0; if (mode == MODE_METABALLS_CENTER) { step = sdf_metaballs(v); } if (mode == MODE_CROSS_CENTER || mode == MODE_CROSS_JUMPING) { step = sdf_cross(v); } if (mode == MODE_SWIRLS_CENTER || mode == MODE_SWIRLS_SIDE) { step = sdf_swirls(v, mode); } if (abs(step) < EPSILON) { return vec2(dist, glow); } dist += step; minDist = min(minDist, step * 4.); glow = pow( 1. / minDist, 0.4); if (dist >= MAX_DIST) { return vec2(dist, glow); } } return vec2(dist, glow); } vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) { vec2 xy = fragCoord - size / 2.0; float z = size.y / tan(radians(fieldOfView) / 2.0); return normalize(vec3(xy, -z)); } mat4 viewMatrix(vec3 eye, vec3 center, vec3 up) { // Based on gluLookAt man page vec3 f = normalize(center - eye); vec3 s = normalize(cross(f, up)); vec3 u = cross(s, f); return mat4( vec4(s, 0.0), vec4(u, 0.0), vec4(-f, 0.0), vec4(0.0, 0.0, 0.0, 1) ); } // http://learnwebgl.brown37.net/09_lights/lights_attenuation.html vec3 getSunLightColor(vec3 eye, vec3 dir, vec3 p, vec3 lp) { vec3 sun_pos = eye; vec3 L = sun_pos - p; float d = max(length(L), EPSILON); float atten = 1.0 / (1.0 + d*0.2 + d*d*0.1); vec3 c = (K_a + K_d + K_s)*atten; return c; } vec3 getFoggyColor(vec3 eye, float d, vec3 dir, vec3 lightPosition) { vec3 p = eye + d * dir; vec3 c = getSunLightColor(eye, dir, p, lightPosition); float fog = smoothstep(0.0, 0.68, d*0.005); return mix(c, zero3, fog); } vec4 effect_swirls(vec2 fragCoord, int mode) { vec2 uv = vec2(fragCoord.xy - 0.5*iResolution.xy)/iResolution.y; vec3 eye = vec3(mode == MODE_SWIRLS_CENTER ? 0.0 : 0.0, 0.0, (mode == MODE_SWIRLS_CENTER ? -17.0 : 2.0)*iTime); vec3 viewDir = rayDirection(mode == MODE_SWIRLS_CENTER ? 45.0 : 25.0, iResolution.xy, mode == MODE_SWIRLS_CENTER ? fragCoord : fragCoord.yx);//normalize(vec3(uv,2.0)); float d = render_raymarch(eye, viewDir, mode).x; if (d >= MAX_DIST) { return vec4(0.0); } else { return vec4(getFoggyColor(eye, d, viewDir, lp), 1.0); } } vec4 effect_raymarch(vec2 fragCoord, int mode) { float k = (iTime+150.)/ 2.0; vec3 eye = vec3( mode == MODE_METABALLS_CENTER ? 30. : sin(k) * 40., 1. , mode == MODE_METABALLS_CENTER ? -5.+sin(k) :cos(k) * -20.); vec3 viewDir = rayDirection(45.0, iResolution.xy, fragCoord); vec3 tt = vec3(10., mode == MODE_CROSS_CENTER ? 0. : 20. , 0.); if (mode == MODE_METABALLS_CENTER) { tt.x/=2.; tt.y = 2.5+sin(k); } vec2 uv = fragCoord.xy / iResolution.xy - 1.0; vec3 cc = vec3(1.0); if (mode == MODE_CROSS_CENTER) { uv.y += noise(uv)*sin(k*noise(uv*cos(k))); uv.x -= sin(k*noise(uv*sin(k))); float n = (ceil(uv.x * uv.y)); if (abs(n) < EPSILON) { tt.y += 2.0 * sin(iTime); cc = vec3(0.65); } } else { float n, n2, n3; float div = mode == MODE_CROSS_JUMPING ? 1. : -1.; n = (ceil(uv.x*2.5 + div*uv.y*2.5 + div*2.0 - div*sin(k+noise(uv)))); n2 = (ceil(uv.x*2.5 + div*uv.y*2.5 + 2.0*sin(k))); n3 = (ceil(uv.x*2.5 + div*uv.y*2.5 + div*2.0 - div*sin(k)*cos(k))); vec3 cc = vec3(1.0); if (abs(n) < EPSILON) { tt.y += 2.0 * sin(iTime); } if (abs(n2) < EPSILON) { tt.y += 3.0 * cos(iTime); } if (abs(n3) < EPSILON) { tt.y += 4.0 * cos(iTime); } cc = vec3(0.65); } vec3 up = vec3(0.2, 0.2, -1.); if (mode == MODE_CROSS_JUMPING) { up.z = -50.*cos(k); } else if (mode == MODE_CROSS_CENTER) { up.y = sin(k*5.); up.z = cos(k*5.); } mat4 viewToWorld = viewMatrix(eye, tt, up); vec3 worldDir = (viewToWorld * vec4(viewDir, 0.0)).xyz; vec2 dd = render_raymarch(eye, worldDir, mode); float d = dd.x; float glow = dd.y; vec3 c = zero3; if (d >= MAX_DIST) { float g = glow*glow; c += K_s*glow*0.2 + K_d*g; } else { c = getFoggyColor(eye, d, worldDir, mode == 4 ? vec3(0.0, -10., -15.) : lp); } return vec4(c*cc, 1.0); } vec4 intro(vec2 fragCoord) { if (iTime <= 4.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 9.6) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 16.1) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 19.1) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 25.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 28.7) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 33.7) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 38.3) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 43.1) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 47.9) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 57.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 76.8) { return effect_raymarch(fragCoord, MODE_CROSS_CENTER); } else { return effect_swirls(fragCoord, MODE_SWIRLS_CENTER); } } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (fragCoord.xy - iResolution.xy)/ iResolution.xy; fragColor = intro(fragCoord); // Vignette fragColor.rgb *= 1. - (pow(abs(uv.x), 5.) + pow(abs(uv.y), 5.)) * .4; // Tonemapping fragColor.rgb /= (fragColor.rgb + vec3(.5)) * .7; // Gamma fragColor.rgb = pow(fragColor.rgb, vec3(1. / 2.2)); if (iTime > 94.0) { fragColor /= (-93.9+iTime)*6.; } }
cc0-1.0
[ 1572, 1603, 1642, 1642, 1708 ]
[ [ 800, 856, 883, 883, 958 ], [ 960, 960, 983, 983, 1506 ], [ 1508, 1508, 1542, 1542, 1570 ], [ 1572, 1603, 1642, 1642, 1708 ], [ 1710, 1710, 1746, 1746, 1766 ], [ 1768, 1768, 1802, 1802, 1894 ], [ 1896, 1896, 1929, 1929, 2057 ], [ 2059, 2059, 2133, 2133, 2237 ], [ 2240, 2320, 2349, 2349, 2682 ], [ 2684, 2684, 2713, 2713, 3355 ], [ 3357, 3357, 3393, 3393, 3765 ], [ 3767, 3767, 3801, 3801, 4014 ], [ 4017, 4017, 4042, 4042, 4481 ], [ 4484, 4484, 4536, 4536, 5364 ], [ 5367, 5367, 5432, 5432, 5564 ], [ 5566, 5566, 5615, 5650, 5876 ], [ 5878, 5945, 6005, 6005, 6192 ], [ 6194, 6194, 6263, 6263, 6453 ], [ 6458, 6458, 6504, 6504, 7085 ], [ 7087, 7087, 7135, 7135, 9229 ], [ 9232, 9232, 9260, 9260, 10458 ], [ 10461, 10461, 10518, 10518, 10925 ] ]
// http://mercury.sexy/hg_sdf/
float sdfCubeCheap(vec3 p, vec3 size) {
vec3 d = abs(p) - size; return max(d.x, max(d.y, d.z)); }
// http://mercury.sexy/hg_sdf/ float sdfCubeCheap(vec3 p, vec3 size) {
1
1
sscGz4
unjello
2021-08-17T18:12:56
/// Exodus / Aberration Creations, a 4k intro /// 3rd place @ MAGFest 2019 /// License: CC0 /// /// Effects inspired by: /// Octahedral Voxel Tracing / fizzer: https://www.shadertoy.com/view/4lcfDB /// Swirly Strands / Plento: https://www.shadertoy.com/view/MtKfWy /// InFX.1 / patu: https://www.shadertoy.com/view/llSSRm /// /// Soundtrack: https://soundcloud.com/argasek/exodus-video /// Video: www.youtube.com/watch?v=HJA1xIevGl0 /// float MIN_DIST = 0.0; float MAX_DIST = 120.0; float EPSILON = 0.0001; vec3 K_a = vec3(1.); vec3 K_d = vec3(.6); vec3 K_s = vec3(0.5, 1.0, 0.5); vec3 lp = vec3(0.0, 1.0, -0.5); vec3 zero3 = vec3(0.); int MAX_STEPS = 80; int MODE_CROSS_CENTER = 1; int MODE_CROSS_JUMPING = 2; int MODE_METABALLS_CENTER = 3; int MODE_SWIRLS_CENTER = 5; int MODE_SWIRLS_SIDE = 6; // random took from // https://thebookofshaders.com/11/ float random (in vec2 st) { return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123); } float noise (vec2 st) { vec2 i = floor(st); vec2 f = fract(st); // Four corners in 2D of a tile float a = random(i); float b = random(i + vec2(1.0, 0.0)); float c = random(i + vec2(0.0, 1.0)); float d = random(i + vec2(1.0, 1.0)); // Smooth Interpolation // Cubic Hermine Curve. Same as SmoothStep() vec2 u = f*f*(3.0-2.0*f); // u = smoothstep(0.,1.,f); // Mix 4 coorners percentages return 0.4*(mix(a, b, u.x) + (c - a)* u.y * (1.0 - u.x) + (d - b) * u.x * u.y); } float sdfSphere(vec3 p, float r) { return length(p) - r; } // http://mercury.sexy/hg_sdf/ float sdfCubeCheap(vec3 p, vec3 size) { vec3 d = abs(p) - size; return max(d.x, max(d.y, d.z)); } float sdfOpUnion(float a, float b) { return min(a,b); } vec3 sdfOpMod(vec3 p, vec3 size) { vec3 halfsize = size * 0.5; p = mod(p + halfsize, size) - halfsize; return p; } vec3 opTwist( vec3 p, float r ) { float c = cos(r * p.y + r); float s = sin(r * p.y + r); mat2 m = mat2(c,-s,s,c); return vec3(m*p.xz,p.y); } float opBlob(float d1, float d2, float d3, float d4, float d5, float d6) { float k = 2.0; return -log(exp(-k*d1)+exp(-k*d2)+exp(-k*d3)+exp(-k*d4)+exp(-k*d5)+exp(-k*d6))/k; } // https://www1.udel.edu/biology/rosewc/kaap427627/notes/matrices_rotations.pdf mat3 fullRotate(vec3 theta) { float sx=sin(theta.x); float cx=cos(theta.x); float sy=sin(theta.y); float cy=cos(theta.y); float sz=sin(theta.z); float cz=cos(theta.z); return mat3( vec3(cy*cz, -cy*sz, sy), vec3(sx*sy*cz+cx*sz, -sx*sy*sz+cx*cz, -sx*cy), vec3(-cx*sy*cz+sx*sz, cx*sy*sz+sx*cz, cx*cy) ); } float sdf_metaballs(vec3 p) { float t = iTime / 4.; float p1 = sdfSphere(0.5*(p + vec3(cos(t*0.5),sin(t*0.3),cos(t))), 1.+0.5*cos(t*6.0)); float p2 = sdfSphere(2.0*(p + 3.0 * vec3(cos(t*1.1),cos(t*1.3),cos(t*1.7))), 3.+2.*sin(t))/2.0; float p3 = sdfSphere(2.0*(p + 5.0 * vec3(cos(t*0.7),cos(t*1.9),cos(t*2.3))), 3.)/2.0; float p4 = sdfSphere(2.0*(p + 3.0 * vec3(cos(t*0.3),cos(t*2.9),sin(t*1.1))), 3.+2.*sin(t))/2.0; float p5 = sdfSphere(2.0*(p + 6.0 * vec3(sin(t*1.3),sin(t*1.7),sin(t*0.7))), 3.0+1.5*cos(t))/2.0; float p6 = sdfSphere(2.0*(p + 3.0 * vec3(sin(t*2.3),sin(t*1.9),sin(t*2.9))), 3.0)/2.0; return opBlob(p1, p2, p3, p4, p5, p6); } float sdf_swirls(vec3 p, int mode) { p -= vec3(1.0, -0.25, 4.0); p *= fullRotate(vec3( 0.0, 0.0, mode == MODE_SWIRLS_CENTER ? p.z*0.06+0.2*sin(iTime) : p.z*.06+iTime*0.25 )); p.y += sin(p.z + iTime + p.x*1.0)*0.2; p.x += cos(p.y - p.z * 2.0 + iTime)*0.3; p = sdfOpMod(p, vec3(1.5, 1.5, 0.5+0.3*sin(iTime))); return sdfCubeCheap(p, vec3(0.033, 0.033, 2.0)); } float sdfCross(vec3 p, float w ) { float da = sdfCubeCheap(p.xyz,vec3(20., w, w)); float db = sdfCubeCheap(p.yzx,vec3(w, 20., w)); float dc = sdfCubeCheap(p.zxy,vec3(w, w , 20.)); return sdfOpUnion(sdfOpUnion(sdfOpUnion(db,dc), da), da); } float sdf_cross(vec3 p) { float t = iTime / 4.; float w = 1.7 - length(p) / 10.; p = opTwist(p, 0.1*sin(iTime*0.02))*fullRotate(vec3(iTime*0.01, 0.0, iTime*0.02)); p *= fullRotate(vec3(sin(iTime*0.1), 0.0, cos(iTime*0.02))); float res = sdfOpUnion( sdfCross(p, w), sdfCross(p * fullRotate(vec3(3.14/4.0, 0.0, 3.14/4.0)), w)); res = sdfOpUnion(res, sdfCross(p * fullRotate(vec3(3.14, 3.14/4.0, 3.14)), w)); return res; } vec2 render_raymarch(vec3 eye, vec3 dir, int mode) { float dist = MIN_DIST; float glow = 0.0; float minDist = MAX_DIST; for (int i = 0; i < MAX_STEPS; ++i) { vec3 v = eye + dist * dir; float step = 0.0; if (mode == MODE_METABALLS_CENTER) { step = sdf_metaballs(v); } if (mode == MODE_CROSS_CENTER || mode == MODE_CROSS_JUMPING) { step = sdf_cross(v); } if (mode == MODE_SWIRLS_CENTER || mode == MODE_SWIRLS_SIDE) { step = sdf_swirls(v, mode); } if (abs(step) < EPSILON) { return vec2(dist, glow); } dist += step; minDist = min(minDist, step * 4.); glow = pow( 1. / minDist, 0.4); if (dist >= MAX_DIST) { return vec2(dist, glow); } } return vec2(dist, glow); } vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) { vec2 xy = fragCoord - size / 2.0; float z = size.y / tan(radians(fieldOfView) / 2.0); return normalize(vec3(xy, -z)); } mat4 viewMatrix(vec3 eye, vec3 center, vec3 up) { // Based on gluLookAt man page vec3 f = normalize(center - eye); vec3 s = normalize(cross(f, up)); vec3 u = cross(s, f); return mat4( vec4(s, 0.0), vec4(u, 0.0), vec4(-f, 0.0), vec4(0.0, 0.0, 0.0, 1) ); } // http://learnwebgl.brown37.net/09_lights/lights_attenuation.html vec3 getSunLightColor(vec3 eye, vec3 dir, vec3 p, vec3 lp) { vec3 sun_pos = eye; vec3 L = sun_pos - p; float d = max(length(L), EPSILON); float atten = 1.0 / (1.0 + d*0.2 + d*d*0.1); vec3 c = (K_a + K_d + K_s)*atten; return c; } vec3 getFoggyColor(vec3 eye, float d, vec3 dir, vec3 lightPosition) { vec3 p = eye + d * dir; vec3 c = getSunLightColor(eye, dir, p, lightPosition); float fog = smoothstep(0.0, 0.68, d*0.005); return mix(c, zero3, fog); } vec4 effect_swirls(vec2 fragCoord, int mode) { vec2 uv = vec2(fragCoord.xy - 0.5*iResolution.xy)/iResolution.y; vec3 eye = vec3(mode == MODE_SWIRLS_CENTER ? 0.0 : 0.0, 0.0, (mode == MODE_SWIRLS_CENTER ? -17.0 : 2.0)*iTime); vec3 viewDir = rayDirection(mode == MODE_SWIRLS_CENTER ? 45.0 : 25.0, iResolution.xy, mode == MODE_SWIRLS_CENTER ? fragCoord : fragCoord.yx);//normalize(vec3(uv,2.0)); float d = render_raymarch(eye, viewDir, mode).x; if (d >= MAX_DIST) { return vec4(0.0); } else { return vec4(getFoggyColor(eye, d, viewDir, lp), 1.0); } } vec4 effect_raymarch(vec2 fragCoord, int mode) { float k = (iTime+150.)/ 2.0; vec3 eye = vec3( mode == MODE_METABALLS_CENTER ? 30. : sin(k) * 40., 1. , mode == MODE_METABALLS_CENTER ? -5.+sin(k) :cos(k) * -20.); vec3 viewDir = rayDirection(45.0, iResolution.xy, fragCoord); vec3 tt = vec3(10., mode == MODE_CROSS_CENTER ? 0. : 20. , 0.); if (mode == MODE_METABALLS_CENTER) { tt.x/=2.; tt.y = 2.5+sin(k); } vec2 uv = fragCoord.xy / iResolution.xy - 1.0; vec3 cc = vec3(1.0); if (mode == MODE_CROSS_CENTER) { uv.y += noise(uv)*sin(k*noise(uv*cos(k))); uv.x -= sin(k*noise(uv*sin(k))); float n = (ceil(uv.x * uv.y)); if (abs(n) < EPSILON) { tt.y += 2.0 * sin(iTime); cc = vec3(0.65); } } else { float n, n2, n3; float div = mode == MODE_CROSS_JUMPING ? 1. : -1.; n = (ceil(uv.x*2.5 + div*uv.y*2.5 + div*2.0 - div*sin(k+noise(uv)))); n2 = (ceil(uv.x*2.5 + div*uv.y*2.5 + 2.0*sin(k))); n3 = (ceil(uv.x*2.5 + div*uv.y*2.5 + div*2.0 - div*sin(k)*cos(k))); vec3 cc = vec3(1.0); if (abs(n) < EPSILON) { tt.y += 2.0 * sin(iTime); } if (abs(n2) < EPSILON) { tt.y += 3.0 * cos(iTime); } if (abs(n3) < EPSILON) { tt.y += 4.0 * cos(iTime); } cc = vec3(0.65); } vec3 up = vec3(0.2, 0.2, -1.); if (mode == MODE_CROSS_JUMPING) { up.z = -50.*cos(k); } else if (mode == MODE_CROSS_CENTER) { up.y = sin(k*5.); up.z = cos(k*5.); } mat4 viewToWorld = viewMatrix(eye, tt, up); vec3 worldDir = (viewToWorld * vec4(viewDir, 0.0)).xyz; vec2 dd = render_raymarch(eye, worldDir, mode); float d = dd.x; float glow = dd.y; vec3 c = zero3; if (d >= MAX_DIST) { float g = glow*glow; c += K_s*glow*0.2 + K_d*g; } else { c = getFoggyColor(eye, d, worldDir, mode == 4 ? vec3(0.0, -10., -15.) : lp); } return vec4(c*cc, 1.0); } vec4 intro(vec2 fragCoord) { if (iTime <= 4.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 9.6) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 16.1) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 19.1) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 25.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 28.7) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 33.7) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 38.3) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 43.1) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 47.9) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 57.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 76.8) { return effect_raymarch(fragCoord, MODE_CROSS_CENTER); } else { return effect_swirls(fragCoord, MODE_SWIRLS_CENTER); } } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (fragCoord.xy - iResolution.xy)/ iResolution.xy; fragColor = intro(fragCoord); // Vignette fragColor.rgb *= 1. - (pow(abs(uv.x), 5.) + pow(abs(uv.y), 5.)) * .4; // Tonemapping fragColor.rgb /= (fragColor.rgb + vec3(.5)) * .7; // Gamma fragColor.rgb = pow(fragColor.rgb, vec3(1. / 2.2)); if (iTime > 94.0) { fragColor /= (-93.9+iTime)*6.; } }
cc0-1.0
[ 2240, 2320, 2349, 2349, 2682 ]
[ [ 800, 856, 883, 883, 958 ], [ 960, 960, 983, 983, 1506 ], [ 1508, 1508, 1542, 1542, 1570 ], [ 1572, 1603, 1642, 1642, 1708 ], [ 1710, 1710, 1746, 1746, 1766 ], [ 1768, 1768, 1802, 1802, 1894 ], [ 1896, 1896, 1929, 1929, 2057 ], [ 2059, 2059, 2133, 2133, 2237 ], [ 2240, 2320, 2349, 2349, 2682 ], [ 2684, 2684, 2713, 2713, 3355 ], [ 3357, 3357, 3393, 3393, 3765 ], [ 3767, 3767, 3801, 3801, 4014 ], [ 4017, 4017, 4042, 4042, 4481 ], [ 4484, 4484, 4536, 4536, 5364 ], [ 5367, 5367, 5432, 5432, 5564 ], [ 5566, 5566, 5615, 5650, 5876 ], [ 5878, 5945, 6005, 6005, 6192 ], [ 6194, 6194, 6263, 6263, 6453 ], [ 6458, 6458, 6504, 6504, 7085 ], [ 7087, 7087, 7135, 7135, 9229 ], [ 9232, 9232, 9260, 9260, 10458 ], [ 10461, 10461, 10518, 10518, 10925 ] ]
// https://www1.udel.edu/biology/rosewc/kaap427627/notes/matrices_rotations.pdf
mat3 fullRotate(vec3 theta) {
float sx=sin(theta.x); float cx=cos(theta.x); float sy=sin(theta.y); float cy=cos(theta.y); float sz=sin(theta.z); float cz=cos(theta.z); return mat3( vec3(cy*cz, -cy*sz, sy), vec3(sx*sy*cz+cx*sz, -sx*sy*sz+cx*cz, -sx*cy), vec3(-cx*sy*cz+sx*sz, cx*sy*sz+sx*cz, cx*cy) ); }
// https://www1.udel.edu/biology/rosewc/kaap427627/notes/matrices_rotations.pdf mat3 fullRotate(vec3 theta) {
1
1
sscGz4
unjello
2021-08-17T18:12:56
/// Exodus / Aberration Creations, a 4k intro /// 3rd place @ MAGFest 2019 /// License: CC0 /// /// Effects inspired by: /// Octahedral Voxel Tracing / fizzer: https://www.shadertoy.com/view/4lcfDB /// Swirly Strands / Plento: https://www.shadertoy.com/view/MtKfWy /// InFX.1 / patu: https://www.shadertoy.com/view/llSSRm /// /// Soundtrack: https://soundcloud.com/argasek/exodus-video /// Video: www.youtube.com/watch?v=HJA1xIevGl0 /// float MIN_DIST = 0.0; float MAX_DIST = 120.0; float EPSILON = 0.0001; vec3 K_a = vec3(1.); vec3 K_d = vec3(.6); vec3 K_s = vec3(0.5, 1.0, 0.5); vec3 lp = vec3(0.0, 1.0, -0.5); vec3 zero3 = vec3(0.); int MAX_STEPS = 80; int MODE_CROSS_CENTER = 1; int MODE_CROSS_JUMPING = 2; int MODE_METABALLS_CENTER = 3; int MODE_SWIRLS_CENTER = 5; int MODE_SWIRLS_SIDE = 6; // random took from // https://thebookofshaders.com/11/ float random (in vec2 st) { return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123); } float noise (vec2 st) { vec2 i = floor(st); vec2 f = fract(st); // Four corners in 2D of a tile float a = random(i); float b = random(i + vec2(1.0, 0.0)); float c = random(i + vec2(0.0, 1.0)); float d = random(i + vec2(1.0, 1.0)); // Smooth Interpolation // Cubic Hermine Curve. Same as SmoothStep() vec2 u = f*f*(3.0-2.0*f); // u = smoothstep(0.,1.,f); // Mix 4 coorners percentages return 0.4*(mix(a, b, u.x) + (c - a)* u.y * (1.0 - u.x) + (d - b) * u.x * u.y); } float sdfSphere(vec3 p, float r) { return length(p) - r; } // http://mercury.sexy/hg_sdf/ float sdfCubeCheap(vec3 p, vec3 size) { vec3 d = abs(p) - size; return max(d.x, max(d.y, d.z)); } float sdfOpUnion(float a, float b) { return min(a,b); } vec3 sdfOpMod(vec3 p, vec3 size) { vec3 halfsize = size * 0.5; p = mod(p + halfsize, size) - halfsize; return p; } vec3 opTwist( vec3 p, float r ) { float c = cos(r * p.y + r); float s = sin(r * p.y + r); mat2 m = mat2(c,-s,s,c); return vec3(m*p.xz,p.y); } float opBlob(float d1, float d2, float d3, float d4, float d5, float d6) { float k = 2.0; return -log(exp(-k*d1)+exp(-k*d2)+exp(-k*d3)+exp(-k*d4)+exp(-k*d5)+exp(-k*d6))/k; } // https://www1.udel.edu/biology/rosewc/kaap427627/notes/matrices_rotations.pdf mat3 fullRotate(vec3 theta) { float sx=sin(theta.x); float cx=cos(theta.x); float sy=sin(theta.y); float cy=cos(theta.y); float sz=sin(theta.z); float cz=cos(theta.z); return mat3( vec3(cy*cz, -cy*sz, sy), vec3(sx*sy*cz+cx*sz, -sx*sy*sz+cx*cz, -sx*cy), vec3(-cx*sy*cz+sx*sz, cx*sy*sz+sx*cz, cx*cy) ); } float sdf_metaballs(vec3 p) { float t = iTime / 4.; float p1 = sdfSphere(0.5*(p + vec3(cos(t*0.5),sin(t*0.3),cos(t))), 1.+0.5*cos(t*6.0)); float p2 = sdfSphere(2.0*(p + 3.0 * vec3(cos(t*1.1),cos(t*1.3),cos(t*1.7))), 3.+2.*sin(t))/2.0; float p3 = sdfSphere(2.0*(p + 5.0 * vec3(cos(t*0.7),cos(t*1.9),cos(t*2.3))), 3.)/2.0; float p4 = sdfSphere(2.0*(p + 3.0 * vec3(cos(t*0.3),cos(t*2.9),sin(t*1.1))), 3.+2.*sin(t))/2.0; float p5 = sdfSphere(2.0*(p + 6.0 * vec3(sin(t*1.3),sin(t*1.7),sin(t*0.7))), 3.0+1.5*cos(t))/2.0; float p6 = sdfSphere(2.0*(p + 3.0 * vec3(sin(t*2.3),sin(t*1.9),sin(t*2.9))), 3.0)/2.0; return opBlob(p1, p2, p3, p4, p5, p6); } float sdf_swirls(vec3 p, int mode) { p -= vec3(1.0, -0.25, 4.0); p *= fullRotate(vec3( 0.0, 0.0, mode == MODE_SWIRLS_CENTER ? p.z*0.06+0.2*sin(iTime) : p.z*.06+iTime*0.25 )); p.y += sin(p.z + iTime + p.x*1.0)*0.2; p.x += cos(p.y - p.z * 2.0 + iTime)*0.3; p = sdfOpMod(p, vec3(1.5, 1.5, 0.5+0.3*sin(iTime))); return sdfCubeCheap(p, vec3(0.033, 0.033, 2.0)); } float sdfCross(vec3 p, float w ) { float da = sdfCubeCheap(p.xyz,vec3(20., w, w)); float db = sdfCubeCheap(p.yzx,vec3(w, 20., w)); float dc = sdfCubeCheap(p.zxy,vec3(w, w , 20.)); return sdfOpUnion(sdfOpUnion(sdfOpUnion(db,dc), da), da); } float sdf_cross(vec3 p) { float t = iTime / 4.; float w = 1.7 - length(p) / 10.; p = opTwist(p, 0.1*sin(iTime*0.02))*fullRotate(vec3(iTime*0.01, 0.0, iTime*0.02)); p *= fullRotate(vec3(sin(iTime*0.1), 0.0, cos(iTime*0.02))); float res = sdfOpUnion( sdfCross(p, w), sdfCross(p * fullRotate(vec3(3.14/4.0, 0.0, 3.14/4.0)), w)); res = sdfOpUnion(res, sdfCross(p * fullRotate(vec3(3.14, 3.14/4.0, 3.14)), w)); return res; } vec2 render_raymarch(vec3 eye, vec3 dir, int mode) { float dist = MIN_DIST; float glow = 0.0; float minDist = MAX_DIST; for (int i = 0; i < MAX_STEPS; ++i) { vec3 v = eye + dist * dir; float step = 0.0; if (mode == MODE_METABALLS_CENTER) { step = sdf_metaballs(v); } if (mode == MODE_CROSS_CENTER || mode == MODE_CROSS_JUMPING) { step = sdf_cross(v); } if (mode == MODE_SWIRLS_CENTER || mode == MODE_SWIRLS_SIDE) { step = sdf_swirls(v, mode); } if (abs(step) < EPSILON) { return vec2(dist, glow); } dist += step; minDist = min(minDist, step * 4.); glow = pow( 1. / minDist, 0.4); if (dist >= MAX_DIST) { return vec2(dist, glow); } } return vec2(dist, glow); } vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) { vec2 xy = fragCoord - size / 2.0; float z = size.y / tan(radians(fieldOfView) / 2.0); return normalize(vec3(xy, -z)); } mat4 viewMatrix(vec3 eye, vec3 center, vec3 up) { // Based on gluLookAt man page vec3 f = normalize(center - eye); vec3 s = normalize(cross(f, up)); vec3 u = cross(s, f); return mat4( vec4(s, 0.0), vec4(u, 0.0), vec4(-f, 0.0), vec4(0.0, 0.0, 0.0, 1) ); } // http://learnwebgl.brown37.net/09_lights/lights_attenuation.html vec3 getSunLightColor(vec3 eye, vec3 dir, vec3 p, vec3 lp) { vec3 sun_pos = eye; vec3 L = sun_pos - p; float d = max(length(L), EPSILON); float atten = 1.0 / (1.0 + d*0.2 + d*d*0.1); vec3 c = (K_a + K_d + K_s)*atten; return c; } vec3 getFoggyColor(vec3 eye, float d, vec3 dir, vec3 lightPosition) { vec3 p = eye + d * dir; vec3 c = getSunLightColor(eye, dir, p, lightPosition); float fog = smoothstep(0.0, 0.68, d*0.005); return mix(c, zero3, fog); } vec4 effect_swirls(vec2 fragCoord, int mode) { vec2 uv = vec2(fragCoord.xy - 0.5*iResolution.xy)/iResolution.y; vec3 eye = vec3(mode == MODE_SWIRLS_CENTER ? 0.0 : 0.0, 0.0, (mode == MODE_SWIRLS_CENTER ? -17.0 : 2.0)*iTime); vec3 viewDir = rayDirection(mode == MODE_SWIRLS_CENTER ? 45.0 : 25.0, iResolution.xy, mode == MODE_SWIRLS_CENTER ? fragCoord : fragCoord.yx);//normalize(vec3(uv,2.0)); float d = render_raymarch(eye, viewDir, mode).x; if (d >= MAX_DIST) { return vec4(0.0); } else { return vec4(getFoggyColor(eye, d, viewDir, lp), 1.0); } } vec4 effect_raymarch(vec2 fragCoord, int mode) { float k = (iTime+150.)/ 2.0; vec3 eye = vec3( mode == MODE_METABALLS_CENTER ? 30. : sin(k) * 40., 1. , mode == MODE_METABALLS_CENTER ? -5.+sin(k) :cos(k) * -20.); vec3 viewDir = rayDirection(45.0, iResolution.xy, fragCoord); vec3 tt = vec3(10., mode == MODE_CROSS_CENTER ? 0. : 20. , 0.); if (mode == MODE_METABALLS_CENTER) { tt.x/=2.; tt.y = 2.5+sin(k); } vec2 uv = fragCoord.xy / iResolution.xy - 1.0; vec3 cc = vec3(1.0); if (mode == MODE_CROSS_CENTER) { uv.y += noise(uv)*sin(k*noise(uv*cos(k))); uv.x -= sin(k*noise(uv*sin(k))); float n = (ceil(uv.x * uv.y)); if (abs(n) < EPSILON) { tt.y += 2.0 * sin(iTime); cc = vec3(0.65); } } else { float n, n2, n3; float div = mode == MODE_CROSS_JUMPING ? 1. : -1.; n = (ceil(uv.x*2.5 + div*uv.y*2.5 + div*2.0 - div*sin(k+noise(uv)))); n2 = (ceil(uv.x*2.5 + div*uv.y*2.5 + 2.0*sin(k))); n3 = (ceil(uv.x*2.5 + div*uv.y*2.5 + div*2.0 - div*sin(k)*cos(k))); vec3 cc = vec3(1.0); if (abs(n) < EPSILON) { tt.y += 2.0 * sin(iTime); } if (abs(n2) < EPSILON) { tt.y += 3.0 * cos(iTime); } if (abs(n3) < EPSILON) { tt.y += 4.0 * cos(iTime); } cc = vec3(0.65); } vec3 up = vec3(0.2, 0.2, -1.); if (mode == MODE_CROSS_JUMPING) { up.z = -50.*cos(k); } else if (mode == MODE_CROSS_CENTER) { up.y = sin(k*5.); up.z = cos(k*5.); } mat4 viewToWorld = viewMatrix(eye, tt, up); vec3 worldDir = (viewToWorld * vec4(viewDir, 0.0)).xyz; vec2 dd = render_raymarch(eye, worldDir, mode); float d = dd.x; float glow = dd.y; vec3 c = zero3; if (d >= MAX_DIST) { float g = glow*glow; c += K_s*glow*0.2 + K_d*g; } else { c = getFoggyColor(eye, d, worldDir, mode == 4 ? vec3(0.0, -10., -15.) : lp); } return vec4(c*cc, 1.0); } vec4 intro(vec2 fragCoord) { if (iTime <= 4.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 9.6) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 16.1) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 19.1) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 25.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 28.7) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 33.7) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 38.3) { return effect_raymarch(fragCoord, MODE_CROSS_JUMPING); } else if (iTime <= 43.1) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 47.9) { return effect_swirls(fragCoord, MODE_SWIRLS_SIDE); } else if (iTime <= 57.7) { return effect_raymarch(fragCoord, MODE_METABALLS_CENTER); } else if (iTime <= 76.8) { return effect_raymarch(fragCoord, MODE_CROSS_CENTER); } else { return effect_swirls(fragCoord, MODE_SWIRLS_CENTER); } } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 uv = (fragCoord.xy - iResolution.xy)/ iResolution.xy; fragColor = intro(fragCoord); // Vignette fragColor.rgb *= 1. - (pow(abs(uv.x), 5.) + pow(abs(uv.y), 5.)) * .4; // Tonemapping fragColor.rgb /= (fragColor.rgb + vec3(.5)) * .7; // Gamma fragColor.rgb = pow(fragColor.rgb, vec3(1. / 2.2)); if (iTime > 94.0) { fragColor /= (-93.9+iTime)*6.; } }
cc0-1.0
[ 5878, 5945, 6005, 6005, 6192 ]
[ [ 800, 856, 883, 883, 958 ], [ 960, 960, 983, 983, 1506 ], [ 1508, 1508, 1542, 1542, 1570 ], [ 1572, 1603, 1642, 1642, 1708 ], [ 1710, 1710, 1746, 1746, 1766 ], [ 1768, 1768, 1802, 1802, 1894 ], [ 1896, 1896, 1929, 1929, 2057 ], [ 2059, 2059, 2133, 2133, 2237 ], [ 2240, 2320, 2349, 2349, 2682 ], [ 2684, 2684, 2713, 2713, 3355 ], [ 3357, 3357, 3393, 3393, 3765 ], [ 3767, 3767, 3801, 3801, 4014 ], [ 4017, 4017, 4042, 4042, 4481 ], [ 4484, 4484, 4536, 4536, 5364 ], [ 5367, 5367, 5432, 5432, 5564 ], [ 5566, 5566, 5615, 5650, 5876 ], [ 5878, 5945, 6005, 6005, 6192 ], [ 6194, 6194, 6263, 6263, 6453 ], [ 6458, 6458, 6504, 6504, 7085 ], [ 7087, 7087, 7135, 7135, 9229 ], [ 9232, 9232, 9260, 9260, 10458 ], [ 10461, 10461, 10518, 10518, 10925 ] ]
// http://learnwebgl.brown37.net/09_lights/lights_attenuation.html
vec3 getSunLightColor(vec3 eye, vec3 dir, vec3 p, vec3 lp) {
vec3 sun_pos = eye; vec3 L = sun_pos - p; float d = max(length(L), EPSILON); float atten = 1.0 / (1.0 + d*0.2 + d*d*0.1); vec3 c = (K_a + K_d + K_s)*atten; return c; }
// http://learnwebgl.brown37.net/09_lights/lights_attenuation.html vec3 getSunLightColor(vec3 eye, vec3 dir, vec3 p, vec3 lp) {
1
1
7sdXz7
mrange
2021-09-29T08:15:20
// License CC0: Simple circle tiling // Been working too much lately to do shader stuff. // But today I experimented a bit with tiling so thought I share #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PI 3.141592654 #define TAU (2.0*PI) // License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488 const vec4 hsv2rgb_K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 hsv2rgb(vec3 c) { vec3 p = abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www); return c.z * mix(hsv2rgb_K.xxx, clamp(p - hsv2rgb_K.xxx, 0.0, 1.0), c.y); } // License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488 // Macro version of above to enable compile-time constants #define HSV2RGB(c) (c.z * mix(hsv2rgb_K.xxx, clamp(abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www) - hsv2rgb_K.xxx, 0.0, 1.0), c.y)) float circle(vec2 p, float r) { return length(p) - r; } // License: MIT OR CC-BY-NC-4.0, author: mercury, found: https://mercury.sexy/hg_sdf/ vec2 mod2(inout vec2 p, vec2 size) { vec2 c = floor((p + size*0.5)/size); p = mod(p + size*0.5,size) - size*0.5; return c; } // License: Unknown, author: Hexler, found: Kodelife example Grid float hash(vec2 uv) { return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453); } float df(vec2 p, out float n, out float sc) { vec2 pp = p; float sz = 2.0; float r = 0.0; for (int i = 0; i < 5; ++i) { vec2 nn = mod2(pp, vec2(sz)); sz /= 3.0; float rr = hash(nn+123.4); r += rr; if (rr < 0.5) break; } float d = circle(pp, 1.25*sz); n = fract(r); sc = sz; return d; } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm vec3 postProcess(vec3 col, vec2 q) { // Found this somewhere on the interwebs col = clamp(col, 0.0, 1.0); // Gamma correction col = pow(col, 1.0/vec3(2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); // Vignetting col*= 0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord/RESOLUTION.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; float aa = 2.0/RESOLUTION.y; const float r = 25.0; float a = 0.05*TAU*TIME/r; const float z = 1.0; p /= z; p += r*vec2(cos(a), sin(a)); p *= ROT(-a+0.25); float n = 0.0; float sc = 0.0; float d = df(p, n, sc)*z; vec3 col = vec3(0.0); vec3 hsv = vec3(n-0.25*d/sc, 0.5+0.5*d/sc, 1.0); vec3 rgb = hsv2rgb(hsv); col = mix(col, rgb, smoothstep(aa, -aa, d)); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 1009, 1095, 1131, 1131, 1225 ]
[ [ 486, 486, 508, 508, 654 ], [ 950, 950, 981, 981, 1007 ], [ 1009, 1095, 1131, 1131, 1225 ], [ 1227, 1293, 1314, 1314, 1382 ], [ 1384, 1384, 1429, 1429, 1722 ], [ 1724, 1811, 1847, 1891, 2162 ], [ 2164, 2164, 2219, 2219, 2762 ] ]
// License: MIT OR CC-BY-NC-4.0, author: mercury, found: https://mercury.sexy/hg_sdf/
vec2 mod2(inout vec2 p, vec2 size) {
vec2 c = floor((p + size*0.5)/size); p = mod(p + size*0.5,size) - size*0.5; return c; }
// License: MIT OR CC-BY-NC-4.0, author: mercury, found: https://mercury.sexy/hg_sdf/ vec2 mod2(inout vec2 p, vec2 size) {
52
53
7sdXz7
mrange
2021-09-29T08:15:20
// License CC0: Simple circle tiling // Been working too much lately to do shader stuff. // But today I experimented a bit with tiling so thought I share #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PI 3.141592654 #define TAU (2.0*PI) // License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488 const vec4 hsv2rgb_K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 hsv2rgb(vec3 c) { vec3 p = abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www); return c.z * mix(hsv2rgb_K.xxx, clamp(p - hsv2rgb_K.xxx, 0.0, 1.0), c.y); } // License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488 // Macro version of above to enable compile-time constants #define HSV2RGB(c) (c.z * mix(hsv2rgb_K.xxx, clamp(abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www) - hsv2rgb_K.xxx, 0.0, 1.0), c.y)) float circle(vec2 p, float r) { return length(p) - r; } // License: MIT OR CC-BY-NC-4.0, author: mercury, found: https://mercury.sexy/hg_sdf/ vec2 mod2(inout vec2 p, vec2 size) { vec2 c = floor((p + size*0.5)/size); p = mod(p + size*0.5,size) - size*0.5; return c; } // License: Unknown, author: Hexler, found: Kodelife example Grid float hash(vec2 uv) { return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453); } float df(vec2 p, out float n, out float sc) { vec2 pp = p; float sz = 2.0; float r = 0.0; for (int i = 0; i < 5; ++i) { vec2 nn = mod2(pp, vec2(sz)); sz /= 3.0; float rr = hash(nn+123.4); r += rr; if (rr < 0.5) break; } float d = circle(pp, 1.25*sz); n = fract(r); sc = sz; return d; } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm vec3 postProcess(vec3 col, vec2 q) { // Found this somewhere on the interwebs col = clamp(col, 0.0, 1.0); // Gamma correction col = pow(col, 1.0/vec3(2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); // Vignetting col*= 0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord/RESOLUTION.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; float aa = 2.0/RESOLUTION.y; const float r = 25.0; float a = 0.05*TAU*TIME/r; const float z = 1.0; p /= z; p += r*vec2(cos(a), sin(a)); p *= ROT(-a+0.25); float n = 0.0; float sc = 0.0; float d = df(p, n, sc)*z; vec3 col = vec3(0.0); vec3 hsv = vec3(n-0.25*d/sc, 0.5+0.5*d/sc, 1.0); vec3 rgb = hsv2rgb(hsv); col = mix(col, rgb, smoothstep(aa, -aa, d)); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 1227, 1293, 1314, 1314, 1382 ]
[ [ 486, 486, 508, 508, 654 ], [ 950, 950, 981, 981, 1007 ], [ 1009, 1095, 1131, 1131, 1225 ], [ 1227, 1293, 1314, 1314, 1382 ], [ 1384, 1384, 1429, 1429, 1722 ], [ 1724, 1811, 1847, 1891, 2162 ], [ 2164, 2164, 2219, 2219, 2762 ] ]
// License: Unknown, author: Hexler, found: Kodelife example Grid
float hash(vec2 uv) {
return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453); }
// License: Unknown, author: Hexler, found: Kodelife example Grid float hash(vec2 uv) {
3
21
NstSDn
mrange
2021-09-29T19:44:14
// License CC0: Adaptive tile sizes // Been working too much lately to do shader stuff. // But today I experimented a bit with tiling so thought I share #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PI 3.141592654 #define TAU (2.0*PI) #define DOT2(x) dot(x, x) const int max_iter = 6; // License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488 const vec4 hsv2rgb_K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 hsv2rgb(vec3 c) { vec3 p = abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www); return c.z * mix(hsv2rgb_K.xxx, clamp(p - hsv2rgb_K.xxx, 0.0, 1.0), c.y); } // License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488 // Macro version of above to enable compile-time constants #define HSV2RGB(c) (c.z * mix(hsv2rgb_K.xxx, clamp(abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www) - hsv2rgb_K.xxx, 0.0, 1.0), c.y)) // License: MIT OR CC-BY-NC-4.0, author: mercury, found: https://mercury.sexy/hg_sdf/ vec2 mod2(inout vec2 p, vec2 size) { vec2 c = floor((p + size*0.5)/size); p = mod(p + size*0.5,size) - size*0.5; return c; } // License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float heart(vec2 p) { p.x = abs(p.x); if( p.y+p.x>1.0 ) return sqrt(DOT2(p-vec2(0.25,0.75))) - sqrt(2.0)/4.0; return sqrt(min(DOT2(p-vec2(0.00,1.00)), DOT2(p-0.5*max(p.x+p.y,0.0)))) * sign(p.x-p.y); } // License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float box(vec2 p, vec2 b) { vec2 d = abs(p)-b; return length(max(d,0.0)) + min(max(d.x,d.y),0.0); } float shape(vec2 p) { const float z = 1.6; p /= z; p *= ROT(TIME*0.25); p.y += 0.58; float d = heart(p)*z; return d; } float df(vec2 p, out int ii, out bool inside) { float aa = 3.0/RESOLUTION.y; float sz = 0.9; float ds = shape(p); vec2 pp = p; float r = 0.0; ii = max_iter; for (int i=0; i<max_iter; ++i) { pp = p; vec2 nn = mod2(pp, vec2(sz)); vec2 cp = nn*sz; float d = shape(cp); r = sz*0.5; if (abs(d) > 0.5*sz*sqrt(2.0)) { ii = i; inside = d < 0.0; break; } sz /= 3.0; } return box(pp, vec2(r-aa)); } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm vec3 postProcess(vec3 col, vec2 q) { // Found this somewhere on the interwebs col = clamp(col, 0.0, 1.0); // Gamma correction col = pow(col, 1.0/vec3(2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); // Vignetting col*= 0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord/RESOLUTION.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; float aa = 2.0/RESOLUTION.y; const float r = 25.0; float a = 0.05*TAU*TIME/r; const float z = 1.0; p /= z; int i; bool inside; float d = df(p, i, inside)*z; float ds = shape(p)*z; float ii = float(i)/float(max_iter); if (!inside) d = abs(d)-aa; vec3 col = vec3(0.0); vec3 hsv = vec3((inside ? 0.1 : 0.6) +1.1*ii, 0.7, sqrt(max(1.0-ii, 0.0))); vec3 rgb = hsv2rgb(hsv); col = mix(col, rgb, smoothstep(aa, -aa, d)); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 1223, 1341, 1362, 1362, 1582 ]
[ [ 541, 541, 563, 563, 709 ], [ 1005, 1091, 1127, 1127, 1221 ], [ 1223, 1341, 1362, 1362, 1582 ], [ 1584, 1702, 1729, 1729, 1809 ], [ 1811, 1811, 1832, 1832, 1941 ], [ 1943, 1943, 1990, 1990, 2416 ], [ 2418, 2505, 2541, 2585, 2856 ], [ 2858, 2858, 2913, 2913, 3519 ] ]
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float heart(vec2 p) {
p.x = abs(p.x); if( p.y+p.x>1.0 ) return sqrt(DOT2(p-vec2(0.25,0.75))) - sqrt(2.0)/4.0; return sqrt(min(DOT2(p-vec2(0.00,1.00)), DOT2(p-0.5*max(p.x+p.y,0.0)))) * sign(p.x-p.y); }
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float heart(vec2 p) {
1
12
NstSDn
mrange
2021-09-29T19:44:14
// License CC0: Adaptive tile sizes // Been working too much lately to do shader stuff. // But today I experimented a bit with tiling so thought I share #define TIME iTime #define RESOLUTION iResolution #define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a)) #define PI 3.141592654 #define TAU (2.0*PI) #define DOT2(x) dot(x, x) const int max_iter = 6; // License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488 const vec4 hsv2rgb_K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); vec3 hsv2rgb(vec3 c) { vec3 p = abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www); return c.z * mix(hsv2rgb_K.xxx, clamp(p - hsv2rgb_K.xxx, 0.0, 1.0), c.y); } // License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488 // Macro version of above to enable compile-time constants #define HSV2RGB(c) (c.z * mix(hsv2rgb_K.xxx, clamp(abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www) - hsv2rgb_K.xxx, 0.0, 1.0), c.y)) // License: MIT OR CC-BY-NC-4.0, author: mercury, found: https://mercury.sexy/hg_sdf/ vec2 mod2(inout vec2 p, vec2 size) { vec2 c = floor((p + size*0.5)/size); p = mod(p + size*0.5,size) - size*0.5; return c; } // License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float heart(vec2 p) { p.x = abs(p.x); if( p.y+p.x>1.0 ) return sqrt(DOT2(p-vec2(0.25,0.75))) - sqrt(2.0)/4.0; return sqrt(min(DOT2(p-vec2(0.00,1.00)), DOT2(p-0.5*max(p.x+p.y,0.0)))) * sign(p.x-p.y); } // License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float box(vec2 p, vec2 b) { vec2 d = abs(p)-b; return length(max(d,0.0)) + min(max(d.x,d.y),0.0); } float shape(vec2 p) { const float z = 1.6; p /= z; p *= ROT(TIME*0.25); p.y += 0.58; float d = heart(p)*z; return d; } float df(vec2 p, out int ii, out bool inside) { float aa = 3.0/RESOLUTION.y; float sz = 0.9; float ds = shape(p); vec2 pp = p; float r = 0.0; ii = max_iter; for (int i=0; i<max_iter; ++i) { pp = p; vec2 nn = mod2(pp, vec2(sz)); vec2 cp = nn*sz; float d = shape(cp); r = sz*0.5; if (abs(d) > 0.5*sz*sqrt(2.0)) { ii = i; inside = d < 0.0; break; } sz /= 3.0; } return box(pp, vec2(r-aa)); } // License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm vec3 postProcess(vec3 col, vec2 q) { // Found this somewhere on the interwebs col = clamp(col, 0.0, 1.0); // Gamma correction col = pow(col, 1.0/vec3(2.2)); col = col*0.6+0.4*col*col*(3.0-2.0*col); col = mix(col, vec3(dot(col, vec3(0.33))), -0.4); // Vignetting col*= 0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); return col; } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 q = fragCoord/RESOLUTION.xy; vec2 p = -1. + 2. * q; p.x *= RESOLUTION.x/RESOLUTION.y; float aa = 2.0/RESOLUTION.y; const float r = 25.0; float a = 0.05*TAU*TIME/r; const float z = 1.0; p /= z; int i; bool inside; float d = df(p, i, inside)*z; float ds = shape(p)*z; float ii = float(i)/float(max_iter); if (!inside) d = abs(d)-aa; vec3 col = vec3(0.0); vec3 hsv = vec3((inside ? 0.1 : 0.6) +1.1*ii, 0.7, sqrt(max(1.0-ii, 0.0))); vec3 rgb = hsv2rgb(hsv); col = mix(col, rgb, smoothstep(aa, -aa, d)); col = postProcess(col, q); fragColor = vec4(col, 1.0); }
cc0-1.0
[ 1584, 1702, 1729, 1729, 1809 ]
[ [ 541, 541, 563, 563, 709 ], [ 1005, 1091, 1127, 1127, 1221 ], [ 1223, 1341, 1362, 1362, 1582 ], [ 1584, 1702, 1729, 1729, 1809 ], [ 1811, 1811, 1832, 1832, 1941 ], [ 1943, 1943, 1990, 1990, 2416 ], [ 2418, 2505, 2541, 2585, 2856 ], [ 2858, 2858, 2913, 2913, 3519 ] ]
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float box(vec2 p, vec2 b) {
vec2 d = abs(p)-b; return length(max(d,0.0)) + min(max(d.x,d.y),0.0); }
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float box(vec2 p, vec2 b) {
4
62
wtVyDz
iq
2021-09-09T02:12:40
// The MIT License // Copyright © 2021 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. // The three bisectors of a triangle meet at a single point // which is also the point that is equidistant to the three // sides. And so, it's also the center of the incircle of // the triangle. vec2 triangleIncenter( in vec2 v0, in vec2 v1, in vec2 v2 ) { float l0 = length(v2-v1); float l1 = length(v0-v2); float l2 = length(v1-v0); return (v0*l0+v1*l1+v2*l2)/(l0+l1+l2); } //===================================================== // signed distance to a disk float sdDisk( in vec2 p, in vec2 c, in float r ) { return length(p-c)-r; } // distance to a line segment float sdSegment( 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 ); } // signed distance to a 2D triangle float cro(in vec2 a, in vec2 b ) { return a.x*b.y-a.y*b.x; } float dot2( in vec2 a ) { return dot(a,a); } float sdTriangle( in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p ) { vec2 e0 = p1-p0; vec2 v0 = p-p0; vec2 e1 = p2-p1; vec2 v1 = p-p1; vec2 e2 = p0-p2; vec2 v2 = p-p2; vec2 pq0 = v0 - e0*clamp( dot(v0,e0)/dot2(e0), 0.0, 1.0 ); vec2 pq1 = v1 - e1*clamp( dot(v1,e1)/dot2(e1), 0.0, 1.0 ); vec2 pq2 = v2 - e2*clamp( dot(v2,e2)/dot2(e2), 0.0, 1.0 ); vec2 d = min( min( vec2( dot2( pq0 ), cro(v0,e0) ), vec2( dot2( pq1 ), cro(v1,e1) )), vec2( dot2( pq2 ), cro(v2,e2) )); return -sqrt(d.x)*sign(d.y); } //===================================================== void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 v0 = vec2(1.2,0.8)*cos( 0.5*iTime + vec2(0.0,2.0) ); vec2 v1 = vec2(1.2,0.8)*cos( 0.5*iTime + vec2(1.5,3.0) ); vec2 v2 = vec2(1.2,0.8)*cos( 0.5*iTime + vec2(4.0,1.0) ); // compute traingle SDF float dis = sdTriangle( v0, v1, v2, p ); // compute triangle equicenter (yellow dot) vec2 ce = triangleIncenter( v0, v1, v2 ); // draw triangle SDF vec3 col = vec3(1.0) - sign(dis)*vec3(0.1,0.4,0.7); col *= 1.0 - exp(-2.0*abs(dis)); col *= 0.8 + 0.2*cos(150.0*dis); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(dis)) ); // draw helped bisectors col = mix(col,vec3(1.0,1.0,1.0),smoothstep(0.005,0.001,sdSegment( p, v0, ce ))); col = mix(col,vec3(1.0,1.0,1.0),smoothstep(0.005,0.001,sdSegment( p, v1, ce ))); col = mix(col,vec3(1.0,1.0,1.0),smoothstep(0.005,0.001,sdSegment( p, v2, ce ))); // draw equicenter in yellow col = mix(col,vec3(1.0,1.0,0.0),smoothstep(0.005,0.001,sdDisk(p,ce,0.02))); // output fragColor = vec4(col,1.0); }
mit
[ 1531, 1560, 1610, 1610, 1638 ]
[ [ 1275, 1275, 1336, 1336, 1472 ], [ 1531, 1560, 1610, 1610, 1638 ], [ 1640, 1670, 1722, 1722, 1842 ], [ 1844, 1880, 1914, 1914, 1940 ], [ 1941, 1941, 1966, 1966, 1985 ], [ 1986, 1986, 2053, 2053, 2544 ], [ 2603, 2603, 2660, 2660, 3731 ] ]
// signed distance to a disk
float sdDisk( in vec2 p, in vec2 c, in float r ) {
return length(p-c)-r; }
// signed distance to a disk float sdDisk( in vec2 p, in vec2 c, in float r ) {
2
2
wtVyDz
iq
2021-09-09T02:12:40
// The MIT License // Copyright © 2021 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. // The three bisectors of a triangle meet at a single point // which is also the point that is equidistant to the three // sides. And so, it's also the center of the incircle of // the triangle. vec2 triangleIncenter( in vec2 v0, in vec2 v1, in vec2 v2 ) { float l0 = length(v2-v1); float l1 = length(v0-v2); float l2 = length(v1-v0); return (v0*l0+v1*l1+v2*l2)/(l0+l1+l2); } //===================================================== // signed distance to a disk float sdDisk( in vec2 p, in vec2 c, in float r ) { return length(p-c)-r; } // distance to a line segment float sdSegment( 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 ); } // signed distance to a 2D triangle float cro(in vec2 a, in vec2 b ) { return a.x*b.y-a.y*b.x; } float dot2( in vec2 a ) { return dot(a,a); } float sdTriangle( in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p ) { vec2 e0 = p1-p0; vec2 v0 = p-p0; vec2 e1 = p2-p1; vec2 v1 = p-p1; vec2 e2 = p0-p2; vec2 v2 = p-p2; vec2 pq0 = v0 - e0*clamp( dot(v0,e0)/dot2(e0), 0.0, 1.0 ); vec2 pq1 = v1 - e1*clamp( dot(v1,e1)/dot2(e1), 0.0, 1.0 ); vec2 pq2 = v2 - e2*clamp( dot(v2,e2)/dot2(e2), 0.0, 1.0 ); vec2 d = min( min( vec2( dot2( pq0 ), cro(v0,e0) ), vec2( dot2( pq1 ), cro(v1,e1) )), vec2( dot2( pq2 ), cro(v2,e2) )); return -sqrt(d.x)*sign(d.y); } //===================================================== void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 v0 = vec2(1.2,0.8)*cos( 0.5*iTime + vec2(0.0,2.0) ); vec2 v1 = vec2(1.2,0.8)*cos( 0.5*iTime + vec2(1.5,3.0) ); vec2 v2 = vec2(1.2,0.8)*cos( 0.5*iTime + vec2(4.0,1.0) ); // compute traingle SDF float dis = sdTriangle( v0, v1, v2, p ); // compute triangle equicenter (yellow dot) vec2 ce = triangleIncenter( v0, v1, v2 ); // draw triangle SDF vec3 col = vec3(1.0) - sign(dis)*vec3(0.1,0.4,0.7); col *= 1.0 - exp(-2.0*abs(dis)); col *= 0.8 + 0.2*cos(150.0*dis); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(dis)) ); // draw helped bisectors col = mix(col,vec3(1.0,1.0,1.0),smoothstep(0.005,0.001,sdSegment( p, v0, ce ))); col = mix(col,vec3(1.0,1.0,1.0),smoothstep(0.005,0.001,sdSegment( p, v1, ce ))); col = mix(col,vec3(1.0,1.0,1.0),smoothstep(0.005,0.001,sdSegment( p, v2, ce ))); // draw equicenter in yellow col = mix(col,vec3(1.0,1.0,0.0),smoothstep(0.005,0.001,sdDisk(p,ce,0.02))); // output fragColor = vec4(col,1.0); }
mit
[ 1640, 1670, 1722, 1722, 1842 ]
[ [ 1275, 1275, 1336, 1336, 1472 ], [ 1531, 1560, 1610, 1610, 1638 ], [ 1640, 1670, 1722, 1722, 1842 ], [ 1844, 1880, 1914, 1914, 1940 ], [ 1941, 1941, 1966, 1966, 1985 ], [ 1986, 1986, 2053, 2053, 2544 ], [ 2603, 2603, 2660, 2660, 3731 ] ]
// distance to a line segment
float sdSegment( 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 ); }
// distance to a line segment float sdSegment( in vec2 p, in vec2 a, in vec2 b ) {
5
121
wtVyDz
iq
2021-09-09T02:12:40
// The MIT License // Copyright © 2021 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. // The three bisectors of a triangle meet at a single point // which is also the point that is equidistant to the three // sides. And so, it's also the center of the incircle of // the triangle. vec2 triangleIncenter( in vec2 v0, in vec2 v1, in vec2 v2 ) { float l0 = length(v2-v1); float l1 = length(v0-v2); float l2 = length(v1-v0); return (v0*l0+v1*l1+v2*l2)/(l0+l1+l2); } //===================================================== // signed distance to a disk float sdDisk( in vec2 p, in vec2 c, in float r ) { return length(p-c)-r; } // distance to a line segment float sdSegment( 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 ); } // signed distance to a 2D triangle float cro(in vec2 a, in vec2 b ) { return a.x*b.y-a.y*b.x; } float dot2( in vec2 a ) { return dot(a,a); } float sdTriangle( in vec2 p0, in vec2 p1, in vec2 p2, in vec2 p ) { vec2 e0 = p1-p0; vec2 v0 = p-p0; vec2 e1 = p2-p1; vec2 v1 = p-p1; vec2 e2 = p0-p2; vec2 v2 = p-p2; vec2 pq0 = v0 - e0*clamp( dot(v0,e0)/dot2(e0), 0.0, 1.0 ); vec2 pq1 = v1 - e1*clamp( dot(v1,e1)/dot2(e1), 0.0, 1.0 ); vec2 pq2 = v2 - e2*clamp( dot(v2,e2)/dot2(e2), 0.0, 1.0 ); vec2 d = min( min( vec2( dot2( pq0 ), cro(v0,e0) ), vec2( dot2( pq1 ), cro(v1,e1) )), vec2( dot2( pq2 ), cro(v2,e2) )); return -sqrt(d.x)*sign(d.y); } //===================================================== void mainImage( out vec4 fragColor, in vec2 fragCoord ) { vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 v0 = vec2(1.2,0.8)*cos( 0.5*iTime + vec2(0.0,2.0) ); vec2 v1 = vec2(1.2,0.8)*cos( 0.5*iTime + vec2(1.5,3.0) ); vec2 v2 = vec2(1.2,0.8)*cos( 0.5*iTime + vec2(4.0,1.0) ); // compute traingle SDF float dis = sdTriangle( v0, v1, v2, p ); // compute triangle equicenter (yellow dot) vec2 ce = triangleIncenter( v0, v1, v2 ); // draw triangle SDF vec3 col = vec3(1.0) - sign(dis)*vec3(0.1,0.4,0.7); col *= 1.0 - exp(-2.0*abs(dis)); col *= 0.8 + 0.2*cos(150.0*dis); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(dis)) ); // draw helped bisectors col = mix(col,vec3(1.0,1.0,1.0),smoothstep(0.005,0.001,sdSegment( p, v0, ce ))); col = mix(col,vec3(1.0,1.0,1.0),smoothstep(0.005,0.001,sdSegment( p, v1, ce ))); col = mix(col,vec3(1.0,1.0,1.0),smoothstep(0.005,0.001,sdSegment( p, v2, ce ))); // draw equicenter in yellow col = mix(col,vec3(1.0,1.0,0.0),smoothstep(0.005,0.001,sdDisk(p,ce,0.02))); // output fragColor = vec4(col,1.0); }
mit
[ 1844, 1880, 1914, 1914, 1940 ]
[ [ 1275, 1275, 1336, 1336, 1472 ], [ 1531, 1560, 1610, 1610, 1638 ], [ 1640, 1670, 1722, 1722, 1842 ], [ 1844, 1880, 1914, 1914, 1940 ], [ 1941, 1941, 1966, 1966, 1985 ], [ 1986, 1986, 2053, 2053, 2544 ], [ 2603, 2603, 2660, 2660, 3731 ] ]
// signed distance to a 2D triangle
float cro(in vec2 a, in vec2 b ) {
return a.x*b.y-a.y*b.x; }
// signed distance to a 2D triangle float cro(in vec2 a, in vec2 b ) {
1
7
7sdXz2
iq
2021-10-06T23:49:45
// The MIT License // Copyright © 2021 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 // Signed distance to a 2D rounded square. // // List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf // and www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm // s = side length // r = corner radius float sdRoundSquare( in vec2 p, in float s, in float r ) { vec2 q = abs(p)-s+r; return min(max(q.x,q.y),0.0) + length(max(q,0.0)) - r; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // pixel and moust coordinates vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y; // animate the square float si = 0.7 + 0.2*cos(1.2*iTime+0.0); float ra = 0.3 - 0.2*cos(3.1*iTime+4.0); ra = min(ra,si); // draw the square float d = sdRoundSquare( p, si, ra ); // apply colors to it vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7); col *= 1.0 - exp(-3.0*abs(d)); col *= 0.8 + 0.2*cos(150.0*d); col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.01,abs(d)) ); // mouse interaction if( iMouse.z>0.001 ) { d = sdRoundSquare( m, si, ra ); col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0025)); col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-m)-0.015)); } fragColor = vec4(col,1.0); }
mit
[ 1347, 1387, 1446, 1446, 1532 ]
[ [ 1347, 1387, 1446, 1446, 1532 ], [ 1536, 1536, 1593, 1628, 2465 ] ]
// s = side length // r = corner radius
float sdRoundSquare( in vec2 p, in float s, in float r ) {
vec2 q = abs(p)-s+r; return min(max(q.x,q.y),0.0) + length(max(q,0.0)) - r; }
// s = side length // r = corner radius float sdRoundSquare( in vec2 p, in float s, in float r ) {
5
5
fs3SWj
mattz
2021-10-12T00:52:06
////////////////////////////////////////////////////////////////////// // // "pentagonal tiling variations" by mattz // License https://creativecommons.org/licenses/by/4.0/ // // Click and drag to set vertex position. // // Renders "type 4" Cairo pentagonal tilings. See // https://en.wikipedia.org/wiki/Cairo_pentagonal_tiling // for details. // // Inspired by https://twitter.com/cs_kaplan // // Related shaders: // // - "Cairo tiling" by nimitz // https://www.shadertoy.com/view/4ssSWf // // - "More Cairo Tiles" by mla // https://www.shadertoy.com/view/MlSfRd // // - "Extruded Pentagon Tiling" by Shane // https://www.shadertoy.com/view/3t2cDK // // - "15th Pentagonal tiling" by tomkh // https://www.shadertoy.com/view/4lBXRV // // - "pentagonal tiling" by FabriceNeyret2 // https://www.shadertoy.com/view/ltBBzK // (golfed at https://www.shadertoy.com/view/XljfRV) // // Noise function from iq's "Noise - gradient - 2D" // https://www.shadertoy.com/view/XdXGW8 // ////////////////////////////////////////////////////////////////////// // vector rotated by 90 degrees CCW vec2 perp(vec2 u) { return vec2(-u.y, u.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 rotate(vec2 rvec, vec2 p) { return p.x * rvec + p.y * vec2(-rvec.y, rvec.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 unrotate(vec2 rvec, vec2 p) { return p.x * vec2(rvec.x, -rvec.y) + p.y * rvec.yx; } // distance from point to line segment float dseg(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; float u = dot(pa, ba) / dot(ba, ba); u = clamp(u, 0.0, 1.0); return length(pa - u * ba); } // half-plane test bool in_half_plane(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; return dot(pa, perp(ba)) > 0.0; } // point in triangle bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) { return in_half_plane(p, a, b) && in_half_plane(p, b, c) && in_half_plane(p, c, a); } // from https://www.shadertoy.com/view/XdXGW8 - used for noise below vec2 grad( ivec2 z ) { // 2D to 1D (feel free to replace by some other) int n = z.x+z.y*11111; // Hugo Elias hash (feel free to replace by another one) n = (n<<13)^n; n = (n*(n*n*15731+789221)+1376312589)>>16; // Perlin style vectors n &= 7; vec2 gr = vec2(n&1,n>>1)*2.0-1.0; return ( n>=6 ) ? vec2(0.0,gr.x) : ( n>=4 ) ? vec2(gr.x,0.0) : gr; } // from https://www.shadertoy.com/view/XdXGW8 float noise( in vec2 p ) { ivec2 i = ivec2(floor( p )); vec2 f = fract( p ); vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); return mix( mix( dot( grad( i+ivec2(0,0) ), f-vec2(0.0,0.0) ), dot( grad( i+ivec2(1,0) ), f-vec2(1.0,0.0) ), u.x), mix( dot( grad( i+ivec2(0,1) ), f-vec2(0.0,1.0) ), dot( grad( i+ivec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y); } // colors for each cell const vec3 CELL_COLORS[4] = vec3[4]( vec3(0.9, 0.0, 0.05), vec3(0.95, 0.85, 0.0), vec3(0.1, 0.8, 0.05), vec3(0.1, 0.5, 0.8) ); // rotation vectors for theta = 0, pi/2, pi, 3*pi/2 const vec2 ROT_VECTORS[4] = vec2[4]( vec2(1, 0), vec2(0, 1), vec2(-1, 0), vec2(0, -1) ); // un-rotated cell geometry // // // C // _*_____ // _- -----____ // D _*------------------* B --- // _- | || // * | || // | | | | // | | | | t // | | | | // | | A | | // | | __* | --- // | | __-- \ | // || __-- \ | y // ||__-- \| // O *------------------* X --- // // | x | s | // // notes: // // square is 0.5 x 0.5 // // A = O + (x, y) // B = O + (0.5, 0.5) // C = B + (-t, s) = B + (y - 0.5, 0.5 - x) // D = O + (-y, x) // X = O + (0.5, 0) // // segments OD and OA are congruent and perpendicular // segments AB and BC are congruent and perpendicular // // there are four rotated copies of polygon OABCD around point O // polygon points vec2 O, A, B, C, D; // for bump-mapped shading vec2 heightmap(vec2 p) { // get polygon distance float dpoly = dseg(p, O, A); dpoly = min(dpoly, dseg(p, A, B)); dpoly = min(dpoly, dseg(p, B, C)); dpoly = min(dpoly, dseg(p, C, D)); dpoly = min(dpoly, dseg(p, D, O)); // offset from edge float k = 0.08; // base height float z = k + 0.01 * noise(5.*p); if (dpoly < k) { // semicircular shoulder float w = (dpoly/k - 1.0); z *= sqrt(1.0 - w*w); } else { // depression inwards from edge z *= (1.0 - 0.03*smoothstep(k, 2.0*k, dpoly)); } // return height and polygon distance return vec2(z, dpoly); } // do the things! void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // global rotation by 45 degrees vec2 global_rot = vec2(0.5*sqrt(2.0)); // image should be six blocks high float scl = 6.0 * global_rot.x / iResolution.y; // uv in [0,1] x [0, 1] holds vertex position vec2 uv = vec2(0.7886751345948132, 0.21132486540518713); // light coords relative to center vec2 lcoord = vec2(0); if (iMouse.z > 0.) { // set vertex coords by dragging - light is fixed uv = clamp(iMouse.xy / iResolution.xy, 0.0, 1.0); lcoord = vec2(-0.0, 0.5); } else { // set vertex coords varying over screen // and move light uv = (fragCoord.xy - 0.5 * iResolution.xy) / (max(iResolution.x, iResolution.y)); const float repeat = 30.0; // seconds const float wraps_per_repeat = 5.0; const float x_lobes = 3.0; const float y_lobes = 2.0; const float two_pi = 6.283185307179586; float t = iTime * two_pi / repeat; float t_wrap = t * wraps_per_repeat; float c = cos(t_wrap); float s = sin(t_wrap); uv = rotate(vec2(s, -c), uv); uv = clamp(uv + 0.5, 0.0, 1.0); lcoord = vec2(-sin(t * x_lobes), cos(t * y_lobes)); } // z coordinate of camera and light (tiles live at z=0) const float cz = 3.5; // set light pos in 3D vec3 lpos = vec3(lcoord * 0.5 * iResolution.xy * scl, cz); // camera pos in 3D const vec3 cpos = vec3(0, 0, cz); // map frag cords to scene coords (before global rotation) vec2 porig = (fragCoord + vec2(0.13, 0.17) - 0.5*iResolution.xy) * scl; // apply global rotation vec2 p = rotate(porig, global_rot); // find starting origin of tile cluster -- note this could change below O = floor(p + 0.5); // figure out which quadrant we are in relative to the origin ivec2 qstep = ivec2(step(p, O)); int quadrant = (qstep.x ^ qstep.y) + 2*qstep.y; // each quadrant rotates by 90 degrees vec2 rvec = ROT_VECTORS[quadrant]; // form some critical points of the polygon in this cell vec2 xy = 0.5*uv; vec2 st = 0.5 - xy; A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); vec2 X = O + rotate(rvec, vec2(0.5, 0)); // get distance from point to semgent AX float dline = dseg(p, A, X); // figure out whether we are in the main upper-left part of the // cell or one of the two triangles int cell = quadrant; if (in_triangle(p, X, B, A)) { // in triangle XBA -- rotate polygon CCW by 90 degrees and translate it over by 1 cell cell = (quadrant + 1) & 3; O += rvec; rvec = perp(rvec); } else if (in_triangle(p, O, X, A)) { // in trangle OXA -- rotate polygon CW by 90 degrees cell = (quadrant + 3) & 3; rvec = -perp(rvec); } // now we know which polygonal tile p is in, so get the distance to the // polygon A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); C = B + rotate(rvec, perp(st)); D = O + rotate(rvec, perp(xy)); vec2 hm = heightmap(p); const float h = 1e-3; const vec2 eps = vec2(h, 0); vec2 hgrad = (0.5 / h) * vec2( heightmap(p + eps.xy).x - heightmap(p - eps.xy).x, heightmap(p + eps.yx).x - heightmap(p - eps.yx).x ); hgrad = unrotate(global_rot, hgrad); float z = hm.x; dline = min(dline, hm.y); // bump-mapped surface normal vec3 N = normalize(cross(vec3(1, 0, hgrad.x), vec3(0, 1, hgrad.y))); // get color of this cell vec3 color = CELL_COLORS[cell]; color *= color; // gamma un-correct // desaturate a bit color = mix(color, vec3(0.5), 0.08); // get 3D point position vec3 pos = vec3(porig, z); // fake phong lighting vec3 L = normalize(lpos - pos); vec3 V = -normalize(cpos - pos); vec3 R = reflect(L, N); color *= 0.1 + 0.9 * clamp(dot(N, L), 0.0, 1.0); color += 0.3*pow(clamp(dot(V, R), 0.0, 1.0), 10.0)*vec3(1); // darken by lines color *= smoothstep(0.0, 0.0125, dline); // gamma "correct" color = sqrt(color); // done! fragColor = vec4(color, 1); }
cc-by-4.0
[ 1083, 1119, 1138, 1138, 1168 ]
[ [ 1083, 1119, 1138, 1138, 1168 ], [ 1170, 1223, 1255, 1255, 1310 ], [ 1312, 1365, 1399, 1399, 1457 ], [ 1459, 1498, 1534, 1534, 1700 ], [ 1702, 1721, 1765, 1765, 1845 ], [ 1847, 1868, 1918, 1918, 2007 ], [ 2009, 2078, 2100, 2155, 2507 ], [ 2509, 2555, 2581, 2581, 2976 ], [ 4232, 4259, 4283, 4312, 4931 ], [ 4933, 4951, 5008, 5046, 9379 ] ]
// vector rotated by 90 degrees CCW
vec2 perp(vec2 u) {
return vec2(-u.y, u.x); }
// vector rotated by 90 degrees CCW vec2 perp(vec2 u) {
2
2
fs3SWj
mattz
2021-10-12T00:52:06
////////////////////////////////////////////////////////////////////// // // "pentagonal tiling variations" by mattz // License https://creativecommons.org/licenses/by/4.0/ // // Click and drag to set vertex position. // // Renders "type 4" Cairo pentagonal tilings. See // https://en.wikipedia.org/wiki/Cairo_pentagonal_tiling // for details. // // Inspired by https://twitter.com/cs_kaplan // // Related shaders: // // - "Cairo tiling" by nimitz // https://www.shadertoy.com/view/4ssSWf // // - "More Cairo Tiles" by mla // https://www.shadertoy.com/view/MlSfRd // // - "Extruded Pentagon Tiling" by Shane // https://www.shadertoy.com/view/3t2cDK // // - "15th Pentagonal tiling" by tomkh // https://www.shadertoy.com/view/4lBXRV // // - "pentagonal tiling" by FabriceNeyret2 // https://www.shadertoy.com/view/ltBBzK // (golfed at https://www.shadertoy.com/view/XljfRV) // // Noise function from iq's "Noise - gradient - 2D" // https://www.shadertoy.com/view/XdXGW8 // ////////////////////////////////////////////////////////////////////// // vector rotated by 90 degrees CCW vec2 perp(vec2 u) { return vec2(-u.y, u.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 rotate(vec2 rvec, vec2 p) { return p.x * rvec + p.y * vec2(-rvec.y, rvec.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 unrotate(vec2 rvec, vec2 p) { return p.x * vec2(rvec.x, -rvec.y) + p.y * rvec.yx; } // distance from point to line segment float dseg(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; float u = dot(pa, ba) / dot(ba, ba); u = clamp(u, 0.0, 1.0); return length(pa - u * ba); } // half-plane test bool in_half_plane(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; return dot(pa, perp(ba)) > 0.0; } // point in triangle bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) { return in_half_plane(p, a, b) && in_half_plane(p, b, c) && in_half_plane(p, c, a); } // from https://www.shadertoy.com/view/XdXGW8 - used for noise below vec2 grad( ivec2 z ) { // 2D to 1D (feel free to replace by some other) int n = z.x+z.y*11111; // Hugo Elias hash (feel free to replace by another one) n = (n<<13)^n; n = (n*(n*n*15731+789221)+1376312589)>>16; // Perlin style vectors n &= 7; vec2 gr = vec2(n&1,n>>1)*2.0-1.0; return ( n>=6 ) ? vec2(0.0,gr.x) : ( n>=4 ) ? vec2(gr.x,0.0) : gr; } // from https://www.shadertoy.com/view/XdXGW8 float noise( in vec2 p ) { ivec2 i = ivec2(floor( p )); vec2 f = fract( p ); vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); return mix( mix( dot( grad( i+ivec2(0,0) ), f-vec2(0.0,0.0) ), dot( grad( i+ivec2(1,0) ), f-vec2(1.0,0.0) ), u.x), mix( dot( grad( i+ivec2(0,1) ), f-vec2(0.0,1.0) ), dot( grad( i+ivec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y); } // colors for each cell const vec3 CELL_COLORS[4] = vec3[4]( vec3(0.9, 0.0, 0.05), vec3(0.95, 0.85, 0.0), vec3(0.1, 0.8, 0.05), vec3(0.1, 0.5, 0.8) ); // rotation vectors for theta = 0, pi/2, pi, 3*pi/2 const vec2 ROT_VECTORS[4] = vec2[4]( vec2(1, 0), vec2(0, 1), vec2(-1, 0), vec2(0, -1) ); // un-rotated cell geometry // // // C // _*_____ // _- -----____ // D _*------------------* B --- // _- | || // * | || // | | | | // | | | | t // | | | | // | | A | | // | | __* | --- // | | __-- \ | // || __-- \ | y // ||__-- \| // O *------------------* X --- // // | x | s | // // notes: // // square is 0.5 x 0.5 // // A = O + (x, y) // B = O + (0.5, 0.5) // C = B + (-t, s) = B + (y - 0.5, 0.5 - x) // D = O + (-y, x) // X = O + (0.5, 0) // // segments OD and OA are congruent and perpendicular // segments AB and BC are congruent and perpendicular // // there are four rotated copies of polygon OABCD around point O // polygon points vec2 O, A, B, C, D; // for bump-mapped shading vec2 heightmap(vec2 p) { // get polygon distance float dpoly = dseg(p, O, A); dpoly = min(dpoly, dseg(p, A, B)); dpoly = min(dpoly, dseg(p, B, C)); dpoly = min(dpoly, dseg(p, C, D)); dpoly = min(dpoly, dseg(p, D, O)); // offset from edge float k = 0.08; // base height float z = k + 0.01 * noise(5.*p); if (dpoly < k) { // semicircular shoulder float w = (dpoly/k - 1.0); z *= sqrt(1.0 - w*w); } else { // depression inwards from edge z *= (1.0 - 0.03*smoothstep(k, 2.0*k, dpoly)); } // return height and polygon distance return vec2(z, dpoly); } // do the things! void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // global rotation by 45 degrees vec2 global_rot = vec2(0.5*sqrt(2.0)); // image should be six blocks high float scl = 6.0 * global_rot.x / iResolution.y; // uv in [0,1] x [0, 1] holds vertex position vec2 uv = vec2(0.7886751345948132, 0.21132486540518713); // light coords relative to center vec2 lcoord = vec2(0); if (iMouse.z > 0.) { // set vertex coords by dragging - light is fixed uv = clamp(iMouse.xy / iResolution.xy, 0.0, 1.0); lcoord = vec2(-0.0, 0.5); } else { // set vertex coords varying over screen // and move light uv = (fragCoord.xy - 0.5 * iResolution.xy) / (max(iResolution.x, iResolution.y)); const float repeat = 30.0; // seconds const float wraps_per_repeat = 5.0; const float x_lobes = 3.0; const float y_lobes = 2.0; const float two_pi = 6.283185307179586; float t = iTime * two_pi / repeat; float t_wrap = t * wraps_per_repeat; float c = cos(t_wrap); float s = sin(t_wrap); uv = rotate(vec2(s, -c), uv); uv = clamp(uv + 0.5, 0.0, 1.0); lcoord = vec2(-sin(t * x_lobes), cos(t * y_lobes)); } // z coordinate of camera and light (tiles live at z=0) const float cz = 3.5; // set light pos in 3D vec3 lpos = vec3(lcoord * 0.5 * iResolution.xy * scl, cz); // camera pos in 3D const vec3 cpos = vec3(0, 0, cz); // map frag cords to scene coords (before global rotation) vec2 porig = (fragCoord + vec2(0.13, 0.17) - 0.5*iResolution.xy) * scl; // apply global rotation vec2 p = rotate(porig, global_rot); // find starting origin of tile cluster -- note this could change below O = floor(p + 0.5); // figure out which quadrant we are in relative to the origin ivec2 qstep = ivec2(step(p, O)); int quadrant = (qstep.x ^ qstep.y) + 2*qstep.y; // each quadrant rotates by 90 degrees vec2 rvec = ROT_VECTORS[quadrant]; // form some critical points of the polygon in this cell vec2 xy = 0.5*uv; vec2 st = 0.5 - xy; A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); vec2 X = O + rotate(rvec, vec2(0.5, 0)); // get distance from point to semgent AX float dline = dseg(p, A, X); // figure out whether we are in the main upper-left part of the // cell or one of the two triangles int cell = quadrant; if (in_triangle(p, X, B, A)) { // in triangle XBA -- rotate polygon CCW by 90 degrees and translate it over by 1 cell cell = (quadrant + 1) & 3; O += rvec; rvec = perp(rvec); } else if (in_triangle(p, O, X, A)) { // in trangle OXA -- rotate polygon CW by 90 degrees cell = (quadrant + 3) & 3; rvec = -perp(rvec); } // now we know which polygonal tile p is in, so get the distance to the // polygon A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); C = B + rotate(rvec, perp(st)); D = O + rotate(rvec, perp(xy)); vec2 hm = heightmap(p); const float h = 1e-3; const vec2 eps = vec2(h, 0); vec2 hgrad = (0.5 / h) * vec2( heightmap(p + eps.xy).x - heightmap(p - eps.xy).x, heightmap(p + eps.yx).x - heightmap(p - eps.yx).x ); hgrad = unrotate(global_rot, hgrad); float z = hm.x; dline = min(dline, hm.y); // bump-mapped surface normal vec3 N = normalize(cross(vec3(1, 0, hgrad.x), vec3(0, 1, hgrad.y))); // get color of this cell vec3 color = CELL_COLORS[cell]; color *= color; // gamma un-correct // desaturate a bit color = mix(color, vec3(0.5), 0.08); // get 3D point position vec3 pos = vec3(porig, z); // fake phong lighting vec3 L = normalize(lpos - pos); vec3 V = -normalize(cpos - pos); vec3 R = reflect(L, N); color *= 0.1 + 0.9 * clamp(dot(N, L), 0.0, 1.0); color += 0.3*pow(clamp(dot(V, R), 0.0, 1.0), 10.0)*vec3(1); // darken by lines color *= smoothstep(0.0, 0.0125, dline); // gamma "correct" color = sqrt(color); // done! fragColor = vec4(color, 1); }
cc-by-4.0
[ 1170, 1223, 1255, 1255, 1310 ]
[ [ 1083, 1119, 1138, 1138, 1168 ], [ 1170, 1223, 1255, 1255, 1310 ], [ 1312, 1365, 1399, 1399, 1457 ], [ 1459, 1498, 1534, 1534, 1700 ], [ 1702, 1721, 1765, 1765, 1845 ], [ 1847, 1868, 1918, 1918, 2007 ], [ 2009, 2078, 2100, 2155, 2507 ], [ 2509, 2555, 2581, 2581, 2976 ], [ 4232, 4259, 4283, 4312, 4931 ], [ 4933, 4951, 5008, 5046, 9379 ] ]
// rotate vector by rotation vector (cos(t), sin(t))
vec2 rotate(vec2 rvec, vec2 p) {
return p.x * rvec + p.y * vec2(-rvec.y, rvec.x); }
// rotate vector by rotation vector (cos(t), sin(t)) vec2 rotate(vec2 rvec, vec2 p) {
2
2
fs3SWj
mattz
2021-10-12T00:52:06
////////////////////////////////////////////////////////////////////// // // "pentagonal tiling variations" by mattz // License https://creativecommons.org/licenses/by/4.0/ // // Click and drag to set vertex position. // // Renders "type 4" Cairo pentagonal tilings. See // https://en.wikipedia.org/wiki/Cairo_pentagonal_tiling // for details. // // Inspired by https://twitter.com/cs_kaplan // // Related shaders: // // - "Cairo tiling" by nimitz // https://www.shadertoy.com/view/4ssSWf // // - "More Cairo Tiles" by mla // https://www.shadertoy.com/view/MlSfRd // // - "Extruded Pentagon Tiling" by Shane // https://www.shadertoy.com/view/3t2cDK // // - "15th Pentagonal tiling" by tomkh // https://www.shadertoy.com/view/4lBXRV // // - "pentagonal tiling" by FabriceNeyret2 // https://www.shadertoy.com/view/ltBBzK // (golfed at https://www.shadertoy.com/view/XljfRV) // // Noise function from iq's "Noise - gradient - 2D" // https://www.shadertoy.com/view/XdXGW8 // ////////////////////////////////////////////////////////////////////// // vector rotated by 90 degrees CCW vec2 perp(vec2 u) { return vec2(-u.y, u.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 rotate(vec2 rvec, vec2 p) { return p.x * rvec + p.y * vec2(-rvec.y, rvec.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 unrotate(vec2 rvec, vec2 p) { return p.x * vec2(rvec.x, -rvec.y) + p.y * rvec.yx; } // distance from point to line segment float dseg(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; float u = dot(pa, ba) / dot(ba, ba); u = clamp(u, 0.0, 1.0); return length(pa - u * ba); } // half-plane test bool in_half_plane(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; return dot(pa, perp(ba)) > 0.0; } // point in triangle bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) { return in_half_plane(p, a, b) && in_half_plane(p, b, c) && in_half_plane(p, c, a); } // from https://www.shadertoy.com/view/XdXGW8 - used for noise below vec2 grad( ivec2 z ) { // 2D to 1D (feel free to replace by some other) int n = z.x+z.y*11111; // Hugo Elias hash (feel free to replace by another one) n = (n<<13)^n; n = (n*(n*n*15731+789221)+1376312589)>>16; // Perlin style vectors n &= 7; vec2 gr = vec2(n&1,n>>1)*2.0-1.0; return ( n>=6 ) ? vec2(0.0,gr.x) : ( n>=4 ) ? vec2(gr.x,0.0) : gr; } // from https://www.shadertoy.com/view/XdXGW8 float noise( in vec2 p ) { ivec2 i = ivec2(floor( p )); vec2 f = fract( p ); vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); return mix( mix( dot( grad( i+ivec2(0,0) ), f-vec2(0.0,0.0) ), dot( grad( i+ivec2(1,0) ), f-vec2(1.0,0.0) ), u.x), mix( dot( grad( i+ivec2(0,1) ), f-vec2(0.0,1.0) ), dot( grad( i+ivec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y); } // colors for each cell const vec3 CELL_COLORS[4] = vec3[4]( vec3(0.9, 0.0, 0.05), vec3(0.95, 0.85, 0.0), vec3(0.1, 0.8, 0.05), vec3(0.1, 0.5, 0.8) ); // rotation vectors for theta = 0, pi/2, pi, 3*pi/2 const vec2 ROT_VECTORS[4] = vec2[4]( vec2(1, 0), vec2(0, 1), vec2(-1, 0), vec2(0, -1) ); // un-rotated cell geometry // // // C // _*_____ // _- -----____ // D _*------------------* B --- // _- | || // * | || // | | | | // | | | | t // | | | | // | | A | | // | | __* | --- // | | __-- \ | // || __-- \ | y // ||__-- \| // O *------------------* X --- // // | x | s | // // notes: // // square is 0.5 x 0.5 // // A = O + (x, y) // B = O + (0.5, 0.5) // C = B + (-t, s) = B + (y - 0.5, 0.5 - x) // D = O + (-y, x) // X = O + (0.5, 0) // // segments OD and OA are congruent and perpendicular // segments AB and BC are congruent and perpendicular // // there are four rotated copies of polygon OABCD around point O // polygon points vec2 O, A, B, C, D; // for bump-mapped shading vec2 heightmap(vec2 p) { // get polygon distance float dpoly = dseg(p, O, A); dpoly = min(dpoly, dseg(p, A, B)); dpoly = min(dpoly, dseg(p, B, C)); dpoly = min(dpoly, dseg(p, C, D)); dpoly = min(dpoly, dseg(p, D, O)); // offset from edge float k = 0.08; // base height float z = k + 0.01 * noise(5.*p); if (dpoly < k) { // semicircular shoulder float w = (dpoly/k - 1.0); z *= sqrt(1.0 - w*w); } else { // depression inwards from edge z *= (1.0 - 0.03*smoothstep(k, 2.0*k, dpoly)); } // return height and polygon distance return vec2(z, dpoly); } // do the things! void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // global rotation by 45 degrees vec2 global_rot = vec2(0.5*sqrt(2.0)); // image should be six blocks high float scl = 6.0 * global_rot.x / iResolution.y; // uv in [0,1] x [0, 1] holds vertex position vec2 uv = vec2(0.7886751345948132, 0.21132486540518713); // light coords relative to center vec2 lcoord = vec2(0); if (iMouse.z > 0.) { // set vertex coords by dragging - light is fixed uv = clamp(iMouse.xy / iResolution.xy, 0.0, 1.0); lcoord = vec2(-0.0, 0.5); } else { // set vertex coords varying over screen // and move light uv = (fragCoord.xy - 0.5 * iResolution.xy) / (max(iResolution.x, iResolution.y)); const float repeat = 30.0; // seconds const float wraps_per_repeat = 5.0; const float x_lobes = 3.0; const float y_lobes = 2.0; const float two_pi = 6.283185307179586; float t = iTime * two_pi / repeat; float t_wrap = t * wraps_per_repeat; float c = cos(t_wrap); float s = sin(t_wrap); uv = rotate(vec2(s, -c), uv); uv = clamp(uv + 0.5, 0.0, 1.0); lcoord = vec2(-sin(t * x_lobes), cos(t * y_lobes)); } // z coordinate of camera and light (tiles live at z=0) const float cz = 3.5; // set light pos in 3D vec3 lpos = vec3(lcoord * 0.5 * iResolution.xy * scl, cz); // camera pos in 3D const vec3 cpos = vec3(0, 0, cz); // map frag cords to scene coords (before global rotation) vec2 porig = (fragCoord + vec2(0.13, 0.17) - 0.5*iResolution.xy) * scl; // apply global rotation vec2 p = rotate(porig, global_rot); // find starting origin of tile cluster -- note this could change below O = floor(p + 0.5); // figure out which quadrant we are in relative to the origin ivec2 qstep = ivec2(step(p, O)); int quadrant = (qstep.x ^ qstep.y) + 2*qstep.y; // each quadrant rotates by 90 degrees vec2 rvec = ROT_VECTORS[quadrant]; // form some critical points of the polygon in this cell vec2 xy = 0.5*uv; vec2 st = 0.5 - xy; A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); vec2 X = O + rotate(rvec, vec2(0.5, 0)); // get distance from point to semgent AX float dline = dseg(p, A, X); // figure out whether we are in the main upper-left part of the // cell or one of the two triangles int cell = quadrant; if (in_triangle(p, X, B, A)) { // in triangle XBA -- rotate polygon CCW by 90 degrees and translate it over by 1 cell cell = (quadrant + 1) & 3; O += rvec; rvec = perp(rvec); } else if (in_triangle(p, O, X, A)) { // in trangle OXA -- rotate polygon CW by 90 degrees cell = (quadrant + 3) & 3; rvec = -perp(rvec); } // now we know which polygonal tile p is in, so get the distance to the // polygon A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); C = B + rotate(rvec, perp(st)); D = O + rotate(rvec, perp(xy)); vec2 hm = heightmap(p); const float h = 1e-3; const vec2 eps = vec2(h, 0); vec2 hgrad = (0.5 / h) * vec2( heightmap(p + eps.xy).x - heightmap(p - eps.xy).x, heightmap(p + eps.yx).x - heightmap(p - eps.yx).x ); hgrad = unrotate(global_rot, hgrad); float z = hm.x; dline = min(dline, hm.y); // bump-mapped surface normal vec3 N = normalize(cross(vec3(1, 0, hgrad.x), vec3(0, 1, hgrad.y))); // get color of this cell vec3 color = CELL_COLORS[cell]; color *= color; // gamma un-correct // desaturate a bit color = mix(color, vec3(0.5), 0.08); // get 3D point position vec3 pos = vec3(porig, z); // fake phong lighting vec3 L = normalize(lpos - pos); vec3 V = -normalize(cpos - pos); vec3 R = reflect(L, N); color *= 0.1 + 0.9 * clamp(dot(N, L), 0.0, 1.0); color += 0.3*pow(clamp(dot(V, R), 0.0, 1.0), 10.0)*vec3(1); // darken by lines color *= smoothstep(0.0, 0.0125, dline); // gamma "correct" color = sqrt(color); // done! fragColor = vec4(color, 1); }
cc-by-4.0
[ 1312, 1365, 1399, 1399, 1457 ]
[ [ 1083, 1119, 1138, 1138, 1168 ], [ 1170, 1223, 1255, 1255, 1310 ], [ 1312, 1365, 1399, 1399, 1457 ], [ 1459, 1498, 1534, 1534, 1700 ], [ 1702, 1721, 1765, 1765, 1845 ], [ 1847, 1868, 1918, 1918, 2007 ], [ 2009, 2078, 2100, 2155, 2507 ], [ 2509, 2555, 2581, 2581, 2976 ], [ 4232, 4259, 4283, 4312, 4931 ], [ 4933, 4951, 5008, 5046, 9379 ] ]
// rotate vector by rotation vector (cos(t), sin(t))
vec2 unrotate(vec2 rvec, vec2 p) {
return p.x * vec2(rvec.x, -rvec.y) + p.y * rvec.yx; }
// rotate vector by rotation vector (cos(t), sin(t)) vec2 unrotate(vec2 rvec, vec2 p) {
1
2
fs3SWj
mattz
2021-10-12T00:52:06
////////////////////////////////////////////////////////////////////// // // "pentagonal tiling variations" by mattz // License https://creativecommons.org/licenses/by/4.0/ // // Click and drag to set vertex position. // // Renders "type 4" Cairo pentagonal tilings. See // https://en.wikipedia.org/wiki/Cairo_pentagonal_tiling // for details. // // Inspired by https://twitter.com/cs_kaplan // // Related shaders: // // - "Cairo tiling" by nimitz // https://www.shadertoy.com/view/4ssSWf // // - "More Cairo Tiles" by mla // https://www.shadertoy.com/view/MlSfRd // // - "Extruded Pentagon Tiling" by Shane // https://www.shadertoy.com/view/3t2cDK // // - "15th Pentagonal tiling" by tomkh // https://www.shadertoy.com/view/4lBXRV // // - "pentagonal tiling" by FabriceNeyret2 // https://www.shadertoy.com/view/ltBBzK // (golfed at https://www.shadertoy.com/view/XljfRV) // // Noise function from iq's "Noise - gradient - 2D" // https://www.shadertoy.com/view/XdXGW8 // ////////////////////////////////////////////////////////////////////// // vector rotated by 90 degrees CCW vec2 perp(vec2 u) { return vec2(-u.y, u.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 rotate(vec2 rvec, vec2 p) { return p.x * rvec + p.y * vec2(-rvec.y, rvec.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 unrotate(vec2 rvec, vec2 p) { return p.x * vec2(rvec.x, -rvec.y) + p.y * rvec.yx; } // distance from point to line segment float dseg(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; float u = dot(pa, ba) / dot(ba, ba); u = clamp(u, 0.0, 1.0); return length(pa - u * ba); } // half-plane test bool in_half_plane(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; return dot(pa, perp(ba)) > 0.0; } // point in triangle bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) { return in_half_plane(p, a, b) && in_half_plane(p, b, c) && in_half_plane(p, c, a); } // from https://www.shadertoy.com/view/XdXGW8 - used for noise below vec2 grad( ivec2 z ) { // 2D to 1D (feel free to replace by some other) int n = z.x+z.y*11111; // Hugo Elias hash (feel free to replace by another one) n = (n<<13)^n; n = (n*(n*n*15731+789221)+1376312589)>>16; // Perlin style vectors n &= 7; vec2 gr = vec2(n&1,n>>1)*2.0-1.0; return ( n>=6 ) ? vec2(0.0,gr.x) : ( n>=4 ) ? vec2(gr.x,0.0) : gr; } // from https://www.shadertoy.com/view/XdXGW8 float noise( in vec2 p ) { ivec2 i = ivec2(floor( p )); vec2 f = fract( p ); vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); return mix( mix( dot( grad( i+ivec2(0,0) ), f-vec2(0.0,0.0) ), dot( grad( i+ivec2(1,0) ), f-vec2(1.0,0.0) ), u.x), mix( dot( grad( i+ivec2(0,1) ), f-vec2(0.0,1.0) ), dot( grad( i+ivec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y); } // colors for each cell const vec3 CELL_COLORS[4] = vec3[4]( vec3(0.9, 0.0, 0.05), vec3(0.95, 0.85, 0.0), vec3(0.1, 0.8, 0.05), vec3(0.1, 0.5, 0.8) ); // rotation vectors for theta = 0, pi/2, pi, 3*pi/2 const vec2 ROT_VECTORS[4] = vec2[4]( vec2(1, 0), vec2(0, 1), vec2(-1, 0), vec2(0, -1) ); // un-rotated cell geometry // // // C // _*_____ // _- -----____ // D _*------------------* B --- // _- | || // * | || // | | | | // | | | | t // | | | | // | | A | | // | | __* | --- // | | __-- \ | // || __-- \ | y // ||__-- \| // O *------------------* X --- // // | x | s | // // notes: // // square is 0.5 x 0.5 // // A = O + (x, y) // B = O + (0.5, 0.5) // C = B + (-t, s) = B + (y - 0.5, 0.5 - x) // D = O + (-y, x) // X = O + (0.5, 0) // // segments OD and OA are congruent and perpendicular // segments AB and BC are congruent and perpendicular // // there are four rotated copies of polygon OABCD around point O // polygon points vec2 O, A, B, C, D; // for bump-mapped shading vec2 heightmap(vec2 p) { // get polygon distance float dpoly = dseg(p, O, A); dpoly = min(dpoly, dseg(p, A, B)); dpoly = min(dpoly, dseg(p, B, C)); dpoly = min(dpoly, dseg(p, C, D)); dpoly = min(dpoly, dseg(p, D, O)); // offset from edge float k = 0.08; // base height float z = k + 0.01 * noise(5.*p); if (dpoly < k) { // semicircular shoulder float w = (dpoly/k - 1.0); z *= sqrt(1.0 - w*w); } else { // depression inwards from edge z *= (1.0 - 0.03*smoothstep(k, 2.0*k, dpoly)); } // return height and polygon distance return vec2(z, dpoly); } // do the things! void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // global rotation by 45 degrees vec2 global_rot = vec2(0.5*sqrt(2.0)); // image should be six blocks high float scl = 6.0 * global_rot.x / iResolution.y; // uv in [0,1] x [0, 1] holds vertex position vec2 uv = vec2(0.7886751345948132, 0.21132486540518713); // light coords relative to center vec2 lcoord = vec2(0); if (iMouse.z > 0.) { // set vertex coords by dragging - light is fixed uv = clamp(iMouse.xy / iResolution.xy, 0.0, 1.0); lcoord = vec2(-0.0, 0.5); } else { // set vertex coords varying over screen // and move light uv = (fragCoord.xy - 0.5 * iResolution.xy) / (max(iResolution.x, iResolution.y)); const float repeat = 30.0; // seconds const float wraps_per_repeat = 5.0; const float x_lobes = 3.0; const float y_lobes = 2.0; const float two_pi = 6.283185307179586; float t = iTime * two_pi / repeat; float t_wrap = t * wraps_per_repeat; float c = cos(t_wrap); float s = sin(t_wrap); uv = rotate(vec2(s, -c), uv); uv = clamp(uv + 0.5, 0.0, 1.0); lcoord = vec2(-sin(t * x_lobes), cos(t * y_lobes)); } // z coordinate of camera and light (tiles live at z=0) const float cz = 3.5; // set light pos in 3D vec3 lpos = vec3(lcoord * 0.5 * iResolution.xy * scl, cz); // camera pos in 3D const vec3 cpos = vec3(0, 0, cz); // map frag cords to scene coords (before global rotation) vec2 porig = (fragCoord + vec2(0.13, 0.17) - 0.5*iResolution.xy) * scl; // apply global rotation vec2 p = rotate(porig, global_rot); // find starting origin of tile cluster -- note this could change below O = floor(p + 0.5); // figure out which quadrant we are in relative to the origin ivec2 qstep = ivec2(step(p, O)); int quadrant = (qstep.x ^ qstep.y) + 2*qstep.y; // each quadrant rotates by 90 degrees vec2 rvec = ROT_VECTORS[quadrant]; // form some critical points of the polygon in this cell vec2 xy = 0.5*uv; vec2 st = 0.5 - xy; A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); vec2 X = O + rotate(rvec, vec2(0.5, 0)); // get distance from point to semgent AX float dline = dseg(p, A, X); // figure out whether we are in the main upper-left part of the // cell or one of the two triangles int cell = quadrant; if (in_triangle(p, X, B, A)) { // in triangle XBA -- rotate polygon CCW by 90 degrees and translate it over by 1 cell cell = (quadrant + 1) & 3; O += rvec; rvec = perp(rvec); } else if (in_triangle(p, O, X, A)) { // in trangle OXA -- rotate polygon CW by 90 degrees cell = (quadrant + 3) & 3; rvec = -perp(rvec); } // now we know which polygonal tile p is in, so get the distance to the // polygon A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); C = B + rotate(rvec, perp(st)); D = O + rotate(rvec, perp(xy)); vec2 hm = heightmap(p); const float h = 1e-3; const vec2 eps = vec2(h, 0); vec2 hgrad = (0.5 / h) * vec2( heightmap(p + eps.xy).x - heightmap(p - eps.xy).x, heightmap(p + eps.yx).x - heightmap(p - eps.yx).x ); hgrad = unrotate(global_rot, hgrad); float z = hm.x; dline = min(dline, hm.y); // bump-mapped surface normal vec3 N = normalize(cross(vec3(1, 0, hgrad.x), vec3(0, 1, hgrad.y))); // get color of this cell vec3 color = CELL_COLORS[cell]; color *= color; // gamma un-correct // desaturate a bit color = mix(color, vec3(0.5), 0.08); // get 3D point position vec3 pos = vec3(porig, z); // fake phong lighting vec3 L = normalize(lpos - pos); vec3 V = -normalize(cpos - pos); vec3 R = reflect(L, N); color *= 0.1 + 0.9 * clamp(dot(N, L), 0.0, 1.0); color += 0.3*pow(clamp(dot(V, R), 0.0, 1.0), 10.0)*vec3(1); // darken by lines color *= smoothstep(0.0, 0.0125, dline); // gamma "correct" color = sqrt(color); // done! fragColor = vec4(color, 1); }
cc-by-4.0
[ 1459, 1498, 1534, 1534, 1700 ]
[ [ 1083, 1119, 1138, 1138, 1168 ], [ 1170, 1223, 1255, 1255, 1310 ], [ 1312, 1365, 1399, 1399, 1457 ], [ 1459, 1498, 1534, 1534, 1700 ], [ 1702, 1721, 1765, 1765, 1845 ], [ 1847, 1868, 1918, 1918, 2007 ], [ 2009, 2078, 2100, 2155, 2507 ], [ 2509, 2555, 2581, 2581, 2976 ], [ 4232, 4259, 4283, 4312, 4931 ], [ 4933, 4951, 5008, 5046, 9379 ] ]
// distance from point to line segment
float dseg(vec2 p, vec2 a, vec2 b) {
vec2 pa = p - a; vec2 ba = b - a; float u = dot(pa, ba) / dot(ba, ba); u = clamp(u, 0.0, 1.0); return length(pa - u * ba); }
// distance from point to line segment float dseg(vec2 p, vec2 a, vec2 b) {
2
2
fs3SWj
mattz
2021-10-12T00:52:06
////////////////////////////////////////////////////////////////////// // // "pentagonal tiling variations" by mattz // License https://creativecommons.org/licenses/by/4.0/ // // Click and drag to set vertex position. // // Renders "type 4" Cairo pentagonal tilings. See // https://en.wikipedia.org/wiki/Cairo_pentagonal_tiling // for details. // // Inspired by https://twitter.com/cs_kaplan // // Related shaders: // // - "Cairo tiling" by nimitz // https://www.shadertoy.com/view/4ssSWf // // - "More Cairo Tiles" by mla // https://www.shadertoy.com/view/MlSfRd // // - "Extruded Pentagon Tiling" by Shane // https://www.shadertoy.com/view/3t2cDK // // - "15th Pentagonal tiling" by tomkh // https://www.shadertoy.com/view/4lBXRV // // - "pentagonal tiling" by FabriceNeyret2 // https://www.shadertoy.com/view/ltBBzK // (golfed at https://www.shadertoy.com/view/XljfRV) // // Noise function from iq's "Noise - gradient - 2D" // https://www.shadertoy.com/view/XdXGW8 // ////////////////////////////////////////////////////////////////////// // vector rotated by 90 degrees CCW vec2 perp(vec2 u) { return vec2(-u.y, u.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 rotate(vec2 rvec, vec2 p) { return p.x * rvec + p.y * vec2(-rvec.y, rvec.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 unrotate(vec2 rvec, vec2 p) { return p.x * vec2(rvec.x, -rvec.y) + p.y * rvec.yx; } // distance from point to line segment float dseg(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; float u = dot(pa, ba) / dot(ba, ba); u = clamp(u, 0.0, 1.0); return length(pa - u * ba); } // half-plane test bool in_half_plane(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; return dot(pa, perp(ba)) > 0.0; } // point in triangle bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) { return in_half_plane(p, a, b) && in_half_plane(p, b, c) && in_half_plane(p, c, a); } // from https://www.shadertoy.com/view/XdXGW8 - used for noise below vec2 grad( ivec2 z ) { // 2D to 1D (feel free to replace by some other) int n = z.x+z.y*11111; // Hugo Elias hash (feel free to replace by another one) n = (n<<13)^n; n = (n*(n*n*15731+789221)+1376312589)>>16; // Perlin style vectors n &= 7; vec2 gr = vec2(n&1,n>>1)*2.0-1.0; return ( n>=6 ) ? vec2(0.0,gr.x) : ( n>=4 ) ? vec2(gr.x,0.0) : gr; } // from https://www.shadertoy.com/view/XdXGW8 float noise( in vec2 p ) { ivec2 i = ivec2(floor( p )); vec2 f = fract( p ); vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); return mix( mix( dot( grad( i+ivec2(0,0) ), f-vec2(0.0,0.0) ), dot( grad( i+ivec2(1,0) ), f-vec2(1.0,0.0) ), u.x), mix( dot( grad( i+ivec2(0,1) ), f-vec2(0.0,1.0) ), dot( grad( i+ivec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y); } // colors for each cell const vec3 CELL_COLORS[4] = vec3[4]( vec3(0.9, 0.0, 0.05), vec3(0.95, 0.85, 0.0), vec3(0.1, 0.8, 0.05), vec3(0.1, 0.5, 0.8) ); // rotation vectors for theta = 0, pi/2, pi, 3*pi/2 const vec2 ROT_VECTORS[4] = vec2[4]( vec2(1, 0), vec2(0, 1), vec2(-1, 0), vec2(0, -1) ); // un-rotated cell geometry // // // C // _*_____ // _- -----____ // D _*------------------* B --- // _- | || // * | || // | | | | // | | | | t // | | | | // | | A | | // | | __* | --- // | | __-- \ | // || __-- \ | y // ||__-- \| // O *------------------* X --- // // | x | s | // // notes: // // square is 0.5 x 0.5 // // A = O + (x, y) // B = O + (0.5, 0.5) // C = B + (-t, s) = B + (y - 0.5, 0.5 - x) // D = O + (-y, x) // X = O + (0.5, 0) // // segments OD and OA are congruent and perpendicular // segments AB and BC are congruent and perpendicular // // there are four rotated copies of polygon OABCD around point O // polygon points vec2 O, A, B, C, D; // for bump-mapped shading vec2 heightmap(vec2 p) { // get polygon distance float dpoly = dseg(p, O, A); dpoly = min(dpoly, dseg(p, A, B)); dpoly = min(dpoly, dseg(p, B, C)); dpoly = min(dpoly, dseg(p, C, D)); dpoly = min(dpoly, dseg(p, D, O)); // offset from edge float k = 0.08; // base height float z = k + 0.01 * noise(5.*p); if (dpoly < k) { // semicircular shoulder float w = (dpoly/k - 1.0); z *= sqrt(1.0 - w*w); } else { // depression inwards from edge z *= (1.0 - 0.03*smoothstep(k, 2.0*k, dpoly)); } // return height and polygon distance return vec2(z, dpoly); } // do the things! void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // global rotation by 45 degrees vec2 global_rot = vec2(0.5*sqrt(2.0)); // image should be six blocks high float scl = 6.0 * global_rot.x / iResolution.y; // uv in [0,1] x [0, 1] holds vertex position vec2 uv = vec2(0.7886751345948132, 0.21132486540518713); // light coords relative to center vec2 lcoord = vec2(0); if (iMouse.z > 0.) { // set vertex coords by dragging - light is fixed uv = clamp(iMouse.xy / iResolution.xy, 0.0, 1.0); lcoord = vec2(-0.0, 0.5); } else { // set vertex coords varying over screen // and move light uv = (fragCoord.xy - 0.5 * iResolution.xy) / (max(iResolution.x, iResolution.y)); const float repeat = 30.0; // seconds const float wraps_per_repeat = 5.0; const float x_lobes = 3.0; const float y_lobes = 2.0; const float two_pi = 6.283185307179586; float t = iTime * two_pi / repeat; float t_wrap = t * wraps_per_repeat; float c = cos(t_wrap); float s = sin(t_wrap); uv = rotate(vec2(s, -c), uv); uv = clamp(uv + 0.5, 0.0, 1.0); lcoord = vec2(-sin(t * x_lobes), cos(t * y_lobes)); } // z coordinate of camera and light (tiles live at z=0) const float cz = 3.5; // set light pos in 3D vec3 lpos = vec3(lcoord * 0.5 * iResolution.xy * scl, cz); // camera pos in 3D const vec3 cpos = vec3(0, 0, cz); // map frag cords to scene coords (before global rotation) vec2 porig = (fragCoord + vec2(0.13, 0.17) - 0.5*iResolution.xy) * scl; // apply global rotation vec2 p = rotate(porig, global_rot); // find starting origin of tile cluster -- note this could change below O = floor(p + 0.5); // figure out which quadrant we are in relative to the origin ivec2 qstep = ivec2(step(p, O)); int quadrant = (qstep.x ^ qstep.y) + 2*qstep.y; // each quadrant rotates by 90 degrees vec2 rvec = ROT_VECTORS[quadrant]; // form some critical points of the polygon in this cell vec2 xy = 0.5*uv; vec2 st = 0.5 - xy; A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); vec2 X = O + rotate(rvec, vec2(0.5, 0)); // get distance from point to semgent AX float dline = dseg(p, A, X); // figure out whether we are in the main upper-left part of the // cell or one of the two triangles int cell = quadrant; if (in_triangle(p, X, B, A)) { // in triangle XBA -- rotate polygon CCW by 90 degrees and translate it over by 1 cell cell = (quadrant + 1) & 3; O += rvec; rvec = perp(rvec); } else if (in_triangle(p, O, X, A)) { // in trangle OXA -- rotate polygon CW by 90 degrees cell = (quadrant + 3) & 3; rvec = -perp(rvec); } // now we know which polygonal tile p is in, so get the distance to the // polygon A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); C = B + rotate(rvec, perp(st)); D = O + rotate(rvec, perp(xy)); vec2 hm = heightmap(p); const float h = 1e-3; const vec2 eps = vec2(h, 0); vec2 hgrad = (0.5 / h) * vec2( heightmap(p + eps.xy).x - heightmap(p - eps.xy).x, heightmap(p + eps.yx).x - heightmap(p - eps.yx).x ); hgrad = unrotate(global_rot, hgrad); float z = hm.x; dline = min(dline, hm.y); // bump-mapped surface normal vec3 N = normalize(cross(vec3(1, 0, hgrad.x), vec3(0, 1, hgrad.y))); // get color of this cell vec3 color = CELL_COLORS[cell]; color *= color; // gamma un-correct // desaturate a bit color = mix(color, vec3(0.5), 0.08); // get 3D point position vec3 pos = vec3(porig, z); // fake phong lighting vec3 L = normalize(lpos - pos); vec3 V = -normalize(cpos - pos); vec3 R = reflect(L, N); color *= 0.1 + 0.9 * clamp(dot(N, L), 0.0, 1.0); color += 0.3*pow(clamp(dot(V, R), 0.0, 1.0), 10.0)*vec3(1); // darken by lines color *= smoothstep(0.0, 0.0125, dline); // gamma "correct" color = sqrt(color); // done! fragColor = vec4(color, 1); }
cc-by-4.0
[ 1702, 1721, 1765, 1765, 1845 ]
[ [ 1083, 1119, 1138, 1138, 1168 ], [ 1170, 1223, 1255, 1255, 1310 ], [ 1312, 1365, 1399, 1399, 1457 ], [ 1459, 1498, 1534, 1534, 1700 ], [ 1702, 1721, 1765, 1765, 1845 ], [ 1847, 1868, 1918, 1918, 2007 ], [ 2009, 2078, 2100, 2155, 2507 ], [ 2509, 2555, 2581, 2581, 2976 ], [ 4232, 4259, 4283, 4312, 4931 ], [ 4933, 4951, 5008, 5046, 9379 ] ]
// half-plane test
bool in_half_plane(vec2 p, vec2 a, vec2 b) {
vec2 pa = p - a; vec2 ba = b - a; return dot(pa, perp(ba)) > 0.0; }
// half-plane test bool in_half_plane(vec2 p, vec2 a, vec2 b) {
2
2
fs3SWj
mattz
2021-10-12T00:52:06
////////////////////////////////////////////////////////////////////// // // "pentagonal tiling variations" by mattz // License https://creativecommons.org/licenses/by/4.0/ // // Click and drag to set vertex position. // // Renders "type 4" Cairo pentagonal tilings. See // https://en.wikipedia.org/wiki/Cairo_pentagonal_tiling // for details. // // Inspired by https://twitter.com/cs_kaplan // // Related shaders: // // - "Cairo tiling" by nimitz // https://www.shadertoy.com/view/4ssSWf // // - "More Cairo Tiles" by mla // https://www.shadertoy.com/view/MlSfRd // // - "Extruded Pentagon Tiling" by Shane // https://www.shadertoy.com/view/3t2cDK // // - "15th Pentagonal tiling" by tomkh // https://www.shadertoy.com/view/4lBXRV // // - "pentagonal tiling" by FabriceNeyret2 // https://www.shadertoy.com/view/ltBBzK // (golfed at https://www.shadertoy.com/view/XljfRV) // // Noise function from iq's "Noise - gradient - 2D" // https://www.shadertoy.com/view/XdXGW8 // ////////////////////////////////////////////////////////////////////// // vector rotated by 90 degrees CCW vec2 perp(vec2 u) { return vec2(-u.y, u.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 rotate(vec2 rvec, vec2 p) { return p.x * rvec + p.y * vec2(-rvec.y, rvec.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 unrotate(vec2 rvec, vec2 p) { return p.x * vec2(rvec.x, -rvec.y) + p.y * rvec.yx; } // distance from point to line segment float dseg(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; float u = dot(pa, ba) / dot(ba, ba); u = clamp(u, 0.0, 1.0); return length(pa - u * ba); } // half-plane test bool in_half_plane(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; return dot(pa, perp(ba)) > 0.0; } // point in triangle bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) { return in_half_plane(p, a, b) && in_half_plane(p, b, c) && in_half_plane(p, c, a); } // from https://www.shadertoy.com/view/XdXGW8 - used for noise below vec2 grad( ivec2 z ) { // 2D to 1D (feel free to replace by some other) int n = z.x+z.y*11111; // Hugo Elias hash (feel free to replace by another one) n = (n<<13)^n; n = (n*(n*n*15731+789221)+1376312589)>>16; // Perlin style vectors n &= 7; vec2 gr = vec2(n&1,n>>1)*2.0-1.0; return ( n>=6 ) ? vec2(0.0,gr.x) : ( n>=4 ) ? vec2(gr.x,0.0) : gr; } // from https://www.shadertoy.com/view/XdXGW8 float noise( in vec2 p ) { ivec2 i = ivec2(floor( p )); vec2 f = fract( p ); vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); return mix( mix( dot( grad( i+ivec2(0,0) ), f-vec2(0.0,0.0) ), dot( grad( i+ivec2(1,0) ), f-vec2(1.0,0.0) ), u.x), mix( dot( grad( i+ivec2(0,1) ), f-vec2(0.0,1.0) ), dot( grad( i+ivec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y); } // colors for each cell const vec3 CELL_COLORS[4] = vec3[4]( vec3(0.9, 0.0, 0.05), vec3(0.95, 0.85, 0.0), vec3(0.1, 0.8, 0.05), vec3(0.1, 0.5, 0.8) ); // rotation vectors for theta = 0, pi/2, pi, 3*pi/2 const vec2 ROT_VECTORS[4] = vec2[4]( vec2(1, 0), vec2(0, 1), vec2(-1, 0), vec2(0, -1) ); // un-rotated cell geometry // // // C // _*_____ // _- -----____ // D _*------------------* B --- // _- | || // * | || // | | | | // | | | | t // | | | | // | | A | | // | | __* | --- // | | __-- \ | // || __-- \ | y // ||__-- \| // O *------------------* X --- // // | x | s | // // notes: // // square is 0.5 x 0.5 // // A = O + (x, y) // B = O + (0.5, 0.5) // C = B + (-t, s) = B + (y - 0.5, 0.5 - x) // D = O + (-y, x) // X = O + (0.5, 0) // // segments OD and OA are congruent and perpendicular // segments AB and BC are congruent and perpendicular // // there are four rotated copies of polygon OABCD around point O // polygon points vec2 O, A, B, C, D; // for bump-mapped shading vec2 heightmap(vec2 p) { // get polygon distance float dpoly = dseg(p, O, A); dpoly = min(dpoly, dseg(p, A, B)); dpoly = min(dpoly, dseg(p, B, C)); dpoly = min(dpoly, dseg(p, C, D)); dpoly = min(dpoly, dseg(p, D, O)); // offset from edge float k = 0.08; // base height float z = k + 0.01 * noise(5.*p); if (dpoly < k) { // semicircular shoulder float w = (dpoly/k - 1.0); z *= sqrt(1.0 - w*w); } else { // depression inwards from edge z *= (1.0 - 0.03*smoothstep(k, 2.0*k, dpoly)); } // return height and polygon distance return vec2(z, dpoly); } // do the things! void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // global rotation by 45 degrees vec2 global_rot = vec2(0.5*sqrt(2.0)); // image should be six blocks high float scl = 6.0 * global_rot.x / iResolution.y; // uv in [0,1] x [0, 1] holds vertex position vec2 uv = vec2(0.7886751345948132, 0.21132486540518713); // light coords relative to center vec2 lcoord = vec2(0); if (iMouse.z > 0.) { // set vertex coords by dragging - light is fixed uv = clamp(iMouse.xy / iResolution.xy, 0.0, 1.0); lcoord = vec2(-0.0, 0.5); } else { // set vertex coords varying over screen // and move light uv = (fragCoord.xy - 0.5 * iResolution.xy) / (max(iResolution.x, iResolution.y)); const float repeat = 30.0; // seconds const float wraps_per_repeat = 5.0; const float x_lobes = 3.0; const float y_lobes = 2.0; const float two_pi = 6.283185307179586; float t = iTime * two_pi / repeat; float t_wrap = t * wraps_per_repeat; float c = cos(t_wrap); float s = sin(t_wrap); uv = rotate(vec2(s, -c), uv); uv = clamp(uv + 0.5, 0.0, 1.0); lcoord = vec2(-sin(t * x_lobes), cos(t * y_lobes)); } // z coordinate of camera and light (tiles live at z=0) const float cz = 3.5; // set light pos in 3D vec3 lpos = vec3(lcoord * 0.5 * iResolution.xy * scl, cz); // camera pos in 3D const vec3 cpos = vec3(0, 0, cz); // map frag cords to scene coords (before global rotation) vec2 porig = (fragCoord + vec2(0.13, 0.17) - 0.5*iResolution.xy) * scl; // apply global rotation vec2 p = rotate(porig, global_rot); // find starting origin of tile cluster -- note this could change below O = floor(p + 0.5); // figure out which quadrant we are in relative to the origin ivec2 qstep = ivec2(step(p, O)); int quadrant = (qstep.x ^ qstep.y) + 2*qstep.y; // each quadrant rotates by 90 degrees vec2 rvec = ROT_VECTORS[quadrant]; // form some critical points of the polygon in this cell vec2 xy = 0.5*uv; vec2 st = 0.5 - xy; A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); vec2 X = O + rotate(rvec, vec2(0.5, 0)); // get distance from point to semgent AX float dline = dseg(p, A, X); // figure out whether we are in the main upper-left part of the // cell or one of the two triangles int cell = quadrant; if (in_triangle(p, X, B, A)) { // in triangle XBA -- rotate polygon CCW by 90 degrees and translate it over by 1 cell cell = (quadrant + 1) & 3; O += rvec; rvec = perp(rvec); } else if (in_triangle(p, O, X, A)) { // in trangle OXA -- rotate polygon CW by 90 degrees cell = (quadrant + 3) & 3; rvec = -perp(rvec); } // now we know which polygonal tile p is in, so get the distance to the // polygon A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); C = B + rotate(rvec, perp(st)); D = O + rotate(rvec, perp(xy)); vec2 hm = heightmap(p); const float h = 1e-3; const vec2 eps = vec2(h, 0); vec2 hgrad = (0.5 / h) * vec2( heightmap(p + eps.xy).x - heightmap(p - eps.xy).x, heightmap(p + eps.yx).x - heightmap(p - eps.yx).x ); hgrad = unrotate(global_rot, hgrad); float z = hm.x; dline = min(dline, hm.y); // bump-mapped surface normal vec3 N = normalize(cross(vec3(1, 0, hgrad.x), vec3(0, 1, hgrad.y))); // get color of this cell vec3 color = CELL_COLORS[cell]; color *= color; // gamma un-correct // desaturate a bit color = mix(color, vec3(0.5), 0.08); // get 3D point position vec3 pos = vec3(porig, z); // fake phong lighting vec3 L = normalize(lpos - pos); vec3 V = -normalize(cpos - pos); vec3 R = reflect(L, N); color *= 0.1 + 0.9 * clamp(dot(N, L), 0.0, 1.0); color += 0.3*pow(clamp(dot(V, R), 0.0, 1.0), 10.0)*vec3(1); // darken by lines color *= smoothstep(0.0, 0.0125, dline); // gamma "correct" color = sqrt(color); // done! fragColor = vec4(color, 1); }
cc-by-4.0
[ 1847, 1868, 1918, 1918, 2007 ]
[ [ 1083, 1119, 1138, 1138, 1168 ], [ 1170, 1223, 1255, 1255, 1310 ], [ 1312, 1365, 1399, 1399, 1457 ], [ 1459, 1498, 1534, 1534, 1700 ], [ 1702, 1721, 1765, 1765, 1845 ], [ 1847, 1868, 1918, 1918, 2007 ], [ 2009, 2078, 2100, 2155, 2507 ], [ 2509, 2555, 2581, 2581, 2976 ], [ 4232, 4259, 4283, 4312, 4931 ], [ 4933, 4951, 5008, 5046, 9379 ] ]
// point in triangle
bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) {
return in_half_plane(p, a, b) && in_half_plane(p, b, c) && in_half_plane(p, c, a); }
// point in triangle bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) {
2
2
fs3SWj
mattz
2021-10-12T00:52:06
////////////////////////////////////////////////////////////////////// // // "pentagonal tiling variations" by mattz // License https://creativecommons.org/licenses/by/4.0/ // // Click and drag to set vertex position. // // Renders "type 4" Cairo pentagonal tilings. See // https://en.wikipedia.org/wiki/Cairo_pentagonal_tiling // for details. // // Inspired by https://twitter.com/cs_kaplan // // Related shaders: // // - "Cairo tiling" by nimitz // https://www.shadertoy.com/view/4ssSWf // // - "More Cairo Tiles" by mla // https://www.shadertoy.com/view/MlSfRd // // - "Extruded Pentagon Tiling" by Shane // https://www.shadertoy.com/view/3t2cDK // // - "15th Pentagonal tiling" by tomkh // https://www.shadertoy.com/view/4lBXRV // // - "pentagonal tiling" by FabriceNeyret2 // https://www.shadertoy.com/view/ltBBzK // (golfed at https://www.shadertoy.com/view/XljfRV) // // Noise function from iq's "Noise - gradient - 2D" // https://www.shadertoy.com/view/XdXGW8 // ////////////////////////////////////////////////////////////////////// // vector rotated by 90 degrees CCW vec2 perp(vec2 u) { return vec2(-u.y, u.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 rotate(vec2 rvec, vec2 p) { return p.x * rvec + p.y * vec2(-rvec.y, rvec.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 unrotate(vec2 rvec, vec2 p) { return p.x * vec2(rvec.x, -rvec.y) + p.y * rvec.yx; } // distance from point to line segment float dseg(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; float u = dot(pa, ba) / dot(ba, ba); u = clamp(u, 0.0, 1.0); return length(pa - u * ba); } // half-plane test bool in_half_plane(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; return dot(pa, perp(ba)) > 0.0; } // point in triangle bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) { return in_half_plane(p, a, b) && in_half_plane(p, b, c) && in_half_plane(p, c, a); } // from https://www.shadertoy.com/view/XdXGW8 - used for noise below vec2 grad( ivec2 z ) { // 2D to 1D (feel free to replace by some other) int n = z.x+z.y*11111; // Hugo Elias hash (feel free to replace by another one) n = (n<<13)^n; n = (n*(n*n*15731+789221)+1376312589)>>16; // Perlin style vectors n &= 7; vec2 gr = vec2(n&1,n>>1)*2.0-1.0; return ( n>=6 ) ? vec2(0.0,gr.x) : ( n>=4 ) ? vec2(gr.x,0.0) : gr; } // from https://www.shadertoy.com/view/XdXGW8 float noise( in vec2 p ) { ivec2 i = ivec2(floor( p )); vec2 f = fract( p ); vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); return mix( mix( dot( grad( i+ivec2(0,0) ), f-vec2(0.0,0.0) ), dot( grad( i+ivec2(1,0) ), f-vec2(1.0,0.0) ), u.x), mix( dot( grad( i+ivec2(0,1) ), f-vec2(0.0,1.0) ), dot( grad( i+ivec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y); } // colors for each cell const vec3 CELL_COLORS[4] = vec3[4]( vec3(0.9, 0.0, 0.05), vec3(0.95, 0.85, 0.0), vec3(0.1, 0.8, 0.05), vec3(0.1, 0.5, 0.8) ); // rotation vectors for theta = 0, pi/2, pi, 3*pi/2 const vec2 ROT_VECTORS[4] = vec2[4]( vec2(1, 0), vec2(0, 1), vec2(-1, 0), vec2(0, -1) ); // un-rotated cell geometry // // // C // _*_____ // _- -----____ // D _*------------------* B --- // _- | || // * | || // | | | | // | | | | t // | | | | // | | A | | // | | __* | --- // | | __-- \ | // || __-- \ | y // ||__-- \| // O *------------------* X --- // // | x | s | // // notes: // // square is 0.5 x 0.5 // // A = O + (x, y) // B = O + (0.5, 0.5) // C = B + (-t, s) = B + (y - 0.5, 0.5 - x) // D = O + (-y, x) // X = O + (0.5, 0) // // segments OD and OA are congruent and perpendicular // segments AB and BC are congruent and perpendicular // // there are four rotated copies of polygon OABCD around point O // polygon points vec2 O, A, B, C, D; // for bump-mapped shading vec2 heightmap(vec2 p) { // get polygon distance float dpoly = dseg(p, O, A); dpoly = min(dpoly, dseg(p, A, B)); dpoly = min(dpoly, dseg(p, B, C)); dpoly = min(dpoly, dseg(p, C, D)); dpoly = min(dpoly, dseg(p, D, O)); // offset from edge float k = 0.08; // base height float z = k + 0.01 * noise(5.*p); if (dpoly < k) { // semicircular shoulder float w = (dpoly/k - 1.0); z *= sqrt(1.0 - w*w); } else { // depression inwards from edge z *= (1.0 - 0.03*smoothstep(k, 2.0*k, dpoly)); } // return height and polygon distance return vec2(z, dpoly); } // do the things! void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // global rotation by 45 degrees vec2 global_rot = vec2(0.5*sqrt(2.0)); // image should be six blocks high float scl = 6.0 * global_rot.x / iResolution.y; // uv in [0,1] x [0, 1] holds vertex position vec2 uv = vec2(0.7886751345948132, 0.21132486540518713); // light coords relative to center vec2 lcoord = vec2(0); if (iMouse.z > 0.) { // set vertex coords by dragging - light is fixed uv = clamp(iMouse.xy / iResolution.xy, 0.0, 1.0); lcoord = vec2(-0.0, 0.5); } else { // set vertex coords varying over screen // and move light uv = (fragCoord.xy - 0.5 * iResolution.xy) / (max(iResolution.x, iResolution.y)); const float repeat = 30.0; // seconds const float wraps_per_repeat = 5.0; const float x_lobes = 3.0; const float y_lobes = 2.0; const float two_pi = 6.283185307179586; float t = iTime * two_pi / repeat; float t_wrap = t * wraps_per_repeat; float c = cos(t_wrap); float s = sin(t_wrap); uv = rotate(vec2(s, -c), uv); uv = clamp(uv + 0.5, 0.0, 1.0); lcoord = vec2(-sin(t * x_lobes), cos(t * y_lobes)); } // z coordinate of camera and light (tiles live at z=0) const float cz = 3.5; // set light pos in 3D vec3 lpos = vec3(lcoord * 0.5 * iResolution.xy * scl, cz); // camera pos in 3D const vec3 cpos = vec3(0, 0, cz); // map frag cords to scene coords (before global rotation) vec2 porig = (fragCoord + vec2(0.13, 0.17) - 0.5*iResolution.xy) * scl; // apply global rotation vec2 p = rotate(porig, global_rot); // find starting origin of tile cluster -- note this could change below O = floor(p + 0.5); // figure out which quadrant we are in relative to the origin ivec2 qstep = ivec2(step(p, O)); int quadrant = (qstep.x ^ qstep.y) + 2*qstep.y; // each quadrant rotates by 90 degrees vec2 rvec = ROT_VECTORS[quadrant]; // form some critical points of the polygon in this cell vec2 xy = 0.5*uv; vec2 st = 0.5 - xy; A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); vec2 X = O + rotate(rvec, vec2(0.5, 0)); // get distance from point to semgent AX float dline = dseg(p, A, X); // figure out whether we are in the main upper-left part of the // cell or one of the two triangles int cell = quadrant; if (in_triangle(p, X, B, A)) { // in triangle XBA -- rotate polygon CCW by 90 degrees and translate it over by 1 cell cell = (quadrant + 1) & 3; O += rvec; rvec = perp(rvec); } else if (in_triangle(p, O, X, A)) { // in trangle OXA -- rotate polygon CW by 90 degrees cell = (quadrant + 3) & 3; rvec = -perp(rvec); } // now we know which polygonal tile p is in, so get the distance to the // polygon A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); C = B + rotate(rvec, perp(st)); D = O + rotate(rvec, perp(xy)); vec2 hm = heightmap(p); const float h = 1e-3; const vec2 eps = vec2(h, 0); vec2 hgrad = (0.5 / h) * vec2( heightmap(p + eps.xy).x - heightmap(p - eps.xy).x, heightmap(p + eps.yx).x - heightmap(p - eps.yx).x ); hgrad = unrotate(global_rot, hgrad); float z = hm.x; dline = min(dline, hm.y); // bump-mapped surface normal vec3 N = normalize(cross(vec3(1, 0, hgrad.x), vec3(0, 1, hgrad.y))); // get color of this cell vec3 color = CELL_COLORS[cell]; color *= color; // gamma un-correct // desaturate a bit color = mix(color, vec3(0.5), 0.08); // get 3D point position vec3 pos = vec3(porig, z); // fake phong lighting vec3 L = normalize(lpos - pos); vec3 V = -normalize(cpos - pos); vec3 R = reflect(L, N); color *= 0.1 + 0.9 * clamp(dot(N, L), 0.0, 1.0); color += 0.3*pow(clamp(dot(V, R), 0.0, 1.0), 10.0)*vec3(1); // darken by lines color *= smoothstep(0.0, 0.0125, dline); // gamma "correct" color = sqrt(color); // done! fragColor = vec4(color, 1); }
cc-by-4.0
[ 2009, 2078, 2100, 2155, 2507 ]
[ [ 1083, 1119, 1138, 1138, 1168 ], [ 1170, 1223, 1255, 1255, 1310 ], [ 1312, 1365, 1399, 1399, 1457 ], [ 1459, 1498, 1534, 1534, 1700 ], [ 1702, 1721, 1765, 1765, 1845 ], [ 1847, 1868, 1918, 1918, 2007 ], [ 2009, 2078, 2100, 2155, 2507 ], [ 2509, 2555, 2581, 2581, 2976 ], [ 4232, 4259, 4283, 4312, 4931 ], [ 4933, 4951, 5008, 5046, 9379 ] ]
// from https://www.shadertoy.com/view/XdXGW8 - used for noise below
vec2 grad( ivec2 z ) {
int n = z.x+z.y*11111; // Hugo Elias hash (feel free to replace by another one) n = (n<<13)^n; n = (n*(n*n*15731+789221)+1376312589)>>16; // Perlin style vectors n &= 7; vec2 gr = vec2(n&1,n>>1)*2.0-1.0; return ( n>=6 ) ? vec2(0.0,gr.x) : ( n>=4 ) ? vec2(gr.x,0.0) : gr; }
// from https://www.shadertoy.com/view/XdXGW8 - used for noise below vec2 grad( ivec2 z ) {
2
2
fs3SWj
mattz
2021-10-12T00:52:06
////////////////////////////////////////////////////////////////////// // // "pentagonal tiling variations" by mattz // License https://creativecommons.org/licenses/by/4.0/ // // Click and drag to set vertex position. // // Renders "type 4" Cairo pentagonal tilings. See // https://en.wikipedia.org/wiki/Cairo_pentagonal_tiling // for details. // // Inspired by https://twitter.com/cs_kaplan // // Related shaders: // // - "Cairo tiling" by nimitz // https://www.shadertoy.com/view/4ssSWf // // - "More Cairo Tiles" by mla // https://www.shadertoy.com/view/MlSfRd // // - "Extruded Pentagon Tiling" by Shane // https://www.shadertoy.com/view/3t2cDK // // - "15th Pentagonal tiling" by tomkh // https://www.shadertoy.com/view/4lBXRV // // - "pentagonal tiling" by FabriceNeyret2 // https://www.shadertoy.com/view/ltBBzK // (golfed at https://www.shadertoy.com/view/XljfRV) // // Noise function from iq's "Noise - gradient - 2D" // https://www.shadertoy.com/view/XdXGW8 // ////////////////////////////////////////////////////////////////////// // vector rotated by 90 degrees CCW vec2 perp(vec2 u) { return vec2(-u.y, u.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 rotate(vec2 rvec, vec2 p) { return p.x * rvec + p.y * vec2(-rvec.y, rvec.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 unrotate(vec2 rvec, vec2 p) { return p.x * vec2(rvec.x, -rvec.y) + p.y * rvec.yx; } // distance from point to line segment float dseg(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; float u = dot(pa, ba) / dot(ba, ba); u = clamp(u, 0.0, 1.0); return length(pa - u * ba); } // half-plane test bool in_half_plane(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; return dot(pa, perp(ba)) > 0.0; } // point in triangle bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) { return in_half_plane(p, a, b) && in_half_plane(p, b, c) && in_half_plane(p, c, a); } // from https://www.shadertoy.com/view/XdXGW8 - used for noise below vec2 grad( ivec2 z ) { // 2D to 1D (feel free to replace by some other) int n = z.x+z.y*11111; // Hugo Elias hash (feel free to replace by another one) n = (n<<13)^n; n = (n*(n*n*15731+789221)+1376312589)>>16; // Perlin style vectors n &= 7; vec2 gr = vec2(n&1,n>>1)*2.0-1.0; return ( n>=6 ) ? vec2(0.0,gr.x) : ( n>=4 ) ? vec2(gr.x,0.0) : gr; } // from https://www.shadertoy.com/view/XdXGW8 float noise( in vec2 p ) { ivec2 i = ivec2(floor( p )); vec2 f = fract( p ); vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); return mix( mix( dot( grad( i+ivec2(0,0) ), f-vec2(0.0,0.0) ), dot( grad( i+ivec2(1,0) ), f-vec2(1.0,0.0) ), u.x), mix( dot( grad( i+ivec2(0,1) ), f-vec2(0.0,1.0) ), dot( grad( i+ivec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y); } // colors for each cell const vec3 CELL_COLORS[4] = vec3[4]( vec3(0.9, 0.0, 0.05), vec3(0.95, 0.85, 0.0), vec3(0.1, 0.8, 0.05), vec3(0.1, 0.5, 0.8) ); // rotation vectors for theta = 0, pi/2, pi, 3*pi/2 const vec2 ROT_VECTORS[4] = vec2[4]( vec2(1, 0), vec2(0, 1), vec2(-1, 0), vec2(0, -1) ); // un-rotated cell geometry // // // C // _*_____ // _- -----____ // D _*------------------* B --- // _- | || // * | || // | | | | // | | | | t // | | | | // | | A | | // | | __* | --- // | | __-- \ | // || __-- \ | y // ||__-- \| // O *------------------* X --- // // | x | s | // // notes: // // square is 0.5 x 0.5 // // A = O + (x, y) // B = O + (0.5, 0.5) // C = B + (-t, s) = B + (y - 0.5, 0.5 - x) // D = O + (-y, x) // X = O + (0.5, 0) // // segments OD and OA are congruent and perpendicular // segments AB and BC are congruent and perpendicular // // there are four rotated copies of polygon OABCD around point O // polygon points vec2 O, A, B, C, D; // for bump-mapped shading vec2 heightmap(vec2 p) { // get polygon distance float dpoly = dseg(p, O, A); dpoly = min(dpoly, dseg(p, A, B)); dpoly = min(dpoly, dseg(p, B, C)); dpoly = min(dpoly, dseg(p, C, D)); dpoly = min(dpoly, dseg(p, D, O)); // offset from edge float k = 0.08; // base height float z = k + 0.01 * noise(5.*p); if (dpoly < k) { // semicircular shoulder float w = (dpoly/k - 1.0); z *= sqrt(1.0 - w*w); } else { // depression inwards from edge z *= (1.0 - 0.03*smoothstep(k, 2.0*k, dpoly)); } // return height and polygon distance return vec2(z, dpoly); } // do the things! void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // global rotation by 45 degrees vec2 global_rot = vec2(0.5*sqrt(2.0)); // image should be six blocks high float scl = 6.0 * global_rot.x / iResolution.y; // uv in [0,1] x [0, 1] holds vertex position vec2 uv = vec2(0.7886751345948132, 0.21132486540518713); // light coords relative to center vec2 lcoord = vec2(0); if (iMouse.z > 0.) { // set vertex coords by dragging - light is fixed uv = clamp(iMouse.xy / iResolution.xy, 0.0, 1.0); lcoord = vec2(-0.0, 0.5); } else { // set vertex coords varying over screen // and move light uv = (fragCoord.xy - 0.5 * iResolution.xy) / (max(iResolution.x, iResolution.y)); const float repeat = 30.0; // seconds const float wraps_per_repeat = 5.0; const float x_lobes = 3.0; const float y_lobes = 2.0; const float two_pi = 6.283185307179586; float t = iTime * two_pi / repeat; float t_wrap = t * wraps_per_repeat; float c = cos(t_wrap); float s = sin(t_wrap); uv = rotate(vec2(s, -c), uv); uv = clamp(uv + 0.5, 0.0, 1.0); lcoord = vec2(-sin(t * x_lobes), cos(t * y_lobes)); } // z coordinate of camera and light (tiles live at z=0) const float cz = 3.5; // set light pos in 3D vec3 lpos = vec3(lcoord * 0.5 * iResolution.xy * scl, cz); // camera pos in 3D const vec3 cpos = vec3(0, 0, cz); // map frag cords to scene coords (before global rotation) vec2 porig = (fragCoord + vec2(0.13, 0.17) - 0.5*iResolution.xy) * scl; // apply global rotation vec2 p = rotate(porig, global_rot); // find starting origin of tile cluster -- note this could change below O = floor(p + 0.5); // figure out which quadrant we are in relative to the origin ivec2 qstep = ivec2(step(p, O)); int quadrant = (qstep.x ^ qstep.y) + 2*qstep.y; // each quadrant rotates by 90 degrees vec2 rvec = ROT_VECTORS[quadrant]; // form some critical points of the polygon in this cell vec2 xy = 0.5*uv; vec2 st = 0.5 - xy; A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); vec2 X = O + rotate(rvec, vec2(0.5, 0)); // get distance from point to semgent AX float dline = dseg(p, A, X); // figure out whether we are in the main upper-left part of the // cell or one of the two triangles int cell = quadrant; if (in_triangle(p, X, B, A)) { // in triangle XBA -- rotate polygon CCW by 90 degrees and translate it over by 1 cell cell = (quadrant + 1) & 3; O += rvec; rvec = perp(rvec); } else if (in_triangle(p, O, X, A)) { // in trangle OXA -- rotate polygon CW by 90 degrees cell = (quadrant + 3) & 3; rvec = -perp(rvec); } // now we know which polygonal tile p is in, so get the distance to the // polygon A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); C = B + rotate(rvec, perp(st)); D = O + rotate(rvec, perp(xy)); vec2 hm = heightmap(p); const float h = 1e-3; const vec2 eps = vec2(h, 0); vec2 hgrad = (0.5 / h) * vec2( heightmap(p + eps.xy).x - heightmap(p - eps.xy).x, heightmap(p + eps.yx).x - heightmap(p - eps.yx).x ); hgrad = unrotate(global_rot, hgrad); float z = hm.x; dline = min(dline, hm.y); // bump-mapped surface normal vec3 N = normalize(cross(vec3(1, 0, hgrad.x), vec3(0, 1, hgrad.y))); // get color of this cell vec3 color = CELL_COLORS[cell]; color *= color; // gamma un-correct // desaturate a bit color = mix(color, vec3(0.5), 0.08); // get 3D point position vec3 pos = vec3(porig, z); // fake phong lighting vec3 L = normalize(lpos - pos); vec3 V = -normalize(cpos - pos); vec3 R = reflect(L, N); color *= 0.1 + 0.9 * clamp(dot(N, L), 0.0, 1.0); color += 0.3*pow(clamp(dot(V, R), 0.0, 1.0), 10.0)*vec3(1); // darken by lines color *= smoothstep(0.0, 0.0125, dline); // gamma "correct" color = sqrt(color); // done! fragColor = vec4(color, 1); }
cc-by-4.0
[ 2509, 2555, 2581, 2581, 2976 ]
[ [ 1083, 1119, 1138, 1138, 1168 ], [ 1170, 1223, 1255, 1255, 1310 ], [ 1312, 1365, 1399, 1399, 1457 ], [ 1459, 1498, 1534, 1534, 1700 ], [ 1702, 1721, 1765, 1765, 1845 ], [ 1847, 1868, 1918, 1918, 2007 ], [ 2009, 2078, 2100, 2155, 2507 ], [ 2509, 2555, 2581, 2581, 2976 ], [ 4232, 4259, 4283, 4312, 4931 ], [ 4933, 4951, 5008, 5046, 9379 ] ]
// from https://www.shadertoy.com/view/XdXGW8
float noise( in vec2 p ) {
ivec2 i = ivec2(floor( p )); vec2 f = fract( p ); vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); return mix( mix( dot( grad( i+ivec2(0,0) ), f-vec2(0.0,0.0) ), dot( grad( i+ivec2(1,0) ), f-vec2(1.0,0.0) ), u.x), mix( dot( grad( i+ivec2(0,1) ), f-vec2(0.0,1.0) ), dot( grad( i+ivec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y); }
// from https://www.shadertoy.com/view/XdXGW8 float noise( in vec2 p ) {
2
83
fs3SWj
mattz
2021-10-12T00:52:06
////////////////////////////////////////////////////////////////////// // // "pentagonal tiling variations" by mattz // License https://creativecommons.org/licenses/by/4.0/ // // Click and drag to set vertex position. // // Renders "type 4" Cairo pentagonal tilings. See // https://en.wikipedia.org/wiki/Cairo_pentagonal_tiling // for details. // // Inspired by https://twitter.com/cs_kaplan // // Related shaders: // // - "Cairo tiling" by nimitz // https://www.shadertoy.com/view/4ssSWf // // - "More Cairo Tiles" by mla // https://www.shadertoy.com/view/MlSfRd // // - "Extruded Pentagon Tiling" by Shane // https://www.shadertoy.com/view/3t2cDK // // - "15th Pentagonal tiling" by tomkh // https://www.shadertoy.com/view/4lBXRV // // - "pentagonal tiling" by FabriceNeyret2 // https://www.shadertoy.com/view/ltBBzK // (golfed at https://www.shadertoy.com/view/XljfRV) // // Noise function from iq's "Noise - gradient - 2D" // https://www.shadertoy.com/view/XdXGW8 // ////////////////////////////////////////////////////////////////////// // vector rotated by 90 degrees CCW vec2 perp(vec2 u) { return vec2(-u.y, u.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 rotate(vec2 rvec, vec2 p) { return p.x * rvec + p.y * vec2(-rvec.y, rvec.x); } // rotate vector by rotation vector (cos(t), sin(t)) vec2 unrotate(vec2 rvec, vec2 p) { return p.x * vec2(rvec.x, -rvec.y) + p.y * rvec.yx; } // distance from point to line segment float dseg(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; float u = dot(pa, ba) / dot(ba, ba); u = clamp(u, 0.0, 1.0); return length(pa - u * ba); } // half-plane test bool in_half_plane(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; return dot(pa, perp(ba)) > 0.0; } // point in triangle bool in_triangle(vec2 p, vec2 a, vec2 b, vec2 c) { return in_half_plane(p, a, b) && in_half_plane(p, b, c) && in_half_plane(p, c, a); } // from https://www.shadertoy.com/view/XdXGW8 - used for noise below vec2 grad( ivec2 z ) { // 2D to 1D (feel free to replace by some other) int n = z.x+z.y*11111; // Hugo Elias hash (feel free to replace by another one) n = (n<<13)^n; n = (n*(n*n*15731+789221)+1376312589)>>16; // Perlin style vectors n &= 7; vec2 gr = vec2(n&1,n>>1)*2.0-1.0; return ( n>=6 ) ? vec2(0.0,gr.x) : ( n>=4 ) ? vec2(gr.x,0.0) : gr; } // from https://www.shadertoy.com/view/XdXGW8 float noise( in vec2 p ) { ivec2 i = ivec2(floor( p )); vec2 f = fract( p ); vec2 u = f*f*f*(f*(f*6.0-15.0)+10.0); return mix( mix( dot( grad( i+ivec2(0,0) ), f-vec2(0.0,0.0) ), dot( grad( i+ivec2(1,0) ), f-vec2(1.0,0.0) ), u.x), mix( dot( grad( i+ivec2(0,1) ), f-vec2(0.0,1.0) ), dot( grad( i+ivec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y); } // colors for each cell const vec3 CELL_COLORS[4] = vec3[4]( vec3(0.9, 0.0, 0.05), vec3(0.95, 0.85, 0.0), vec3(0.1, 0.8, 0.05), vec3(0.1, 0.5, 0.8) ); // rotation vectors for theta = 0, pi/2, pi, 3*pi/2 const vec2 ROT_VECTORS[4] = vec2[4]( vec2(1, 0), vec2(0, 1), vec2(-1, 0), vec2(0, -1) ); // un-rotated cell geometry // // // C // _*_____ // _- -----____ // D _*------------------* B --- // _- | || // * | || // | | | | // | | | | t // | | | | // | | A | | // | | __* | --- // | | __-- \ | // || __-- \ | y // ||__-- \| // O *------------------* X --- // // | x | s | // // notes: // // square is 0.5 x 0.5 // // A = O + (x, y) // B = O + (0.5, 0.5) // C = B + (-t, s) = B + (y - 0.5, 0.5 - x) // D = O + (-y, x) // X = O + (0.5, 0) // // segments OD and OA are congruent and perpendicular // segments AB and BC are congruent and perpendicular // // there are four rotated copies of polygon OABCD around point O // polygon points vec2 O, A, B, C, D; // for bump-mapped shading vec2 heightmap(vec2 p) { // get polygon distance float dpoly = dseg(p, O, A); dpoly = min(dpoly, dseg(p, A, B)); dpoly = min(dpoly, dseg(p, B, C)); dpoly = min(dpoly, dseg(p, C, D)); dpoly = min(dpoly, dseg(p, D, O)); // offset from edge float k = 0.08; // base height float z = k + 0.01 * noise(5.*p); if (dpoly < k) { // semicircular shoulder float w = (dpoly/k - 1.0); z *= sqrt(1.0 - w*w); } else { // depression inwards from edge z *= (1.0 - 0.03*smoothstep(k, 2.0*k, dpoly)); } // return height and polygon distance return vec2(z, dpoly); } // do the things! void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // global rotation by 45 degrees vec2 global_rot = vec2(0.5*sqrt(2.0)); // image should be six blocks high float scl = 6.0 * global_rot.x / iResolution.y; // uv in [0,1] x [0, 1] holds vertex position vec2 uv = vec2(0.7886751345948132, 0.21132486540518713); // light coords relative to center vec2 lcoord = vec2(0); if (iMouse.z > 0.) { // set vertex coords by dragging - light is fixed uv = clamp(iMouse.xy / iResolution.xy, 0.0, 1.0); lcoord = vec2(-0.0, 0.5); } else { // set vertex coords varying over screen // and move light uv = (fragCoord.xy - 0.5 * iResolution.xy) / (max(iResolution.x, iResolution.y)); const float repeat = 30.0; // seconds const float wraps_per_repeat = 5.0; const float x_lobes = 3.0; const float y_lobes = 2.0; const float two_pi = 6.283185307179586; float t = iTime * two_pi / repeat; float t_wrap = t * wraps_per_repeat; float c = cos(t_wrap); float s = sin(t_wrap); uv = rotate(vec2(s, -c), uv); uv = clamp(uv + 0.5, 0.0, 1.0); lcoord = vec2(-sin(t * x_lobes), cos(t * y_lobes)); } // z coordinate of camera and light (tiles live at z=0) const float cz = 3.5; // set light pos in 3D vec3 lpos = vec3(lcoord * 0.5 * iResolution.xy * scl, cz); // camera pos in 3D const vec3 cpos = vec3(0, 0, cz); // map frag cords to scene coords (before global rotation) vec2 porig = (fragCoord + vec2(0.13, 0.17) - 0.5*iResolution.xy) * scl; // apply global rotation vec2 p = rotate(porig, global_rot); // find starting origin of tile cluster -- note this could change below O = floor(p + 0.5); // figure out which quadrant we are in relative to the origin ivec2 qstep = ivec2(step(p, O)); int quadrant = (qstep.x ^ qstep.y) + 2*qstep.y; // each quadrant rotates by 90 degrees vec2 rvec = ROT_VECTORS[quadrant]; // form some critical points of the polygon in this cell vec2 xy = 0.5*uv; vec2 st = 0.5 - xy; A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); vec2 X = O + rotate(rvec, vec2(0.5, 0)); // get distance from point to semgent AX float dline = dseg(p, A, X); // figure out whether we are in the main upper-left part of the // cell or one of the two triangles int cell = quadrant; if (in_triangle(p, X, B, A)) { // in triangle XBA -- rotate polygon CCW by 90 degrees and translate it over by 1 cell cell = (quadrant + 1) & 3; O += rvec; rvec = perp(rvec); } else if (in_triangle(p, O, X, A)) { // in trangle OXA -- rotate polygon CW by 90 degrees cell = (quadrant + 3) & 3; rvec = -perp(rvec); } // now we know which polygonal tile p is in, so get the distance to the // polygon A = O + rotate(rvec, xy); B = O + rotate(rvec, vec2(0.5)); C = B + rotate(rvec, perp(st)); D = O + rotate(rvec, perp(xy)); vec2 hm = heightmap(p); const float h = 1e-3; const vec2 eps = vec2(h, 0); vec2 hgrad = (0.5 / h) * vec2( heightmap(p + eps.xy).x - heightmap(p - eps.xy).x, heightmap(p + eps.yx).x - heightmap(p - eps.yx).x ); hgrad = unrotate(global_rot, hgrad); float z = hm.x; dline = min(dline, hm.y); // bump-mapped surface normal vec3 N = normalize(cross(vec3(1, 0, hgrad.x), vec3(0, 1, hgrad.y))); // get color of this cell vec3 color = CELL_COLORS[cell]; color *= color; // gamma un-correct // desaturate a bit color = mix(color, vec3(0.5), 0.08); // get 3D point position vec3 pos = vec3(porig, z); // fake phong lighting vec3 L = normalize(lpos - pos); vec3 V = -normalize(cpos - pos); vec3 R = reflect(L, N); color *= 0.1 + 0.9 * clamp(dot(N, L), 0.0, 1.0); color += 0.3*pow(clamp(dot(V, R), 0.0, 1.0), 10.0)*vec3(1); // darken by lines color *= smoothstep(0.0, 0.0125, dline); // gamma "correct" color = sqrt(color); // done! fragColor = vec4(color, 1); }
cc-by-4.0
[ 4232, 4259, 4283, 4312, 4931 ]
[ [ 1083, 1119, 1138, 1138, 1168 ], [ 1170, 1223, 1255, 1255, 1310 ], [ 1312, 1365, 1399, 1399, 1457 ], [ 1459, 1498, 1534, 1534, 1700 ], [ 1702, 1721, 1765, 1765, 1845 ], [ 1847, 1868, 1918, 1918, 2007 ], [ 2009, 2078, 2100, 2155, 2507 ], [ 2509, 2555, 2581, 2581, 2976 ], [ 4232, 4259, 4283, 4312, 4931 ], [ 4933, 4951, 5008, 5046, 9379 ] ]
// for bump-mapped shading
vec2 heightmap(vec2 p) {
float dpoly = dseg(p, O, A); dpoly = min(dpoly, dseg(p, A, B)); dpoly = min(dpoly, dseg(p, B, C)); dpoly = min(dpoly, dseg(p, C, D)); dpoly = min(dpoly, dseg(p, D, O)); // offset from edge float k = 0.08; // base height float z = k + 0.01 * noise(5.*p); if (dpoly < k) { // semicircular shoulder float w = (dpoly/k - 1.0); z *= sqrt(1.0 - w*w); } else { // depression inwards from edge z *= (1.0 - 0.03*smoothstep(k, 2.0*k, dpoly)); } // return height and polygon distance return vec2(z, dpoly); }
// for bump-mapped shading vec2 heightmap(vec2 p) {
2
2
ftKGzm
Jabo
2021-11-20T13:00:40
// The MIT License // 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://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdCircle( in vec2 p, in float r ) { return length(p)-r; } // https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdTriangle( in vec2 p ) { const float k = sqrt(3.0); p.x = abs(p.x) - 1.0; p.y = p.y + 1.0/k; if( p.x+k*p.y>0.0 ) p = vec2(p.x-k*p.y,-k*p.x-p.y)/2.0; p.x -= clamp( p.x, -2.0, 0.0 ); return -length(p)*sign(p.y); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float opSmoothSubtraction( float d1, float d2, float k ) { float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 ); return mix( d2, -d1, h ) + k*h*(1.0-h); } // modified https://www.shadertoy.com/view/ldBGDc float spiral(vec2 m, float t) { float r = pow(length(m) / 0.75, 3.5) * 0.75; float a = atan(m.x, m.y); float v = sin(48.*(sqrt(r)-0.0625*a-.05*t)); return clamp(v,0.,1.); } // adapted for webgl https://www.ronja-tutorials.com/post/041-hsv-colorspace/#rgb-to-hsv-conversion vec3 hue2rgb(float hue) { hue = fract(hue); //only use fractional part of hue, making it loop float r = abs(hue * 6. - 3.) - 1.; //red float g = 2. - abs(hue * 6. - 2.); //green float b = 2. - abs(hue * 6. - 4.); //blue vec3 rgb = vec3(r,g,b); //combine components rgb = clamp(rgb, 0., 1.); //saturate return rgb; } vec3 hsv2rgb(vec3 hsv) { vec3 rgb = hue2rgb(hsv.x); //apply hue rgb = mix(vec3(1.0), rgb, hsv.y); //apply saturation rgb = rgb * hsv.z; //apply value return rgb; } vec3 scene(vec2 fragCoord, float iTime) { vec3 col = vec3(0.5); vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y; float radius = 0.8 + 0.1*cos(iTime)*cos(0.1*iTime); float d = sdCircle(p, radius); // stripes float distortion = 6.0*cos(69.0*d/2.0 + cos(iTime))*clamp(sin(4.+0.09*iTime),0.,1.) * (d + .5) / iResolution.y; float fraction = clamp((1.0 - p.y)/2.0+distortion, 0.01, 1.0); float stripe = clamp(floor(fraction * 6.0) / 6.0, 0.0, 1.0); // upper 3: mix(-0.06, 0.65, stripe) // lower 3: mix(-0.1, 0.91, stripe) float stripeHue1 = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); stripe += 1./6.; float stripeHue2 = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); float stripeHue = mix(stripeHue1, stripeHue2, smoothstep(stripe - 0.003, stripe + 0.003, fraction - 0.0005)); float stripeSaturation = mix(0.95, 0.45, pow(clamp(sin(stripe * 6. + 0.4*iTime),0.,1.),24.)*clamp(2.*sin(5.+ 0.031*iTime),0.,1.)); col = hsv2rgb(vec3(clamp(stripeHue,0.001, 0.75), stripeSaturation, 0.95)); if(iMouse.z > 0.001) { // old method has no smoothstep to reduce aliasing float stripe = (floor(clamp((1.0 - p.y)/2.0+distortion, 0.01, 1.0) * 6.0) / 6.0); // upper 3: mix(-0.06, 0.65, stripe) // lower 3: mix(-0.1, 0.91, stripe) float stripeHue = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); col = hsv2rgb(vec3(clamp(stripeHue,0.001, 0.75), 0.95, 0.95)); } // vingette glow col = mix(col, col * 0.95 + 0.2 * exp(-8.0*abs(d)), 0.5 + 0.5*cos(0.7*iTime)); // ripples col = mix(col, col * cos(69.0*d + 4.2*cos(iTime)), clamp(0.1*cos(3.0+0.05*iTime),0.0,1.0)); // spiral if ( d < 0.0 ) { vec2 uv = vec2(0.9, 0.5) - fragCoord.xy / iResolution.y; float s = spiral(uv, iTime); s = smoothstep(0.0, 0.05, s); col = vec3(s); // droplet vec2 p2 = p * (0.95 + 0.05 * sin(0.7*iTime)); float d2 = min(sdCircle(p2+vec2(0.0,0.1), 0.3), sdTriangle((p2-vec2(0.0,0.2))*3.65)) - 0.01; if (d2 < 0.005 ) { col = mix(hsv2rgb(vec3(0.60, 0.95, 0.95)), col, smoothstep(0.0, 0.005, d2)); // reflection float d3 = opSmoothSubtraction( sdCircle(p2+vec2(0.2,-0.1), 0.46), sdCircle(p2+vec2(0.0,0.1), 0.24), 0.02 ); d3 *= 1.2; if(d3 < 0.005) { col = mix(vec3(1), col, smoothstep(0.0, 0.005, d3)); } } } // outer border col = mix( col, vec3(0.0), 1.0-smoothstep(0.0,0.005, abs(d) - 0.02) ); return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { float abberation = 0.06*sin(0.033*iTime); vec3 col = vec3(0); // yes temporal chromatic abberation makes no sense, but we don't have to make sense col += scene(fragCoord, iTime - abberation) * vec3(.8,.1,.1); col += scene(fragCoord, iTime ) * vec3(.1,.8,.1); col += scene(fragCoord, iTime + abberation) * vec3(.1,.1,.8); fragColor = vec4(col,1.0); } // c9558532c93019c667e3fc5e14532d21
mit
[ 1044, 1119, 1161, 1161, 1187 ]
[ [ 1044, 1119, 1161, 1161, 1187 ], [ 1189, 1264, 1295, 1295, 1503 ], [ 1505, 1576, 1634, 1634, 1734 ], [ 1736, 1786, 1817, 1817, 1965 ], [ 1967, 2067, 2092, 2092, 2419 ], [ 2420, 2420, 2444, 2444, 2599 ], [ 2601, 2601, 2642, 2642, 5406 ], [ 5408, 5408, 5465, 5465, 5850 ] ]
// https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float sdCircle( in vec2 p, in float r ) {
return length(p)-r; }
// https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdCircle( in vec2 p, in float r ) {
9
11
ftKGzm
Jabo
2021-11-20T13:00:40
// The MIT License // 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://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdCircle( in vec2 p, in float r ) { return length(p)-r; } // https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdTriangle( in vec2 p ) { const float k = sqrt(3.0); p.x = abs(p.x) - 1.0; p.y = p.y + 1.0/k; if( p.x+k*p.y>0.0 ) p = vec2(p.x-k*p.y,-k*p.x-p.y)/2.0; p.x -= clamp( p.x, -2.0, 0.0 ); return -length(p)*sign(p.y); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float opSmoothSubtraction( float d1, float d2, float k ) { float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 ); return mix( d2, -d1, h ) + k*h*(1.0-h); } // modified https://www.shadertoy.com/view/ldBGDc float spiral(vec2 m, float t) { float r = pow(length(m) / 0.75, 3.5) * 0.75; float a = atan(m.x, m.y); float v = sin(48.*(sqrt(r)-0.0625*a-.05*t)); return clamp(v,0.,1.); } // adapted for webgl https://www.ronja-tutorials.com/post/041-hsv-colorspace/#rgb-to-hsv-conversion vec3 hue2rgb(float hue) { hue = fract(hue); //only use fractional part of hue, making it loop float r = abs(hue * 6. - 3.) - 1.; //red float g = 2. - abs(hue * 6. - 2.); //green float b = 2. - abs(hue * 6. - 4.); //blue vec3 rgb = vec3(r,g,b); //combine components rgb = clamp(rgb, 0., 1.); //saturate return rgb; } vec3 hsv2rgb(vec3 hsv) { vec3 rgb = hue2rgb(hsv.x); //apply hue rgb = mix(vec3(1.0), rgb, hsv.y); //apply saturation rgb = rgb * hsv.z; //apply value return rgb; } vec3 scene(vec2 fragCoord, float iTime) { vec3 col = vec3(0.5); vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y; float radius = 0.8 + 0.1*cos(iTime)*cos(0.1*iTime); float d = sdCircle(p, radius); // stripes float distortion = 6.0*cos(69.0*d/2.0 + cos(iTime))*clamp(sin(4.+0.09*iTime),0.,1.) * (d + .5) / iResolution.y; float fraction = clamp((1.0 - p.y)/2.0+distortion, 0.01, 1.0); float stripe = clamp(floor(fraction * 6.0) / 6.0, 0.0, 1.0); // upper 3: mix(-0.06, 0.65, stripe) // lower 3: mix(-0.1, 0.91, stripe) float stripeHue1 = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); stripe += 1./6.; float stripeHue2 = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); float stripeHue = mix(stripeHue1, stripeHue2, smoothstep(stripe - 0.003, stripe + 0.003, fraction - 0.0005)); float stripeSaturation = mix(0.95, 0.45, pow(clamp(sin(stripe * 6. + 0.4*iTime),0.,1.),24.)*clamp(2.*sin(5.+ 0.031*iTime),0.,1.)); col = hsv2rgb(vec3(clamp(stripeHue,0.001, 0.75), stripeSaturation, 0.95)); if(iMouse.z > 0.001) { // old method has no smoothstep to reduce aliasing float stripe = (floor(clamp((1.0 - p.y)/2.0+distortion, 0.01, 1.0) * 6.0) / 6.0); // upper 3: mix(-0.06, 0.65, stripe) // lower 3: mix(-0.1, 0.91, stripe) float stripeHue = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); col = hsv2rgb(vec3(clamp(stripeHue,0.001, 0.75), 0.95, 0.95)); } // vingette glow col = mix(col, col * 0.95 + 0.2 * exp(-8.0*abs(d)), 0.5 + 0.5*cos(0.7*iTime)); // ripples col = mix(col, col * cos(69.0*d + 4.2*cos(iTime)), clamp(0.1*cos(3.0+0.05*iTime),0.0,1.0)); // spiral if ( d < 0.0 ) { vec2 uv = vec2(0.9, 0.5) - fragCoord.xy / iResolution.y; float s = spiral(uv, iTime); s = smoothstep(0.0, 0.05, s); col = vec3(s); // droplet vec2 p2 = p * (0.95 + 0.05 * sin(0.7*iTime)); float d2 = min(sdCircle(p2+vec2(0.0,0.1), 0.3), sdTriangle((p2-vec2(0.0,0.2))*3.65)) - 0.01; if (d2 < 0.005 ) { col = mix(hsv2rgb(vec3(0.60, 0.95, 0.95)), col, smoothstep(0.0, 0.005, d2)); // reflection float d3 = opSmoothSubtraction( sdCircle(p2+vec2(0.2,-0.1), 0.46), sdCircle(p2+vec2(0.0,0.1), 0.24), 0.02 ); d3 *= 1.2; if(d3 < 0.005) { col = mix(vec3(1), col, smoothstep(0.0, 0.005, d3)); } } } // outer border col = mix( col, vec3(0.0), 1.0-smoothstep(0.0,0.005, abs(d) - 0.02) ); return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { float abberation = 0.06*sin(0.033*iTime); vec3 col = vec3(0); // yes temporal chromatic abberation makes no sense, but we don't have to make sense col += scene(fragCoord, iTime - abberation) * vec3(.8,.1,.1); col += scene(fragCoord, iTime ) * vec3(.1,.8,.1); col += scene(fragCoord, iTime + abberation) * vec3(.1,.1,.8); fragColor = vec4(col,1.0); } // c9558532c93019c667e3fc5e14532d21
mit
[ 1189, 1264, 1295, 1295, 1503 ]
[ [ 1044, 1119, 1161, 1161, 1187 ], [ 1189, 1264, 1295, 1295, 1503 ], [ 1505, 1576, 1634, 1634, 1734 ], [ 1736, 1786, 1817, 1817, 1965 ], [ 1967, 2067, 2092, 2092, 2419 ], [ 2420, 2420, 2444, 2444, 2599 ], [ 2601, 2601, 2642, 2642, 5406 ], [ 5408, 5408, 5465, 5465, 5850 ] ]
// https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float sdTriangle( in vec2 p ) {
const float k = sqrt(3.0); p.x = abs(p.x) - 1.0; p.y = p.y + 1.0/k; if( p.x+k*p.y>0.0 ) p = vec2(p.x-k*p.y,-k*p.x-p.y)/2.0; p.x -= clamp( p.x, -2.0, 0.0 ); return -length(p)*sign(p.y); }
// https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdTriangle( in vec2 p ) {
1
1
ftKGzm
Jabo
2021-11-20T13:00:40
// The MIT License // 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://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdCircle( in vec2 p, in float r ) { return length(p)-r; } // https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdTriangle( in vec2 p ) { const float k = sqrt(3.0); p.x = abs(p.x) - 1.0; p.y = p.y + 1.0/k; if( p.x+k*p.y>0.0 ) p = vec2(p.x-k*p.y,-k*p.x-p.y)/2.0; p.x -= clamp( p.x, -2.0, 0.0 ); return -length(p)*sign(p.y); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float opSmoothSubtraction( float d1, float d2, float k ) { float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 ); return mix( d2, -d1, h ) + k*h*(1.0-h); } // modified https://www.shadertoy.com/view/ldBGDc float spiral(vec2 m, float t) { float r = pow(length(m) / 0.75, 3.5) * 0.75; float a = atan(m.x, m.y); float v = sin(48.*(sqrt(r)-0.0625*a-.05*t)); return clamp(v,0.,1.); } // adapted for webgl https://www.ronja-tutorials.com/post/041-hsv-colorspace/#rgb-to-hsv-conversion vec3 hue2rgb(float hue) { hue = fract(hue); //only use fractional part of hue, making it loop float r = abs(hue * 6. - 3.) - 1.; //red float g = 2. - abs(hue * 6. - 2.); //green float b = 2. - abs(hue * 6. - 4.); //blue vec3 rgb = vec3(r,g,b); //combine components rgb = clamp(rgb, 0., 1.); //saturate return rgb; } vec3 hsv2rgb(vec3 hsv) { vec3 rgb = hue2rgb(hsv.x); //apply hue rgb = mix(vec3(1.0), rgb, hsv.y); //apply saturation rgb = rgb * hsv.z; //apply value return rgb; } vec3 scene(vec2 fragCoord, float iTime) { vec3 col = vec3(0.5); vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y; float radius = 0.8 + 0.1*cos(iTime)*cos(0.1*iTime); float d = sdCircle(p, radius); // stripes float distortion = 6.0*cos(69.0*d/2.0 + cos(iTime))*clamp(sin(4.+0.09*iTime),0.,1.) * (d + .5) / iResolution.y; float fraction = clamp((1.0 - p.y)/2.0+distortion, 0.01, 1.0); float stripe = clamp(floor(fraction * 6.0) / 6.0, 0.0, 1.0); // upper 3: mix(-0.06, 0.65, stripe) // lower 3: mix(-0.1, 0.91, stripe) float stripeHue1 = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); stripe += 1./6.; float stripeHue2 = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); float stripeHue = mix(stripeHue1, stripeHue2, smoothstep(stripe - 0.003, stripe + 0.003, fraction - 0.0005)); float stripeSaturation = mix(0.95, 0.45, pow(clamp(sin(stripe * 6. + 0.4*iTime),0.,1.),24.)*clamp(2.*sin(5.+ 0.031*iTime),0.,1.)); col = hsv2rgb(vec3(clamp(stripeHue,0.001, 0.75), stripeSaturation, 0.95)); if(iMouse.z > 0.001) { // old method has no smoothstep to reduce aliasing float stripe = (floor(clamp((1.0 - p.y)/2.0+distortion, 0.01, 1.0) * 6.0) / 6.0); // upper 3: mix(-0.06, 0.65, stripe) // lower 3: mix(-0.1, 0.91, stripe) float stripeHue = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); col = hsv2rgb(vec3(clamp(stripeHue,0.001, 0.75), 0.95, 0.95)); } // vingette glow col = mix(col, col * 0.95 + 0.2 * exp(-8.0*abs(d)), 0.5 + 0.5*cos(0.7*iTime)); // ripples col = mix(col, col * cos(69.0*d + 4.2*cos(iTime)), clamp(0.1*cos(3.0+0.05*iTime),0.0,1.0)); // spiral if ( d < 0.0 ) { vec2 uv = vec2(0.9, 0.5) - fragCoord.xy / iResolution.y; float s = spiral(uv, iTime); s = smoothstep(0.0, 0.05, s); col = vec3(s); // droplet vec2 p2 = p * (0.95 + 0.05 * sin(0.7*iTime)); float d2 = min(sdCircle(p2+vec2(0.0,0.1), 0.3), sdTriangle((p2-vec2(0.0,0.2))*3.65)) - 0.01; if (d2 < 0.005 ) { col = mix(hsv2rgb(vec3(0.60, 0.95, 0.95)), col, smoothstep(0.0, 0.005, d2)); // reflection float d3 = opSmoothSubtraction( sdCircle(p2+vec2(0.2,-0.1), 0.46), sdCircle(p2+vec2(0.0,0.1), 0.24), 0.02 ); d3 *= 1.2; if(d3 < 0.005) { col = mix(vec3(1), col, smoothstep(0.0, 0.005, d3)); } } } // outer border col = mix( col, vec3(0.0), 1.0-smoothstep(0.0,0.005, abs(d) - 0.02) ); return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { float abberation = 0.06*sin(0.033*iTime); vec3 col = vec3(0); // yes temporal chromatic abberation makes no sense, but we don't have to make sense col += scene(fragCoord, iTime - abberation) * vec3(.8,.1,.1); col += scene(fragCoord, iTime ) * vec3(.1,.8,.1); col += scene(fragCoord, iTime + abberation) * vec3(.1,.1,.8); fragColor = vec4(col,1.0); } // c9558532c93019c667e3fc5e14532d21
mit
[ 1505, 1576, 1634, 1634, 1734 ]
[ [ 1044, 1119, 1161, 1161, 1187 ], [ 1189, 1264, 1295, 1295, 1503 ], [ 1505, 1576, 1634, 1634, 1734 ], [ 1736, 1786, 1817, 1817, 1965 ], [ 1967, 2067, 2092, 2092, 2419 ], [ 2420, 2420, 2444, 2444, 2599 ], [ 2601, 2601, 2642, 2642, 5406 ], [ 5408, 5408, 5465, 5465, 5850 ] ]
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float opSmoothSubtraction( float d1, float d2, float k ) {
float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 ); return mix( d2, -d1, h ) + k*h*(1.0-h); }
// https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float opSmoothSubtraction( float d1, float d2, float k ) {
1
5
ftKGzm
Jabo
2021-11-20T13:00:40
// The MIT License // 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://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdCircle( in vec2 p, in float r ) { return length(p)-r; } // https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdTriangle( in vec2 p ) { const float k = sqrt(3.0); p.x = abs(p.x) - 1.0; p.y = p.y + 1.0/k; if( p.x+k*p.y>0.0 ) p = vec2(p.x-k*p.y,-k*p.x-p.y)/2.0; p.x -= clamp( p.x, -2.0, 0.0 ); return -length(p)*sign(p.y); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float opSmoothSubtraction( float d1, float d2, float k ) { float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 ); return mix( d2, -d1, h ) + k*h*(1.0-h); } // modified https://www.shadertoy.com/view/ldBGDc float spiral(vec2 m, float t) { float r = pow(length(m) / 0.75, 3.5) * 0.75; float a = atan(m.x, m.y); float v = sin(48.*(sqrt(r)-0.0625*a-.05*t)); return clamp(v,0.,1.); } // adapted for webgl https://www.ronja-tutorials.com/post/041-hsv-colorspace/#rgb-to-hsv-conversion vec3 hue2rgb(float hue) { hue = fract(hue); //only use fractional part of hue, making it loop float r = abs(hue * 6. - 3.) - 1.; //red float g = 2. - abs(hue * 6. - 2.); //green float b = 2. - abs(hue * 6. - 4.); //blue vec3 rgb = vec3(r,g,b); //combine components rgb = clamp(rgb, 0., 1.); //saturate return rgb; } vec3 hsv2rgb(vec3 hsv) { vec3 rgb = hue2rgb(hsv.x); //apply hue rgb = mix(vec3(1.0), rgb, hsv.y); //apply saturation rgb = rgb * hsv.z; //apply value return rgb; } vec3 scene(vec2 fragCoord, float iTime) { vec3 col = vec3(0.5); vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y; float radius = 0.8 + 0.1*cos(iTime)*cos(0.1*iTime); float d = sdCircle(p, radius); // stripes float distortion = 6.0*cos(69.0*d/2.0 + cos(iTime))*clamp(sin(4.+0.09*iTime),0.,1.) * (d + .5) / iResolution.y; float fraction = clamp((1.0 - p.y)/2.0+distortion, 0.01, 1.0); float stripe = clamp(floor(fraction * 6.0) / 6.0, 0.0, 1.0); // upper 3: mix(-0.06, 0.65, stripe) // lower 3: mix(-0.1, 0.91, stripe) float stripeHue1 = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); stripe += 1./6.; float stripeHue2 = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); float stripeHue = mix(stripeHue1, stripeHue2, smoothstep(stripe - 0.003, stripe + 0.003, fraction - 0.0005)); float stripeSaturation = mix(0.95, 0.45, pow(clamp(sin(stripe * 6. + 0.4*iTime),0.,1.),24.)*clamp(2.*sin(5.+ 0.031*iTime),0.,1.)); col = hsv2rgb(vec3(clamp(stripeHue,0.001, 0.75), stripeSaturation, 0.95)); if(iMouse.z > 0.001) { // old method has no smoothstep to reduce aliasing float stripe = (floor(clamp((1.0 - p.y)/2.0+distortion, 0.01, 1.0) * 6.0) / 6.0); // upper 3: mix(-0.06, 0.65, stripe) // lower 3: mix(-0.1, 0.91, stripe) float stripeHue = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); col = hsv2rgb(vec3(clamp(stripeHue,0.001, 0.75), 0.95, 0.95)); } // vingette glow col = mix(col, col * 0.95 + 0.2 * exp(-8.0*abs(d)), 0.5 + 0.5*cos(0.7*iTime)); // ripples col = mix(col, col * cos(69.0*d + 4.2*cos(iTime)), clamp(0.1*cos(3.0+0.05*iTime),0.0,1.0)); // spiral if ( d < 0.0 ) { vec2 uv = vec2(0.9, 0.5) - fragCoord.xy / iResolution.y; float s = spiral(uv, iTime); s = smoothstep(0.0, 0.05, s); col = vec3(s); // droplet vec2 p2 = p * (0.95 + 0.05 * sin(0.7*iTime)); float d2 = min(sdCircle(p2+vec2(0.0,0.1), 0.3), sdTriangle((p2-vec2(0.0,0.2))*3.65)) - 0.01; if (d2 < 0.005 ) { col = mix(hsv2rgb(vec3(0.60, 0.95, 0.95)), col, smoothstep(0.0, 0.005, d2)); // reflection float d3 = opSmoothSubtraction( sdCircle(p2+vec2(0.2,-0.1), 0.46), sdCircle(p2+vec2(0.0,0.1), 0.24), 0.02 ); d3 *= 1.2; if(d3 < 0.005) { col = mix(vec3(1), col, smoothstep(0.0, 0.005, d3)); } } } // outer border col = mix( col, vec3(0.0), 1.0-smoothstep(0.0,0.005, abs(d) - 0.02) ); return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { float abberation = 0.06*sin(0.033*iTime); vec3 col = vec3(0); // yes temporal chromatic abberation makes no sense, but we don't have to make sense col += scene(fragCoord, iTime - abberation) * vec3(.8,.1,.1); col += scene(fragCoord, iTime ) * vec3(.1,.8,.1); col += scene(fragCoord, iTime + abberation) * vec3(.1,.1,.8); fragColor = vec4(col,1.0); } // c9558532c93019c667e3fc5e14532d21
mit
[ 1736, 1786, 1817, 1817, 1965 ]
[ [ 1044, 1119, 1161, 1161, 1187 ], [ 1189, 1264, 1295, 1295, 1503 ], [ 1505, 1576, 1634, 1634, 1734 ], [ 1736, 1786, 1817, 1817, 1965 ], [ 1967, 2067, 2092, 2092, 2419 ], [ 2420, 2420, 2444, 2444, 2599 ], [ 2601, 2601, 2642, 2642, 5406 ], [ 5408, 5408, 5465, 5465, 5850 ] ]
// modified https://www.shadertoy.com/view/ldBGDc
float spiral(vec2 m, float t) {
float r = pow(length(m) / 0.75, 3.5) * 0.75; float a = atan(m.x, m.y); float v = sin(48.*(sqrt(r)-0.0625*a-.05*t)); return clamp(v,0.,1.); }
// modified https://www.shadertoy.com/view/ldBGDc float spiral(vec2 m, float t) {
1
3
ftKGzm
Jabo
2021-11-20T13:00:40
// The MIT License // 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://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdCircle( in vec2 p, in float r ) { return length(p)-r; } // https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdTriangle( in vec2 p ) { const float k = sqrt(3.0); p.x = abs(p.x) - 1.0; p.y = p.y + 1.0/k; if( p.x+k*p.y>0.0 ) p = vec2(p.x-k*p.y,-k*p.x-p.y)/2.0; p.x -= clamp( p.x, -2.0, 0.0 ); return -length(p)*sign(p.y); } // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm float opSmoothSubtraction( float d1, float d2, float k ) { float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 ); return mix( d2, -d1, h ) + k*h*(1.0-h); } // modified https://www.shadertoy.com/view/ldBGDc float spiral(vec2 m, float t) { float r = pow(length(m) / 0.75, 3.5) * 0.75; float a = atan(m.x, m.y); float v = sin(48.*(sqrt(r)-0.0625*a-.05*t)); return clamp(v,0.,1.); } // adapted for webgl https://www.ronja-tutorials.com/post/041-hsv-colorspace/#rgb-to-hsv-conversion vec3 hue2rgb(float hue) { hue = fract(hue); //only use fractional part of hue, making it loop float r = abs(hue * 6. - 3.) - 1.; //red float g = 2. - abs(hue * 6. - 2.); //green float b = 2. - abs(hue * 6. - 4.); //blue vec3 rgb = vec3(r,g,b); //combine components rgb = clamp(rgb, 0., 1.); //saturate return rgb; } vec3 hsv2rgb(vec3 hsv) { vec3 rgb = hue2rgb(hsv.x); //apply hue rgb = mix(vec3(1.0), rgb, hsv.y); //apply saturation rgb = rgb * hsv.z; //apply value return rgb; } vec3 scene(vec2 fragCoord, float iTime) { vec3 col = vec3(0.5); vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y; float radius = 0.8 + 0.1*cos(iTime)*cos(0.1*iTime); float d = sdCircle(p, radius); // stripes float distortion = 6.0*cos(69.0*d/2.0 + cos(iTime))*clamp(sin(4.+0.09*iTime),0.,1.) * (d + .5) / iResolution.y; float fraction = clamp((1.0 - p.y)/2.0+distortion, 0.01, 1.0); float stripe = clamp(floor(fraction * 6.0) / 6.0, 0.0, 1.0); // upper 3: mix(-0.06, 0.65, stripe) // lower 3: mix(-0.1, 0.91, stripe) float stripeHue1 = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); stripe += 1./6.; float stripeHue2 = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); float stripeHue = mix(stripeHue1, stripeHue2, smoothstep(stripe - 0.003, stripe + 0.003, fraction - 0.0005)); float stripeSaturation = mix(0.95, 0.45, pow(clamp(sin(stripe * 6. + 0.4*iTime),0.,1.),24.)*clamp(2.*sin(5.+ 0.031*iTime),0.,1.)); col = hsv2rgb(vec3(clamp(stripeHue,0.001, 0.75), stripeSaturation, 0.95)); if(iMouse.z > 0.001) { // old method has no smoothstep to reduce aliasing float stripe = (floor(clamp((1.0 - p.y)/2.0+distortion, 0.01, 1.0) * 6.0) / 6.0); // upper 3: mix(-0.06, 0.65, stripe) // lower 3: mix(-0.1, 0.91, stripe) float stripeHue = stripe < 0.5 ? mix(-0.06, 0.65, stripe) : mix(-0.1, 0.91, stripe); col = hsv2rgb(vec3(clamp(stripeHue,0.001, 0.75), 0.95, 0.95)); } // vingette glow col = mix(col, col * 0.95 + 0.2 * exp(-8.0*abs(d)), 0.5 + 0.5*cos(0.7*iTime)); // ripples col = mix(col, col * cos(69.0*d + 4.2*cos(iTime)), clamp(0.1*cos(3.0+0.05*iTime),0.0,1.0)); // spiral if ( d < 0.0 ) { vec2 uv = vec2(0.9, 0.5) - fragCoord.xy / iResolution.y; float s = spiral(uv, iTime); s = smoothstep(0.0, 0.05, s); col = vec3(s); // droplet vec2 p2 = p * (0.95 + 0.05 * sin(0.7*iTime)); float d2 = min(sdCircle(p2+vec2(0.0,0.1), 0.3), sdTriangle((p2-vec2(0.0,0.2))*3.65)) - 0.01; if (d2 < 0.005 ) { col = mix(hsv2rgb(vec3(0.60, 0.95, 0.95)), col, smoothstep(0.0, 0.005, d2)); // reflection float d3 = opSmoothSubtraction( sdCircle(p2+vec2(0.2,-0.1), 0.46), sdCircle(p2+vec2(0.0,0.1), 0.24), 0.02 ); d3 *= 1.2; if(d3 < 0.005) { col = mix(vec3(1), col, smoothstep(0.0, 0.005, d3)); } } } // outer border col = mix( col, vec3(0.0), 1.0-smoothstep(0.0,0.005, abs(d) - 0.02) ); return col; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { float abberation = 0.06*sin(0.033*iTime); vec3 col = vec3(0); // yes temporal chromatic abberation makes no sense, but we don't have to make sense col += scene(fragCoord, iTime - abberation) * vec3(.8,.1,.1); col += scene(fragCoord, iTime ) * vec3(.1,.8,.1); col += scene(fragCoord, iTime + abberation) * vec3(.1,.1,.8); fragColor = vec4(col,1.0); } // c9558532c93019c667e3fc5e14532d21
mit
[ 1967, 2067, 2092, 2092, 2419 ]
[ [ 1044, 1119, 1161, 1161, 1187 ], [ 1189, 1264, 1295, 1295, 1503 ], [ 1505, 1576, 1634, 1634, 1734 ], [ 1736, 1786, 1817, 1817, 1965 ], [ 1967, 2067, 2092, 2092, 2419 ], [ 2420, 2420, 2444, 2444, 2599 ], [ 2601, 2601, 2642, 2642, 5406 ], [ 5408, 5408, 5465, 5465, 5850 ] ]
// adapted for webgl https://www.ronja-tutorials.com/post/041-hsv-colorspace/#rgb-to-hsv-conversion
vec3 hue2rgb(float hue) {
hue = fract(hue); //only use fractional part of hue, making it loop float r = abs(hue * 6. - 3.) - 1.; //red float g = 2. - abs(hue * 6. - 2.); //green float b = 2. - abs(hue * 6. - 4.); //blue vec3 rgb = vec3(r,g,b); //combine components rgb = clamp(rgb, 0., 1.); //saturate return rgb; }
// adapted for webgl https://www.ronja-tutorials.com/post/041-hsv-colorspace/#rgb-to-hsv-conversion vec3 hue2rgb(float hue) {
1
10
sl3XRn
iq
2021-12-04T00:18:43
// The MIT License // Copyright © 2021 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. // Intersecting two line segments. float cro( in vec2 a, in vec2 b ) { return a.x*b.y - a.y*b.x; } bool intersect( vec2 a1, vec2 b1, vec2 a2, vec2 b2, out vec2 point ) { float d = cro(b2-a2,b1-a1); float s = cro(a1-a2,b1-a1) / d; float t = cro(a1-a2,b2-a2) / d; point = a1 + (b1-a1)*t; // or point = a2 + (b2-a2)*s; return s>=0.0 && t>=0.0 && t<=1.0 && s<=1.0; } /* // same math as above, alternative writing by mla (see comments) bool intersect( vec2 a1, vec2 b1, vec2 a2, vec2 b2, out vec2 point ) { vec2 st = inverse(mat2(b1-a1,a2-b2))*(a2-a1); point = a1 + (b1-a1)*st.x; return s>=0.0 && t>=0.0 && t<=1.0 && s<=1.0; // alternative range test with single comparison // st = abs(st-0.5); return max(st.x,st.y)<0.5; } */ // https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdLine( in vec2 p, in vec2 a, in vec2 b) { vec2 pa = p-a, ba = b-a; float h = clamp(dot(pa,ba)/(dot(ba,ba)),0.0, 1.0); return length(pa-ba*h); } // https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdDisk( in vec2 p, in vec2 c, in float r ) { return length(p-c)-r; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { // animate vec2 a1 = vec2(-2.0+vec2(1.5,1.0)*sin(iTime*1.1+vec2(0.0,0.5))); vec2 b1 = vec2( 2.0+vec2(1.5,1.0)*sin(iTime*1.2+vec2(5.0,2.0))); vec2 a2 = vec2(-2.0+vec2(1.5,1.0)*sin(iTime*1.3+vec2(3.0,1.0))); vec2 b2 = vec2( 2.0+vec2(1.5,1.0)*sin(iTime*1.4+vec2(1.5,4.5))); // NDC coords vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y; // background vec3 col = vec3(0.15) - 0.04*length(p); p *= 3.5; // segment 1 { float d = sdLine(p,a1,b1)-0.02; d = min( d, sdDisk(p,a1,0.06) ); d = min( d, sdDisk(p,b1,0.06) ); col = mix(col, vec3(0.0,0.7,0.7), smoothstep(0.01,0.0,d) ); } // segment 2 { float d = sdLine(p,a2,b2)-0.02; d = min( d, sdDisk(p,a2,0.06) ); d = min( d, sdDisk(p,b2,0.06) ); col = mix(col, vec3(0.2,0.5,1.0), smoothstep(0.01,0.0,d) ); } // intersection vec2 pos; if( intersect(a1, b1, a2, b2, pos) ) { float d = sdDisk(p,pos,0.03); d = min( d, abs(d-0.2) ) - 0.01; // onion, see https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm col = mix(col, vec3(1.0,0.7,0.0), smoothstep(0.01,0.0,d)); } // cheap dither (color banding removal) col += (1.0/512.0)*sin(fragCoord.x*2.0+13.0*fragCoord.y); fragColor = vec4(col,1.0); }
mit
[ 1844, 1919, 1967, 1967, 2081 ]
[ [ 1114, 1114, 1149, 1149, 1177 ], [ 1179, 1179, 1249, 1249, 1462 ], [ 1844, 1919, 1967, 1967, 2081 ], [ 2083, 2158, 2208, 2208, 2236 ], [ 2238, 2238, 2295, 2310, 3645 ] ]
// https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float sdLine( in vec2 p, in vec2 a, in vec2 b) {
vec2 pa = p-a, ba = b-a; float h = clamp(dot(pa,ba)/(dot(ba,ba)),0.0, 1.0); return length(pa-ba*h); }
// https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm float sdLine( in vec2 p, in vec2 a, in vec2 b) {
1
1