├── glsl ├── 4-uv-draw │ ├── uv.glsl │ ├── uv-blur.glsl │ ├── uv-glow.glsl │ ├── uv-circle.glsl │ ├── uv-sdf.glsl │ ├── uv-polar.glsl │ ├── uv-sdf-alteration.glsl │ ├── uv-repeat-pattern.glsl │ ├── uv-mix.glsl │ ├── uv-transform.glsl │ └── uv-sdf-bool.glsl ├── 5-texture │ ├── texture-load.glsl │ ├── texture-distort.glsl │ └── texture-transition.glsl ├── 7-postprocessing │ ├── tint.glsl │ ├── pixelate.glsl │ ├── vignette.glsl │ ├── bulge.glsl │ └── rgb-shift.glsl ├── 6-random │ ├── random.glsl │ ├── random-grain.glsl │ ├── noise.glsl │ ├── noise-transition.glsl │ ├── fbm.glsl │ └── fbm-dissolve.glsl └── 2-first-shader │ └── first-shader.glsl ├── html ├── 4-uv-draw │ ├── uv.html │ ├── uv-blur.html │ ├── uv-glow.html │ ├── uv-circle.html │ ├── uv-sdf.html │ ├── uv-polar.html │ ├── uv-sdf-alteration.html │ ├── uv-repeat-pattern.html │ ├── uv-mix.html │ ├── uv-transform.html │ └── uv-sdf-bool.html ├── 5-texture │ ├── texture-load.html │ ├── texture-distort.html │ └── texture-transition.html ├── 7-postprocessing │ ├── tint.html │ ├── pixelate.html │ ├── vignette.html │ ├── bulge.html │ └── rgb-shift.html ├── 6-random │ ├── random.html │ ├── random-grain.html │ ├── noise.html │ ├── noise-transition.html │ ├── fbm.html │ └── fbm-dissolve.html ├── 2-first-shader │ └── first-shader.html ├── 8-vertex │ ├── vertex-shader-object.html │ ├── vertex-shader-points.html │ ├── vertex-shader-distort.html │ └── vertex-shader-attribute.html └── 9-lighting │ ├── lighting-phong.html │ ├── lighting-blinn-phong.html │ ├── lighting-ibl-specular.html │ ├── lighting-fresnel.html │ └── lighting-fix-normal.html └── bonus ├── shader-image-transition-ripple.glsl ├── shader-image-transition-mosaic.glsl ├── shader-simple-god-rays-effect.glsl ├── shader-image-transition-sdf-mask.glsl ├── shader-fbm-paper-texture.glsl └── shader-fbm-particle-transition.glsl /glsl/4-uv-draw/uv.glsl: -------------------------------------------------------------------------------- 1 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 2 | vec2 uv=fragCoord/iResolution.xy; 3 | // fragColor=vec4(uv.x,0.,0.,1.); 4 | // fragColor=vec4(0.,uv.y,0.,1.); 5 | fragColor=vec4(uv,0.,1.); 6 | } 7 | -------------------------------------------------------------------------------- /glsl/5-texture/texture-load.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/09/10/QozT59R6KsYmb3q.jpg" 2 | 3 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 4 | vec2 uv=fragCoord/iResolution.xy; 5 | vec3 tex=texture(iChannel0,uv).xyz; 6 | fragColor=vec4(tex,1.); 7 | } 8 | -------------------------------------------------------------------------------- /glsl/7-postprocessing/tint.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/09/10/QozT59R6KsYmb3q.jpg" 2 | 3 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 4 | vec2 uv=fragCoord/iResolution.xy; 5 | vec3 tex=texture(iChannel0,uv).xyz; 6 | 7 | vec3 col=tex; 8 | vec3 tintColor=vec3(.220,.380,.651); 9 | col*=tintColor; 10 | fragColor=vec4(col,1.); 11 | } 12 | -------------------------------------------------------------------------------- /glsl/4-uv-draw/uv-blur.glsl: -------------------------------------------------------------------------------- 1 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 2 | vec2 uv=fragCoord/iResolution.xy; 3 | uv=(uv-.5)*2.; 4 | uv.x*=iResolution.x/iResolution.y; 5 | 6 | float d=length(uv); 7 | d-=.5; 8 | // float c=step(0.,d); 9 | // float c=smoothstep(0.,.02,d); 10 | float c=smoothstep(0.,.2,d); 11 | fragColor=vec4(vec3(c),1.); 12 | } 13 | -------------------------------------------------------------------------------- /glsl/5-texture/texture-distort.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/09/10/QozT59R6KsYmb3q.jpg" 2 | 3 | vec2 distort(vec2 p){ 4 | p.x+=sin(p.y*10.+iTime)/50.; 5 | return p; 6 | } 7 | 8 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 9 | vec2 uv=fragCoord/iResolution.xy; 10 | uv=distort(uv); 11 | vec3 tex=texture(iChannel0,uv).xyz; 12 | fragColor=vec4(tex,1.); 13 | } 14 | -------------------------------------------------------------------------------- /glsl/4-uv-draw/uv-glow.glsl: -------------------------------------------------------------------------------- 1 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 2 | vec2 uv=fragCoord/iResolution.xy; 3 | uv=(uv-.5)*2.; 4 | uv.x*=iResolution.x/iResolution.y; 5 | 6 | float d=length(uv); 7 | // d-=.5; 8 | // float c=step(0.,d); 9 | // float c=smoothstep(0.,.02,d); 10 | float c=.25/d; 11 | c=pow(c,1.6); 12 | fragColor=vec4(vec3(c),1.); 13 | } 14 | -------------------------------------------------------------------------------- /glsl/6-random/random.glsl: -------------------------------------------------------------------------------- 1 | highp float random(vec2 co) 2 | { 3 | highp float a=12.9898; 4 | highp float b=78.233; 5 | highp float c=43758.5453; 6 | highp float dt=dot(co.xy,vec2(a,b)); 7 | highp float sn=mod(dt,3.14); 8 | return fract(sin(sn)*c); 9 | } 10 | 11 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 12 | vec2 uv=fragCoord/iResolution.xy; 13 | 14 | float noise=random(uv); 15 | // float noise=random(uv+iTime); 16 | 17 | vec3 col=vec3(noise); 18 | fragColor=vec4(col,1.); 19 | } 20 | -------------------------------------------------------------------------------- /glsl/4-uv-draw/uv-circle.glsl: -------------------------------------------------------------------------------- 1 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 2 | vec2 uv=fragCoord/iResolution.xy; 3 | uv=(uv-.5)*2.; 4 | uv.x*=iResolution.x/iResolution.y; 5 | float d=length(uv); 6 | d-=.5; 7 | // float c=0.; 8 | // if(d>0.){ 9 | // c=1.; 10 | // }else{ 11 | // c=0.; 12 | // } 13 | // float c=step(0.,d); 14 | float c=smoothstep(0.,.02,d); 15 | // fragColor=vec4(vec3(d),1.); 16 | fragColor=vec4(vec3(c),1.); 17 | // fragColor=vec4(uv,0.,1.); 18 | } 19 | -------------------------------------------------------------------------------- /glsl/4-uv-draw/uv-sdf.glsl: -------------------------------------------------------------------------------- 1 | float sdCircle(vec2 p,float r) 2 | { 3 | return length(p)-r; 4 | } 5 | 6 | float sdBox(in vec2 p,in vec2 b) 7 | { 8 | vec2 d=abs(p)-b; 9 | return length(max(d,0.))+min(max(d.x,d.y),0.); 10 | } 11 | 12 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 13 | vec2 uv=fragCoord/iResolution.xy; 14 | uv=(uv-.5)*2.; 15 | uv.x*=iResolution.x/iResolution.y; 16 | 17 | // float d=sdCircle(uv,.5); 18 | float d=sdBox(uv,vec2(.6,.3)); 19 | float c=smoothstep(0.,.02,d); 20 | fragColor=vec4(vec3(c),1.); 21 | } 22 | -------------------------------------------------------------------------------- /glsl/7-postprocessing/pixelate.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/09/10/QozT59R6KsYmb3q.jpg" 2 | 3 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 4 | vec2 uv=fragCoord/iResolution.xy; 5 | 6 | // uv 7 | // float c=uv.x; 8 | // c=floor(c*10.)/10.; 9 | // fragColor=vec4(vec3(c),1.); 10 | 11 | // pixelate 12 | vec2 size=vec2(50.,50.); 13 | // uv.x=floor(uv.x*size.x)/size.x; 14 | // uv.y=floor(uv.y*size.y)/size.y; 15 | uv=floor(uv*size)/size; 16 | vec3 tex=texture(iChannel0,uv).xyz; 17 | fragColor=vec4(tex,1.); 18 | } 19 | -------------------------------------------------------------------------------- /glsl/7-postprocessing/vignette.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/09/10/QozT59R6KsYmb3q.jpg" 2 | 3 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 4 | vec2 uv=fragCoord/iResolution.xy; 5 | vec3 tex=texture(iChannel0,uv).xyz; 6 | 7 | // vec3 col=vec3(1.); 8 | vec3 col=tex; 9 | vec2 p=uv; 10 | p-=.5; 11 | float d=length(p); 12 | // col=vec3(d); 13 | // float c=smoothstep(.4,.8,d); 14 | // float c=1.-smoothstep(.4,.8,d); 15 | float c=smoothstep(.8,.4,d); 16 | // col=vec3(c); 17 | col*=c; 18 | 19 | fragColor=vec4(col,1.); 20 | } 21 | -------------------------------------------------------------------------------- /glsl/2-first-shader/first-shader.glsl: -------------------------------------------------------------------------------- 1 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 2 | vec3 color1=vec3(1.,0.,1.); 3 | vec3 color2=vec3(1.,1.,0.); 4 | vec3 color3=vec3(0.,0.,1.); 5 | vec3 color4=vec3(1.,0.,0.); 6 | if(fragCoord.x=iResolution.x*.25&&fragCoord.x=iResolution.x*.5&&fragCoord.x 2 | body { 3 | margin: 0; 4 | background: black; 5 | } 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /glsl/6-random/random-grain.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/09/10/QozT59R6KsYmb3q.jpg" 2 | 3 | highp float random(vec2 co) 4 | { 5 | highp float a=12.9898; 6 | highp float b=78.233; 7 | highp float c=43758.5453; 8 | highp float dt=dot(co.xy,vec2(a,b)); 9 | highp float sn=mod(dt,3.14); 10 | return fract(sin(sn)*c); 11 | } 12 | 13 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 14 | vec2 uv=fragCoord/iResolution.xy; 15 | 16 | float noise=random(uv); 17 | // float noise=random(uv+iTime); 18 | 19 | vec3 col=texture(iChannel0,uv).xyz; 20 | col+=(noise-.5)*.2; 21 | fragColor=vec4(col,1.); 22 | } 23 | -------------------------------------------------------------------------------- /html/5-texture/texture-load.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /glsl/4-uv-draw/uv-polar.glsl: -------------------------------------------------------------------------------- 1 | vec2 cart2polar(vec2 uv){ 2 | float phi=atan(uv.y,uv.x); 3 | float r=length(uv); 4 | return vec2(phi,r); 5 | } 6 | 7 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 8 | vec2 uv=fragCoord/iResolution.xy; 9 | uv=(uv-.5)*2.; 10 | uv.x*=iResolution.x/iResolution.y; 11 | 12 | // float phi=atan(uv.y,uv.x); 13 | // float r=length(uv); 14 | // uv=vec2(phi,r); 15 | uv=cart2polar(uv); 16 | 17 | // uv 18 | // fragColor=vec4(uv,0.,1.); 19 | 20 | // radial 21 | // float c=sin(uv.x*12.); 22 | // fragColor=vec4(vec3(c),1.); 23 | 24 | // spiral 25 | float c=sin(uv.y*20.+uv.x); 26 | fragColor=vec4(vec3(c),1.); 27 | } 28 | -------------------------------------------------------------------------------- /glsl/7-postprocessing/bulge.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/09/10/QozT59R6KsYmb3q.jpg" 2 | 3 | vec2 bulge(vec2 p){ 4 | // vec2 center=vec2(.5); 5 | vec2 center=iMouse.xy/iResolution.xy; 6 | 7 | float radius=.9; 8 | float strength=1.1; 9 | 10 | p-=center; 11 | 12 | float d=length(p); 13 | d/=radius; 14 | float dPow=pow(d,2.); 15 | float dRev=strength/(dPow+1.); 16 | 17 | // p*=d; 18 | // p*=dPow; 19 | p*=dRev; 20 | 21 | p+=center; 22 | 23 | return p; 24 | } 25 | 26 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 27 | vec2 uv=fragCoord/iResolution.xy; 28 | uv=bulge(uv); 29 | vec3 tex=texture(iChannel0,uv).xyz; 30 | fragColor=vec4(tex,1.); 31 | } 32 | -------------------------------------------------------------------------------- /html/4-uv-draw/uv-blur.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /html/7-postprocessing/tint.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /html/4-uv-draw/uv-glow.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /html/5-texture/texture-distort.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /glsl/4-uv-draw/uv-sdf-alteration.glsl: -------------------------------------------------------------------------------- 1 | float sdCircle(vec2 p,float r) 2 | { 3 | return length(p)-r; 4 | } 5 | 6 | float sdBox(in vec2 p,in vec2 b) 7 | { 8 | vec2 d=abs(p)-b; 9 | return length(max(d,0.))+min(max(d.x,d.y),0.); 10 | } 11 | 12 | float opRound(in float d,in float r) 13 | { 14 | return d-r; 15 | } 16 | 17 | float opOnion(in float d,in float r) 18 | { 19 | return abs(d)-r; 20 | } 21 | 22 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 23 | vec2 uv=fragCoord/iResolution.xy; 24 | uv=(uv-.5)*2.; 25 | uv.x*=iResolution.x/iResolution.y; 26 | 27 | // float d=sdCircle(uv,.5); 28 | float d=sdBox(uv,vec2(.6,.3)); 29 | 30 | // round 31 | // d=opRound(d,.1); 32 | 33 | // onion 34 | d=opOnion(d,.1); 35 | 36 | float c=smoothstep(0.,.02,d); 37 | fragColor=vec4(vec3(c),1.); 38 | } 39 | -------------------------------------------------------------------------------- /html/6-random/random.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /html/4-uv-draw/uv-circle.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /glsl/4-uv-draw/uv-repeat-pattern.glsl: -------------------------------------------------------------------------------- 1 | float sdCircle(vec2 p,float r) 2 | { 3 | return length(p)-r; 4 | } 5 | 6 | float opUnion(float d1,float d2) 7 | { 8 | return min(d1,d2); 9 | } 10 | 11 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 12 | vec2 uv=fragCoord/iResolution.xy; 13 | 14 | // stripe 15 | // uv=fract(uv*16.); 16 | // vec3 c=vec3(step(.5,uv.x)); 17 | 18 | // wave 19 | // uv.y+=sin(uv.x*6.)*.4; 20 | // uv=fract(uv*16.); 21 | // vec3 c=vec3(step(.5,uv.y)); 22 | 23 | // grid 24 | // uv.x*=iResolution.x/iResolution.y; 25 | // uv=fract(uv*16.); 26 | // vec3 c=vec3(opUnion(step(.25,uv.x),step(.25,uv.y))); 27 | 28 | // ripple 29 | uv=(uv-.5)*2.; 30 | uv.x*=iResolution.x/iResolution.y; 31 | float d=sdCircle(uv,.5); 32 | d=sin(d*40.); 33 | float mask=smoothstep(0.,.02,d); 34 | vec3 c=vec3(mask); 35 | 36 | fragColor=vec4(c,1.); 37 | } 38 | -------------------------------------------------------------------------------- /bonus/shader-image-transition-ripple.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/03/20/5jmPqdJW7EL2a4X.jpg" 2 | #iChannel1"https://s2.loli.net/2023/03/20/AgKHd6cZkUE9rGO.jpg" 3 | 4 | vec4 getFromColor(vec2 uv){ 5 | return texture(iChannel0,uv); 6 | } 7 | 8 | vec4 getToColor(vec2 uv){ 9 | return texture(iChannel1,uv); 10 | } 11 | 12 | vec4 transition(vec2 uv){ 13 | float progress=iMouse.x/iResolution.x; 14 | 15 | float ratio=iResolution.x/iResolution.y; 16 | 17 | // ripple 18 | vec2 p=uv; 19 | float d=distance(vec2(.5),p); 20 | d*=40.; 21 | d=abs(sin(d)); 22 | d=step(smoothstep(.1,.9,1.-progress),d); 23 | 24 | return mix( 25 | getFromColor(uv), 26 | getToColor(uv), 27 | d 28 | ); 29 | } 30 | 31 | void mainImage(out vec4 fragColor,in vec2 fragCoord) 32 | { 33 | vec2 uv=fragCoord/iResolution.xy; 34 | 35 | vec4 col=transition(uv); 36 | 37 | fragColor=col; 38 | } 39 | -------------------------------------------------------------------------------- /html/4-uv-draw/uv-sdf.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /glsl/7-postprocessing/rgb-shift.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/09/10/63quVIA9xZLksDc.jpg" 2 | 3 | highp float random(vec2 co) 4 | { 5 | highp float a=12.9898; 6 | highp float b=78.233; 7 | highp float c=43758.5453; 8 | highp float dt=dot(co.xy,vec2(a,b)); 9 | highp float sn=mod(dt,3.14); 10 | return fract(sin(sn)*c); 11 | } 12 | 13 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 14 | vec2 uv=fragCoord/iResolution.xy; 15 | vec2 rUv=uv; 16 | vec2 gUv=uv; 17 | vec2 bUv=uv; 18 | // float offset=.0025; 19 | float noise=random(uv)*.5+.5; 20 | // float noise=random(uv+iTime)*.5+.5; 21 | vec2 offset=.0025*vec2(cos(noise),sin(noise)); 22 | rUv+=offset; 23 | bUv-=offset; 24 | // vec3 tex=texture(iChannel0,uv).xyz; 25 | vec4 rTex=texture(iChannel0,rUv); 26 | vec4 gTex=texture(iChannel0,gUv); 27 | vec4 bTex=texture(iChannel0,bUv); 28 | vec4 col=vec4(rTex.r,gTex.g,bTex.b,gTex.a); 29 | fragColor=col; 30 | } 31 | -------------------------------------------------------------------------------- /html/7-postprocessing/pixelate.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /html/2-first-shader/first-shader.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /html/7-postprocessing/vignette.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /bonus/shader-image-transition-mosaic.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/03/20/5jmPqdJW7EL2a4X.jpg" 2 | #iChannel1"https://s2.loli.net/2023/03/20/AgKHd6cZkUE9rGO.jpg" 3 | 4 | vec4 getFromColor(vec2 uv){ 5 | return texture(iChannel0,uv); 6 | } 7 | 8 | vec4 getToColor(vec2 uv){ 9 | return texture(iChannel1,uv); 10 | } 11 | 12 | float random(vec2 uv) 13 | { 14 | return fract(sin(dot(uv,vec2(12.9898,78.233)))*43758.5453); 15 | } 16 | 17 | vec4 transition(vec2 uv){ 18 | float progress=iMouse.x/iResolution.x; 19 | 20 | float ratio=iResolution.x/iResolution.y; 21 | 22 | vec2 p=uv; 23 | p.x*=ratio; 24 | 25 | float d=random(floor(p*vec2(20.,20.))); 26 | 27 | float s=.5; 28 | 29 | return mix( 30 | getFromColor(uv), 31 | getToColor(uv), 32 | smoothstep(0.,-s,d-progress*(1.+s)) 33 | ); 34 | } 35 | 36 | void mainImage(out vec4 fragColor,in vec2 fragCoord) 37 | { 38 | vec2 uv=fragCoord/iResolution.xy; 39 | 40 | vec4 col=transition(uv); 41 | 42 | fragColor=col; 43 | } 44 | -------------------------------------------------------------------------------- /html/6-random/random-grain.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /html/4-uv-draw/uv-polar.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /glsl/4-uv-draw/uv-mix.glsl: -------------------------------------------------------------------------------- 1 | float sdCircle(vec2 p,float r) 2 | { 3 | return length(p)-r; 4 | } 5 | 6 | float sdBox(in vec2 p,in vec2 b) 7 | { 8 | vec2 d=abs(p)-b; 9 | return length(max(d,0.))+min(max(d.x,d.y),0.); 10 | } 11 | 12 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 13 | vec2 uv=fragCoord/iResolution.xy; 14 | 15 | // gradient 16 | // vec3 col1=vec3(1.,0.,0.); 17 | // vec3 col2=vec3(0.,1.,0.); 18 | // vec3 col=mix(col1,col2,uv.x); 19 | // fragColor=vec4(col,1.); 20 | 21 | // tint shape 22 | // uv=(uv-.5)*2.; 23 | // uv.x*=iResolution.x/iResolution.y; 24 | 25 | // float d=sdBox(uv,vec2(.6,.3)); 26 | // float c=smoothstep(0.,.02,d); 27 | // vec3 colInner=vec3(1.,0.,0.); 28 | // vec3 colOuter=vec3(1.); 29 | // vec3 col=mix(colInner,colOuter,c); 30 | // fragColor=vec4(col,1.); 31 | 32 | // shape transition 33 | uv=(uv-.5)*2.; 34 | uv.x*=iResolution.x/iResolution.y; 35 | 36 | float d1=sdCircle(uv,.5); 37 | float d2=sdBox(uv,vec2(.6,.3)); 38 | float d=mix(d1,d2,abs(sin(iTime))); 39 | float c=smoothstep(0.,.02,d); 40 | fragColor=vec4(vec3(c),1.); 41 | } 42 | -------------------------------------------------------------------------------- /html/7-postprocessing/bulge.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /html/4-uv-draw/uv-sdf-alteration.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /html/4-uv-draw/uv-repeat-pattern.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /html/7-postprocessing/rgb-shift.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /glsl/5-texture/texture-transition.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/09/10/QozT59R6KsYmb3q.jpg" 2 | #iChannel1"https://s2.loli.net/2023/09/10/Jb8mIhZMBElPiuC.jpg" 3 | #iChannel2"https://s2.loli.net/2023/07/17/3GDlwcvehqQjTPH.jpg" 4 | 5 | vec4 getFromColor(vec2 uv){ 6 | return texture(iChannel0,uv); 7 | } 8 | 9 | vec4 getToColor(vec2 uv){ 10 | return texture(iChannel1,uv); 11 | } 12 | 13 | float sdCircle(vec2 p,float r) 14 | { 15 | return length(p)-r; 16 | } 17 | 18 | vec4 transition(vec2 uv){ 19 | float progress=iMouse.x/iResolution.x; 20 | float ratio=iResolution.x/iResolution.y; 21 | 22 | // fade 23 | // return mix(getFromColor(uv),getToColor(uv),progress); 24 | 25 | // slide 26 | // return mix(getFromColor(uv),getToColor(uv),1.-step(progress,uv.x)); 27 | 28 | // circle 29 | // vec2 p=uv; 30 | // p-=.5; 31 | // p.x*=ratio; 32 | // float d=sdCircle(p,progress*sqrt(2.)); 33 | // float c=smoothstep(-.2,0.,d); 34 | // return mix(getFromColor(uv),getToColor(uv),1.-c); 35 | 36 | // displacement 37 | vec2 dispVec=texture(iChannel2,uv).xy; 38 | float strength=.6; 39 | vec2 uv1=vec2(uv.x-dispVec.x*progress*strength,uv.y); 40 | vec2 uv2=vec2(uv.x+dispVec.x*(1.-progress)*strength,uv.y); 41 | return mix(getFromColor(uv1),getToColor(uv2),progress); 42 | } 43 | 44 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 45 | vec2 uv=fragCoord/iResolution.xy; 46 | vec4 col=transition(uv); 47 | fragColor=col; 48 | } 49 | -------------------------------------------------------------------------------- /glsl/4-uv-draw/uv-transform.glsl: -------------------------------------------------------------------------------- 1 | float sdBox(in vec2 p,in vec2 b) 2 | { 3 | vec2 d=abs(p)-b; 4 | return length(max(d,0.))+min(max(d.x,d.y),0.); 5 | } 6 | 7 | float sdEquilateralTriangle(in vec2 p,in float r) 8 | { 9 | const float k=sqrt(3.); 10 | p.x=abs(p.x)-r; 11 | p.y=p.y+r/k; 12 | if(p.x+k*p.y>0.)p=vec2(p.x-k*p.y,-k*p.x-p.y)/2.; 13 | p.x-=clamp(p.x,-2.*r,0.); 14 | return-length(p)*sign(p.y); 15 | } 16 | 17 | mat2 rotation2d(float angle){ 18 | float s=sin(angle); 19 | float c=cos(angle); 20 | 21 | return mat2( 22 | c,-s, 23 | s,c 24 | ); 25 | } 26 | 27 | vec2 rotate(vec2 v,float angle){ 28 | return rotation2d(angle)*v; 29 | } 30 | 31 | const float PI=3.14159265359; 32 | 33 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 34 | vec2 uv=fragCoord/iResolution.xy; 35 | 36 | // repeat 37 | // uv=fract(uv*vec2(2.,2.)); 38 | 39 | // center 40 | uv=(uv-.5)*2.; 41 | uv.x*=iResolution.x/iResolution.y; 42 | 43 | // translate 44 | // uv-=vec2(.2,.4); 45 | 46 | // mirror 47 | uv.y=abs(uv.y); 48 | 49 | // scale 50 | // uv/=vec2(2.,2.); 51 | 52 | // flip 53 | // uv.y*=-1.; 54 | 55 | // rotate 56 | // uv=rotate(uv,PI/2.); 57 | // uv=rotate(uv,iTime); 58 | 59 | // float d=sdBox(uv,vec2(.6,.3)); 60 | float d=sdEquilateralTriangle(uv,.5); 61 | float c=smoothstep(0.,.02,d); 62 | fragColor=vec4(vec3(c),1.); 63 | // fragColor=vec4(uv,0.,1.); 64 | } 65 | -------------------------------------------------------------------------------- /html/4-uv-draw/uv-mix.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /bonus/shader-simple-god-rays-effect.glsl: -------------------------------------------------------------------------------- 1 | // https://github.com/MaxBittker/glsl-voronoi-noise/blob/master/2d.glsl 2 | const mat2 myt=mat2(.12121212,.13131313,-.13131313,.12121212); 3 | const vec2 mys=vec2(1e4,1e6); 4 | 5 | vec2 rhash(vec2 uv){ 6 | uv*=myt; 7 | uv*=mys; 8 | return fract(fract(uv/mys)*uv); 9 | } 10 | 11 | vec3 hash(vec3 p){ 12 | return fract(sin(vec3(dot(p,vec3(1.,57.,113.)), 13 | dot(p,vec3(57.,113.,1.)), 14 | dot(p,vec3(113.,1.,57.))))* 15 | 43758.5453); 16 | } 17 | 18 | float voronoi2d(const in vec2 point){ 19 | vec2 p=floor(point); 20 | vec2 f=fract(point); 21 | float res=0.; 22 | for(int j=-1;j<=1;j++){ 23 | for(int i=-1;i<=1;i++){ 24 | vec2 b=vec2(i,j); 25 | vec2 r=vec2(b)-f+rhash(p+b); 26 | res+=1./pow(dot(r,r),8.); 27 | } 28 | } 29 | return pow(1./res,.0625); 30 | } 31 | 32 | vec2 cart2polar(vec2 uv){ 33 | float phi=atan(uv.y,uv.x); 34 | float r=length(uv); 35 | return vec2(phi,r); 36 | } 37 | 38 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 39 | vec2 uv=fragCoord/iResolution.xy; 40 | 41 | vec2 p=uv; 42 | p.x*=iResolution.x/iResolution.y; 43 | 44 | uv=(uv-.5)*2.; 45 | uv.x*=iResolution.x/iResolution.y; 46 | 47 | vec3 col=vec3(0.); 48 | // float noise=voronoi2d(uv*3.); 49 | // col=vec3(noise); 50 | 51 | uv.y-=2.; 52 | uv/=50.; 53 | uv=cart2polar(uv); 54 | col=vec3(uv.x); 55 | 56 | float n1=voronoi2d((vec2(uv.x,0.)+.04*iTime)*10.); 57 | col=vec3(n1); 58 | float n2=voronoi2d((vec2(.1,uv.x)+.04*iTime*1.5)*10.); 59 | col=vec3(n2); 60 | float n3=min(n1,n2); 61 | col=vec3(n3); 62 | 63 | // col=vec3(1.); 64 | float mask=smoothstep(.15,.86,p.y); 65 | float alpha=n3*mask*.8; 66 | 67 | col=mix(vec3(0.,.412,.580),vec3(1.),alpha); 68 | 69 | fragColor=vec4(col,1.); 70 | } -------------------------------------------------------------------------------- /html/8-vertex/vertex-shader-object.html: -------------------------------------------------------------------------------- 1 | 7 |
8 | 9 | 10 | -------------------------------------------------------------------------------- /html/4-uv-draw/uv-transform.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /glsl/4-uv-draw/uv-sdf-bool.glsl: -------------------------------------------------------------------------------- 1 | float sdCircle(vec2 p,float r) 2 | { 3 | return length(p)-r; 4 | } 5 | 6 | float sdBox(in vec2 p,in vec2 b) 7 | { 8 | vec2 d=abs(p)-b; 9 | return length(max(d,0.))+min(max(d.x,d.y),0.); 10 | } 11 | 12 | float opUnion(float d1,float d2) 13 | { 14 | return min(d1,d2); 15 | } 16 | 17 | float opIntersection(float d1,float d2) 18 | { 19 | return max(d1,d2); 20 | } 21 | 22 | float opSubtraction(float d1,float d2) 23 | { 24 | return max(-d1,d2); 25 | } 26 | 27 | float opSmoothUnion(float d1,float d2,float k){ 28 | float h=clamp(.5+.5*(d2-d1)/k,0.,1.); 29 | return mix(d2,d1,h)-k*h*(1.-h); 30 | } 31 | 32 | float opSmoothSubtraction(float d1,float d2,float k){ 33 | float h=clamp(.5-.5*(d2+d1)/k,0.,1.); 34 | return mix(d2,-d1,h)+k*h*(1.-h); 35 | } 36 | 37 | float opSmoothIntersection(float d1,float d2,float k){ 38 | float h=clamp(.5-.5*(d2-d1)/k,0.,1.); 39 | return mix(d2,d1,h)+k*h*(1.-h); 40 | } 41 | 42 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 43 | vec2 uv=fragCoord/iResolution.xy; 44 | uv=(uv-.5)*2.; 45 | uv.x*=iResolution.x/iResolution.y; 46 | 47 | float d1=sdCircle(uv,.5); 48 | float d2=sdBox(uv,vec2(.6,.3)); 49 | 50 | float d=d1; 51 | 52 | // union 53 | // d=opUnion(d1,d2); 54 | 55 | // intersection 56 | // d=opIntersection(d1,d2); 57 | 58 | // subtraction 59 | // box - circle 60 | // d=opSubtraction(d1,d2); 61 | // circle - box 62 | // d=opSubtraction(d2,d1); 63 | 64 | // smooth union 65 | // d=opSmoothUnion(d1,d2,.1); 66 | 67 | // smooth intersection 68 | // d=opSmoothIntersection(d1,d2,.1); 69 | 70 | // smooth subtraction 71 | // box - circle 72 | // d=opSmoothSubtraction(d1,d2,.1); 73 | // circle - box 74 | d=opSmoothSubtraction(d2,d1,.1); 75 | 76 | float c=smoothstep(0.,.02,d); 77 | fragColor=vec4(vec3(c),1.); 78 | } 79 | -------------------------------------------------------------------------------- /html/5-texture/texture-transition.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /bonus/shader-image-transition-sdf-mask.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/03/20/5jmPqdJW7EL2a4X.jpg" 2 | #iChannel1"https://s2.loli.net/2023/03/20/AgKHd6cZkUE9rGO.jpg" 3 | 4 | vec4 getFromColor(vec2 uv){ 5 | return texture(iChannel0,uv); 6 | } 7 | 8 | vec4 getToColor(vec2 uv){ 9 | return texture(iChannel1,uv); 10 | } 11 | 12 | const float PI=3.14159265359; 13 | 14 | // rotate 15 | mat2 rotation2d(float angle){ 16 | float s=sin(angle); 17 | float c=cos(angle); 18 | 19 | return mat2( 20 | c,-s, 21 | s,c 22 | ); 23 | } 24 | 25 | vec2 rotate(vec2 v,float angle){ 26 | return rotation2d(angle)*v; 27 | } 28 | 29 | float sdBox(in vec2 p,in vec2 b) 30 | { 31 | vec2 d=abs(p)-b; 32 | return length(max(d,0.))+min(max(d.x,d.y),0.); 33 | } 34 | 35 | float opUnion(float d1,float d2) 36 | { 37 | return min(d1,d2); 38 | } 39 | 40 | float opIntersection(float d1,float d2) 41 | { 42 | return max(d1,d2); 43 | } 44 | 45 | float opSubtraction(float d1,float d2) 46 | { 47 | return max(-d1,d2); 48 | } 49 | 50 | vec4 transition(vec2 uv){ 51 | float progress=iMouse.x/iResolution.x; 52 | 53 | float ratio=iResolution.x/iResolution.y; 54 | 55 | // SDF mask 56 | vec2 grid=vec2(4.,4.); 57 | 58 | float pr=-2.*pow((2.*progress-1.),2.)+1.; 59 | float offset=mix(0.,-.25,pr); 60 | 61 | // float angle=PI*.25/3.; 62 | float angle=mix(0.,PI*.5,progress); 63 | vec2 p1=uv; 64 | p1=fract(p1*grid); 65 | p1-=vec2(.5); 66 | p1=rotate(p1,angle); 67 | float d1=sdBox(p1,vec2(.5+offset,.75)); 68 | 69 | vec2 p2=uv; 70 | p2=fract(p2*grid); 71 | p2-=vec2(.5); 72 | p2=rotate(p2,angle+PI*.5); 73 | float d2=sdBox(p2,vec2(.5+offset,.75)); 74 | 75 | float d=opIntersection(d1,d2); 76 | 77 | float mask=1.-smoothstep(0.,.02,d); 78 | 79 | return mix( 80 | getFromColor(uv)*mask, 81 | getToColor(uv)*mask, 82 | progress 83 | ); 84 | } 85 | 86 | void mainImage(out vec4 fragColor,in vec2 fragCoord) 87 | { 88 | vec2 uv=fragCoord/iResolution.xy; 89 | 90 | vec4 col=transition(uv); 91 | 92 | fragColor=col; 93 | } 94 | -------------------------------------------------------------------------------- /html/4-uv-draw/uv-sdf-bool.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /html/8-vertex/vertex-shader-points.html: -------------------------------------------------------------------------------- 1 | 7 |
8 | 9 | 10 | -------------------------------------------------------------------------------- /html/9-lighting/lighting-phong.html: -------------------------------------------------------------------------------- 1 | 7 |
8 | 9 | 10 | -------------------------------------------------------------------------------- /html/9-lighting/lighting-blinn-phong.html: -------------------------------------------------------------------------------- 1 | 7 |
8 | 9 | 10 | -------------------------------------------------------------------------------- /glsl/6-random/noise.glsl: -------------------------------------------------------------------------------- 1 | // 2 | // GLSL textureless classic 3D noise "cnoise", 3 | // with an RSL-style periodic variant "pnoise". 4 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 5 | // Version: 2011-10-11 6 | // 7 | // Many thanks to Ian McEwan of Ashima Arts for the 8 | // ideas for permutation and gradient selection. 9 | // 10 | // Copyright (c) 2011 Stefan Gustavson. All rights reserved. 11 | // Distributed under the MIT license. See LICENSE file. 12 | // https://github.com/ashima/webgl-noise 13 | // 14 | 15 | vec3 mod289(vec3 x) 16 | { 17 | return x-floor(x*(1./289.))*289.; 18 | } 19 | 20 | vec4 mod289(vec4 x) 21 | { 22 | return x-floor(x*(1./289.))*289.; 23 | } 24 | 25 | vec4 permute(vec4 x) 26 | { 27 | return mod289(((x*34.)+1.)*x); 28 | } 29 | 30 | vec4 taylorInvSqrt(vec4 r) 31 | { 32 | return 1.79284291400159-.85373472095314*r; 33 | } 34 | 35 | vec3 fade(vec3 t){ 36 | return t*t*t*(t*(t*6.-15.)+10.); 37 | } 38 | 39 | // Classic Perlin noise 40 | float cnoise(vec3 P) 41 | { 42 | vec3 Pi0=floor(P);// Integer part for indexing 43 | vec3 Pi1=Pi0+vec3(1.);// Integer part + 1 44 | Pi0=mod289(Pi0); 45 | Pi1=mod289(Pi1); 46 | vec3 Pf0=fract(P);// Fractional part for interpolation 47 | vec3 Pf1=Pf0-vec3(1.);// Fractional part - 1.0 48 | vec4 ix=vec4(Pi0.x,Pi1.x,Pi0.x,Pi1.x); 49 | vec4 iy=vec4(Pi0.yy,Pi1.yy); 50 | vec4 iz0=Pi0.zzzz; 51 | vec4 iz1=Pi1.zzzz; 52 | 53 | vec4 ixy=permute(permute(ix)+iy); 54 | vec4 ixy0=permute(ixy+iz0); 55 | vec4 ixy1=permute(ixy+iz1); 56 | 57 | vec4 gx0=ixy0*(1./7.); 58 | vec4 gy0=fract(floor(gx0)*(1./7.))-.5; 59 | gx0=fract(gx0); 60 | vec4 gz0=vec4(.5)-abs(gx0)-abs(gy0); 61 | vec4 sz0=step(gz0,vec4(0.)); 62 | gx0-=sz0*(step(0.,gx0)-.5); 63 | gy0-=sz0*(step(0.,gy0)-.5); 64 | 65 | vec4 gx1=ixy1*(1./7.); 66 | vec4 gy1=fract(floor(gx1)*(1./7.))-.5; 67 | gx1=fract(gx1); 68 | vec4 gz1=vec4(.5)-abs(gx1)-abs(gy1); 69 | vec4 sz1=step(gz1,vec4(0.)); 70 | gx1-=sz1*(step(0.,gx1)-.5); 71 | gy1-=sz1*(step(0.,gy1)-.5); 72 | 73 | vec3 g000=vec3(gx0.x,gy0.x,gz0.x); 74 | vec3 g100=vec3(gx0.y,gy0.y,gz0.y); 75 | vec3 g010=vec3(gx0.z,gy0.z,gz0.z); 76 | vec3 g110=vec3(gx0.w,gy0.w,gz0.w); 77 | vec3 g001=vec3(gx1.x,gy1.x,gz1.x); 78 | vec3 g101=vec3(gx1.y,gy1.y,gz1.y); 79 | vec3 g011=vec3(gx1.z,gy1.z,gz1.z); 80 | vec3 g111=vec3(gx1.w,gy1.w,gz1.w); 81 | 82 | vec4 norm0=taylorInvSqrt(vec4(dot(g000,g000),dot(g010,g010),dot(g100,g100),dot(g110,g110))); 83 | g000*=norm0.x; 84 | g010*=norm0.y; 85 | g100*=norm0.z; 86 | g110*=norm0.w; 87 | vec4 norm1=taylorInvSqrt(vec4(dot(g001,g001),dot(g011,g011),dot(g101,g101),dot(g111,g111))); 88 | g001*=norm1.x; 89 | g011*=norm1.y; 90 | g101*=norm1.z; 91 | g111*=norm1.w; 92 | 93 | float n000=dot(g000,Pf0); 94 | float n100=dot(g100,vec3(Pf1.x,Pf0.yz)); 95 | float n010=dot(g010,vec3(Pf0.x,Pf1.y,Pf0.z)); 96 | float n110=dot(g110,vec3(Pf1.xy,Pf0.z)); 97 | float n001=dot(g001,vec3(Pf0.xy,Pf1.z)); 98 | float n101=dot(g101,vec3(Pf1.x,Pf0.y,Pf1.z)); 99 | float n011=dot(g011,vec3(Pf0.x,Pf1.yz)); 100 | float n111=dot(g111,Pf1); 101 | 102 | vec3 fade_xyz=fade(Pf0); 103 | vec4 n_z=mix(vec4(n000,n100,n010,n110),vec4(n001,n101,n011,n111),fade_xyz.z); 104 | vec2 n_yz=mix(n_z.xy,n_z.zw,fade_xyz.y); 105 | float n_xyz=mix(n_yz.x,n_yz.y,fade_xyz.x); 106 | return 2.2*n_xyz; 107 | } 108 | 109 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 110 | vec2 uv=fragCoord/iResolution.xy; 111 | 112 | float noise=cnoise(vec3(uv*10.,0.)); 113 | // float noise=cnoise(vec3(uv*10.,iTime)); 114 | 115 | vec3 col=vec3(noise); 116 | fragColor=vec4(col,1.); 117 | } 118 | -------------------------------------------------------------------------------- /html/9-lighting/lighting-ibl-specular.html: -------------------------------------------------------------------------------- 1 | 7 |
8 | 9 | 10 | -------------------------------------------------------------------------------- /html/6-random/noise.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /html/9-lighting/lighting-fresnel.html: -------------------------------------------------------------------------------- 1 | 7 |
8 | 9 | 10 | -------------------------------------------------------------------------------- /glsl/6-random/noise-transition.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/09/10/QozT59R6KsYmb3q.jpg" 2 | #iChannel1"https://s2.loli.net/2023/09/10/Jb8mIhZMBElPiuC.jpg" 3 | 4 | // 5 | // GLSL textureless classic 3D noise "cnoise", 6 | // with an RSL-style periodic variant "pnoise". 7 | // Author: Stefan Gustavson (stefan.gustavson@liu.se) 8 | // Version: 2011-10-11 9 | // 10 | // Many thanks to Ian McEwan of Ashima Arts for the 11 | // ideas for permutation and gradient selection. 12 | // 13 | // Copyright (c) 2011 Stefan Gustavson. All rights reserved. 14 | // Distributed under the MIT license. See LICENSE file. 15 | // https://github.com/ashima/webgl-noise 16 | // 17 | 18 | vec3 mod289(vec3 x) 19 | { 20 | return x-floor(x*(1./289.))*289.; 21 | } 22 | 23 | vec4 mod289(vec4 x) 24 | { 25 | return x-floor(x*(1./289.))*289.; 26 | } 27 | 28 | vec4 permute(vec4 x) 29 | { 30 | return mod289(((x*34.)+1.)*x); 31 | } 32 | 33 | vec4 taylorInvSqrt(vec4 r) 34 | { 35 | return 1.79284291400159-.85373472095314*r; 36 | } 37 | 38 | vec3 fade(vec3 t){ 39 | return t*t*t*(t*(t*6.-15.)+10.); 40 | } 41 | 42 | // Classic Perlin noise 43 | float cnoise(vec3 P) 44 | { 45 | vec3 Pi0=floor(P);// Integer part for indexing 46 | vec3 Pi1=Pi0+vec3(1.);// Integer part + 1 47 | Pi0=mod289(Pi0); 48 | Pi1=mod289(Pi1); 49 | vec3 Pf0=fract(P);// Fractional part for interpolation 50 | vec3 Pf1=Pf0-vec3(1.);// Fractional part - 1.0 51 | vec4 ix=vec4(Pi0.x,Pi1.x,Pi0.x,Pi1.x); 52 | vec4 iy=vec4(Pi0.yy,Pi1.yy); 53 | vec4 iz0=Pi0.zzzz; 54 | vec4 iz1=Pi1.zzzz; 55 | 56 | vec4 ixy=permute(permute(ix)+iy); 57 | vec4 ixy0=permute(ixy+iz0); 58 | vec4 ixy1=permute(ixy+iz1); 59 | 60 | vec4 gx0=ixy0*(1./7.); 61 | vec4 gy0=fract(floor(gx0)*(1./7.))-.5; 62 | gx0=fract(gx0); 63 | vec4 gz0=vec4(.5)-abs(gx0)-abs(gy0); 64 | vec4 sz0=step(gz0,vec4(0.)); 65 | gx0-=sz0*(step(0.,gx0)-.5); 66 | gy0-=sz0*(step(0.,gy0)-.5); 67 | 68 | vec4 gx1=ixy1*(1./7.); 69 | vec4 gy1=fract(floor(gx1)*(1./7.))-.5; 70 | gx1=fract(gx1); 71 | vec4 gz1=vec4(.5)-abs(gx1)-abs(gy1); 72 | vec4 sz1=step(gz1,vec4(0.)); 73 | gx1-=sz1*(step(0.,gx1)-.5); 74 | gy1-=sz1*(step(0.,gy1)-.5); 75 | 76 | vec3 g000=vec3(gx0.x,gy0.x,gz0.x); 77 | vec3 g100=vec3(gx0.y,gy0.y,gz0.y); 78 | vec3 g010=vec3(gx0.z,gy0.z,gz0.z); 79 | vec3 g110=vec3(gx0.w,gy0.w,gz0.w); 80 | vec3 g001=vec3(gx1.x,gy1.x,gz1.x); 81 | vec3 g101=vec3(gx1.y,gy1.y,gz1.y); 82 | vec3 g011=vec3(gx1.z,gy1.z,gz1.z); 83 | vec3 g111=vec3(gx1.w,gy1.w,gz1.w); 84 | 85 | vec4 norm0=taylorInvSqrt(vec4(dot(g000,g000),dot(g010,g010),dot(g100,g100),dot(g110,g110))); 86 | g000*=norm0.x; 87 | g010*=norm0.y; 88 | g100*=norm0.z; 89 | g110*=norm0.w; 90 | vec4 norm1=taylorInvSqrt(vec4(dot(g001,g001),dot(g011,g011),dot(g101,g101),dot(g111,g111))); 91 | g001*=norm1.x; 92 | g011*=norm1.y; 93 | g101*=norm1.z; 94 | g111*=norm1.w; 95 | 96 | float n000=dot(g000,Pf0); 97 | float n100=dot(g100,vec3(Pf1.x,Pf0.yz)); 98 | float n010=dot(g010,vec3(Pf0.x,Pf1.y,Pf0.z)); 99 | float n110=dot(g110,vec3(Pf1.xy,Pf0.z)); 100 | float n001=dot(g001,vec3(Pf0.xy,Pf1.z)); 101 | float n101=dot(g101,vec3(Pf1.x,Pf0.y,Pf1.z)); 102 | float n011=dot(g011,vec3(Pf0.x,Pf1.yz)); 103 | float n111=dot(g111,Pf1); 104 | 105 | vec3 fade_xyz=fade(Pf0); 106 | vec4 n_z=mix(vec4(n000,n100,n010,n110),vec4(n001,n101,n011,n111),fade_xyz.z); 107 | vec2 n_yz=mix(n_z.xy,n_z.zw,fade_xyz.y); 108 | float n_xyz=mix(n_yz.x,n_yz.y,fade_xyz.x); 109 | return 2.2*n_xyz; 110 | } 111 | 112 | vec4 getFromColor(vec2 uv){ 113 | return texture(iChannel0,uv); 114 | } 115 | 116 | vec4 getToColor(vec2 uv){ 117 | return texture(iChannel1,uv); 118 | } 119 | 120 | float sdCircle(vec2 p,float r) 121 | { 122 | return length(p)-r; 123 | } 124 | 125 | vec4 transition(vec2 uv){ 126 | float progress=iMouse.x/iResolution.x; 127 | float ratio=iResolution.x/iResolution.y; 128 | 129 | vec2 p=uv; 130 | p-=.5; 131 | p.x*=ratio; 132 | 133 | float noise=cnoise(vec3(p*10.,0.)); 134 | float pr=progress+noise*.1; 135 | 136 | float d=sdCircle(p,pr*sqrt(2.4)); 137 | float c=smoothstep(-.1,-.05,d); 138 | 139 | return mix(getFromColor(uv),getToColor(uv),1.-c); 140 | } 141 | 142 | void mainImage(out vec4 fragColor,in vec2 fragCoord){ 143 | vec2 uv=fragCoord/iResolution.xy; 144 | 145 | vec4 col=transition(uv); 146 | 147 | fragColor=col; 148 | } 149 | -------------------------------------------------------------------------------- /html/9-lighting/lighting-fix-normal.html: -------------------------------------------------------------------------------- 1 | 7 |
8 | 9 | 10 | -------------------------------------------------------------------------------- /glsl/6-random/fbm.glsl: -------------------------------------------------------------------------------- 1 | // 2 | // Description : Array and textureless GLSL 2D/3D/4D simplex 3 | // noise functions. 4 | // Author : Ian McEwan, Ashima Arts. 5 | // Maintainer : ijm 6 | // Lastmod : 20110822 (ijm) 7 | // License : Copyright (C) 2011 Ashima Arts. All rights reserved. 8 | // Distributed under the MIT License. See LICENSE file. 9 | // https://github.com/ashima/webgl-noise 10 | // 11 | 12 | vec3 mod289(vec3 x){ 13 | return x-floor(x*(1./289.))*289.; 14 | } 15 | 16 | vec4 mod289(vec4 x){ 17 | return x-floor(x*(1./289.))*289.; 18 | } 19 | 20 | vec4 permute(vec4 x){ 21 | return mod289(((x*34.)+1.)*x); 22 | } 23 | 24 | vec4 taylorInvSqrt(vec4 r) 25 | { 26 | return 1.79284291400159-.85373472095314*r; 27 | } 28 | 29 | float snoise(vec3 v) 30 | { 31 | const vec2 C=vec2(1./6.,1./3.); 32 | const vec4 D=vec4(0.,.5,1.,2.); 33 | 34 | // First corner 35 | vec3 i=floor(v+dot(v,C.yyy)); 36 | vec3 x0=v-i+dot(i,C.xxx); 37 | 38 | // Other corners 39 | vec3 g=step(x0.yzx,x0.xyz); 40 | vec3 l=1.-g; 41 | vec3 i1=min(g.xyz,l.zxy); 42 | vec3 i2=max(g.xyz,l.zxy); 43 | 44 | // x0 = x0 - 0.0 + 0.0 * C.xxx; 45 | // x1 = x0 - i1 + 1.0 * C.xxx; 46 | // x2 = x0 - i2 + 2.0 * C.xxx; 47 | // x3 = x0 - 1.0 + 3.0 * C.xxx; 48 | vec3 x1=x0-i1+C.xxx; 49 | vec3 x2=x0-i2+C.yyy;// 2.0*C.x = 1/3 = C.y 50 | vec3 x3=x0-D.yyy;// -1.0+3.0*C.x = -0.5 = -D.y 51 | 52 | // Permutations 53 | i=mod289(i); 54 | vec4 p=permute(permute(permute( 55 | i.z+vec4(0.,i1.z,i2.z,1.)) 56 | +i.y+vec4(0.,i1.y,i2.y,1.)) 57 | +i.x+vec4(0.,i1.x,i2.x,1.)); 58 | 59 | // Gradients: 7x7 points over a square, mapped onto an octahedron. 60 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 61 | float n_=.142857142857;// 1.0/7.0 62 | vec3 ns=n_*D.wyz-D.xzx; 63 | 64 | vec4 j=p-49.*floor(p*ns.z*ns.z);// mod(p,7*7) 65 | 66 | vec4 x_=floor(j*ns.z); 67 | vec4 y_=floor(j-7.*x_);// mod(j,N) 68 | 69 | vec4 x=x_*ns.x+ns.yyyy; 70 | vec4 y=y_*ns.x+ns.yyyy; 71 | vec4 h=1.-abs(x)-abs(y); 72 | 73 | vec4 b0=vec4(x.xy,y.xy); 74 | vec4 b1=vec4(x.zw,y.zw); 75 | 76 | //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0; 77 | //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0; 78 | vec4 s0=floor(b0)*2.+1.; 79 | vec4 s1=floor(b1)*2.+1.; 80 | vec4 sh=-step(h,vec4(0.)); 81 | 82 | vec4 a0=b0.xzyw+s0.xzyw*sh.xxyy; 83 | vec4 a1=b1.xzyw+s1.xzyw*sh.zzww; 84 | 85 | vec3 p0=vec3(a0.xy,h.x); 86 | vec3 p1=vec3(a0.zw,h.y); 87 | vec3 p2=vec3(a1.xy,h.z); 88 | vec3 p3=vec3(a1.zw,h.w); 89 | 90 | //Normalise gradients 91 | vec4 norm=taylorInvSqrt(vec4(dot(p0,p0),dot(p1,p1),dot(p2,p2),dot(p3,p3))); 92 | p0*=norm.x; 93 | p1*=norm.y; 94 | p2*=norm.z; 95 | p3*=norm.w; 96 | 97 | // Mix final noise value 98 | vec4 m=max(.6-vec4(dot(x0,x0),dot(x1,x1),dot(x2,x2),dot(x3,x3)),0.); 99 | m=m*m; 100 | return 42.*dot(m*m,vec4(dot(p0,x0),dot(p1,x1), 101 | dot(p2,x2),dot(p3,x3))); 102 | } 103 | 104 | float fbm(vec3 p){ 105 | float value=0.; 106 | float amplitude=1.; 107 | float frequency=1.; 108 | float lacunarity=2.; 109 | float persistance=.5; 110 | float scale=1.; 111 | int octaves=8; 112 | 113 | for(int i=0;i 2 | body { 3 | margin: 0; 4 | background: black; 5 | } 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /html/6-random/fbm.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /bonus/shader-fbm-paper-texture.glsl: -------------------------------------------------------------------------------- 1 | // 2 | // Description : Array and textureless GLSL 2D/3D/4D simplex 3 | // noise functions. 4 | // Author : Ian McEwan, Ashima Arts. 5 | // Maintainer : ijm 6 | // Lastmod : 20110822 (ijm) 7 | // License : Copyright (C) 2011 Ashima Arts. All rights reserved. 8 | // Distributed under the MIT License. See LICENSE file. 9 | // https://github.com/ashima/webgl-noise 10 | // 11 | 12 | vec3 mod289(vec3 x){ 13 | return x-floor(x*(1./289.))*289.; 14 | } 15 | 16 | vec4 mod289(vec4 x){ 17 | return x-floor(x*(1./289.))*289.; 18 | } 19 | 20 | vec4 permute(vec4 x){ 21 | return mod289(((x*34.)+1.)*x); 22 | } 23 | 24 | vec4 taylorInvSqrt(vec4 r) 25 | { 26 | return 1.79284291400159-.85373472095314*r; 27 | } 28 | 29 | float snoise(vec3 v) 30 | { 31 | const vec2 C=vec2(1./6.,1./3.); 32 | const vec4 D=vec4(0.,.5,1.,2.); 33 | 34 | // First corner 35 | vec3 i=floor(v+dot(v,C.yyy)); 36 | vec3 x0=v-i+dot(i,C.xxx); 37 | 38 | // Other corners 39 | vec3 g=step(x0.yzx,x0.xyz); 40 | vec3 l=1.-g; 41 | vec3 i1=min(g.xyz,l.zxy); 42 | vec3 i2=max(g.xyz,l.zxy); 43 | 44 | // x0 = x0 - 0.0 + 0.0 * C.xxx; 45 | // x1 = x0 - i1 + 1.0 * C.xxx; 46 | // x2 = x0 - i2 + 2.0 * C.xxx; 47 | // x3 = x0 - 1.0 + 3.0 * C.xxx; 48 | vec3 x1=x0-i1+C.xxx; 49 | vec3 x2=x0-i2+C.yyy;// 2.0*C.x = 1/3 = C.y 50 | vec3 x3=x0-D.yyy;// -1.0+3.0*C.x = -0.5 = -D.y 51 | 52 | // Permutations 53 | i=mod289(i); 54 | vec4 p=permute(permute(permute( 55 | i.z+vec4(0.,i1.z,i2.z,1.)) 56 | +i.y+vec4(0.,i1.y,i2.y,1.)) 57 | +i.x+vec4(0.,i1.x,i2.x,1.)); 58 | 59 | // Gradients: 7x7 points over a square, mapped onto an octahedron. 60 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 61 | float n_=.142857142857;// 1.0/7.0 62 | vec3 ns=n_*D.wyz-D.xzx; 63 | 64 | vec4 j=p-49.*floor(p*ns.z*ns.z);// mod(p,7*7) 65 | 66 | vec4 x_=floor(j*ns.z); 67 | vec4 y_=floor(j-7.*x_);// mod(j,N) 68 | 69 | vec4 x=x_*ns.x+ns.yyyy; 70 | vec4 y=y_*ns.x+ns.yyyy; 71 | vec4 h=1.-abs(x)-abs(y); 72 | 73 | vec4 b0=vec4(x.xy,y.xy); 74 | vec4 b1=vec4(x.zw,y.zw); 75 | 76 | //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0; 77 | //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0; 78 | vec4 s0=floor(b0)*2.+1.; 79 | vec4 s1=floor(b1)*2.+1.; 80 | vec4 sh=-step(h,vec4(0.)); 81 | 82 | vec4 a0=b0.xzyw+s0.xzyw*sh.xxyy; 83 | vec4 a1=b1.xzyw+s1.xzyw*sh.zzww; 84 | 85 | vec3 p0=vec3(a0.xy,h.x); 86 | vec3 p1=vec3(a0.zw,h.y); 87 | vec3 p2=vec3(a1.xy,h.z); 88 | vec3 p3=vec3(a1.zw,h.w); 89 | 90 | //Normalise gradients 91 | vec4 norm=taylorInvSqrt(vec4(dot(p0,p0),dot(p1,p1),dot(p2,p2),dot(p3,p3))); 92 | p0*=norm.x; 93 | p1*=norm.y; 94 | p2*=norm.z; 95 | p3*=norm.w; 96 | 97 | // Mix final noise value 98 | vec4 m=max(.6-vec4(dot(x0,x0),dot(x1,x1),dot(x2,x2),dot(x3,x3)),0.); 99 | m=m*m; 100 | return 42.*dot(m*m,vec4(dot(p0,x0),dot(p1,x1), 101 | dot(p2,x2),dot(p3,x3))); 102 | } 103 | 104 | float fbm(vec3 p){ 105 | float value=0.; 106 | float amplitude=1.; 107 | // float frequency=1.; 108 | float frequency=.025; 109 | float lacunarity=2.; 110 | float persistance=.5; 111 | // float scale=1.; 112 | float scale=.5; 113 | int octaves=8; 114 | 115 | for(int i=0;i 2 | body { 3 | margin: 0; 4 | background: black; 5 | } 6 | 7 |
8 | 9 | 10 | -------------------------------------------------------------------------------- /bonus/shader-fbm-particle-transition.glsl: -------------------------------------------------------------------------------- 1 | #iChannel0"https://s2.loli.net/2023/03/20/5jmPqdJW7EL2a4X.jpg" 2 | 3 | const float PI=3.14159265359; 4 | 5 | // 6 | // Description : Array and textureless GLSL 2D/3D/4D simplex 7 | // noise functions. 8 | // Author : Ian McEwan, Ashima Arts. 9 | // Maintainer : ijm 10 | // Lastmod : 20110822 (ijm) 11 | // License : Copyright (C) 2011 Ashima Arts. All rights reserved. 12 | // Distributed under the MIT License. See LICENSE file. 13 | // https://github.com/ashima/webgl-noise 14 | // 15 | 16 | vec3 mod289(vec3 x){ 17 | return x-floor(x*(1./289.))*289.; 18 | } 19 | 20 | vec4 mod289(vec4 x){ 21 | return x-floor(x*(1./289.))*289.; 22 | } 23 | 24 | vec4 permute(vec4 x){ 25 | return mod289(((x*34.)+1.)*x); 26 | } 27 | 28 | vec4 taylorInvSqrt(vec4 r) 29 | { 30 | return 1.79284291400159-.85373472095314*r; 31 | } 32 | 33 | float snoise(vec3 v) 34 | { 35 | const vec2 C=vec2(1./6.,1./3.); 36 | const vec4 D=vec4(0.,.5,1.,2.); 37 | 38 | // First corner 39 | vec3 i=floor(v+dot(v,C.yyy)); 40 | vec3 x0=v-i+dot(i,C.xxx); 41 | 42 | // Other corners 43 | vec3 g=step(x0.yzx,x0.xyz); 44 | vec3 l=1.-g; 45 | vec3 i1=min(g.xyz,l.zxy); 46 | vec3 i2=max(g.xyz,l.zxy); 47 | 48 | // x0 = x0 - 0.0 + 0.0 * C.xxx; 49 | // x1 = x0 - i1 + 1.0 * C.xxx; 50 | // x2 = x0 - i2 + 2.0 * C.xxx; 51 | // x3 = x0 - 1.0 + 3.0 * C.xxx; 52 | vec3 x1=x0-i1+C.xxx; 53 | vec3 x2=x0-i2+C.yyy;// 2.0*C.x = 1/3 = C.y 54 | vec3 x3=x0-D.yyy;// -1.0+3.0*C.x = -0.5 = -D.y 55 | 56 | // Permutations 57 | i=mod289(i); 58 | vec4 p=permute(permute(permute( 59 | i.z+vec4(0.,i1.z,i2.z,1.)) 60 | +i.y+vec4(0.,i1.y,i2.y,1.)) 61 | +i.x+vec4(0.,i1.x,i2.x,1.)); 62 | 63 | // Gradients: 7x7 points over a square, mapped onto an octahedron. 64 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 65 | float n_=.142857142857;// 1.0/7.0 66 | vec3 ns=n_*D.wyz-D.xzx; 67 | 68 | vec4 j=p-49.*floor(p*ns.z*ns.z);// mod(p,7*7) 69 | 70 | vec4 x_=floor(j*ns.z); 71 | vec4 y_=floor(j-7.*x_);// mod(j,N) 72 | 73 | vec4 x=x_*ns.x+ns.yyyy; 74 | vec4 y=y_*ns.x+ns.yyyy; 75 | vec4 h=1.-abs(x)-abs(y); 76 | 77 | vec4 b0=vec4(x.xy,y.xy); 78 | vec4 b1=vec4(x.zw,y.zw); 79 | 80 | //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0; 81 | //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0; 82 | vec4 s0=floor(b0)*2.+1.; 83 | vec4 s1=floor(b1)*2.+1.; 84 | vec4 sh=-step(h,vec4(0.)); 85 | 86 | vec4 a0=b0.xzyw+s0.xzyw*sh.xxyy; 87 | vec4 a1=b1.xzyw+s1.xzyw*sh.zzww; 88 | 89 | vec3 p0=vec3(a0.xy,h.x); 90 | vec3 p1=vec3(a0.zw,h.y); 91 | vec3 p2=vec3(a1.xy,h.z); 92 | vec3 p3=vec3(a1.zw,h.w); 93 | 94 | //Normalise gradients 95 | vec4 norm=taylorInvSqrt(vec4(dot(p0,p0),dot(p1,p1),dot(p2,p2),dot(p3,p3))); 96 | p0*=norm.x; 97 | p1*=norm.y; 98 | p2*=norm.z; 99 | p3*=norm.w; 100 | 101 | // Mix final noise value 102 | vec4 m=max(.6-vec4(dot(x0,x0),dot(x1,x1),dot(x2,x2),dot(x3,x3)),0.); 103 | m=m*m; 104 | return 42.*dot(m*m,vec4(dot(p0,x0),dot(p1,x1), 105 | dot(p2,x2),dot(p3,x3))); 106 | } 107 | 108 | float fbm(vec3 p,float progress){ 109 | float value=0.; 110 | float amplitude=1.; 111 | float frequency=1.; 112 | float lacunarity=2.; 113 | float persistance=.5; 114 | // float scale=1.; 115 | // float scale=600.; 116 | float scale=mix(0.,600.,progress); 117 | int octaves=10; 118 | 119 | for(int i=0;i 2 | body { 3 | margin: 0; 4 | background: black; 5 | } 6 | 7 |
8 | 9 | 10 | -------------------------------------------------------------------------------- /html/6-random/fbm-dissolve.html: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | --------------------------------------------------------------------------------