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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
dls3Wr | mkeeter | 2022-12-21T17:04:32 | // Using distance-to-quadratic and winding number to generate a closed-form
// distance field of a font outline, which is specified as lines + quadratic
// Bézier curves.
//
// Quadratic solver is based on https://www.shadertoy.com/view/MlKcDD, which
// includes the following copyright notice:
//
// Copyright © 2018 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.
// Curves are baked by an external tool
#define QUAD_COUNT 38
const vec2 QUADS[QUAD_COUNT * 3] = vec2[QUAD_COUNT * 3](
vec2(0.5758487, -4.5724106), vec2(0.5758487, -4.9204984), vec2(0.7176622, -5.221315),
vec2(0.7176622, -5.221315), vec2(0.8594757, -5.5221314), vec2(1.1216158, -5.758487),
vec2(0.7520412, -2.0842285), vec2(0.8594757, -1.6974645), vec2(1.0034379, -1.3859046),
vec2(0.86162436, -3.5904598), vec2(0.5758487, -3.9664803), vec2(0.5758487, -4.5724106),
vec2(1.0034379, -1.3859046), vec2(1.1474, -1.0743446), vec2(1.3837559, -0.8036098),
vec2(1.1216158, -5.758487), vec2(1.3708637, -5.9819508), vec2(1.7103566, -6.1087236),
vec2(1.3794585, -4.9591746), vec2(1.3794585, -4.563816), vec2(1.5642458, -4.3038244),
vec2(1.3837559, -0.8036098), vec2(1.6072196, -0.55006444), vec2(1.9058874, -0.3996562),
vec2(1.5642458, -4.3038244), vec2(1.7490331, -4.0438333), vec2(2.101418, -3.8762355),
vec2(1.6630855, -2.9823806), vec2(1.1474, -3.2144392), vec2(0.86162436, -3.5904598),
vec2(1.6673828, -5.599484), vec2(1.3794585, -5.337344), vec2(1.3794585, -4.9591746),
vec2(1.6974645, -0.0021486892), vec2(1.2892135, -0.12892136), vec2(0.99269444, -0.30941126),
vec2(1.7103566, -6.1087236), vec2(2.0498495, -6.235496), vec2(2.419424, -6.235496),
vec2(1.9058874, -0.3996562), vec2(2.2045553, -0.24924795), vec2(2.599914, -0.24924795),
vec2(2.101418, -3.8762355), vec2(2.4151268, -3.7258272), vec2(2.718092, -3.616244),
vec2(2.2862053, -2.7266867), vec2(1.9252255, -2.862054), vec2(1.6630855, -2.9823806),
vec2(2.363558, -5.8616242), vec2(1.9553072, -5.8616242), vec2(1.6673828, -5.599484),
vec2(2.419424, -6.235496), vec2(2.840567, -6.235496), vec2(3.173614, -6.106575),
vec2(2.5139663, 0.12462398), vec2(2.1057155, 0.12462398), vec2(1.6974645, -0.0021486892),
vec2(2.599914, -0.24924795), vec2(2.896433, -0.24924795), vec2(3.117748, -0.32660076),
vec2(2.718092, -3.616244), vec2(3.0210571, -3.506661), vec2(3.3046842, -3.382037),
vec2(2.9265146, -2.4795873), vec2(2.647185, -2.5913193), vec2(2.2862053, -2.7266867),
vec2(3.0167596, -5.7133646), vec2(2.7503223, -5.8616242), vec2(2.363558, -5.8616242),
vec2(3.117748, -0.32660076), vec2(3.3390632, -0.40395358), vec2(3.4765792, -0.54576707),
vec2(3.173614, -6.106575), vec2(3.506661, -5.9776535), vec2(3.781693, -5.8057585),
vec2(3.3046842, -3.382037), vec2(3.5625267, -3.270305), vec2(3.8010314, -3.1284916),
vec2(3.4679844, -5.315857), vec2(3.2831972, -5.565105), vec2(3.0167596, -5.7133646),
vec2(3.4765792, -0.54576707), vec2(3.6140952, -0.6875806), vec2(3.6807046, -0.8788139),
vec2(3.54104, -2.0004296), vec2(3.3347657, -2.316287), vec2(2.9265146, -2.4795873),
vec2(3.6807046, -0.8788139), vec2(3.747314, -1.0700473), vec2(3.747314, -1.3192952),
vec2(3.747314, -1.3192952), vec2(3.747314, -1.6845723), vec2(3.54104, -2.0004296),
vec2(3.775247, -4.7400084), vec2(3.657069, -5.0580144), vec2(3.4679844, -5.315857),
vec2(3.8010314, -3.1284916), vec2(4.039536, -2.9866781), vec2(4.2157283, -2.7975934),
vec2(3.9879673, -4.073915), vec2(3.893425, -4.4220023), vec2(3.775247, -4.7400084),
vec2(3.996562, -0.3996562), vec2(3.3992264, 0.12462398), vec2(2.5139663, 0.12462398),
vec2(4.2157283, -2.7975934), vec2(4.4091105, -2.5827246), vec2(4.501504, -2.3270304),
vec2(4.501504, -2.3270304), vec2(4.593898, -2.0713365), vec2(4.593898, -1.7318435),
vec2(4.593898, -1.7318435), vec2(4.593898, -0.92393637), vec2(3.996562, -0.3996562)
);
#define LINE_COUNT 8
const vec2 LINES[LINE_COUNT * 2] = vec2[LINE_COUNT * 2](
vec2(0.40395358, -2.0842285), vec2(0.7520412, -2.0842285),
vec2(0.46411687, 0.0042973785), vec2(0.40395358, -2.0842285),
vec2(0.80790716, 0.0042973785), vec2(0.46411687, 0.0042973785),
vec2(0.99269444, -0.30941126), vec2(0.80790716, 0.0042973785),
vec2(3.781693, -5.8057585), vec2(3.9578855, -6.09798),
vec2(3.9578855, -6.09798), vec2(4.301676, -6.09798),
vec2(4.301676, -6.09798), vec2(4.336055, -4.073915),
vec2(4.336055, -4.073915), vec2(3.9879673, -4.073915)
);
float dot2(in vec2 v) { return dot(v, v); }
float cro(in vec2 a, in vec2 b) { return a.x * b.y - a.y * b.x; }
// signed distance to a quadratic bezier
float sdBezier(in vec2 pos, in vec2 A, in vec2 B, in vec2 C) {
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 sgn = 0.0;
float p = ky - kx * kx;
float q = kx * (2.0 * kx * kx - 3.0 * ky) + kz;
float p3 = p * p * p;
float q2 = q * q;
float h = q2 + 4.0 * p3;
if(h >= 0.0) { // 1 root
h = sqrt(h);
vec2 x = (vec2(h, -h) - q) / 2.0;
// When p≈0 and p<0, h - q has catastrophic cancelation. So, we do
// h=√(q² + 4p³)=q·√(1 + 4p³/q²)=q·√(1 + w) instead. Now we approximate
// √ by a linear Taylor expansion into h≈q(1 + ½w) so that the q's
// cancel each other in h - q. Expanding and simplifying further we
// get x=vec2(p³/q, -p³/q - q). And using a second degree Taylor
// expansion instead: x=vec2(k, -k - q) with k=(1 - p³/q²)·p³/q
if(abs(abs(h/q) - 1.0) < 0.0001) {
float k = (1.0 - p3 / q2) * p3 / q; // quadratic approx
x = vec2(k, -k - q);
}
vec2 uv = sign(x) * pow(abs(x), vec2(1.0/3.0));
float t = clamp(uv.x + uv.y - kx, 0.0, 1.0);
vec2 q = d + (c + b * t) * t;
res = dot2(q);
sgn = cro(c + 2.0 * b * t, q);
} 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) * 1.732050808;
vec3 t = clamp(vec3(m + m, -n - m, n - m) * z - kx, 0.0, 1.0);
vec2 qx=d + (c + b * t.x) * t.x;
float dx = dot2(qx), sx = cro(c + 2.0 * b * t.x, qx);
vec2 qy=d + (c + b * t.y) * t.y;
float dy = dot2(qy);
float sy = cro(c + 2.0 * b * t.y, qy);
if (dx<dy) {
res=dx;
sgn=sx;
} else {
res=dy;
sgn=sy;
}
}
return sqrt(res) * sign(sgn);
}
// Source: https://www.shadertoy.com/view/wdBXRW
float winding_sign(in vec2 p, in vec2 a, in vec2 b) {
vec2 e = b - a;
vec2 w = p - a;
// winding number from http://geomalgorithms.com/a03-_inclusion.html
bvec3 cond = bvec3(p.y >= a.y,
p.y < b.y,
e.x*w.y > e.y*w.x);
if( all(cond) || all(not(cond))) {
return -1.0;
} else {
return 1.0;
}
}
float winding_angle(in vec2 p, in vec2 a, in vec2 b) {
float pa = dot2(a - p);
float pb = dot2(b - p);
float ab = dot2(a - b);
float ang = acos((pa + pb - ab) / (2.0 * sqrt(pa * pb)));
return sign(cro(a - p, b - p)) * ang;
}
float udSegment(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);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
p = (p + vec2(0.5, 0.75)) * vec2(4.0, -4.0);
vec2 m = (2.0 * iMouse.xy - iResolution.xy) / iResolution.y;
m = (m + vec2(0.5, 0.75)) * vec2(4.0, -4.0);
float d = 1e10;
float winding = 1.0;
for (int i=0; i < QUAD_COUNT; i++) {
vec2 v0 = QUADS[i * 3];
vec2 v1 = QUADS[i * 3 + 1];
vec2 v2 = QUADS[i * 3 + 2];
float sd = sdBezier(p, v0, v1, v2);
d = min(d, abs(sd));
if (sd > 0.0 == cro(v1 - v2, v1 - v0) < 0.0) {
winding *= winding_sign(p, v0, v1);
winding *= winding_sign(p, v1, v2);
} else {
winding *= winding_sign(p, v0, v2);
}
}
for (int i=0; i < LINE_COUNT; i++) {
vec2 v0 = LINES[i * 2];
vec2 v1 = LINES[i * 2 + 1];
d = min(d, udSegment(p, v0, v1));
winding *= winding_sign(p, v0, v1);
}
d *= winding;
// Apply a color based on signed distance
vec3 col = vec3(1.0) - vec3(0.1, 0.4, 0.7) * sign(d);
col *= 1.0 - exp(-4.0 * abs(d));
col *= 0.8 + 0.2 * cos(60.0 * d);
col = mix(col, vec3(1.0), 1.0 - smoothstep(0.0, 0.015, abs(d)));
// Draw the mouse stuff
if(iMouse.z > 0.001) {
float d = 1e10;
for (int i=0; i < QUAD_COUNT; i++) {
vec2 v0 = QUADS[i * 3];
vec2 v1 = QUADS[i * 3 + 1];
vec2 v2 = QUADS[i * 3 + 2];
d = min(d, abs(sdBezier(m, v0, v1, v2)));
}
for (int i=0; i < LINE_COUNT; i++) {
vec2 v0 = LINES[i * 2];
vec2 v1 = LINES[i * 2 + 1];
d = min(d, udSegment(m, v0, v1));
}
col = mix(col, vec3(1.0, 1.0, 0.0), 1.0 - smoothstep(0.0, 0.005, abs(length(p - m) - abs(d)) - 0.01));
col = mix(col, vec3(1.0, 1.0, 0.0), 1.0 - smoothstep(0.0, 0.005, length(p - m) - 0.05));
}
{ // Draw the skeleton of the Bezier curves
float d = 1e10;
for (int i=0; i < QUAD_COUNT; i++) {
vec2 v0 = QUADS[i * 3];
vec2 v1 = QUADS[i * 3 + 1];
vec2 v2 = QUADS[i * 3 + 2];
d = min(d, min(udSegment(p, v0, v1), udSegment(p, v1, v2)));
d = min(d, length(p - v0) - 0.05);
d = min(d, length(p - v1) - 0.05);
d = min(d, length(p - v2) - 0.05);
}
for (int i=0; i < LINE_COUNT; i++) {
vec2 v0 = LINES[i * 2];
vec2 v1 = LINES[i * 2 + 1];
d = min(d, udSegment(p, v0, v1));
d = min(d, length(p - v0) - 0.05);
d = min(d, length(p - v1) - 0.05);
}
col = mix(col, vec3(1, 0, 0), 1.0 - smoothstep(0.0, 0.014, d));
}
fragColor = vec4(col, 1.0);
} | mit | [
7709,
7758,
7811,
7811,
8140
] | [
[
5475,
5475,
5498,
5498,
5518
],
[
5519,
5519,
5552,
5552,
5584
],
[
5586,
5627,
5689,
5689,
7707
],
[
7709,
7758,
7811,
7811,
8140
],
[
8142,
8142,
8196,
8196,
8386
],
[
8388,
8388,
8438,
8438,
8570
],
[
8572,
8572,
8627,
8627,
11360
]
] | // Source: https://www.shadertoy.com/view/wdBXRW
| float winding_sign(in vec2 p, in vec2 a, in vec2 b) { |
vec2 e = b - a;
vec2 w = p - a;
// winding number from http://geomalgorithms.com/a03-_inclusion.html
bvec3 cond = bvec3(p.y >= a.y,
p.y < b.y,
e.x*w.y > e.y*w.x);
if( all(cond) || all(not(cond))) {
return -1.0;
} else {
return 1.0;
}
} | // Source: https://www.shadertoy.com/view/wdBXRW
float winding_sign(in vec2 p, in vec2 a, in vec2 b) { | 1 | 1 |
mllGzH | iq | 2022-12-18T05:04:33 | // The MIT License
// Copyright © 2022 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.
// Intersection of a ray and a wedge.
//
// List of ray-surface intersectors at https://www.shadertoy.com/playlist/l3dXRf
// and https://iquilezles.org/articles/intersectors
// takes:
// ro,rd = ray origin and direction
// s = wedge length, height and width
// returns:
// .x = distance to intersection
// .yzw = normal at intersection point
vec4 iWedge( in vec3 ro, in vec3 rd, in vec3 s )
{
// intersect box
vec3 m = 1.0/rd;
vec3 z = vec3(rd.x>=0.0?1.0:-1.0, rd.y>=0.0?1.0:-1.0, rd.z>=0.0?1.0:-1.0);
vec3 k = s*z;
vec3 t1 = (-ro - k)*m;
vec3 t2 = (-ro + k)*m;
float tn = max(max(t1.x, t1.y), t1.z);
float tf = min(min(t2.x, t2.y), t2.z);
if( tn>tf ) return vec4(-1.0);
// boolean with plane
float k1 = s.y*ro.x - s.x*ro.y;
float k2 = s.x*rd.y - s.y*rd.x;
float tp = k1/k2;
// enable this ONLY if the ray origin can be inside the wedge
/*
if( tn<0.0 )
{
if( tp>0.0 && tp<tf ) return vec4(tp,normalize(vec3(-s.y,s.x,0.0))); // plane
if( k1<0.0 ) return vec4(tf,step(t2,vec3(tf))*z); // box
return vec4(-1.0);
}
*/
if( k1>tn*k2 ) return vec4(tn,-step(tn,t1)*z); // box
if( tp>tn && tp<tf ) return vec4(tp,normalize(vec3(-s.y,s.x,0.0))); // plane
return vec4(-1.0);
}
vec3 pattern( in vec2 uv )
{
return vec3(0.6 + 0.4*smoothstep(-0.01,0.01,cos(uv.x*0.5)*cos(uv.y*0.5)))
*smoothstep(-1.0,-0.98,cos(uv.x))*smoothstep(-1.0,-0.98,cos(uv.y));
}
#define AA 3
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// camera movement
float an = 0.2*iTime;
vec3 ro = vec3( 1.0*sin(an), 0.4*sin(1.6*an), 1.0*cos(an) );
vec3 ta = vec3( 0.0, 0.01, 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));
// wedge
const vec3 siz = vec3(0.5,0.2,0.4);
// 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 = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
#endif
// ray direction
vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );
// background
vec3 col = vec3(0.08+0.02*rd.y)*(1.0-0.3*length(p));
// wedge
vec4 tnor = iWedge( ro, rd, siz );
if( tnor.x>0.0 )
{
float t = tnor.x;
vec3 pos = ro + t*rd;
vec3 nor = tnor.yzw;
// texture
vec3 mor = abs(nor);
vec2 uv = (mor.x>mor.y && mor.x>mor.z) ? pos.yz :
(mor.y>mor.z) ? pos.zx :
pos.xy;
col = pattern( 47.0*uv );
// lighting
vec3 lig = normalize(vec3(0.7,0.6,0.3));
vec3 hal = normalize(-rd+lig);
float dif = clamp( dot(nor,lig), 0.0, 1.0 );
float amb = clamp( 0.6 + 0.4*nor.y, 0.0, 1.0 );
col *= vec3(0.2,0.3,0.4)*amb + vec3(1.0,0.9,0.7)*dif;
col += 0.4*pow(clamp(dot(hal,nor),0.0,1.0),12.0)*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,17,11)+fragCoord.y*vec3(1,7,5))*158.391832)/255.0;
fragColor = vec4( tot, 1.0 );
} | mit | [
1254,
1434,
1484,
1505,
2395
] | [
[
1254,
1434,
1484,
1505,
2395
],
[
2397,
2397,
2425,
2425,
2584
]
] | // takes:
// ro,rd = ray origin and direction
// s = wedge length, height and width
// returns:
// .x = distance to intersection
// .yzw = normal at intersection point
| vec4 iWedge( in vec3 ro, in vec3 rd, in vec3 s )
{ |
vec3 m = 1.0/rd;
vec3 z = vec3(rd.x>=0.0?1.0:-1.0, rd.y>=0.0?1.0:-1.0, rd.z>=0.0?1.0:-1.0);
vec3 k = s*z;
vec3 t1 = (-ro - k)*m;
vec3 t2 = (-ro + k)*m;
float tn = max(max(t1.x, t1.y), t1.z);
float tf = min(min(t2.x, t2.y), t2.z);
if( tn>tf ) return vec4(-1.0);
// boolean with plane
float k1 = s.y*ro.x - s.x*ro.y;
float k2 = s.x*rd.y - s.y*rd.x;
float tp = k1/k2;
// enable this ONLY if the ray origin can be inside the wedge
/*
if( tn<0.0 )
{
if( tp>0.0 && tp<tf ) return vec4(tp,normalize(vec3(-s.y,s.x,0.0))); // plane
if( k1<0.0 ) return vec4(tf,step(t2,vec3(tf))*z); // box
return vec4(-1.0);
}
*/
if( k1>tn*k2 ) return vec4(tn,-step(tn,t1)*z); // box
if( tp>tn && tp<tf ) return vec4(tp,normalize(vec3(-s.y,s.x,0.0))); // plane
return vec4(-1.0);
} | // takes:
// ro,rd = ray origin and direction
// s = wedge length, height and width
// returns:
// .x = distance to intersection
// .yzw = normal at intersection point
vec4 iWedge( in vec3 ro, in vec3 rd, in vec3 s )
{ | 1 | 1 |
ctf3z8 | ttg | 2022-12-16T23:56:20 | /*
24-bit fixed * 12-mantissa-bit multiply
https://www.shadertoy.com/view/ctf3z8
Multiply 24-bit signed integer by float using 12 bits of mantissa into
whole part and fract part without loss of precision over the integer argument's
range.
Click left/right to compare one at a time.
Copyright 2022 Theron Tarigo. All rights reserved.
This file may be used and copied under the terms of the ISC License;
see end of file.
*/
// https://www.shadertoy.com/view/ctf3z8
// requires abs(a) < (1<<24), recognizes only 12 significand bits of b
// logical operation: returns fract(a*b+x), ret_n=floor(a*b+x)
float frac_mad_i24_m12_f32 (out int ret_n, int a, float b, float x) {
#ifdef SAFE
int _M=~0xFFF;
if (a!=(a&0xFFFFFF)) return 0.;
b = intBitsToFloat(floatBitsToInt(b)&_M);
#endif
vec2 p = vec2(a&0xFFF,a&~0xFFF)*b,i=floor(p),f=p-i,r=f+f.y+x;
ret_n = int(i.x+i.y+(r.y=floor(r.x)));
return r.x-r.y;
}
void mainImage (out vec4 O, vec2 f) {
vec2 uv = f/iResolution.xy;
float yplt=fract(uv.y*2.);
int i=int(floor(uv.x*160.))+0x1000000; // index
float b=intBitsToFloat(0x3da3d000); // frequency = 7.9986572e-02
float x=.2; // phase shift
float Af,Bf;
int Ai,Bi;
// mad f32 result (imprecise)
Af=fract(float(i)*b+x);
Ai=int(floor(float(i)*b+x));
// frac_mad_i24_m12_f32 result
Bf=frac_mad_i24_m12_f32(Bi,i,b,x);
vec3 col=vec3(0.);
if(uv.y>.5){
col.r=float(yplt<Af);
col.g=float(yplt<Bf);
}
else{
int shift=1341950;
int range=20;
col.r=float(yplt<float(Ai-shift)/float(range));
col.g=float(yplt<float(Bi-shift)/float(range));
}
if(iMouse.z>0.){
float m=iMouse.x/iResolution.x;
if(m<.5)col=vec3(col.r);
else col=vec3(col.g);
}
// sRGB output https://www.shadertoy.com/view/sl3cRs
{vec3 c=col;O.rgb=min(12.9*c,abs(1.054*pow(c,c-c+.4166)-.095)+.04);}
}
/*
Copyright 2022 Theron Tarigo
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
| isc | [
428,
603,
672,
672,
911
] | [
[
428,
603,
672,
672,
911
],
[
913,
913,
950,
950,
1848
]
] | // https://www.shadertoy.com/view/ctf3z8
// requires abs(a) < (1<<24), recognizes only 12 significand bits of b
// logical operation: returns fract(a*b+x), ret_n=floor(a*b+x)
| float frac_mad_i24_m12_f32 (out int ret_n, int a, float b, float x) { |
#ifdef SAFE
int _M=~0xFFF;
if (a!=(a&0xFFFFFF)) return 0.;
b = intBitsToFloat(floatBitsToInt(b)&_M);
#endif
vec2 p = vec2(a&0xFFF,a&~0xFFF)*b,i=floor(p),f=p-i,r=f+f.y+x;
ret_n = int(i.x+i.y+(r.y=floor(r.x)));
return r.x-r.y;
} | // https://www.shadertoy.com/view/ctf3z8
// requires abs(a) < (1<<24), recognizes only 12 significand bits of b
// logical operation: returns fract(a*b+x), ret_n=floor(a*b+x)
float frac_mad_i24_m12_f32 (out int ret_n, int a, float b, float x) { | 1 | 1 |
DdSXzW | fishy | 2022-12-02T00:28:26 | // 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 of the quadratic smooth minimum.
// (https://iquilezles.org/articles/smin)
//
// Please note that the smooth-minimum is not a SDF preserving
// operator. It's a good approximation when far enough from the
// surfaces, but quickly distorts near them. However, the gradient
// stays smaller than 1, which explains why it does NOT break
// raymarchers, but just slows them down.
//
// List of other 2D distances+gradients:
//
// https://iquilezles.org/articles/distgradfunctions2d
//
// 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 unfortunatelly
vec3 sdgSMin( in vec3 a, in vec3 b, in float k )
{
float h = max(k-abs(a.x-b.x),0.0);
float m = 0.25*h*h/k; // [0 - k/4] for [|a-b|=k - |a-b|=0]
float n = 0.50* h/k; // [0 - 1/2] for [|a-b|=k - |a-b|=0]
return vec3( min(a.x, b.x) - m,
mix(a.yz, b.yz, (a.x<b.x)?n:1.0-n) );
}
// .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgMin( in vec3 a, in vec3 b )
{
return (a.x<b.x) ? a : b;
}
vec3 sdgBox( in vec2 p, in vec2 b )
{
vec2 w = abs(p)-b;
vec2 s = vec2(p.x<0.0?-1:1,p.y<0.0?-1:1);
float g = max(w.x,w.y);
vec2 q = max(w,0.0);
float l = length(q);
return vec3( (g>0.0)?l : g,
s*((g>0.0)?q/l : ((w.x>w.y)?vec2(1,0):vec2(0,1))));
}
vec3 sdgSegment( in vec2 p, in vec2 a, in vec2 b )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 q = pa-h*ba;
float d = length(q);
return vec3(d,q/d);
}
vec3 map(vec2 p)
{
// sdf(p) and gradient(sdf(p))
vec3 dg1 = sdgBox(p,vec2(0.8,0.3));
vec3 dg2 = sdgSegment( p, vec2(-1.0,-0.5), vec2(0.7,0.7) ) - vec3(0.15,0.0,0.0);
return sdgSMin(dg1,dg2,0.2);
}
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;
// sdf(p) and gradient(sdf(p))
//vec3 dg = sdgMin(dg1,dg2);
vec3 dg = map(p);
float d = dg.x;
vec2 g = dg.yz;
// central differenes based gradient, for validation
// 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 *= 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)) );
if( iMouse.z>0.001 )
{
d = map(m).x;
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 | [
1648,
1758,
1808,
1808,
2068
] | [
[
1648,
1758,
1808,
1808,
2068
],
[
2070,
2165,
2202,
2202,
2234
],
[
2236,
2236,
2273,
2273,
2534
],
[
2536,
2536,
2588,
2588,
2756
],
[
2758,
2758,
2776,
2811,
2976
],
[
2978,
2978,
3035,
3035,
3932
]
] | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ < 1 unfortunatelly
| vec3 sdgSMin( in vec3 a, in vec3 b, in float k )
{ |
float h = max(k-abs(a.x-b.x),0.0);
float m = 0.25*h*h/k; // [0 - k/4] for [|a-b|=k - |a-b|=0]
float n = 0.50* h/k; // [0 - 1/2] for [|a-b|=k - |a-b|=0]
return vec3( min(a.x, b.x) - m,
mix(a.yz, b.yz, (a.x<b.x)?n:1.0-n) );
} | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ < 1 unfortunatelly
vec3 sdgSMin( in vec3 a, in vec3 b, in float k )
{ | 2 | 3 |
ctBSDG | Paul_31415 | 2023-02-15T18:23:43 | // 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.
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org
// Analytical distance to an 2D ellipse, which is more
// complicated than it seems. It ends up being a quartic
// equation, which can be resolved through a cubic, then
// a quadratic. Some steps through the derivation can be
// found in this article:
//
// https://iquilezles.org/articles/ellipsedist
//
//
// Ellipse distances related shaders:
//
// Analytical : https://www.shadertoy.com/view/4sS3zz
// Newton Trig : https://www.shadertoy.com/view/4lsXDN
// Newton No-Trig : https://www.shadertoy.com/view/tttfzr
// ?????????????? : https://www.shadertoy.com/view/tt3yz7
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
//
// and iquilezles.org/articles/distfunctions2d
float msign(in float x) { return (x<0.0)?-1.0:1.0; }
// MIT License
// Copyright © 2023 Paul Soulanille
// 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.
float cbrt(in float x) {
return sign(x)*pow(abs(x),1.0/3.0);
}
float quart(in float x) {
x *= x;
return x*x;
}
// Hyperbola
//
// y=1/t |
// x=t \__
// rotate 45° (we do this to be able to change the open angle via x and y scaling)
// x = t-1/t \_/
// y = t+1/t
// has min Y at 2 and focus at 2√2
//
// z,w s.t. |(x,y)-(z,w)| is min
// https://www.wolframalpha.com/input?i=d%2Fdt+%28%28a*%28t-1%2Ft%29-z%29%5E2%2B%28b*%28t%2B1%2Ft%29-w%29%5E2+%29+
// (2 a^2 t^4 - 2 a^2 - 2 a t^3 z - 2 a t z + 2 b^2 t^4 - 2 b^2 - 2 b t^3 w + 2 b t w)/t^3 = 0
// t^4 : 2 a^2 + 2 b^2
// t^3 : - 2 a z - 2 b w
// t^2 : 0
// t : - 2 a z + 2 b w
// 1 : - 2 a^2 - 2 b^2
// using: https://en.wikipedia.org/wiki/Quartic_equation#Summary_of_Ferrari's_method
// let r^2 = a^2 + b^2
// coeffs (A…E): 2r^2, -2(ab•zw), 0, -2((a,b)•(z,-w)), -2r^2
// α = -3/8 (B/A)^2 + C/A
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
//
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
// else
// P = - α^2/12 - γ
// Q = - α^3/108 + αγ/3 - β^2/8
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
// U = ∛R
//
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
// W = √(α + 2d)
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
//
//
// currently suffers from precision loss in some areas
// which can be shown by uncommenting "col = p.x<m.x?ploss: col;" in mainImage
//
vec2 pHyperbola( vec2 p, in vec2 ab){
//vec2 s1 = pHyperbola_sols(p,ab,1.0,1.0);
//vec2 s2 = pHyperbola_sols(p,ab,-1.0,1.0);
//vec2 s3 = pHyperbola_sols(p,ab,-1.0,-1.0);
//vec2 s4 = pHyperbola_sols(p,ab,1.0,-1.0);
//s1 = (length(s1-p)<length(s2-p))?s1:s2;
//s2 = (length(s3-p)<length(s4-p))?s3:s4;
//s1 = (length(s1-p)<length(s2-p))?s1:s2;
//return s1;
float sx = sign(p.x);
float r2 = ab.x*ab.x+ab.y*ab.y;
float az = ab.x*abs(p.x);
float bw = ab.y*p.y;
float A = 2.0*r2;
float B = -2.0*(az+bw);
float D = -2.0*(az-bw);
float boa = B/A;
float doa = D/A;
float boa2 = boa*boa;
// α = -3/8 (B/A)^2 + C/A
float alpha = -0.375 * boa2;
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
float beta = 0.125 * boa2*boa + doa;
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
float gamma = -3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa - 1.0;
// = (-3/256 boa^3 - 1/4 doa)*boa - 1
// _ ⎛az+bw⎞4 ⎛az+bw⎞⎛az-bw⎞
// ¯ -3 ⎝¯4¯r2⎠ - ⎝¯4¯r2⎠⎝¯r2¯¯⎠ - 1
// =
// -(3 x^4)/(256 d^4) - (3 x^3 y)/(64 d^4) - (9 x^2 y^2)/(128 d^4) - (3 x y^3)/(64 d^4) - (3 y^4)/(256 d^4) - x^2/(4 d^2) + y^2/(4 d^2) - 1
//float gamma = -3.0/256.0 * quart(az / r2) - 3.0*(az
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
//t = -0.25*boa + s1*sqrt(0.5*(-alpha + s2*sqrt(alpha * alpha - 4.0*gamma)));
// else
float alpha2 = alpha * alpha;
// P = - α^2/12 - γ
//float P = - alpha2 / 12.0 - gamma;
// P = - (3/8 * boa2)^2/12 - (-3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa - 1.0);
// = - 3/256 boa^4 + 3.0/256.0 * (boa2*boa2) + 0.25 * boa*doa + 1.0;
// = 0.25 * boa*doa + 1.0;
float P = 0.25 * boa*doa + 1.0;
// Q = - α^3/108 + αγ/3 - β^2/8
float Q = - alpha*(alpha2 / 108.0 - gamma/3.0) - beta*beta / 8.0;
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
float R = - Q * 0.5 + sign(-Q)*sqrt(abs(Q*Q*0.25 + P*P*P/27.0));
// ^^^ lowers cancellation error
// U = ∛R
float U = cbrt(R);
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
float dp56a = (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U));
float d = -5.0/6.0 * alpha + dp56a;
// W = √(α + 2d)
//float W = sqrt(abs(alpha + 2.0 * d));
// W = √(α + -5/3 α + (U==0? -2∛Q : 2U - 2P/(3U))
// = √(-2/3 α + )
float W = sqrt(abs(-2.0/3.0 * alpha + 2.0 * dp56a));
// W = √(-2/3 α + (U==0? -∛Q : U - P/(3U))
// case U==0
// W = √(-2/3 α -∛Q)
// = √(-2/3 α -∛(- α^3/108 + αγ/3 - β^2/8))
// case U!=0
// W = √(-2/3 α + U + (α^2/12 + γ)/(3U))
// = √(-2/3 α + U - P/(3U))
// W = √(-2/3 α + ∛R - P/(3∛R))
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
//float t1 = -boa*0.25 + 0.5 * (W + sqrt(abs(3.0*alpha + 2.0*d + 2.0*beta/W)));
//float t2 = -boa*0.25 + 0.5 * (-W + sqrt(abs(3.0*alpha + 2.0*d + -2.0*beta/W)));
// 3α + 2d = 3α + 2(-5/6 α + dp56a)
// = (3-5/3) α + 2 dp56a
// = 4/3 α + 2 dp56a
float t1 = -boa*0.25 + 0.5 * (W + sqrt(abs(4.0/3.0*alpha + 2.0*dp56a + 2.0*beta/W)));
float t2 = -boa*0.25 + 0.5 * (-W + sqrt(abs(4.0/3.0*alpha + 2.0*dp56a + -2.0*beta/W)));
float recip_t1 = 1.0/t1;
float recip_t2 = 1.0/t2;
vec2 p1 = vec2((t1-recip_t1)*sx,t1+recip_t1)*ab;
vec2 p2 = vec2((t2-recip_t2)*sx,t2+recip_t2)*ab;
return (length(p1-p)<length(p2-p) && p1.y>=0.0) || p2.y < 0.0?p1:p2;
}
//using the "locus of points" geometric defn of hyperbola
float inside_Hyperbola( vec2 p, in vec2 ab){
float sqrt2 = sqrt(2.0);//foci are at (0,±√2)
p /= ab;
p /= 2.0;
return sign(length(p-vec2(0,sqrt2))-length(p-vec2(0,-sqrt2))+2.0);
}
//precision loss testing
float add_ploss(in float a, in float b){
return -log2(abs(a+b)/(abs(a)+abs(b)))/24.0;
}
vec3 pHyperbola_loss( vec2 p, in vec2 ab)
{
float loss1 = 0.0;
float loss2 = 0.0;
float loss3 = 0.0;
float sx = sign(p.x);
float r2 = ab.x*ab.x+ab.y*ab.y;
float az = ab.x*abs(p.x);
float bw = ab.y*p.y;
float A = 2.0*r2;
float B = -2.0*(az+bw);
float D = -2.0*(az-bw);
float boa = B/A;
float doa = D/A;
float boa2 = boa*boa;
// α = -3/8 (B/A)^2 + C/A
float alpha = -0.375 * boa2;
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
float beta = 0.125 * boa2*boa + doa;
//loss3 = add_ploss(0.125 * boa2*boa, doa);
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
float gamma = -3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa - 1.0;
//loss3 = add_ploss(-3.0/256.0 * (boa2*boa2) ,- 0.25 * boa*doa-1.0);
loss3 = add_ploss(-3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa,-1.0);
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
//t = -0.25*boa + s1*sqrt(0.5*(-alpha + s2*sqrt(alpha * alpha - 4.0*gamma)));
// else
float alpha2 = alpha * alpha;
// P = - α^2/12 - γ
float P = - alpha2 / 12.0 - gamma;
loss1 = add_ploss(alpha2 / 12.0,gamma);
// Q = - α^3/108 + αγ/3 - β^2/8
float Q = - alpha*(alpha2 / 108.0 - gamma/3.0) - beta*beta / 8.0;
//loss3 = add_ploss(alpha2 / 108.0, - gamma/3.0);
//loss3 = add_ploss(- alpha*(alpha2 / 108.0 - gamma/3.0),- beta*beta / 8.0);
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
float R = - Q * 0.5 + sign(-Q)*sqrt(abs(Q*Q*0.25 + P*P*P/27.0));
//loss3 = add_ploss(Q*Q*0.25,P*P*P/27.0);
//loss3 = add_ploss(- Q * 0.5,sqrt(abs(Q*Q*0.25 + P*P*P/27.0))); //lines up with P in upper half, fixed
// U = ∛R
float U = cbrt(R);
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
float d = -5.0/6.0 * alpha + (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U));
//loss3 = add_ploss(-5.0/6.0 * alpha, (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U))); //not here
//loss3 = add_ploss(U, - P/(3.0*U));// not here
// W = √(α + 2d)
//float W = sqrt(abs(alpha + 2.0 * d));
//loss2 = add_ploss(alpha,2.0*d);
float W = sqrt(abs(-2.0/3.0 * alpha + 2.0 * (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U))));
loss2 = add_ploss(-2.0/3.0 * alpha,2.0*(abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U)));
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
// W = √(-2/3 α + (U==0? -∛Q : U - P/(3U))
// case U==0
// W = √(-2/3 α -∛Q)
// = √(-2/3 α -∛(- α^3/108 + αγ/3 - β^2/8))
// case U!=0
// W = √(-2/3 α + U + (α^2/12 + γ)/(3U))
// = √(-2/3 α + U - P/(3U))
// W = √(-2/3 α + ∛R - P/(3∛R))
//loss3 = add_ploss(-2.0/3.0 *alpha, + U);
float t1 = -boa*0.25 + 0.5 * (W + sqrt(abs(3.0*alpha + 2.0*d + 2.0*beta/W)));
//loss3 = add_ploss(-boa*0.25,0.5*(W+sqrt(abs(3.0*alpha + 2.0*d + 2.0*beta/W))));
float t2 = -boa*0.25 + 0.5 * (-W + sqrt(abs(3.0*alpha + 2.0*d + -2.0*beta/W)));
//loss3 = add_ploss(-boa*0.25,0.5*(-W+sqrt(abs(3.0*alpha + 2.0*d - 2.0*beta/W))));
float recip_t1 = 1.0/t1;
float recip_t2 = 1.0/t2;
vec2 p1 = vec2((t1-recip_t1)*sx,t1+recip_t1)*ab;
vec2 p2 = vec2((t2-recip_t2)*sx,t2+recip_t2)*ab;
vec2 result = (length(p1-p)<length(p2-p) && p1.y>=0.0) || p2.y < 0.0?p1:p2;
return vec3(loss1,loss2,loss3);
}
//for highlighting individual solutions
vec2 pHyperbola_sols( vec2 p, in vec2 ab, in float s1, in float s2 )
{
float t = 0.0;
float r2 = ab.x*ab.x+ab.y*ab.y;
float az = ab.x*p.x;
float bw = ab.y*p.y;
float A = 2.0*r2;
float B = -2.0*(az+bw);
float C = 0.0;
float D = -2.0*(az-bw);
float E = -2.0*r2;
float boa = B/A;
float coa = C/A;
float doa = D/A;
float eoa = E/A;
float boa2 = boa*boa;
// α = -3/8 (B/A)^2 + C/A
float alpha = -0.375 * boa2 + coa;
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
float beta = 0.125 * boa2*boa - 0.5*boa*coa + doa;
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
float gamma = -3.0/256.0 * (boa2*boa2) + 0.0625 * coa*boa2 - 0.25 * boa*doa + eoa;
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
//t = -0.25*boa + s1*sqrt(0.5*(-alpha + s2*sqrt(alpha * alpha - 4.0*gamma)));
// else
float alpha2 = alpha * alpha;
// P = - α^2/12 - γ
float P = - alpha2 / 12.0 - gamma;
// Q = - α^3/108 + αγ/3 - β^2/8
float Q = - alpha*(alpha2 / 108.0 - gamma/3.0) - beta*beta / 8.0;
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
float R = - Q * 0.5 + sign(-Q)*sqrt(abs(Q*Q*0.25 + P*P*P/27.0));
// U = ∛R
float U = cbrt(R);
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
float d = -5.0/6.0 * alpha + (U==0.0? -cbrt(Q) : U - P/(3.0*U));
// W = √(α + 2d)
float W = sqrt(alpha + 2.0 * d);
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
t = -boa*0.25 + 0.5 * (s1*W + s2*sqrt(abs(3.0*alpha + 2.0*d + s1*2.0*beta/W)));
float recip_t = 1.0/t;
return vec2(t-recip_t,t+recip_t)*ab;
}
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;
vec2 ra = (vec2(0.25,0.25) + 0.25*cos(iTime*vec2(1.1,1.3)+vec2(1.0,1.0) ))*0.25*(1.0625-sin(iTime));
vec2 pd = pHyperbola( p, ra);
float d = length(pd-p)*inside_Hyperbola(p,ra);
vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7);
col *= 1.0 - exp(-2.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)) );
vec3 ploss = pHyperbola_loss(p,ra);
//uncomment the next line to see a map of floating point precision loss on the solution
col = p.x<m.x?ploss: col;
if( iMouse.z>0.001 )
{
pd = pHyperbola(m, ra);
d = length(pd-m);
col = mix(col, vec3(1.0,1.0,1.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0030));
col = mix(col, vec3(1.0,1.0,1.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.030));
pd = pHyperbola_sols(m, ra,1.0,1.0);
d = length(pd-m);
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-pd)-0.025));
pd = pHyperbola_sols(m, ra,-1.0,1.0);
d = length(pd-m);
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0020));
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.020));
pd = pHyperbola_sols(m, ra,1.0,-1.0);
d = length(pd-m);
col = mix(col, vec3(0.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0015));
col = mix(col, vec3(0.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.015));
pd = pHyperbola_sols(m, ra,-1.0,-1.0);
d = length(pd-m);
col = mix(col, vec3(0.0,0.0,1.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0010));
col = mix(col, vec3(0.0,0.0,1.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.010));
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 | [
8184,
8242,
8286,
8286,
8439
] | [
[
1858,
1858,
1883,
1883,
1910
],
[
2993,
2993,
3017,
3017,
3059
],
[
3060,
3060,
3085,
3085,
3115
],
[
4329,
4467,
4504,
4857,
8182
],
[
8184,
8242,
8286,
8286,
8439
],
[
8442,
8467,
8507,
8507,
8558
],
[
8559,
8559,
8602,
8602,
12040
],
[
12043,
12083,
12153,
12153,
13776
],
[
13784,
13784,
13841,
13841,
15929
]
] | //using the "locus of points" geometric defn of hyperbola
| float inside_Hyperbola( vec2 p, in vec2 ab){ |
float sqrt2 = sqrt(2.0);//foci are at (0,±√2)
p /= ab;
p /= 2.0;
return sign(length(p-vec2(0,sqrt2))-length(p-vec2(0,-sqrt2))+2.0);
} | //using the "locus of points" geometric defn of hyperbola
float inside_Hyperbola( vec2 p, in vec2 ab){ | 1 | 1 |
ctBSDG | Paul_31415 | 2023-02-15T18:23:43 | // 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.
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org
// Analytical distance to an 2D ellipse, which is more
// complicated than it seems. It ends up being a quartic
// equation, which can be resolved through a cubic, then
// a quadratic. Some steps through the derivation can be
// found in this article:
//
// https://iquilezles.org/articles/ellipsedist
//
//
// Ellipse distances related shaders:
//
// Analytical : https://www.shadertoy.com/view/4sS3zz
// Newton Trig : https://www.shadertoy.com/view/4lsXDN
// Newton No-Trig : https://www.shadertoy.com/view/tttfzr
// ?????????????? : https://www.shadertoy.com/view/tt3yz7
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
//
// and iquilezles.org/articles/distfunctions2d
float msign(in float x) { return (x<0.0)?-1.0:1.0; }
// MIT License
// Copyright © 2023 Paul Soulanille
// 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.
float cbrt(in float x) {
return sign(x)*pow(abs(x),1.0/3.0);
}
float quart(in float x) {
x *= x;
return x*x;
}
// Hyperbola
//
// y=1/t |
// x=t \__
// rotate 45° (we do this to be able to change the open angle via x and y scaling)
// x = t-1/t \_/
// y = t+1/t
// has min Y at 2 and focus at 2√2
//
// z,w s.t. |(x,y)-(z,w)| is min
// https://www.wolframalpha.com/input?i=d%2Fdt+%28%28a*%28t-1%2Ft%29-z%29%5E2%2B%28b*%28t%2B1%2Ft%29-w%29%5E2+%29+
// (2 a^2 t^4 - 2 a^2 - 2 a t^3 z - 2 a t z + 2 b^2 t^4 - 2 b^2 - 2 b t^3 w + 2 b t w)/t^3 = 0
// t^4 : 2 a^2 + 2 b^2
// t^3 : - 2 a z - 2 b w
// t^2 : 0
// t : - 2 a z + 2 b w
// 1 : - 2 a^2 - 2 b^2
// using: https://en.wikipedia.org/wiki/Quartic_equation#Summary_of_Ferrari's_method
// let r^2 = a^2 + b^2
// coeffs (A…E): 2r^2, -2(ab•zw), 0, -2((a,b)•(z,-w)), -2r^2
// α = -3/8 (B/A)^2 + C/A
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
//
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
// else
// P = - α^2/12 - γ
// Q = - α^3/108 + αγ/3 - β^2/8
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
// U = ∛R
//
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
// W = √(α + 2d)
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
//
//
// currently suffers from precision loss in some areas
// which can be shown by uncommenting "col = p.x<m.x?ploss: col;" in mainImage
//
vec2 pHyperbola( vec2 p, in vec2 ab){
//vec2 s1 = pHyperbola_sols(p,ab,1.0,1.0);
//vec2 s2 = pHyperbola_sols(p,ab,-1.0,1.0);
//vec2 s3 = pHyperbola_sols(p,ab,-1.0,-1.0);
//vec2 s4 = pHyperbola_sols(p,ab,1.0,-1.0);
//s1 = (length(s1-p)<length(s2-p))?s1:s2;
//s2 = (length(s3-p)<length(s4-p))?s3:s4;
//s1 = (length(s1-p)<length(s2-p))?s1:s2;
//return s1;
float sx = sign(p.x);
float r2 = ab.x*ab.x+ab.y*ab.y;
float az = ab.x*abs(p.x);
float bw = ab.y*p.y;
float A = 2.0*r2;
float B = -2.0*(az+bw);
float D = -2.0*(az-bw);
float boa = B/A;
float doa = D/A;
float boa2 = boa*boa;
// α = -3/8 (B/A)^2 + C/A
float alpha = -0.375 * boa2;
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
float beta = 0.125 * boa2*boa + doa;
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
float gamma = -3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa - 1.0;
// = (-3/256 boa^3 - 1/4 doa)*boa - 1
// _ ⎛az+bw⎞4 ⎛az+bw⎞⎛az-bw⎞
// ¯ -3 ⎝¯4¯r2⎠ - ⎝¯4¯r2⎠⎝¯r2¯¯⎠ - 1
// =
// -(3 x^4)/(256 d^4) - (3 x^3 y)/(64 d^4) - (9 x^2 y^2)/(128 d^4) - (3 x y^3)/(64 d^4) - (3 y^4)/(256 d^4) - x^2/(4 d^2) + y^2/(4 d^2) - 1
//float gamma = -3.0/256.0 * quart(az / r2) - 3.0*(az
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
//t = -0.25*boa + s1*sqrt(0.5*(-alpha + s2*sqrt(alpha * alpha - 4.0*gamma)));
// else
float alpha2 = alpha * alpha;
// P = - α^2/12 - γ
//float P = - alpha2 / 12.0 - gamma;
// P = - (3/8 * boa2)^2/12 - (-3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa - 1.0);
// = - 3/256 boa^4 + 3.0/256.0 * (boa2*boa2) + 0.25 * boa*doa + 1.0;
// = 0.25 * boa*doa + 1.0;
float P = 0.25 * boa*doa + 1.0;
// Q = - α^3/108 + αγ/3 - β^2/8
float Q = - alpha*(alpha2 / 108.0 - gamma/3.0) - beta*beta / 8.0;
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
float R = - Q * 0.5 + sign(-Q)*sqrt(abs(Q*Q*0.25 + P*P*P/27.0));
// ^^^ lowers cancellation error
// U = ∛R
float U = cbrt(R);
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
float dp56a = (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U));
float d = -5.0/6.0 * alpha + dp56a;
// W = √(α + 2d)
//float W = sqrt(abs(alpha + 2.0 * d));
// W = √(α + -5/3 α + (U==0? -2∛Q : 2U - 2P/(3U))
// = √(-2/3 α + )
float W = sqrt(abs(-2.0/3.0 * alpha + 2.0 * dp56a));
// W = √(-2/3 α + (U==0? -∛Q : U - P/(3U))
// case U==0
// W = √(-2/3 α -∛Q)
// = √(-2/3 α -∛(- α^3/108 + αγ/3 - β^2/8))
// case U!=0
// W = √(-2/3 α + U + (α^2/12 + γ)/(3U))
// = √(-2/3 α + U - P/(3U))
// W = √(-2/3 α + ∛R - P/(3∛R))
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
//float t1 = -boa*0.25 + 0.5 * (W + sqrt(abs(3.0*alpha + 2.0*d + 2.0*beta/W)));
//float t2 = -boa*0.25 + 0.5 * (-W + sqrt(abs(3.0*alpha + 2.0*d + -2.0*beta/W)));
// 3α + 2d = 3α + 2(-5/6 α + dp56a)
// = (3-5/3) α + 2 dp56a
// = 4/3 α + 2 dp56a
float t1 = -boa*0.25 + 0.5 * (W + sqrt(abs(4.0/3.0*alpha + 2.0*dp56a + 2.0*beta/W)));
float t2 = -boa*0.25 + 0.5 * (-W + sqrt(abs(4.0/3.0*alpha + 2.0*dp56a + -2.0*beta/W)));
float recip_t1 = 1.0/t1;
float recip_t2 = 1.0/t2;
vec2 p1 = vec2((t1-recip_t1)*sx,t1+recip_t1)*ab;
vec2 p2 = vec2((t2-recip_t2)*sx,t2+recip_t2)*ab;
return (length(p1-p)<length(p2-p) && p1.y>=0.0) || p2.y < 0.0?p1:p2;
}
//using the "locus of points" geometric defn of hyperbola
float inside_Hyperbola( vec2 p, in vec2 ab){
float sqrt2 = sqrt(2.0);//foci are at (0,±√2)
p /= ab;
p /= 2.0;
return sign(length(p-vec2(0,sqrt2))-length(p-vec2(0,-sqrt2))+2.0);
}
//precision loss testing
float add_ploss(in float a, in float b){
return -log2(abs(a+b)/(abs(a)+abs(b)))/24.0;
}
vec3 pHyperbola_loss( vec2 p, in vec2 ab)
{
float loss1 = 0.0;
float loss2 = 0.0;
float loss3 = 0.0;
float sx = sign(p.x);
float r2 = ab.x*ab.x+ab.y*ab.y;
float az = ab.x*abs(p.x);
float bw = ab.y*p.y;
float A = 2.0*r2;
float B = -2.0*(az+bw);
float D = -2.0*(az-bw);
float boa = B/A;
float doa = D/A;
float boa2 = boa*boa;
// α = -3/8 (B/A)^2 + C/A
float alpha = -0.375 * boa2;
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
float beta = 0.125 * boa2*boa + doa;
//loss3 = add_ploss(0.125 * boa2*boa, doa);
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
float gamma = -3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa - 1.0;
//loss3 = add_ploss(-3.0/256.0 * (boa2*boa2) ,- 0.25 * boa*doa-1.0);
loss3 = add_ploss(-3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa,-1.0);
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
//t = -0.25*boa + s1*sqrt(0.5*(-alpha + s2*sqrt(alpha * alpha - 4.0*gamma)));
// else
float alpha2 = alpha * alpha;
// P = - α^2/12 - γ
float P = - alpha2 / 12.0 - gamma;
loss1 = add_ploss(alpha2 / 12.0,gamma);
// Q = - α^3/108 + αγ/3 - β^2/8
float Q = - alpha*(alpha2 / 108.0 - gamma/3.0) - beta*beta / 8.0;
//loss3 = add_ploss(alpha2 / 108.0, - gamma/3.0);
//loss3 = add_ploss(- alpha*(alpha2 / 108.0 - gamma/3.0),- beta*beta / 8.0);
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
float R = - Q * 0.5 + sign(-Q)*sqrt(abs(Q*Q*0.25 + P*P*P/27.0));
//loss3 = add_ploss(Q*Q*0.25,P*P*P/27.0);
//loss3 = add_ploss(- Q * 0.5,sqrt(abs(Q*Q*0.25 + P*P*P/27.0))); //lines up with P in upper half, fixed
// U = ∛R
float U = cbrt(R);
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
float d = -5.0/6.0 * alpha + (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U));
//loss3 = add_ploss(-5.0/6.0 * alpha, (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U))); //not here
//loss3 = add_ploss(U, - P/(3.0*U));// not here
// W = √(α + 2d)
//float W = sqrt(abs(alpha + 2.0 * d));
//loss2 = add_ploss(alpha,2.0*d);
float W = sqrt(abs(-2.0/3.0 * alpha + 2.0 * (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U))));
loss2 = add_ploss(-2.0/3.0 * alpha,2.0*(abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U)));
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
// W = √(-2/3 α + (U==0? -∛Q : U - P/(3U))
// case U==0
// W = √(-2/3 α -∛Q)
// = √(-2/3 α -∛(- α^3/108 + αγ/3 - β^2/8))
// case U!=0
// W = √(-2/3 α + U + (α^2/12 + γ)/(3U))
// = √(-2/3 α + U - P/(3U))
// W = √(-2/3 α + ∛R - P/(3∛R))
//loss3 = add_ploss(-2.0/3.0 *alpha, + U);
float t1 = -boa*0.25 + 0.5 * (W + sqrt(abs(3.0*alpha + 2.0*d + 2.0*beta/W)));
//loss3 = add_ploss(-boa*0.25,0.5*(W+sqrt(abs(3.0*alpha + 2.0*d + 2.0*beta/W))));
float t2 = -boa*0.25 + 0.5 * (-W + sqrt(abs(3.0*alpha + 2.0*d + -2.0*beta/W)));
//loss3 = add_ploss(-boa*0.25,0.5*(-W+sqrt(abs(3.0*alpha + 2.0*d - 2.0*beta/W))));
float recip_t1 = 1.0/t1;
float recip_t2 = 1.0/t2;
vec2 p1 = vec2((t1-recip_t1)*sx,t1+recip_t1)*ab;
vec2 p2 = vec2((t2-recip_t2)*sx,t2+recip_t2)*ab;
vec2 result = (length(p1-p)<length(p2-p) && p1.y>=0.0) || p2.y < 0.0?p1:p2;
return vec3(loss1,loss2,loss3);
}
//for highlighting individual solutions
vec2 pHyperbola_sols( vec2 p, in vec2 ab, in float s1, in float s2 )
{
float t = 0.0;
float r2 = ab.x*ab.x+ab.y*ab.y;
float az = ab.x*p.x;
float bw = ab.y*p.y;
float A = 2.0*r2;
float B = -2.0*(az+bw);
float C = 0.0;
float D = -2.0*(az-bw);
float E = -2.0*r2;
float boa = B/A;
float coa = C/A;
float doa = D/A;
float eoa = E/A;
float boa2 = boa*boa;
// α = -3/8 (B/A)^2 + C/A
float alpha = -0.375 * boa2 + coa;
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
float beta = 0.125 * boa2*boa - 0.5*boa*coa + doa;
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
float gamma = -3.0/256.0 * (boa2*boa2) + 0.0625 * coa*boa2 - 0.25 * boa*doa + eoa;
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
//t = -0.25*boa + s1*sqrt(0.5*(-alpha + s2*sqrt(alpha * alpha - 4.0*gamma)));
// else
float alpha2 = alpha * alpha;
// P = - α^2/12 - γ
float P = - alpha2 / 12.0 - gamma;
// Q = - α^3/108 + αγ/3 - β^2/8
float Q = - alpha*(alpha2 / 108.0 - gamma/3.0) - beta*beta / 8.0;
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
float R = - Q * 0.5 + sign(-Q)*sqrt(abs(Q*Q*0.25 + P*P*P/27.0));
// U = ∛R
float U = cbrt(R);
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
float d = -5.0/6.0 * alpha + (U==0.0? -cbrt(Q) : U - P/(3.0*U));
// W = √(α + 2d)
float W = sqrt(alpha + 2.0 * d);
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
t = -boa*0.25 + 0.5 * (s1*W + s2*sqrt(abs(3.0*alpha + 2.0*d + s1*2.0*beta/W)));
float recip_t = 1.0/t;
return vec2(t-recip_t,t+recip_t)*ab;
}
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;
vec2 ra = (vec2(0.25,0.25) + 0.25*cos(iTime*vec2(1.1,1.3)+vec2(1.0,1.0) ))*0.25*(1.0625-sin(iTime));
vec2 pd = pHyperbola( p, ra);
float d = length(pd-p)*inside_Hyperbola(p,ra);
vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7);
col *= 1.0 - exp(-2.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)) );
vec3 ploss = pHyperbola_loss(p,ra);
//uncomment the next line to see a map of floating point precision loss on the solution
col = p.x<m.x?ploss: col;
if( iMouse.z>0.001 )
{
pd = pHyperbola(m, ra);
d = length(pd-m);
col = mix(col, vec3(1.0,1.0,1.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0030));
col = mix(col, vec3(1.0,1.0,1.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.030));
pd = pHyperbola_sols(m, ra,1.0,1.0);
d = length(pd-m);
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-pd)-0.025));
pd = pHyperbola_sols(m, ra,-1.0,1.0);
d = length(pd-m);
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0020));
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.020));
pd = pHyperbola_sols(m, ra,1.0,-1.0);
d = length(pd-m);
col = mix(col, vec3(0.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0015));
col = mix(col, vec3(0.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.015));
pd = pHyperbola_sols(m, ra,-1.0,-1.0);
d = length(pd-m);
col = mix(col, vec3(0.0,0.0,1.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0010));
col = mix(col, vec3(0.0,0.0,1.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.010));
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 | [
8442,
8467,
8507,
8507,
8558
] | [
[
1858,
1858,
1883,
1883,
1910
],
[
2993,
2993,
3017,
3017,
3059
],
[
3060,
3060,
3085,
3085,
3115
],
[
4329,
4467,
4504,
4857,
8182
],
[
8184,
8242,
8286,
8286,
8439
],
[
8442,
8467,
8507,
8507,
8558
],
[
8559,
8559,
8602,
8602,
12040
],
[
12043,
12083,
12153,
12153,
13776
],
[
13784,
13784,
13841,
13841,
15929
]
] | //precision loss testing
| float add_ploss(in float a, in float b){ |
return -log2(abs(a+b)/(abs(a)+abs(b)))/24.0;
} | //precision loss testing
float add_ploss(in float a, in float b){ | 1 | 1 |
ctBSDG | Paul_31415 | 2023-02-15T18:23:43 | // 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.
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org
// Analytical distance to an 2D ellipse, which is more
// complicated than it seems. It ends up being a quartic
// equation, which can be resolved through a cubic, then
// a quadratic. Some steps through the derivation can be
// found in this article:
//
// https://iquilezles.org/articles/ellipsedist
//
//
// Ellipse distances related shaders:
//
// Analytical : https://www.shadertoy.com/view/4sS3zz
// Newton Trig : https://www.shadertoy.com/view/4lsXDN
// Newton No-Trig : https://www.shadertoy.com/view/tttfzr
// ?????????????? : https://www.shadertoy.com/view/tt3yz7
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
//
// and iquilezles.org/articles/distfunctions2d
float msign(in float x) { return (x<0.0)?-1.0:1.0; }
// MIT License
// Copyright © 2023 Paul Soulanille
// 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.
float cbrt(in float x) {
return sign(x)*pow(abs(x),1.0/3.0);
}
float quart(in float x) {
x *= x;
return x*x;
}
// Hyperbola
//
// y=1/t |
// x=t \__
// rotate 45° (we do this to be able to change the open angle via x and y scaling)
// x = t-1/t \_/
// y = t+1/t
// has min Y at 2 and focus at 2√2
//
// z,w s.t. |(x,y)-(z,w)| is min
// https://www.wolframalpha.com/input?i=d%2Fdt+%28%28a*%28t-1%2Ft%29-z%29%5E2%2B%28b*%28t%2B1%2Ft%29-w%29%5E2+%29+
// (2 a^2 t^4 - 2 a^2 - 2 a t^3 z - 2 a t z + 2 b^2 t^4 - 2 b^2 - 2 b t^3 w + 2 b t w)/t^3 = 0
// t^4 : 2 a^2 + 2 b^2
// t^3 : - 2 a z - 2 b w
// t^2 : 0
// t : - 2 a z + 2 b w
// 1 : - 2 a^2 - 2 b^2
// using: https://en.wikipedia.org/wiki/Quartic_equation#Summary_of_Ferrari's_method
// let r^2 = a^2 + b^2
// coeffs (A…E): 2r^2, -2(ab•zw), 0, -2((a,b)•(z,-w)), -2r^2
// α = -3/8 (B/A)^2 + C/A
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
//
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
// else
// P = - α^2/12 - γ
// Q = - α^3/108 + αγ/3 - β^2/8
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
// U = ∛R
//
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
// W = √(α + 2d)
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
//
//
// currently suffers from precision loss in some areas
// which can be shown by uncommenting "col = p.x<m.x?ploss: col;" in mainImage
//
vec2 pHyperbola( vec2 p, in vec2 ab){
//vec2 s1 = pHyperbola_sols(p,ab,1.0,1.0);
//vec2 s2 = pHyperbola_sols(p,ab,-1.0,1.0);
//vec2 s3 = pHyperbola_sols(p,ab,-1.0,-1.0);
//vec2 s4 = pHyperbola_sols(p,ab,1.0,-1.0);
//s1 = (length(s1-p)<length(s2-p))?s1:s2;
//s2 = (length(s3-p)<length(s4-p))?s3:s4;
//s1 = (length(s1-p)<length(s2-p))?s1:s2;
//return s1;
float sx = sign(p.x);
float r2 = ab.x*ab.x+ab.y*ab.y;
float az = ab.x*abs(p.x);
float bw = ab.y*p.y;
float A = 2.0*r2;
float B = -2.0*(az+bw);
float D = -2.0*(az-bw);
float boa = B/A;
float doa = D/A;
float boa2 = boa*boa;
// α = -3/8 (B/A)^2 + C/A
float alpha = -0.375 * boa2;
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
float beta = 0.125 * boa2*boa + doa;
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
float gamma = -3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa - 1.0;
// = (-3/256 boa^3 - 1/4 doa)*boa - 1
// _ ⎛az+bw⎞4 ⎛az+bw⎞⎛az-bw⎞
// ¯ -3 ⎝¯4¯r2⎠ - ⎝¯4¯r2⎠⎝¯r2¯¯⎠ - 1
// =
// -(3 x^4)/(256 d^4) - (3 x^3 y)/(64 d^4) - (9 x^2 y^2)/(128 d^4) - (3 x y^3)/(64 d^4) - (3 y^4)/(256 d^4) - x^2/(4 d^2) + y^2/(4 d^2) - 1
//float gamma = -3.0/256.0 * quart(az / r2) - 3.0*(az
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
//t = -0.25*boa + s1*sqrt(0.5*(-alpha + s2*sqrt(alpha * alpha - 4.0*gamma)));
// else
float alpha2 = alpha * alpha;
// P = - α^2/12 - γ
//float P = - alpha2 / 12.0 - gamma;
// P = - (3/8 * boa2)^2/12 - (-3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa - 1.0);
// = - 3/256 boa^4 + 3.0/256.0 * (boa2*boa2) + 0.25 * boa*doa + 1.0;
// = 0.25 * boa*doa + 1.0;
float P = 0.25 * boa*doa + 1.0;
// Q = - α^3/108 + αγ/3 - β^2/8
float Q = - alpha*(alpha2 / 108.0 - gamma/3.0) - beta*beta / 8.0;
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
float R = - Q * 0.5 + sign(-Q)*sqrt(abs(Q*Q*0.25 + P*P*P/27.0));
// ^^^ lowers cancellation error
// U = ∛R
float U = cbrt(R);
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
float dp56a = (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U));
float d = -5.0/6.0 * alpha + dp56a;
// W = √(α + 2d)
//float W = sqrt(abs(alpha + 2.0 * d));
// W = √(α + -5/3 α + (U==0? -2∛Q : 2U - 2P/(3U))
// = √(-2/3 α + )
float W = sqrt(abs(-2.0/3.0 * alpha + 2.0 * dp56a));
// W = √(-2/3 α + (U==0? -∛Q : U - P/(3U))
// case U==0
// W = √(-2/3 α -∛Q)
// = √(-2/3 α -∛(- α^3/108 + αγ/3 - β^2/8))
// case U!=0
// W = √(-2/3 α + U + (α^2/12 + γ)/(3U))
// = √(-2/3 α + U - P/(3U))
// W = √(-2/3 α + ∛R - P/(3∛R))
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
//float t1 = -boa*0.25 + 0.5 * (W + sqrt(abs(3.0*alpha + 2.0*d + 2.0*beta/W)));
//float t2 = -boa*0.25 + 0.5 * (-W + sqrt(abs(3.0*alpha + 2.0*d + -2.0*beta/W)));
// 3α + 2d = 3α + 2(-5/6 α + dp56a)
// = (3-5/3) α + 2 dp56a
// = 4/3 α + 2 dp56a
float t1 = -boa*0.25 + 0.5 * (W + sqrt(abs(4.0/3.0*alpha + 2.0*dp56a + 2.0*beta/W)));
float t2 = -boa*0.25 + 0.5 * (-W + sqrt(abs(4.0/3.0*alpha + 2.0*dp56a + -2.0*beta/W)));
float recip_t1 = 1.0/t1;
float recip_t2 = 1.0/t2;
vec2 p1 = vec2((t1-recip_t1)*sx,t1+recip_t1)*ab;
vec2 p2 = vec2((t2-recip_t2)*sx,t2+recip_t2)*ab;
return (length(p1-p)<length(p2-p) && p1.y>=0.0) || p2.y < 0.0?p1:p2;
}
//using the "locus of points" geometric defn of hyperbola
float inside_Hyperbola( vec2 p, in vec2 ab){
float sqrt2 = sqrt(2.0);//foci are at (0,±√2)
p /= ab;
p /= 2.0;
return sign(length(p-vec2(0,sqrt2))-length(p-vec2(0,-sqrt2))+2.0);
}
//precision loss testing
float add_ploss(in float a, in float b){
return -log2(abs(a+b)/(abs(a)+abs(b)))/24.0;
}
vec3 pHyperbola_loss( vec2 p, in vec2 ab)
{
float loss1 = 0.0;
float loss2 = 0.0;
float loss3 = 0.0;
float sx = sign(p.x);
float r2 = ab.x*ab.x+ab.y*ab.y;
float az = ab.x*abs(p.x);
float bw = ab.y*p.y;
float A = 2.0*r2;
float B = -2.0*(az+bw);
float D = -2.0*(az-bw);
float boa = B/A;
float doa = D/A;
float boa2 = boa*boa;
// α = -3/8 (B/A)^2 + C/A
float alpha = -0.375 * boa2;
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
float beta = 0.125 * boa2*boa + doa;
//loss3 = add_ploss(0.125 * boa2*boa, doa);
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
float gamma = -3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa - 1.0;
//loss3 = add_ploss(-3.0/256.0 * (boa2*boa2) ,- 0.25 * boa*doa-1.0);
loss3 = add_ploss(-3.0/256.0 * (boa2*boa2) - 0.25 * boa*doa,-1.0);
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
//t = -0.25*boa + s1*sqrt(0.5*(-alpha + s2*sqrt(alpha * alpha - 4.0*gamma)));
// else
float alpha2 = alpha * alpha;
// P = - α^2/12 - γ
float P = - alpha2 / 12.0 - gamma;
loss1 = add_ploss(alpha2 / 12.0,gamma);
// Q = - α^3/108 + αγ/3 - β^2/8
float Q = - alpha*(alpha2 / 108.0 - gamma/3.0) - beta*beta / 8.0;
//loss3 = add_ploss(alpha2 / 108.0, - gamma/3.0);
//loss3 = add_ploss(- alpha*(alpha2 / 108.0 - gamma/3.0),- beta*beta / 8.0);
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
float R = - Q * 0.5 + sign(-Q)*sqrt(abs(Q*Q*0.25 + P*P*P/27.0));
//loss3 = add_ploss(Q*Q*0.25,P*P*P/27.0);
//loss3 = add_ploss(- Q * 0.5,sqrt(abs(Q*Q*0.25 + P*P*P/27.0))); //lines up with P in upper half, fixed
// U = ∛R
float U = cbrt(R);
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
float d = -5.0/6.0 * alpha + (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U));
//loss3 = add_ploss(-5.0/6.0 * alpha, (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U))); //not here
//loss3 = add_ploss(U, - P/(3.0*U));// not here
// W = √(α + 2d)
//float W = sqrt(abs(alpha + 2.0 * d));
//loss2 = add_ploss(alpha,2.0*d);
float W = sqrt(abs(-2.0/3.0 * alpha + 2.0 * (abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U))));
loss2 = add_ploss(-2.0/3.0 * alpha,2.0*(abs(U)<=0.0? -cbrt(Q) : U - P/(3.0*U)));
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
// W = √(-2/3 α + (U==0? -∛Q : U - P/(3U))
// case U==0
// W = √(-2/3 α -∛Q)
// = √(-2/3 α -∛(- α^3/108 + αγ/3 - β^2/8))
// case U!=0
// W = √(-2/3 α + U + (α^2/12 + γ)/(3U))
// = √(-2/3 α + U - P/(3U))
// W = √(-2/3 α + ∛R - P/(3∛R))
//loss3 = add_ploss(-2.0/3.0 *alpha, + U);
float t1 = -boa*0.25 + 0.5 * (W + sqrt(abs(3.0*alpha + 2.0*d + 2.0*beta/W)));
//loss3 = add_ploss(-boa*0.25,0.5*(W+sqrt(abs(3.0*alpha + 2.0*d + 2.0*beta/W))));
float t2 = -boa*0.25 + 0.5 * (-W + sqrt(abs(3.0*alpha + 2.0*d + -2.0*beta/W)));
//loss3 = add_ploss(-boa*0.25,0.5*(-W+sqrt(abs(3.0*alpha + 2.0*d - 2.0*beta/W))));
float recip_t1 = 1.0/t1;
float recip_t2 = 1.0/t2;
vec2 p1 = vec2((t1-recip_t1)*sx,t1+recip_t1)*ab;
vec2 p2 = vec2((t2-recip_t2)*sx,t2+recip_t2)*ab;
vec2 result = (length(p1-p)<length(p2-p) && p1.y>=0.0) || p2.y < 0.0?p1:p2;
return vec3(loss1,loss2,loss3);
}
//for highlighting individual solutions
vec2 pHyperbola_sols( vec2 p, in vec2 ab, in float s1, in float s2 )
{
float t = 0.0;
float r2 = ab.x*ab.x+ab.y*ab.y;
float az = ab.x*p.x;
float bw = ab.y*p.y;
float A = 2.0*r2;
float B = -2.0*(az+bw);
float C = 0.0;
float D = -2.0*(az-bw);
float E = -2.0*r2;
float boa = B/A;
float coa = C/A;
float doa = D/A;
float eoa = E/A;
float boa2 = boa*boa;
// α = -3/8 (B/A)^2 + C/A
float alpha = -0.375 * boa2 + coa;
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
float beta = 0.125 * boa2*boa - 0.5*boa*coa + doa;
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
float gamma = -3.0/256.0 * (boa2*boa2) + 0.0625 * coa*boa2 - 0.25 * boa*doa + eoa;
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
//t = -0.25*boa + s1*sqrt(0.5*(-alpha + s2*sqrt(alpha * alpha - 4.0*gamma)));
// else
float alpha2 = alpha * alpha;
// P = - α^2/12 - γ
float P = - alpha2 / 12.0 - gamma;
// Q = - α^3/108 + αγ/3 - β^2/8
float Q = - alpha*(alpha2 / 108.0 - gamma/3.0) - beta*beta / 8.0;
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
float R = - Q * 0.5 + sign(-Q)*sqrt(abs(Q*Q*0.25 + P*P*P/27.0));
// U = ∛R
float U = cbrt(R);
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
float d = -5.0/6.0 * alpha + (U==0.0? -cbrt(Q) : U - P/(3.0*U));
// W = √(α + 2d)
float W = sqrt(alpha + 2.0 * d);
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
t = -boa*0.25 + 0.5 * (s1*W + s2*sqrt(abs(3.0*alpha + 2.0*d + s1*2.0*beta/W)));
float recip_t = 1.0/t;
return vec2(t-recip_t,t+recip_t)*ab;
}
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;
vec2 ra = (vec2(0.25,0.25) + 0.25*cos(iTime*vec2(1.1,1.3)+vec2(1.0,1.0) ))*0.25*(1.0625-sin(iTime));
vec2 pd = pHyperbola( p, ra);
float d = length(pd-p)*inside_Hyperbola(p,ra);
vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7);
col *= 1.0 - exp(-2.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)) );
vec3 ploss = pHyperbola_loss(p,ra);
//uncomment the next line to see a map of floating point precision loss on the solution
col = p.x<m.x?ploss: col;
if( iMouse.z>0.001 )
{
pd = pHyperbola(m, ra);
d = length(pd-m);
col = mix(col, vec3(1.0,1.0,1.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0030));
col = mix(col, vec3(1.0,1.0,1.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.030));
pd = pHyperbola_sols(m, ra,1.0,1.0);
d = length(pd-m);
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-pd)-0.025));
pd = pHyperbola_sols(m, ra,-1.0,1.0);
d = length(pd-m);
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0020));
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.020));
pd = pHyperbola_sols(m, ra,1.0,-1.0);
d = length(pd-m);
col = mix(col, vec3(0.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0015));
col = mix(col, vec3(0.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.015));
pd = pHyperbola_sols(m, ra,-1.0,-1.0);
d = length(pd-m);
col = mix(col, vec3(0.0,0.0,1.0), 1.0-smoothstep(0.0, 0.005, abs(length(p-m)-abs(d))-0.0010));
col = mix(col, vec3(0.0,0.0,1.0), 1.0-smoothstep(0.0, 0.005, length(p-pd)-0.010));
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 | [
12043,
12083,
12153,
12153,
13776
] | [
[
1858,
1858,
1883,
1883,
1910
],
[
2993,
2993,
3017,
3017,
3059
],
[
3060,
3060,
3085,
3085,
3115
],
[
4329,
4467,
4504,
4857,
8182
],
[
8184,
8242,
8286,
8286,
8439
],
[
8442,
8467,
8507,
8507,
8558
],
[
8559,
8559,
8602,
8602,
12040
],
[
12043,
12083,
12153,
12153,
13776
],
[
13784,
13784,
13841,
13841,
15929
]
] | //for highlighting individual solutions
| vec2 pHyperbola_sols( vec2 p, in vec2 ab, in float s1, in float s2 )
{ |
float t = 0.0;
float r2 = ab.x*ab.x+ab.y*ab.y;
float az = ab.x*p.x;
float bw = ab.y*p.y;
float A = 2.0*r2;
float B = -2.0*(az+bw);
float C = 0.0;
float D = -2.0*(az-bw);
float E = -2.0*r2;
float boa = B/A;
float coa = C/A;
float doa = D/A;
float eoa = E/A;
float boa2 = boa*boa;
// α = -3/8 (B/A)^2 + C/A
float alpha = -0.375 * boa2 + coa;
// β = 1/8 (B/A)^3 - 1/2 BC/A^2 + D/A
float beta = 0.125 * boa2*boa - 0.5*boa*coa + doa;
// γ = - 3/256 (B/A)^4 + 1/16 CB^2/A^3 - 1/4 BD/A^2 + E/A
float gamma = -3.0/256.0 * (boa2*boa2) + 0.0625 * coa*boa2 - 0.25 * boa*doa + eoa;
// when β = 0
// t = -B/4A ± √((-α ± √(α^2-4γ))/2)
//t = -0.25*boa + s1*sqrt(0.5*(-alpha + s2*sqrt(alpha * alpha - 4.0*gamma)));
// else
float alpha2 = alpha * alpha;
// P = - α^2/12 - γ
float P = - alpha2 / 12.0 - gamma;
// Q = - α^3/108 + αγ/3 - β^2/8
float Q = - alpha*(alpha2 / 108.0 - gamma/3.0) - beta*beta / 8.0;
// R = - Q/2 ± √((Q/2)^2 + (P/3)^3)
float R = - Q * 0.5 + sign(-Q)*sqrt(abs(Q*Q*0.25 + P*P*P/27.0));
// U = ∛R
float U = cbrt(R);
// d = -5/6 α + (U==0? -∛Q : U - P/(3U))
float d = -5.0/6.0 * alpha + (U==0.0? -cbrt(Q) : U - P/(3.0*U));
// W = √(α + 2d)
float W = sqrt(alpha + 2.0 * d);
// t = -B/4A + 1/2( ±_s W ± √(-(3α + 2d ±_s 2β/W)))
t = -boa*0.25 + 0.5 * (s1*W + s2*sqrt(abs(3.0*alpha + 2.0*d + s1*2.0*beta/W)));
float recip_t = 1.0/t;
return vec2(t-recip_t,t+recip_t)*ab;
} | //for highlighting individual solutions
vec2 pHyperbola_sols( vec2 p, in vec2 ab, in float s1, in float s2 )
{ | 1 | 1 |
ddd3zs | iq | 2023-03-02T04:52:06 | // The MIT License
// Copyright © 2023 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 capped cone. For closest points on other primitives, check
//
// https://www.shadertoy.com/playlist/wXsSzB
// .x distance to the cone
// .yzw closest point
vec4 sdcCappedCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{
//--------------------------------------
// 3D to 2D : p.xyz -> (u,v)
//--------------------------------------
vec3 pa = p-a;
vec3 ba = b-a;
float baba = dot(ba,ba);
float bale = sqrt(baba);
vec3 w = ba/bale;
float v = dot(pa,w);
vec3 q = a + w*v;
vec3 pq = p-q;
float pqpq = dot(pq,pq);
float u = sqrt(pqpq);
//--------------------------------------
// distance and closest in 2D, in (u,v)
// from https://www.shadertoy.com/view/ddt3Rs
//--------------------------------------
float he = 0.5*bale;
v -= he;
float sy = (v<0.0)?-1.0:1.0;
vec4 res;
// top and bottom edges
{
float h = min(u,(v<0.0)?ra:rb);
vec2 c = vec2(h,sy*he);
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = abs(v)-he;
res = vec4(d,c.x,c.y,s);
}
// side edge
{
vec2 k = vec2(rb-ra,2.0*he);
vec2 w = vec2(u,v)-vec2(ra,-he);
float h = clamp(dot(w,k)/dot(k,k),0.0,1.0);
vec2 c = vec2(ra,-he) + h*k;
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = w.x*k.y - w.y*k.x;
res = vec4( (d<res.x) ? vec3(d,c.x,c.y) : res.xyz,
(s>res.w) ? s : res.w );
}
// distance and sign
res.x = sqrt(res.x)*sign(res.w);
// closest is in res.yz
//--------------------------------------
// 2D to 3D : res.yz -> xyz
//--------------------------------------
float d = res.x;
vec2 cl = vec2(res.y,res.z+he);
return vec4( d, a + w*cl.y + (p-q)*cl.x/u );
}
//------------------------------------------------------------
// https://iquilezles.org/articles/distfunctions
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/articles/distfunctions
float sdSphere( vec3 p, vec3 cen, float rad )
{
return length(p-cen)-rad;
}
//------------------------------------------------------------
const vec3 pa = vec3(0.4, 0.6, 0.0);
const vec3 pb = vec3(0.0,-0.4, 0.0);
const float ra = 0.2;
const float rb = 0.8;
vec2 map( in vec3 pos, bool showSurface, vec3 samplePoint )
{
// compute closest point to gPoint on the surace of the capsule
vec3 closestPoint = sdcCappedCone(samplePoint, pa, pb, ra, rb ).yzw;
// point
vec2 res = vec2( sdSphere( pos, samplePoint, 0.06 ), 1.0 );
// closest point
{
float d = sdSphere( pos, closestPoint, 0.06 );
if( d<res.x ) res = vec2( d, 4.0 );
}
// object
if( showSurface )
{
float d = sdcCappedCone( pos, pa, pb, ra, rb ).x;
if( d<res.x ) res = vec2( d, 5.0 );
}
// segment
{
float d = sdCapsule( pos, samplePoint, closestPoint, 0.015 );
if( d<res.x ) res = vec2( d, 4.0 );
}
return res;
}
// https://iquilezles.org/articles/normalsSDF
vec3 calcNormal( in vec3 pos, in bool showSurface, vec3 samplePoint )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.0005;
return normalize( e.xyy*map( pos + e.xyy*eps, showSurface, samplePoint ).x +
e.yyx*map( pos + e.yyx*eps, showSurface, samplePoint ).x +
e.yxy*map( pos + e.yxy*eps, showSurface, samplePoint ).x +
e.xxx*map( pos + e.xxx*eps, showSurface, samplePoint ).x );
}
// https://iquilezles.org/articles/rmshadows
float calcSoftShadow( vec3 ro, vec3 rd, in bool showSurface, vec3 samplePoint )
{
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, samplePoint).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 - 0.0*0.5*(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
// make shape transparent
//bool showSurface = ((samp.x+samp.y)&1)==0; // 50% opaque
bool showSurface = ((samp.x&1)+(samp.y&1))!=0; // 75% opaque
// animate camera
float an = 0.25*time + 6.283185*iMouse.x/iResolution.x;
vec3 ro = vec3( 2.0*cos(an), 0.8, 2.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.2,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// animate point
vec3 samplePoint = sin(time*0.9*vec3(1.0,1.1,1.2)+vec3(0.0,4.0,5.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,samplePoint);
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,samplePoint);
// color
vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) );
// show distance isolines
if( abs(m-5.0)<0.5 )
{
float dref = sdcCappedCone( samplePoint, pa, pb, ra, rb ).x;
float dsam = length(pos-samplePoint);
mate += 0.25*smoothstep(0.8,0.9,sin((dsam-dref)*100.0))*exp2(-12.0*(dsam-dref)*(dsam-dref));
}
// 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,samplePoint);
col += 1.5*mate*vec3(1.0,0.9,0.8)*dif;
}
{
// dome light
float dif = 0.5 + 0.5*nor.y;
col += 0.5*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 | [
1213,
1264,
1328,
1451,
2914
] | [
[
1213,
1264,
1328,
1451,
2914
],
[
2980,
3029,
3081,
3081,
3195
],
[
3197,
3246,
3293,
3293,
3325
],
[
3513,
3513,
3574,
3644,
4234
],
[
4236,
4282,
4353,
4353,
4704
],
[
4706,
4751,
4832,
4832,
5218
]
] | // .x distance to the cone
// .yzw closest point
| vec4 sdcCappedCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{ |
vec3 pa = p-a;
vec3 ba = b-a;
float baba = dot(ba,ba);
float bale = sqrt(baba);
vec3 w = ba/bale;
float v = dot(pa,w);
vec3 q = a + w*v;
vec3 pq = p-q;
float pqpq = dot(pq,pq);
float u = sqrt(pqpq);
//--------------------------------------
// distance and closest in 2D, in (u,v)
// from https://www.shadertoy.com/view/ddt3Rs
//--------------------------------------
float he = 0.5*bale;
v -= he;
float sy = (v<0.0)?-1.0:1.0;
vec4 res;
// top and bottom edges
{
float h = min(u,(v<0.0)?ra:rb);
vec2 c = vec2(h,sy*he);
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = abs(v)-he;
res = vec4(d,c.x,c.y,s);
}
// side edge
{
vec2 k = vec2(rb-ra,2.0*he);
vec2 w = vec2(u,v)-vec2(ra,-he);
float h = clamp(dot(w,k)/dot(k,k),0.0,1.0);
vec2 c = vec2(ra,-he) + h*k;
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = w.x*k.y - w.y*k.x;
res = vec4( (d<res.x) ? vec3(d,c.x,c.y) : res.xyz,
(s>res.w) ? s : res.w );
}
// distance and sign
res.x = sqrt(res.x)*sign(res.w);
// closest is in res.yz
//--------------------------------------
// 2D to 3D : res.yz -> xyz
//--------------------------------------
float d = res.x;
vec2 cl = vec2(res.y,res.z+he);
return vec4( d, a + w*cl.y + (p-q)*cl.x/u );
} | // .x distance to the cone
// .yzw closest point
vec4 sdcCappedCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{ | 1 | 1 |
ddd3zs | iq | 2023-03-02T04:52:06 | // The MIT License
// Copyright © 2023 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 capped cone. For closest points on other primitives, check
//
// https://www.shadertoy.com/playlist/wXsSzB
// .x distance to the cone
// .yzw closest point
vec4 sdcCappedCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{
//--------------------------------------
// 3D to 2D : p.xyz -> (u,v)
//--------------------------------------
vec3 pa = p-a;
vec3 ba = b-a;
float baba = dot(ba,ba);
float bale = sqrt(baba);
vec3 w = ba/bale;
float v = dot(pa,w);
vec3 q = a + w*v;
vec3 pq = p-q;
float pqpq = dot(pq,pq);
float u = sqrt(pqpq);
//--------------------------------------
// distance and closest in 2D, in (u,v)
// from https://www.shadertoy.com/view/ddt3Rs
//--------------------------------------
float he = 0.5*bale;
v -= he;
float sy = (v<0.0)?-1.0:1.0;
vec4 res;
// top and bottom edges
{
float h = min(u,(v<0.0)?ra:rb);
vec2 c = vec2(h,sy*he);
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = abs(v)-he;
res = vec4(d,c.x,c.y,s);
}
// side edge
{
vec2 k = vec2(rb-ra,2.0*he);
vec2 w = vec2(u,v)-vec2(ra,-he);
float h = clamp(dot(w,k)/dot(k,k),0.0,1.0);
vec2 c = vec2(ra,-he) + h*k;
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = w.x*k.y - w.y*k.x;
res = vec4( (d<res.x) ? vec3(d,c.x,c.y) : res.xyz,
(s>res.w) ? s : res.w );
}
// distance and sign
res.x = sqrt(res.x)*sign(res.w);
// closest is in res.yz
//--------------------------------------
// 2D to 3D : res.yz -> xyz
//--------------------------------------
float d = res.x;
vec2 cl = vec2(res.y,res.z+he);
return vec4( d, a + w*cl.y + (p-q)*cl.x/u );
}
//------------------------------------------------------------
// https://iquilezles.org/articles/distfunctions
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/articles/distfunctions
float sdSphere( vec3 p, vec3 cen, float rad )
{
return length(p-cen)-rad;
}
//------------------------------------------------------------
const vec3 pa = vec3(0.4, 0.6, 0.0);
const vec3 pb = vec3(0.0,-0.4, 0.0);
const float ra = 0.2;
const float rb = 0.8;
vec2 map( in vec3 pos, bool showSurface, vec3 samplePoint )
{
// compute closest point to gPoint on the surace of the capsule
vec3 closestPoint = sdcCappedCone(samplePoint, pa, pb, ra, rb ).yzw;
// point
vec2 res = vec2( sdSphere( pos, samplePoint, 0.06 ), 1.0 );
// closest point
{
float d = sdSphere( pos, closestPoint, 0.06 );
if( d<res.x ) res = vec2( d, 4.0 );
}
// object
if( showSurface )
{
float d = sdcCappedCone( pos, pa, pb, ra, rb ).x;
if( d<res.x ) res = vec2( d, 5.0 );
}
// segment
{
float d = sdCapsule( pos, samplePoint, closestPoint, 0.015 );
if( d<res.x ) res = vec2( d, 4.0 );
}
return res;
}
// https://iquilezles.org/articles/normalsSDF
vec3 calcNormal( in vec3 pos, in bool showSurface, vec3 samplePoint )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.0005;
return normalize( e.xyy*map( pos + e.xyy*eps, showSurface, samplePoint ).x +
e.yyx*map( pos + e.yyx*eps, showSurface, samplePoint ).x +
e.yxy*map( pos + e.yxy*eps, showSurface, samplePoint ).x +
e.xxx*map( pos + e.xxx*eps, showSurface, samplePoint ).x );
}
// https://iquilezles.org/articles/rmshadows
float calcSoftShadow( vec3 ro, vec3 rd, in bool showSurface, vec3 samplePoint )
{
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, samplePoint).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 - 0.0*0.5*(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
// make shape transparent
//bool showSurface = ((samp.x+samp.y)&1)==0; // 50% opaque
bool showSurface = ((samp.x&1)+(samp.y&1))!=0; // 75% opaque
// animate camera
float an = 0.25*time + 6.283185*iMouse.x/iResolution.x;
vec3 ro = vec3( 2.0*cos(an), 0.8, 2.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.2,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// animate point
vec3 samplePoint = sin(time*0.9*vec3(1.0,1.1,1.2)+vec3(0.0,4.0,5.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,samplePoint);
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,samplePoint);
// color
vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) );
// show distance isolines
if( abs(m-5.0)<0.5 )
{
float dref = sdcCappedCone( samplePoint, pa, pb, ra, rb ).x;
float dsam = length(pos-samplePoint);
mate += 0.25*smoothstep(0.8,0.9,sin((dsam-dref)*100.0))*exp2(-12.0*(dsam-dref)*(dsam-dref));
}
// 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,samplePoint);
col += 1.5*mate*vec3(1.0,0.9,0.8)*dif;
}
{
// dome light
float dif = 0.5 + 0.5*nor.y;
col += 0.5*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 | [
2980,
3029,
3081,
3081,
3195
] | [
[
1213,
1264,
1328,
1451,
2914
],
[
2980,
3029,
3081,
3081,
3195
],
[
3197,
3246,
3293,
3293,
3325
],
[
3513,
3513,
3574,
3644,
4234
],
[
4236,
4282,
4353,
4353,
4704
],
[
4706,
4751,
4832,
4832,
5218
]
] | // https://iquilezles.org/articles/distfunctions
| 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/articles/distfunctions
float sdCapsule( vec3 p, vec3 a, vec3 b, float r )
{ | 56 | 160 |
ddd3zs | iq | 2023-03-02T04:52:06 | // The MIT License
// Copyright © 2023 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 capped cone. For closest points on other primitives, check
//
// https://www.shadertoy.com/playlist/wXsSzB
// .x distance to the cone
// .yzw closest point
vec4 sdcCappedCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{
//--------------------------------------
// 3D to 2D : p.xyz -> (u,v)
//--------------------------------------
vec3 pa = p-a;
vec3 ba = b-a;
float baba = dot(ba,ba);
float bale = sqrt(baba);
vec3 w = ba/bale;
float v = dot(pa,w);
vec3 q = a + w*v;
vec3 pq = p-q;
float pqpq = dot(pq,pq);
float u = sqrt(pqpq);
//--------------------------------------
// distance and closest in 2D, in (u,v)
// from https://www.shadertoy.com/view/ddt3Rs
//--------------------------------------
float he = 0.5*bale;
v -= he;
float sy = (v<0.0)?-1.0:1.0;
vec4 res;
// top and bottom edges
{
float h = min(u,(v<0.0)?ra:rb);
vec2 c = vec2(h,sy*he);
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = abs(v)-he;
res = vec4(d,c.x,c.y,s);
}
// side edge
{
vec2 k = vec2(rb-ra,2.0*he);
vec2 w = vec2(u,v)-vec2(ra,-he);
float h = clamp(dot(w,k)/dot(k,k),0.0,1.0);
vec2 c = vec2(ra,-he) + h*k;
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = w.x*k.y - w.y*k.x;
res = vec4( (d<res.x) ? vec3(d,c.x,c.y) : res.xyz,
(s>res.w) ? s : res.w );
}
// distance and sign
res.x = sqrt(res.x)*sign(res.w);
// closest is in res.yz
//--------------------------------------
// 2D to 3D : res.yz -> xyz
//--------------------------------------
float d = res.x;
vec2 cl = vec2(res.y,res.z+he);
return vec4( d, a + w*cl.y + (p-q)*cl.x/u );
}
//------------------------------------------------------------
// https://iquilezles.org/articles/distfunctions
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/articles/distfunctions
float sdSphere( vec3 p, vec3 cen, float rad )
{
return length(p-cen)-rad;
}
//------------------------------------------------------------
const vec3 pa = vec3(0.4, 0.6, 0.0);
const vec3 pb = vec3(0.0,-0.4, 0.0);
const float ra = 0.2;
const float rb = 0.8;
vec2 map( in vec3 pos, bool showSurface, vec3 samplePoint )
{
// compute closest point to gPoint on the surace of the capsule
vec3 closestPoint = sdcCappedCone(samplePoint, pa, pb, ra, rb ).yzw;
// point
vec2 res = vec2( sdSphere( pos, samplePoint, 0.06 ), 1.0 );
// closest point
{
float d = sdSphere( pos, closestPoint, 0.06 );
if( d<res.x ) res = vec2( d, 4.0 );
}
// object
if( showSurface )
{
float d = sdcCappedCone( pos, pa, pb, ra, rb ).x;
if( d<res.x ) res = vec2( d, 5.0 );
}
// segment
{
float d = sdCapsule( pos, samplePoint, closestPoint, 0.015 );
if( d<res.x ) res = vec2( d, 4.0 );
}
return res;
}
// https://iquilezles.org/articles/normalsSDF
vec3 calcNormal( in vec3 pos, in bool showSurface, vec3 samplePoint )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.0005;
return normalize( e.xyy*map( pos + e.xyy*eps, showSurface, samplePoint ).x +
e.yyx*map( pos + e.yyx*eps, showSurface, samplePoint ).x +
e.yxy*map( pos + e.yxy*eps, showSurface, samplePoint ).x +
e.xxx*map( pos + e.xxx*eps, showSurface, samplePoint ).x );
}
// https://iquilezles.org/articles/rmshadows
float calcSoftShadow( vec3 ro, vec3 rd, in bool showSurface, vec3 samplePoint )
{
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, samplePoint).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 - 0.0*0.5*(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
// make shape transparent
//bool showSurface = ((samp.x+samp.y)&1)==0; // 50% opaque
bool showSurface = ((samp.x&1)+(samp.y&1))!=0; // 75% opaque
// animate camera
float an = 0.25*time + 6.283185*iMouse.x/iResolution.x;
vec3 ro = vec3( 2.0*cos(an), 0.8, 2.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.2,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// animate point
vec3 samplePoint = sin(time*0.9*vec3(1.0,1.1,1.2)+vec3(0.0,4.0,5.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,samplePoint);
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,samplePoint);
// color
vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) );
// show distance isolines
if( abs(m-5.0)<0.5 )
{
float dref = sdcCappedCone( samplePoint, pa, pb, ra, rb ).x;
float dsam = length(pos-samplePoint);
mate += 0.25*smoothstep(0.8,0.9,sin((dsam-dref)*100.0))*exp2(-12.0*(dsam-dref)*(dsam-dref));
}
// 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,samplePoint);
col += 1.5*mate*vec3(1.0,0.9,0.8)*dif;
}
{
// dome light
float dif = 0.5 + 0.5*nor.y;
col += 0.5*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 | [
3197,
3246,
3293,
3293,
3325
] | [
[
1213,
1264,
1328,
1451,
2914
],
[
2980,
3029,
3081,
3081,
3195
],
[
3197,
3246,
3293,
3293,
3325
],
[
3513,
3513,
3574,
3644,
4234
],
[
4236,
4282,
4353,
4353,
4704
],
[
4706,
4751,
4832,
4832,
5218
]
] | // https://iquilezles.org/articles/distfunctions
| float sdSphere( vec3 p, vec3 cen, float rad )
{ |
return length(p-cen)-rad;
} | // https://iquilezles.org/articles/distfunctions
float sdSphere( vec3 p, vec3 cen, float rad )
{ | 2 | 2 |
ddd3zs | iq | 2023-03-02T04:52:06 | // The MIT License
// Copyright © 2023 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 capped cone. For closest points on other primitives, check
//
// https://www.shadertoy.com/playlist/wXsSzB
// .x distance to the cone
// .yzw closest point
vec4 sdcCappedCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{
//--------------------------------------
// 3D to 2D : p.xyz -> (u,v)
//--------------------------------------
vec3 pa = p-a;
vec3 ba = b-a;
float baba = dot(ba,ba);
float bale = sqrt(baba);
vec3 w = ba/bale;
float v = dot(pa,w);
vec3 q = a + w*v;
vec3 pq = p-q;
float pqpq = dot(pq,pq);
float u = sqrt(pqpq);
//--------------------------------------
// distance and closest in 2D, in (u,v)
// from https://www.shadertoy.com/view/ddt3Rs
//--------------------------------------
float he = 0.5*bale;
v -= he;
float sy = (v<0.0)?-1.0:1.0;
vec4 res;
// top and bottom edges
{
float h = min(u,(v<0.0)?ra:rb);
vec2 c = vec2(h,sy*he);
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = abs(v)-he;
res = vec4(d,c.x,c.y,s);
}
// side edge
{
vec2 k = vec2(rb-ra,2.0*he);
vec2 w = vec2(u,v)-vec2(ra,-he);
float h = clamp(dot(w,k)/dot(k,k),0.0,1.0);
vec2 c = vec2(ra,-he) + h*k;
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = w.x*k.y - w.y*k.x;
res = vec4( (d<res.x) ? vec3(d,c.x,c.y) : res.xyz,
(s>res.w) ? s : res.w );
}
// distance and sign
res.x = sqrt(res.x)*sign(res.w);
// closest is in res.yz
//--------------------------------------
// 2D to 3D : res.yz -> xyz
//--------------------------------------
float d = res.x;
vec2 cl = vec2(res.y,res.z+he);
return vec4( d, a + w*cl.y + (p-q)*cl.x/u );
}
//------------------------------------------------------------
// https://iquilezles.org/articles/distfunctions
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/articles/distfunctions
float sdSphere( vec3 p, vec3 cen, float rad )
{
return length(p-cen)-rad;
}
//------------------------------------------------------------
const vec3 pa = vec3(0.4, 0.6, 0.0);
const vec3 pb = vec3(0.0,-0.4, 0.0);
const float ra = 0.2;
const float rb = 0.8;
vec2 map( in vec3 pos, bool showSurface, vec3 samplePoint )
{
// compute closest point to gPoint on the surace of the capsule
vec3 closestPoint = sdcCappedCone(samplePoint, pa, pb, ra, rb ).yzw;
// point
vec2 res = vec2( sdSphere( pos, samplePoint, 0.06 ), 1.0 );
// closest point
{
float d = sdSphere( pos, closestPoint, 0.06 );
if( d<res.x ) res = vec2( d, 4.0 );
}
// object
if( showSurface )
{
float d = sdcCappedCone( pos, pa, pb, ra, rb ).x;
if( d<res.x ) res = vec2( d, 5.0 );
}
// segment
{
float d = sdCapsule( pos, samplePoint, closestPoint, 0.015 );
if( d<res.x ) res = vec2( d, 4.0 );
}
return res;
}
// https://iquilezles.org/articles/normalsSDF
vec3 calcNormal( in vec3 pos, in bool showSurface, vec3 samplePoint )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.0005;
return normalize( e.xyy*map( pos + e.xyy*eps, showSurface, samplePoint ).x +
e.yyx*map( pos + e.yyx*eps, showSurface, samplePoint ).x +
e.yxy*map( pos + e.yxy*eps, showSurface, samplePoint ).x +
e.xxx*map( pos + e.xxx*eps, showSurface, samplePoint ).x );
}
// https://iquilezles.org/articles/rmshadows
float calcSoftShadow( vec3 ro, vec3 rd, in bool showSurface, vec3 samplePoint )
{
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, samplePoint).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 - 0.0*0.5*(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
// make shape transparent
//bool showSurface = ((samp.x+samp.y)&1)==0; // 50% opaque
bool showSurface = ((samp.x&1)+(samp.y&1))!=0; // 75% opaque
// animate camera
float an = 0.25*time + 6.283185*iMouse.x/iResolution.x;
vec3 ro = vec3( 2.0*cos(an), 0.8, 2.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.2,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// animate point
vec3 samplePoint = sin(time*0.9*vec3(1.0,1.1,1.2)+vec3(0.0,4.0,5.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,samplePoint);
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,samplePoint);
// color
vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) );
// show distance isolines
if( abs(m-5.0)<0.5 )
{
float dref = sdcCappedCone( samplePoint, pa, pb, ra, rb ).x;
float dsam = length(pos-samplePoint);
mate += 0.25*smoothstep(0.8,0.9,sin((dsam-dref)*100.0))*exp2(-12.0*(dsam-dref)*(dsam-dref));
}
// 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,samplePoint);
col += 1.5*mate*vec3(1.0,0.9,0.8)*dif;
}
{
// dome light
float dif = 0.5 + 0.5*nor.y;
col += 0.5*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 | [
4236,
4282,
4353,
4353,
4704
] | [
[
1213,
1264,
1328,
1451,
2914
],
[
2980,
3029,
3081,
3081,
3195
],
[
3197,
3246,
3293,
3293,
3325
],
[
3513,
3513,
3574,
3644,
4234
],
[
4236,
4282,
4353,
4353,
4704
],
[
4706,
4751,
4832,
4832,
5218
]
] | // https://iquilezles.org/articles/normalsSDF
| vec3 calcNormal( in vec3 pos, in bool showSurface, vec3 samplePoint )
{ |
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.0005;
return normalize( e.xyy*map( pos + e.xyy*eps, showSurface, samplePoint ).x +
e.yyx*map( pos + e.yyx*eps, showSurface, samplePoint ).x +
e.yxy*map( pos + e.yxy*eps, showSurface, samplePoint ).x +
e.xxx*map( pos + e.xxx*eps, showSurface, samplePoint ).x );
} | // https://iquilezles.org/articles/normalsSDF
vec3 calcNormal( in vec3 pos, in bool showSurface, vec3 samplePoint )
{ | 1 | 1 |
ddd3zs | iq | 2023-03-02T04:52:06 | // The MIT License
// Copyright © 2023 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 capped cone. For closest points on other primitives, check
//
// https://www.shadertoy.com/playlist/wXsSzB
// .x distance to the cone
// .yzw closest point
vec4 sdcCappedCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{
//--------------------------------------
// 3D to 2D : p.xyz -> (u,v)
//--------------------------------------
vec3 pa = p-a;
vec3 ba = b-a;
float baba = dot(ba,ba);
float bale = sqrt(baba);
vec3 w = ba/bale;
float v = dot(pa,w);
vec3 q = a + w*v;
vec3 pq = p-q;
float pqpq = dot(pq,pq);
float u = sqrt(pqpq);
//--------------------------------------
// distance and closest in 2D, in (u,v)
// from https://www.shadertoy.com/view/ddt3Rs
//--------------------------------------
float he = 0.5*bale;
v -= he;
float sy = (v<0.0)?-1.0:1.0;
vec4 res;
// top and bottom edges
{
float h = min(u,(v<0.0)?ra:rb);
vec2 c = vec2(h,sy*he);
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = abs(v)-he;
res = vec4(d,c.x,c.y,s);
}
// side edge
{
vec2 k = vec2(rb-ra,2.0*he);
vec2 w = vec2(u,v)-vec2(ra,-he);
float h = clamp(dot(w,k)/dot(k,k),0.0,1.0);
vec2 c = vec2(ra,-he) + h*k;
vec2 q = vec2(u,v) - c;
float d = dot(q,q);
float s = w.x*k.y - w.y*k.x;
res = vec4( (d<res.x) ? vec3(d,c.x,c.y) : res.xyz,
(s>res.w) ? s : res.w );
}
// distance and sign
res.x = sqrt(res.x)*sign(res.w);
// closest is in res.yz
//--------------------------------------
// 2D to 3D : res.yz -> xyz
//--------------------------------------
float d = res.x;
vec2 cl = vec2(res.y,res.z+he);
return vec4( d, a + w*cl.y + (p-q)*cl.x/u );
}
//------------------------------------------------------------
// https://iquilezles.org/articles/distfunctions
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/articles/distfunctions
float sdSphere( vec3 p, vec3 cen, float rad )
{
return length(p-cen)-rad;
}
//------------------------------------------------------------
const vec3 pa = vec3(0.4, 0.6, 0.0);
const vec3 pb = vec3(0.0,-0.4, 0.0);
const float ra = 0.2;
const float rb = 0.8;
vec2 map( in vec3 pos, bool showSurface, vec3 samplePoint )
{
// compute closest point to gPoint on the surace of the capsule
vec3 closestPoint = sdcCappedCone(samplePoint, pa, pb, ra, rb ).yzw;
// point
vec2 res = vec2( sdSphere( pos, samplePoint, 0.06 ), 1.0 );
// closest point
{
float d = sdSphere( pos, closestPoint, 0.06 );
if( d<res.x ) res = vec2( d, 4.0 );
}
// object
if( showSurface )
{
float d = sdcCappedCone( pos, pa, pb, ra, rb ).x;
if( d<res.x ) res = vec2( d, 5.0 );
}
// segment
{
float d = sdCapsule( pos, samplePoint, closestPoint, 0.015 );
if( d<res.x ) res = vec2( d, 4.0 );
}
return res;
}
// https://iquilezles.org/articles/normalsSDF
vec3 calcNormal( in vec3 pos, in bool showSurface, vec3 samplePoint )
{
vec2 e = vec2(1.0,-1.0)*0.5773;
const float eps = 0.0005;
return normalize( e.xyy*map( pos + e.xyy*eps, showSurface, samplePoint ).x +
e.yyx*map( pos + e.yyx*eps, showSurface, samplePoint ).x +
e.yxy*map( pos + e.yxy*eps, showSurface, samplePoint ).x +
e.xxx*map( pos + e.xxx*eps, showSurface, samplePoint ).x );
}
// https://iquilezles.org/articles/rmshadows
float calcSoftShadow( vec3 ro, vec3 rd, in bool showSurface, vec3 samplePoint )
{
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, samplePoint).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 - 0.0*0.5*(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
// make shape transparent
//bool showSurface = ((samp.x+samp.y)&1)==0; // 50% opaque
bool showSurface = ((samp.x&1)+(samp.y&1))!=0; // 75% opaque
// animate camera
float an = 0.25*time + 6.283185*iMouse.x/iResolution.x;
vec3 ro = vec3( 2.0*cos(an), 0.8, 2.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.2,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
// animate point
vec3 samplePoint = sin(time*0.9*vec3(1.0,1.1,1.2)+vec3(0.0,4.0,5.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,samplePoint);
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,samplePoint);
// color
vec3 mate = 0.55 + 0.45*cos( m + vec3(0.0,1.0,1.5) );
// show distance isolines
if( abs(m-5.0)<0.5 )
{
float dref = sdcCappedCone( samplePoint, pa, pb, ra, rb ).x;
float dsam = length(pos-samplePoint);
mate += 0.25*smoothstep(0.8,0.9,sin((dsam-dref)*100.0))*exp2(-12.0*(dsam-dref)*(dsam-dref));
}
// 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,samplePoint);
col += 1.5*mate*vec3(1.0,0.9,0.8)*dif;
}
{
// dome light
float dif = 0.5 + 0.5*nor.y;
col += 0.5*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 | [
4706,
4751,
4832,
4832,
5218
] | [
[
1213,
1264,
1328,
1451,
2914
],
[
2980,
3029,
3081,
3081,
3195
],
[
3197,
3246,
3293,
3293,
3325
],
[
3513,
3513,
3574,
3644,
4234
],
[
4236,
4282,
4353,
4353,
4704
],
[
4706,
4751,
4832,
4832,
5218
]
] | // https://iquilezles.org/articles/rmshadows
| float calcSoftShadow( vec3 ro, vec3 rd, in bool showSurface, vec3 samplePoint )
{ |
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, samplePoint).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]
} | // https://iquilezles.org/articles/rmshadows
float calcSoftShadow( vec3 ro, vec3 rd, in bool showSurface, vec3 samplePoint )
{ | 1 | 1 |
ddt3Rs | iq | 2023-03-02T04:51:38 | // The MIT License
// Copyright © 2023 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, closest point and gradient to a trapezoid.
// List of other 2D distances+gradients:
//
// https://iquilezles.org/articles/distgradfunctions2d
//
// 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 sdgTrapezoid( in vec2 p, in float ra, float rb, float he, out vec2 ocl )
{
float sx = (p.x<0.0)?-1.0:1.0;
float sy = (p.y<0.0)?-1.0:1.0;
p.x = abs(p.x);
vec4 res;
// bottom and top edges
{
float h = min(p.x,(p.y<0.0)?ra:rb);
vec2 c = vec2(h,sy*he);
vec2 q = p - c;
float d = dot(q,q);
float s = abs(p.y) - he;
res = vec4(d,q,s);
ocl = c;
}
// side edge
{
vec2 k = vec2(rb-ra,2.0*he);
vec2 w = p - vec2(ra, -he);
float h = clamp(dot(w,k)/dot(k,k),0.0,1.0);
vec2 c = vec2(ra,-he) + h*k;
vec2 q = p - c;
float d = dot(q,q);
float s = w.x*k.y - w.y*k.x;
if( d<res.x ) { ocl = c; res.xyz = vec3(d,q); }
if( s>res.w ) { res.w = s; }
}
// distance and sign
float d = sqrt(res.x)*sign(res.w);
res.y *= sx;
ocl.x *= sx;
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
vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y;
// animation
float ra = 0.2+0.15*sin(iTime*1.3+0.0);
float rb = 0.2+0.15*sin(iTime*1.4+1.1);
float he = 0.5+0.2*sin(1.3*iTime);
// sdf(p) and gradient(sdf(p))
vec2 kk;
vec3 dg = sdgTrapezoid( p, ra, rb, he, kk );
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)) );
// interaction
if( iMouse.z>0.001 )
{
vec2 cl;
d = sdgTrapezoid(m, ra, rb, he, cl).x;
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));
col = mix(col, vec3(1.0,1.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-cl)-0.015));
}
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
fragColor = vec4( tot, 1.0 );
} | mit | [
1301,
1396,
1475,
1475,
2350
] | [
[
1301,
1396,
1475,
1475,
2350
]
] | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
| vec3 sdgTrapezoid( in vec2 p, in float ra, float rb, float he, out vec2 ocl )
{ |
float sx = (p.x<0.0)?-1.0:1.0;
float sy = (p.y<0.0)?-1.0:1.0;
p.x = abs(p.x);
vec4 res;
// bottom and top edges
{
float h = min(p.x,(p.y<0.0)?ra:rb);
vec2 c = vec2(h,sy*he);
vec2 q = p - c;
float d = dot(q,q);
float s = abs(p.y) - he;
res = vec4(d,q,s);
ocl = c;
}
// side edge
{
vec2 k = vec2(rb-ra,2.0*he);
vec2 w = p - vec2(ra, -he);
float h = clamp(dot(w,k)/dot(k,k),0.0,1.0);
vec2 c = vec2(ra,-he) + h*k;
vec2 q = p - c;
float d = dot(q,q);
float s = w.x*k.y - w.y*k.x;
if( d<res.x ) { ocl = c; res.xyz = vec3(d,q); }
if( s>res.w ) { res.w = s; }
}
// distance and sign
float d = sqrt(res.x)*sign(res.w);
res.y *= sx;
ocl.x *= sx;
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 sdgTrapezoid( in vec2 p, in float ra, float rb, float he, out vec2 ocl )
{ | 1 | 1 |
mt3Gzl | bub | 2023-05-01T05:14:30 | // 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
// Ellipsoid: https://www.shadertoy.com/view/flXyRS
// .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .w = ∂f(p)/∂z
// .yzw = ∇f(p) with ‖∇f(p)‖ = 1
//where is major radius and minor radius?
//ra is major and rb is minor?
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));
float size = 0.2;
float idk = 1.0;
// animate torus
float majorRadius = idk*size + idk*size*sin(iTime); //major radius
float minorRadius = size; //minor radius
// 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,majorRadius,minorRadius).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,majorRadius,minorRadius).yzw;
// compute normal numerically, for comparison
// https://iquilezles.org/articles/normalsSDF
#if 0
const vec2 e = vec2(1,-1);
const float eps = 0.0002;
nor = normalize( e.xyy*sdgTorus( pos + e.xyy*eps, majorRadius, minorRadius ).x +
e.yyx*sdgTorus( pos + e.yyx*eps, majorRadius, minorRadius ).x +
e.yxy*sdgTorus( pos + e.yxy*eps, majorRadius, minorRadius ).x +
e.xxx*sdgTorus( pos + e.xxx*eps, majorRadius, minorRadius ).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 | [
1660,
1733,
1778,
1778,
1903
] | [
[
1660,
1733,
1778,
1778,
1903
]
] | //where is major radius and minor radius?
//ra is major and rb is minor?
| 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)) );
} | //where is major radius and minor radius?
//ra is major and rb is minor?
vec4 sdgTorus( vec3 p, float ra, float rb )
{ | 2 | 2 |
cs2yzG | iq | 2023-06-27T05:15:20 | // The MIT License
// Copyright © 2023 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 to a vesica given by two points and a (semi)width, using
// two square roots per evaluation.
// See also https://www.shadertoy.com/view/XtVfRW for an axis aligned vesica
// List of some other 2D distances:
// https://www.shadertoy.com/playlist/MXdSRf
// and
// iquilezles.org/articles/distfunctions2d
float sdVesicaSegment( in vec2 p, in vec2 a, in vec2 b, float w )
{
// shape constants
float r = 0.5*length(b-a);
float d = 0.5*(r*r-w*w)/w;
// center, orient and mirror
vec2 v = (b-a)/r;
vec2 c = (b+a)*0.5;
vec2 q = 0.5*abs(mat2(v.y,v.x,-v.x,v.y)*(p-c));
// feature selection (vertex or body)
vec3 h = (r*q.x < d*(q.y-r)) ? vec3(0.0,r,0.0) : vec3(-d,0.0,d+w);
// distance
return length(q-h.xy) - h.z;
}
// iquilezles.org/articles/distfunctions2d
float udSegment( in vec2 p, in vec2 a, in vec2 b )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length(pa-h*ba);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// coordinates
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y;
p *= 1.4;
m *= 1.4;
// animation
vec2 v1 = cos( iTime*0.5 + vec2(0.0,1.00) + 0.0 );
vec2 v2 = cos( iTime*0.5 + vec2(0.0,3.00) + 1.5 );
float th = 0.40*(0.5+0.495*cos(iTime*1.1+2.0));
float ra = 0.15*(0.5+0.495*cos(iTime*1.3+1.0));
float al = smoothstep( -0.5, 0.5,sin(iTime+0.1) );
ra *= 1.0-al;
// distance
float d = sdVesicaSegment( p, v1, v2, th ) - ra;
// color
vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.65,0.85,1.0);
col *= 1.0 - exp2(-10.0*abs(d));
col *= 0.8 + 0.2*cos(100.0*d);
col = mix( col, vec3(1.0), 1.0-smoothstep(0.0,0.015,abs(d)) );
// mouse
if( iMouse.z>0.001 )
{
d = sdVesicaSegment( m, v1, v2, th ) - 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));
}
// geometry
{
vec2 c = (v1+v2)/2.0;
vec2 u = normalize( vec2(v2.y-v1.y,v1.x-v2.x) );
vec2 v3 = c + u*(th+ra);
vec2 v4 = c - u*(th+ra);
d = min(min(min(length(p-v1),
length(p-v2)),
min(length(p-v3),
length(p-v4))) - 0.015,
min(udSegment(p,v1,v2),
udSegment(p,v3,v4)) - 0.004 );
col = mix(col, vec3(1.0,1.0,0.0), al*(1.0-smoothstep(0.0, 0.005, d)));
}
fragColor = vec4(col,1.0);
} | mit | [
1873,
1916,
1968,
1968,
2092
] | [
[
1411,
1411,
1478,
1501,
1870
],
[
1873,
1916,
1968,
1968,
2092
],
[
2094,
2094,
2151,
2170,
3725
]
] | // iquilezles.org/articles/distfunctions2d
| float udSegment( in vec2 p, in vec2 a, in vec2 b )
{ |
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length(pa-h*ba);
} | // iquilezles.org/articles/distfunctions2d
float udSegment( in vec2 p, in vec2 a, in vec2 b )
{ | 1 | 11 |
DslcWB | pizzahollandaise | 2023-06-22T07:13:11 | // 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.
// Based on:
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
// and iquilezles.org/articles/distfunctions2d
#define INF 1.0 / 0.0
float distLine(vec3 p, vec3 dir) {
return length(cross(p, dir))/length(dir)-0.1;
}
float sdSphere(vec3 p, vec4 s) {
return length(p-s.xyz)-s.w;
}
bool bounded = false;
// Rounded capsule https://iquilezles.org/articles/distfunctions/
float dot2(in vec3 v) { return dot(v,v); }
float sdBranchFast(vec3 p, vec4 a, vec4 b, float minDist) {
// sampling independent computations (only depend on shape)
vec3 ba = b.xyz - a.xyz;
float l2 = dot(ba, ba);
float bound = length(cross(p-a.xyz, ba))/sqrt(l2) -a.w;
if (bound > minDist) {
bounded = true;
return minDist; // Early skip
} // Bounding cylinder
float rr = a.w - b.w;
float a2 = l2 - rr*rr;
float il2 = 1.0/l2;
// sampling dependant computations
vec3 pa = p - a.xyz;
float y = dot(pa, ba);
float z = y - l2;
float x2 = dot2( pa*l2 - ba*y );
float y2 = y*y*l2;
float z2 = z*z*l2;
// single square root!
float k = sign(rr)*rr*rr*x2;
if( sign(z)*a2*z2>k ) return min(sqrt(x2 + z2) *il2 - b.w, minDist);
if( sign(y)*a2*y2<k ) return min(sqrt(x2 + y2) *il2 - a.w, minDist);
return min((sqrt(x2*a2*il2)+y*rr)*il2 - a.w, minDist);
}
float map(vec2 p2) {
vec3 p = vec3(p2.x, p2.y, 0.0);
float other = sdSphere(p, vec4(0.5, 0.0, 0.0, 0.3));
//return distLine(p, vec3(1.0, 1.0, 0.0));
return sdBranchFast(p, vec4(-0.3, -0.3, 0.0, 0.4), vec4(0.5, 0.5, 0.0, 0.1), other);
}
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;
float d = map(p);
// coloring
vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.65,0.85,1.0);
col *= 1.0 - exp(-6.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)) );
if( iMouse.z>0.001 ) {
d = map(m);
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));
}
if (bounded) col *= 0.5;
fragColor = vec4(col,1.0);
} | mit | [
1419,
1485,
1508,
1508,
1527
] | [
[
1241,
1241,
1275,
1275,
1327
],
[
1329,
1329,
1361,
1361,
1395
],
[
1419,
1485,
1508,
1508,
1527
],
[
1528,
1528,
1587,
1651,
2473
],
[
2475,
2475,
2495,
2495,
2726
],
[
2729,
2729,
2786,
2786,
3441
]
] | // Rounded capsule https://iquilezles.org/articles/distfunctions/
| float dot2(in vec3 v) { | return dot(v,v); } | // Rounded capsule https://iquilezles.org/articles/distfunctions/
float dot2(in vec3 v) { | 2 | 7 |
csscRl | pizzahollandaise | 2023-06-18T16:31:14 | // 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.
// Based on:
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
// and iquilezles.org/articles/distfunctions2d
// Essentially the box from iq
// Squared distance so you only have to take one sqrt for multiple aabbs
float sqAABB( in vec2 p, in vec2 minCorner, in vec2 maxCorner )
{
vec2 size = (maxCorner - minCorner) * 0.5;
vec2 center = minCorner + size;
vec2 d = max(abs(p - center) - size, 0.0);
return dot(d, d);
}
float map(vec2 p) {
float bb1 = sqAABB(p,vec2(-abs(sin(iTime)) * 0.5),vec2(abs(cos(iTime))) * 0.5);
float bb2 = sqAABB(p,vec2(cos(iTime), 0.0),vec2(cos(iTime)+0.3, 0.3));
return sqrt(min(bb1, bb2));
}
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;
float d = map(p);
// coloring
vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.65,0.85,1.0);
col *= 1.0 - exp(-6.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)) );
if( iMouse.z>0.001 )
{
d = map(m);
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 | [
1219,
1323,
1389,
1389,
1543
] | [
[
1219,
1323,
1389,
1389,
1543
],
[
1545,
1545,
1564,
1564,
1757
],
[
1760,
1760,
1817,
1817,
2436
]
] | // Essentially the box from iq
// Squared distance so you only have to take one sqrt for multiple aabbs
| float sqAABB( in vec2 p, in vec2 minCorner, in vec2 maxCorner )
{ |
vec2 size = (maxCorner - minCorner) * 0.5;
vec2 center = minCorner + size;
vec2 d = max(abs(p - center) - size, 0.0);
return dot(d, d);
} | // Essentially the box from iq
// Squared distance so you only have to take one sqrt for multiple aabbs
float sqAABB( in vec2 p, in vec2 minCorner, in vec2 maxCorner )
{ | 1 | 1 |
DdlyRr | lush3dash1 | 2023-06-11T21:14:16 | // "Wind Waker Ocean" by @Polyflare (29/1/15)
// License: Creative Commons Attribution 4.0 International
// Source code for the texture generator is available at:
// https://github.com/lmurray/circleator
//-----------------------------------------------------------------------------
// User settings
// 0 = No antialiasing
// 1 = 2x2 supersampling antialiasing
#define ANTIALIAS 1
// 0 = Static camera
// 1 = Animate the camera
#define ANIMATE_CAM 0
// 0 = Do not distort the water texture
// 1 = Apply lateral distortion to the water texture
#define DISTORT_WATER 1
// 0 = Disable parallax effects
// 1 = Change the height of the water with parallax effects
#define PARALLAX_WATER 1
// 0 = Antialias the water texture
// 1 = Do not antialias the water texture
#define FAST_CIRCLES 1
//-----------------------------------------------------------------------------
#define WATER3_COL vec3(0.0, 0.4453, 0.7305)
#define WATER4_COL vec3(0.0, 0.4180, 0.6758)
#define WATER_COL vec3(0.374, 0.8453, 0.5305)
#define WATER2_COL vec3(0.374, 0.8180, 0.4758)
#define FOAM_COL vec3(0.5125, 0.9609, 0.9648)
#define FOG_COL vec3(0.7006, 0.9453, 0.9336)
#define SKY_COL vec3(0.0, 0.8203, 1.0)
#define M_2PI 6.283185307
#define M_6PI 18.84955592
float circ(vec2 pos, vec2 c, float s)
{
c = abs(pos - c);
c = min(c, 1.0 - c);
#if FAST_CIRCLES
return dot(c, c) < s ? -1.0 : 0.0;
#else
return smoothstep(0.0, 0.002, sqrt(s) - sqrt(dot(c, c))) * -1.0;
#endif
}
// Foam pattern for the water constructed out of a series of circles
float waterlayer(vec2 uv)
{
uv = mod(uv, 1.0); // Clamp to [0..1]
float ret = 1.0;
ret += circ(uv, vec2(0.37378, 0.277169), 0.0268181);
ret += circ(uv, vec2(0.0317477, 0.540372), 0.0193742);
ret += circ(uv, vec2(0.430044, 0.882218), 0.0232337);
ret += circ(uv, vec2(0.641033, 0.695106), 0.0117864);
ret += circ(uv, vec2(0.0146398, 0.0791346), 0.0299458);
ret += circ(uv, vec2(0.43871, 0.394445), 0.0289087);
ret += circ(uv, vec2(0.909446, 0.878141), 0.028466);
ret += circ(uv, vec2(0.310149, 0.686637), 0.0128496);
ret += circ(uv, vec2(0.928617, 0.195986), 0.0152041);
ret += circ(uv, vec2(0.0438506, 0.868153), 0.0268601);
ret += circ(uv, vec2(0.308619, 0.194937), 0.00806102);
ret += circ(uv, vec2(0.349922, 0.449714), 0.00928667);
ret += circ(uv, vec2(0.0449556, 0.953415), 0.023126);
ret += circ(uv, vec2(0.117761, 0.503309), 0.0151272);
ret += circ(uv, vec2(0.563517, 0.244991), 0.0292322);
ret += circ(uv, vec2(0.566936, 0.954457), 0.00981141);
ret += circ(uv, vec2(0.0489944, 0.200931), 0.0178746);
ret += circ(uv, vec2(0.569297, 0.624893), 0.0132408);
ret += circ(uv, vec2(0.298347, 0.710972), 0.0114426);
ret += circ(uv, vec2(0.878141, 0.771279), 0.00322719);
ret += circ(uv, vec2(0.150995, 0.376221), 0.00216157);
ret += circ(uv, vec2(0.119673, 0.541984), 0.0124621);
ret += circ(uv, vec2(0.629598, 0.295629), 0.0198736);
ret += circ(uv, vec2(0.334357, 0.266278), 0.0187145);
ret += circ(uv, vec2(0.918044, 0.968163), 0.0182928);
ret += circ(uv, vec2(0.965445, 0.505026), 0.006348);
ret += circ(uv, vec2(0.514847, 0.865444), 0.00623523);
ret += circ(uv, vec2(0.710575, 0.0415131), 0.00322689);
ret += circ(uv, vec2(0.71403, 0.576945), 0.0215641);
ret += circ(uv, vec2(0.748873, 0.413325), 0.0110795);
ret += circ(uv, vec2(0.0623365, 0.896713), 0.0236203);
ret += circ(uv, vec2(0.980482, 0.473849), 0.00573439);
ret += circ(uv, vec2(0.647463, 0.654349), 0.0188713);
ret += circ(uv, vec2(0.651406, 0.981297), 0.00710875);
ret += circ(uv, vec2(0.428928, 0.382426), 0.0298806);
ret += circ(uv, vec2(0.811545, 0.62568), 0.00265539);
ret += circ(uv, vec2(0.400787, 0.74162), 0.00486609);
ret += circ(uv, vec2(0.331283, 0.418536), 0.00598028);
ret += circ(uv, vec2(0.894762, 0.0657997), 0.00760375);
ret += circ(uv, vec2(0.525104, 0.572233), 0.0141796);
ret += circ(uv, vec2(0.431526, 0.911372), 0.0213234);
ret += circ(uv, vec2(0.658212, 0.910553), 0.000741023);
ret += circ(uv, vec2(0.514523, 0.243263), 0.0270685);
ret += circ(uv, vec2(0.0249494, 0.252872), 0.00876653);
ret += circ(uv, vec2(0.502214, 0.47269), 0.0234534);
ret += circ(uv, vec2(0.693271, 0.431469), 0.0246533);
ret += circ(uv, vec2(0.415, 0.884418), 0.0271696);
ret += circ(uv, vec2(0.149073, 0.41204), 0.00497198);
ret += circ(uv, vec2(0.533816, 0.897634), 0.00650833);
ret += circ(uv, vec2(0.0409132, 0.83406), 0.0191398);
ret += circ(uv, vec2(0.638585, 0.646019), 0.0206129);
ret += circ(uv, vec2(0.660342, 0.966541), 0.0053511);
ret += circ(uv, vec2(0.513783, 0.142233), 0.00471653);
ret += circ(uv, vec2(0.124305, 0.644263), 0.00116724);
ret += circ(uv, vec2(0.99871, 0.583864), 0.0107329);
ret += circ(uv, vec2(0.894879, 0.233289), 0.00667092);
ret += circ(uv, vec2(0.246286, 0.682766), 0.00411623);
ret += circ(uv, vec2(0.0761895, 0.16327), 0.0145935);
ret += circ(uv, vec2(0.949386, 0.802936), 0.0100873);
ret += circ(uv, vec2(0.480122, 0.196554), 0.0110185);
ret += circ(uv, vec2(0.896854, 0.803707), 0.013969);
ret += circ(uv, vec2(0.292865, 0.762973), 0.00566413);
ret += circ(uv, vec2(0.0995585, 0.117457), 0.00869407);
ret += circ(uv, vec2(0.377713, 0.00335442), 0.0063147);
ret += circ(uv, vec2(0.506365, 0.531118), 0.0144016);
ret += circ(uv, vec2(0.408806, 0.894771), 0.0243923);
ret += circ(uv, vec2(0.143579, 0.85138), 0.00418529);
ret += circ(uv, vec2(0.0902811, 0.181775), 0.0108896);
ret += circ(uv, vec2(0.780695, 0.394644), 0.00475475);
ret += circ(uv, vec2(0.298036, 0.625531), 0.00325285);
ret += circ(uv, vec2(0.218423, 0.714537), 0.00157212);
ret += circ(uv, vec2(0.658836, 0.159556), 0.00225897);
ret += circ(uv, vec2(0.987324, 0.146545), 0.0288391);
ret += circ(uv, vec2(0.222646, 0.251694), 0.00092276);
ret += circ(uv, vec2(0.159826, 0.528063), 0.00605293);
return max(ret, 0.0);
}
// Procedural texture generation for the water
vec3 water(vec2 uv, vec3 cdir)
{
uv *= vec2(0.25);
#if PARALLAX_WATER
// Parallax height distortion with two directional waves at
// slightly different angles.
vec2 a = 0.025 * cdir.xz / cdir.y; // Parallax offset
float h = sin(uv.x + iTime); // Height at UV
uv += a * h;
h = sin(0.841471 * uv.x - 0.540302 * uv.y + iTime);
uv += a * h;
#endif
#if DISTORT_WATER
// Texture distortion
float d1 = mod(uv.x + uv.y, M_2PI);
float d2 = mod((uv.x + uv.y + 0.25) * 1.3, M_6PI);
d1 = iTime * 0.07 + d1;
d2 = iTime * 0.5 + d2;
vec2 dist = vec2(
sin(d1) * 0.15 + sin(d2) * 0.05,
cos(d1) * 0.15 + cos(d2) * 0.05
);
#else
const vec2 dist = vec2(0.0);
#endif
vec3 ret = mix(WATER_COL, WATER2_COL, waterlayer(uv + dist.xy));
ret = mix(ret, FOAM_COL, waterlayer(vec2(1.0) - uv - dist.yx));
return ret;
}
// Camera perspective based on [0..1] viewport
vec3 pixtoray(vec2 uv)
{
vec3 pixpos;
pixpos.xy = uv - 0.5;
pixpos.y *= iResolution.y / iResolution.x; // Aspect correction
pixpos.z = -0.6; // Focal length (Controls field of view)
return normalize(pixpos);
}
// Quaternion-vector multiplication
vec3 quatmul(vec4 q, vec3 v)
{
vec3 qvec = q.xyz;
vec3 uv = cross(qvec, v);
vec3 uuv = cross(qvec, uv);
uv *= (2.0 * q.w);
uuv *= 2.0;
return v + uv + uuv;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
fragColor = vec4(0.0, 0.0, 0.0, 1.0);
#if ANTIALIAS
for(int y = 0; y < 2; y++) {
for(int x = 0; x < 2; x++) {
vec2 offset = vec2(0.5) * vec2(x, y) - vec2(0.25);
#else
vec2 offset = vec2(0.0);
#endif
// Camera stuff
vec2 uv = (fragCoord.xy + offset) / iResolution.xy;
vec3 cpos = vec3(0.0, 7.0, 10.0); // Camera position
vec3 cdir = pixtoray(uv);
cdir = quatmul( // Tilt down slightly
vec4(-0.19867, 0.0, 0.0, 0.980067), cdir);
#if ANIMATE_CAM
// Rotating camera
float cost = cos(iTime * -0.05);
float sint = sin(iTime * -0.05);
cdir.xz = cost * cdir.xz + sint * vec2(-cdir.z, cdir.x);
cpos.xz = cost * cpos.xz + sint * vec2(-cpos.z, cpos.x);
#endif
// Ray-plane intersection
const vec3 ocean = vec3(0.0, 1.0, 0.0);
float dist = -dot(cpos, ocean) / dot(cdir, ocean);
vec3 pos = cpos + dist * cdir;
vec3 pix;
if(dist > 0.0 && dist < 100.0) {
// Ocean
vec3 wat = water(pos.xz, cdir);
pix = mix(wat, FOG_COL, min(dist * 0.01, 1.0));
} else {
// Sky
pix = mix(FOG_COL, SKY_COL, min(cdir.y * 4.0, 1.0));
}
#if ANTIALIAS
fragColor.rgb += pix * vec3(0.25);
}
}
#else
fragColor.rgb = pix;
#endif
} | cc-by-4.0 | [
6034,
6081,
6113,
6113,
6970
] | [
[
1241,
1241,
1280,
1280,
1467
],
[
1469,
1538,
1565,
1565,
6032
],
[
6034,
6081,
6113,
6113,
6970
],
[
6972,
7019,
7043,
7043,
7248
],
[
7250,
7286,
7316,
7316,
7467
]
] | // Procedural texture generation for the water
| vec3 water(vec2 uv, vec3 cdir)
{ |
uv *= vec2(0.25);
#if PARALLAX_WATER
// Parallax height distortion with two directional waves at
// slightly different angles.
vec2 a = 0.025 * cdir.xz / cdir.y; // Parallax offset
float h = sin(uv.x + iTime); // Height at UV
uv += a * h;
h = sin(0.841471 * uv.x - 0.540302 * uv.y + iTime);
uv += a * h;
#endif
#if DISTORT_WATER
// Texture distortion
float d1 = mod(uv.x + uv.y, M_2PI);
float d2 = mod((uv.x + uv.y + 0.25) * 1.3, M_6PI);
d1 = iTime * 0.07 + d1;
d2 = iTime * 0.5 + d2;
vec2 dist = vec2(
sin(d1) * 0.15 + sin(d2) * 0.05,
cos(d1) * 0.15 + cos(d2) * 0.05
);
#else
const vec2 dist = vec2(0.0);
#endif
vec3 ret = mix(WATER_COL, WATER2_COL, waterlayer(uv + dist.xy));
ret = mix(ret, FOAM_COL, waterlayer(vec2(1.0) - uv - dist.yx));
return ret;
} | // Procedural texture generation for the water
vec3 water(vec2 uv, vec3 cdir)
{ | 2 | 3 |
DdlyRr | lush3dash1 | 2023-06-11T21:14:16 | // "Wind Waker Ocean" by @Polyflare (29/1/15)
// License: Creative Commons Attribution 4.0 International
// Source code for the texture generator is available at:
// https://github.com/lmurray/circleator
//-----------------------------------------------------------------------------
// User settings
// 0 = No antialiasing
// 1 = 2x2 supersampling antialiasing
#define ANTIALIAS 1
// 0 = Static camera
// 1 = Animate the camera
#define ANIMATE_CAM 0
// 0 = Do not distort the water texture
// 1 = Apply lateral distortion to the water texture
#define DISTORT_WATER 1
// 0 = Disable parallax effects
// 1 = Change the height of the water with parallax effects
#define PARALLAX_WATER 1
// 0 = Antialias the water texture
// 1 = Do not antialias the water texture
#define FAST_CIRCLES 1
//-----------------------------------------------------------------------------
#define WATER3_COL vec3(0.0, 0.4453, 0.7305)
#define WATER4_COL vec3(0.0, 0.4180, 0.6758)
#define WATER_COL vec3(0.374, 0.8453, 0.5305)
#define WATER2_COL vec3(0.374, 0.8180, 0.4758)
#define FOAM_COL vec3(0.5125, 0.9609, 0.9648)
#define FOG_COL vec3(0.7006, 0.9453, 0.9336)
#define SKY_COL vec3(0.0, 0.8203, 1.0)
#define M_2PI 6.283185307
#define M_6PI 18.84955592
float circ(vec2 pos, vec2 c, float s)
{
c = abs(pos - c);
c = min(c, 1.0 - c);
#if FAST_CIRCLES
return dot(c, c) < s ? -1.0 : 0.0;
#else
return smoothstep(0.0, 0.002, sqrt(s) - sqrt(dot(c, c))) * -1.0;
#endif
}
// Foam pattern for the water constructed out of a series of circles
float waterlayer(vec2 uv)
{
uv = mod(uv, 1.0); // Clamp to [0..1]
float ret = 1.0;
ret += circ(uv, vec2(0.37378, 0.277169), 0.0268181);
ret += circ(uv, vec2(0.0317477, 0.540372), 0.0193742);
ret += circ(uv, vec2(0.430044, 0.882218), 0.0232337);
ret += circ(uv, vec2(0.641033, 0.695106), 0.0117864);
ret += circ(uv, vec2(0.0146398, 0.0791346), 0.0299458);
ret += circ(uv, vec2(0.43871, 0.394445), 0.0289087);
ret += circ(uv, vec2(0.909446, 0.878141), 0.028466);
ret += circ(uv, vec2(0.310149, 0.686637), 0.0128496);
ret += circ(uv, vec2(0.928617, 0.195986), 0.0152041);
ret += circ(uv, vec2(0.0438506, 0.868153), 0.0268601);
ret += circ(uv, vec2(0.308619, 0.194937), 0.00806102);
ret += circ(uv, vec2(0.349922, 0.449714), 0.00928667);
ret += circ(uv, vec2(0.0449556, 0.953415), 0.023126);
ret += circ(uv, vec2(0.117761, 0.503309), 0.0151272);
ret += circ(uv, vec2(0.563517, 0.244991), 0.0292322);
ret += circ(uv, vec2(0.566936, 0.954457), 0.00981141);
ret += circ(uv, vec2(0.0489944, 0.200931), 0.0178746);
ret += circ(uv, vec2(0.569297, 0.624893), 0.0132408);
ret += circ(uv, vec2(0.298347, 0.710972), 0.0114426);
ret += circ(uv, vec2(0.878141, 0.771279), 0.00322719);
ret += circ(uv, vec2(0.150995, 0.376221), 0.00216157);
ret += circ(uv, vec2(0.119673, 0.541984), 0.0124621);
ret += circ(uv, vec2(0.629598, 0.295629), 0.0198736);
ret += circ(uv, vec2(0.334357, 0.266278), 0.0187145);
ret += circ(uv, vec2(0.918044, 0.968163), 0.0182928);
ret += circ(uv, vec2(0.965445, 0.505026), 0.006348);
ret += circ(uv, vec2(0.514847, 0.865444), 0.00623523);
ret += circ(uv, vec2(0.710575, 0.0415131), 0.00322689);
ret += circ(uv, vec2(0.71403, 0.576945), 0.0215641);
ret += circ(uv, vec2(0.748873, 0.413325), 0.0110795);
ret += circ(uv, vec2(0.0623365, 0.896713), 0.0236203);
ret += circ(uv, vec2(0.980482, 0.473849), 0.00573439);
ret += circ(uv, vec2(0.647463, 0.654349), 0.0188713);
ret += circ(uv, vec2(0.651406, 0.981297), 0.00710875);
ret += circ(uv, vec2(0.428928, 0.382426), 0.0298806);
ret += circ(uv, vec2(0.811545, 0.62568), 0.00265539);
ret += circ(uv, vec2(0.400787, 0.74162), 0.00486609);
ret += circ(uv, vec2(0.331283, 0.418536), 0.00598028);
ret += circ(uv, vec2(0.894762, 0.0657997), 0.00760375);
ret += circ(uv, vec2(0.525104, 0.572233), 0.0141796);
ret += circ(uv, vec2(0.431526, 0.911372), 0.0213234);
ret += circ(uv, vec2(0.658212, 0.910553), 0.000741023);
ret += circ(uv, vec2(0.514523, 0.243263), 0.0270685);
ret += circ(uv, vec2(0.0249494, 0.252872), 0.00876653);
ret += circ(uv, vec2(0.502214, 0.47269), 0.0234534);
ret += circ(uv, vec2(0.693271, 0.431469), 0.0246533);
ret += circ(uv, vec2(0.415, 0.884418), 0.0271696);
ret += circ(uv, vec2(0.149073, 0.41204), 0.00497198);
ret += circ(uv, vec2(0.533816, 0.897634), 0.00650833);
ret += circ(uv, vec2(0.0409132, 0.83406), 0.0191398);
ret += circ(uv, vec2(0.638585, 0.646019), 0.0206129);
ret += circ(uv, vec2(0.660342, 0.966541), 0.0053511);
ret += circ(uv, vec2(0.513783, 0.142233), 0.00471653);
ret += circ(uv, vec2(0.124305, 0.644263), 0.00116724);
ret += circ(uv, vec2(0.99871, 0.583864), 0.0107329);
ret += circ(uv, vec2(0.894879, 0.233289), 0.00667092);
ret += circ(uv, vec2(0.246286, 0.682766), 0.00411623);
ret += circ(uv, vec2(0.0761895, 0.16327), 0.0145935);
ret += circ(uv, vec2(0.949386, 0.802936), 0.0100873);
ret += circ(uv, vec2(0.480122, 0.196554), 0.0110185);
ret += circ(uv, vec2(0.896854, 0.803707), 0.013969);
ret += circ(uv, vec2(0.292865, 0.762973), 0.00566413);
ret += circ(uv, vec2(0.0995585, 0.117457), 0.00869407);
ret += circ(uv, vec2(0.377713, 0.00335442), 0.0063147);
ret += circ(uv, vec2(0.506365, 0.531118), 0.0144016);
ret += circ(uv, vec2(0.408806, 0.894771), 0.0243923);
ret += circ(uv, vec2(0.143579, 0.85138), 0.00418529);
ret += circ(uv, vec2(0.0902811, 0.181775), 0.0108896);
ret += circ(uv, vec2(0.780695, 0.394644), 0.00475475);
ret += circ(uv, vec2(0.298036, 0.625531), 0.00325285);
ret += circ(uv, vec2(0.218423, 0.714537), 0.00157212);
ret += circ(uv, vec2(0.658836, 0.159556), 0.00225897);
ret += circ(uv, vec2(0.987324, 0.146545), 0.0288391);
ret += circ(uv, vec2(0.222646, 0.251694), 0.00092276);
ret += circ(uv, vec2(0.159826, 0.528063), 0.00605293);
return max(ret, 0.0);
}
// Procedural texture generation for the water
vec3 water(vec2 uv, vec3 cdir)
{
uv *= vec2(0.25);
#if PARALLAX_WATER
// Parallax height distortion with two directional waves at
// slightly different angles.
vec2 a = 0.025 * cdir.xz / cdir.y; // Parallax offset
float h = sin(uv.x + iTime); // Height at UV
uv += a * h;
h = sin(0.841471 * uv.x - 0.540302 * uv.y + iTime);
uv += a * h;
#endif
#if DISTORT_WATER
// Texture distortion
float d1 = mod(uv.x + uv.y, M_2PI);
float d2 = mod((uv.x + uv.y + 0.25) * 1.3, M_6PI);
d1 = iTime * 0.07 + d1;
d2 = iTime * 0.5 + d2;
vec2 dist = vec2(
sin(d1) * 0.15 + sin(d2) * 0.05,
cos(d1) * 0.15 + cos(d2) * 0.05
);
#else
const vec2 dist = vec2(0.0);
#endif
vec3 ret = mix(WATER_COL, WATER2_COL, waterlayer(uv + dist.xy));
ret = mix(ret, FOAM_COL, waterlayer(vec2(1.0) - uv - dist.yx));
return ret;
}
// Camera perspective based on [0..1] viewport
vec3 pixtoray(vec2 uv)
{
vec3 pixpos;
pixpos.xy = uv - 0.5;
pixpos.y *= iResolution.y / iResolution.x; // Aspect correction
pixpos.z = -0.6; // Focal length (Controls field of view)
return normalize(pixpos);
}
// Quaternion-vector multiplication
vec3 quatmul(vec4 q, vec3 v)
{
vec3 qvec = q.xyz;
vec3 uv = cross(qvec, v);
vec3 uuv = cross(qvec, uv);
uv *= (2.0 * q.w);
uuv *= 2.0;
return v + uv + uuv;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
fragColor = vec4(0.0, 0.0, 0.0, 1.0);
#if ANTIALIAS
for(int y = 0; y < 2; y++) {
for(int x = 0; x < 2; x++) {
vec2 offset = vec2(0.5) * vec2(x, y) - vec2(0.25);
#else
vec2 offset = vec2(0.0);
#endif
// Camera stuff
vec2 uv = (fragCoord.xy + offset) / iResolution.xy;
vec3 cpos = vec3(0.0, 7.0, 10.0); // Camera position
vec3 cdir = pixtoray(uv);
cdir = quatmul( // Tilt down slightly
vec4(-0.19867, 0.0, 0.0, 0.980067), cdir);
#if ANIMATE_CAM
// Rotating camera
float cost = cos(iTime * -0.05);
float sint = sin(iTime * -0.05);
cdir.xz = cost * cdir.xz + sint * vec2(-cdir.z, cdir.x);
cpos.xz = cost * cpos.xz + sint * vec2(-cpos.z, cpos.x);
#endif
// Ray-plane intersection
const vec3 ocean = vec3(0.0, 1.0, 0.0);
float dist = -dot(cpos, ocean) / dot(cdir, ocean);
vec3 pos = cpos + dist * cdir;
vec3 pix;
if(dist > 0.0 && dist < 100.0) {
// Ocean
vec3 wat = water(pos.xz, cdir);
pix = mix(wat, FOG_COL, min(dist * 0.01, 1.0));
} else {
// Sky
pix = mix(FOG_COL, SKY_COL, min(cdir.y * 4.0, 1.0));
}
#if ANTIALIAS
fragColor.rgb += pix * vec3(0.25);
}
}
#else
fragColor.rgb = pix;
#endif
} | cc-by-4.0 | [
6972,
7019,
7043,
7043,
7248
] | [
[
1241,
1241,
1280,
1280,
1467
],
[
1469,
1538,
1565,
1565,
6032
],
[
6034,
6081,
6113,
6113,
6970
],
[
6972,
7019,
7043,
7043,
7248
],
[
7250,
7286,
7316,
7316,
7467
]
] | // Camera perspective based on [0..1] viewport
| vec3 pixtoray(vec2 uv)
{ |
vec3 pixpos;
pixpos.xy = uv - 0.5;
pixpos.y *= iResolution.y / iResolution.x; // Aspect correction
pixpos.z = -0.6; // Focal length (Controls field of view)
return normalize(pixpos);
} | // Camera perspective based on [0..1] viewport
vec3 pixtoray(vec2 uv)
{ | 2 | 2 |
DdlyRr | lush3dash1 | 2023-06-11T21:14:16 | // "Wind Waker Ocean" by @Polyflare (29/1/15)
// License: Creative Commons Attribution 4.0 International
// Source code for the texture generator is available at:
// https://github.com/lmurray/circleator
//-----------------------------------------------------------------------------
// User settings
// 0 = No antialiasing
// 1 = 2x2 supersampling antialiasing
#define ANTIALIAS 1
// 0 = Static camera
// 1 = Animate the camera
#define ANIMATE_CAM 0
// 0 = Do not distort the water texture
// 1 = Apply lateral distortion to the water texture
#define DISTORT_WATER 1
// 0 = Disable parallax effects
// 1 = Change the height of the water with parallax effects
#define PARALLAX_WATER 1
// 0 = Antialias the water texture
// 1 = Do not antialias the water texture
#define FAST_CIRCLES 1
//-----------------------------------------------------------------------------
#define WATER3_COL vec3(0.0, 0.4453, 0.7305)
#define WATER4_COL vec3(0.0, 0.4180, 0.6758)
#define WATER_COL vec3(0.374, 0.8453, 0.5305)
#define WATER2_COL vec3(0.374, 0.8180, 0.4758)
#define FOAM_COL vec3(0.5125, 0.9609, 0.9648)
#define FOG_COL vec3(0.7006, 0.9453, 0.9336)
#define SKY_COL vec3(0.0, 0.8203, 1.0)
#define M_2PI 6.283185307
#define M_6PI 18.84955592
float circ(vec2 pos, vec2 c, float s)
{
c = abs(pos - c);
c = min(c, 1.0 - c);
#if FAST_CIRCLES
return dot(c, c) < s ? -1.0 : 0.0;
#else
return smoothstep(0.0, 0.002, sqrt(s) - sqrt(dot(c, c))) * -1.0;
#endif
}
// Foam pattern for the water constructed out of a series of circles
float waterlayer(vec2 uv)
{
uv = mod(uv, 1.0); // Clamp to [0..1]
float ret = 1.0;
ret += circ(uv, vec2(0.37378, 0.277169), 0.0268181);
ret += circ(uv, vec2(0.0317477, 0.540372), 0.0193742);
ret += circ(uv, vec2(0.430044, 0.882218), 0.0232337);
ret += circ(uv, vec2(0.641033, 0.695106), 0.0117864);
ret += circ(uv, vec2(0.0146398, 0.0791346), 0.0299458);
ret += circ(uv, vec2(0.43871, 0.394445), 0.0289087);
ret += circ(uv, vec2(0.909446, 0.878141), 0.028466);
ret += circ(uv, vec2(0.310149, 0.686637), 0.0128496);
ret += circ(uv, vec2(0.928617, 0.195986), 0.0152041);
ret += circ(uv, vec2(0.0438506, 0.868153), 0.0268601);
ret += circ(uv, vec2(0.308619, 0.194937), 0.00806102);
ret += circ(uv, vec2(0.349922, 0.449714), 0.00928667);
ret += circ(uv, vec2(0.0449556, 0.953415), 0.023126);
ret += circ(uv, vec2(0.117761, 0.503309), 0.0151272);
ret += circ(uv, vec2(0.563517, 0.244991), 0.0292322);
ret += circ(uv, vec2(0.566936, 0.954457), 0.00981141);
ret += circ(uv, vec2(0.0489944, 0.200931), 0.0178746);
ret += circ(uv, vec2(0.569297, 0.624893), 0.0132408);
ret += circ(uv, vec2(0.298347, 0.710972), 0.0114426);
ret += circ(uv, vec2(0.878141, 0.771279), 0.00322719);
ret += circ(uv, vec2(0.150995, 0.376221), 0.00216157);
ret += circ(uv, vec2(0.119673, 0.541984), 0.0124621);
ret += circ(uv, vec2(0.629598, 0.295629), 0.0198736);
ret += circ(uv, vec2(0.334357, 0.266278), 0.0187145);
ret += circ(uv, vec2(0.918044, 0.968163), 0.0182928);
ret += circ(uv, vec2(0.965445, 0.505026), 0.006348);
ret += circ(uv, vec2(0.514847, 0.865444), 0.00623523);
ret += circ(uv, vec2(0.710575, 0.0415131), 0.00322689);
ret += circ(uv, vec2(0.71403, 0.576945), 0.0215641);
ret += circ(uv, vec2(0.748873, 0.413325), 0.0110795);
ret += circ(uv, vec2(0.0623365, 0.896713), 0.0236203);
ret += circ(uv, vec2(0.980482, 0.473849), 0.00573439);
ret += circ(uv, vec2(0.647463, 0.654349), 0.0188713);
ret += circ(uv, vec2(0.651406, 0.981297), 0.00710875);
ret += circ(uv, vec2(0.428928, 0.382426), 0.0298806);
ret += circ(uv, vec2(0.811545, 0.62568), 0.00265539);
ret += circ(uv, vec2(0.400787, 0.74162), 0.00486609);
ret += circ(uv, vec2(0.331283, 0.418536), 0.00598028);
ret += circ(uv, vec2(0.894762, 0.0657997), 0.00760375);
ret += circ(uv, vec2(0.525104, 0.572233), 0.0141796);
ret += circ(uv, vec2(0.431526, 0.911372), 0.0213234);
ret += circ(uv, vec2(0.658212, 0.910553), 0.000741023);
ret += circ(uv, vec2(0.514523, 0.243263), 0.0270685);
ret += circ(uv, vec2(0.0249494, 0.252872), 0.00876653);
ret += circ(uv, vec2(0.502214, 0.47269), 0.0234534);
ret += circ(uv, vec2(0.693271, 0.431469), 0.0246533);
ret += circ(uv, vec2(0.415, 0.884418), 0.0271696);
ret += circ(uv, vec2(0.149073, 0.41204), 0.00497198);
ret += circ(uv, vec2(0.533816, 0.897634), 0.00650833);
ret += circ(uv, vec2(0.0409132, 0.83406), 0.0191398);
ret += circ(uv, vec2(0.638585, 0.646019), 0.0206129);
ret += circ(uv, vec2(0.660342, 0.966541), 0.0053511);
ret += circ(uv, vec2(0.513783, 0.142233), 0.00471653);
ret += circ(uv, vec2(0.124305, 0.644263), 0.00116724);
ret += circ(uv, vec2(0.99871, 0.583864), 0.0107329);
ret += circ(uv, vec2(0.894879, 0.233289), 0.00667092);
ret += circ(uv, vec2(0.246286, 0.682766), 0.00411623);
ret += circ(uv, vec2(0.0761895, 0.16327), 0.0145935);
ret += circ(uv, vec2(0.949386, 0.802936), 0.0100873);
ret += circ(uv, vec2(0.480122, 0.196554), 0.0110185);
ret += circ(uv, vec2(0.896854, 0.803707), 0.013969);
ret += circ(uv, vec2(0.292865, 0.762973), 0.00566413);
ret += circ(uv, vec2(0.0995585, 0.117457), 0.00869407);
ret += circ(uv, vec2(0.377713, 0.00335442), 0.0063147);
ret += circ(uv, vec2(0.506365, 0.531118), 0.0144016);
ret += circ(uv, vec2(0.408806, 0.894771), 0.0243923);
ret += circ(uv, vec2(0.143579, 0.85138), 0.00418529);
ret += circ(uv, vec2(0.0902811, 0.181775), 0.0108896);
ret += circ(uv, vec2(0.780695, 0.394644), 0.00475475);
ret += circ(uv, vec2(0.298036, 0.625531), 0.00325285);
ret += circ(uv, vec2(0.218423, 0.714537), 0.00157212);
ret += circ(uv, vec2(0.658836, 0.159556), 0.00225897);
ret += circ(uv, vec2(0.987324, 0.146545), 0.0288391);
ret += circ(uv, vec2(0.222646, 0.251694), 0.00092276);
ret += circ(uv, vec2(0.159826, 0.528063), 0.00605293);
return max(ret, 0.0);
}
// Procedural texture generation for the water
vec3 water(vec2 uv, vec3 cdir)
{
uv *= vec2(0.25);
#if PARALLAX_WATER
// Parallax height distortion with two directional waves at
// slightly different angles.
vec2 a = 0.025 * cdir.xz / cdir.y; // Parallax offset
float h = sin(uv.x + iTime); // Height at UV
uv += a * h;
h = sin(0.841471 * uv.x - 0.540302 * uv.y + iTime);
uv += a * h;
#endif
#if DISTORT_WATER
// Texture distortion
float d1 = mod(uv.x + uv.y, M_2PI);
float d2 = mod((uv.x + uv.y + 0.25) * 1.3, M_6PI);
d1 = iTime * 0.07 + d1;
d2 = iTime * 0.5 + d2;
vec2 dist = vec2(
sin(d1) * 0.15 + sin(d2) * 0.05,
cos(d1) * 0.15 + cos(d2) * 0.05
);
#else
const vec2 dist = vec2(0.0);
#endif
vec3 ret = mix(WATER_COL, WATER2_COL, waterlayer(uv + dist.xy));
ret = mix(ret, FOAM_COL, waterlayer(vec2(1.0) - uv - dist.yx));
return ret;
}
// Camera perspective based on [0..1] viewport
vec3 pixtoray(vec2 uv)
{
vec3 pixpos;
pixpos.xy = uv - 0.5;
pixpos.y *= iResolution.y / iResolution.x; // Aspect correction
pixpos.z = -0.6; // Focal length (Controls field of view)
return normalize(pixpos);
}
// Quaternion-vector multiplication
vec3 quatmul(vec4 q, vec3 v)
{
vec3 qvec = q.xyz;
vec3 uv = cross(qvec, v);
vec3 uuv = cross(qvec, uv);
uv *= (2.0 * q.w);
uuv *= 2.0;
return v + uv + uuv;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
fragColor = vec4(0.0, 0.0, 0.0, 1.0);
#if ANTIALIAS
for(int y = 0; y < 2; y++) {
for(int x = 0; x < 2; x++) {
vec2 offset = vec2(0.5) * vec2(x, y) - vec2(0.25);
#else
vec2 offset = vec2(0.0);
#endif
// Camera stuff
vec2 uv = (fragCoord.xy + offset) / iResolution.xy;
vec3 cpos = vec3(0.0, 7.0, 10.0); // Camera position
vec3 cdir = pixtoray(uv);
cdir = quatmul( // Tilt down slightly
vec4(-0.19867, 0.0, 0.0, 0.980067), cdir);
#if ANIMATE_CAM
// Rotating camera
float cost = cos(iTime * -0.05);
float sint = sin(iTime * -0.05);
cdir.xz = cost * cdir.xz + sint * vec2(-cdir.z, cdir.x);
cpos.xz = cost * cpos.xz + sint * vec2(-cpos.z, cpos.x);
#endif
// Ray-plane intersection
const vec3 ocean = vec3(0.0, 1.0, 0.0);
float dist = -dot(cpos, ocean) / dot(cdir, ocean);
vec3 pos = cpos + dist * cdir;
vec3 pix;
if(dist > 0.0 && dist < 100.0) {
// Ocean
vec3 wat = water(pos.xz, cdir);
pix = mix(wat, FOG_COL, min(dist * 0.01, 1.0));
} else {
// Sky
pix = mix(FOG_COL, SKY_COL, min(cdir.y * 4.0, 1.0));
}
#if ANTIALIAS
fragColor.rgb += pix * vec3(0.25);
}
}
#else
fragColor.rgb = pix;
#endif
} | cc-by-4.0 | [
7250,
7286,
7316,
7316,
7467
] | [
[
1241,
1241,
1280,
1280,
1467
],
[
1469,
1538,
1565,
1565,
6032
],
[
6034,
6081,
6113,
6113,
6970
],
[
6972,
7019,
7043,
7043,
7248
],
[
7250,
7286,
7316,
7316,
7467
]
] | // Quaternion-vector multiplication
| vec3 quatmul(vec4 q, vec3 v)
{ |
vec3 qvec = q.xyz;
vec3 uv = cross(qvec, v);
vec3 uuv = cross(qvec, uv);
uv *= (2.0 * q.w);
uuv *= 2.0;
return v + uv + uuv;
} | // Quaternion-vector multiplication
vec3 quatmul(vec4 q, vec3 v)
{ | 2 | 2 |
Dt3SDH | iq | 2023-06-06T07:02:34 | // The MIT License
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org/
// Copyright © 2023 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.
// Shader for https://www.youtube.com/watch?v=tfLku5IjUzE (cosmetics removed)
// When a ray doesn't intersect a sphere, it still does it if one is
// willing to take complex coordinates for the intersection points.
// This shader shows where these complex intersections are in space,
// for some arbitrary moving ray.
//-------------------------------------------------------------------
// https://iquilezles.org/articles/distfunctions
float sdCircle( in vec2 p, in vec2 c, in float r )
{
return length(p-c)-r;
}
// https://iquilezles.org/articles/distfunctions
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/articles/distfunctions/
vec2 opUnion( vec2 m, float d, float a )
{
return (d<m.x) ? vec2(d,a) : m;
}
// https://iquilezles.org/articles/intersectors/
void intersectCircle( in vec2 ro, in vec2 rd, float rad, out vec4 p1, out vec4 p2 )
{
float b = dot( ro, rd );
float c = dot( ro, ro ) - rad*rad;
float h = b*b - c;
if( h>0.0 )
{
// real
h = sqrt(h);
p1 = vec4( ro + (-b+h)*rd, ro );
p2 = vec4( ro + (-b-h)*rd, ro );
}
else
{
// complex
h = sqrt(-h);
p1 = vec4( ro - b*rd, ro + h*rd );
p2 = vec4( ro - b*rd, ro - h*rd );
}
}
//-------------------------------------------------------------------
void get_ray( out vec2 ro, out vec2 rd, in float t )
{
float an = 3.1415927*(0.5 + 0.2*sin(t*0.735+4.0));
ro = vec2(-1.0*cos(t*0.525),-1.0+0.1*sin(t*1.5));
rd = vec2(cos(an),sin(an));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float px = 2.0/iResolution.y;
p.y -= 0.4;
const float rad = 0.4;
const float cth = 0.03;
// background
vec3 col = vec3(0.2 - 0.1*length(p*0.5));
// circle
{
float d = sdCircle( p, vec2(0.0,0.0), rad );
col = mix( col, vec3(0.75), 1.0-smoothstep(0.0,0.01,abs(d)-0.005) );
}
// trails
{
vec2 dr = vec2(1e20,1.0);
vec2 di = vec2(1e20,1.0);
vec4 op1;
vec4 op2;
const int num = 256;
for( int i=0; i<num; i++ )
{
float a = float(i)/float(num);
vec2 ro, rd; get_ray( ro, rd, iTime - 3.0*a );
vec4 p1, p2; intersectCircle( ro, rd, rad, p1, p2 );
if( i>0 )
{
dr = opUnion(dr, min(sdLine( p, p1.xy, op1.xy ), sdLine( p, p2.xy, op2.xy )), a);
di = opUnion(di, min(sdLine( p, p1.zw, op1.zw ), sdLine( p, p2.zw, op2.zw )), a);
}
op1 = p1;
op2 = p2;
}
col = mix( col, vec3(0.0,0.7,1.2), smoothstep(1.0,0.7,dr.y)*(1.0-smoothstep(0.0,0.01,dr.x)) );
col = mix( col, vec3(1.2,0.7,0.0), smoothstep(1.0,0.7,di.y)*(1.0-smoothstep(0.0,0.01,di.x)) );
}
vec2 ro, rd; get_ray( ro, rd, iTime );
// ray
{
{
float d = sdLine( p, ro-rd*10.0, ro+rd*10.0 );
col = mix( col, vec3(1.0), 0.25*(1.0-smoothstep(0.0,0.008,d)) );
}
{
vec2 rdp = vec2(-rd.y,rd.x);
vec2 tip = ro+rd*0.2;
float d = sdLine( p, ro, tip );
d = min( d, sdLine( p, tip, tip + 0.05*normalize( rdp-2.0*rd) ) );
d = min( d, sdLine( p, tip, tip + 0.05*normalize(-rdp-2.0*rd) ) );
//d = min( d, sdCircle( p, ro, cth ) );
col = mix( col, vec3(0.75), 1.0-smoothstep(0.005,0.01,d) );
}
}
// intersections
{
vec4 p1, p2; intersectCircle( ro, rd, rad, p1, p2 );
float dr = min( sdCircle( p, p1.xy, cth ), sdCircle( p, p2.xy, cth ) );
float di = min( sdCircle( p, p1.zw, cth ), sdCircle( p, p2.zw, cth ) );
col = mix( col, vec3(0.0,0.7,1.2), 1.0-smoothstep(0.005,0.01,dr) );
col = mix( col, vec3(1.2,0.7,0.0), 1.0-smoothstep(0.005,0.01,di) );
}
// cheap dithering
col += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor = vec4(col,1.0);
} | mit | [
1539,
1588,
1640,
1640,
1668
] | [
[
1539,
1588,
1640,
1640,
1668
],
[
1670,
1719,
1768,
1768,
1878
],
[
1880,
1930,
1972,
1972,
2010
],
[
2012,
2061,
2146,
2146,
2530
],
[
2603,
2603,
2657,
2657,
2800
],
[
2802,
2802,
2859,
2859,
5307
]
] | // https://iquilezles.org/articles/distfunctions
| float sdCircle( in vec2 p, in vec2 c, in float r )
{ |
return length(p-c)-r;
} | // https://iquilezles.org/articles/distfunctions
float sdCircle( in vec2 p, in vec2 c, in float r )
{ | 2 | 5 |
Dt3SDH | iq | 2023-06-06T07:02:34 | // The MIT License
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org/
// Copyright © 2023 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.
// Shader for https://www.youtube.com/watch?v=tfLku5IjUzE (cosmetics removed)
// When a ray doesn't intersect a sphere, it still does it if one is
// willing to take complex coordinates for the intersection points.
// This shader shows where these complex intersections are in space,
// for some arbitrary moving ray.
//-------------------------------------------------------------------
// https://iquilezles.org/articles/distfunctions
float sdCircle( in vec2 p, in vec2 c, in float r )
{
return length(p-c)-r;
}
// https://iquilezles.org/articles/distfunctions
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/articles/distfunctions/
vec2 opUnion( vec2 m, float d, float a )
{
return (d<m.x) ? vec2(d,a) : m;
}
// https://iquilezles.org/articles/intersectors/
void intersectCircle( in vec2 ro, in vec2 rd, float rad, out vec4 p1, out vec4 p2 )
{
float b = dot( ro, rd );
float c = dot( ro, ro ) - rad*rad;
float h = b*b - c;
if( h>0.0 )
{
// real
h = sqrt(h);
p1 = vec4( ro + (-b+h)*rd, ro );
p2 = vec4( ro + (-b-h)*rd, ro );
}
else
{
// complex
h = sqrt(-h);
p1 = vec4( ro - b*rd, ro + h*rd );
p2 = vec4( ro - b*rd, ro - h*rd );
}
}
//-------------------------------------------------------------------
void get_ray( out vec2 ro, out vec2 rd, in float t )
{
float an = 3.1415927*(0.5 + 0.2*sin(t*0.735+4.0));
ro = vec2(-1.0*cos(t*0.525),-1.0+0.1*sin(t*1.5));
rd = vec2(cos(an),sin(an));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float px = 2.0/iResolution.y;
p.y -= 0.4;
const float rad = 0.4;
const float cth = 0.03;
// background
vec3 col = vec3(0.2 - 0.1*length(p*0.5));
// circle
{
float d = sdCircle( p, vec2(0.0,0.0), rad );
col = mix( col, vec3(0.75), 1.0-smoothstep(0.0,0.01,abs(d)-0.005) );
}
// trails
{
vec2 dr = vec2(1e20,1.0);
vec2 di = vec2(1e20,1.0);
vec4 op1;
vec4 op2;
const int num = 256;
for( int i=0; i<num; i++ )
{
float a = float(i)/float(num);
vec2 ro, rd; get_ray( ro, rd, iTime - 3.0*a );
vec4 p1, p2; intersectCircle( ro, rd, rad, p1, p2 );
if( i>0 )
{
dr = opUnion(dr, min(sdLine( p, p1.xy, op1.xy ), sdLine( p, p2.xy, op2.xy )), a);
di = opUnion(di, min(sdLine( p, p1.zw, op1.zw ), sdLine( p, p2.zw, op2.zw )), a);
}
op1 = p1;
op2 = p2;
}
col = mix( col, vec3(0.0,0.7,1.2), smoothstep(1.0,0.7,dr.y)*(1.0-smoothstep(0.0,0.01,dr.x)) );
col = mix( col, vec3(1.2,0.7,0.0), smoothstep(1.0,0.7,di.y)*(1.0-smoothstep(0.0,0.01,di.x)) );
}
vec2 ro, rd; get_ray( ro, rd, iTime );
// ray
{
{
float d = sdLine( p, ro-rd*10.0, ro+rd*10.0 );
col = mix( col, vec3(1.0), 0.25*(1.0-smoothstep(0.0,0.008,d)) );
}
{
vec2 rdp = vec2(-rd.y,rd.x);
vec2 tip = ro+rd*0.2;
float d = sdLine( p, ro, tip );
d = min( d, sdLine( p, tip, tip + 0.05*normalize( rdp-2.0*rd) ) );
d = min( d, sdLine( p, tip, tip + 0.05*normalize(-rdp-2.0*rd) ) );
//d = min( d, sdCircle( p, ro, cth ) );
col = mix( col, vec3(0.75), 1.0-smoothstep(0.005,0.01,d) );
}
}
// intersections
{
vec4 p1, p2; intersectCircle( ro, rd, rad, p1, p2 );
float dr = min( sdCircle( p, p1.xy, cth ), sdCircle( p, p2.xy, cth ) );
float di = min( sdCircle( p, p1.zw, cth ), sdCircle( p, p2.zw, cth ) );
col = mix( col, vec3(0.0,0.7,1.2), 1.0-smoothstep(0.005,0.01,dr) );
col = mix( col, vec3(1.2,0.7,0.0), 1.0-smoothstep(0.005,0.01,di) );
}
// cheap dithering
col += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor = vec4(col,1.0);
} | mit | [
1670,
1719,
1768,
1768,
1878
] | [
[
1539,
1588,
1640,
1640,
1668
],
[
1670,
1719,
1768,
1768,
1878
],
[
1880,
1930,
1972,
1972,
2010
],
[
2012,
2061,
2146,
2146,
2530
],
[
2603,
2603,
2657,
2657,
2800
],
[
2802,
2802,
2859,
2859,
5307
]
] | // https://iquilezles.org/articles/distfunctions
| 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/articles/distfunctions
float sdLine( in vec2 p, in vec2 a, in vec2 b )
{ | 6 | 23 |
Dt3SDH | iq | 2023-06-06T07:02:34 | // The MIT License
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org/
// Copyright © 2023 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.
// Shader for https://www.youtube.com/watch?v=tfLku5IjUzE (cosmetics removed)
// When a ray doesn't intersect a sphere, it still does it if one is
// willing to take complex coordinates for the intersection points.
// This shader shows where these complex intersections are in space,
// for some arbitrary moving ray.
//-------------------------------------------------------------------
// https://iquilezles.org/articles/distfunctions
float sdCircle( in vec2 p, in vec2 c, in float r )
{
return length(p-c)-r;
}
// https://iquilezles.org/articles/distfunctions
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/articles/distfunctions/
vec2 opUnion( vec2 m, float d, float a )
{
return (d<m.x) ? vec2(d,a) : m;
}
// https://iquilezles.org/articles/intersectors/
void intersectCircle( in vec2 ro, in vec2 rd, float rad, out vec4 p1, out vec4 p2 )
{
float b = dot( ro, rd );
float c = dot( ro, ro ) - rad*rad;
float h = b*b - c;
if( h>0.0 )
{
// real
h = sqrt(h);
p1 = vec4( ro + (-b+h)*rd, ro );
p2 = vec4( ro + (-b-h)*rd, ro );
}
else
{
// complex
h = sqrt(-h);
p1 = vec4( ro - b*rd, ro + h*rd );
p2 = vec4( ro - b*rd, ro - h*rd );
}
}
//-------------------------------------------------------------------
void get_ray( out vec2 ro, out vec2 rd, in float t )
{
float an = 3.1415927*(0.5 + 0.2*sin(t*0.735+4.0));
ro = vec2(-1.0*cos(t*0.525),-1.0+0.1*sin(t*1.5));
rd = vec2(cos(an),sin(an));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float px = 2.0/iResolution.y;
p.y -= 0.4;
const float rad = 0.4;
const float cth = 0.03;
// background
vec3 col = vec3(0.2 - 0.1*length(p*0.5));
// circle
{
float d = sdCircle( p, vec2(0.0,0.0), rad );
col = mix( col, vec3(0.75), 1.0-smoothstep(0.0,0.01,abs(d)-0.005) );
}
// trails
{
vec2 dr = vec2(1e20,1.0);
vec2 di = vec2(1e20,1.0);
vec4 op1;
vec4 op2;
const int num = 256;
for( int i=0; i<num; i++ )
{
float a = float(i)/float(num);
vec2 ro, rd; get_ray( ro, rd, iTime - 3.0*a );
vec4 p1, p2; intersectCircle( ro, rd, rad, p1, p2 );
if( i>0 )
{
dr = opUnion(dr, min(sdLine( p, p1.xy, op1.xy ), sdLine( p, p2.xy, op2.xy )), a);
di = opUnion(di, min(sdLine( p, p1.zw, op1.zw ), sdLine( p, p2.zw, op2.zw )), a);
}
op1 = p1;
op2 = p2;
}
col = mix( col, vec3(0.0,0.7,1.2), smoothstep(1.0,0.7,dr.y)*(1.0-smoothstep(0.0,0.01,dr.x)) );
col = mix( col, vec3(1.2,0.7,0.0), smoothstep(1.0,0.7,di.y)*(1.0-smoothstep(0.0,0.01,di.x)) );
}
vec2 ro, rd; get_ray( ro, rd, iTime );
// ray
{
{
float d = sdLine( p, ro-rd*10.0, ro+rd*10.0 );
col = mix( col, vec3(1.0), 0.25*(1.0-smoothstep(0.0,0.008,d)) );
}
{
vec2 rdp = vec2(-rd.y,rd.x);
vec2 tip = ro+rd*0.2;
float d = sdLine( p, ro, tip );
d = min( d, sdLine( p, tip, tip + 0.05*normalize( rdp-2.0*rd) ) );
d = min( d, sdLine( p, tip, tip + 0.05*normalize(-rdp-2.0*rd) ) );
//d = min( d, sdCircle( p, ro, cth ) );
col = mix( col, vec3(0.75), 1.0-smoothstep(0.005,0.01,d) );
}
}
// intersections
{
vec4 p1, p2; intersectCircle( ro, rd, rad, p1, p2 );
float dr = min( sdCircle( p, p1.xy, cth ), sdCircle( p, p2.xy, cth ) );
float di = min( sdCircle( p, p1.zw, cth ), sdCircle( p, p2.zw, cth ) );
col = mix( col, vec3(0.0,0.7,1.2), 1.0-smoothstep(0.005,0.01,dr) );
col = mix( col, vec3(1.2,0.7,0.0), 1.0-smoothstep(0.005,0.01,di) );
}
// cheap dithering
col += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor = vec4(col,1.0);
} | mit | [
1880,
1930,
1972,
1972,
2010
] | [
[
1539,
1588,
1640,
1640,
1668
],
[
1670,
1719,
1768,
1768,
1878
],
[
1880,
1930,
1972,
1972,
2010
],
[
2012,
2061,
2146,
2146,
2530
],
[
2603,
2603,
2657,
2657,
2800
],
[
2802,
2802,
2859,
2859,
5307
]
] | // https://iquilezles.org/articles/distfunctions/
| vec2 opUnion( vec2 m, float d, float a )
{ |
return (d<m.x) ? vec2(d,a) : m;
} | // https://iquilezles.org/articles/distfunctions/
vec2 opUnion( vec2 m, float d, float a )
{ | 1 | 1 |
Dt3SDH | iq | 2023-06-06T07:02:34 | // The MIT License
// https://www.youtube.com/c/InigoQuilez
// https://iquilezles.org/
// Copyright © 2023 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.
// Shader for https://www.youtube.com/watch?v=tfLku5IjUzE (cosmetics removed)
// When a ray doesn't intersect a sphere, it still does it if one is
// willing to take complex coordinates for the intersection points.
// This shader shows where these complex intersections are in space,
// for some arbitrary moving ray.
//-------------------------------------------------------------------
// https://iquilezles.org/articles/distfunctions
float sdCircle( in vec2 p, in vec2 c, in float r )
{
return length(p-c)-r;
}
// https://iquilezles.org/articles/distfunctions
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/articles/distfunctions/
vec2 opUnion( vec2 m, float d, float a )
{
return (d<m.x) ? vec2(d,a) : m;
}
// https://iquilezles.org/articles/intersectors/
void intersectCircle( in vec2 ro, in vec2 rd, float rad, out vec4 p1, out vec4 p2 )
{
float b = dot( ro, rd );
float c = dot( ro, ro ) - rad*rad;
float h = b*b - c;
if( h>0.0 )
{
// real
h = sqrt(h);
p1 = vec4( ro + (-b+h)*rd, ro );
p2 = vec4( ro + (-b-h)*rd, ro );
}
else
{
// complex
h = sqrt(-h);
p1 = vec4( ro - b*rd, ro + h*rd );
p2 = vec4( ro - b*rd, ro - h*rd );
}
}
//-------------------------------------------------------------------
void get_ray( out vec2 ro, out vec2 rd, in float t )
{
float an = 3.1415927*(0.5 + 0.2*sin(t*0.735+4.0));
ro = vec2(-1.0*cos(t*0.525),-1.0+0.1*sin(t*1.5));
rd = vec2(cos(an),sin(an));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
float px = 2.0/iResolution.y;
p.y -= 0.4;
const float rad = 0.4;
const float cth = 0.03;
// background
vec3 col = vec3(0.2 - 0.1*length(p*0.5));
// circle
{
float d = sdCircle( p, vec2(0.0,0.0), rad );
col = mix( col, vec3(0.75), 1.0-smoothstep(0.0,0.01,abs(d)-0.005) );
}
// trails
{
vec2 dr = vec2(1e20,1.0);
vec2 di = vec2(1e20,1.0);
vec4 op1;
vec4 op2;
const int num = 256;
for( int i=0; i<num; i++ )
{
float a = float(i)/float(num);
vec2 ro, rd; get_ray( ro, rd, iTime - 3.0*a );
vec4 p1, p2; intersectCircle( ro, rd, rad, p1, p2 );
if( i>0 )
{
dr = opUnion(dr, min(sdLine( p, p1.xy, op1.xy ), sdLine( p, p2.xy, op2.xy )), a);
di = opUnion(di, min(sdLine( p, p1.zw, op1.zw ), sdLine( p, p2.zw, op2.zw )), a);
}
op1 = p1;
op2 = p2;
}
col = mix( col, vec3(0.0,0.7,1.2), smoothstep(1.0,0.7,dr.y)*(1.0-smoothstep(0.0,0.01,dr.x)) );
col = mix( col, vec3(1.2,0.7,0.0), smoothstep(1.0,0.7,di.y)*(1.0-smoothstep(0.0,0.01,di.x)) );
}
vec2 ro, rd; get_ray( ro, rd, iTime );
// ray
{
{
float d = sdLine( p, ro-rd*10.0, ro+rd*10.0 );
col = mix( col, vec3(1.0), 0.25*(1.0-smoothstep(0.0,0.008,d)) );
}
{
vec2 rdp = vec2(-rd.y,rd.x);
vec2 tip = ro+rd*0.2;
float d = sdLine( p, ro, tip );
d = min( d, sdLine( p, tip, tip + 0.05*normalize( rdp-2.0*rd) ) );
d = min( d, sdLine( p, tip, tip + 0.05*normalize(-rdp-2.0*rd) ) );
//d = min( d, sdCircle( p, ro, cth ) );
col = mix( col, vec3(0.75), 1.0-smoothstep(0.005,0.01,d) );
}
}
// intersections
{
vec4 p1, p2; intersectCircle( ro, rd, rad, p1, p2 );
float dr = min( sdCircle( p, p1.xy, cth ), sdCircle( p, p2.xy, cth ) );
float di = min( sdCircle( p, p1.zw, cth ), sdCircle( p, p2.zw, cth ) );
col = mix( col, vec3(0.0,0.7,1.2), 1.0-smoothstep(0.005,0.01,dr) );
col = mix( col, vec3(1.2,0.7,0.0), 1.0-smoothstep(0.005,0.01,di) );
}
// cheap dithering
col += sin(fragCoord.x*114.0)*sin(fragCoord.y*211.1)/512.0;
fragColor = vec4(col,1.0);
} | mit | [
2012,
2061,
2146,
2146,
2530
] | [
[
1539,
1588,
1640,
1640,
1668
],
[
1670,
1719,
1768,
1768,
1878
],
[
1880,
1930,
1972,
1972,
2010
],
[
2012,
2061,
2146,
2146,
2530
],
[
2603,
2603,
2657,
2657,
2800
],
[
2802,
2802,
2859,
2859,
5307
]
] | // https://iquilezles.org/articles/intersectors/
| void intersectCircle( in vec2 ro, in vec2 rd, float rad, out vec4 p1, out vec4 p2 )
{ |
float b = dot( ro, rd );
float c = dot( ro, ro ) - rad*rad;
float h = b*b - c;
if( h>0.0 )
{
// real
h = sqrt(h);
p1 = vec4( ro + (-b+h)*rd, ro );
p2 = vec4( ro + (-b-h)*rd, ro );
}
else
{
// complex
h = sqrt(-h);
p1 = vec4( ro - b*rd, ro + h*rd );
p2 = vec4( ro - b*rd, ro - h*rd );
}
} | // https://iquilezles.org/articles/intersectors/
void intersectCircle( in vec2 ro, in vec2 rd, float rad, out vec4 p1, out vec4 p2 )
{ | 1 | 1 |
slGyWt | iq | 2023-06-02T00:42:14 | // The MIT License
// Copyright © 2023 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.
// Approximating the SDF of the subtraction of two SDFs,
// by finding the closest intersection between the two shapes.
// Not working very well :(
// SDFs from iquilezles.org/articles/distfunctions2d
// .x = f(p), .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgCircle( in vec2 p, in vec2 c, in float r )
{
p -= c;
float l = length(p);
return vec3( l-r, p/l );
}
// SDFs from iquilezles.org/articles/distfunctions2d
// .x = f(p), .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgBox( in vec2 p, in vec2 b )
{
vec2 w = abs(p)-b;
vec2 s = vec2(p.x<0.0?-1:1,p.y<0.0?-1:1);
float g = max(w.x,w.y);
vec2 q = max(w,0.0);
float l = length(q);
return vec3( (g>0.0)?l: g,
s*((g>0.0)?q/l : ((w.x>w.y)?vec2(1,0):vec2(0,1))));
}
float cro( vec2 a, vec2 b ) { return a.x*b.y - a.y*b.x; }
//-----------------
#define opSubtract(p,A,B)\
/* regular subtraction */ \
max(A.x,-B.x);\
if( d>0.0 )\
{\
vec2 op = p;\
/* find closest intersection of the two shapes */ \
/* by recursively averaging the two closest points */ \
for( int i=0; i<512; i++ ) \
{ \
float d1=A.x; vec2 g1=A.yz; \
float d2=B.x; vec2 g2=B.yz; \
if( max(abs(d1),abs(d2))<0.001 ) break; \
p -= 0.5*(d1*g1 + d2*g2); \
} \
/* distance to closest intersection*/ \
float d3 = length(p-op);\
/* decide whether we should update distance */ \
vec2 g1 = A.yz;\
vec2 g2 = B.yz;\
float no = cro(g1,g2);\
if( min(cro(op-p,g1)*no,cro(op-p,g2)*no)>0.0) d = d3;\
}
float map( in vec2 p )
{
vec2 off = 0.1*sin(iTime+vec2(0.0,2.0));
float d = opSubtract( p, sdgBox(p,vec2(0.3,0.6)),
sdgCircle(p,vec2(0.0,0.2)+off,0.4) );
return d;
}
vec2 gra( in vec2 p )
{
const float e = 0.0002;
return vec2(map(p+vec2(e,0.0))-map(p-vec2(e,0.0)),
map(p+vec2(0.0,e))-map(p-vec2(0.0,e)))/(2.0*e);
}
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;
// distance
float d = map(p);
// coloring
vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.5,0.85,1.0);
col *= 1.0 - exp2(-32.0*abs(d));
col *= 0.8 + 0.2*cos(128.0*abs(d));
col = mix( col, vec3(1.0), 1.0-smoothstep(0.002,0.005,abs(d)) );
// debug gradient
{
#if 0
vec2 g = gra(p);
col *= 1.0 + vec3(0.5*g,0.0);
float l = length(g);
if( l>1.01 ) col=vec3(1,0,0);
if( l<0.99 ) col=vec3(0,0,1);
#endif
}
// debug distance with mouse
if( iMouse.z>0.001 )
{
d = map(m);
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 | [
1229,
1333,
1386,
1386,
1454
] | [
[
1229,
1333,
1386,
1386,
1454
],
[
1456,
1560,
1597,
1597,
1855
],
[
1858,
1858,
1887,
1887,
1915
],
[
2726,
2726,
2750,
2750,
2934
],
[
2936,
2936,
2959,
2959,
3108
],
[
3112,
3112,
3169,
3205,
4117
]
] | // SDFs from iquilezles.org/articles/distfunctions2d
// .x = f(p), .yz = ∇f(p) with ‖∇f(p)‖ = 1
| vec3 sdgCircle( in vec2 p, in vec2 c, in float r )
{ |
p -= c;
float l = length(p);
return vec3( l-r, p/l );
} | // SDFs from iquilezles.org/articles/distfunctions2d
// .x = f(p), .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgCircle( in vec2 p, in vec2 c, in float r )
{ | 1 | 1 |
slGyWt | iq | 2023-06-02T00:42:14 | // The MIT License
// Copyright © 2023 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.
// Approximating the SDF of the subtraction of two SDFs,
// by finding the closest intersection between the two shapes.
// Not working very well :(
// SDFs from iquilezles.org/articles/distfunctions2d
// .x = f(p), .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgCircle( in vec2 p, in vec2 c, in float r )
{
p -= c;
float l = length(p);
return vec3( l-r, p/l );
}
// SDFs from iquilezles.org/articles/distfunctions2d
// .x = f(p), .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgBox( in vec2 p, in vec2 b )
{
vec2 w = abs(p)-b;
vec2 s = vec2(p.x<0.0?-1:1,p.y<0.0?-1:1);
float g = max(w.x,w.y);
vec2 q = max(w,0.0);
float l = length(q);
return vec3( (g>0.0)?l: g,
s*((g>0.0)?q/l : ((w.x>w.y)?vec2(1,0):vec2(0,1))));
}
float cro( vec2 a, vec2 b ) { return a.x*b.y - a.y*b.x; }
//-----------------
#define opSubtract(p,A,B)\
/* regular subtraction */ \
max(A.x,-B.x);\
if( d>0.0 )\
{\
vec2 op = p;\
/* find closest intersection of the two shapes */ \
/* by recursively averaging the two closest points */ \
for( int i=0; i<512; i++ ) \
{ \
float d1=A.x; vec2 g1=A.yz; \
float d2=B.x; vec2 g2=B.yz; \
if( max(abs(d1),abs(d2))<0.001 ) break; \
p -= 0.5*(d1*g1 + d2*g2); \
} \
/* distance to closest intersection*/ \
float d3 = length(p-op);\
/* decide whether we should update distance */ \
vec2 g1 = A.yz;\
vec2 g2 = B.yz;\
float no = cro(g1,g2);\
if( min(cro(op-p,g1)*no,cro(op-p,g2)*no)>0.0) d = d3;\
}
float map( in vec2 p )
{
vec2 off = 0.1*sin(iTime+vec2(0.0,2.0));
float d = opSubtract( p, sdgBox(p,vec2(0.3,0.6)),
sdgCircle(p,vec2(0.0,0.2)+off,0.4) );
return d;
}
vec2 gra( in vec2 p )
{
const float e = 0.0002;
return vec2(map(p+vec2(e,0.0))-map(p-vec2(e,0.0)),
map(p+vec2(0.0,e))-map(p-vec2(0.0,e)))/(2.0*e);
}
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;
// distance
float d = map(p);
// coloring
vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.5,0.85,1.0);
col *= 1.0 - exp2(-32.0*abs(d));
col *= 0.8 + 0.2*cos(128.0*abs(d));
col = mix( col, vec3(1.0), 1.0-smoothstep(0.002,0.005,abs(d)) );
// debug gradient
{
#if 0
vec2 g = gra(p);
col *= 1.0 + vec3(0.5*g,0.0);
float l = length(g);
if( l>1.01 ) col=vec3(1,0,0);
if( l<0.99 ) col=vec3(0,0,1);
#endif
}
// debug distance with mouse
if( iMouse.z>0.001 )
{
d = map(m);
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 | [
1456,
1560,
1597,
1597,
1855
] | [
[
1229,
1333,
1386,
1386,
1454
],
[
1456,
1560,
1597,
1597,
1855
],
[
1858,
1858,
1887,
1887,
1915
],
[
2726,
2726,
2750,
2750,
2934
],
[
2936,
2936,
2959,
2959,
3108
],
[
3112,
3112,
3169,
3205,
4117
]
] | // SDFs from iquilezles.org/articles/distfunctions2d
// .x = f(p), .yz = ∇f(p) with ‖∇f(p)‖ = 1
| vec3 sdgBox( in vec2 p, in vec2 b )
{ |
vec2 w = abs(p)-b;
vec2 s = vec2(p.x<0.0?-1:1,p.y<0.0?-1:1);
float g = max(w.x,w.y);
vec2 q = max(w,0.0);
float l = length(q);
return vec3( (g>0.0)?l: g,
s*((g>0.0)?q/l : ((w.x>w.y)?vec2(1,0):vec2(0,1))));
} | // SDFs from iquilezles.org/articles/distfunctions2d
// .x = f(p), .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgBox( in vec2 p, in vec2 b )
{ | 1 | 5 |
DldXRf | iq | 2023-06-01T22:51:37 | // The MIT License
// Copyright © 2023 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 heart. Probably faster than
// central differences or automatic differentiation/dual numbers.
// List of other 2D distances+gradients:
// https://iquilezles.org/articles/distgradfunctions2d
// 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 sdgHeart( in vec2 p )
{
float sx = p.x<0.0?-1.0:1.0;
p.x = abs(p.x);
if( p.y+p.x>1.0 )
{
const float r = sqrt(2.0)/4.0;
vec2 q0 = p - vec2(0.25,0.75);
float l = length(q0);
vec3 d = vec3(l-r,q0/l);
d.y *= sx;
return d;
}
else
{
vec2 q1 = p-vec2(0.0,1.0); vec3 d1 = vec3(dot(q1,q1),q1);
vec2 q2 = p-0.5*max(p.x+p.y,0.0); vec3 d2 = vec3(dot(q2,q2),q2);
vec3 d = (d1.x<d2.x) ? d1 : d2;
d.x = sqrt(d.x);
d.yz /= d.x;
d *= ((p.x>p.y)?1.0:-1.0);
d.y *= sx;
return d;
}
}
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;
p.y += 0.5;
m.y += 0.5;
// sdf(p) and gradient(sdf(p))
vec3 dg = sdgHeart(p);
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)) );
// interaction
if( iMouse.z>0.001 )
{
d = sdgHeart(m).x;
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 | [
1363,
1458,
1486,
1486,
2088
] | [
[
1363,
1458,
1486,
1486,
2088
],
[
2091,
2091,
2148,
2148,
3108
]
] | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
| vec3 sdgHeart( in vec2 p )
{ |
float sx = p.x<0.0?-1.0:1.0;
p.x = abs(p.x);
if( p.y+p.x>1.0 )
{
const float r = sqrt(2.0)/4.0;
vec2 q0 = p - vec2(0.25,0.75);
float l = length(q0);
vec3 d = vec3(l-r,q0/l);
d.y *= sx;
return d;
}
else
{
vec2 q1 = p-vec2(0.0,1.0); vec3 d1 = vec3(dot(q1,q1),q1);
vec2 q2 = p-0.5*max(p.x+p.y,0.0); vec3 d2 = vec3(dot(q2,q2),q2);
vec3 d = (d1.x<d2.x) ? d1 : d2;
d.x = sqrt(d.x);
d.yz /= d.x;
d *= ((p.x>p.y)?1.0:-1.0);
d.y *= sx;
return d;
}
} | // .x = f(p)
// .y = ∂f(p)/∂x
// .z = ∂f(p)/∂y
// .yz = ∇f(p) with ‖∇f(p)‖ = 1
vec3 sdgHeart( in vec2 p )
{ | 1 | 1 |
ctfcW4 | SnoopethDuckDuck | 2023-07-30T13:52:14 | // License: CC0
#define pi 3.14159
#define ss(a) smoothstep(-1./R.y, 1./R.y, .02 - length(u - a))
// Spring easing function:
// https://www.desmos.com/calculator/vluz5j0svo
// Wobble both ways (green)
// https://www.desmos.com/calculator/9atfimg1ox
#define f(a,b,x) sign(cos(x)) \
* (1. - exp(-(a) * abs(cos(x))) * cos((b) * abs(cos(x))))
// Instant push -> wobble (red)
// https://www.desmos.com/calculator/gbkax818fx
float g(float a1, float b1, float a2, float b2, float x) {
x = mod(x, 2.);
float f1 = 1. - exp(-a1 * x) * cos(b1 * x);
float f2 = exp(-a2 * (x-1.)) * cos(b2 * (x-1.));
f2 = mix(1., f2, step(1., x));
return 1. - 2. * f1 * f2;
}
// Slow push -> wobble (blue)
// https://www.desmos.com/calculator/rwsnoaj9by
// a: oscillation strength, a > 0
// b: oscillation amount, b = anything
// n: Superellipse strength, n = 2, 4, 6, etc.
float h(float a, float b, float n, float x) {
// Multiply by square wave to flip-flop sign of wave
float s = sign(mod(x, 4.) - 2.);
// Make x periodic
x = mod(x, 2.);
// Clamp x so mix(f,g,v) is a quarter superellipse for 1 < x < 2
float v = min(1., x);
// Spring equation
float f = 1. - exp(-a * x) * cos(b * x);
// Half superellipse equation (n = 2. is circle)
float g = pow(1. - pow(1.-x, n), 1./n);
// g = sqrt(1. - (1.-x) * (1.-x));
// Mix spring into circle, then mix with an equation which
// is vertical at 0 so that the start/end gradients
// match at x = 0, 2, 4 etc.
// (sloppy and expensive)
float l = mix(mix(f, g, v),
1. - exp(-7. * sqrt(x)),
1. - pow(v, .1));
return l * s;
}
void mainImage( out vec4 O, in vec2 I )
{
float t = iTime;
vec2 R = iResolution.xy,
u = (I-.5*R)/R.y,
p = vec2(.5 * f(5., 10., pi/2. * (t + 1.)), .1),
q = vec2(.5 * g(12., 20., 6., 16., .5 * t), 0),
r = vec2(.5 * h(8., 22., 4., t), -.1);
O = vec4(ss(q), ss(p), ss(r), 0);
} | cc0-1.0 | [
358,
438,
496,
496,
685
] | [
[
358,
438,
496,
496,
685
],
[
687,
889,
934,
991,
1723
],
[
1731,
1731,
1772,
1772,
2067
]
] | // Instant push -> wobble (red)
// https://www.desmos.com/calculator/gbkax818fx
| float g(float a1, float b1, float a2, float b2, float x) { |
x = mod(x, 2.);
float f1 = 1. - exp(-a1 * x) * cos(b1 * x);
float f2 = exp(-a2 * (x-1.)) * cos(b2 * (x-1.));
f2 = mix(1., f2, step(1., x));
return 1. - 2. * f1 * f2;
} | // Instant push -> wobble (red)
// https://www.desmos.com/calculator/gbkax818fx
float g(float a1, float b1, float a2, float b2, float x) { | 1 | 1 |
ctfcW4 | SnoopethDuckDuck | 2023-07-30T13:52:14 | // License: CC0
#define pi 3.14159
#define ss(a) smoothstep(-1./R.y, 1./R.y, .02 - length(u - a))
// Spring easing function:
// https://www.desmos.com/calculator/vluz5j0svo
// Wobble both ways (green)
// https://www.desmos.com/calculator/9atfimg1ox
#define f(a,b,x) sign(cos(x)) \
* (1. - exp(-(a) * abs(cos(x))) * cos((b) * abs(cos(x))))
// Instant push -> wobble (red)
// https://www.desmos.com/calculator/gbkax818fx
float g(float a1, float b1, float a2, float b2, float x) {
x = mod(x, 2.);
float f1 = 1. - exp(-a1 * x) * cos(b1 * x);
float f2 = exp(-a2 * (x-1.)) * cos(b2 * (x-1.));
f2 = mix(1., f2, step(1., x));
return 1. - 2. * f1 * f2;
}
// Slow push -> wobble (blue)
// https://www.desmos.com/calculator/rwsnoaj9by
// a: oscillation strength, a > 0
// b: oscillation amount, b = anything
// n: Superellipse strength, n = 2, 4, 6, etc.
float h(float a, float b, float n, float x) {
// Multiply by square wave to flip-flop sign of wave
float s = sign(mod(x, 4.) - 2.);
// Make x periodic
x = mod(x, 2.);
// Clamp x so mix(f,g,v) is a quarter superellipse for 1 < x < 2
float v = min(1., x);
// Spring equation
float f = 1. - exp(-a * x) * cos(b * x);
// Half superellipse equation (n = 2. is circle)
float g = pow(1. - pow(1.-x, n), 1./n);
// g = sqrt(1. - (1.-x) * (1.-x));
// Mix spring into circle, then mix with an equation which
// is vertical at 0 so that the start/end gradients
// match at x = 0, 2, 4 etc.
// (sloppy and expensive)
float l = mix(mix(f, g, v),
1. - exp(-7. * sqrt(x)),
1. - pow(v, .1));
return l * s;
}
void mainImage( out vec4 O, in vec2 I )
{
float t = iTime;
vec2 R = iResolution.xy,
u = (I-.5*R)/R.y,
p = vec2(.5 * f(5., 10., pi/2. * (t + 1.)), .1),
q = vec2(.5 * g(12., 20., 6., 16., .5 * t), 0),
r = vec2(.5 * h(8., 22., 4., t), -.1);
O = vec4(ss(q), ss(p), ss(r), 0);
} | cc0-1.0 | [
687,
889,
934,
991,
1723
] | [
[
358,
438,
496,
496,
685
],
[
687,
889,
934,
991,
1723
],
[
1731,
1731,
1772,
1772,
2067
]
] | // Slow push -> wobble (blue)
// https://www.desmos.com/calculator/rwsnoaj9by
// a: oscillation strength, a > 0
// b: oscillation amount, b = anything
// n: Superellipse strength, n = 2, 4, 6, etc.
| float h(float a, float b, float n, float x) { |
float s = sign(mod(x, 4.) - 2.);
// Make x periodic
x = mod(x, 2.);
// Clamp x so mix(f,g,v) is a quarter superellipse for 1 < x < 2
float v = min(1., x);
// Spring equation
float f = 1. - exp(-a * x) * cos(b * x);
// Half superellipse equation (n = 2. is circle)
float g = pow(1. - pow(1.-x, n), 1./n);
// g = sqrt(1. - (1.-x) * (1.-x));
// Mix spring into circle, then mix with an equation which
// is vertical at 0 so that the start/end gradients
// match at x = 0, 2, 4 etc.
// (sloppy and expensive)
float l = mix(mix(f, g, v),
1. - exp(-7. * sqrt(x)),
1. - pow(v, .1));
return l * s;
} | // Slow push -> wobble (blue)
// https://www.desmos.com/calculator/rwsnoaj9by
// a: oscillation strength, a > 0
// b: oscillation amount, b = anything
// n: Superellipse strength, n = 2, 4, 6, etc.
float h(float a, float b, float n, float x) { | 1 | 1 |
ms2BWV | alphardex | 2023-07-24T07:28:13 | //
// Description : Array and textureless GLSL 2D/3D/4D simplex
// noise functions.
// 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
//
vec3 mod289(vec3 x){
return x-floor(x*(1./289.))*289.;
}
vec4 mod289(vec4 x){
return x-floor(x*(1./289.))*289.;
}
vec4 permute(vec4 x){
return mod289(((x*34.)+1.)*x);
}
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159-.85373472095314*r;
}
float snoise(vec3 v)
{
const vec2 C=vec2(1./6.,1./3.);
const vec4 D=vec4(0.,.5,1.,2.);
// First corner
vec3 i=floor(v+dot(v,C.yyy));
vec3 x0=v-i+dot(i,C.xxx);
// Other corners
vec3 g=step(x0.yzx,x0.xyz);
vec3 l=1.-g;
vec3 i1=min(g.xyz,l.zxy);
vec3 i2=max(g.xyz,l.zxy);
// x0 = x0 - 0.0 + 0.0 * C.xxx;
// x1 = x0 - i1 + 1.0 * C.xxx;
// x2 = x0 - i2 + 2.0 * C.xxx;
// x3 = x0 - 1.0 + 3.0 * C.xxx;
vec3 x1=x0-i1+C.xxx;
vec3 x2=x0-i2+C.yyy;// 2.0*C.x = 1/3 = C.y
vec3 x3=x0-D.yyy;// -1.0+3.0*C.x = -0.5 = -D.y
// Permutations
i=mod289(i);
vec4 p=permute(permute(permute(
i.z+vec4(0.,i1.z,i2.z,1.))
+i.y+vec4(0.,i1.y,i2.y,1.))
+i.x+vec4(0.,i1.x,i2.x,1.));
// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float n_=.142857142857;// 1.0/7.0
vec3 ns=n_*D.wyz-D.xzx;
vec4 j=p-49.*floor(p*ns.z*ns.z);// mod(p,7*7)
vec4 x_=floor(j*ns.z);
vec4 y_=floor(j-7.*x_);// mod(j,N)
vec4 x=x_*ns.x+ns.yyyy;
vec4 y=y_*ns.x+ns.yyyy;
vec4 h=1.-abs(x)-abs(y);
vec4 b0=vec4(x.xy,y.xy);
vec4 b1=vec4(x.zw,y.zw);
//vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
//vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
vec4 s0=floor(b0)*2.+1.;
vec4 s1=floor(b1)*2.+1.;
vec4 sh=-step(h,vec4(0.));
vec4 a0=b0.xzyw+s0.xzyw*sh.xxyy;
vec4 a1=b1.xzyw+s1.xzyw*sh.zzww;
vec3 p0=vec3(a0.xy,h.x);
vec3 p1=vec3(a0.zw,h.y);
vec3 p2=vec3(a1.xy,h.z);
vec3 p3=vec3(a1.zw,h.w);
//Normalise gradients
vec4 norm=taylorInvSqrt(vec4(dot(p0,p0),dot(p1,p1),dot(p2,p2),dot(p3,p3)));
p0*=norm.x;
p1*=norm.y;
p2*=norm.z;
p3*=norm.w;
// Mix final noise value
vec4 m=max(.6-vec4(dot(x0,x0),dot(x1,x1),dot(x2,x2),dot(x3,x3)),0.);
m=m*m;
return 42.*dot(m*m,vec4(dot(p0,x0),dot(p1,x1),
dot(p2,x2),dot(p3,x3)));
}
float fbm(vec3 p){
float value=0.;
float amplitude=1.;
// float frequency=1.;
float frequency=.025;
float lacunarity=2.;
float persistance=.5;
// float scale=1.;
float scale=.5;
int octaves=8;
for(int i=0;i<octaves;i++){
float noiseVal=snoise(p*frequency*scale);
value+=amplitude*noiseVal;
frequency*=lacunarity;
amplitude*=persistance;
}
return value;
}
// inspired from: https://www.shadertoy.com/view/fsjyR3
float paperNoise(vec2 uv){
float n1=fbm(vec3(uv.xy+vec2(1.,0.),0.));
float n2=fbm(vec3(uv.xy-vec2(1.,0.),0.));
float n=n1-n2;
return n;
}
void mainImage(out vec4 fragColor,in vec2 fragCoord){
vec2 uv=fragCoord/iResolution.xy;
vec3 col=vec3(.8);
float noise=paperNoise(uv*iResolution.xy)*.3;
col+=noise;
fragColor=vec4(col,1.);
} | mit | [
4014,
4082,
4108,
4108,
4295
] | [
[
392,
392,
412,
412,
452
],
[
454,
454,
474,
474,
514
],
[
516,
516,
537,
537,
574
],
[
576,
576,
604,
604,
653
],
[
655,
655,
677,
677,
3269
],
[
3295,
3295,
3313,
3313,
3988
],
[
4014,
4082,
4108,
4108,
4295
],
[
4321,
4321,
4374,
4374,
4603
]
] | // inspired from: https://www.shadertoy.com/view/fsjyR3
| float paperNoise(vec2 uv){ |
float n1=fbm(vec3(uv.xy+vec2(1.,0.),0.));
float n2=fbm(vec3(uv.xy-vec2(1.,0.),0.));
float n=n1-n2;
return n;
} | // inspired from: https://www.shadertoy.com/view/fsjyR3
float paperNoise(vec2 uv){ | 1 | 1 |
Dtf3Dl | iq | 2023-07-21T20:43:16 | // The MIT License
// Copyright © 2023 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 shader I used while designing the Human Shader experiment
//
// https://humanshader.com
int shift( int x, int n )
{
if( n==1 ) { return (x+5)/10; }
if( n==2 ) { return (x+50)/100; }
if( n==3 ) { return (x+500)/1000; }
return (x+5000)/10000;
}
// Cost: (6/14 MUL 6/13 ADD)
vec3 compute( int x, int y )
{
int R, B;
//-------------------------
// Section A (2 MUL, 3 ADD)
//-------------------------
int u = x-36;
int v = 18-y;
int u2 = u*u;
int v2 = v*v;
int h = u2 + v2;
//-------------------------
if( h < 200 )
{
//-------------------------------------
// Section B, Sphere (4/7 MUL, 5/9 ADD)
//-------------------------------------
R = 420;
B = 520;
int t = 5000 + h*8;
int p = shift(t*u,2);
int q = shift(t*v,2);
int s = 2*q;
// bounce light
int w = 18 + shift(p - s,2);
if( w>0 ) R += w*w;
// sky light / ambient occlusion
int o = s + 2200;
R = shift(R*o,4);
B = shift(B*o,4);
// sun/key light
if( p > -q )
{
int w = shift(p+q,1);
R += w;
B += w;
}
//-------------------------
}
else if( v<0 )
{
//-------------------------------------
// Section C, Ground (5/9 MUL, 6/9 ADD)
//-------------------------------------
R = 150 + 2*v;
B = 50;
int p = h + 8*v2;
int c = 240*(-v) - p;
// sky light / ambient occlusion
if( c>1200 )
{
int o = shift(6*c,1);
o = shift(c*(1500-o),2) - 8360;
R = shift(R*o,3);
B = shift(B*o,3);
}
// sun/key light with soft shadow
int r = c + u*v;
int d = 3200 - h - 2*r;
if( d>0 ) R += d;
//-------------------------
}
else
{
//------------------------------
// Section D, Sky (1 MUL, 2 ADD)
//------------------------------
int c = x + 4*y;
R = 132 + c;
B = 192 + c;
//-------------------------
}
//-------------------------
// Section E (3 MUL, 1 ADD)
//-------------------------
R = min(R,255);
B = min(B,255);
int G = shift(R*7 + 3*B,1);
//-------------------------
return vec3(R,G,B);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
const vec2 resolution = vec2(71,40);
ivec2 ij = ivec2(floor(resolution*fragCoord/iResolution.xy));
vec3 col = compute( ij.x, 39-ij.y );
// draw grid lines
{
ivec2 ijp = ivec2(floor(resolution*(fragCoord-vec2(1,1))/iResolution.xy));
ivec2 ijn = ivec2(floor(resolution*(fragCoord+vec2(1,1))/iResolution.xy));
if( ijn.x!=ij.x || ijn.y!=ij.y || ijp.x!=ij.x || ijp.y!=ij.y) col*=0.75;
}
fragColor = vec4( col/255.0, 1.0 );
} | mit | [
1359,
1389,
1419,
1419,
3543
] | [
[
1174,
1174,
1201,
1201,
1357
],
[
1359,
1389,
1419,
1419,
3543
],
[
3549,
3549,
3606,
3606,
4082
]
] | // Cost: (6/14 MUL 6/13 ADD)
| vec3 compute( int x, int y )
{ |
int R, B;
//-------------------------
// Section A (2 MUL, 3 ADD)
//-------------------------
int u = x-36;
int v = 18-y;
int u2 = u*u;
int v2 = v*v;
int h = u2 + v2;
//-------------------------
if( h < 200 )
{
//-------------------------------------
// Section B, Sphere (4/7 MUL, 5/9 ADD)
//-------------------------------------
R = 420;
B = 520;
int t = 5000 + h*8;
int p = shift(t*u,2);
int q = shift(t*v,2);
int s = 2*q;
// bounce light
int w = 18 + shift(p - s,2);
if( w>0 ) R += w*w;
// sky light / ambient occlusion
int o = s + 2200;
R = shift(R*o,4);
B = shift(B*o,4);
// sun/key light
if( p > -q )
{
int w = shift(p+q,1);
R += w;
B += w;
}
//-------------------------
}
else if( v<0 )
{
//-------------------------------------
// Section C, Ground (5/9 MUL, 6/9 ADD)
//-------------------------------------
R = 150 + 2*v;
B = 50;
int p = h + 8*v2;
int c = 240*(-v) - p;
// sky light / ambient occlusion
if( c>1200 )
{
int o = shift(6*c,1);
o = shift(c*(1500-o),2) - 8360;
R = shift(R*o,3);
B = shift(B*o,3);
}
// sun/key light with soft shadow
int r = c + u*v;
int d = 3200 - h - 2*r;
if( d>0 ) R += d;
//-------------------------
}
else
{
//------------------------------
// Section D, Sky (1 MUL, 2 ADD)
//------------------------------
int c = x + 4*y;
R = 132 + c;
B = 192 + c;
//-------------------------
}
//-------------------------
// Section E (3 MUL, 1 ADD)
//-------------------------
R = min(R,255);
B = min(B,255);
int G = shift(R*7 + 3*B,1);
//-------------------------
return vec3(R,G,B);
} | // Cost: (6/14 MUL 6/13 ADD)
vec3 compute( int x, int y )
{ | 1 | 1 |
ml2cWc | MattOstgard | 2023-08-18T18:09:43 | // The MIT License
// Copyright © 2016 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.
// Intersection of a ray and a capped cylinder oriented in an arbitrary direction
//
// List of ray-surface intersectors at https://www.shadertoy.com/playlist/l3dXRf
//
// and https://iquilezles.org/articles/intersectors
//
// Other cylinder functions:
// Cylinder intersection: https://www.shadertoy.com/view/4lcSRn
// Cylinder bounding box: https://www.shadertoy.com/view/MtcXRf
// Cylinder distance: https://www.shadertoy.com/view/wdXGDr
// Intersection of a ray and a capped cylinder oriented in an arbitrary direction.
//
// Arguments:
// - ro: The origin of the ray being cast.
// - rd: The direction of the ray being cast.
// - pa: One endpoint of the cylinder's axis.
// - pb: The other endpoint of the cylinder's axis.
// - ra: Cylinder's radius.
//
// Returns vec4:
// - x: -1 if no hit, otherwise the distance from the ray to the hit point.
// - yzw: Hit surface's normal.
vec4 iCylinder( in vec3 ro, in vec3 rd, in vec3 pa, in vec3 pb, float ra )
{
// Vector from point A to point B, defining the cylinder's main axis
vec3 ba = pb - pa;
// Vector from ray origin (ro) to point A on the cylinder
vec3 oc = ro - pa;
// Dot product of ba with itself, representing the squared length of the cylinder's axis
float baba = dot(ba,ba);
// Dot product of ba with ray direction (rd), representing alignment of ray with cylinder's axis
float bard = dot(ba,rd);
// Dot product of ba with oc, representing alignment of the cylinder's axis with the vector to the ray origin
float baoc = dot(ba,oc);
// Quadratic coefficients for solving ray-cylinder intersection
float k2 = baba - bard*bard;
float k1 = baba*dot(oc,rd) - baoc*bard;
float k0 = baba*dot(oc,oc) - baoc*baoc - ra*ra*baba;
/*
// In case you really need to handle parallel raycasts.
if( k2==0.0 )
{
// Handle the case where the ray is parallel to the cylinder's axis.
// If this special case is detected (i.e., k2 == 0.0), calculate intersections with the two endpoints of the
// cylinder and verify that the intersection is within the cylinder's radius. If so, it returns the intersection
// point and surface normal information. If not, it returns -1.0, indicating no intersection.
// Parameters for intersection with the two endpoints of the cylinder.
float ta = -dot(ro-pa,ba)/bard;
float tb = ta + baba/bard;
// Determining the intersection point based on ray direction.
vec4 pt = (bard>0.0) ? vec4(pa,-ta) : vec4(pb,tb);
// Calculating the offset from the intersection point to determine if the intersection is within the cylinder's radius.
vec3 q = ro + rd*abs(pt.w) - pt.xyz;
if( dot(q,q)>ra*ra ) return vec4(-1.0);
// Returning the intersection point and surface normal information.
return vec4( abs(pt.w), sign(pt.w)*ba/sqrt(baba) );
}
*/
// Discriminant of the quadratic equation; determines whether there is an intersection.
float h = k1*k1 - k2*k0;
if( h<0.0 ) return vec4(-1.0); // No intersection.
h = sqrt(h);
float t = (-k1-h)/k2; // The "t" value where the intersection occurs.
// Checking intersection with the body of the cylinder.
float y = baoc + t*bard;
if( y>0.0 && y<baba ) return vec4( t, (oc+t*rd - ba*y/baba)/ra );
// Checking intersection with the end caps of the cylinder.
t = ( ((y<0.0) ? 0.0 : baba) - baoc)/bard;
if( abs(k1+k2*t)<h ) return vec4( t, ba*sign(y)/sqrt(baba) );
return vec4(-1.0); // No intersection.
}
// Get the normal of a cylinder's body from a point on the surface.
//
// Arguments:
// - p: Point on the cylinder body.
// - 'a' and 'b': Points along the axis of the cylinder, defining its direction.
// - 'ra': The radius of the cylinder.
//
// Returns the cylinder body's normal.
//
vec3 nCylinder( in vec3 p, in vec3 a, in vec3 b, in float ra )
{
// 'pa' is the vector from point 'a' to the point 'p' where we want to find the normal.
vec3 pa = p - a;
// 'ba' is the vector from point 'a' to point 'b', defining the direction of the cylinder's axis.
vec3 ba = b - a;
// 'baba' is the squared length of vector 'ba', used to normalize 'ba'.
float baba = dot(ba,ba);
// 'paba' is the projection of 'pa' onto 'ba', giving us the portion of 'pa' that is parallel to the cylinder's axis.
float paba = dot(pa,ba);
// Subtracting the projected vector (ba * paba / baba) from 'pa' gives us the vector perpendicular to the axis,
// pointing towards the surface of the cylinder from 'p'. Dividing by 'ra' scales it to the normal direction.
return (pa - ba*paba/baba)/ra;
}
// Same as above, but specialized to the Y axis.
//
// Arguments:
// - ro: The origin of the ray being cast.
// - rd: The direction of the ray being cast.
// - he: Cylinder's height.
// - ra: Cylinder's radius.
//
// Returns vec4:
// - x: -1 if no hit, otherwise the distance from the ray to the hit point.
// - yzw: Hit surface's normal.
//
vec4 iCylinderVertical( in vec3 ro, in vec3 rd, float he, float ra )
{
// Quadratic coefficients for ray-cylinder intersection with a vertical axis.
float k2 = 1.0 - rd.y*rd.y;
float k1 = dot(ro,rd) - ro.y*rd.y;
float k0 = dot(ro,ro) - ro.y*ro.y - ra*ra;
// Discriminant of the quadratic equation; determines whether there is an intersection.
float h = k1*k1 - k2*k0;
if( h<0.0 ) return vec4(-1.0); // No intersection.
h = sqrt(h);
float t = (-k1-h)/k2;
// Checking intersection with the body of the cylinder.
float y = ro.y + t*rd.y;
if( y>-he && y<he ) return vec4( t, (ro + t*rd - vec3(0.0,y,0.0))/ra );
// Checking intersection with the end caps of the cylinder.
t = ( ((y<0.0)?-he:he) - ro.y)/rd.y;
if( abs(k1+k2*t)<h ) return vec4( t, vec3(0.0,sign(y),0.0) );
return vec4(-1.0); // No intersection
}
// Generate a pattern (grid-like) based on the given UV coordinates.
vec3 pattern( in vec2 uv )
{
vec3 col = vec3(0.6);
col += 0.4*smoothstep(-0.01,0.01,cos(uv.x*0.5)*cos(uv.y*0.5));
col *= smoothstep(-1.0,-0.98,cos(uv.x))*smoothstep(-1.0,-0.98,cos(uv.y));
return col;
}
#define AA 3
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Define camera rotation and position.
float an = 0.5*iTime;
vec3 ro = vec3( 1.0*cos(an), 0.4, 1.0*sin(an) );
vec3 ta = vec3( 0.0, 0.0, 0.0 );
// Create camera's orthonormal basis (u,v,w).
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
vec3 tot = vec3(0.0);
// Anti-aliasing loop (if AA > 1).
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
{
// Subpixel sampling.
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
// Create view ray.
vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );
// Define cylinder.
const vec3 capA = vec3(-0.3,-0.1,-0.1);
const vec3 capB = vec3(0.3,0.1,0.4);
const float capR = 0.2;
// Initialize background color.
vec3 col = vec3(0.08)*(1.0-0.3*length(p)) + 0.02*rd.y;
// Cylinder-ray intersection.
vec4 tnor = iCylinder( ro, rd, capA, capB, capR );
if( tnor.x>0.0 )
{
// Compute shading, texture, and lighting if intersection occurred.
// SHADING
// Calculating the position and normal at the intersection.
float t = tnor.x;
vec3 pos = ro + t*rd;
vec3 nor = tnor.yzw;
// LIGHTING
// Define the light direction and halfway vector.
vec3 lig = normalize(vec3(0.7,0.6,0.3));
vec3 hal = normalize(-rd+lig);
// Calculate the diffuse and ambient components using dot products.
float dif = clamp( dot(nor,lig), 0.0, 1.0 ); // Diffuse lighting
float amb = clamp( 0.5 + 0.5*dot(nor,vec3(0.0,1.0,0.0)), 0.0, 1.0 ); // Ambient lighting
float occ = 0.5 + 0.5*nor.y; // Occlusion factor, contributes to ambient shading
// TEXTURING
// Coordinate transformation for texture mapping on the cylinder.
vec3 w = normalize(capB-capA);
vec3 u = normalize(cross(w,vec3(0,0,1)));
vec3 v = normalize(cross(u,w) );
vec3 q = (pos-capA)*mat3(u,v,w);
col = pattern( vec2(12.0,64.0)*vec2(atan(q.y,q.x),q.z) ); // Apply texture pattern.
// Combine shading, texturing, and lighting
col *= vec3(0.2,0.3,0.4)*amb*occ + vec3(1.0,0.9,0.7)*dif; // Apply ambient and diffuse components.
col += 0.4*pow(clamp(dot(hal,nor),0.0,1.0),12.0)*dif; // Apply specular reflection.
}
col = sqrt( col );
// Accumulate color.
tot += col;
#if AA>1
}
// Average color for anti-aliasing.
tot /= float(AA*AA);
#endif
// Add dithering 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;
// Output final color.
fragColor = vec4( tot, 1.0 );
}
| mit | [
7134,
7203,
7231,
7231,
7421
] | [
[
1532,
2003,
2080,
2153,
4752
],
[
4755,
5053,
5117,
5209,
5883
],
[
5886,
6252,
6322,
6404,
7132
],
[
7134,
7203,
7231,
7231,
7421
]
] | // Generate a pattern (grid-like) based on the given UV coordinates.
| vec3 pattern( in vec2 uv )
{ |
vec3 col = vec3(0.6);
col += 0.4*smoothstep(-0.01,0.01,cos(uv.x*0.5)*cos(uv.y*0.5));
col *= smoothstep(-1.0,-0.98,cos(uv.x))*smoothstep(-1.0,-0.98,cos(uv.y));
return col;
} | // Generate a pattern (grid-like) based on the given UV coordinates.
vec3 pattern( in vec2 uv )
{ | 6 | 36 |
dt2yDK | iq | 2023-08-18T01:10:50 | // The MIT License
// Copyright © 2023 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.
// Other closest point distances: https://www.shadertoy.com/playlist/ff2BRD
// Closest point on segment
vec2 cloSegment( in vec2 p, in vec2 a, in vec2 b, in float th )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 c = a + h*ba;
return c + th*normalize(p-c);
}
/*
vec2 cloSegment( in vec2 p, in vec2 a, in vec2 b, in float th )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 q = pa-h*ba;
float d = length(q); // distance
vec2 g = q/d; // gradient
return p-(d-th)*g; // closest point
}
*/
// distance to segment
float sdSegment( in vec2 p, in vec2 a, in vec2 b, in float th )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 q = pa-h*ba;
return length(q) - th;
}
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;
float px = 2.0/iResolution.y;
if( iMouse.z<0.01 ) m = vec2(1.1,0.8)*cos(iTime*vec2(1.1,1.3)+vec2(0,2));
vec2 v1 = 0.7*cos( 0.5*iTime*vec2(1.3,1.0) + vec2(2,4) );
vec2 v2 = 0.7*cos( 0.5*iTime*vec2(0.9,1.2) + vec2(1,5) );
float th = 0.2*(0.7+0.3*sin(iTime*1.2+2.0));
vec3 col;
// background color
{
float d = sdSegment(p,v1,v2,th);
col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.65,0.85,1.0);
col *= 1.0 - exp2(-24.0*abs(d));
col *= 0.8 + 0.2*cos(120.0*abs(d));
col = mix( col, vec3(1.0), 1.0-smoothstep(-px,px,abs(d)-0.005) );
}
{
// distance from pointer
float d = sdSegment(m,v1,v2,th);
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));
}
// closest point
{
vec2 cl = cloSegment(m,v1,v2,th);
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-cl)-0.025));
}
fragColor = vec4(col,1.0);
}
| mit | [
1156,
1184,
1249,
1249,
1403
] | [
[
1156,
1184,
1249,
1249,
1403
],
[
1732,
1755,
1820,
1820,
1966
],
[
1968,
1968,
2025,
2061,
3243
]
] | // Closest point on segment
| vec2 cloSegment( in vec2 p, in vec2 a, in vec2 b, in float th )
{ |
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 c = a + h*ba;
return c + th*normalize(p-c);
} | // Closest point on segment
vec2 cloSegment( in vec2 p, in vec2 a, in vec2 b, in float th )
{ | 1 | 1 |
dt2yDK | iq | 2023-08-18T01:10:50 | // The MIT License
// Copyright © 2023 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.
// Other closest point distances: https://www.shadertoy.com/playlist/ff2BRD
// Closest point on segment
vec2 cloSegment( in vec2 p, in vec2 a, in vec2 b, in float th )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 c = a + h*ba;
return c + th*normalize(p-c);
}
/*
vec2 cloSegment( in vec2 p, in vec2 a, in vec2 b, in float th )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 q = pa-h*ba;
float d = length(q); // distance
vec2 g = q/d; // gradient
return p-(d-th)*g; // closest point
}
*/
// distance to segment
float sdSegment( in vec2 p, in vec2 a, in vec2 b, in float th )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 q = pa-h*ba;
return length(q) - th;
}
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;
float px = 2.0/iResolution.y;
if( iMouse.z<0.01 ) m = vec2(1.1,0.8)*cos(iTime*vec2(1.1,1.3)+vec2(0,2));
vec2 v1 = 0.7*cos( 0.5*iTime*vec2(1.3,1.0) + vec2(2,4) );
vec2 v2 = 0.7*cos( 0.5*iTime*vec2(0.9,1.2) + vec2(1,5) );
float th = 0.2*(0.7+0.3*sin(iTime*1.2+2.0));
vec3 col;
// background color
{
float d = sdSegment(p,v1,v2,th);
col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.65,0.85,1.0);
col *= 1.0 - exp2(-24.0*abs(d));
col *= 0.8 + 0.2*cos(120.0*abs(d));
col = mix( col, vec3(1.0), 1.0-smoothstep(-px,px,abs(d)-0.005) );
}
{
// distance from pointer
float d = sdSegment(m,v1,v2,th);
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));
}
// closest point
{
vec2 cl = cloSegment(m,v1,v2,th);
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-cl)-0.025));
}
fragColor = vec4(col,1.0);
}
| mit | [
1732,
1755,
1820,
1820,
1966
] | [
[
1156,
1184,
1249,
1249,
1403
],
[
1732,
1755,
1820,
1820,
1966
],
[
1968,
1968,
2025,
2061,
3243
]
] | // distance to segment
| float sdSegment( in vec2 p, in vec2 a, in vec2 b, in float th )
{ |
vec2 ba = b-a;
vec2 pa = p-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 q = pa-h*ba;
return length(q) - th;
} | // distance to segment
float sdSegment( in vec2 p, in vec2 a, in vec2 b, in float th )
{ | 1 | 1 |
DljcDG | iq | 2023-08-17T22:23:32 | // The MIT License
// Copyright © 2023 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.
// Other closest point distances: https://www.shadertoy.com/playlist/ff2BRD
// closest point on rounded box
vec2 cloRoundBox( in vec2 p, in vec2 b, float r )
{
vec2 s = vec2(p.x<0.0?-1:1,p.y<0.0?-1:1);
vec2 w = abs(p) - b;
if( max(w.x,w.y)>0.0 )
{
vec2 q = max(w,0.0);
float l = length(q);
p -= s*(l-r)*q/l;
}
else
{
if( w.x>w.y ) p.x = s.x*(b.x+r);
else p.y = s.y*(b.y+r);
}
return p;
}
// distance to rounded box
float sdRoundBox( in vec2 p, in vec2 b, float r )
{
vec2 w = abs(p)-b;
float g = max(w.x,w.y);
return ((g>0.0)?length(max(w,0.0)):g) - r;
}
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;
float px = 2.0/iResolution.y;
if( iMouse.z<0.01 ) m = vec2(1.2,0.8)*cos(iTime*vec2(1.1,1.3)+vec2(0,2));
const vec2 bs = vec2(0.7,0.3);
const float ra = 0.2;
vec3 col;
// background color
{
float d = sdRoundBox(p,bs,ra);
col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.65,0.85,1.0);
col *= 1.0 - exp2(-24.0*abs(d));
col *= 0.8 + 0.2*cos(120.0*abs(d));
col = mix( col, vec3(1.0), 1.0-smoothstep(-px,px,abs(d)-0.005) );
}
{
// distance from pointer
float d = sdRoundBox(m,bs,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));
}
// closest point
{
vec2 cl = cloRoundBox(m,bs,ra);
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-cl)-0.025));
}
fragColor = vec4(col,1.0);
} | mit | [
1157,
1189,
1240,
1240,
1554
] | [
[
1157,
1189,
1240,
1240,
1554
],
[
1556,
1583,
1634,
1634,
1735
],
[
1737,
1737,
1794,
1830,
2895
]
] | // closest point on rounded box
| vec2 cloRoundBox( in vec2 p, in vec2 b, float r )
{ |
vec2 s = vec2(p.x<0.0?-1:1,p.y<0.0?-1:1);
vec2 w = abs(p) - b;
if( max(w.x,w.y)>0.0 )
{
vec2 q = max(w,0.0);
float l = length(q);
p -= s*(l-r)*q/l;
}
else
{
if( w.x>w.y ) p.x = s.x*(b.x+r);
else p.y = s.y*(b.y+r);
}
return p;
} | // closest point on rounded box
vec2 cloRoundBox( in vec2 p, in vec2 b, float r )
{ | 1 | 1 |
DljcDG | iq | 2023-08-17T22:23:32 | // The MIT License
// Copyright © 2023 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.
// Other closest point distances: https://www.shadertoy.com/playlist/ff2BRD
// closest point on rounded box
vec2 cloRoundBox( in vec2 p, in vec2 b, float r )
{
vec2 s = vec2(p.x<0.0?-1:1,p.y<0.0?-1:1);
vec2 w = abs(p) - b;
if( max(w.x,w.y)>0.0 )
{
vec2 q = max(w,0.0);
float l = length(q);
p -= s*(l-r)*q/l;
}
else
{
if( w.x>w.y ) p.x = s.x*(b.x+r);
else p.y = s.y*(b.y+r);
}
return p;
}
// distance to rounded box
float sdRoundBox( in vec2 p, in vec2 b, float r )
{
vec2 w = abs(p)-b;
float g = max(w.x,w.y);
return ((g>0.0)?length(max(w,0.0)):g) - r;
}
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;
float px = 2.0/iResolution.y;
if( iMouse.z<0.01 ) m = vec2(1.2,0.8)*cos(iTime*vec2(1.1,1.3)+vec2(0,2));
const vec2 bs = vec2(0.7,0.3);
const float ra = 0.2;
vec3 col;
// background color
{
float d = sdRoundBox(p,bs,ra);
col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.65,0.85,1.0);
col *= 1.0 - exp2(-24.0*abs(d));
col *= 0.8 + 0.2*cos(120.0*abs(d));
col = mix( col, vec3(1.0), 1.0-smoothstep(-px,px,abs(d)-0.005) );
}
{
// distance from pointer
float d = sdRoundBox(m,bs,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));
}
// closest point
{
vec2 cl = cloRoundBox(m,bs,ra);
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-cl)-0.025));
}
fragColor = vec4(col,1.0);
} | mit | [
1556,
1583,
1634,
1634,
1735
] | [
[
1157,
1189,
1240,
1240,
1554
],
[
1556,
1583,
1634,
1634,
1735
],
[
1737,
1737,
1794,
1830,
2895
]
] | // distance to rounded box
| float sdRoundBox( in vec2 p, in vec2 b, float r )
{ |
vec2 w = abs(p)-b;
float g = max(w.x,w.y);
return ((g>0.0)?length(max(w,0.0)):g) - r;
} | // distance to rounded box
float sdRoundBox( in vec2 p, in vec2 b, float r )
{ | 1 | 1 |
cldSzf | iq | 2023-08-17T22:23:20 | // The MIT License
// Copyright © 2023 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 box
//
// For points inside the box where 3 or 4 closest points exist (p.y=0 or p.x=0) or
// where 2 solutions exit (the diagonals |p.x-p.y|=|b.x-b.y|, you need to take
// extra precautions, for example:
//
// if( g<0.0 )
// {
// if( w.x==w.y ) return vec2(1e20); // 2 solutions
// if( (b.x>b.y) && abs(p.x)<=(b.x-b.y) && abs(p.y)==0.0 ) return vec2(1e20); // 3 solutions
// if( (b.x<b.y) && abs(p.y)<=(b.y-b.x) && abs(p.x)==0.0 ) return vec2(1e20); // 3 solutions
// }
//
// Other closest point distances: https://www.shadertoy.com/playlist/ff2BRD
vec2 cloBox( in vec2 p, in vec2 b )
{
vec2 s = sign(p);
vec2 w = abs(p) - b;
float g = max(w.x,w.y);
float m = min(0.0,g);
return p - vec2(w.x>=m?w.x:0.0,w.y>=m?w.y:0.0)*s;
}
// distance to box
float sdBox( in vec2 p, in vec2 b )
{
vec2 w = abs(p)-b;
float g = max(w.x,w.y);
return (g>0.0)?length(max(w,0.0)):g;
}
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;
float px = 2.0/iResolution.y;
if( iMouse.z<0.01 ) m = vec2(1.2,0.8)*cos(iTime*vec2(1.1,1.3)+vec2(0,2));
vec2 b1 = vec2(0.7,0.5);
vec3 col;
// background color
{
float d = sdBox(p,b1);
col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.65,0.85,1.0);
col *= 1.0 - exp2(-24.0*abs(d));
col *= 0.8 + 0.2*cos(120.0*abs(d));
col = mix( col, vec3(1.0), 1.0-smoothstep(-px,px,abs(d)-0.005) );
}
{
// distance from pointer
float d = sdBox(m,b1);
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));
}
// closest point
{
vec2 cl = cloBox(m,b1);
col = mix(col, vec3(1.0,0.0,0.0), 1.0-smoothstep(0.0, 0.005, length(p-cl)-0.025));
}
fragColor = vec4(col,1.0);
}
| mit | [
1917,
1936,
1973,
1973,
2067
] | [
[
1714,
1714,
1751,
1751,
1914
],
[
1917,
1936,
1973,
1973,
2067
],
[
2069,
2069,
2126,
2162,
3174
]
] | // distance to box
| float sdBox( in vec2 p, in vec2 b )
{ |
vec2 w = abs(p)-b;
float g = max(w.x,w.y);
return (g>0.0)?length(max(w,0.0)):g;
} | // distance to box
float sdBox( in vec2 p, in vec2 b )
{ | 1 | 145 |
Dt2yRV | iY0Yi | 2023-08-14T14:32:21 | // Hash without Sine
// MIT License...
// Copyright (c)2014 David Hoskins.
//https://www.shadertoy.com/view/4djSRW
float hash12(vec2 p){
vec3 p3 = fract(vec3(p.xyx) * .1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
int getDigits(float f, int digc, int index) {
// Note: For 32-bit floats, the precision is typically up to about 7 decimal places.
// Exceeding this may lead to accuracy issues.
// 1. Multiply the float by a power of 10 to shift the desired digits to the integer part.
// This depends on both the desired number of digits (dig) and the offset (id).
f = f * pow(10., float(digc + index));
// 2. Convert the float to an integer to discard any fractional part.
// 3. Return the last 'dig' digits of the integer.
return int(f) % int(pow(10., float(digc)));
}
bool inRange(float v, float min, float max) {
return v > min && v < max;
}
bool inRange(int v, int min, int max) {
return v > min && v < max;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord ){
fragCoord=floor(fragCoord/8.+(iTime*8.));
float f = hash12(fragCoord);
const int ans0 = 123;
const int ans1 = 456;
const int ans2 = 789;
int digt = getDigits(f, 3, 1);
if(ans0==digt)
fragColor = vec4(1,0,0,1);
else
if(ans1==digt)
fragColor = vec4(0,1,0,1);
else
if(ans2==digt)
fragColor = vec4(0,0,1,1);
else
if(inRange(digt, ans2-5, ans2+5))
fragColor = vec4(1,1,0,1);
else
fragColor = vec4(f*.0125);
fragColor = pow(fragColor,vec4(.4545));
} | mit | [
0,
115,
136,
136,
253
] | [
[
0,
115,
136,
136,
253
],
[
255,
255,
300,
622,
844
],
[
846,
846,
891,
891,
924
],
[
926,
926,
965,
965,
998
],
[
1000,
1000,
1056,
1056,
1621
]
] | // Hash without Sine
// MIT License...
// Copyright (c)2014 David Hoskins.
//https://www.shadertoy.com/view/4djSRW
| float hash12(vec2 p){ |
vec3 p3 = fract(vec3(p.xyx) * .1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
} | // Hash without Sine
// MIT License...
// Copyright (c)2014 David Hoskins.
//https://www.shadertoy.com/view/4djSRW
float hash12(vec2 p){ | 12 | 19 |
msKyDR | uctumi | 2023-09-30T21:00:47 | /*
* Copyright 2014 Roman Bobniev (FatumR)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define OCTAVES 8.0
#define LIVE_SMOKE 1
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
float rand2(vec2 co){
return fract(cos(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
// Rough Value noise implementation
float valueNoiseSimple(vec2 vl) {
float minStep = 1.0 ;
vec2 grid = floor(vl);
vec2 gridPnt1 = grid;
vec2 gridPnt2 = vec2(grid.x, grid.y + minStep);
vec2 gridPnt3 = vec2(grid.x + minStep, grid.y);
vec2 gridPnt4 = vec2(gridPnt3.x, gridPnt2.y);
float s = rand2(grid);
float t = rand2(gridPnt3);
float u = rand2(gridPnt2);
float v = rand2(gridPnt4);
float x1 = smoothstep(0., 1., fract(vl.x));
float interpX1 = mix(s, t, x1);
float interpX2 = mix(u, v, x1);
float y = smoothstep(0., 1., fract(vl.y));
float interpY = mix(interpX1, interpX2, y);
return interpY;
}
float getLowFreqs()
{
const int NUM_FREQS = 32;
/* Close to the spectrum of the voice frequencies for this song. */
const float lowStart = 0.65;
const float lowEnd = 0.75;
float result = 0.0;
for (int i = 0; i < NUM_FREQS; i++)
{
result += texture(iChannel0,
vec2(lowStart + (lowEnd - lowStart)*float(i)/float(NUM_FREQS - 1),
0.25)).x;
}
return smoothstep(0.0, 1.0, (result / float(NUM_FREQS)) * 2.);
}
float fractalNoise(vec2 vl) {
float persistance = 2.0;
float amplitude = 0.5;
float rez = 0.0;
vec2 p = vl;
for (float i = 0.0; i < OCTAVES; i++) {
rez += amplitude * valueNoiseSimple(p);
amplitude /= persistance;
p *= persistance;
}
return rez;
}
float complexFBM(vec2 p) {
float sound = getLowFreqs();
float slow = iTime / 2.5;
float fast = iTime / .5;
vec2 offset1 = vec2(slow , 0.); // Main front
vec2 offset2 = vec2(sin(fast )* 0.1, 0.); // sub fronts
return
#if LIVE_SMOKE
(1. + sound) *
#endif
fractalNoise( p + offset1 + fractalNoise(
p + fractalNoise(
p + 2. * fractalNoise(p - offset2)
)
)
);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord.xy / iResolution.xy;
vec3 blueColor = vec3(0.07,0.2,.5);
vec3 orangeColor2 = vec3(0, 0, 0);
vec3 rez = mix(orangeColor2, blueColor, complexFBM(uv));
fragColor = vec4(rez, 1.0);
} | apache-2.0 | [
841,
877,
910,
910,
1511
] | [
[
654,
654,
674,
674,
745
],
[
747,
747,
768,
768,
839
],
[
841,
877,
910,
910,
1511
],
[
1513,
1513,
1534,
1534,
2026
],
[
2028,
2028,
2057,
2057,
2332
],
[
2797,
2797,
2854,
2854,
3092
]
] | // Rough Value noise implementation
| float valueNoiseSimple(vec2 vl) { |
float minStep = 1.0 ;
vec2 grid = floor(vl);
vec2 gridPnt1 = grid;
vec2 gridPnt2 = vec2(grid.x, grid.y + minStep);
vec2 gridPnt3 = vec2(grid.x + minStep, grid.y);
vec2 gridPnt4 = vec2(gridPnt3.x, gridPnt2.y);
float s = rand2(grid);
float t = rand2(gridPnt3);
float u = rand2(gridPnt2);
float v = rand2(gridPnt4);
float x1 = smoothstep(0., 1., fract(vl.x));
float interpX1 = mix(s, t, x1);
float interpX2 = mix(u, v, x1);
float y = smoothstep(0., 1., fract(vl.y));
float interpY = mix(interpX1, interpX2, y);
return interpY;
} | // Rough Value noise implementation
float valueNoiseSimple(vec2 vl) { | 3 | 5 |
msdBWX | virmoesiae | 2023-10-19T16:13:27 | /*
AD BY VIRMODOETIAE, a.k.a VIRMOESIAE ---------------------------
Do you like shaders? Would you like to toy with them offline?
Would you like an interactive UI to play around with your shader
variables/uniforms in real-time without compilation? Would you
like to have a layer-based shader blending, without having to
pass through obscure buffers all the time? Would like to export
your shaders directly as GIFs?
Would you like a stand-alone executable (currently for Windows-
only, but the source code is cross-platform) in less than 9MB?
Then, my dear friend, come get your free copy of:
>>> SHADERTHING <<<
a live, offline, GUI-based shader editor developed by me,
virmodoetiae (a.k.a, virmoesiae) freely obtainable at:
https://github.com/virmodoetiae/shaderthing
For the release, head to :
https://github.com/virmodoetiae/shaderthing/releases
Everything, including the source code, is available under a
very permissive libz/libpng license, so you can really do
almost anything you want with it!
Please note that currently no tutorials are available, but the
core usage should be intuitive to most ShaderToy users.
Enjoy!
*/
// Swap with other noise implementations to check for differences
// (best seen when VIEW is set to fbm). The novel implementations
// are triValueNoise and triGradNoise
#define NOISE triValueNoise
//#define NOISE triGradNoise
//#define NOISE quadValueNoise
//#define NOISE quadGradNoise
#define VIEW pattern
//#define VIEW fbm
// My take on the pseudo-random number thing
float random(vec2 x)
{
return fract(138912.*sin(dot(x, vec2(138.9, 191.2))));
}
// From iq
vec2 random2(vec2 st){
st = vec2( dot(st,vec2(127.1,311.7)),
dot(st,vec2(269.5,183.3)) );
return -1.0 + 2.0*fract(sin(st)*43758.5453123);
}
// Your average 4-point value noise implementation
float quadValueNoise(vec2 n)
{
const vec2 d = vec2(0.0, 1.0);
vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
return
mix
(
mix(random(b), random(b + d.yx), f.x),
mix(random(b + d.xy), random(b + d.yy), f.x),
f.y
);
}
// Your average 4-point, Perlin-like noise implementation
float quadGradNoise(vec2 n)
{
const vec2 d = vec2(0.0, 1.0);
vec2 r = fract(n);
vec2 l = floor(n);
vec2 f = r*r*(3.0-2.0*r);
return
.5+.5*mix
(
mix
(
dot(random2(l), r),
dot(random2(l + d.yx), r-d.yx),
f.x
),
mix
(
dot(random2(l + d.xy), r-d.xy),
dot(random2(l + d.yy), r-d.yy),
f.x
),
f.y
);
}
// A 2D noise implementation I came up with that requires
// one less call to the pseudo-random number generator and
// one less mixing. Easily extandable to 3D
float triValueNoise(vec2 x)
{
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = random(l+s*e.yx);
float b = random(l+s*e.yx+e.xy);
float c = random(l+s*e.xy+(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
r *= r*(3.-2.*r); // Same cubic profile as smoothstep
return mix(mix(a, b, r.x), c, r.y);
}
// Same as before but using gradients, much smoother
float triGradNoise(vec2 x)
{
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = dot(random2(l+s*e.yx), r-s*e.yx);
float b = dot(random2(l+s*e.yx+e.xy), r-s*e.yx-e.xy);
float c = dot(random2(l+s*e.xy+(1.-s)*e.yx), r-s*e.xy-(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
// Quintic profile to have null second derivate at
// boundaries
r *= 6.*r*r*r*r-15.*r*r*r+10.*r*r;
return .5+.5*mix(mix(a, b, r.x), c, r.y);
}
// Fractional Brownian Motion noise to test the
// single-octave noise function
float fbm(vec2 x)
{
float n = 0.;
float A = 0.;
vec2 af = vec2(1., 2.);
for (int i=0; i<4; i++)
{
// Rotate each octave
float s = sin(float(2*i));
float c = cos(float(2*i));
mat2 m = mat2(c, s, -s, c);
n += af.x*NOISE(af.y*m*x);
A += af.x;
af *= vec2(.4,2.);
}
return n/A;
}
// A warp-based pattern inspired by https://iquilezles.org/articles/warp/
float pattern(vec2 x)
{
vec2 a = vec2(fbm(x)+iTime/15., fbm(x+vec2(2.2,0.)));
float b = fbm(x+a+iTime/10.);
vec2 c = vec2(fbm(x+b), fbm(a-vec2(1.7,0.)));
return fbm(x+.5*c);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord/iResolution.y;
vec2 d = 3.*vec2(2.+cos(iTime/10.), 3.+sin(iTime/10.));
fragColor = vec4(vec3(VIEW(2.*uv+d)), 1.);
} | libpng | [
1611,
1656,
1678,
1678,
1739
] | [
[
1611,
1656,
1678,
1678,
1739
],
[
1741,
1752,
1774,
1774,
1913
],
[
1915,
1966,
1997,
1997,
2278
],
[
2280,
2338,
2368,
2368,
2865
],
[
2867,
3029,
3058,
3058,
3481
],
[
3483,
3536,
3564,
3564,
4114
],
[
4116,
4197,
4216,
4216,
4555
],
[
4557,
4631,
4654,
4654,
4822
],
[
4824,
4824,
4881,
4881,
5029
]
] | // My take on the pseudo-random number thing
| float random(vec2 x)
{ |
return fract(138912.*sin(dot(x, vec2(138.9, 191.2))));
} | // My take on the pseudo-random number thing
float random(vec2 x)
{ | 1 | 1 |
msdBWX | virmoesiae | 2023-10-19T16:13:27 | /*
AD BY VIRMODOETIAE, a.k.a VIRMOESIAE ---------------------------
Do you like shaders? Would you like to toy with them offline?
Would you like an interactive UI to play around with your shader
variables/uniforms in real-time without compilation? Would you
like to have a layer-based shader blending, without having to
pass through obscure buffers all the time? Would like to export
your shaders directly as GIFs?
Would you like a stand-alone executable (currently for Windows-
only, but the source code is cross-platform) in less than 9MB?
Then, my dear friend, come get your free copy of:
>>> SHADERTHING <<<
a live, offline, GUI-based shader editor developed by me,
virmodoetiae (a.k.a, virmoesiae) freely obtainable at:
https://github.com/virmodoetiae/shaderthing
For the release, head to :
https://github.com/virmodoetiae/shaderthing/releases
Everything, including the source code, is available under a
very permissive libz/libpng license, so you can really do
almost anything you want with it!
Please note that currently no tutorials are available, but the
core usage should be intuitive to most ShaderToy users.
Enjoy!
*/
// Swap with other noise implementations to check for differences
// (best seen when VIEW is set to fbm). The novel implementations
// are triValueNoise and triGradNoise
#define NOISE triValueNoise
//#define NOISE triGradNoise
//#define NOISE quadValueNoise
//#define NOISE quadGradNoise
#define VIEW pattern
//#define VIEW fbm
// My take on the pseudo-random number thing
float random(vec2 x)
{
return fract(138912.*sin(dot(x, vec2(138.9, 191.2))));
}
// From iq
vec2 random2(vec2 st){
st = vec2( dot(st,vec2(127.1,311.7)),
dot(st,vec2(269.5,183.3)) );
return -1.0 + 2.0*fract(sin(st)*43758.5453123);
}
// Your average 4-point value noise implementation
float quadValueNoise(vec2 n)
{
const vec2 d = vec2(0.0, 1.0);
vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
return
mix
(
mix(random(b), random(b + d.yx), f.x),
mix(random(b + d.xy), random(b + d.yy), f.x),
f.y
);
}
// Your average 4-point, Perlin-like noise implementation
float quadGradNoise(vec2 n)
{
const vec2 d = vec2(0.0, 1.0);
vec2 r = fract(n);
vec2 l = floor(n);
vec2 f = r*r*(3.0-2.0*r);
return
.5+.5*mix
(
mix
(
dot(random2(l), r),
dot(random2(l + d.yx), r-d.yx),
f.x
),
mix
(
dot(random2(l + d.xy), r-d.xy),
dot(random2(l + d.yy), r-d.yy),
f.x
),
f.y
);
}
// A 2D noise implementation I came up with that requires
// one less call to the pseudo-random number generator and
// one less mixing. Easily extandable to 3D
float triValueNoise(vec2 x)
{
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = random(l+s*e.yx);
float b = random(l+s*e.yx+e.xy);
float c = random(l+s*e.xy+(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
r *= r*(3.-2.*r); // Same cubic profile as smoothstep
return mix(mix(a, b, r.x), c, r.y);
}
// Same as before but using gradients, much smoother
float triGradNoise(vec2 x)
{
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = dot(random2(l+s*e.yx), r-s*e.yx);
float b = dot(random2(l+s*e.yx+e.xy), r-s*e.yx-e.xy);
float c = dot(random2(l+s*e.xy+(1.-s)*e.yx), r-s*e.xy-(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
// Quintic profile to have null second derivate at
// boundaries
r *= 6.*r*r*r*r-15.*r*r*r+10.*r*r;
return .5+.5*mix(mix(a, b, r.x), c, r.y);
}
// Fractional Brownian Motion noise to test the
// single-octave noise function
float fbm(vec2 x)
{
float n = 0.;
float A = 0.;
vec2 af = vec2(1., 2.);
for (int i=0; i<4; i++)
{
// Rotate each octave
float s = sin(float(2*i));
float c = cos(float(2*i));
mat2 m = mat2(c, s, -s, c);
n += af.x*NOISE(af.y*m*x);
A += af.x;
af *= vec2(.4,2.);
}
return n/A;
}
// A warp-based pattern inspired by https://iquilezles.org/articles/warp/
float pattern(vec2 x)
{
vec2 a = vec2(fbm(x)+iTime/15., fbm(x+vec2(2.2,0.)));
float b = fbm(x+a+iTime/10.);
vec2 c = vec2(fbm(x+b), fbm(a-vec2(1.7,0.)));
return fbm(x+.5*c);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord/iResolution.y;
vec2 d = 3.*vec2(2.+cos(iTime/10.), 3.+sin(iTime/10.));
fragColor = vec4(vec3(VIEW(2.*uv+d)), 1.);
} | libpng | [
1741,
1752,
1774,
1774,
1913
] | [
[
1611,
1656,
1678,
1678,
1739
],
[
1741,
1752,
1774,
1774,
1913
],
[
1915,
1966,
1997,
1997,
2278
],
[
2280,
2338,
2368,
2368,
2865
],
[
2867,
3029,
3058,
3058,
3481
],
[
3483,
3536,
3564,
3564,
4114
],
[
4116,
4197,
4216,
4216,
4555
],
[
4557,
4631,
4654,
4654,
4822
],
[
4824,
4824,
4881,
4881,
5029
]
] | // From iq
| vec2 random2(vec2 st){ |
st = vec2( dot(st,vec2(127.1,311.7)),
dot(st,vec2(269.5,183.3)) );
return -1.0 + 2.0*fract(sin(st)*43758.5453123);
} | // From iq
vec2 random2(vec2 st){ | 39 | 56 |
msdBWX | virmoesiae | 2023-10-19T16:13:27 | /*
AD BY VIRMODOETIAE, a.k.a VIRMOESIAE ---------------------------
Do you like shaders? Would you like to toy with them offline?
Would you like an interactive UI to play around with your shader
variables/uniforms in real-time without compilation? Would you
like to have a layer-based shader blending, without having to
pass through obscure buffers all the time? Would like to export
your shaders directly as GIFs?
Would you like a stand-alone executable (currently for Windows-
only, but the source code is cross-platform) in less than 9MB?
Then, my dear friend, come get your free copy of:
>>> SHADERTHING <<<
a live, offline, GUI-based shader editor developed by me,
virmodoetiae (a.k.a, virmoesiae) freely obtainable at:
https://github.com/virmodoetiae/shaderthing
For the release, head to :
https://github.com/virmodoetiae/shaderthing/releases
Everything, including the source code, is available under a
very permissive libz/libpng license, so you can really do
almost anything you want with it!
Please note that currently no tutorials are available, but the
core usage should be intuitive to most ShaderToy users.
Enjoy!
*/
// Swap with other noise implementations to check for differences
// (best seen when VIEW is set to fbm). The novel implementations
// are triValueNoise and triGradNoise
#define NOISE triValueNoise
//#define NOISE triGradNoise
//#define NOISE quadValueNoise
//#define NOISE quadGradNoise
#define VIEW pattern
//#define VIEW fbm
// My take on the pseudo-random number thing
float random(vec2 x)
{
return fract(138912.*sin(dot(x, vec2(138.9, 191.2))));
}
// From iq
vec2 random2(vec2 st){
st = vec2( dot(st,vec2(127.1,311.7)),
dot(st,vec2(269.5,183.3)) );
return -1.0 + 2.0*fract(sin(st)*43758.5453123);
}
// Your average 4-point value noise implementation
float quadValueNoise(vec2 n)
{
const vec2 d = vec2(0.0, 1.0);
vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
return
mix
(
mix(random(b), random(b + d.yx), f.x),
mix(random(b + d.xy), random(b + d.yy), f.x),
f.y
);
}
// Your average 4-point, Perlin-like noise implementation
float quadGradNoise(vec2 n)
{
const vec2 d = vec2(0.0, 1.0);
vec2 r = fract(n);
vec2 l = floor(n);
vec2 f = r*r*(3.0-2.0*r);
return
.5+.5*mix
(
mix
(
dot(random2(l), r),
dot(random2(l + d.yx), r-d.yx),
f.x
),
mix
(
dot(random2(l + d.xy), r-d.xy),
dot(random2(l + d.yy), r-d.yy),
f.x
),
f.y
);
}
// A 2D noise implementation I came up with that requires
// one less call to the pseudo-random number generator and
// one less mixing. Easily extandable to 3D
float triValueNoise(vec2 x)
{
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = random(l+s*e.yx);
float b = random(l+s*e.yx+e.xy);
float c = random(l+s*e.xy+(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
r *= r*(3.-2.*r); // Same cubic profile as smoothstep
return mix(mix(a, b, r.x), c, r.y);
}
// Same as before but using gradients, much smoother
float triGradNoise(vec2 x)
{
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = dot(random2(l+s*e.yx), r-s*e.yx);
float b = dot(random2(l+s*e.yx+e.xy), r-s*e.yx-e.xy);
float c = dot(random2(l+s*e.xy+(1.-s)*e.yx), r-s*e.xy-(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
// Quintic profile to have null second derivate at
// boundaries
r *= 6.*r*r*r*r-15.*r*r*r+10.*r*r;
return .5+.5*mix(mix(a, b, r.x), c, r.y);
}
// Fractional Brownian Motion noise to test the
// single-octave noise function
float fbm(vec2 x)
{
float n = 0.;
float A = 0.;
vec2 af = vec2(1., 2.);
for (int i=0; i<4; i++)
{
// Rotate each octave
float s = sin(float(2*i));
float c = cos(float(2*i));
mat2 m = mat2(c, s, -s, c);
n += af.x*NOISE(af.y*m*x);
A += af.x;
af *= vec2(.4,2.);
}
return n/A;
}
// A warp-based pattern inspired by https://iquilezles.org/articles/warp/
float pattern(vec2 x)
{
vec2 a = vec2(fbm(x)+iTime/15., fbm(x+vec2(2.2,0.)));
float b = fbm(x+a+iTime/10.);
vec2 c = vec2(fbm(x+b), fbm(a-vec2(1.7,0.)));
return fbm(x+.5*c);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord/iResolution.y;
vec2 d = 3.*vec2(2.+cos(iTime/10.), 3.+sin(iTime/10.));
fragColor = vec4(vec3(VIEW(2.*uv+d)), 1.);
} | libpng | [
2867,
3029,
3058,
3058,
3481
] | [
[
1611,
1656,
1678,
1678,
1739
],
[
1741,
1752,
1774,
1774,
1913
],
[
1915,
1966,
1997,
1997,
2278
],
[
2280,
2338,
2368,
2368,
2865
],
[
2867,
3029,
3058,
3058,
3481
],
[
3483,
3536,
3564,
3564,
4114
],
[
4116,
4197,
4216,
4216,
4555
],
[
4557,
4631,
4654,
4654,
4822
],
[
4824,
4824,
4881,
4881,
5029
]
] | // A 2D noise implementation I came up with that requires
// one less call to the pseudo-random number generator and
// one less mixing. Easily extandable to 3D
| float triValueNoise(vec2 x)
{ |
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = random(l+s*e.yx);
float b = random(l+s*e.yx+e.xy);
float c = random(l+s*e.xy+(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
r *= r*(3.-2.*r); // Same cubic profile as smoothstep
return mix(mix(a, b, r.x), c, r.y);
} | // A 2D noise implementation I came up with that requires
// one less call to the pseudo-random number generator and
// one less mixing. Easily extandable to 3D
float triValueNoise(vec2 x)
{ | 1 | 1 |
msdBWX | virmoesiae | 2023-10-19T16:13:27 | /*
AD BY VIRMODOETIAE, a.k.a VIRMOESIAE ---------------------------
Do you like shaders? Would you like to toy with them offline?
Would you like an interactive UI to play around with your shader
variables/uniforms in real-time without compilation? Would you
like to have a layer-based shader blending, without having to
pass through obscure buffers all the time? Would like to export
your shaders directly as GIFs?
Would you like a stand-alone executable (currently for Windows-
only, but the source code is cross-platform) in less than 9MB?
Then, my dear friend, come get your free copy of:
>>> SHADERTHING <<<
a live, offline, GUI-based shader editor developed by me,
virmodoetiae (a.k.a, virmoesiae) freely obtainable at:
https://github.com/virmodoetiae/shaderthing
For the release, head to :
https://github.com/virmodoetiae/shaderthing/releases
Everything, including the source code, is available under a
very permissive libz/libpng license, so you can really do
almost anything you want with it!
Please note that currently no tutorials are available, but the
core usage should be intuitive to most ShaderToy users.
Enjoy!
*/
// Swap with other noise implementations to check for differences
// (best seen when VIEW is set to fbm). The novel implementations
// are triValueNoise and triGradNoise
#define NOISE triValueNoise
//#define NOISE triGradNoise
//#define NOISE quadValueNoise
//#define NOISE quadGradNoise
#define VIEW pattern
//#define VIEW fbm
// My take on the pseudo-random number thing
float random(vec2 x)
{
return fract(138912.*sin(dot(x, vec2(138.9, 191.2))));
}
// From iq
vec2 random2(vec2 st){
st = vec2( dot(st,vec2(127.1,311.7)),
dot(st,vec2(269.5,183.3)) );
return -1.0 + 2.0*fract(sin(st)*43758.5453123);
}
// Your average 4-point value noise implementation
float quadValueNoise(vec2 n)
{
const vec2 d = vec2(0.0, 1.0);
vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
return
mix
(
mix(random(b), random(b + d.yx), f.x),
mix(random(b + d.xy), random(b + d.yy), f.x),
f.y
);
}
// Your average 4-point, Perlin-like noise implementation
float quadGradNoise(vec2 n)
{
const vec2 d = vec2(0.0, 1.0);
vec2 r = fract(n);
vec2 l = floor(n);
vec2 f = r*r*(3.0-2.0*r);
return
.5+.5*mix
(
mix
(
dot(random2(l), r),
dot(random2(l + d.yx), r-d.yx),
f.x
),
mix
(
dot(random2(l + d.xy), r-d.xy),
dot(random2(l + d.yy), r-d.yy),
f.x
),
f.y
);
}
// A 2D noise implementation I came up with that requires
// one less call to the pseudo-random number generator and
// one less mixing. Easily extandable to 3D
float triValueNoise(vec2 x)
{
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = random(l+s*e.yx);
float b = random(l+s*e.yx+e.xy);
float c = random(l+s*e.xy+(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
r *= r*(3.-2.*r); // Same cubic profile as smoothstep
return mix(mix(a, b, r.x), c, r.y);
}
// Same as before but using gradients, much smoother
float triGradNoise(vec2 x)
{
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = dot(random2(l+s*e.yx), r-s*e.yx);
float b = dot(random2(l+s*e.yx+e.xy), r-s*e.yx-e.xy);
float c = dot(random2(l+s*e.xy+(1.-s)*e.yx), r-s*e.xy-(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
// Quintic profile to have null second derivate at
// boundaries
r *= 6.*r*r*r*r-15.*r*r*r+10.*r*r;
return .5+.5*mix(mix(a, b, r.x), c, r.y);
}
// Fractional Brownian Motion noise to test the
// single-octave noise function
float fbm(vec2 x)
{
float n = 0.;
float A = 0.;
vec2 af = vec2(1., 2.);
for (int i=0; i<4; i++)
{
// Rotate each octave
float s = sin(float(2*i));
float c = cos(float(2*i));
mat2 m = mat2(c, s, -s, c);
n += af.x*NOISE(af.y*m*x);
A += af.x;
af *= vec2(.4,2.);
}
return n/A;
}
// A warp-based pattern inspired by https://iquilezles.org/articles/warp/
float pattern(vec2 x)
{
vec2 a = vec2(fbm(x)+iTime/15., fbm(x+vec2(2.2,0.)));
float b = fbm(x+a+iTime/10.);
vec2 c = vec2(fbm(x+b), fbm(a-vec2(1.7,0.)));
return fbm(x+.5*c);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord/iResolution.y;
vec2 d = 3.*vec2(2.+cos(iTime/10.), 3.+sin(iTime/10.));
fragColor = vec4(vec3(VIEW(2.*uv+d)), 1.);
} | libpng | [
4116,
4197,
4216,
4216,
4555
] | [
[
1611,
1656,
1678,
1678,
1739
],
[
1741,
1752,
1774,
1774,
1913
],
[
1915,
1966,
1997,
1997,
2278
],
[
2280,
2338,
2368,
2368,
2865
],
[
2867,
3029,
3058,
3058,
3481
],
[
3483,
3536,
3564,
3564,
4114
],
[
4116,
4197,
4216,
4216,
4555
],
[
4557,
4631,
4654,
4654,
4822
],
[
4824,
4824,
4881,
4881,
5029
]
] | // Fractional Brownian Motion noise to test the
// single-octave noise function
| float fbm(vec2 x)
{ |
float n = 0.;
float A = 0.;
vec2 af = vec2(1., 2.);
for (int i=0; i<4; i++)
{
// Rotate each octave
float s = sin(float(2*i));
float c = cos(float(2*i));
mat2 m = mat2(c, s, -s, c);
n += af.x*NOISE(af.y*m*x);
A += af.x;
af *= vec2(.4,2.);
}
return n/A;
} | // Fractional Brownian Motion noise to test the
// single-octave noise function
float fbm(vec2 x)
{ | 1 | 9 |
msdBWX | virmoesiae | 2023-10-19T16:13:27 | /*
AD BY VIRMODOETIAE, a.k.a VIRMOESIAE ---------------------------
Do you like shaders? Would you like to toy with them offline?
Would you like an interactive UI to play around with your shader
variables/uniforms in real-time without compilation? Would you
like to have a layer-based shader blending, without having to
pass through obscure buffers all the time? Would like to export
your shaders directly as GIFs?
Would you like a stand-alone executable (currently for Windows-
only, but the source code is cross-platform) in less than 9MB?
Then, my dear friend, come get your free copy of:
>>> SHADERTHING <<<
a live, offline, GUI-based shader editor developed by me,
virmodoetiae (a.k.a, virmoesiae) freely obtainable at:
https://github.com/virmodoetiae/shaderthing
For the release, head to :
https://github.com/virmodoetiae/shaderthing/releases
Everything, including the source code, is available under a
very permissive libz/libpng license, so you can really do
almost anything you want with it!
Please note that currently no tutorials are available, but the
core usage should be intuitive to most ShaderToy users.
Enjoy!
*/
// Swap with other noise implementations to check for differences
// (best seen when VIEW is set to fbm). The novel implementations
// are triValueNoise and triGradNoise
#define NOISE triValueNoise
//#define NOISE triGradNoise
//#define NOISE quadValueNoise
//#define NOISE quadGradNoise
#define VIEW pattern
//#define VIEW fbm
// My take on the pseudo-random number thing
float random(vec2 x)
{
return fract(138912.*sin(dot(x, vec2(138.9, 191.2))));
}
// From iq
vec2 random2(vec2 st){
st = vec2( dot(st,vec2(127.1,311.7)),
dot(st,vec2(269.5,183.3)) );
return -1.0 + 2.0*fract(sin(st)*43758.5453123);
}
// Your average 4-point value noise implementation
float quadValueNoise(vec2 n)
{
const vec2 d = vec2(0.0, 1.0);
vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
return
mix
(
mix(random(b), random(b + d.yx), f.x),
mix(random(b + d.xy), random(b + d.yy), f.x),
f.y
);
}
// Your average 4-point, Perlin-like noise implementation
float quadGradNoise(vec2 n)
{
const vec2 d = vec2(0.0, 1.0);
vec2 r = fract(n);
vec2 l = floor(n);
vec2 f = r*r*(3.0-2.0*r);
return
.5+.5*mix
(
mix
(
dot(random2(l), r),
dot(random2(l + d.yx), r-d.yx),
f.x
),
mix
(
dot(random2(l + d.xy), r-d.xy),
dot(random2(l + d.yy), r-d.yy),
f.x
),
f.y
);
}
// A 2D noise implementation I came up with that requires
// one less call to the pseudo-random number generator and
// one less mixing. Easily extandable to 3D
float triValueNoise(vec2 x)
{
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = random(l+s*e.yx);
float b = random(l+s*e.yx+e.xy);
float c = random(l+s*e.xy+(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
r *= r*(3.-2.*r); // Same cubic profile as smoothstep
return mix(mix(a, b, r.x), c, r.y);
}
// Same as before but using gradients, much smoother
float triGradNoise(vec2 x)
{
x.y *= 1.1547;
x.x -= 0.5*x.y;
vec2 l = floor(x);
vec2 r = fract(x);
float s = float(int(r.x+r.y > 1.));
vec2 e = vec2(1.,0.);
float a = dot(random2(l+s*e.yx), r-s*e.yx);
float b = dot(random2(l+s*e.yx+e.xy), r-s*e.yx-e.xy);
float c = dot(random2(l+s*e.xy+(1.-s)*e.yx), r-s*e.xy-(1.-s)*e.yx);
r.y = s+r.y*(1.-2.*s);
r.x = (r.x-s*r.y)/(1.-r.y);
// Quintic profile to have null second derivate at
// boundaries
r *= 6.*r*r*r*r-15.*r*r*r+10.*r*r;
return .5+.5*mix(mix(a, b, r.x), c, r.y);
}
// Fractional Brownian Motion noise to test the
// single-octave noise function
float fbm(vec2 x)
{
float n = 0.;
float A = 0.;
vec2 af = vec2(1., 2.);
for (int i=0; i<4; i++)
{
// Rotate each octave
float s = sin(float(2*i));
float c = cos(float(2*i));
mat2 m = mat2(c, s, -s, c);
n += af.x*NOISE(af.y*m*x);
A += af.x;
af *= vec2(.4,2.);
}
return n/A;
}
// A warp-based pattern inspired by https://iquilezles.org/articles/warp/
float pattern(vec2 x)
{
vec2 a = vec2(fbm(x)+iTime/15., fbm(x+vec2(2.2,0.)));
float b = fbm(x+a+iTime/10.);
vec2 c = vec2(fbm(x+b), fbm(a-vec2(1.7,0.)));
return fbm(x+.5*c);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord/iResolution.y;
vec2 d = 3.*vec2(2.+cos(iTime/10.), 3.+sin(iTime/10.));
fragColor = vec4(vec3(VIEW(2.*uv+d)), 1.);
} | libpng | [
4557,
4631,
4654,
4654,
4822
] | [
[
1611,
1656,
1678,
1678,
1739
],
[
1741,
1752,
1774,
1774,
1913
],
[
1915,
1966,
1997,
1997,
2278
],
[
2280,
2338,
2368,
2368,
2865
],
[
2867,
3029,
3058,
3058,
3481
],
[
3483,
3536,
3564,
3564,
4114
],
[
4116,
4197,
4216,
4216,
4555
],
[
4557,
4631,
4654,
4654,
4822
],
[
4824,
4824,
4881,
4881,
5029
]
] | // A warp-based pattern inspired by https://iquilezles.org/articles/warp/
| float pattern(vec2 x)
{ |
vec2 a = vec2(fbm(x)+iTime/15., fbm(x+vec2(2.2,0.)));
float b = fbm(x+a+iTime/10.);
vec2 c = vec2(fbm(x+b), fbm(a-vec2(1.7,0.)));
return fbm(x+.5*c);
} | // A warp-based pattern inspired by https://iquilezles.org/articles/warp/
float pattern(vec2 x)
{ | 1 | 2 |
dsGyWy | Zavie | 2023-10-05T20:46:19 | /*
This shader visualises the relative solid angle of the texels
of the face of a cubemap (compared to the center texel).
A very simple approximation is proposed. The approximation is
too crude for very low resolutions, but beyond 8x8 it seems
to result in under 1% error.
Maybe a fitting or offset could correct the error for the
very low resolutions as well?
Top center: texels of the cubemap face.
Bottom left: relative solid angle of the texels.
Bottom right: approximation of the relative solid angle.
Center: (200x exaggerated) error of the approximation.
The bottom histogram shows the error accross the diagonal, on
a squeezed scale. The short dashed graduations indicate the
0.125%, 0.25%, 0.5%, 1% and 2% error. The longer dashed
graduations indicate the 10%, 20%, 40% and 80% error.
License: CC BY 4.0
--
Zavie
*/
float approximateRelativeSolidAngle(vec2 uv, float resolution)
{
vec2 p = uv * 2. - 1.;
#if 0
// Verbose version:
// The more off-center the texel, the further the distance.
// So we scale by the inverse square distance.
float invSqrDist = 1. / (dot(p, p) + 1.);
// The more off-center the texel, the more it's facing away.
// So we scale by the cosine.
float cos_factor = 1. / sqrt(dot(p, p) + 1.);
// Combine the two factors:
return invSqrDist * cos_factor;
#else
// Short version:
// After derivation, the whole thing becomes just
float sqrDist = dot(p, p) + 1.;
return 1. / sqrt(sqrDist * sqrDist * sqrDist);
#endif
}
//
// areaElement and texelSolidAngle are adapted from
// AMD cubemapgen source code:
// https://code.google.com/archive/p/cubemapgen/
//
// See the derivation here:
// https://www.rorydriscoll.com/2012/01/15/cubemap-texel-solid-angle/
//
float areaElement(float x, float y)
{
return atan(x * y, sqrt(x * x + y * y + 1.));
}
float texelSolidAngle(vec2 uv, float resolution)
{
vec2 p = uv * 2. - 1.;
float invResolution = 1. / resolution;
// p is the -1..1 texture coordinate on the current face.
// Get projected area for this texel
vec2 pmin = p - invResolution;
vec2 pmax = p + invResolution;
float solidAngle = (
areaElement(pmin.x, pmin.y) -
areaElement(pmin.x, pmax.y) -
areaElement(pmax.x, pmin.y) +
areaElement(pmax.x, pmax.y));
return solidAngle;
}
float exactRelativeSolidAngle(vec2 uv, float resolution)
{
return texelSolidAngle(uv, resolution) / texelSolidAngle(vec2(0.5), resolution);
}
// ----------------------------------------------------------
vec2 remap(vec2 a, vec2 b, vec2 u)
{
return (u - a) / (b - a);
}
vec2 getMask(vec2 uv)
{
vec2 du = 2.*vec2(dFdx(uv.x), dFdy(uv.y));
return smoothstep(vec2(0.), du, uv) * (1. - smoothstep(vec2(1.) - du, vec2(1.), uv));
}
vec2 getRectangle(vec2 uv, float x, float y, float width, float height)
{
vec2 p = vec2(x, y);
vec2 size = vec2(width, height);
return remap(p - size/2., p + size/2., uv);
}
vec2 getSquare(vec2 uv, float x, float y, float size)
{
vec2 p = vec2(x, y);
return remap(p - size/2., p + size/2., uv);
}
vec2 discretise(vec2 uv, float resolution)
{
return (floor(uv * resolution) + 0.5) / resolution;
}
vec3 errorToColor(float error)
{
// Colours picked from:
// https://www.kennethmoreland.com/color-advice/
// The publication explains how to properly interpolate,
// but I was too lazy to implement it.
vec3 midPoint = vec3(0.865, 0.865, 0.865);
vec3 positive = vec3(0.230, 0.299, 0.754);
vec3 negative = vec3(0.706, 0.016, 0.150);
if (error > 0.)
{
if (error <= 1.)
return mix(midPoint, positive, error);
// Saturate to blue above 1:
return vec3(0., 0., 1.);
}
else
{
if (error >= -1.)
return mix(midPoint, negative, -error);
// Saturate to red below -1:
return vec3(1., 0., 0.);
}
}
float logScale(float x)
{
float scale = 9.;
return (exp(scale * x) - 1.) / (exp(scale) - 1.);
}
vec4 errorHistogram(vec2 uvPlot, vec2 uvFace, float error)
{
float x = uvPlot.x;
float dx = dFdy(x);
float y = logScale(uvPlot.y);
float dy = dFdy(y);
vec4 color = vec4(errorToColor(0.), 0.1);
if (error > 0.)
{
float e = (error);
color = mix(color, vec4(errorToColor(200. * error), 1.), smoothstep(y, y + dy, e));
}
else if (error < 0.)
{
float e = (-error);
color = mix(color, vec4(errorToColor(200. * -error), 1.), smoothstep(y, y + dy, e));
}
// Draw graduations at 0.125%, 0.25%, 0.5%, 1% and 2%
for (float graduation = 0.00125; graduation <= 0.02; graduation *= 2.)
{
float line = smoothstep(y - dy, y, graduation) * (1. - smoothstep(y, y + dy, graduation));
float dotted = fract(100. * x);
float dottedLine = line * smoothstep(0., dx, dotted) * (1. - smoothstep(0.75, 0.75 + dx, dotted));
color = mix(color, vec4(1. - color.rgb, 1.), 0.5*dottedLine);
}
// Draw graduations at 10%, 20%, 40% and 80%
for (float graduation = 0.1; graduation < 1.0; graduation *= 2.0)
{
float line = smoothstep(y - dy, y, graduation) * (1. - smoothstep(y, y + dy, graduation));
float dotted = fract(25. * x);
float dottedLine = line * smoothstep(0., dx, dotted) * (1. - smoothstep(0.85, 0.85 + dx, dotted));
color = mix(color, vec4(1. - color.rgb, 1.), 0.5*dottedLine);
}
return color;
}
// ----------------------------------------------------------
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
float evolution = abs(fract(iTime * 0.05) * 2. - 1.);
float cubeMapResolution = pow(2., 1. + floor(evolution * 10.));
vec2 uv = fragCoord/iResolution.x;
vec3 color = vec3(0.2);
// Visualise the texels depending on the cubemap resolution
{
vec2 uvFace = getSquare(uv, 0.5, 0.45, 0.22);
vec2 mask = getMask(uvFace);
uvFace = discretise(uvFace, cubeMapResolution);
vec3 texels = vec3(uvFace, 0.);
color = mix(color, texels, mask.x * mask.y);
}
// Visualise the texel relative solid angle
{
vec2 uvFace = getSquare(uv, 0.2, 0.32, 0.35);
vec2 mask = getMask(uvFace);
uvFace = discretise(uvFace, cubeMapResolution);
float ersa = exactRelativeSolidAngle(uvFace, cubeMapResolution);
color = mix(color, vec3(ersa), mask.x * mask.y);
}
// Visualise the approximated texel relative solid angle
{
vec2 uvFace = getSquare(uv, 1. - 0.2, 0.32, 0.35);
vec2 mask = getMask(uvFace);
uvFace = discretise(uvFace, cubeMapResolution);
float arsa = approximateRelativeSolidAngle(uvFace, cubeMapResolution);
color = mix(color, vec3(arsa), mask.x * mask.y);
}
// Visualise the difference between the texel relative
// solid angle and the approximation
{
vec2 uvFace = getSquare(uv, 0.5, 0.22, 0.22);
vec2 mask = getMask(uvFace);
uvFace = discretise(uvFace, cubeMapResolution);
float arsa2 = approximateRelativeSolidAngle(uvFace, cubeMapResolution);
float ersa2 = exactRelativeSolidAngle(uvFace, cubeMapResolution);
float error = ersa2 - arsa2;
color = mix(color, errorToColor(200. * error), mask.x * mask.y);
}
// Visualise more precisely the error across the diagonal
{
vec2 uvPlot = getRectangle(uv, 0.5, 0.05, 1., 0.1);
vec2 mask5 = getMask(uvPlot);
vec2 uvFace = vec2(uvPlot.x);
uvFace = discretise(uvFace, cubeMapResolution);
float arsa2 = approximateRelativeSolidAngle(uvFace, cubeMapResolution);
float ersa2 = exactRelativeSolidAngle(uvFace, cubeMapResolution);
float error = ersa2 - arsa2;
vec4 histogram = errorHistogram(uvPlot, uvFace, error);
color = mix(color, histogram.rgb, mask5.x * mask5.y * histogram.a);
}
fragColor = vec4(color, 1.0);
} | cc-by-4.0 | [
1519,
1758,
1795,
1795,
1847
] | [
[
835,
835,
899,
899,
1517
],
[
1519,
1758,
1795,
1795,
1847
],
[
1849,
1849,
1899,
1899,
2349
],
[
2351,
2351,
2409,
2409,
2496
],
[
2561,
2561,
2597,
2597,
2629
],
[
2631,
2631,
2654,
2654,
2793
],
[
2795,
2795,
2868,
2868,
2980
],
[
2982,
2982,
3037,
3037,
3112
],
[
3114,
3114,
3158,
3158,
3216
],
[
3218,
3218,
3250,
3435,
3926
],
[
3928,
3928,
3953,
3953,
4031
],
[
4033,
4033,
4093,
4093,
5484
],
[
5549,
5549,
5606,
5606,
7986
]
] | //
// areaElement and texelSolidAngle are adapted from
// AMD cubemapgen source code:
// https://code.google.com/archive/p/cubemapgen/
//
// See the derivation here:
// https://www.rorydriscoll.com/2012/01/15/cubemap-texel-solid-angle/
//
| float areaElement(float x, float y)
{ |
return atan(x * y, sqrt(x * x + y * y + 1.));
} | //
// areaElement and texelSolidAngle are adapted from
// AMD cubemapgen source code:
// https://code.google.com/archive/p/cubemapgen/
//
// See the derivation here:
// https://www.rorydriscoll.com/2012/01/15/cubemap-texel-solid-angle/
//
float areaElement(float x, float y)
{ | 1 | 1 |
ctdfzN | Peace | 2023-11-26T20:59:48 | // The MIT License
// Copyright © 2024 Giorgi Azmaipharashvili
// 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.
// From David Hoskins (MIT licensed): https://www.shadertoy.com/view/4djSRW
vec3 hash33(vec3 p3) {
p3 = fract(p3 * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yxx) * p3.zyx) - 0.5;
}
// From Nikita Miropolskiy (MIT licensed): https://www.shadertoy.com/view/XsX3zB
float simplex3d(vec3 p) {
vec3 s = floor(p + dot(p, vec3(1.0 / 3.0)));
vec3 x = p - s + dot(s, vec3(1.0 / 6.0));
vec3 e = step(vec3(0), x - x.yzx);
vec3 i1 = e * (1.0 - e.zxy);
vec3 i2 = 1.0 - e.zxy * (1.0 - e);
vec3 x1 = x - i1 + 1.0 / 6.0;
vec3 x2 = x - i2 + 1.0 / 3.0;
vec3 x3 = x - 0.5;
vec4 w = max(0.6 - vec4(dot(x, x), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
w *= w;
return dot(vec4(dot(hash33(s), x),
dot(hash33(s + i1), x1),
dot(hash33(s + i2), x2),
dot(hash33(s + 1.0), x3)) * w * w, vec4(52));
}
void mainImage( out vec4 fragColor, vec2 fragCoord) {
float time = iTime * 2.0;
float mr = min(iResolution.x, iResolution.y);
vec2 uv = (fragCoord.xy * 2.0 - iResolution.xy) / mr * 0.5;
vec2 p = vec2(0.5) + normalize(uv) * min(length(uv), 0.05);
vec3 p3 = 13.0 * vec3(p.xy, 0) + vec3(0, 0, time * 0.025);
float noise = simplex3d(p3 * 32.0) * 0.5 + 0.5;
float dist = abs(clamp(length(uv) / 12.0, 0.0, 1.0) * noise * 2.0 - 1.0);
const float e = 0.3;
float stepped = smoothstep(e - 0.5, e + 0.5, noise * (1.0 - pow(dist, 4.0)));
float final = smoothstep(e - 0.05, e + 0.05, noise * stepped);
fragColor = vec4(vec3(final),1.0);
} | mit | [
1090,
1166,
1188,
1188,
1325
] | [
[
1090,
1166,
1188,
1188,
1325
],
[
1327,
1408,
1433,
1433,
2009
],
[
2011,
2011,
2064,
2064,
2677
]
] | // From David Hoskins (MIT licensed): https://www.shadertoy.com/view/4djSRW
| vec3 hash33(vec3 p3) { |
p3 = fract(p3 * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yxx) * p3.zyx) - 0.5;
} | // From David Hoskins (MIT licensed): https://www.shadertoy.com/view/4djSRW
vec3 hash33(vec3 p3) { | 1 | 13 |
ctdfzN | Peace | 2023-11-26T20:59:48 | // The MIT License
// Copyright © 2024 Giorgi Azmaipharashvili
// 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.
// From David Hoskins (MIT licensed): https://www.shadertoy.com/view/4djSRW
vec3 hash33(vec3 p3) {
p3 = fract(p3 * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yxx) * p3.zyx) - 0.5;
}
// From Nikita Miropolskiy (MIT licensed): https://www.shadertoy.com/view/XsX3zB
float simplex3d(vec3 p) {
vec3 s = floor(p + dot(p, vec3(1.0 / 3.0)));
vec3 x = p - s + dot(s, vec3(1.0 / 6.0));
vec3 e = step(vec3(0), x - x.yzx);
vec3 i1 = e * (1.0 - e.zxy);
vec3 i2 = 1.0 - e.zxy * (1.0 - e);
vec3 x1 = x - i1 + 1.0 / 6.0;
vec3 x2 = x - i2 + 1.0 / 3.0;
vec3 x3 = x - 0.5;
vec4 w = max(0.6 - vec4(dot(x, x), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
w *= w;
return dot(vec4(dot(hash33(s), x),
dot(hash33(s + i1), x1),
dot(hash33(s + i2), x2),
dot(hash33(s + 1.0), x3)) * w * w, vec4(52));
}
void mainImage( out vec4 fragColor, vec2 fragCoord) {
float time = iTime * 2.0;
float mr = min(iResolution.x, iResolution.y);
vec2 uv = (fragCoord.xy * 2.0 - iResolution.xy) / mr * 0.5;
vec2 p = vec2(0.5) + normalize(uv) * min(length(uv), 0.05);
vec3 p3 = 13.0 * vec3(p.xy, 0) + vec3(0, 0, time * 0.025);
float noise = simplex3d(p3 * 32.0) * 0.5 + 0.5;
float dist = abs(clamp(length(uv) / 12.0, 0.0, 1.0) * noise * 2.0 - 1.0);
const float e = 0.3;
float stepped = smoothstep(e - 0.5, e + 0.5, noise * (1.0 - pow(dist, 4.0)));
float final = smoothstep(e - 0.05, e + 0.05, noise * stepped);
fragColor = vec4(vec3(final),1.0);
} | mit | [
1327,
1408,
1433,
1433,
2009
] | [
[
1090,
1166,
1188,
1188,
1325
],
[
1327,
1408,
1433,
1433,
2009
],
[
2011,
2011,
2064,
2064,
2677
]
] | // From Nikita Miropolskiy (MIT licensed): https://www.shadertoy.com/view/XsX3zB
| float simplex3d(vec3 p) { |
vec3 s = floor(p + dot(p, vec3(1.0 / 3.0)));
vec3 x = p - s + dot(s, vec3(1.0 / 6.0));
vec3 e = step(vec3(0), x - x.yzx);
vec3 i1 = e * (1.0 - e.zxy);
vec3 i2 = 1.0 - e.zxy * (1.0 - e);
vec3 x1 = x - i1 + 1.0 / 6.0;
vec3 x2 = x - i2 + 1.0 / 3.0;
vec3 x3 = x - 0.5;
vec4 w = max(0.6 - vec4(dot(x, x), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
w *= w;
return dot(vec4(dot(hash33(s), x),
dot(hash33(s + i1), x1),
dot(hash33(s + i2), x2),
dot(hash33(s + 1.0), x3)) * w * w, vec4(52));
} | // From Nikita Miropolskiy (MIT licensed): https://www.shadertoy.com/view/XsX3zB
float simplex3d(vec3 p) { | 1 | 56 |
clGyWm | iq | 2023-11-18T00:27:31 | // The MIT License
// Copyright © 2023 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
// Analytic derivatives for a simple field of random Gabor kernels
// please never use this hash or any other fract based
// hash in production. they are really bad.
vec2 hash( in vec2 x )
{
const vec2 k = vec2( 0.3183099, 0.3678794 );
x = x*k + k.yx;
return fract( 16.0*k*fract( x.x*x.y*(x.x+x.y)) );
}
vec3 gabor_wave(in vec2 p)
{
vec2 ip = floor(p);
vec2 fp = fract(p);
const float fr = 2.0*6.283185;
const float fa = 4.0;
vec3 av = vec3(0.0,0.0,0.0);
vec3 at = vec3(0.0,0.0,0.0);
for( int j=-2; j<=2; j++ ) // can reduce this search to just [-1,1]
for( int i=-2; i<=2; i++ ) // if you are okey with some small errors
{
vec2 o = vec2( i, j );
vec2 h = hash(ip+o);
vec2 r = fp - (o+h);
vec2 k = normalize(-1.0+2.0*hash(ip+o+vec2(11,31)) );
float d = dot(r, r);
float l = dot(r, k);
float w = exp(-fa*d);
vec2 cs = vec2( cos(fr*l), sin(fr*l) );
av += w*vec3(cs.x, -2.0*fa*r*cs.x - cs.y*fr*k );
at += w*vec3(1.0, -2.0*fa*r);
}
//return av;
return vec3( av.x, av.yz-av.x*at.yz/at.x ) /at.x;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord/iResolution.y;
vec3 f = gabor_wave(8.0*p);
vec3 col = vec3(0.5 + 0.5*f.x);
if( p.y<0.5 )
{
// show analytical derivatives
col = 0.5 + 0.01*vec3(f.yz*8.0,0.0);
// show low quality numerical derivatives (check correctness)
//col = 0.5 + 0.01*vec3(dFdx(f.x)*iResolution.y, dFdy(f.x)*iResolution.y, 0.0 );
}
fragColor = vec4( col, 1.0 );
} | mit | [
1213,
1312,
1339,
1339,
1464
] | [
[
1213,
1312,
1339,
1339,
1464
],
[
1466,
1466,
1494,
1494,
2306
],
[
2308,
2308,
2365,
2365,
2792
]
] | // please never use this hash or any other fract based
// hash in production. they are really bad.
| vec2 hash( in vec2 x )
{ |
const vec2 k = vec2( 0.3183099, 0.3678794 );
x = x*k + k.yx;
return fract( 16.0*k*fract( x.x*x.y*(x.x+x.y)) );
} | // please never use this hash or any other fract based
// hash in production. they are really bad.
vec2 hash( in vec2 x )
{ | 1 | 1 |
dtGyWw | fishy | 2023-11-17T23:40:21 | // The MIT License
// Copyright © 2015 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.
// Using Newtown's root solver to compute the distance to
// an ellipse, instead of using the analytical solution in
// https://www.shadertoy.com/view/4sS3zz.
//
// In retrospect, it's the same as Antonalog's https://www.shadertoy.com/view/MtXXW7
//
// More information here:
//
// https://iquilezles.org/articles/ellipsedist
//
//
// Ellipse distances related shaders:
//
// Analytical : https://www.shadertoy.com/view/4sS3zz
// Newton Trig : https://www.shadertoy.com/view/4lsXDN
// Newton No-Trig : https://www.shadertoy.com/view/tttfzr
// ?????????????? : https://www.shadertoy.com/view/tt3yz7
// List of some other 2D distances: https://www.shadertoy.com/playlist/MXdSRf
//
// and iquilezles.org/articles/distfunctions2d
#define rot(t) mat2(cos(t), -sin(t), sin(t), cos(t))
// for visualization purposes only
float sdSegment( in vec2 p, in vec2 a, in vec2 b )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h =clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length(pa-h*ba);
}
float sdEllipse( vec2 p, vec2 a, vec2 b )
{
// do transformations and get the minor/major radii
float la = length(a);
float lb = length(b);
p *= mat2(a/la, b/lb);
vec2 ab = vec2(la, lb);
// everything past this point is by iq
// symmetry
p = abs( p );
// find root with Newton solver
vec2 q = ab*(p-ab);
float w = (q.x<q.y)? 1.570796327 : 0.0;
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));
}
// compute final point and distance
float d = length(p-ab*vec2(cos(w),sin(w)));
// return signed distance
return (dot(p/ab,p/ab)>1.0) ? d : -d;
}
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;
vec2 a = vec2(sin(iTime*2.5)*0.1 + 0.5, 0);
vec2 b = vec2(0, sin(iTime*1.5)*0.1 + 0.5);
a *= rot(iTime*0.25);
b *= rot(iTime*0.25);
if(iMouse.z > 0.001)
{
a = m;
b = (normalize(a)*(sin(iTime*1.5)*0.1+0.5))*rot(1.57);
}
float d = sdEllipse( p, a, b );
vec3 col = vec3(1.0) - sign(d)*vec3(0.1,0.4,0.7);
col *= 1.0 - exp(-2.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)) );
// added these lines to show the vectors
col = mix( col, vec3(1.0, 1.0, 0.0), 1.0-smoothstep(0.0,0.01,sdSegment(p, vec2(0), a)) );
col = mix( col, vec3(0.0, 1.0, 1.0), 1.0-smoothstep(0.0,0.01,sdSegment(p, vec2(0), b)) );
fragColor = vec4( col, 1.0 );;
} | mit | [
1868,
1903,
1955,
1955,
2078
] | [
[
1868,
1903,
1955,
1955,
2078
],
[
2080,
2080,
2123,
2179,
2850
],
[
2852,
2852,
2909,
2909,
3772
]
] | // for visualization purposes only
| float sdSegment( in vec2 p, in vec2 a, in vec2 b )
{ |
vec2 ba = b-a;
vec2 pa = p-a;
float h =clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length(pa-h*ba);
} | // for visualization purposes only
float sdSegment( in vec2 p, in vec2 a, in vec2 b )
{ | 2 | 121 |
dldyWN | lf94 | 2023-11-06T03:44:40 | // 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.
// I'm starting with 2D but should scale up to 3D no problem
// This demonstrates having different radius ratios
// I need this for my SDF CAD stuff
// Most mininal example. You see it stretches the circle.
float sdCircle( in vec2 p, in float r )
{
return length(vec2(p.x / 2., p.y))-r;
}
// Derived from the SDF box video from IQ. The max(r.y/r.x, 1.) is my stuff
float sdBox1(in vec2 p, in vec2 r) {
return sqrt(pow(max((p.x-r.x)*max(r.y/r.x, 1.), 0.0),2.) + pow(max((p.y-r.y)*max(r.x/r.y, 1.),0.0),2.));
}
// This is the final form
float sdBox( in vec2 p, in vec2 b, in vec2 r )
{
vec2 s = max(vec2(r.y/r.x, r.x/r.y), 1.);
vec2 b2 = b*vec2(r.y/r.x, r.x/r.y);
vec2 d = abs(p)-b2;
vec2 d2 = vec2((d.x-r.x)*s.x, (d.y-r.y)*s.y);
return (length(max(d2,0.0)) + min(max(d2.x,d2.y),0.0)) - max(r.y/r.x, r.x/r.y);
}
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;
float d = sdBox(p * 10., vec2(1.0, 1.0), vec2(4.00, 1.00)) / 10.;
// coloring
vec3 col = (d>0.0) ? vec3(0.9,0.6,0.3) : vec3(0.65,0.85,1.0);
col *= 1.0 - exp(-6.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)) );
if( iMouse.z>0.001 )
{
d = sdBox(p * 10., vec2(1.0, 1.0), vec2(4.00, 1.00)) / 10.;
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 | [
1600,
1626,
1674,
1674,
1925
] | [
[
1230,
1288,
1330,
1330,
1374
],
[
1376,
1453,
1489,
1489,
1598
],
[
1600,
1626,
1674,
1674,
1925
],
[
1927,
1927,
1984,
1984,
2699
]
] | // This is the final form
| float sdBox( in vec2 p, in vec2 b, in vec2 r )
{ |
vec2 s = max(vec2(r.y/r.x, r.x/r.y), 1.);
vec2 b2 = b*vec2(r.y/r.x, r.x/r.y);
vec2 d = abs(p)-b2;
vec2 d2 = vec2((d.x-r.x)*s.x, (d.y-r.y)*s.y);
return (length(max(d2,0.0)) + min(max(d2.x,d2.y),0.0)) - max(r.y/r.x, r.x/r.y);
} | // This is the final form
float sdBox( in vec2 p, in vec2 b, in vec2 r )
{ | 1 | 1 |
dltcW4 | hasse | 2023-11-06T00:35:27 | // Forked from https://www.shadertoy.com/view/ll3Xzf
// Original license text:
// The MIT License
// Copyright © 2016 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.
// Disk projection code (project_disk()) is by me and is CC0.
// Currently, there's some float instability near grazing angles.
// https://math.stackexchange.com/questions/2411047/parameters-of-the-conic-curve-resulting-from-the-perspective-projection-of-a-cir/2411591#comment4978079_2411047
// https://en.wikipedia.org/wiki/Ellipse#General_ellipse
void project_disk(
vec3 center,
vec3 normal,
float radius,
vec2 proj_info,
out vec2 proj_center,
out vec2 major,
out vec2 minor
){
float nc = dot(normal, center);
float r2 = radius * radius;
float c2r2 = dot(center, center) - r2;
vec3 n2 = normal * normal;
vec3 ACF = nc * nc - 2.0f * center * normal * nc + c2r2 * n2;
float A = ACF.x, C = ACF.y, F = ACF.z;
float B = 2.0f * c2r2 * normal.x * normal.y - 2.0f * (center.x * normal.y + center.y * normal.x) * nc;
proj_center = (normal.xy * normal.z * r2 + center.xy * center.z) / (r2 - r2 * n2.z - center.z * center.z);
float angle = -0.5f * atan(B, A-C);
float cos_a = cos(angle);
float sin_a = sin(angle);
float cos2_a = cos_a * cos_a;
// TODO: It may be possible to simplify this further.
float K = A*proj_center.x*proj_center.x + B*proj_center.x*proj_center.y + C*proj_center.y*proj_center.y - F;
float radius_num = sqrt(abs(K * (2.0f * cos2_a - 1.0f)));
float major_radius = radius_num * inversesqrt(abs((A + C) * cos2_a - A));
float minor_radius = radius_num * inversesqrt(abs((A + C) * cos2_a - C));
major = vec2(sin_a, cos_a) * major_radius;
minor = vec2(cos_a, -sin_a) * minor_radius;
proj_center *= 2.0f / proj_info;
major *= 2.0f / proj_info;
minor *= 2.0f / proj_info;
}
// ray-disk intersection
float iDisk( in vec3 ro, in vec3 rd, // ray: origin, direction
in vec3 cen, in vec3 nor, float rad ) // disk: center, normal, radius
{
vec3 q = ro - cen;
float t = -dot(nor,q)/dot(rd,nor);
if( t<0.0 ) return -1.0;
vec3 d = q + rd*t;
if( dot(d,d)>(rad*rad) ) return -1.0;
return t;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 tot = vec3(0.0);
vec2 p = (fragCoord.xy/iResolution.xy) * 2.0f - 1.0f;
float aspect = iResolution.x / iResolution.y;
p.x *= aspect;
// camera position
vec3 ro = vec3( 0,0,1.5 );
// create view ray
vec3 rd = normalize(vec3(p.xy, -1.5) );
// disk animation
vec3 disk_center = 0.3*sin(iTime*vec3(1.11,1.27,1.47)+vec3(2.0,5.0,6.0));
vec3 disk_axis = normalize( sin(iTime*vec3(1.23,1.41,1.07)+vec3(0.0,1.0,3.0)) );
float disk_radius = 0.4 + 0.2*sin(iTime*1.3+0.5);
// render
vec3 col = vec3(0.4)*(1.0-0.3*length(p));
// raytrace disk
float t = iDisk( ro, rd, disk_center, disk_axis, disk_radius );
float tmin = 1e10;
if( t>0.0 )
{
tmin = t;
col = vec3(1.0,0.75,0.3)*(0.7+0.2*abs(disk_axis.y));
}
tot += col;
float proj_plane_dist = 1.5 * aspect * 0.5f;
vec2 proj_info = vec2(proj_plane_dist, proj_plane_dist);
vec2 proj_center;
vec2 major;
vec2 minor;
project_disk(
disk_center-ro,
disk_axis,
disk_radius,
proj_info,
proj_center,
major,
minor
);
vec2 nmajor = normalize(major);
vec2 nminor = normalize(minor);
// Paint major axis point
if(distance(p, proj_center + major) < 0.015f)
tot = vec3(0,1,0);
// Paint major axis line
if(distance(p, proj_center + dot(p-proj_center, nmajor) * nmajor) < 0.005f && abs(dot(p-proj_center, nmajor)) < length(major))
tot = vec3(0,1,0);
// Paint minor axis point
if(distance(p, proj_center + minor) < 0.015f)
tot = vec3(0,0,1);
// Paint minor axis line
if(distance(p, proj_center + dot(p-proj_center, nminor) * nminor) < 0.005f && abs(dot(p-proj_center, nminor)) < length(minor))
tot = vec3(0,0,1);
// Paint center of projected ellipse
if(distance(p, proj_center) < 0.015f)
tot = vec3(1,0,0);
fragColor = vec4( tot, 1.0 );
} | mit | [
1158,
1507,
1665,
1665,
2864
] | [
[
1158,
1507,
1665,
1665,
2864
],
[
2866,
2891,
3052,
3052,
3222
],
[
3224,
3224,
3281,
3281,
5204
]
] | // Disk projection code (project_disk()) is by me and is CC0.
// Currently, there's some float instability near grazing angles.
// https://math.stackexchange.com/questions/2411047/parameters-of-the-conic-curve-resulting-from-the-perspective-projection-of-a-cir/2411591#comment4978079_2411047
// https://en.wikipedia.org/wiki/Ellipse#General_ellipse
| void project_disk(
vec3 center,
vec3 normal,
float radius,
vec2 proj_info,
out vec2 proj_center,
out vec2 major,
out vec2 minor
){ |
float nc = dot(normal, center);
float r2 = radius * radius;
float c2r2 = dot(center, center) - r2;
vec3 n2 = normal * normal;
vec3 ACF = nc * nc - 2.0f * center * normal * nc + c2r2 * n2;
float A = ACF.x, C = ACF.y, F = ACF.z;
float B = 2.0f * c2r2 * normal.x * normal.y - 2.0f * (center.x * normal.y + center.y * normal.x) * nc;
proj_center = (normal.xy * normal.z * r2 + center.xy * center.z) / (r2 - r2 * n2.z - center.z * center.z);
float angle = -0.5f * atan(B, A-C);
float cos_a = cos(angle);
float sin_a = sin(angle);
float cos2_a = cos_a * cos_a;
// TODO: It may be possible to simplify this further.
float K = A*proj_center.x*proj_center.x + B*proj_center.x*proj_center.y + C*proj_center.y*proj_center.y - F;
float radius_num = sqrt(abs(K * (2.0f * cos2_a - 1.0f)));
float major_radius = radius_num * inversesqrt(abs((A + C) * cos2_a - A));
float minor_radius = radius_num * inversesqrt(abs((A + C) * cos2_a - C));
major = vec2(sin_a, cos_a) * major_radius;
minor = vec2(cos_a, -sin_a) * minor_radius;
proj_center *= 2.0f / proj_info;
major *= 2.0f / proj_info;
minor *= 2.0f / proj_info;
} | // Disk projection code (project_disk()) is by me and is CC0.
// Currently, there's some float instability near grazing angles.
// https://math.stackexchange.com/questions/2411047/parameters-of-the-conic-curve-resulting-from-the-perspective-projection-of-a-cir/2411591#comment4978079_2411047
// https://en.wikipedia.org/wiki/Ellipse#General_ellipse
void project_disk(
vec3 center,
vec3 normal,
float radius,
vec2 proj_info,
out vec2 proj_center,
out vec2 major,
out vec2 minor
){ | 1 | 1 |
Dts3R2 | piyushslayer | 2023-12-24T18:31:04 | /**
* Creative Commons CC0 1.0 Universal (CC-0)
*
* A simple christmas tree made from 2D points.
*
*/
#define PI 3.1415926535
#define TAU 6.2831853071
#define ROTATE(v, x) mat2(cos(x), sin(x), -sin(x), cos(x)) * v
#define REMAP_HALF_NDC(x, c, d) (((x + 0.5) * (d - c)) + c) // Remap from [-0.5, 0.5] domain to [c, d]
#define N 512.0
#define N_ONE_QUARTER N * 0.25
// This is mostly to cull any points at the bottom that are too close to the "camera".
#define N_OFFSET 1.0
#define STAR_N 7.0
#define STAR_BEAM_THICKNESS 3.0
const vec3 LIGHT_COLORS[3] = vec3[3](
vec3(1.0, 0.05, 0.05),
vec3(0.05, 1.0, 0.05),
vec3(1.0, 0.25, 0.05)
);
// https://www.shadertoy.com/view/4djSRW
float Hash12(vec2 p)
{
vec3 p3 = fract(vec3(p.xyx) * .1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
vec2 Hash21(float p)
{
vec3 p3 = fract(vec3(p) * vec3(.1031, .1030, .0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
// Signed distance to an n-star polygon with external angle en by iq: https://www.shadertoy.com/view/3tSGDy
float SignedDistanceNStar2D(in vec2 p, in float r, in float an, in float bn, in vec2 acs, in float m) // m=[2,n]
{
float en = PI / m;
vec2 ecs = vec2(cos(en), sin(en));
p = length(p) * vec2(cos(bn), abs(sin(bn)));
p -= r * acs;
p += ecs * clamp(-dot(p, ecs), 0.0, r * acs.y / ecs.y);
return length(p) * sign(p.x);
}
void DrawStar(in vec2 uv, in float time, inout vec3 outColor)
{
uv -= vec2(0.001, 0.225);
uv = ROTATE(uv, time * 0.75);
// Some common pre-calculation in order to avoid duplication
float an = PI / STAR_N;
float bn = mod(atan(uv.x, uv.y), 2.0 * an) - an;
vec2 acs = vec2(cos(an), sin(an));
// Top star
outColor += 5e-4 / pow(abs(SignedDistanceNStar2D(uv, 0.01, an, bn, acs, STAR_N * 0.5)), 1.23) * LIGHT_COLORS[2];
// Star beams
outColor += smoothstep(STAR_BEAM_THICKNESS / max(iResolution.x, iResolution.y), 0.0, SignedDistanceNStar2D(uv, 1.5, an, bn, acs, STAR_N)) *
LIGHT_COLORS[2] * smoothstep(0.75, -5.0, length(uv));
}
void DrawTree(in vec2 uv, in float time, inout vec3 outColor)
{
float u, theta, pointHeight, invN = 1.0 / N;
vec2 st, hash, layer;
vec3 pointOnCone, pointColor = vec3(1.0);
const vec2 radius = vec2(1.5, 3.2);
uvec3 colorThreshold;
for (float i = N_OFFSET; i < N; ++i)
{
// Modify this to change the tree pattern
hash = Hash21(2.0 * TAU * i);
// Some basic light color based on hash
colorThreshold.x = uint(hash.x < 0.45); // red;
colorThreshold.y = 1u - colorThreshold.x; // green
colorThreshold.z = uint(hash.x > 0.9); // white;
pointColor = vec3(colorThreshold | colorThreshold.z);
// Calculate point on cone based on: https://mathworld.wolfram.com/Cone.html
u = i * invN;
theta = 1609.0 * hash.x + time * 0.5;
pointHeight = 1.0 - u;
// Split the cone into layers to make it look more like a christmas tree
layer = vec2(3.2 * mod(i, N_ONE_QUARTER) * invN, 0.0);
pointOnCone = 0.5 * (radius.xyx - layer.xyx) * vec3(pointHeight * cos(theta), u - 0.5, pointHeight * sin(theta)); // [-0.5, 0.5]
// Scale uv based on depth of the point
st = uv * (REMAP_HALF_NDC(pointOnCone.z, 0.5, 1.0) + hash.y) * 4.5;
// outColor += smoothstep(0.01, 0.0, length(st - pointOnCone.xy));
outColor += REMAP_HALF_NDC(pointOnCone.z, 3.0, 0.6) * // Slightly adjust the size of the point based on distance to "camera"
2e-5 / pow(length(st - pointOnCone.xy), 1.7) * pointColor;
}
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord - iResolution.xy * 0.5) / iResolution.y; // [-0.5, 0.5] adjusted for aspect ratio
vec3 outColor = vec3(0.005, 0.01, 0.03); // Background color
vec4 m = iMouse / iResolution.yyyy;
float t = 0.0;
if (m.z > 0.0)
{
t = m.x * TAU;
}
else
{
t = iTime * 0.5;
}
DrawTree(uv, t, outColor);
DrawStar(uv, t, outColor);
float vignette = dot(uv, uv);
vignette *= vignette;
vignette = 1.0 / (vignette * vignette + 1.0);
fragColor = vec4(pow(outColor * vignette, vec3(0.4545)), 1.0) - Hash12(fragCoord.xy * t + iResolution.yy) * 0.04;
} | cc0-1.0 | [
800,
841,
863,
863,
980
] | [
[
800,
841,
863,
863,
980
],
[
982,
982,
1004,
1004,
1137
],
[
1139,
1247,
1361,
1361,
1588
],
[
1590,
1590,
1653,
1653,
2262
],
[
2264,
2264,
2327,
2327,
3853
],
[
3855,
3855,
3912,
3912,
4555
]
] | // https://www.shadertoy.com/view/4djSRW
| float Hash12(vec2 p)
{ |
vec3 p3 = fract(vec3(p.xyx) * .1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
} | // https://www.shadertoy.com/view/4djSRW
float Hash12(vec2 p)
{ | 2 | 6 |
Dts3R2 | piyushslayer | 2023-12-24T18:31:04 | /**
* Creative Commons CC0 1.0 Universal (CC-0)
*
* A simple christmas tree made from 2D points.
*
*/
#define PI 3.1415926535
#define TAU 6.2831853071
#define ROTATE(v, x) mat2(cos(x), sin(x), -sin(x), cos(x)) * v
#define REMAP_HALF_NDC(x, c, d) (((x + 0.5) * (d - c)) + c) // Remap from [-0.5, 0.5] domain to [c, d]
#define N 512.0
#define N_ONE_QUARTER N * 0.25
// This is mostly to cull any points at the bottom that are too close to the "camera".
#define N_OFFSET 1.0
#define STAR_N 7.0
#define STAR_BEAM_THICKNESS 3.0
const vec3 LIGHT_COLORS[3] = vec3[3](
vec3(1.0, 0.05, 0.05),
vec3(0.05, 1.0, 0.05),
vec3(1.0, 0.25, 0.05)
);
// https://www.shadertoy.com/view/4djSRW
float Hash12(vec2 p)
{
vec3 p3 = fract(vec3(p.xyx) * .1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
vec2 Hash21(float p)
{
vec3 p3 = fract(vec3(p) * vec3(.1031, .1030, .0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
// Signed distance to an n-star polygon with external angle en by iq: https://www.shadertoy.com/view/3tSGDy
float SignedDistanceNStar2D(in vec2 p, in float r, in float an, in float bn, in vec2 acs, in float m) // m=[2,n]
{
float en = PI / m;
vec2 ecs = vec2(cos(en), sin(en));
p = length(p) * vec2(cos(bn), abs(sin(bn)));
p -= r * acs;
p += ecs * clamp(-dot(p, ecs), 0.0, r * acs.y / ecs.y);
return length(p) * sign(p.x);
}
void DrawStar(in vec2 uv, in float time, inout vec3 outColor)
{
uv -= vec2(0.001, 0.225);
uv = ROTATE(uv, time * 0.75);
// Some common pre-calculation in order to avoid duplication
float an = PI / STAR_N;
float bn = mod(atan(uv.x, uv.y), 2.0 * an) - an;
vec2 acs = vec2(cos(an), sin(an));
// Top star
outColor += 5e-4 / pow(abs(SignedDistanceNStar2D(uv, 0.01, an, bn, acs, STAR_N * 0.5)), 1.23) * LIGHT_COLORS[2];
// Star beams
outColor += smoothstep(STAR_BEAM_THICKNESS / max(iResolution.x, iResolution.y), 0.0, SignedDistanceNStar2D(uv, 1.5, an, bn, acs, STAR_N)) *
LIGHT_COLORS[2] * smoothstep(0.75, -5.0, length(uv));
}
void DrawTree(in vec2 uv, in float time, inout vec3 outColor)
{
float u, theta, pointHeight, invN = 1.0 / N;
vec2 st, hash, layer;
vec3 pointOnCone, pointColor = vec3(1.0);
const vec2 radius = vec2(1.5, 3.2);
uvec3 colorThreshold;
for (float i = N_OFFSET; i < N; ++i)
{
// Modify this to change the tree pattern
hash = Hash21(2.0 * TAU * i);
// Some basic light color based on hash
colorThreshold.x = uint(hash.x < 0.45); // red;
colorThreshold.y = 1u - colorThreshold.x; // green
colorThreshold.z = uint(hash.x > 0.9); // white;
pointColor = vec3(colorThreshold | colorThreshold.z);
// Calculate point on cone based on: https://mathworld.wolfram.com/Cone.html
u = i * invN;
theta = 1609.0 * hash.x + time * 0.5;
pointHeight = 1.0 - u;
// Split the cone into layers to make it look more like a christmas tree
layer = vec2(3.2 * mod(i, N_ONE_QUARTER) * invN, 0.0);
pointOnCone = 0.5 * (radius.xyx - layer.xyx) * vec3(pointHeight * cos(theta), u - 0.5, pointHeight * sin(theta)); // [-0.5, 0.5]
// Scale uv based on depth of the point
st = uv * (REMAP_HALF_NDC(pointOnCone.z, 0.5, 1.0) + hash.y) * 4.5;
// outColor += smoothstep(0.01, 0.0, length(st - pointOnCone.xy));
outColor += REMAP_HALF_NDC(pointOnCone.z, 3.0, 0.6) * // Slightly adjust the size of the point based on distance to "camera"
2e-5 / pow(length(st - pointOnCone.xy), 1.7) * pointColor;
}
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord - iResolution.xy * 0.5) / iResolution.y; // [-0.5, 0.5] adjusted for aspect ratio
vec3 outColor = vec3(0.005, 0.01, 0.03); // Background color
vec4 m = iMouse / iResolution.yyyy;
float t = 0.0;
if (m.z > 0.0)
{
t = m.x * TAU;
}
else
{
t = iTime * 0.5;
}
DrawTree(uv, t, outColor);
DrawStar(uv, t, outColor);
float vignette = dot(uv, uv);
vignette *= vignette;
vignette = 1.0 / (vignette * vignette + 1.0);
fragColor = vec4(pow(outColor * vignette, vec3(0.4545)), 1.0) - Hash12(fragCoord.xy * t + iResolution.yy) * 0.04;
} | cc0-1.0 | [
1139,
1247,
1361,
1361,
1588
] | [
[
800,
841,
863,
863,
980
],
[
982,
982,
1004,
1004,
1137
],
[
1139,
1247,
1361,
1361,
1588
],
[
1590,
1590,
1653,
1653,
2262
],
[
2264,
2264,
2327,
2327,
3853
],
[
3855,
3855,
3912,
3912,
4555
]
] | // Signed distance to an n-star polygon with external angle en by iq: https://www.shadertoy.com/view/3tSGDy
| float SignedDistanceNStar2D(in vec2 p, in float r, in float an, in float bn, in vec2 acs, in float m) // m=[2,n]
{ |
float en = PI / m;
vec2 ecs = vec2(cos(en), sin(en));
p = length(p) * vec2(cos(bn), abs(sin(bn)));
p -= r * acs;
p += ecs * clamp(-dot(p, ecs), 0.0, r * acs.y / ecs.y);
return length(p) * sign(p.x);
} | // Signed distance to an n-star polygon with external angle en by iq: https://www.shadertoy.com/view/3tSGDy
float SignedDistanceNStar2D(in vec2 p, in float r, in float an, in float bn, in vec2 acs, in float m) // m=[2,n]
{ | 2 | 2 |
dtGBRG | Dain | 2023-12-11T01:18:25 | // https://www.shadertoy.com/view/dtGBDz orthogonal circles grassy plant, 2023 jt
// based on https://www.shadertoy.com/view/ctyBzm orthogonal circles flower sdf 3d
// based on https://www.shadertoy.com/view/clGBzm orthogonal circles flower sdf 2
// based on https://www.shadertoy.com/view/dldBWl orthogonal circles flower sdf
// exact sdf for shape related to https://www.shadertoy.com/view/cltfW2 orthogonal circles flower
// SDF exactness using https://www.shadertoy.com/view/DdX3WH Interior Distance Detect Errors
// Circle arcs orthogonal to unit sphere
// with circle segment endpoints at equidistant latitude / longitude.
// Reminds me of a clumpy grass variant I like.
// TODO: Can something similar be done with spherical fibonacci instead?
// (see e.g. https://www.shadertoy.com/view/lllXz4 )
// tags: sdf, flower, circle, grass, distance, conformal, disk, plant, loopless, exact, orthogonal
// The MIT License
// Copyright (c) 2023 Jakob Thomsen
// 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.
//succulent makes DF not so precise, so set to 0 if you want exact SDF
//It doesn't appear to miss the surface, but it will jump further into the grass rather than landing on the 0 contour
#define SUCCULENT_RADIUS .08
//exact SDF radius, if somewhat boring looking
#define GRASS_RADIUS 0.01
#define pi 3.1415926
float ortho_circle_flower_sdf(float n, vec2 p) // https://www.shadertoy.com/view/dldBWl orthogonal circle flower sdf (jt)
{
p.x = abs(p.x);
n = 2.01+ abs(sin(iTime*.5)*10.0);//Any N > 2 will work now
float slice2 = (pi*2.0)/float(n);
// endpoints
float phiReal = atan(p.x,p.y);
float endPtRound = fract(n*.5);
//quantized phi--but do it so it works with any N value not just odd
float phi = (round(phiReal/slice2 - endPtRound)+endPtRound)*slice2;
vec2 c = vec2(sin(phi),cos(phi));
float d = length(p-c);//distance to endpoint
bool wantCircle = dot(p,c)<1.0
|| phi <0.0; //Without this sometimes you get a floating tip at center/top with non odd N :/
//we can skip this circle stuff when outside the shape
if(wantCircle){
float slice = slice2*.5;
float a = (p.y*p.y+2.0*p.y+p.x*p.x+1.0);
float b = p.x*2.0;
float ang1 = floor(atan(a,b)/slice)*slice;
float r0 = tan(ang1); // quantize floor angle
float r1 = tan(ang1 + slice); // quantize ceil angle
d = min(abs(length(p-vec2(r0,-1))-r0),
abs(length(p-vec2(r1,-1))-r1));
}
return d;
}
float dot2(vec2 a){
// return length(a);
return dot(a,a);
}
float fade(float f){
// return sin(f*pi);
f = 1.0-f*f;
f = 1.0-f*f;
return f;
}
float ortho_circle_flower_sdf(float m, float n, vec3 p) // https://www.shadertoy.com/view/dtGBDz orthogonal circles grassy plant (jt)
{
float phi = round(atan(p.y,p.x)/(2.0*pi/float(m)))*(2.0*pi/float(m)); // polar & quantize
// phi = abs(phi);
p.xy = mat2(cos(phi),-sin(phi),sin(phi),cos(phi))*p.xy;
// p.x = abs(p.x);
// return length(vec2(ortho_circle_flower_sdf(n, vec2(p.xz)),p.y));
float q = ortho_circle_flower_sdf(float(n), vec2(p.x,p.z));
return length(vec2(q,p.y))-SUCCULENT_RADIUS*fade(1.0-min(dot2(p.xz),1.0));;
}
mat3 yaw_pitch_roll(float yaw, float pitch, float roll)
{
mat3 R = mat3(vec3(cos(yaw), sin(yaw), 0.0), vec3(-sin(yaw), cos(yaw), 0.0), vec3(0.0, 0.0, 1.0));
mat3 S = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, cos(pitch), sin(pitch)), vec3(0.0, -sin(pitch), cos(pitch)));
mat3 T = mat3(vec3(cos(roll), 0.0, sin(roll)), vec3(0.0, 1.0, 0.0), vec3(-sin(roll), 0.0, cos(roll)));
return R * S * T;
}
float map(vec3 p)
{
float n = float(mix(3.0, 11.0, 0.5+0.5*cos(2.0*pi*iTime/10.0))); // animate number of leafs
// n = 5.0;
float m =2.01 + cos(iTime*.2)*10.0;
// m =3;
return ortho_circle_flower_sdf(m, n, p)-GRASS_RADIUS;
}
#define EPSILON 0.001
#define DIST_MAX 50.0
#define ITER_MAX 200u
// https://iquilezles.org/articles/normalsSDF tetrahedron normals
vec3 normal( vec3 p )
{
const float h = EPSILON;
const vec2 k = vec2(1,-1);
return normalize( k.xyy*map( p + k.xyy*h ) +
k.yyx*map( p + k.yyx*h ) +
k.yxy*map( p + k.yxy*h ) +
k.xxx*map( p + k.xxx*h ) );
}
float trace(vec3 ro, vec3 rd, float t0, float t1) // pass on running out of iterations
{
// NOTE: Limited number of iterations to avoid stalling
// when ray passes closely (just above EPSILON)
// in parallel to a surface.
uint i;
float t;
for(t = t0, i = 0u; t < t1 && i < ITER_MAX; i++)
{
float h = map(ro + rd * t);
if(h < EPSILON)
return t;
t += h;
}
return t; // stop on running out of iterations
//return t1; // pass on running out of iterations
}
// NOTE: Don't forget to add +normal*EPSILON to the starting position
// to avoid artifacts caused by getting stuck in the surface
// due to starting at distance < EPSILON from the surface.
// (normal could be calculated here but that would most likely be redundant)
float shadow(vec3 ro, vec3 rd, float t0, float t1)
{
return trace(ro, rd, t0, t1) < t1 ? 0.0 : 1.0;
}
// https://iquilezles.org/articles/rmshadows
float softshadow(vec3 ro, in vec3 rd, float t0, float t1, float k)
{
float res = 1.0;
float ph = 1e20;
uint i;
float t;
for(t = t0, i = 0u; t < t1 && i < ITER_MAX; i++)
{
float h = map(ro + rd*t);
if( h < EPSILON )
return 0.0;
float y = h*h/(2.0*ph);
float d = sqrt(h*h-y*y);
res = min( res, k*d/max(0.0,t-y) );
ph = h;
t += h;
}
return res;
}
// https://www.shadertoy.com/view/Xds3zN raymarching primitives
float calcAO( in vec3 pos, in vec3 nor )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float h = 0.01 + 0.12*float(i)/4.0;
float d = map( pos + h*nor );
occ += (h-d)*sca;
sca *= 0.95;
if( occ>0.35 ) break;
}
return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) ;
}
vec3 material(vec3 p)
{
return vec3(0.1,1.0,0.0);
}
void mainImage(out vec4 fragColor, vec2 I)
{
bool demo = all(lessThan(iMouse.xy, vec2(10.0)));
vec2 R = iResolution.xy;
I = (2.0 * I - R) / R.y; // concise scaling thanks to Fabrice
float yaw = 2.0 * pi * float(iMouse.x) / float(R.x);
float pitch = pi - pi / 2.0 * float(iMouse.y) / float(R.y);
yaw = !demo ? yaw : 2.0 * pi * fract(iTime * 0.01);
pitch = !demo ? pitch : 4.0/3.0 * pi / 2.0;
vec3 ro = vec3(0.0, 0.0,-2.5);
vec3 rd = normalize(vec3(I.xy, 2.0)); // NOTE: omitting normalization results in clipped edges artifact
mat3 M = yaw_pitch_roll(yaw, pitch, 0.0);
ro = M * ro;
rd = M * rd;
//ro.z += 1.0;
vec3 color = vec3(1);
float dist = trace(ro, rd, 0.0, DIST_MAX);
if(dist < DIST_MAX)
{
vec3 dst = ro + rd * dist;
vec3 n = normal(dst);
//color *= (n * 0.5 + 0.5);
color *= material(dst);
vec3 lightdir = normalize(vec3(1.0, 1.0, 1.0));
vec3 ambient = vec3(0.4);
float brightness = max(dot(lightdir, n), 0.0);
if(brightness > 0.0)
brightness *= shadow(ro + rd * dist + n * 0.01, lightdir, 0.0, DIST_MAX);
//brightness *= softshadow(ro + rd * dist + n * 0.01, lightdir, 0.0, DIST_MAX, 20.0);
color *= (ambient * calcAO(dst, n) + brightness);
if(brightness > 0.0)
{
float specular = pow(max(0.0, dot(n, normalize(-rd + lightdir))), 250.0);
color += specular;
}
vec3 fog_color = vec3(0.2);
color = mix(fog_color, vec3(color), exp(-pow(dist/20.0, 2.0))); // fog
}
else
{
//color *= mix(vec3(0,0.5,0.5),vec3(0,0,1),abs(-rd.z)); // sky
color *= 0.0;
}
color = tanh(color); // roll-off overly bright colors
color = sqrt(color); // approximate gamma
fragColor = vec4(color, 1);
}
| mit | [
5004,
5070,
5093,
5093,
5352
] | [
[
2307,
2307,
2430,
2430,
3560
],
[
3562,
3562,
3581,
3605,
3628
],
[
3629,
3629,
3649,
3673,
3723
],
[
3724,
3724,
3859,
3859,
4286
],
[
4288,
4288,
4345,
4345,
4691
],
[
4693,
4693,
4712,
4712,
4935
],
[
5004,
5070,
5093,
5093,
5352
],
[
5354,
5354,
5442,
5599,
5895
],
[
5897,
6182,
6234,
6234,
6287
],
[
6289,
6334,
6402,
6402,
6777
],
[
6779,
6843,
6885,
6885,
7175
],
[
7177,
7177,
7200,
7200,
7232
],
[
7234,
7234,
7278,
7278,
9093
]
] | // https://iquilezles.org/articles/normalsSDF tetrahedron normals
| vec3 normal( vec3 p )
{ |
const float h = EPSILON;
const vec2 k = vec2(1,-1);
return normalize( k.xyy*map( p + k.xyy*h ) +
k.yyx*map( p + k.yyx*h ) +
k.yxy*map( p + k.yxy*h ) +
k.xxx*map( p + k.xxx*h ) );
} | // https://iquilezles.org/articles/normalsSDF tetrahedron normals
vec3 normal( vec3 p )
{ | 1 | 6 |
dtGBRG | Dain | 2023-12-11T01:18:25 | // https://www.shadertoy.com/view/dtGBDz orthogonal circles grassy plant, 2023 jt
// based on https://www.shadertoy.com/view/ctyBzm orthogonal circles flower sdf 3d
// based on https://www.shadertoy.com/view/clGBzm orthogonal circles flower sdf 2
// based on https://www.shadertoy.com/view/dldBWl orthogonal circles flower sdf
// exact sdf for shape related to https://www.shadertoy.com/view/cltfW2 orthogonal circles flower
// SDF exactness using https://www.shadertoy.com/view/DdX3WH Interior Distance Detect Errors
// Circle arcs orthogonal to unit sphere
// with circle segment endpoints at equidistant latitude / longitude.
// Reminds me of a clumpy grass variant I like.
// TODO: Can something similar be done with spherical fibonacci instead?
// (see e.g. https://www.shadertoy.com/view/lllXz4 )
// tags: sdf, flower, circle, grass, distance, conformal, disk, plant, loopless, exact, orthogonal
// The MIT License
// Copyright (c) 2023 Jakob Thomsen
// 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.
//succulent makes DF not so precise, so set to 0 if you want exact SDF
//It doesn't appear to miss the surface, but it will jump further into the grass rather than landing on the 0 contour
#define SUCCULENT_RADIUS .08
//exact SDF radius, if somewhat boring looking
#define GRASS_RADIUS 0.01
#define pi 3.1415926
float ortho_circle_flower_sdf(float n, vec2 p) // https://www.shadertoy.com/view/dldBWl orthogonal circle flower sdf (jt)
{
p.x = abs(p.x);
n = 2.01+ abs(sin(iTime*.5)*10.0);//Any N > 2 will work now
float slice2 = (pi*2.0)/float(n);
// endpoints
float phiReal = atan(p.x,p.y);
float endPtRound = fract(n*.5);
//quantized phi--but do it so it works with any N value not just odd
float phi = (round(phiReal/slice2 - endPtRound)+endPtRound)*slice2;
vec2 c = vec2(sin(phi),cos(phi));
float d = length(p-c);//distance to endpoint
bool wantCircle = dot(p,c)<1.0
|| phi <0.0; //Without this sometimes you get a floating tip at center/top with non odd N :/
//we can skip this circle stuff when outside the shape
if(wantCircle){
float slice = slice2*.5;
float a = (p.y*p.y+2.0*p.y+p.x*p.x+1.0);
float b = p.x*2.0;
float ang1 = floor(atan(a,b)/slice)*slice;
float r0 = tan(ang1); // quantize floor angle
float r1 = tan(ang1 + slice); // quantize ceil angle
d = min(abs(length(p-vec2(r0,-1))-r0),
abs(length(p-vec2(r1,-1))-r1));
}
return d;
}
float dot2(vec2 a){
// return length(a);
return dot(a,a);
}
float fade(float f){
// return sin(f*pi);
f = 1.0-f*f;
f = 1.0-f*f;
return f;
}
float ortho_circle_flower_sdf(float m, float n, vec3 p) // https://www.shadertoy.com/view/dtGBDz orthogonal circles grassy plant (jt)
{
float phi = round(atan(p.y,p.x)/(2.0*pi/float(m)))*(2.0*pi/float(m)); // polar & quantize
// phi = abs(phi);
p.xy = mat2(cos(phi),-sin(phi),sin(phi),cos(phi))*p.xy;
// p.x = abs(p.x);
// return length(vec2(ortho_circle_flower_sdf(n, vec2(p.xz)),p.y));
float q = ortho_circle_flower_sdf(float(n), vec2(p.x,p.z));
return length(vec2(q,p.y))-SUCCULENT_RADIUS*fade(1.0-min(dot2(p.xz),1.0));;
}
mat3 yaw_pitch_roll(float yaw, float pitch, float roll)
{
mat3 R = mat3(vec3(cos(yaw), sin(yaw), 0.0), vec3(-sin(yaw), cos(yaw), 0.0), vec3(0.0, 0.0, 1.0));
mat3 S = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, cos(pitch), sin(pitch)), vec3(0.0, -sin(pitch), cos(pitch)));
mat3 T = mat3(vec3(cos(roll), 0.0, sin(roll)), vec3(0.0, 1.0, 0.0), vec3(-sin(roll), 0.0, cos(roll)));
return R * S * T;
}
float map(vec3 p)
{
float n = float(mix(3.0, 11.0, 0.5+0.5*cos(2.0*pi*iTime/10.0))); // animate number of leafs
// n = 5.0;
float m =2.01 + cos(iTime*.2)*10.0;
// m =3;
return ortho_circle_flower_sdf(m, n, p)-GRASS_RADIUS;
}
#define EPSILON 0.001
#define DIST_MAX 50.0
#define ITER_MAX 200u
// https://iquilezles.org/articles/normalsSDF tetrahedron normals
vec3 normal( vec3 p )
{
const float h = EPSILON;
const vec2 k = vec2(1,-1);
return normalize( k.xyy*map( p + k.xyy*h ) +
k.yyx*map( p + k.yyx*h ) +
k.yxy*map( p + k.yxy*h ) +
k.xxx*map( p + k.xxx*h ) );
}
float trace(vec3 ro, vec3 rd, float t0, float t1) // pass on running out of iterations
{
// NOTE: Limited number of iterations to avoid stalling
// when ray passes closely (just above EPSILON)
// in parallel to a surface.
uint i;
float t;
for(t = t0, i = 0u; t < t1 && i < ITER_MAX; i++)
{
float h = map(ro + rd * t);
if(h < EPSILON)
return t;
t += h;
}
return t; // stop on running out of iterations
//return t1; // pass on running out of iterations
}
// NOTE: Don't forget to add +normal*EPSILON to the starting position
// to avoid artifacts caused by getting stuck in the surface
// due to starting at distance < EPSILON from the surface.
// (normal could be calculated here but that would most likely be redundant)
float shadow(vec3 ro, vec3 rd, float t0, float t1)
{
return trace(ro, rd, t0, t1) < t1 ? 0.0 : 1.0;
}
// https://iquilezles.org/articles/rmshadows
float softshadow(vec3 ro, in vec3 rd, float t0, float t1, float k)
{
float res = 1.0;
float ph = 1e20;
uint i;
float t;
for(t = t0, i = 0u; t < t1 && i < ITER_MAX; i++)
{
float h = map(ro + rd*t);
if( h < EPSILON )
return 0.0;
float y = h*h/(2.0*ph);
float d = sqrt(h*h-y*y);
res = min( res, k*d/max(0.0,t-y) );
ph = h;
t += h;
}
return res;
}
// https://www.shadertoy.com/view/Xds3zN raymarching primitives
float calcAO( in vec3 pos, in vec3 nor )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float h = 0.01 + 0.12*float(i)/4.0;
float d = map( pos + h*nor );
occ += (h-d)*sca;
sca *= 0.95;
if( occ>0.35 ) break;
}
return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) ;
}
vec3 material(vec3 p)
{
return vec3(0.1,1.0,0.0);
}
void mainImage(out vec4 fragColor, vec2 I)
{
bool demo = all(lessThan(iMouse.xy, vec2(10.0)));
vec2 R = iResolution.xy;
I = (2.0 * I - R) / R.y; // concise scaling thanks to Fabrice
float yaw = 2.0 * pi * float(iMouse.x) / float(R.x);
float pitch = pi - pi / 2.0 * float(iMouse.y) / float(R.y);
yaw = !demo ? yaw : 2.0 * pi * fract(iTime * 0.01);
pitch = !demo ? pitch : 4.0/3.0 * pi / 2.0;
vec3 ro = vec3(0.0, 0.0,-2.5);
vec3 rd = normalize(vec3(I.xy, 2.0)); // NOTE: omitting normalization results in clipped edges artifact
mat3 M = yaw_pitch_roll(yaw, pitch, 0.0);
ro = M * ro;
rd = M * rd;
//ro.z += 1.0;
vec3 color = vec3(1);
float dist = trace(ro, rd, 0.0, DIST_MAX);
if(dist < DIST_MAX)
{
vec3 dst = ro + rd * dist;
vec3 n = normal(dst);
//color *= (n * 0.5 + 0.5);
color *= material(dst);
vec3 lightdir = normalize(vec3(1.0, 1.0, 1.0));
vec3 ambient = vec3(0.4);
float brightness = max(dot(lightdir, n), 0.0);
if(brightness > 0.0)
brightness *= shadow(ro + rd * dist + n * 0.01, lightdir, 0.0, DIST_MAX);
//brightness *= softshadow(ro + rd * dist + n * 0.01, lightdir, 0.0, DIST_MAX, 20.0);
color *= (ambient * calcAO(dst, n) + brightness);
if(brightness > 0.0)
{
float specular = pow(max(0.0, dot(n, normalize(-rd + lightdir))), 250.0);
color += specular;
}
vec3 fog_color = vec3(0.2);
color = mix(fog_color, vec3(color), exp(-pow(dist/20.0, 2.0))); // fog
}
else
{
//color *= mix(vec3(0,0.5,0.5),vec3(0,0,1),abs(-rd.z)); // sky
color *= 0.0;
}
color = tanh(color); // roll-off overly bright colors
color = sqrt(color); // approximate gamma
fragColor = vec4(color, 1);
}
| mit | [
5897,
6182,
6234,
6234,
6287
] | [
[
2307,
2307,
2430,
2430,
3560
],
[
3562,
3562,
3581,
3605,
3628
],
[
3629,
3629,
3649,
3673,
3723
],
[
3724,
3724,
3859,
3859,
4286
],
[
4288,
4288,
4345,
4345,
4691
],
[
4693,
4693,
4712,
4712,
4935
],
[
5004,
5070,
5093,
5093,
5352
],
[
5354,
5354,
5442,
5599,
5895
],
[
5897,
6182,
6234,
6234,
6287
],
[
6289,
6334,
6402,
6402,
6777
],
[
6779,
6843,
6885,
6885,
7175
],
[
7177,
7177,
7200,
7200,
7232
],
[
7234,
7234,
7278,
7278,
9093
]
] | // NOTE: Don't forget to add +normal*EPSILON to the starting position
// to avoid artifacts caused by getting stuck in the surface
// due to starting at distance < EPSILON from the surface.
// (normal could be calculated here but that would most likely be redundant)
| float shadow(vec3 ro, vec3 rd, float t0, float t1)
{ |
return trace(ro, rd, t0, t1) < t1 ? 0.0 : 1.0;
} | // NOTE: Don't forget to add +normal*EPSILON to the starting position
// to avoid artifacts caused by getting stuck in the surface
// due to starting at distance < EPSILON from the surface.
// (normal could be calculated here but that would most likely be redundant)
float shadow(vec3 ro, vec3 rd, float t0, float t1)
{ | 1 | 1 |
dtGBRG | Dain | 2023-12-11T01:18:25 | // https://www.shadertoy.com/view/dtGBDz orthogonal circles grassy plant, 2023 jt
// based on https://www.shadertoy.com/view/ctyBzm orthogonal circles flower sdf 3d
// based on https://www.shadertoy.com/view/clGBzm orthogonal circles flower sdf 2
// based on https://www.shadertoy.com/view/dldBWl orthogonal circles flower sdf
// exact sdf for shape related to https://www.shadertoy.com/view/cltfW2 orthogonal circles flower
// SDF exactness using https://www.shadertoy.com/view/DdX3WH Interior Distance Detect Errors
// Circle arcs orthogonal to unit sphere
// with circle segment endpoints at equidistant latitude / longitude.
// Reminds me of a clumpy grass variant I like.
// TODO: Can something similar be done with spherical fibonacci instead?
// (see e.g. https://www.shadertoy.com/view/lllXz4 )
// tags: sdf, flower, circle, grass, distance, conformal, disk, plant, loopless, exact, orthogonal
// The MIT License
// Copyright (c) 2023 Jakob Thomsen
// 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.
//succulent makes DF not so precise, so set to 0 if you want exact SDF
//It doesn't appear to miss the surface, but it will jump further into the grass rather than landing on the 0 contour
#define SUCCULENT_RADIUS .08
//exact SDF radius, if somewhat boring looking
#define GRASS_RADIUS 0.01
#define pi 3.1415926
float ortho_circle_flower_sdf(float n, vec2 p) // https://www.shadertoy.com/view/dldBWl orthogonal circle flower sdf (jt)
{
p.x = abs(p.x);
n = 2.01+ abs(sin(iTime*.5)*10.0);//Any N > 2 will work now
float slice2 = (pi*2.0)/float(n);
// endpoints
float phiReal = atan(p.x,p.y);
float endPtRound = fract(n*.5);
//quantized phi--but do it so it works with any N value not just odd
float phi = (round(phiReal/slice2 - endPtRound)+endPtRound)*slice2;
vec2 c = vec2(sin(phi),cos(phi));
float d = length(p-c);//distance to endpoint
bool wantCircle = dot(p,c)<1.0
|| phi <0.0; //Without this sometimes you get a floating tip at center/top with non odd N :/
//we can skip this circle stuff when outside the shape
if(wantCircle){
float slice = slice2*.5;
float a = (p.y*p.y+2.0*p.y+p.x*p.x+1.0);
float b = p.x*2.0;
float ang1 = floor(atan(a,b)/slice)*slice;
float r0 = tan(ang1); // quantize floor angle
float r1 = tan(ang1 + slice); // quantize ceil angle
d = min(abs(length(p-vec2(r0,-1))-r0),
abs(length(p-vec2(r1,-1))-r1));
}
return d;
}
float dot2(vec2 a){
// return length(a);
return dot(a,a);
}
float fade(float f){
// return sin(f*pi);
f = 1.0-f*f;
f = 1.0-f*f;
return f;
}
float ortho_circle_flower_sdf(float m, float n, vec3 p) // https://www.shadertoy.com/view/dtGBDz orthogonal circles grassy plant (jt)
{
float phi = round(atan(p.y,p.x)/(2.0*pi/float(m)))*(2.0*pi/float(m)); // polar & quantize
// phi = abs(phi);
p.xy = mat2(cos(phi),-sin(phi),sin(phi),cos(phi))*p.xy;
// p.x = abs(p.x);
// return length(vec2(ortho_circle_flower_sdf(n, vec2(p.xz)),p.y));
float q = ortho_circle_flower_sdf(float(n), vec2(p.x,p.z));
return length(vec2(q,p.y))-SUCCULENT_RADIUS*fade(1.0-min(dot2(p.xz),1.0));;
}
mat3 yaw_pitch_roll(float yaw, float pitch, float roll)
{
mat3 R = mat3(vec3(cos(yaw), sin(yaw), 0.0), vec3(-sin(yaw), cos(yaw), 0.0), vec3(0.0, 0.0, 1.0));
mat3 S = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, cos(pitch), sin(pitch)), vec3(0.0, -sin(pitch), cos(pitch)));
mat3 T = mat3(vec3(cos(roll), 0.0, sin(roll)), vec3(0.0, 1.0, 0.0), vec3(-sin(roll), 0.0, cos(roll)));
return R * S * T;
}
float map(vec3 p)
{
float n = float(mix(3.0, 11.0, 0.5+0.5*cos(2.0*pi*iTime/10.0))); // animate number of leafs
// n = 5.0;
float m =2.01 + cos(iTime*.2)*10.0;
// m =3;
return ortho_circle_flower_sdf(m, n, p)-GRASS_RADIUS;
}
#define EPSILON 0.001
#define DIST_MAX 50.0
#define ITER_MAX 200u
// https://iquilezles.org/articles/normalsSDF tetrahedron normals
vec3 normal( vec3 p )
{
const float h = EPSILON;
const vec2 k = vec2(1,-1);
return normalize( k.xyy*map( p + k.xyy*h ) +
k.yyx*map( p + k.yyx*h ) +
k.yxy*map( p + k.yxy*h ) +
k.xxx*map( p + k.xxx*h ) );
}
float trace(vec3 ro, vec3 rd, float t0, float t1) // pass on running out of iterations
{
// NOTE: Limited number of iterations to avoid stalling
// when ray passes closely (just above EPSILON)
// in parallel to a surface.
uint i;
float t;
for(t = t0, i = 0u; t < t1 && i < ITER_MAX; i++)
{
float h = map(ro + rd * t);
if(h < EPSILON)
return t;
t += h;
}
return t; // stop on running out of iterations
//return t1; // pass on running out of iterations
}
// NOTE: Don't forget to add +normal*EPSILON to the starting position
// to avoid artifacts caused by getting stuck in the surface
// due to starting at distance < EPSILON from the surface.
// (normal could be calculated here but that would most likely be redundant)
float shadow(vec3 ro, vec3 rd, float t0, float t1)
{
return trace(ro, rd, t0, t1) < t1 ? 0.0 : 1.0;
}
// https://iquilezles.org/articles/rmshadows
float softshadow(vec3 ro, in vec3 rd, float t0, float t1, float k)
{
float res = 1.0;
float ph = 1e20;
uint i;
float t;
for(t = t0, i = 0u; t < t1 && i < ITER_MAX; i++)
{
float h = map(ro + rd*t);
if( h < EPSILON )
return 0.0;
float y = h*h/(2.0*ph);
float d = sqrt(h*h-y*y);
res = min( res, k*d/max(0.0,t-y) );
ph = h;
t += h;
}
return res;
}
// https://www.shadertoy.com/view/Xds3zN raymarching primitives
float calcAO( in vec3 pos, in vec3 nor )
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float h = 0.01 + 0.12*float(i)/4.0;
float d = map( pos + h*nor );
occ += (h-d)*sca;
sca *= 0.95;
if( occ>0.35 ) break;
}
return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) ;
}
vec3 material(vec3 p)
{
return vec3(0.1,1.0,0.0);
}
void mainImage(out vec4 fragColor, vec2 I)
{
bool demo = all(lessThan(iMouse.xy, vec2(10.0)));
vec2 R = iResolution.xy;
I = (2.0 * I - R) / R.y; // concise scaling thanks to Fabrice
float yaw = 2.0 * pi * float(iMouse.x) / float(R.x);
float pitch = pi - pi / 2.0 * float(iMouse.y) / float(R.y);
yaw = !demo ? yaw : 2.0 * pi * fract(iTime * 0.01);
pitch = !demo ? pitch : 4.0/3.0 * pi / 2.0;
vec3 ro = vec3(0.0, 0.0,-2.5);
vec3 rd = normalize(vec3(I.xy, 2.0)); // NOTE: omitting normalization results in clipped edges artifact
mat3 M = yaw_pitch_roll(yaw, pitch, 0.0);
ro = M * ro;
rd = M * rd;
//ro.z += 1.0;
vec3 color = vec3(1);
float dist = trace(ro, rd, 0.0, DIST_MAX);
if(dist < DIST_MAX)
{
vec3 dst = ro + rd * dist;
vec3 n = normal(dst);
//color *= (n * 0.5 + 0.5);
color *= material(dst);
vec3 lightdir = normalize(vec3(1.0, 1.0, 1.0));
vec3 ambient = vec3(0.4);
float brightness = max(dot(lightdir, n), 0.0);
if(brightness > 0.0)
brightness *= shadow(ro + rd * dist + n * 0.01, lightdir, 0.0, DIST_MAX);
//brightness *= softshadow(ro + rd * dist + n * 0.01, lightdir, 0.0, DIST_MAX, 20.0);
color *= (ambient * calcAO(dst, n) + brightness);
if(brightness > 0.0)
{
float specular = pow(max(0.0, dot(n, normalize(-rd + lightdir))), 250.0);
color += specular;
}
vec3 fog_color = vec3(0.2);
color = mix(fog_color, vec3(color), exp(-pow(dist/20.0, 2.0))); // fog
}
else
{
//color *= mix(vec3(0,0.5,0.5),vec3(0,0,1),abs(-rd.z)); // sky
color *= 0.0;
}
color = tanh(color); // roll-off overly bright colors
color = sqrt(color); // approximate gamma
fragColor = vec4(color, 1);
}
| mit | [
6779,
6843,
6885,
6885,
7175
] | [
[
2307,
2307,
2430,
2430,
3560
],
[
3562,
3562,
3581,
3605,
3628
],
[
3629,
3629,
3649,
3673,
3723
],
[
3724,
3724,
3859,
3859,
4286
],
[
4288,
4288,
4345,
4345,
4691
],
[
4693,
4693,
4712,
4712,
4935
],
[
5004,
5070,
5093,
5093,
5352
],
[
5354,
5354,
5442,
5599,
5895
],
[
5897,
6182,
6234,
6234,
6287
],
[
6289,
6334,
6402,
6402,
6777
],
[
6779,
6843,
6885,
6885,
7175
],
[
7177,
7177,
7200,
7200,
7232
],
[
7234,
7234,
7278,
7278,
9093
]
] | // https://www.shadertoy.com/view/Xds3zN raymarching primitives
| float calcAO( in vec3 pos, in vec3 nor )
{ |
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float h = 0.01 + 0.12*float(i)/4.0;
float d = map( pos + h*nor );
occ += (h-d)*sca;
sca *= 0.95;
if( occ>0.35 ) break;
}
return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) ;
} | // https://www.shadertoy.com/view/Xds3zN raymarching primitives
float calcAO( in vec3 pos, in vec3 nor )
{ | 1 | 259 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
993,
1070,
1130,
1130,
1523
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // Helper function generating a rotation matrix around the axis by the angle
| mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) { |
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
} | // Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) { | 1 | 1 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
1525,
1592,
1621,
1621,
2317
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // Helper function that generates camera ray based on UV and mouse
| vec3 getRay(vec2 fragCoord) { |
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
} | // Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) { | 1 | 1 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
2319,
2340,
2366,
2366,
2454
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // Standard 2d noise
| float rand2dTime(vec2 co){ |
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
} | // Standard 2d noise
float rand2dTime(vec2 co){ | 1 | 1 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
2456,
2479,
2500,
2500,
2569
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // Hash for 3d vectors
| float rand3d(vec3 p){ |
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
} | // Hash for 3d vectors
float rand3d(vec3 p){ | 1 | 1 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
2571,
2589,
2611,
2611,
3773
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // 3D value noise
| float noise3d(vec3 x){ |
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
} | // 3D value noise
float noise3d(vec3 x){ | 1 | 1 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
3775,
3807,
3834,
3834,
3915
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // 3D simplex noise, cool trick
| float supernoise3d(vec3 p){ |
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
} | // 3D simplex noise, cool trick
float supernoise3d(vec3 p){ | 1 | 2 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
4801,
4870,
4894,
4894,
5105
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // Pretty self explanatory FBM with some precisely adjusted behavior
| float cloudsFBM(vec3 p){ |
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
} | // Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){ | 1 | 1 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
5221,
5393,
5424,
5424,
6194
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
| vec2 cloudsDensity3D(vec3 pos){ |
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
} | // this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){ | 1 | 1 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
7849,
8201,
8257,
8257,
8618
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
| vec3 renderGround(vec3 point, float dist, float random){ |
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
} | // very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){ | 1 | 1 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
8625,
8713,
8806,
8806,
9094
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // Straightforward, render raymarch, apply fog, alpha blend with the background, return
| vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){ |
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
} | // Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){ | 1 | 1 |
clKfWm | afl_ext | 2023-12-09T21:45:07 | // afl_ext 2017-2023
// MIT License
// Use your mouse to move the camera around! Press the Left Mouse Button on the image to look around!
#define NormalizedMouse (iMouse.xy / iResolution.xy) // normalize mouse coords
// settings for the shader
#define STEP_SIZE_SCALE 500.0 // lower means more steps
#define CloudsFloor 1000.0
#define CloudsCeil 5000.0
// decide how much clouds coverage there is, this can dramatically affect performance
// basically this shader works better when there are more than less clouds
#define COVERAGE_START 0.02
#define COVERAGE_END 0.23
#define CLOUDS_FBM_STEPS 5
#define EXPOSURE 0.5
// this shader supports view from inside and over the clouds too,
// give it a try by uncommenting this line
//#define FLYING_CAMERA
#ifndef FLYING_CAMERA
#define CAMERA_HEIGHT (200.0)
#define FOG_COLOR vec3(0.04)
#endif
#ifdef FLYING_CAMERA
#define CAMERA_HEIGHT (10.0 + (0.5 + 0.5 * sin(iTime * 0.2)) * 7000.0)
#define FOG_COLOR vec3(0.00)
#endif
// Helper function generating a rotation matrix around the axis by the angle
mat3 createRotationMatrixAxisAngle(vec3 axis, float angle) {
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat3(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c
);
}
// Helper function that generates camera ray based on UV and mouse
vec3 getRay(vec2 fragCoord) {
vec2 uv = ((fragCoord.xy / iResolution.xy) * 2.0 - 1.0) * vec2(iResolution.x / iResolution.y, 1.0);
// for fisheye, uncomment following line and comment the next one
//vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05);
vec3 proj = normalize(vec3(uv.x, uv.y, 1.5));
if(iResolution.x < 600.0 || NormalizedMouse.x == 0.0) {
return proj * createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), -0.6);
}
return createRotationMatrixAxisAngle(vec3(0.0, -1.0, 0.0), 3.0 * ((NormalizedMouse.x + 0.5) * 2.0 - 1.0))
* createRotationMatrixAxisAngle(vec3(1.0, 0.0, 0.0), 0.5 + 1.5 * ((NormalizedMouse.y * 1.5) * 2.0 - 1.0))
* proj;
}
// Standard 2d noise
float rand2dTime(vec2 co){
co *= iTime;
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
// Hash for 3d vectors
float rand3d(vec3 p){
return fract(4768.1232345456 * sin((p.x+p.y*43.0+p.z*137.0)));
}
// 3D value noise
float noise3d(vec3 x){
vec3 p = floor(x);
vec3 fr = fract(x);
vec3 LBZ = p + vec3(0.0, 0.0, 0.0);
vec3 LTZ = p + vec3(0.0, 1.0, 0.0);
vec3 RBZ = p + vec3(1.0, 0.0, 0.0);
vec3 RTZ = p + vec3(1.0, 1.0, 0.0);
vec3 LBF = p + vec3(0.0, 0.0, 1.0);
vec3 LTF = p + vec3(0.0, 1.0, 1.0);
vec3 RBF = p + vec3(1.0, 0.0, 1.0);
vec3 RTF = p + vec3(1.0, 1.0, 1.0);
float l0candidate1 = rand3d(LBZ);
float l0candidate2 = rand3d(RBZ);
float l0candidate3 = rand3d(LTZ);
float l0candidate4 = rand3d(RTZ);
float l0candidate5 = rand3d(LBF);
float l0candidate6 = rand3d(RBF);
float l0candidate7 = rand3d(LTF);
float l0candidate8 = rand3d(RTF);
float l1candidate1 = mix(l0candidate1, l0candidate2, fr[0]);
float l1candidate2 = mix(l0candidate3, l0candidate4, fr[0]);
float l1candidate3 = mix(l0candidate5, l0candidate6, fr[0]);
float l1candidate4 = mix(l0candidate7, l0candidate8, fr[0]);
float l2candidate1 = mix(l1candidate1, l1candidate2, fr[1]);
float l2candidate2 = mix(l1candidate3, l1candidate4, fr[1]);
float l3candidate1 = mix(l2candidate1, l2candidate2, fr[2]);
return l3candidate1;
}
// 3D simplex noise, cool trick
float supernoise3d(vec3 p){
float a = noise3d(p);
float b = noise3d(p + 10.5);
return (a + b) * 0.5;
}
// Sphere raytracing
struct Ray { vec3 o; vec3 d; };
struct Sphere { vec3 pos; float rad; };
float minhit = 0.0;
float maxhit = 0.0;
float raySphereIntersect(in Ray ray, in Sphere sphere)
{
vec3 oc = ray.o - sphere.pos;
float b = 2.0 * dot(ray.d, oc);
float c = dot(oc, oc) - sphere.rad*sphere.rad;
float disc = b * b - 4.0 * c;
vec2 ex = vec2(-b - sqrt(disc), -b + sqrt(disc))/2.0;
minhit = min(ex.x, ex.y);
maxhit = max(ex.x, ex.y);
//return mix(ex.y, ex.x, step(0.0, ex.x));
//return max(ex.x, ex.y);
if(minhit < 0.0 && maxhit > 0.0)
return maxhit;
if(minhit < maxhit && minhit > 0.0)
return minhit;
return 0.0;
}
float hitLimit = 678000.0;
float higherHitLimit = 6780000.0;
#define isHitBoolean(v) (v > 0.0 && v < hitLimit)
#define isHitStep(v) step(0.0, v)
// Clouds code
#define VECTOR_UP vec3(0.0,1.0,0.0)
// Pretty self explanatory FBM with some precisely adjusted behavior
float cloudsFBM(vec3 p){
float a = 0.0;
float w = 0.5;
for(int i=0;i<CLOUDS_FBM_STEPS;i++){
float x = abs(0.5 - supernoise3d(p))*2.0;
a += x * w;
p = p * 2.9;
w *= 0.60;
}
return a;
}
float planetradius = 6378000.1;
float getHeightOverSurface(vec3 p){
return length(p) - planetradius;
}
// this function probes the clouds densite at a point
// returns XY
// X = coverage of clouds at this point,
// Y = cloud color at this point, basically incoming radiance
vec2 cloudsDensity3D(vec3 pos){
float h = getHeightOverSurface(pos);
pos -= vec3(0,planetradius,0);
// make sure the clouds look like clouds by making the density
// scale with height between the layers, make it a height coefficent
// this coeff is 1.0 in between the layers, and 0.0 at the edges
float measurement = (CloudsCeil - CloudsFloor) * 0.5;
float mediana = (CloudsCeil + CloudsFloor) * 0.5;
float mlt = (( 1.0 - (abs( h - mediana ) / measurement )));
// probe the fbm with moving position so the clouds move
float density = cloudsFBM(pos * 0.01 * 0.021 + vec3(iTime * 0.04, 0.0, 0.0));
// calculate the radiance
float scattering = (h - CloudsFloor) / (CloudsCeil - CloudsFloor);
return vec2(density * mlt, scattering);
}
vec4 raymarchClouds(vec3 p1, vec3 p2, float randomValue){
// non constant step size, directly coupled to distance between points
// this is required to make in cloud rendering right
// also makes coverage calculation consistent with different distances raymarched
float stepsize = STEP_SIZE_SCALE / distance(p1, p2);
// coverage is inverted to have less calculations inside the loop
float coverageinv = 1.0;
vec3 color = vec3(0.0);
// start of the raymarching position is nudged by a bit with a random value to make
// the sampling better
float iter = randomValue * stepsize;
while(iter < 1.0 && coverageinv > 0.0){
vec2 density = cloudsDensity3D(mix(p1, p2, iter));
// final coverage at point is calculated here
float clouds = smoothstep(COVERAGE_START, COVERAGE_END, clamp(density.x, 0.0, 1.0));
// adjust the color taking into account the coverage left, this is basically alpha blending
color += clouds * max(0.0, coverageinv) * density.y;//vec3(pow(density.y * 1.0, 2.0));
// add coverage by subtracting from the inverted coverage, and subtract a bit more for fog rendering
coverageinv -= clouds + 0.001;
// variable next step size
// if density is 0 then step should be larger to skip not interesting areas
// if density is higher then step lower to sample the interesting areas more
iter += stepsize * 0.1 + stepsize * 2.0 * max(0.0, 0.2 - density.x);
}
float coverage = 1.0 - clamp(coverageinv, 0.0, 1.0);
return vec4(pow(color, vec3(2.0)) * 20.0, coverage);
}
// very native rendering for the ground, shadow is basically clouds sampled directly above
// this doesnt look that great, but at least look like something...
// by adjusting the direction and smoothstepping the coverage, really nice sun shadows can be achieved
// but with so high clouds coverage no light will peek through so its done like that here
vec3 renderGround(vec3 point, float dist, float random){
float shadow = raymarchClouds(
point + vec3(0.0, CloudsFloor, 0.0),
point + vec3(0.0, CloudsCeil, 0.0),
random
).x;
vec3 color = vec3(0.2, 0.2, 0.2) * vec3(0.8 + 0.2 * shadow);
float fogIntensity = 1.0 - 1.0 / (0.001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Straightforward, render raymarch, apply fog, alpha blend with the background, return
vec3 renderClouds(vec3 pointStart, vec3 pointEnd, vec3 background, float dist, float random){
vec4 clouds = raymarchClouds(
pointStart,
pointEnd,
random
);
vec3 color = mix(background, clouds.xyz, clouds.a);
float fogIntensity = 1.0 - 1.0 / (0.0001 * dist);
return mix(color, FOG_COLOR, clamp(fogIntensity, 0.0, 1.0));
}
// Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) {
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
// Main
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// get the ray direction
vec3 dir = getRay(fragCoord);
vec2 uv = fragCoord.xy / iResolution.xy;
// sky color as the default initial color
vec3 C = vec3(0.5, 0.7, 0.8);
// random for clouds raymarching
float random = fract(rand2dTime(uv));
// define clouds height and planet ground
Sphere sphereCeilLayer = Sphere(vec3(0), planetradius + CloudsCeil);
Sphere sphereFloorLayer = Sphere(vec3(0), planetradius + CloudsFloor);
Sphere sphereGround = Sphere(vec3(0), planetradius);
// define camera origin relative to surface
vec3 origin = vec3(100.0, CAMERA_HEIGHT, 100.0);
// define camera origin relative to the planet
vec3 atmorg = vec3(0.0, planetradius, 0.0) + origin;
Ray ray = Ray(atmorg, dir);
// adjust the ray hit detection if above the clouds, this could be done better
if(origin.y >= CloudsCeil){
hitLimit = higherHitLimit;
}
// Intersections
float hitceil = raySphereIntersect(ray, sphereCeilLayer);
float hitfloor = raySphereIntersect(ray, sphereFloorLayer);
float hitGround = raySphereIntersect(ray, sphereGround);
if(origin.y < CloudsFloor){
// below clouds
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
C = renderGround(groundHitPoint, hitGround, random);
} else {
vec3 cloudsPointStart = atmorg + (dir * min(hitfloor, hitceil));
vec3 cloudsPointEnd = atmorg + (dir * max(hitfloor, hitceil));
C = renderClouds(cloudsPointStart, cloudsPointEnd, C, min(hitfloor, hitceil), random);
}
} else if(origin.y >= CloudsFloor && origin.y < CloudsCeil){
// inside the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
vec3 cloudsPointStart = atmorg;
float targetDistance = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistance = max(hitfloor, hitceil);
} else if(isHitBoolean(hitfloor)){
targetDistance = hitfloor;
} else if(isHitBoolean(hitceil)){
targetDistance = hitceil;
}
vec3 cloudsPointEnd = atmorg + (dir * targetDistance);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
} else if(origin.y >= CloudsCeil){
// above the clouds
vec3 background = C;
if(isHitBoolean(hitGround)){
vec3 groundHitPoint = atmorg + (dir * hitGround);
background = renderGround(groundHitPoint, hitGround, random);
}
float targetDistanceStart = 0.0;
float targetDistanceEnd = 0.0;
if(isHitBoolean(hitfloor) && isHitBoolean(hitceil)){
targetDistanceStart = hitceil;
targetDistanceEnd = hitfloor;
} else if(isHitBoolean(hitceil)){
raySphereIntersect(ray, sphereCeilLayer);
targetDistanceStart = minhit;
targetDistanceEnd = maxhit;
}
if(isHitBoolean(targetDistanceStart) && isHitBoolean(targetDistanceEnd)){
vec3 cloudsPointStart = atmorg + (dir * targetDistanceStart);
vec3 cloudsPointEnd = atmorg + (dir * targetDistanceEnd);
C = renderClouds(cloudsPointStart, cloudsPointEnd, background, 0.0, random);
}
}
// adjust exposure, tonemap and return
fragColor = vec4( aces_tonemap(C * EXPOSURE * vec3(1.0, 0.9, 0.8)),1.0);
} | mit | [
9097,
9187,
9218,
9218,
9649
] | [
[
993,
1070,
1130,
1130,
1523
],
[
1525,
1592,
1621,
1621,
2317
],
[
2319,
2340,
2366,
2366,
2454
],
[
2456,
2479,
2500,
2500,
2569
],
[
2571,
2589,
2611,
2611,
3773
],
[
3775,
3807,
3834,
3834,
3915
],
[
4050,
4050,
4106,
4106,
4601
],
[
4801,
4870,
4894,
4894,
5105
],
[
5145,
5145,
5180,
5180,
5219
],
[
5221,
5393,
5424,
5424,
6194
],
[
6201,
6201,
6258,
6476,
7842
],
[
7849,
8201,
8257,
8257,
8618
],
[
8625,
8713,
8806,
8806,
9094
],
[
9097,
9187,
9218,
9218,
9649
],
[
9651,
9659,
9714,
9743,
13366
]
] | // Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
| vec3 aces_tonemap(vec3 color) { |
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
} | // Great tonemapping function from my other shader: https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color) { | 1 | 2 |