Skip to content

Commit edb37d0

Browse files
committed
cd SkiaSimpleShaderViewer git archive --prefix=shaders/ v1.5:shaders/ | (cd ~/my-git/skia-python-examples && tar -xpvf - ) cd ~/my-git/skia-python-examples cd shaders git add *.sksl git commit . Excluding screenshots.
1 parent c5d4e00 commit edb37d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+6748
-0
lines changed

shaders/2D Clouds.sksl

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
uniform float3 iResolution; // Viewport resolution (pixels)
2+
uniform float iTime; // Shader playback time (s)
3+
4+
// 2D Clouds
5+
// Created by drift in 2016-11-13
6+
// https://www.shadertoy.com/view/4tdSWr
7+
8+
const float cloudscale = 1.1;
9+
const float speed = 0.03;
10+
const float clouddark = 0.5;
11+
const float cloudlight = 0.3;
12+
const float cloudcover = 0.2;
13+
const float cloudalpha = 8.0;
14+
const float skytint = 0.5;
15+
const vec3 skycolour1 = vec3(0.2, 0.4, 0.6);
16+
const vec3 skycolour2 = vec3(0.4, 0.7, 1.0);
17+
18+
const mat2 m = mat2( 1.6, 1.2, -1.2, 1.6 );
19+
20+
vec2 hash( vec2 p ) {
21+
p = vec2(dot(p,vec2(127.1,311.7)), dot(p,vec2(269.5,183.3)));
22+
return -1.0 + 2.0*fract(sin(p)*43758.5453123);
23+
}
24+
25+
float noise( in vec2 p ) {
26+
const float K1 = 0.366025404; // (sqrt(3)-1)/2;
27+
const float K2 = 0.211324865; // (3-sqrt(3))/6;
28+
vec2 i = floor(p + (p.x+p.y)*K1);
29+
vec2 a = p - i + (i.x+i.y)*K2;
30+
vec2 o = (a.x>a.y) ? vec2(1.0,0.0) : vec2(0.0,1.0); //vec2 of = 0.5 + 0.5*vec2(sign(a.x-a.y), sign(a.y-a.x));
31+
vec2 b = a - o + K2;
32+
vec2 c = a - 1.0 + 2.0*K2;
33+
vec3 h = max(0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 );
34+
vec3 n = h*h*h*h*vec3( dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1.0)));
35+
return dot(n, vec3(70.0));
36+
}
37+
38+
float fbm(vec2 n) {
39+
float total = 0.0, amplitude = 0.1;
40+
for (int i = 0; i < 7; i++) {
41+
total += noise(n) * amplitude;
42+
n = m * n;
43+
amplitude *= 0.4;
44+
}
45+
return total;
46+
}
47+
48+
// -----------------------------------------------
49+
50+
vec4 main( in vec2 fragCoord ) {
51+
vec2 p = fragCoord.xy / iResolution.xy;
52+
vec2 uv = p*vec2(iResolution.x/iResolution.y,1.0);
53+
float time = iTime * speed;
54+
float q = fbm(uv * cloudscale * 0.5);
55+
56+
//ridged noise shape
57+
float r = 0.0;
58+
uv *= cloudscale;
59+
uv -= q - time;
60+
float weight = 0.8;
61+
for (int i=0; i<8; i++){
62+
r += abs(weight*noise( uv ));
63+
uv = m*uv + time;
64+
weight *= 0.7;
65+
}
66+
67+
//noise shape
68+
float f = 0.0;
69+
uv = p*vec2(iResolution.x/iResolution.y,1.0);
70+
uv *= cloudscale;
71+
uv -= q - time;
72+
weight = 0.7;
73+
for (int i=0; i<8; i++){
74+
f += weight*noise( uv );
75+
uv = m*uv + time;
76+
weight *= 0.6;
77+
}
78+
79+
f *= r + f;
80+
81+
//noise colour
82+
float c = 0.0;
83+
time = iTime * speed * 2.0;
84+
uv = p*vec2(iResolution.x/iResolution.y,1.0);
85+
uv *= cloudscale*2.0;
86+
uv -= q - time;
87+
weight = 0.4;
88+
for (int i=0; i<7; i++){
89+
c += weight*noise( uv );
90+
uv = m*uv + time;
91+
weight *= 0.6;
92+
}
93+
94+
//noise ridge colour
95+
float c1 = 0.0;
96+
time = iTime * speed * 3.0;
97+
uv = p*vec2(iResolution.x/iResolution.y,1.0);
98+
uv *= cloudscale*3.0;
99+
uv -= q - time;
100+
weight = 0.4;
101+
for (int i=0; i<7; i++){
102+
c1 += abs(weight*noise( uv ));
103+
uv = m*uv + time;
104+
weight *= 0.6;
105+
}
106+
107+
c += c1;
108+
109+
vec3 skycolour = mix(skycolour2, skycolour1, p.y);
110+
vec3 cloudcolour = vec3(1.1, 1.1, 0.9) * clamp((clouddark + cloudlight*c), 0.0, 1.0);
111+
112+
f = cloudcover + cloudalpha*f*r;
113+
114+
vec3 result = mix(skycolour, clamp(skytint * skycolour + cloudcolour, 0.0, 1.0), clamp(f + c, 0.0, 1.0));
115+
116+
return( vec4( result, 1.0 ));
117+
}

shaders/Analytic Motionblur 2D.sksl

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
uniform float2 iResolution;
2+
uniform float iTime;
3+
4+
// The MIT License
5+
// Copyright ? 2014 Inigo Quilez
6+
// 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.
7+
8+
// Original: https://www.shadertoy.com/view/MdSGDm
9+
10+
// Analytic motion blur, for 2D spheres (disks).
11+
//
12+
// (Linearly) Moving Disk - pixel/ray overlap test. The resulting
13+
// equation is a quadratic that can be solved to compute time coverage
14+
// of the swept disk behind the pixel over the aperture of the camera
15+
// (a full frame at 24 hz in this test).
16+
17+
18+
19+
// draw a disk with motion blur
20+
vec3 diskWithMotionBlur( in vec3 pcol, // pixel color (background)
21+
in vec2 puv, // pixel coordinates
22+
in vec3 dpr, // disk (pos,rad)
23+
in vec2 dv, // disk velocity
24+
in vec3 dcol ) // disk color
25+
{
26+
vec2 xc = puv - dpr.xy;
27+
float a = dot(dv,dv);
28+
float b = dot(dv,xc);
29+
float c = dot(xc,xc) - dpr.z*dpr.z;
30+
float h = b*b - a*c;
31+
if( h>0.0 )
32+
{
33+
h = sqrt( h );
34+
35+
float ta = max( 0.0, (-b-h)/a );
36+
float tb = min( 1.0, (-b+h)/a );
37+
38+
if( ta < tb ) // we can comment this conditional, in fact
39+
pcol = mix( pcol, dcol, clamp(2.0*(tb-ta),0.0,1.0) );
40+
}
41+
42+
return pcol;
43+
}
44+
45+
46+
vec3 hash3( float n ) { return fract(sin(vec3(n,n+1.0,n+2.0))*43758.5453123); }
47+
vec4 hash4( float n ) { return fract(sin(vec4(n,n+1.0,n+2.0,n+3.0))*43758.5453123); }
48+
49+
const float speed = 8.0;
50+
vec2 getPosition( float time, vec4 id ) { return vec2( 0.9*sin((speed*(0.75+0.5*id.z))*time+20.0*id.x), 0.75*cos(speed*(0.75+0.5*id.w)*time+20.0*id.y) ); }
51+
vec2 getVelocity( float time, vec4 id ) { return vec2( speed*0.9*cos((speed*(0.75+0.5*id.z))*time+20.0*id.x), -speed*0.75*sin(speed*(0.75+0.5*id.w)*time+20.0*id.y) ); }
52+
53+
vec4 main( vec2 fragCoord )
54+
{
55+
vec2 p = (2.0*fragCoord-iResolution.xy) / iResolution.y;
56+
57+
vec3 col = vec3(0.03) + 0.015*p.y;
58+
59+
for( int i=0; i<16; i++ )
60+
{
61+
vec4 off = hash4( float(i)*13.13 );
62+
vec3 sph = vec3( getPosition( iTime, off ), 0.02+0.1*off.x );
63+
vec2 dv = getVelocity( iTime, off ) /24.0 ;
64+
vec3 sphcol = 0.55 + 0.45*sin( 3.0*off.z + vec3(4.0,0.0,2.0) );
65+
66+
col = diskWithMotionBlur( col, p, sph, dv, sphcol );
67+
}
68+
69+
col = pow( col, vec3(0.4545) );
70+
71+
col += (1.0/255.0)*hash3(p.x+13.0*p.y);
72+
73+
return(vec4(col,1.0));
74+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
uniform float2 iResolution;
2+
uniform float iTime;
3+
4+
// Written by GLtracy
5+
// Original: https://www.shadertoy.com/view/lslXDr
6+
7+
// math const
8+
const float PI = 3.14159265359;
9+
const float MAX = 10000.0;
10+
11+
// ray intersects sphere
12+
// e = -b +/- sqrt( b^2 - c )
13+
vec2 ray_vs_sphere( vec3 p, vec3 dir, float r ) {
14+
float b = dot( p, dir );
15+
float c = dot( p, p ) - r * r;
16+
17+
float d = b * b - c;
18+
if ( d < 0.0 ) {
19+
return vec2( MAX, -MAX );
20+
}
21+
d = sqrt( d );
22+
23+
return vec2( -b - d, -b + d );
24+
}
25+
26+
// Mie
27+
// g : ( -0.75, -0.999 )
28+
// 3 * ( 1 - g^2 ) 1 + c^2
29+
// F = ----------------- * -------------------------------
30+
// 8pi * ( 2 + g^2 ) ( 1 + g^2 - 2 * g * c )^(3/2)
31+
float phase_mie( float g, float c, float cc ) {
32+
float gg = g * g;
33+
34+
float a = ( 1.0 - gg ) * ( 1.0 + cc );
35+
36+
float b = 1.0 + gg - 2.0 * g * c;
37+
b *= sqrt( b );
38+
b *= 2.0 + gg;
39+
40+
return ( 3.0 / 8.0 / PI ) * a / b;
41+
}
42+
43+
// Rayleigh
44+
// g : 0
45+
// F = 3/16PI * ( 1 + c^2 )
46+
float phase_ray( float cc ) {
47+
return ( 3.0 / 16.0 / PI ) * ( 1.0 + cc );
48+
}
49+
50+
// scatter const
51+
const float R_INNER = 1.0;
52+
const float R = R_INNER + 0.5;
53+
54+
const int NUM_OUT_SCATTER = 8;
55+
const int NUM_IN_SCATTER = 80;
56+
57+
float density( vec3 p, float ph ) {
58+
return exp( -max( length( p ) - R_INNER, 0.0 ) / ph );
59+
}
60+
61+
float optic( vec3 p, vec3 q, float ph ) {
62+
vec3 s = ( q - p ) / float( NUM_OUT_SCATTER );
63+
vec3 v = p + s * 0.5;
64+
65+
float sum = 0.0;
66+
for ( int i = 0; i < NUM_OUT_SCATTER; i++ ) {
67+
sum += density( v, ph );
68+
v += s;
69+
}
70+
sum *= length( s );
71+
72+
return sum;
73+
}
74+
75+
vec3 in_scatter( vec3 o, vec3 dir, vec2 e, vec3 l ) {
76+
const float ph_ray = 0.05;
77+
const float ph_mie = 0.02;
78+
79+
const vec3 k_ray = vec3( 3.8, 13.5, 33.1 );
80+
const vec3 k_mie = vec3( 21.0 );
81+
const float k_mie_ex = 1.1;
82+
83+
vec3 sum_ray = vec3( 0.0 );
84+
vec3 sum_mie = vec3( 0.0 );
85+
86+
float n_ray0 = 0.0;
87+
float n_mie0 = 0.0;
88+
89+
float len = ( e.y - e.x ) / float( NUM_IN_SCATTER );
90+
vec3 s = dir * len;
91+
vec3 v = o + dir * ( e.x + len * 0.5 );
92+
93+
for ( int i = 0; i < NUM_IN_SCATTER; i++ ) {
94+
v += s;
95+
float d_ray = density( v, ph_ray ) * len;
96+
float d_mie = density( v, ph_mie ) * len;
97+
98+
n_ray0 += d_ray;
99+
n_mie0 += d_mie;
100+
101+
vec2 f = ray_vs_sphere( v, l, R );
102+
vec3 u = v + l * f.y;
103+
104+
float n_ray1 = optic( v, u, ph_ray );
105+
float n_mie1 = optic( v, u, ph_mie );
106+
107+
vec3 att = exp( - ( n_ray0 + n_ray1 ) * k_ray - ( n_mie0 + n_mie1 ) * k_mie * k_mie_ex );
108+
109+
sum_ray += d_ray * att;
110+
sum_mie += d_mie * att;
111+
}
112+
113+
float c = dot( dir, -l );
114+
float cc = c * c;
115+
vec3 scatter =
116+
sum_ray * k_ray * phase_ray( cc ) +
117+
sum_mie * k_mie * phase_mie( -0.78, c, cc );
118+
119+
120+
return 10.0 * scatter;
121+
}
122+
123+
// angle : pitch, yaw
124+
mat3 rot3xy( vec2 angle ) {
125+
vec2 c = cos( angle );
126+
vec2 s = sin( angle );
127+
128+
return mat3(
129+
c.y , 0.0, -s.y,
130+
s.y * s.x, c.x, c.y * s.x,
131+
s.y * c.x, -s.x, c.y * c.x
132+
);
133+
}
134+
135+
// ray direction
136+
vec3 ray_dir( float fov, vec2 size, vec2 pos ) {
137+
vec2 xy = pos - size * 0.5;
138+
139+
float cot_half_fov = tan( radians( 90.0 - fov * 0.5 ) );
140+
float z = size.y * 0.5 * cot_half_fov;
141+
142+
return normalize( vec3( xy, -z ) );
143+
}
144+
145+
vec4 main( in vec2 fragCoord )
146+
{
147+
// default ray dir
148+
vec3 dir = ray_dir( 45.0, iResolution.xy, fragCoord.xy );
149+
150+
// default ray origin
151+
vec3 eye = vec3( 0.0, 0.0, 3.0 );
152+
153+
// rotate camera
154+
mat3 rot = rot3xy( vec2( 0.0, iTime * 0.5 ) );
155+
dir = rot * dir;
156+
eye = rot * eye;
157+
158+
// sun light dir
159+
vec3 l = vec3( 0.0, 0.0, 1.0 );
160+
161+
vec2 e = ray_vs_sphere( eye, dir, R );
162+
if ( e.x > e.y ) {
163+
return( vec4( 0.0, 0.0, 0.0, 1.0 ));
164+
}
165+
166+
vec2 f = ray_vs_sphere( eye, dir, R_INNER );
167+
e.y = min( e.y, f.x );
168+
169+
vec3 I = in_scatter( eye, dir, e, l );
170+
171+
return(vec4( pow( I, vec3( 1.0 / 2.2 ) ), 1.0 ));
172+
}

0 commit comments

Comments
 (0)