Skip to content

Commit

Permalink
add shadertoy plot type
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Oct 21, 2024
1 parent aa889ab commit 8559986
Show file tree
Hide file tree
Showing 13 changed files with 750 additions and 3 deletions.
13 changes: 13 additions & 0 deletions GLMakie/assets/shader/shadertoy.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{GLSL_VERSION}}

out vec4 fragment_color;

{{SHADERTOY_INPUTS}}
{{TOY_SHADER}}

in vec2 f_uv;

void main()
{
mainImage(fragment_color, f_uv * iResolution.xy);
}
11 changes: 11 additions & 0 deletions GLMakie/assets/shader/shadertoy.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{GLSL_VERSION}}

in vec2 vertices;
in vec2 uv;
out vec2 f_uv;
uniform mat4 projection, view, model;

void main(){
gl_Position = projection * view * vec4(vertices, 0, 1);
f_uv = uv;
}
122 changes: 122 additions & 0 deletions GLMakie/experiments/clouds.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
float noise(in vec3 x) {
vec3 p = floor(x);
vec3 f = fract(x);
f = f * f * (3.0 - 2.0 * f);
vec2 uv = (p.xy + vec2(37.0, 17.0) * p.z) + f.xy;
vec2 rg = texture(iChannel0, (uv + 0.5) / 256.0, -100).yx;
return -1.0 + 2.0 * mix(rg.x, rg.y, f.z);
}

float map5(in vec3 p) {
vec3 q = p - vec3(0.0, 0.1, 1.0) * iGlobalTime;
float f;
f = 0.50000 * noise(q);
q = q * 2.02;
f += 0.25000 * noise(q);
q = q * 2.03;
f += 0.12500 * noise(q);
q = q * 2.01;
f += 0.06250 * noise(q);
q = q * 2.02;
f += 0.03125 * noise(q);
return clamp(1.5 - p.y - 2.0 + 1.75 * f, 0.0, 1.0);
}

float map4(in vec3 p) {
vec3 q = p - vec3(0.0, 0.1, 1.0) * iGlobalTime;
float f;
f = 0.50000 * noise(q);
q = q * 2.02;
f += 0.25000 * noise(q);
q = q * 2.03;
f += 0.12500 * noise(q);
q = q * 2.01;
f += 0.06250 * noise(q);
return clamp(1.5 - p.y - 2.0 + 1.75 * f, 0.0, 1.0);
}
float map3(in vec3 p) {
vec3 q = p - vec3(0.0, 0.1, 1.0) * iGlobalTime;
float f;
f = 0.50000 * noise(q);
q = q * 2.02;
f += 0.25000 * noise(q);
q = q * 2.03;
f += 0.12500 * noise(q);
return clamp(1.5 - p.y - 2.0 + 1.75 * f, 0.0, 1.0);
}
float map2(in vec3 p) {
vec3 q = p - vec3(0.0, 0.1, 1.0) * iGlobalTime;
float f;
f = 0.50000 * noise(q);
q = q * 2.02;
f += 0.25000 * noise(q);
;
return clamp(1.5 - p.y - 2.0 + 1.75 * f, 0.0, 1.0);
}

vec3 sundir = normalize(vec3(-1.0, 0.0, -1.0));

vec4 integrate(in vec4 sum, in float dif, in float den, in vec3 bgcol, in float t) {
// lighting
vec3 lin = vec3(0.65, 0.68, 0.7) * 1.3 + 0.5 * vec3(0.7, 0.5, 0.3) * dif;
vec4 col = vec4(mix(1.15 * vec3(1.0, 0.95, 0.8), vec3(0.65), den), den);
col.xyz *= lin;
col.xyz = mix(col.xyz, bgcol, 1.0 - exp(-0.003 * t * t));
// front to back blending
col.a *= 0.4;
col.rgb *= col.a;
return sum + col * (1.0 - sum.a);
}

#define MARCH(STEPS,MAPLOD) for(int i=0; i<STEPS; i++) { vec3 pos = ro + t*rd; if( pos.y<-3.0 || pos.y>2.0 || sum.a > 0.99 ) break; float den = MAPLOD( pos ); if( den>0.01 ) { float dif = clamp((den - MAPLOD(pos+0.3*sundir))/0.6, 0.0, 1.0 ); sum = integrate( sum, dif, den, bgcol, t ); } t += max(0.1,0.02*t); }

vec4 raymarch(in vec3 ro, in vec3 rd, in vec3 bgcol) {
vec4 sum = vec4(0.0);

float t = 0.0;

MARCH(30, map5);
MARCH(30, map4);
MARCH(30, map3);
MARCH(30, map2);

return clamp(sum, 0.0, 1.0);
}

mat3 setCamera(in vec3 ro, in vec3 ta, float cr) {
vec3 cw = normalize(ta - ro);
vec3 cp = vec3(sin(cr), cos(cr), 0.0);
vec3 cu = normalize(cross(cw, cp));
vec3 cv = normalize(cross(cu, cw));
return mat3(cu, cv, cw);
}

vec4 render(in vec3 ro, in vec3 rd) {
// background sky
float sun = clamp(dot(sundir, rd), 0.0, 1.0);
vec3 col = vec3(0.6, 0.71, 0.75) - rd.y * 0.2 * vec3(1.0, 0.5, 1.0) + 0.15 * 0.5;
col += 0.2 * vec3(1.0, .6, 0.1) * pow(sun, 8.0);

// clouds
vec4 res = raymarch(ro, rd, col);
col = col * (1.0 - res.w) + res.xyz;

// sun glare
col += 0.1 * vec3(1.0, 0.4, 0.2) * pow(sun, 3.0);

return vec4(col, 1.0);
}

void mainImage(in vec2 fragCoord) {
vec2 p = (-iResolution.xy + 2.0 * gl_FragCoord.xy) / iResolution.y;
vec2 m = iMouse.xy / iResolution.xy;

// camera
vec3 ro = 4.0 * normalize(vec3(sin(3.0 * m.x), 0.4 * m.y, cos(3.0 * m.x)));
vec3 ta = vec3(0.0, -1.0, 0.0);
mat3 ca = setCamera(ro, ta, 0.0);
// ray
vec3 rd = ca * normalize(vec3(p.xy, 1.5));

fragment_color = render(ro, rd);
}
217 changes: 217 additions & 0 deletions GLMakie/experiments/monster.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// Created by inigo quilez - iq/2013
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

vec3 hash3( float n )
{
return fract(sin(vec3(n,n+1.0,n+2.0))*vec3(13.5453123,31.1459123,37.3490423));
}

vec3 noise( in float x )
{
float p = floor(x);
float f = fract(x);
f = f*f*(3.0-2.0*f);
return mix( hash3(p+0.0), hash3(p+1.0),f);
}


mat4 rotationMat( in vec3 xyz )
{
vec3 si = sin(xyz);
vec3 co = cos(xyz);

return mat4( co.y*co.z, co.y*si.z, -si.y, 0.0,
si.x*si.y*co.z-co.x*si.z, si.x*si.y*si.z+co.x*co.z, si.x*co.y, 0.0,
co.x*si.y*co.z+si.x*si.z, co.x*si.y*si.z-si.x*co.z, co.x*co.y, 0.0,
0.0, 0.0, 0.0, 1.0 );
}

const float s = 1.1;

mat4 mm;

vec3 map( vec3 p )
{
float k = 1.0;
float m = 1e10;
for( int i=0; i<22; i++ )
{
m = min( m, dot(p,p)/(k*k) );
p = (mm*vec4((abs(p)),1.0)).xyz;
k*= s;
}


float d = (length(p)-0.25)/k;

float h = p.z - 0.35*p.x;

return vec3( d, m, h );
}

vec3 intersect( in vec3 ro, in vec3 rd )
{
float t = 0.0;
for( int i=0; i<100; i++ )
{
vec3 res = map( ro+rd*t );
if( res.x<0.0002 ) return vec3(t,res.yz);
t += res.x;
if( t>9.0 ) break;
}

return vec3( -1.0 );
}

vec3 calcNormal( in vec3 pos, float e )
{
vec3 eps = vec3(e,0.0,0.0);

return normalize( vec3(
map(pos+eps.xyy).x - map(pos-eps.xyy).x,
map(pos+eps.yxy).x - map(pos-eps.yxy).x,
map(pos+eps.yyx).x - map(pos-eps.yyx).x ) );
}

float softshadow( in vec3 ro, in vec3 rd, float mint, float k )
{
float res = 1.0;
float t = mint;
for( int i=0; i<32; i++ )
{
float h = map(ro + rd*t).x;
h = max( h, 0.0 );
res = min( res, k*h/t );
t += clamp( h, 0.001, 0.1 );
if( res<0.01 || t>6.0 ) break;
}
return clamp(res,0.0,1.0);
}

float calcAO( in vec3 pos, in vec3 nor )
{
float totao = 0.0;
for( int aoi=0; aoi<16; aoi++ )
{
vec3 aopos = -1.0+2.0*hash3(float(aoi)*213.47);
aopos *= sign( dot(aopos,nor) );
aopos = pos + nor*0.01 + aopos*0.04;
float dd = clamp( map( aopos ).x*4.0, 0.0, 1.0 );
totao += dd;
}
totao /= 16.0;

return clamp( totao*totao*50.0, 0.0, 1.0 );
}

mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
{
vec3 cw = normalize(ta-ro);
vec3 cp = vec3(sin(cr), cos(cr),0.0);
vec3 cu = normalize( cross(cw,cp) );
vec3 cv = normalize( cross(cu,cw) );
return mat3( cu, cv, cw );
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 q = fragCoord.xy / iResolution.xy;
vec2 p = -1.0 + 2.0 * q;
p.x *= iResolution.x/iResolution.y;
vec2 m = vec2(0.5);
m = iMouse.xy/iResolution.xy;

// animation
float time = iGlobalTime;
time += 15.0*smoothstep( 15.0, 25.0, iGlobalTime );
time += 20.0*smoothstep( 65.0, 80.0, iGlobalTime );
time += 35.0*smoothstep( 105.0, 135.0, iGlobalTime );
time += 20.0*smoothstep( 165.0, 180.0, iGlobalTime );
time += 40.0*smoothstep( 220.0, 290.0, iGlobalTime );
time += 5.0*smoothstep( 320.0, 330.0, iGlobalTime );
float time1 = (time-10.0)*1.5 - 167.0;
float time2 = time;

mm = rotationMat( vec3(0.4,0.1,3.4) +
0.15*sin(0.1*vec3(0.40,0.30,0.61)*time1) +
0.15*sin(0.1*vec3(0.11,0.53,0.48)*time1));
mm[0].xyz *= s;
mm[1].xyz *= s;
mm[2].xyz *= s;
mm[3].xyz = vec3( 0.15, 0.05, -0.07 ) + 0.05*sin(vec3(0.0,1.0,2.0) + 0.2*vec3(0.31,0.24,0.42)*time1);

// camera
float an = 1.0 + 0.1*time2 - 6.2*m.x;
float cr = 0.15*sin(0.2*time2);
vec3 ro = (2.4 + 0.6*smoothstep(10.0,20.0,time2))*vec3(sin(an),0.25,cos(an));
vec3 ta = vec3( 0.0, 0.0 + 0.13*cos(0.3*time2), 0.0 );
ta += 0.05*noise( 0.0 + 1.0*time );
ro += 0.05*noise( 11.3 + 1.0*time );
// camera-to-world transformation
mat3 ca = setCamera( ro, ta, cr );
// ray direction
vec3 rd = ca * normalize( vec3(p.xy,3.0) );

// raymarch
vec3 tmat = intersect(ro,rd);

// shade
vec3 col = vec3(0.0);
vec4 bg = vec4(0.7,0.5,0.7, 1.0);
if( tmat.z>-0.5 )
{
// geometry
vec3 pos = ro + tmat.x*rd;
vec3 nor = calcNormal(pos, 0.005);
vec3 sor = calcNormal(pos, 0.010);

// material
vec3 mate = vec3(1.0);
mate = mix( vec3(0.5,0.5,0.2), vec3(0.5,0.3,0.0), 0.5 + 0.5*sin(4.0+8000.0*tmat.y) );
mate = mix( vec3(1.0,0.9,0.8), mate, 0.5 + 0.5*sin(4.0+20.0*tmat.z) );
mate.x *= 1.15;

// lighting
float occ = 1.1*calcAO( pos, nor );
occ *= 0.75 + 0.25*clamp(tmat.y*400.0,0.0,1.0);

// diffuse
col = vec3(0.0);
for( int i=0; i<32; i++ )
{
//vec3 rr = normalize(-1.0 + 2.0*texture2D( iChannel2, vec2((0.5+float(i)),0.5)/256.0,-100.0).xyz);
vec3 rr = normalize(-1.0 + 2.0*hash3(float(i)*123.5463));
rr = normalize( nor + 7.0*rr );
rr = rr * sign(dot(nor,rr));
float ds = occ;//softshadow( pos, rr, 0.01, 32.0 );
col += pow( bg.xyz, vec3(2.2) ) * dot(rr,nor) * ds;
}
col /= 32.0;

col *= 1.8;

// subsurface
col *= 1.0 + 1.0*vec3(1.0,0.6,0.1)*pow(clamp(1.0+dot(rd,sor),0.0,1.0),2.0)*vec3(1.0);

// specular
float fre = pow( clamp(1.0+dot(rd,nor),0.0,1.0), 5.0 );
vec3 ref = reflect( rd, nor );
float rs = softshadow( pos, ref, 0.01, 32.0 );
col += 1.8 * (0.04 + 12.0*fre) * occ * pow( bg.xyz, vec3(2.0) ) * rs;

col *= mate;
}
else
{
// background
discard;
}

// gamma
col = pow( clamp( col, 0.0, 1.0 ), vec3(0.45) );

// vigneting
col *= 0.5 + 0.5*pow( 16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y), 0.1 );

fragColor = vec4( col, 1.0 );
}
Binary file added GLMakie/experiments/noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8559986

Please sign in to comment.