micugl 0.0.13 → 0.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/managers/WebGLManager.d.ts +1 -1
- package/dist/core/managers/WebGLManager.js +1 -1
- package/dist/core/managers/WebGLManager.mjs +6 -6
- package/dist/react/components/engine/PingPongShaderEngine.d.ts +6 -1
- package/dist/react/components/engine/PingPongShaderEngine.js +1 -1
- package/dist/react/components/engine/PingPongShaderEngine.mjs +65 -53
- package/dist/react/components/engine/ShaderEngine.js +1 -1
- package/dist/react/components/engine/ShaderEngine.mjs +27 -27
- package/dist/react/hooks/usePingPongPasses.d.ts +4 -5
- package/dist/react/hooks/usePingPongPasses.js +1 -1
- package/dist/react/hooks/usePingPongPasses.mjs +93 -78
- package/dist/react/hooks/useUniformUpdaters.d.ts +1 -3
- package/dist/react/hooks/useUniformUpdaters.js +1 -1
- package/dist/react/hooks/useUniformUpdaters.mjs +23 -11
- package/package.json +7 -3
|
@@ -21,7 +21,7 @@ export declare class WebGLManager {
|
|
|
21
21
|
updateBuffer(programId: string, attributeName: string, data: Float32Array | Uint8Array | Uint16Array): void;
|
|
22
22
|
registerUniformUpdater<T extends UniformType>(programId: string, uniformName: string, type: T, updateFn: UniformUpdateFn<T>): void;
|
|
23
23
|
updateUniforms(programId: string, time: number): void;
|
|
24
|
-
setSize(
|
|
24
|
+
setSize(renderWidth: number, renderHeight: number, displayWidth?: number, displayHeight?: number): void;
|
|
25
25
|
prepareRender(programId: string, options?: RenderOptions): void;
|
|
26
26
|
fastRender(programId: string, time: number, clear?: boolean): void;
|
|
27
27
|
setUniform<T extends UniformType>(programId: string, uniformName: string, value: UniformTypeMap[T], type: T): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var w=Object.defineProperty;var b=(
|
|
1
|
+
"use strict";var w=Object.defineProperty;var b=(d,r,o)=>r in d?w(d,r,{enumerable:!0,configurable:!0,writable:!0,value:o}):d[r]=o;var m=(d,r,o)=>b(d,typeof r!="symbol"?r+"":r,o);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const E=require("./FBOManager.js");class p{constructor(r,o){m(this,"gl");m(this,"fboManager");m(this,"resources",new Map);m(this,"compileCache",new Map);m(this,"uniformUpdateFns",new Map);m(this,"extensions",new Map);const t={alpha:!1,depth:!1,stencil:!1,antialias:!1,powerPreference:"low-power",preserveDrawingBuffer:!1},e=r.getContext("webgl",{...t,...o});if(!e)throw new Error("WebGL not supported");this.gl=e,this.fboManager=new E.FBOManager(e),this.getExtension("OES_texture_float"),this.getExtension("OES_texture_float_linear"),this.getExtension("OES_vertex_array_object"),this.getExtension("ANGLE_instanced_arrays")}getExtension(r){if(this.extensions.has(r))return this.extensions.get(r);const o=this.gl.getExtension(r);return this.extensions.set(r,o),o}createProgram(r,o){const{vertexShader:t,fragmentShader:e,uniforms:s,attributes:i}=o,n=this.gl,h=this.getOrCompileShader("vertex:"+t,n.VERTEX_SHADER,t),l=this.getOrCompileShader("fragment:"+e,n.FRAGMENT_SHADER,e),f=n.createProgram();if(!f)throw new Error("Failed to create WebGL program");if(n.attachShader(f,h),n.attachShader(f,l),n.linkProgram(f),!n.getProgramParameter(f,n.LINK_STATUS)){const g=n.getProgramInfoLog(f);throw n.deleteProgram(f),new Error(`Could not link shader program: ${g}`)}const c={};for(const g of s)c[g.name]=n.getUniformLocation(f,g.name);const u={};if(i)for(const g of i)u[g.name]=n.getAttribLocation(f,g.name);const a={program:f,uniforms:c,attributes:u,buffers:{}};return this.resources.set(r,a),this.uniformUpdateFns.set(r,new Map),a}getOrCompileShader(r,o,t){if(this.compileCache.has(r)){const s=this.compileCache.get(r);if(s)return s}const e=this.compileShader(o,t);return this.compileCache.set(r,e),e}compileShader(r,o){const t=this.gl,e=t.createShader(r);if(!e)throw new Error("Failed to create shader");if(t.shaderSource(e,o),t.compileShader(e),!t.getShaderParameter(e,t.COMPILE_STATUS)){const s=t.getShaderInfoLog(e);throw t.deleteShader(e),new Error(`Shader compilation failed: ${s}`)}return e}createBuffer(r,o,t){const e=this.gl,s=this.resources.get(r);if(!s)throw new Error(`Program with id ${r} not found`);const i=e.createBuffer();if(!i)throw new Error("Failed to create buffer");return e.bindBuffer(e.ARRAY_BUFFER,i),e.bufferData(e.ARRAY_BUFFER,t,e.STATIC_DRAW),s.buffers[o]={buffer:i,data:t},i}updateBuffer(r,o,t){const e=this.gl,s=this.resources.get(r);if(!s)throw new Error(`Program with id ${r} not found`);const i=s.buffers[o];if(!i)throw new Error(`Buffer for attribute ${o} not found`);e.bindBuffer(e.ARRAY_BUFFER,i.buffer),e.bufferData(e.ARRAY_BUFFER,t,e.STATIC_DRAW),i.data=t}registerUniformUpdater(r,o,t,e){const s=this.resources.get(r);if(!s)throw new Error(`Program with id ${r} not found`);const i=this.uniformUpdateFns.get(r);if(!i)throw new Error(`Program uniforms for id ${r} not found`);const n=s.uniforms[o];if(n===null)return;const h=this.gl;let l;switch(t){case"int":l=(f,c,u)=>{const a=e(f,c,u);return h.uniform1i(n,a),a};break;case"float":l=(f,c,u)=>{const a=e(f,c,u);return h.uniform1f(n,a),a};break;case"sampler2D":l=(f,c,u)=>{const a=e(f,c,u);return h.uniform1i(n,a),a};break;case"vec2":l=(f,c,u)=>{const a=e(f,c,u);return h.uniform2fv(n,a),a};break;case"vec3":l=(f,c,u)=>{const a=e(f,c,u);return h.uniform3fv(n,a),a};break;case"vec4":l=(f,c,u)=>{const a=e(f,c,u);return h.uniform4fv(n,a),a};break;case"mat2":l=(f,c,u)=>{const a=e(f,c,u);return h.uniformMatrix2fv(n,!1,a),a};break;case"mat3":l=(f,c,u)=>{const a=e(f,c,u);return h.uniformMatrix3fv(n,!1,a),a};break;case"mat4":l=(f,c,u)=>{const a=e(f,c,u);return h.uniformMatrix4fv(n,!1,a),a};break;default:throw new Error(`Unsupported uniform type: ${t}`)}i.set(o,l)}updateUniforms(r,o){const t=this.uniformUpdateFns.get(r);if(!t)return;const e=this.gl.canvas,s=e.width,i=e.height;t.forEach(n=>{n(o,s,i)})}setSize(r,o,t,e){const s=this.gl.canvas,i=t??r,n=e??o;(s.width!==r||s.height!==o)&&(s.width=r,s.height=o,this.gl.viewport(0,0,r,o)),s.style.width=`${i}px`,s.style.height=`${n}px`}prepareRender(r,o={}){const{clear:t=!0,clearColor:e=[0,0,0,1]}=o,s=this.gl,i=this.resources.get(r);if(!i)throw new Error(`Program with id ${r} not found`);s.useProgram(i.program),t&&(s.clearColor(...e),s.clear(s.COLOR_BUFFER_BIT))}fastRender(r,o,t=!0){const e=this.gl,s=this.resources.get(r);if(!s)throw new Error(`Program with id ${r} not found`);e.useProgram(s.program),t&&e.clear(e.COLOR_BUFFER_BIT),this.updateUniforms(r,o)}setUniform(r,o,t,e){const s=this.gl,i=this.resources.get(r);if(!i)throw new Error(`Program with id ${r} not found`);const n=i.uniforms[o];if(n!==null)switch(s.useProgram(i.program),e){case"float":s.uniform1f(n,t);break;case"vec2":s.uniform2fv(n,t);break;case"vec3":s.uniform3fv(n,t);break;case"vec4":s.uniform4fv(n,t);break;case"int":s.uniform1i(n,t);break;case"mat2":s.uniformMatrix2fv(n,!1,t);break;case"mat3":s.uniformMatrix3fv(n,!1,t);break;case"mat4":s.uniformMatrix4fv(n,!1,t);break;case"sampler2D":s.uniform1i(n,t);break;default:throw new Error(`Unsupported uniform type: ${e}`)}}setAttributeOnce(r,o,t){const e=this.gl,s=this.resources.get(r);if(!s)throw new Error(`Program with id ${r} not found`);const i=s.attributes[o];if(i===-1){console.warn(`Attribute ${o} not found or is unused`);return}const n=s.buffers[o];if(!n)throw new Error(`Buffer for attribute ${o} not found`);if(e.bindBuffer(e.ARRAY_BUFFER,n.buffer),e.enableVertexAttribArray(i),e.vertexAttribPointer(i,t.size,e[t.type],t.normalized,t.stride,t.offset),t.instanced){const h=this.getExtension("ANGLE_instanced_arrays");if(h!=null&&h.vertexAttribDivisorANGLE)h.vertexAttribDivisorANGLE(i,1);else if(e.vertexAttribDivisor)e.vertexAttribDivisor(i,1);else throw new Error("Instanced rendering not supported")}}drawArrays(r,o,t){this.gl.drawArrays(r,o,t)}drawElements(r,o,t,e){this.gl.drawElements(r,o,t,e)}destroy(r){const o=this.gl,t=this.resources.get(r);t&&(Object.values(t.buffers).forEach(({buffer:e})=>{o.deleteBuffer(e)}),o.deleteProgram(t.program),this.resources.delete(r),this.uniformUpdateFns.delete(r))}destroyAll(){for(const r of Array.from(this.resources.keys()))this.destroy(r);this.compileCache.clear(),this.fboManager.destroyAll()}get context(){return this.gl}get fbo(){return this.fboManager}}exports.WebGLManager=p;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var
|
|
2
|
-
var b = (
|
|
3
|
-
var m = (
|
|
1
|
+
var d = Object.defineProperty;
|
|
2
|
+
var b = (w, e, o) => e in w ? d(w, e, { enumerable: !0, configurable: !0, writable: !0, value: o }) : w[e] = o;
|
|
3
|
+
var m = (w, e, o) => b(w, typeof e != "symbol" ? e + "" : e, o);
|
|
4
4
|
import { FBOManager as E } from "./FBOManager.mjs";
|
|
5
5
|
class A {
|
|
6
6
|
constructor(e, o) {
|
|
@@ -169,9 +169,9 @@ class A {
|
|
|
169
169
|
n(o, s, i);
|
|
170
170
|
});
|
|
171
171
|
}
|
|
172
|
-
setSize(e, o, t
|
|
173
|
-
const
|
|
174
|
-
(
|
|
172
|
+
setSize(e, o, t, r) {
|
|
173
|
+
const s = this.gl.canvas, i = t ?? e, n = r ?? o;
|
|
174
|
+
(s.width !== e || s.height !== o) && (s.width = e, s.height = o, this.gl.viewport(0, 0, e, o)), s.style.width = `${i}px`, s.style.height = `${n}px`;
|
|
175
175
|
}
|
|
176
176
|
prepareRender(e, o = {}) {
|
|
177
177
|
const { clear: t = !0, clearColor: r = [0, 0, 0, 1] } = o, s = this.gl, i = this.resources.get(e);
|
|
@@ -6,7 +6,12 @@ interface PingPongShaderEngineProps {
|
|
|
6
6
|
framebuffers?: Record<string, FramebufferOptions>;
|
|
7
7
|
className?: string;
|
|
8
8
|
style?: CSSProperties;
|
|
9
|
+
width?: number;
|
|
10
|
+
height?: number;
|
|
11
|
+
renderWidth?: number;
|
|
12
|
+
renderHeight?: number;
|
|
9
13
|
useDevicePixelRatio?: boolean;
|
|
14
|
+
pixelRatio?: number;
|
|
10
15
|
}
|
|
11
|
-
export declare const PingPongShaderEngine: ({ programConfigs, passes, framebuffers, className, style, useDevicePixelRatio }: PingPongShaderEngineProps) => import("react/jsx-runtime").JSX.Element;
|
|
16
|
+
export declare const PingPongShaderEngine: ({ programConfigs, passes, framebuffers, className, style, width, height, renderWidth, renderHeight, useDevicePixelRatio, pixelRatio }: PingPongShaderEngineProps) => import("react/jsx-runtime").JSX.Element;
|
|
12
17
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const T=require("react/jsx-runtime"),r=require("react"),q=require("../../../core/managers/WebGLManager.js"),W=require("../../../core/systems/Passes.js"),x="",O={};function M(a){return a.map(c=>`${c.programId}|${c.outputFramebuffer??"screen"}|${c.inputTextures.map(o=>o.id).join(",")}`).join("||")}const k=({programConfigs:a,passes:c,framebuffers:o,className:A=x,style:F=O,width:w,height:g,renderWidth:h,renderHeight:R,useDevicePixelRatio:y=!0,pixelRatio:E})=>{const u=r.useRef(null),s=r.useRef(null),l=r.useRef(null),d=r.useRef(null),b=r.useRef(0),m=r.useRef(""),p=r.useRef(e=>{const t=s.current,n=l.current;if(!t||!n)return;const i=e-b.current;n.execute(i),d.current=requestAnimationFrame(p.current)}),f=r.useCallback(()=>{if(!u.current||!s.current)return;const e=w??window.innerWidth,t=g??window.innerHeight,n=E??(y?window.devicePixelRatio:1),i=h??Math.floor(e*n),L=R??Math.floor(t*n);s.current.setSize(i,L,e,t);const S=s.current,P=S.context.canvas;Object.entries(o??{}).forEach(([j,_])=>{const v=_.width||P.width,z=_.height||P.height;S.fbo.resizeFramebuffer(j,v,z)})},[o,w,g,h,R,y,E]);return r.useEffect(()=>{if(!u.current)return;const e=new q.WebGLManager(u.current);s.current=e,Object.entries(a).forEach(([n,i])=>{e.createProgram(n,i)}),Object.entries(o??{}).forEach(([n,i])=>{e.fbo.createFramebuffer(n,i)});const t=new W.Passes(e);return l.current=t,c.forEach(n=>{t.addPass(n)}),m.current=M(c),t.initializeResources(),f(),b.current=performance.now(),d.current=requestAnimationFrame(p.current),window.addEventListener("resize",f),()=>{window.removeEventListener("resize",f),d.current&&cancelAnimationFrame(d.current),s.current&&s.current.destroyAll()}},[a,o,f]),r.useEffect(()=>{const e=l.current;if(!e)return;const t=M(c);if(t===m.current){typeof window<"u"&&window.__micuglMetrics&&window.__micuglMetrics.engineSkippedInits++;return}m.current=t,typeof window<"u"&&window.__micuglMetrics&&window.__micuglMetrics.engineActualInits++,e.clearPasses(),c.forEach(n=>{e.addPass(n)}),e.initializeResources()},[c]),T.jsx("canvas",{ref:u,className:A,style:{width:"100%",height:"100%",display:"block",...F}})};exports.PingPongShaderEngine=k;
|
|
@@ -1,68 +1,80 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useRef as
|
|
3
|
-
import { WebGLManager as
|
|
4
|
-
import { Passes as
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { jsx as x } from "react/jsx-runtime";
|
|
2
|
+
import { useRef as s, useCallback as W, useEffect as F } from "react";
|
|
3
|
+
import { WebGLManager as k } from "../../../core/managers/WebGLManager.mjs";
|
|
4
|
+
import { Passes as H } from "../../../core/systems/Passes.mjs";
|
|
5
|
+
const I = "", O = {};
|
|
6
|
+
function z(a) {
|
|
7
|
+
return a.map(
|
|
8
|
+
(t) => `${t.programId}|${t.outputFramebuffer ?? "screen"}|${t.inputTextures.map((o) => o.id).join(",")}`
|
|
9
|
+
).join("||");
|
|
10
|
+
}
|
|
11
|
+
const C = ({
|
|
12
|
+
programConfigs: a,
|
|
13
|
+
passes: t,
|
|
8
14
|
framebuffers: o,
|
|
9
|
-
className:
|
|
10
|
-
style:
|
|
11
|
-
|
|
15
|
+
className: L = I,
|
|
16
|
+
style: M = O,
|
|
17
|
+
width: w,
|
|
18
|
+
height: h,
|
|
19
|
+
renderWidth: g,
|
|
20
|
+
renderHeight: p,
|
|
21
|
+
useDevicePixelRatio: y = !0,
|
|
22
|
+
pixelRatio: E
|
|
12
23
|
}) => {
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
if (!
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
}),
|
|
19
|
-
if (!
|
|
20
|
-
const e = window.innerWidth,
|
|
21
|
-
c.current.setSize(
|
|
22
|
-
const
|
|
23
|
-
Object.entries(o ??
|
|
24
|
-
const
|
|
25
|
-
|
|
24
|
+
const u = s(null), c = s(null), m = s(null), d = s(null), R = s(0), l = s(""), _ = s((e) => {
|
|
25
|
+
const r = c.current, n = m.current;
|
|
26
|
+
if (!r || !n) return;
|
|
27
|
+
const i = e - R.current;
|
|
28
|
+
n.execute(i), d.current = requestAnimationFrame(_.current);
|
|
29
|
+
}), f = W(() => {
|
|
30
|
+
if (!u.current || !c.current) return;
|
|
31
|
+
const e = w ?? window.innerWidth, r = h ?? window.innerHeight, n = E ?? (y ? window.devicePixelRatio : 1), i = g ?? Math.floor(e * n), P = p ?? Math.floor(r * n);
|
|
32
|
+
c.current.setSize(i, P, e, r);
|
|
33
|
+
const b = c.current, S = b.context.canvas;
|
|
34
|
+
Object.entries(o ?? {}).forEach(([v, A]) => {
|
|
35
|
+
const j = A.width || S.width, T = A.height || S.height;
|
|
36
|
+
b.fbo.resizeFramebuffer(v, j, T);
|
|
26
37
|
});
|
|
27
|
-
}, [o,
|
|
28
|
-
return
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
38
|
+
}, [o, w, h, g, p, y, E]);
|
|
39
|
+
return F(() => {
|
|
40
|
+
if (!u.current) return;
|
|
41
|
+
const e = new k(u.current);
|
|
42
|
+
c.current = e, Object.entries(a).forEach(([n, i]) => {
|
|
43
|
+
e.createProgram(n, i);
|
|
44
|
+
}), Object.entries(o ?? {}).forEach(([n, i]) => {
|
|
45
|
+
e.fbo.createFramebuffer(n, i);
|
|
46
|
+
});
|
|
47
|
+
const r = new H(e);
|
|
48
|
+
return m.current = r, t.forEach((n) => {
|
|
49
|
+
r.addPass(n);
|
|
50
|
+
}), l.current = z(t), r.initializeResources(), f(), R.current = performance.now(), d.current = requestAnimationFrame(_.current), window.addEventListener("resize", f), () => {
|
|
51
|
+
window.removeEventListener("resize", f), d.current && cancelAnimationFrame(d.current), c.current && c.current.destroyAll();
|
|
52
|
+
};
|
|
53
|
+
}, [a, o, f]), F(() => {
|
|
54
|
+
const e = m.current;
|
|
55
|
+
if (!e) return;
|
|
56
|
+
const r = z(t);
|
|
57
|
+
if (r === l.current) {
|
|
58
|
+
typeof window < "u" && window.__micuglMetrics && window.__micuglMetrics.engineSkippedInits++;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
l.current = r, typeof window < "u" && window.__micuglMetrics && window.__micuglMetrics.engineActualInits++, e.clearPasses(), t.forEach((n) => {
|
|
62
|
+
e.addPass(n);
|
|
63
|
+
}), e.initializeResources();
|
|
64
|
+
}, [t]), /* @__PURE__ */ x(
|
|
53
65
|
"canvas",
|
|
54
66
|
{
|
|
55
|
-
ref:
|
|
56
|
-
className:
|
|
67
|
+
ref: u,
|
|
68
|
+
className: L,
|
|
57
69
|
style: {
|
|
58
70
|
width: "100%",
|
|
59
71
|
height: "100%",
|
|
60
72
|
display: "block",
|
|
61
|
-
...
|
|
73
|
+
...M
|
|
62
74
|
}
|
|
63
75
|
}
|
|
64
76
|
);
|
|
65
77
|
};
|
|
66
78
|
export {
|
|
67
|
-
|
|
79
|
+
C as PingPongShaderEngine
|
|
68
80
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("react/jsx-runtime"),c=require("react"),I=require("../../../core/managers/WebGLManager.js"),h={},v="",M={},U={},q=({programConfigs:E,renderCallback:p,renderOptions:o=h,className:F=v,style:y=M,width:T,height:w,uniformUpdaters:L=U,useFastPath:R=!1,useDevicePixelRatio:S=!0,pixelRatio:_})=>{const f=c.useRef(null),a=c.useRef(null),l=c.useRef(null),u=c.useRef(null),g=c.useRef(0),d=c.useRef(n=>{const e=a.current,r=l.current;if(!e||!r)return;const t=n-g.current,s=e.context;if(R)e.fastRender(r,t,o.clear),s.drawArrays(s.TRIANGLE_STRIP,0,4);else{const i=e.resources.get(r);if(!i)return;e.prepareRender(r,o),A(t,i,s)}u.current=requestAnimationFrame(d.current)}),A=c.useCallback((n,e,r)=>{p(n,e,r)},[p]);c.useEffect(()=>{d.current=n=>{const e=a.current,r=l.current;if(!e||!r)return;const t=n-g.current,s=e.context;if(R)e.fastRender(r,t,o.clear),s.drawArrays(s.TRIANGLE_STRIP,0,4);else{const i=e.resources.get(r);if(!i)return;e.prepareRender(r,o),A(t,i,s)}u.current=requestAnimationFrame(d.current)}},[o,R,A]);const m=c.useCallback(()=>{if(!f.current||!a.current)return;const n=T??window.innerWidth,e=w??window.innerHeight,r=_??(S?window.devicePixelRatio:1),t=Math.floor(n*r),s=Math.floor(e*r);a.current.setSize(t,s,n,e)},[S,_,T,w]);return c.useEffect(()=>{if(!f.current)return;const n=new I.WebGLManager(f.current);a.current=n,m();const[[e,r]]=Object.entries(E);return n.createProgram(e,r),n.createBuffer(e,"a_position",new Float32Array([-1,-1,1,-1,-1,1,1,1])),n.setAttributeOnce(e,"a_position",{name:"a_position",size:2,type:"FLOAT",normalized:!1,stride:0,offset:0}),l.current=e,g.current=performance.now(),u.current=requestAnimationFrame(d.current),window.addEventListener("resize",m),()=>{window.removeEventListener("resize",m),u.current&&cancelAnimationFrame(u.current),n.destroyAll()}},[E,m]),c.useEffect(()=>{const n=a.current,e=l.current;if(!n||!e)return;const r=L[e];r&&r.forEach(t=>{n.registerUniformUpdater(e,t.name,t.type,t.updateFn)})},[L]),b.jsx("canvas",{ref:f,className:F,style:y})};exports.ShaderEngine=q;
|
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useRef as s, useCallback as
|
|
3
|
-
import { WebGLManager as
|
|
4
|
-
const
|
|
1
|
+
import { jsx as h } from "react/jsx-runtime";
|
|
2
|
+
import { useRef as s, useCallback as y, useEffect as g } from "react";
|
|
3
|
+
import { WebGLManager as v } from "../../../core/managers/WebGLManager.mjs";
|
|
4
|
+
const z = {}, N = "", b = {}, D = {}, q = ({
|
|
5
5
|
programConfigs: E,
|
|
6
6
|
renderCallback: T,
|
|
7
|
-
renderOptions: a =
|
|
8
|
-
className:
|
|
9
|
-
style:
|
|
7
|
+
renderOptions: a = z,
|
|
8
|
+
className: I = N,
|
|
9
|
+
style: U = b,
|
|
10
10
|
width: w,
|
|
11
11
|
height: L,
|
|
12
|
-
uniformUpdaters: _ =
|
|
13
|
-
useFastPath:
|
|
12
|
+
uniformUpdaters: _ = D,
|
|
13
|
+
useFastPath: p = !1,
|
|
14
14
|
useDevicePixelRatio: F = !0,
|
|
15
15
|
pixelRatio: S
|
|
16
16
|
}) => {
|
|
17
|
-
const f = s(null), o = s(null), m = s(null), i = s(null),
|
|
17
|
+
const f = s(null), o = s(null), m = s(null), i = s(null), A = s(0), d = s((n) => {
|
|
18
18
|
const e = o.current, r = m.current;
|
|
19
19
|
if (!e || !r) return;
|
|
20
|
-
const t = n -
|
|
21
|
-
if (
|
|
20
|
+
const t = n - A.current, c = e.context;
|
|
21
|
+
if (p)
|
|
22
22
|
e.fastRender(r, t, a.clear), c.drawArrays(c.TRIANGLE_STRIP, 0, 4);
|
|
23
23
|
else {
|
|
24
24
|
const u = e.resources.get(r);
|
|
25
25
|
if (!u) return;
|
|
26
|
-
e.prepareRender(r, a),
|
|
26
|
+
e.prepareRender(r, a), R(t, u, c);
|
|
27
27
|
}
|
|
28
28
|
i.current = requestAnimationFrame(d.current);
|
|
29
|
-
}),
|
|
29
|
+
}), R = y((n, e, r) => {
|
|
30
30
|
T(n, e, r);
|
|
31
31
|
}, [T]);
|
|
32
32
|
g(() => {
|
|
33
33
|
d.current = (n) => {
|
|
34
34
|
const e = o.current, r = m.current;
|
|
35
35
|
if (!e || !r) return;
|
|
36
|
-
const t = n -
|
|
37
|
-
if (
|
|
36
|
+
const t = n - A.current, c = e.context;
|
|
37
|
+
if (p)
|
|
38
38
|
e.fastRender(r, t, a.clear), c.drawArrays(c.TRIANGLE_STRIP, 0, 4);
|
|
39
39
|
else {
|
|
40
40
|
const u = e.resources.get(r);
|
|
41
41
|
if (!u) return;
|
|
42
|
-
e.prepareRender(r, a),
|
|
42
|
+
e.prepareRender(r, a), R(t, u, c);
|
|
43
43
|
}
|
|
44
44
|
i.current = requestAnimationFrame(d.current);
|
|
45
45
|
};
|
|
46
|
-
}, [a,
|
|
47
|
-
const l =
|
|
46
|
+
}, [a, p, R]);
|
|
47
|
+
const l = y(() => {
|
|
48
48
|
if (!f.current || !o.current) return;
|
|
49
|
-
const n = w ?? window.innerWidth, e = L ?? window.innerHeight, r = S ?? (F ? window.devicePixelRatio : 1);
|
|
50
|
-
o.current.setSize(n, e
|
|
49
|
+
const n = w ?? window.innerWidth, e = L ?? window.innerHeight, r = S ?? (F ? window.devicePixelRatio : 1), t = Math.floor(n * r), c = Math.floor(e * r);
|
|
50
|
+
o.current.setSize(t, c, n, e);
|
|
51
51
|
}, [F, S, w, L]);
|
|
52
52
|
return g(() => {
|
|
53
53
|
if (!f.current) return;
|
|
54
|
-
const n = new
|
|
54
|
+
const n = new v(f.current);
|
|
55
55
|
o.current = n, l();
|
|
56
56
|
const [[e, r]] = Object.entries(E);
|
|
57
57
|
return n.createProgram(e, r), n.createBuffer(e, "a_position", new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1])), n.setAttributeOnce(e, "a_position", {
|
|
@@ -61,7 +61,7 @@ const N = {}, b = "", D = {}, P = {}, M = ({
|
|
|
61
61
|
normalized: !1,
|
|
62
62
|
stride: 0,
|
|
63
63
|
offset: 0
|
|
64
|
-
}), m.current = e,
|
|
64
|
+
}), m.current = e, A.current = performance.now(), i.current = requestAnimationFrame(d.current), window.addEventListener("resize", l), () => {
|
|
65
65
|
window.removeEventListener("resize", l), i.current && cancelAnimationFrame(i.current), n.destroyAll();
|
|
66
66
|
};
|
|
67
67
|
}, [E, l]), g(() => {
|
|
@@ -71,15 +71,15 @@ const N = {}, b = "", D = {}, P = {}, M = ({
|
|
|
71
71
|
r && r.forEach((t) => {
|
|
72
72
|
n.registerUniformUpdater(e, t.name, t.type, t.updateFn);
|
|
73
73
|
});
|
|
74
|
-
}, [_]), /* @__PURE__ */
|
|
74
|
+
}, [_]), /* @__PURE__ */ h(
|
|
75
75
|
"canvas",
|
|
76
76
|
{
|
|
77
77
|
ref: f,
|
|
78
|
-
className:
|
|
79
|
-
style:
|
|
78
|
+
className: I,
|
|
79
|
+
style: U
|
|
80
80
|
}
|
|
81
81
|
);
|
|
82
82
|
};
|
|
83
83
|
export {
|
|
84
|
-
|
|
84
|
+
q as ShaderEngine
|
|
85
85
|
};
|
|
@@ -12,10 +12,9 @@ interface PingPongPassesOptions {
|
|
|
12
12
|
};
|
|
13
13
|
customPasses?: RenderPass[];
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
interface PingPongPassesResult {
|
|
16
16
|
passes: RenderPass[];
|
|
17
|
-
framebuffers:
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
17
|
+
framebuffers: Record<string, FramebufferOptions>;
|
|
18
|
+
}
|
|
19
|
+
export declare const usePingPongPasses: ({ programId, secondaryProgramId, iterations, uniforms, secondaryUniforms, framebufferOptions, renderOptions, customPasses }: PingPongPassesOptions) => PingPongPassesResult;
|
|
21
20
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("react"),R=require("./useUniformUpdaters.js"),C={width:0,height:0,textureCount:2,textureOptions:{minFilter:WebGLRenderingContext.LINEAR,magFilter:WebGLRenderingContext.LINEAR}},N={clear:!0};function A(e){return`${e.width}|${e.height}|${e.textureCount??1}|${JSON.stringify(e.textureOptions??{})}`}function L(e){return`${e.clear??!0}|${JSON.stringify(e.clearColor??[0,0,0,1])}`}function O(e,t,a,h,U,o,s,r){const i=`${e}-fb-a`,f=`${e}-fb-b`,c={[i]:o,[f]:o};let u=[];if(r)u=r;else{u.push({programId:e,inputTextures:[],outputFramebuffer:i,renderOptions:s});let b=i;for(let n=0;n<a;n++){const w=t&&n%2===1?t:e,_=n%2===0?i:f,p=n%2===0?f:i;b=p;const g=t&&n%2===1?U[t]:h[e],y={};g.forEach(x=>{const T=x.updateFn;y[x.name]={type:x.type,value:(d,E,M)=>T(d,E,M)}}),u.push({programId:w,inputTextures:[{id:_,textureUnit:0,bindingType:"read"}],outputFramebuffer:p,uniforms:y,renderOptions:s})}const F=b,l={};(t?U[t]:h[e]).forEach(n=>{const w=n.updateFn;l[n.name]={type:n.type,value:(_,p,g)=>w(_,p,g)}}),u.push({programId:t??e,inputTextures:[{id:F,textureUnit:0,bindingType:"read"}],outputFramebuffer:null,uniforms:l,renderOptions:s})}return{passes:u,framebuffers:c}}const k=({programId:e,secondaryProgramId:t,iterations:a=1,uniforms:h,secondaryUniforms:U={},framebufferOptions:o=C,renderOptions:s=N,customPasses:r})=>{const i=R.useUniformUpdaters(e,h),f=R.useUniformUpdaters(t??`${e}-secondary`,U),c=S.useRef(null),u=A(o),b=L(s),F=r?r.length.toString():"none",l=`${e}|${t??""}|${a}|${u}|${b}|${F}`;if(c.current&&c.current.key===l)return typeof window<"u"&&window.__micuglMetrics&&window.__micuglMetrics.hookCacheHits++,c.current.result;typeof window<"u"&&window.__micuglMetrics&&window.__micuglMetrics.hookCacheMisses++;const $=O(e,t,a,i,f,o,s,r);return c.current={key:l,result:$},$};exports.usePingPongPasses=k;
|
|
@@ -1,93 +1,108 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useUniformUpdaters as
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const v = h.updateFn;
|
|
44
|
-
T[h.name] = {
|
|
45
|
-
type: h.type,
|
|
46
|
-
value: (A, M, $) => v(A, M, $)
|
|
47
|
-
};
|
|
48
|
-
}), i.push({
|
|
49
|
-
programId: r,
|
|
50
|
-
inputTextures: [{
|
|
51
|
-
id: b,
|
|
52
|
-
textureUnit: 0,
|
|
53
|
-
bindingType: "read"
|
|
54
|
-
}],
|
|
55
|
-
outputFramebuffer: x,
|
|
56
|
-
uniforms: T,
|
|
57
|
-
renderOptions: s
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
const R = o % 2 === 0 ? f : u, a = {};
|
|
61
|
-
(n ? U[n] : p[t]).forEach((e) => {
|
|
62
|
-
const r = e.updateFn;
|
|
63
|
-
a[e.name] = {
|
|
64
|
-
type: e.type,
|
|
65
|
-
value: (b, x, F) => r(b, x, F)
|
|
1
|
+
import { useRef as N } from "react";
|
|
2
|
+
import { useUniformUpdaters as T } from "./useUniformUpdaters.mjs";
|
|
3
|
+
const d = {
|
|
4
|
+
width: 0,
|
|
5
|
+
height: 0,
|
|
6
|
+
textureCount: 2,
|
|
7
|
+
textureOptions: {
|
|
8
|
+
minFilter: WebGLRenderingContext.LINEAR,
|
|
9
|
+
magFilter: WebGLRenderingContext.LINEAR
|
|
10
|
+
}
|
|
11
|
+
}, A = { clear: !0 };
|
|
12
|
+
function L(t) {
|
|
13
|
+
return `${t.width}|${t.height}|${t.textureCount ?? 1}|${JSON.stringify(t.textureOptions ?? {})}`;
|
|
14
|
+
}
|
|
15
|
+
function S(t) {
|
|
16
|
+
return `${t.clear ?? !0}|${JSON.stringify(t.clearColor ?? [0, 0, 0, 1])}`;
|
|
17
|
+
}
|
|
18
|
+
function m(t, e, h, a, p, o, s, c) {
|
|
19
|
+
const i = `${t}-fb-a`, f = `${t}-fb-b`, r = {
|
|
20
|
+
[i]: o,
|
|
21
|
+
[f]: o
|
|
22
|
+
};
|
|
23
|
+
let u = [];
|
|
24
|
+
if (c)
|
|
25
|
+
u = c;
|
|
26
|
+
else {
|
|
27
|
+
u.push({
|
|
28
|
+
programId: t,
|
|
29
|
+
inputTextures: [],
|
|
30
|
+
outputFramebuffer: i,
|
|
31
|
+
renderOptions: s
|
|
32
|
+
});
|
|
33
|
+
let F = i;
|
|
34
|
+
for (let n = 0; n < h; n++) {
|
|
35
|
+
const U = e && n % 2 === 1 ? e : t, x = n % 2 === 0 ? i : f, $ = n % 2 === 0 ? f : i;
|
|
36
|
+
F = $;
|
|
37
|
+
const _ = e && n % 2 === 1 ? p[e] : a[t], E = {};
|
|
38
|
+
_.forEach((R) => {
|
|
39
|
+
const y = R.updateFn;
|
|
40
|
+
E[R.name] = {
|
|
41
|
+
type: R.type,
|
|
42
|
+
value: (M, g, C) => y(M, g, C)
|
|
66
43
|
};
|
|
67
|
-
}),
|
|
68
|
-
programId:
|
|
44
|
+
}), u.push({
|
|
45
|
+
programId: U,
|
|
69
46
|
inputTextures: [{
|
|
70
|
-
id:
|
|
47
|
+
id: x,
|
|
71
48
|
textureUnit: 0,
|
|
72
49
|
bindingType: "read"
|
|
73
50
|
}],
|
|
74
|
-
outputFramebuffer:
|
|
75
|
-
uniforms:
|
|
51
|
+
outputFramebuffer: $,
|
|
52
|
+
uniforms: E,
|
|
76
53
|
renderOptions: s
|
|
77
54
|
});
|
|
78
55
|
}
|
|
79
|
-
|
|
80
|
-
|
|
56
|
+
const b = F, l = {};
|
|
57
|
+
(e ? p[e] : a[t]).forEach((n) => {
|
|
58
|
+
const U = n.updateFn;
|
|
59
|
+
l[n.name] = {
|
|
60
|
+
type: n.type,
|
|
61
|
+
value: (x, $, _) => U(x, $, _)
|
|
62
|
+
};
|
|
63
|
+
}), u.push({
|
|
64
|
+
programId: e ?? t,
|
|
65
|
+
inputTextures: [{
|
|
66
|
+
id: b,
|
|
67
|
+
textureUnit: 0,
|
|
68
|
+
bindingType: "read"
|
|
69
|
+
}],
|
|
70
|
+
outputFramebuffer: null,
|
|
71
|
+
uniforms: l,
|
|
72
|
+
renderOptions: s
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return { passes: u, framebuffers: r };
|
|
76
|
+
}
|
|
77
|
+
const K = ({
|
|
78
|
+
programId: t,
|
|
79
|
+
secondaryProgramId: e,
|
|
80
|
+
iterations: h = 1,
|
|
81
|
+
uniforms: a,
|
|
82
|
+
secondaryUniforms: p = {},
|
|
83
|
+
framebufferOptions: o = d,
|
|
84
|
+
renderOptions: s = A,
|
|
85
|
+
customPasses: c
|
|
86
|
+
}) => {
|
|
87
|
+
const i = T(t, a), f = T(
|
|
88
|
+
e ?? `${t}-secondary`,
|
|
89
|
+
p
|
|
90
|
+
), r = N(null), u = L(o), F = S(s), b = c ? c.length.toString() : "none", l = `${t}|${e ?? ""}|${h}|${u}|${F}|${b}`;
|
|
91
|
+
if (r.current && r.current.key === l)
|
|
92
|
+
return typeof window < "u" && window.__micuglMetrics && window.__micuglMetrics.hookCacheHits++, r.current.result;
|
|
93
|
+
typeof window < "u" && window.__micuglMetrics && window.__micuglMetrics.hookCacheMisses++;
|
|
94
|
+
const w = m(
|
|
81
95
|
t,
|
|
82
|
-
|
|
96
|
+
e,
|
|
97
|
+
h,
|
|
98
|
+
i,
|
|
99
|
+
f,
|
|
83
100
|
o,
|
|
84
|
-
p,
|
|
85
|
-
U,
|
|
86
|
-
l,
|
|
87
101
|
s,
|
|
88
102
|
c
|
|
89
|
-
|
|
103
|
+
);
|
|
104
|
+
return r.current = { key: l, result: w }, w;
|
|
90
105
|
};
|
|
91
106
|
export {
|
|
92
|
-
|
|
107
|
+
K as usePingPongPasses
|
|
93
108
|
};
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { UniformParam, UniformUpdaterDef } from '../../types';
|
|
2
2
|
export declare const useUniformUpdaters: (programId: string, uniforms: Record<string, UniformParam>, options?: {
|
|
3
3
|
skipDefaultUniforms?: boolean;
|
|
4
|
-
}) =>
|
|
5
|
-
[x: string]: UniformUpdaterDef<import('../../types').UniformType>[];
|
|
6
|
-
};
|
|
4
|
+
}) => Record<string, UniformUpdaterDef<import('../../types').UniformType>[]>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react"),l=require("../lib/createUniformUpdater.js");function y(n){return Object.keys(n).sort().map(t=>{const e=n[t],c=typeof e.value=="function"?e.value.toString():JSON.stringify(e.value);return`${t}:${e.type}:${c}`}).join("|")}const _=(n,i,t)=>{const e=d.useRef(null),c=(t==null?void 0:t.skipDefaultUniforms)??!1,s=`${n}|${c}|${y(i)}`;if(e.current&&e.current.key===s)return typeof window<"u"&&window.__micuglMetrics&&window.__micuglMetrics.hookCacheHits++,e.current.result;typeof window<"u"&&window.__micuglMetrics&&window.__micuglMetrics.hookCacheMisses++;const u=c?[]:l.createCommonUpdaters().filter(r=>r.name==="u_time"&&!("u_time"in i)||r.name==="u_resolution"&&!("u_resolution"in i));Object.entries(i).forEach(([r,a])=>{const f=r.startsWith("u_")?r:`u_${r}`;u.push(l.createUniformUpdater(f,a.type,a.value))});const o={[n]:u};return e.current={key:s,result:o},o};exports.useUniformUpdaters=_;
|
|
@@ -1,14 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createCommonUpdaters as
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { useRef as l } from "react";
|
|
2
|
+
import { createCommonUpdaters as _, createUniformUpdater as d } from "../lib/createUniformUpdater.mjs";
|
|
3
|
+
function m(i) {
|
|
4
|
+
return Object.keys(i).sort().map((t) => {
|
|
5
|
+
const e = i[t], c = typeof e.value == "function" ? e.value.toString() : JSON.stringify(e.value);
|
|
6
|
+
return `${t}:${e.type}:${c}`;
|
|
7
|
+
}).join("|");
|
|
8
|
+
}
|
|
9
|
+
const p = (i, n, t) => {
|
|
10
|
+
const e = l(null), c = (t == null ? void 0 : t.skipDefaultUniforms) ?? !1, s = `${i}|${c}|${m(n)}`;
|
|
11
|
+
if (e.current && e.current.key === s)
|
|
12
|
+
return typeof window < "u" && window.__micuglMetrics && window.__micuglMetrics.hookCacheHits++, e.current.result;
|
|
13
|
+
typeof window < "u" && window.__micuglMetrics && window.__micuglMetrics.hookCacheMisses++;
|
|
14
|
+
const o = c ? [] : _().filter(
|
|
15
|
+
(r) => r.name === "u_time" && !("u_time" in n) || r.name === "u_resolution" && !("u_resolution" in n)
|
|
6
16
|
);
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
})
|
|
11
|
-
|
|
17
|
+
Object.entries(n).forEach(([r, f]) => {
|
|
18
|
+
const a = r.startsWith("u_") ? r : `u_${r}`;
|
|
19
|
+
o.push(d(a, f.type, f.value));
|
|
20
|
+
});
|
|
21
|
+
const u = { [i]: o };
|
|
22
|
+
return e.current = { key: s, result: u }, u;
|
|
23
|
+
};
|
|
12
24
|
export {
|
|
13
|
-
|
|
25
|
+
p as useUniformUpdaters
|
|
14
26
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "micugl",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"description": "Micu gl - Small React shader library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"dist"
|
|
28
28
|
],
|
|
29
29
|
"scripts": {
|
|
30
|
+
"dev": "vite --config demo/vite.config.ts",
|
|
30
31
|
"build": "vite build",
|
|
31
32
|
"test": "vitest run",
|
|
32
33
|
"lint": "eslint . --ext .ts,.tsx",
|
|
@@ -39,8 +40,11 @@
|
|
|
39
40
|
"devDependencies": {
|
|
40
41
|
"@eslint/js": "^9.25.0",
|
|
41
42
|
"@types/node": "^22.14.0",
|
|
42
|
-
"@types/react": "^
|
|
43
|
-
"@types/react-dom": "^
|
|
43
|
+
"@types/react": "^19.0.0",
|
|
44
|
+
"@types/react-dom": "^19.0.0",
|
|
45
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
46
|
+
"react": "^19.0.0",
|
|
47
|
+
"react-dom": "^19.0.0",
|
|
44
48
|
"ajv": "^8",
|
|
45
49
|
"eslint": "^9.25.0",
|
|
46
50
|
"eslint-plugin-react-dom": "^1.48.4",
|