@paper-design/shaders
Advanced tools
Comparing version 0.0.12 to 0.0.13
/** The core Shader Mounting class. Pass it a canvas element and a fragment shader to get started. */ | ||
export { ShaderMount } from './shader-mount'; | ||
/** An example fragment shader that renders a grainy texture over top of blobby animated clouds */ | ||
export { grainCloudsFragmentShader, type GrainCloudsUniforms, } from './shaders/grain-clouds'; | ||
export { grainCloudsFragmentShader, type GrainCloudsUniforms } from './shaders/grain-clouds'; | ||
/** A shader that renders a mesh gradient with a rotating noise pattern and several layers of fractal noise */ | ||
export { meshGradientFragmentShader, type MeshGradientUniforms } from './shaders/mesh-gradient'; | ||
export { getShaderColorFromString } from './shader-mount'; |
@@ -1,2 +0,2 @@ | ||
var p=class{canvas;gl;program=null;uniformLocations={};fragmentShader;rafId=null;lastFrameTime=0;totalAnimationTime=0;isAnimated;providedUniforms;hasBeenDisposed=!1;resolutionChanged=!0;constructor(e,r,i={},n,s=!0){this.canvas=e,this.fragmentShader=r,this.providedUniforms=i,this.isAnimated=s;let o=e.getContext("webgl",n);if(!o)throw new Error("WebGL not supported");this.gl=o,this.initWebGL(),this.setupResizeObserver(),this.isAnimated?this.startAnimating():this.render(performance.now())}initWebGL=()=>{let e=x(this.gl,v,this.fragmentShader);e&&(this.program=e,this.setupPositionAttribute(),this.setupUniforms())};setupPositionAttribute=()=>{let e=this.gl.getAttribLocation(this.program,"a_position"),r=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,r);let i=[-1,-1,1,-1,-1,1,-1,1,1,-1,1,1];this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array(i),this.gl.STATIC_DRAW),this.gl.enableVertexAttribArray(e),this.gl.vertexAttribPointer(e,2,this.gl.FLOAT,!1,0,0)};setupUniforms=()=>{this.uniformLocations={u_time:this.gl.getUniformLocation(this.program,"u_time"),u_resolution:this.gl.getUniformLocation(this.program,"u_resolution"),...Object.fromEntries(Object.keys(this.providedUniforms).map(e=>[e,this.gl.getUniformLocation(this.program,e)]))}};resizeObserver=null;setupResizeObserver=()=>{this.resizeObserver=new ResizeObserver(()=>this.handleResize()),this.resizeObserver.observe(this.canvas),this.handleResize()};handleResize=()=>{let e=this.canvas.clientWidth,r=this.canvas.clientHeight;(this.canvas.width!==e||this.canvas.height!==r)&&(this.canvas.width=e,this.canvas.height=r,this.resolutionChanged=!0,this.gl.viewport(0,0,this.gl.canvas.width,this.gl.canvas.height),this.render(performance.now()))};render=e=>{if(this.hasBeenDisposed)return;let r=e-this.lastFrameTime;this.lastFrameTime=e,this.isAnimated&&(this.totalAnimationTime+=r),this.gl.useProgram(this.program),this.gl.uniform1f(this.uniformLocations.u_time,this.totalAnimationTime*.001),this.resolutionChanged&&(this.gl.uniform2f(this.uniformLocations.u_resolution,this.gl.canvas.width,this.gl.canvas.height),this.resolutionChanged=!1),this.gl.drawArrays(this.gl.TRIANGLES,0,6),this.isAnimated?this.requestRender():this.rafId=null};requestRender=()=>{this.rafId!==null&&cancelAnimationFrame(this.rafId),this.rafId=requestAnimationFrame(this.render)};updateProvidedUniforms=()=>{this.gl.useProgram(this.program),Object.entries(this.providedUniforms).forEach(([e,r])=>{let i=this.uniformLocations[e];if(i)if(Array.isArray(r))switch(r.length){case 2:this.gl.uniform2fv(i,r);break;case 3:this.gl.uniform3fv(i,r);break;case 4:this.gl.uniform4fv(i,r);break;default:r.length===9?this.gl.uniformMatrix3fv(i,!1,r):r.length===16?this.gl.uniformMatrix4fv(i,!1,r):console.warn(`Unsupported uniform array length: ${r.length}`)}else typeof r=="number"?this.gl.uniform1f(i,r):typeof r=="boolean"?this.gl.uniform1i(i,r?1:0):console.warn(`Unsupported uniform type for ${e}: ${typeof r}`)})};startAnimating=()=>{this.isAnimated=!0,this.lastFrameTime=performance.now(),this.rafId=requestAnimationFrame(this.render)};stopAnimating=()=>{this.isAnimated=!1,this.rafId&&(cancelAnimationFrame(this.rafId),this.rafId=null)};setUniforms=e=>{this.providedUniforms={...this.providedUniforms,...e},this.updateProvidedUniforms(),this.render(performance.now())};dispose=()=>{this.hasBeenDisposed=!0,this.stopAnimating(),this.gl&&this.program&&(this.gl.deleteProgram(this.program),this.program=null,this.gl.bindBuffer(this.gl.ARRAY_BUFFER,null),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,null),this.gl.bindRenderbuffer(this.gl.RENDERBUFFER,null),this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.getError()),this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=null),this.uniformLocations={}}},v=` | ||
var g=class{canvas;gl;program=null;uniformLocations={};fragmentShader;rafId=null;lastFrameTime=0;totalAnimationTime=0;isAnimated;providedUniforms;hasBeenDisposed=!1;resolutionChanged=!0;constructor(t,r,i={},n,s=!0){this.canvas=t,this.fragmentShader=r,this.providedUniforms=i,this.isAnimated=s;let o=t.getContext("webgl",n);if(!o)throw new Error("WebGL not supported");this.gl=o,this.initWebGL(),this.setupResizeObserver(),this.isAnimated?this.startAnimating():this.render(performance.now())}initWebGL=()=>{let t=x(this.gl,b,this.fragmentShader);t&&(this.program=t,this.setupPositionAttribute(),this.setupUniforms())};setupPositionAttribute=()=>{let t=this.gl.getAttribLocation(this.program,"a_position"),r=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,r);let i=[-1,-1,1,-1,-1,1,-1,1,1,-1,1,1];this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array(i),this.gl.STATIC_DRAW),this.gl.enableVertexAttribArray(t),this.gl.vertexAttribPointer(t,2,this.gl.FLOAT,!1,0,0)};setupUniforms=()=>{this.uniformLocations={u_time:this.gl.getUniformLocation(this.program,"u_time"),u_resolution:this.gl.getUniformLocation(this.program,"u_resolution"),...Object.fromEntries(Object.keys(this.providedUniforms).map(t=>[t,this.gl.getUniformLocation(this.program,t)]))}};resizeObserver=null;setupResizeObserver=()=>{this.resizeObserver=new ResizeObserver(()=>this.handleResize()),this.resizeObserver.observe(this.canvas),this.handleResize()};handleResize=()=>{let t=this.canvas.clientWidth,r=this.canvas.clientHeight;(this.canvas.width!==t||this.canvas.height!==r)&&(this.canvas.width=t,this.canvas.height=r,this.resolutionChanged=!0,this.gl.viewport(0,0,this.gl.canvas.width,this.gl.canvas.height),this.render(performance.now()))};render=t=>{if(this.hasBeenDisposed)return;let r=t-this.lastFrameTime;this.lastFrameTime=t,this.isAnimated&&(this.totalAnimationTime+=r),this.gl.useProgram(this.program),this.gl.uniform1f(this.uniformLocations.u_time,this.totalAnimationTime*.001),this.resolutionChanged&&(this.gl.uniform2f(this.uniformLocations.u_resolution,this.gl.canvas.width,this.gl.canvas.height),this.resolutionChanged=!1),this.gl.drawArrays(this.gl.TRIANGLES,0,6),this.isAnimated?this.requestRender():this.rafId=null};requestRender=()=>{this.rafId!==null&&cancelAnimationFrame(this.rafId),this.rafId=requestAnimationFrame(this.render)};updateProvidedUniforms=()=>{this.gl.useProgram(this.program),Object.entries(this.providedUniforms).forEach(([t,r])=>{let i=this.uniformLocations[t];if(i)if(Array.isArray(r))switch(r.length){case 2:this.gl.uniform2fv(i,r);break;case 3:this.gl.uniform3fv(i,r);break;case 4:this.gl.uniform4fv(i,r);break;default:r.length===9?this.gl.uniformMatrix3fv(i,!1,r):r.length===16?this.gl.uniformMatrix4fv(i,!1,r):console.warn(`Unsupported uniform array length: ${r.length}`)}else typeof r=="number"?this.gl.uniform1f(i,r):typeof r=="boolean"?this.gl.uniform1i(i,r?1:0):console.warn(`Unsupported uniform type for ${t}: ${typeof r}`)})};startAnimating=()=>{this.isAnimated=!0,this.lastFrameTime=performance.now(),this.rafId=requestAnimationFrame(this.render)};stopAnimating=()=>{this.isAnimated=!1,this.rafId&&(cancelAnimationFrame(this.rafId),this.rafId=null)};setUniforms=t=>{this.providedUniforms={...this.providedUniforms,...t},this.updateProvidedUniforms(),this.render(performance.now())};dispose=()=>{this.hasBeenDisposed=!0,this.stopAnimating(),this.gl&&this.program&&(this.gl.deleteProgram(this.program),this.program=null,this.gl.bindBuffer(this.gl.ARRAY_BUFFER,null),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,null),this.gl.bindRenderbuffer(this.gl.RENDERBUFFER,null),this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.getError()),this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=null),this.uniformLocations={}}},b=` | ||
attribute vec4 a_position; | ||
@@ -6,3 +6,3 @@ void main() { | ||
} | ||
`;function b(t,e,r){let i=t.createShader(e);return i?(t.shaderSource(i,r),t.compileShader(i),t.getShaderParameter(i,t.COMPILE_STATUS)?i:(console.error("An error occurred compiling the shaders: "+t.getShaderInfoLog(i)),t.deleteShader(i),null)):null}function x(t,e,r){let i=b(t,t.VERTEX_SHADER,e),n=b(t,t.FRAGMENT_SHADER,r);if(!i||!n)return null;let s=t.createProgram();return s?(t.attachShader(s,i),t.attachShader(s,n),t.linkProgram(s),t.getProgramParameter(s,t.LINK_STATUS)?(t.detachShader(s,i),t.detachShader(s,n),t.deleteShader(i),t.deleteShader(n),s):(console.error("Unable to initialize the shader program: "+t.getProgramInfoLog(s)),t.deleteProgram(s),t.deleteShader(i),t.deleteShader(n),null)):null}function A(t){let e,r,i;if(t.startsWith("#"))[e,r,i]=_(t);else if(t.startsWith("rgb"))[e,r,i]=R(t);else if(t.startsWith("hsl"))[e,r,i]=y(S(t));else return console.error("Unsupported color format"),[0,0,0];return[e,r,i]}function _(t){let e=parseInt(t.slice(1),16),r=(e>>16&255)/255,i=(e>>8&255)/255,n=(e&255)/255;return[r,i,n]}function R(t){let e=t.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);return e?[parseInt(e[1]??"0")/255,parseInt(e[2]??"0")/255,parseInt(e[3]??"0")/255]:[0,0,0]}function S(t){let e=t.match(/^hsl\s*\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)$/i);return e?[parseInt(e[1]??"0"),parseInt(e[2]??"0"),parseInt(e[3]??"0")]:[0,0,0]}function y(t){let[e,r,i]=t,n=e/360,s=r/100,o=i/100,m,l,c;if(r===0)m=l=c=o;else{let f=(h,g,a)=>(a<0&&(a+=1),a>1&&(a-=1),a<.16666666666666666?h+(g-h)*6*a:a<.5?g:a<.6666666666666666?h+(g-h)*(.6666666666666666-a)*6:h),u=o<.5?o*(1+s):o+s-o*s,d=2*o-u;m=f(d,u,n+1/3),l=f(d,u,n),c=f(d,u,n-1/3)}return[m,l,c]}var L=` | ||
`;function v(e,t,r){let i=e.createShader(t);return i?(e.shaderSource(i,r),e.compileShader(i),e.getShaderParameter(i,e.COMPILE_STATUS)?i:(console.error("An error occurred compiling the shaders: "+e.getShaderInfoLog(i)),e.deleteShader(i),null)):null}function x(e,t,r){let i=v(e,e.VERTEX_SHADER,t),n=v(e,e.FRAGMENT_SHADER,r);if(!i||!n)return null;let s=e.createProgram();return s?(e.attachShader(s,i),e.attachShader(s,n),e.linkProgram(s),e.getProgramParameter(s,e.LINK_STATUS)?(e.detachShader(s,i),e.detachShader(s,n),e.deleteShader(i),e.deleteShader(n),s):(console.error("Unable to initialize the shader program: "+e.getProgramInfoLog(s)),e.deleteProgram(s),e.deleteShader(i),e.deleteShader(n),null)):null}function _(e,t=[0,0,0]){if(typeof e!="string")return t;let r,i,n;if(e.startsWith("#"))[r,i,n]=A(e);else if(e.startsWith("rgb"))[r,i,n]=y(e);else if(e.startsWith("hsl"))[r,i,n]=R(S(e));else return console.error("Unsupported color format"),t;return[r,i,n]}function A(e){e.length===4&&(e=`#${e[1]}${e[1]}${e[2]}${e[2]}${e[3]}${e[3]}`);let t=parseInt(e.slice(1),16),r=(t>>16&255)/255,i=(t>>8&255)/255,n=(t&255)/255;return[r,i,n]}function y(e){let t=e.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);return t?[parseInt(t[1]??"0")/255,parseInt(t[2]??"0")/255,parseInt(t[3]??"0")/255]:[0,0,0]}function S(e){let t=e.match(/^hsl\s*\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)$/i);return t?[parseInt(t[1]??"0"),parseInt(t[2]??"0"),parseInt(t[3]??"0")]:[0,0,0]}function R(e){let[t,r,i]=e,n=t/360,s=r/100,o=i/100,c,h,l;if(r===0)c=h=l=o;else{let f=(u,p,a)=>(a<0&&(a+=1),a>1&&(a-=1),a<.16666666666666666?u+(p-u)*6*a:a<.5?p:a<.6666666666666666?u+(p-u)*(.6666666666666666-a)*6:u),m=o<.5?o*(1+s):o+s-o*s,d=2*o-m;c=f(d,m,n+1/3),h=f(d,m,n),l=f(d,m,n-1/3)}return[c,h,l]}var w=` | ||
precision highp float; | ||
@@ -80,3 +80,79 @@ uniform vec2 u_resolution; | ||
} | ||
`;export{p as ShaderMount,A as getShaderColorFromString,L as grainCloudsFragmentShader}; | ||
`;var F=` | ||
// Mesh Gradient: https://www.shadertoy.com/view/wdyczG | ||
#define S(a,b,t) smoothstep(a,b,t) | ||
precision highp float; | ||
uniform vec3 u_color1; | ||
uniform vec3 u_color2; | ||
uniform vec3 u_color3; | ||
uniform vec3 u_color4; | ||
uniform vec2 u_resolution; | ||
uniform float u_time; | ||
uniform float u_speed; | ||
mat2 Rot(float a) | ||
{ | ||
float s = sin(a); | ||
float c = cos(a); | ||
return mat2(c, -s, s, c); | ||
} | ||
vec2 hash(vec2 p) { | ||
vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973)); | ||
p3 += dot(p3, p3.yzx + 33.33); | ||
return fract((p3.xx+p3.yz)*p3.zy); | ||
} | ||
float noise( in vec2 p ) | ||
{ | ||
vec2 i = floor( p ); | ||
vec2 f = fract( p ); | ||
vec2 u = f*f*(3.0-2.0*f); | ||
float n = mix( mix( dot( -1.0+2.0*hash( i + vec2(0.0,0.0) ), f - vec2(0.0,0.0) ), | ||
dot( -1.0+2.0*hash( i + vec2(1.0,0.0) ), f - vec2(1.0,0.0) ), u.x), | ||
mix( dot( -1.0+2.0*hash( i + vec2(0.0,1.0) ), f - vec2(0.0,1.0) ), | ||
dot( -1.0+2.0*hash( i + vec2(1.0,1.0) ), f - vec2(1.0,1.0) ), u.x), u.y); | ||
return 0.5 + 0.5*n; | ||
} | ||
void main() { | ||
vec2 uv = gl_FragCoord.xy / u_resolution.xy; | ||
float ratio = u_resolution.x / u_resolution.y; | ||
vec2 tuv = uv; | ||
tuv -= .5; | ||
// rotate with Noise | ||
float degree = noise(vec2(u_time * u_speed, tuv.x * tuv.y)); | ||
tuv.y *= 1./ratio; | ||
tuv *= Rot(radians((degree-.5)*720.+180.)); | ||
tuv.y *= ratio; | ||
// Wave warp with sin | ||
float frequency = 5.; | ||
float amplitude = 30.; | ||
float speed = u_time * 2.; | ||
tuv.x += sin(tuv.y*frequency+speed)/amplitude; | ||
tuv.y += sin(tuv.x*frequency*1.5+speed)/(amplitude*.5); | ||
// draw the image | ||
vec3 layer1 = mix(u_color1, u_color2, S(-.3, .2, (tuv*Rot(radians(-5.))).x)); | ||
vec3 layer2 = mix(u_color3, u_color4, S(-.3, .2, (tuv*Rot(radians(-5.))).x)); | ||
vec3 finalComp = mix(layer1, layer2, S(.5, -.3, tuv.y)); | ||
vec3 col = finalComp; | ||
gl_FragColor = vec4(col,1.0); | ||
} | ||
`;export{g as ShaderMount,_ as getShaderColorFromString,w as grainCloudsFragmentShader,F as meshGradientFragmentShader}; | ||
//# sourceMappingURL=index.js.map |
@@ -42,2 +42,2 @@ export declare class ShaderMount { | ||
/** Convert color string from HSL, RGB, or hex to 0-to-1-range-RGB array */ | ||
export declare function getShaderColorFromString(colorString: string): [number, number, number]; | ||
export declare function getShaderColorFromString(colorString: string | undefined, fallback?: [number, number, number]): [number, number, number]; |
{ | ||
"name": "@paper-design/shaders", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"license": "MIT", | ||
@@ -15,2 +15,3 @@ "type": "module", | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js", | ||
"default": "./dist/index.js" | ||
@@ -17,0 +18,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46403
9
248