micugl 0.0.15 → 0.0.16
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.js +1 -1
- package/dist/core/managers/WebGLManager.mjs +118 -91
- package/dist/react/components/engine/ShaderEngine.js +1 -1
- package/dist/react/components/engine/ShaderEngine.mjs +50 -46
- package/dist/react/hooks/useUniformUpdaters.d.ts +3 -1
- package/dist/react/hooks/useUniformUpdaters.js +1 -1
- package/dist/react/hooks/useUniformUpdaters.mjs +11 -23
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var b=Object.defineProperty;var A=(d,e,o)=>e in d?b(d,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):d[e]=o;var w=(d,e,o)=>A(d,typeof e!="symbol"?e+"":e,o);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const E=require("./FBOManager.js");class v{constructor(e,o){w(this,"gl");w(this,"fboManager");w(this,"resources",new Map);w(this,"compileCache",new Map);w(this,"uniformUpdateFns",new Map);w(this,"extensions",new Map);const t={alpha:!1,depth:!1,stencil:!1,antialias:!1,powerPreference:"low-power",preserveDrawingBuffer:!1},r=e.getContext("webgl",{...t,...o});if(!r)throw new Error("WebGL not supported");this.gl=r,this.fboManager=new E.FBOManager(r),this.getExtension("OES_texture_float"),this.getExtension("OES_texture_float_linear"),this.getExtension("OES_vertex_array_object"),this.getExtension("ANGLE_instanced_arrays")}getExtension(e){if(this.extensions.has(e))return this.extensions.get(e);const o=this.gl.getExtension(e);return this.extensions.set(e,o),o}createProgram(e,o){const{vertexShader:t,fragmentShader:r,uniforms:s,attributes:f}=o,n=this.gl,u=this.getOrCompileShader("vertex:"+t,n.VERTEX_SHADER,t),m=this.getOrCompileShader("fragment:"+r,n.FRAGMENT_SHADER,r),i=n.createProgram();if(!i)throw new Error("Failed to create WebGL program");if(n.attachShader(i,u),n.attachShader(i,m),n.linkProgram(i),!n.getProgramParameter(i,n.LINK_STATUS)){const a=n.getProgramInfoLog(i);throw n.deleteProgram(i),new Error(`Could not link shader program: ${a}`)}const l={};for(const a of s)l[a.name]=n.getUniformLocation(i,a.name);const h={};if(f)for(const a of f)h[a.name]=n.getAttribLocation(i,a.name);const c={program:i,uniforms:l,attributes:h,buffers:{}};return this.resources.set(e,c),this.uniformUpdateFns.set(e,new Map),c}getOrCompileShader(e,o,t){if(this.compileCache.has(e)){const s=this.compileCache.get(e);if(s)return s}const r=this.compileShader(o,t);return this.compileCache.set(e,r),r}compileShader(e,o){const t=this.gl,r=t.createShader(e);if(!r)throw new Error("Failed to create shader");if(t.shaderSource(r,o),t.compileShader(r),!t.getShaderParameter(r,t.COMPILE_STATUS)){const s=t.getShaderInfoLog(r);throw t.deleteShader(r),new Error(`Shader compilation failed: ${s}`)}return r}createBuffer(e,o,t){const r=this.gl,s=this.resources.get(e);if(!s)throw new Error(`Program with id ${e} not found`);const f=r.createBuffer();if(!f)throw new Error("Failed to create buffer");return r.bindBuffer(r.ARRAY_BUFFER,f),r.bufferData(r.ARRAY_BUFFER,t,r.STATIC_DRAW),s.buffers[o]={buffer:f,data:t},f}updateBuffer(e,o,t){const r=this.gl,s=this.resources.get(e);if(!s)throw new Error(`Program with id ${e} not found`);const f=s.buffers[o];if(!f)throw new Error(`Buffer for attribute ${o} not found`);r.bindBuffer(r.ARRAY_BUFFER,f.buffer),r.bufferData(r.ARRAY_BUFFER,t,r.STATIC_DRAW),f.data=t}registerUniformUpdater(e,o,t,r){const s=this.resources.get(e);if(!s)throw new Error(`Program with id ${e} not found`);const f=this.uniformUpdateFns.get(e);if(!f)throw new Error(`Program uniforms for id ${e} not found`);const n=s.uniforms[o];if(n===null)return;const u=this.gl;let m;switch(t){case"int":m=(i,l,h)=>{const c=r(i,l,h);return u.uniform1i(n,c),c};break;case"float":m=(i,l,h)=>{const c=r(i,l,h);return u.uniform1f(n,c),c};break;case"sampler2D":m=(i,l,h)=>{const c=r(i,l,h);return u.uniform1i(n,c),c};break;case"vec2":{const i=new Float32Array(2);m=(l,h,c)=>{const a=r(l,h,c);return Array.isArray(a)?(i[0]=a[0],i[1]=a[1],u.uniform2fv(n,i)):u.uniform2fv(n,a),i};break}case"vec3":{const i=new Float32Array(3);m=(l,h,c)=>{const a=r(l,h,c);return Array.isArray(a)?(i[0]=a[0],i[1]=a[1],i[2]=a[2],u.uniform3fv(n,i)):u.uniform3fv(n,a),i};break}case"vec4":{const i=new Float32Array(4);m=(l,h,c)=>{const a=r(l,h,c);return Array.isArray(a)?(i[0]=a[0],i[1]=a[1],i[2]=a[2],i[3]=a[3],u.uniform4fv(n,i)):u.uniform4fv(n,a),i};break}case"mat2":{const i=new Float32Array(4);m=(l,h,c)=>{const a=r(l,h,c);if(Array.isArray(a)){for(let g=0;g<4;g++)i[g]=a[g];u.uniformMatrix2fv(n,!1,i)}else u.uniformMatrix2fv(n,!1,a);return i};break}case"mat3":{const i=new Float32Array(9);m=(l,h,c)=>{const a=r(l,h,c);if(Array.isArray(a)){for(let g=0;g<9;g++)i[g]=a[g];u.uniformMatrix3fv(n,!1,i)}else u.uniformMatrix3fv(n,!1,a);return i};break}case"mat4":{const i=new Float32Array(16);m=(l,h,c)=>{const a=r(l,h,c);if(Array.isArray(a)){for(let g=0;g<16;g++)i[g]=a[g];u.uniformMatrix4fv(n,!1,i)}else u.uniformMatrix4fv(n,!1,a);return i};break}default:throw new Error(`Unsupported uniform type: ${t}`)}f.set(o,m)}updateUniforms(e,o){const t=this.uniformUpdateFns.get(e);if(!t)return;const r=this.gl.canvas,s=r.width,f=r.height;t.forEach(n=>{n(o,s,f)})}setSize(e,o,t,r){const s=this.gl.canvas,f=t??e,n=r??o;(s.width!==e||s.height!==o)&&(s.width=e,s.height=o,this.gl.viewport(0,0,e,o)),s.style.width=`${f}px`,s.style.height=`${n}px`}prepareRender(e,o={}){const{clear:t=!0,clearColor:r=[0,0,0,1]}=o,s=this.gl,f=this.resources.get(e);if(!f)throw new Error(`Program with id ${e} not found`);s.useProgram(f.program),t&&(s.clearColor(...r),s.clear(s.COLOR_BUFFER_BIT))}fastRender(e,o,t=!0){const r=this.gl,s=this.resources.get(e);if(!s)throw new Error(`Program with id ${e} not found`);r.useProgram(s.program),t&&r.clear(r.COLOR_BUFFER_BIT),this.updateUniforms(e,o)}setUniform(e,o,t,r){const s=this.gl,f=this.resources.get(e);if(!f)throw new Error(`Program with id ${e} not found`);const n=f.uniforms[o];if(n!==null)switch(s.useProgram(f.program),r){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: ${r}`)}}setAttributeOnce(e,o,t){const r=this.gl,s=this.resources.get(e);if(!s)throw new Error(`Program with id ${e} not found`);const f=s.attributes[o];if(f===-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(r.bindBuffer(r.ARRAY_BUFFER,n.buffer),r.enableVertexAttribArray(f),r.vertexAttribPointer(f,t.size,r[t.type],t.normalized,t.stride,t.offset),t.instanced){const u=this.getExtension("ANGLE_instanced_arrays");if(u!=null&&u.vertexAttribDivisorANGLE)u.vertexAttribDivisorANGLE(f,1);else if(r.vertexAttribDivisor)r.vertexAttribDivisor(f,1);else throw new Error("Instanced rendering not supported")}}drawArrays(e,o,t){this.gl.drawArrays(e,o,t)}drawElements(e,o,t,r){this.gl.drawElements(e,o,t,r)}destroy(e){const o=this.gl,t=this.resources.get(e);t&&(Object.values(t.buffers).forEach(({buffer:r})=>{o.deleteBuffer(r)}),o.deleteProgram(t.program),this.resources.delete(e),this.uniformUpdateFns.delete(e))}destroyAll(){for(const e of Array.from(this.resources.keys()))this.destroy(e);this.compileCache.clear(),this.fboManager.destroyAll()}get context(){return this.gl}get fbo(){return this.fboManager}}exports.WebGLManager=v;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
1
|
+
var b = Object.defineProperty;
|
|
2
|
+
var A = (d, e, o) => e in d ? b(d, e, { enumerable: !0, configurable: !0, writable: !0, value: o }) : d[e] = o;
|
|
3
|
+
var w = (d, e, o) => A(d, typeof e != "symbol" ? e + "" : e, o);
|
|
4
4
|
import { FBOManager as E } from "./FBOManager.mjs";
|
|
5
|
-
class
|
|
5
|
+
class x {
|
|
6
6
|
constructor(e, o) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
w(this, "gl");
|
|
8
|
+
w(this, "fboManager");
|
|
9
|
+
w(this, "resources", /* @__PURE__ */ new Map());
|
|
10
|
+
w(this, "compileCache", /* @__PURE__ */ new Map());
|
|
11
|
+
w(this, "uniformUpdateFns", /* @__PURE__ */ new Map());
|
|
12
|
+
w(this, "extensions", /* @__PURE__ */ new Map());
|
|
13
13
|
const t = {
|
|
14
14
|
alpha: !1,
|
|
15
15
|
depth: !1,
|
|
@@ -29,27 +29,27 @@ class A {
|
|
|
29
29
|
return this.extensions.set(e, o), o;
|
|
30
30
|
}
|
|
31
31
|
createProgram(e, o) {
|
|
32
|
-
const { vertexShader: t, fragmentShader: r, uniforms: s, attributes:
|
|
33
|
-
if (!
|
|
32
|
+
const { vertexShader: t, fragmentShader: r, uniforms: s, attributes: f } = o, n = this.gl, u = this.getOrCompileShader("vertex:" + t, n.VERTEX_SHADER, t), m = this.getOrCompileShader("fragment:" + r, n.FRAGMENT_SHADER, r), i = n.createProgram();
|
|
33
|
+
if (!i)
|
|
34
34
|
throw new Error("Failed to create WebGL program");
|
|
35
|
-
if (n.attachShader(
|
|
36
|
-
const
|
|
37
|
-
throw n.deleteProgram(
|
|
35
|
+
if (n.attachShader(i, u), n.attachShader(i, m), n.linkProgram(i), !n.getProgramParameter(i, n.LINK_STATUS)) {
|
|
36
|
+
const a = n.getProgramInfoLog(i);
|
|
37
|
+
throw n.deleteProgram(i), new Error(`Could not link shader program: ${a}`);
|
|
38
38
|
}
|
|
39
|
-
const
|
|
40
|
-
for (const
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
if (
|
|
44
|
-
for (const
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
program:
|
|
48
|
-
uniforms:
|
|
49
|
-
attributes:
|
|
39
|
+
const l = {};
|
|
40
|
+
for (const a of s)
|
|
41
|
+
l[a.name] = n.getUniformLocation(i, a.name);
|
|
42
|
+
const h = {};
|
|
43
|
+
if (f)
|
|
44
|
+
for (const a of f)
|
|
45
|
+
h[a.name] = n.getAttribLocation(i, a.name);
|
|
46
|
+
const c = {
|
|
47
|
+
program: i,
|
|
48
|
+
uniforms: l,
|
|
49
|
+
attributes: h,
|
|
50
50
|
buffers: {}
|
|
51
51
|
};
|
|
52
|
-
return this.resources.set(e,
|
|
52
|
+
return this.resources.set(e, c), this.uniformUpdateFns.set(e, /* @__PURE__ */ new Map()), c;
|
|
53
53
|
}
|
|
54
54
|
getOrCompileShader(e, o, t) {
|
|
55
55
|
if (this.compileCache.has(e)) {
|
|
@@ -74,110 +74,137 @@ class A {
|
|
|
74
74
|
const r = this.gl, s = this.resources.get(e);
|
|
75
75
|
if (!s)
|
|
76
76
|
throw new Error(`Program with id ${e} not found`);
|
|
77
|
-
const
|
|
78
|
-
if (!
|
|
77
|
+
const f = r.createBuffer();
|
|
78
|
+
if (!f)
|
|
79
79
|
throw new Error("Failed to create buffer");
|
|
80
|
-
return r.bindBuffer(r.ARRAY_BUFFER,
|
|
80
|
+
return r.bindBuffer(r.ARRAY_BUFFER, f), r.bufferData(r.ARRAY_BUFFER, t, r.STATIC_DRAW), s.buffers[o] = { buffer: f, data: t }, f;
|
|
81
81
|
}
|
|
82
82
|
updateBuffer(e, o, t) {
|
|
83
83
|
const r = this.gl, s = this.resources.get(e);
|
|
84
84
|
if (!s)
|
|
85
85
|
throw new Error(`Program with id ${e} not found`);
|
|
86
|
-
const
|
|
87
|
-
if (!
|
|
86
|
+
const f = s.buffers[o];
|
|
87
|
+
if (!f)
|
|
88
88
|
throw new Error(`Buffer for attribute ${o} not found`);
|
|
89
|
-
r.bindBuffer(r.ARRAY_BUFFER,
|
|
89
|
+
r.bindBuffer(r.ARRAY_BUFFER, f.buffer), r.bufferData(r.ARRAY_BUFFER, t, r.STATIC_DRAW), f.data = t;
|
|
90
90
|
}
|
|
91
91
|
registerUniformUpdater(e, o, t, r) {
|
|
92
92
|
const s = this.resources.get(e);
|
|
93
93
|
if (!s)
|
|
94
94
|
throw new Error(`Program with id ${e} not found`);
|
|
95
|
-
const
|
|
96
|
-
if (!
|
|
95
|
+
const f = this.uniformUpdateFns.get(e);
|
|
96
|
+
if (!f)
|
|
97
97
|
throw new Error(`Program uniforms for id ${e} not found`);
|
|
98
98
|
const n = s.uniforms[o];
|
|
99
99
|
if (n === null)
|
|
100
100
|
return;
|
|
101
|
-
const
|
|
102
|
-
let
|
|
101
|
+
const u = this.gl;
|
|
102
|
+
let m;
|
|
103
103
|
switch (t) {
|
|
104
104
|
case "int":
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
return
|
|
105
|
+
m = (i, l, h) => {
|
|
106
|
+
const c = r(i, l, h);
|
|
107
|
+
return u.uniform1i(n, c), c;
|
|
108
108
|
};
|
|
109
109
|
break;
|
|
110
110
|
case "float":
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
return
|
|
111
|
+
m = (i, l, h) => {
|
|
112
|
+
const c = r(i, l, h);
|
|
113
|
+
return u.uniform1f(n, c), c;
|
|
114
114
|
};
|
|
115
115
|
break;
|
|
116
116
|
case "sampler2D":
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
return
|
|
117
|
+
m = (i, l, h) => {
|
|
118
|
+
const c = r(i, l, h);
|
|
119
|
+
return u.uniform1i(n, c), c;
|
|
120
120
|
};
|
|
121
121
|
break;
|
|
122
|
-
case "vec2":
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
122
|
+
case "vec2": {
|
|
123
|
+
const i = new Float32Array(2);
|
|
124
|
+
m = (l, h, c) => {
|
|
125
|
+
const a = r(l, h, c);
|
|
126
|
+
return Array.isArray(a) ? (i[0] = a[0], i[1] = a[1], u.uniform2fv(n, i)) : u.uniform2fv(n, a), i;
|
|
126
127
|
};
|
|
127
128
|
break;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
}
|
|
130
|
+
case "vec3": {
|
|
131
|
+
const i = new Float32Array(3);
|
|
132
|
+
m = (l, h, c) => {
|
|
133
|
+
const a = r(l, h, c);
|
|
134
|
+
return Array.isArray(a) ? (i[0] = a[0], i[1] = a[1], i[2] = a[2], u.uniform3fv(n, i)) : u.uniform3fv(n, a), i;
|
|
132
135
|
};
|
|
133
136
|
break;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
137
|
+
}
|
|
138
|
+
case "vec4": {
|
|
139
|
+
const i = new Float32Array(4);
|
|
140
|
+
m = (l, h, c) => {
|
|
141
|
+
const a = r(l, h, c);
|
|
142
|
+
return Array.isArray(a) ? (i[0] = a[0], i[1] = a[1], i[2] = a[2], i[3] = a[3], u.uniform4fv(n, i)) : u.uniform4fv(n, a), i;
|
|
138
143
|
};
|
|
139
144
|
break;
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
145
|
+
}
|
|
146
|
+
case "mat2": {
|
|
147
|
+
const i = new Float32Array(4);
|
|
148
|
+
m = (l, h, c) => {
|
|
149
|
+
const a = r(l, h, c);
|
|
150
|
+
if (Array.isArray(a)) {
|
|
151
|
+
for (let g = 0; g < 4; g++) i[g] = a[g];
|
|
152
|
+
u.uniformMatrix2fv(n, !1, i);
|
|
153
|
+
} else
|
|
154
|
+
u.uniformMatrix2fv(n, !1, a);
|
|
155
|
+
return i;
|
|
144
156
|
};
|
|
145
157
|
break;
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
158
|
+
}
|
|
159
|
+
case "mat3": {
|
|
160
|
+
const i = new Float32Array(9);
|
|
161
|
+
m = (l, h, c) => {
|
|
162
|
+
const a = r(l, h, c);
|
|
163
|
+
if (Array.isArray(a)) {
|
|
164
|
+
for (let g = 0; g < 9; g++) i[g] = a[g];
|
|
165
|
+
u.uniformMatrix3fv(n, !1, i);
|
|
166
|
+
} else
|
|
167
|
+
u.uniformMatrix3fv(n, !1, a);
|
|
168
|
+
return i;
|
|
150
169
|
};
|
|
151
170
|
break;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
171
|
+
}
|
|
172
|
+
case "mat4": {
|
|
173
|
+
const i = new Float32Array(16);
|
|
174
|
+
m = (l, h, c) => {
|
|
175
|
+
const a = r(l, h, c);
|
|
176
|
+
if (Array.isArray(a)) {
|
|
177
|
+
for (let g = 0; g < 16; g++) i[g] = a[g];
|
|
178
|
+
u.uniformMatrix4fv(n, !1, i);
|
|
179
|
+
} else
|
|
180
|
+
u.uniformMatrix4fv(n, !1, a);
|
|
181
|
+
return i;
|
|
156
182
|
};
|
|
157
183
|
break;
|
|
184
|
+
}
|
|
158
185
|
default:
|
|
159
186
|
throw new Error(`Unsupported uniform type: ${t}`);
|
|
160
187
|
}
|
|
161
|
-
|
|
188
|
+
f.set(o, m);
|
|
162
189
|
}
|
|
163
190
|
updateUniforms(e, o) {
|
|
164
191
|
const t = this.uniformUpdateFns.get(e);
|
|
165
192
|
if (!t)
|
|
166
193
|
return;
|
|
167
|
-
const r = this.gl.canvas, s = r.width,
|
|
194
|
+
const r = this.gl.canvas, s = r.width, f = r.height;
|
|
168
195
|
t.forEach((n) => {
|
|
169
|
-
n(o, s,
|
|
196
|
+
n(o, s, f);
|
|
170
197
|
});
|
|
171
198
|
}
|
|
172
199
|
setSize(e, o, t, r) {
|
|
173
|
-
const s = this.gl.canvas,
|
|
174
|
-
(s.width !== e || s.height !== o) && (s.width = e, s.height = o, this.gl.viewport(0, 0, e, o)), s.style.width = `${
|
|
200
|
+
const s = this.gl.canvas, f = t ?? e, n = r ?? o;
|
|
201
|
+
(s.width !== e || s.height !== o) && (s.width = e, s.height = o, this.gl.viewport(0, 0, e, o)), s.style.width = `${f}px`, s.style.height = `${n}px`;
|
|
175
202
|
}
|
|
176
203
|
prepareRender(e, o = {}) {
|
|
177
|
-
const { clear: t = !0, clearColor: r = [0, 0, 0, 1] } = o, s = this.gl,
|
|
178
|
-
if (!
|
|
204
|
+
const { clear: t = !0, clearColor: r = [0, 0, 0, 1] } = o, s = this.gl, f = this.resources.get(e);
|
|
205
|
+
if (!f)
|
|
179
206
|
throw new Error(`Program with id ${e} not found`);
|
|
180
|
-
s.useProgram(
|
|
207
|
+
s.useProgram(f.program), t && (s.clearColor(...r), s.clear(s.COLOR_BUFFER_BIT));
|
|
181
208
|
}
|
|
182
209
|
fastRender(e, o, t = !0) {
|
|
183
210
|
const r = this.gl, s = this.resources.get(e);
|
|
@@ -186,12 +213,12 @@ class A {
|
|
|
186
213
|
r.useProgram(s.program), t && r.clear(r.COLOR_BUFFER_BIT), this.updateUniforms(e, o);
|
|
187
214
|
}
|
|
188
215
|
setUniform(e, o, t, r) {
|
|
189
|
-
const s = this.gl,
|
|
190
|
-
if (!
|
|
216
|
+
const s = this.gl, f = this.resources.get(e);
|
|
217
|
+
if (!f)
|
|
191
218
|
throw new Error(`Program with id ${e} not found`);
|
|
192
|
-
const n =
|
|
219
|
+
const n = f.uniforms[o];
|
|
193
220
|
if (n !== null)
|
|
194
|
-
switch (s.useProgram(
|
|
221
|
+
switch (s.useProgram(f.program), r) {
|
|
195
222
|
case "float":
|
|
196
223
|
s.uniform1f(n, t);
|
|
197
224
|
break;
|
|
@@ -227,27 +254,27 @@ class A {
|
|
|
227
254
|
const r = this.gl, s = this.resources.get(e);
|
|
228
255
|
if (!s)
|
|
229
256
|
throw new Error(`Program with id ${e} not found`);
|
|
230
|
-
const
|
|
231
|
-
if (
|
|
257
|
+
const f = s.attributes[o];
|
|
258
|
+
if (f === -1) {
|
|
232
259
|
console.warn(`Attribute ${o} not found or is unused`);
|
|
233
260
|
return;
|
|
234
261
|
}
|
|
235
262
|
const n = s.buffers[o];
|
|
236
263
|
if (!n)
|
|
237
264
|
throw new Error(`Buffer for attribute ${o} not found`);
|
|
238
|
-
if (r.bindBuffer(r.ARRAY_BUFFER, n.buffer), r.enableVertexAttribArray(
|
|
239
|
-
|
|
265
|
+
if (r.bindBuffer(r.ARRAY_BUFFER, n.buffer), r.enableVertexAttribArray(f), r.vertexAttribPointer(
|
|
266
|
+
f,
|
|
240
267
|
t.size,
|
|
241
268
|
r[t.type],
|
|
242
269
|
t.normalized,
|
|
243
270
|
t.stride,
|
|
244
271
|
t.offset
|
|
245
272
|
), t.instanced) {
|
|
246
|
-
const
|
|
247
|
-
if (
|
|
248
|
-
|
|
273
|
+
const u = this.getExtension("ANGLE_instanced_arrays");
|
|
274
|
+
if (u != null && u.vertexAttribDivisorANGLE)
|
|
275
|
+
u.vertexAttribDivisorANGLE(f, 1);
|
|
249
276
|
else if (r.vertexAttribDivisor)
|
|
250
|
-
r.vertexAttribDivisor(
|
|
277
|
+
r.vertexAttribDivisor(f, 1);
|
|
251
278
|
else
|
|
252
279
|
throw new Error("Instanced rendering not supported");
|
|
253
280
|
}
|
|
@@ -277,5 +304,5 @@ class A {
|
|
|
277
304
|
}
|
|
278
305
|
}
|
|
279
306
|
export {
|
|
280
|
-
|
|
307
|
+
x as WebGLManager
|
|
281
308
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("react/jsx-runtime"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("react/jsx-runtime"),s=require("react"),h=require("../../../core/managers/WebGLManager.js"),I={},v="",M={},U={},q=({programConfigs:A,renderCallback:T,renderOptions:o=I,className:_=v,style:y=M,width:w,height:L,uniformUpdaters:f=U,useFastPath:g=!1,useDevicePixelRatio:F=!0,pixelRatio:S})=>{const l=s.useRef(null),a=s.useRef(null),d=s.useRef(null),i=s.useRef(null),E=s.useRef(0),m=s.useRef(n=>{const e=a.current,r=d.current;if(!e||!r)return;const t=n-E.current,c=e.context;if(g)e.fastRender(r,t,o.clear),c.drawArrays(c.TRIANGLE_STRIP,0,4);else{const u=e.resources.get(r);if(!u)return;e.prepareRender(r,o),p(t,u,c)}i.current=requestAnimationFrame(m.current)}),p=s.useCallback((n,e,r)=>{T(n,e,r)},[T]);s.useEffect(()=>{m.current=n=>{const e=a.current,r=d.current;if(!e||!r)return;const t=n-E.current,c=e.context;if(g)e.fastRender(r,t,o.clear),c.drawArrays(c.TRIANGLE_STRIP,0,4);else{const u=e.resources.get(r);if(!u)return;e.prepareRender(r,o),p(t,u,c)}i.current=requestAnimationFrame(m.current)}},[o,g,p]);const R=s.useCallback(()=>{if(!l.current||!a.current)return;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);a.current.setSize(t,c,n,e)},[F,S,w,L]);return s.useEffect(()=>{if(!l.current)return;const n=new h.WebGLManager(l.current);a.current=n,R();const[[e,r]]=Object.entries(A);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}),d.current=e;const t=f[e];return t&&t.forEach(c=>{n.registerUniformUpdater(e,c.name,c.type,c.updateFn)}),E.current=performance.now(),i.current=requestAnimationFrame(m.current),window.addEventListener("resize",R),()=>{window.removeEventListener("resize",R),i.current&&cancelAnimationFrame(i.current),n.destroyAll()}},[A,R,f]),s.useEffect(()=>{const n=a.current,e=d.current;if(!n||!e)return;const r=f[e];r&&r.forEach(t=>{n.registerUniformUpdater(e,t.name,t.type,t.updateFn)})},[f]),b.jsx("canvas",{ref:l,className:_,style:y})};exports.ShaderEngine=q;
|
|
@@ -1,82 +1,86 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useRef as s, useCallback as
|
|
1
|
+
import { jsx as U } from "react/jsx-runtime";
|
|
2
|
+
import { useRef as s, useCallback as S, useEffect as E } from "react";
|
|
3
3
|
import { WebGLManager as v } from "../../../core/managers/WebGLManager.mjs";
|
|
4
4
|
const z = {}, N = "", b = {}, D = {}, q = ({
|
|
5
|
-
programConfigs:
|
|
6
|
-
renderCallback:
|
|
5
|
+
programConfigs: T,
|
|
6
|
+
renderCallback: w,
|
|
7
7
|
renderOptions: a = z,
|
|
8
8
|
className: I = N,
|
|
9
|
-
style:
|
|
10
|
-
width:
|
|
11
|
-
height:
|
|
12
|
-
uniformUpdaters:
|
|
13
|
-
useFastPath:
|
|
14
|
-
useDevicePixelRatio:
|
|
15
|
-
pixelRatio:
|
|
9
|
+
style: h = b,
|
|
10
|
+
width: L,
|
|
11
|
+
height: F,
|
|
12
|
+
uniformUpdaters: f = D,
|
|
13
|
+
useFastPath: A = !1,
|
|
14
|
+
useDevicePixelRatio: _ = !0,
|
|
15
|
+
pixelRatio: y
|
|
16
16
|
}) => {
|
|
17
|
-
const
|
|
18
|
-
const e = o.current, r =
|
|
17
|
+
const m = s(null), o = s(null), d = s(null), i = s(null), R = s(0), l = s((n) => {
|
|
18
|
+
const e = o.current, r = d.current;
|
|
19
19
|
if (!e || !r) return;
|
|
20
|
-
const t = n -
|
|
21
|
-
if (
|
|
20
|
+
const t = n - R.current, c = e.context;
|
|
21
|
+
if (A)
|
|
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), g(t, u, c);
|
|
27
27
|
}
|
|
28
|
-
i.current = requestAnimationFrame(
|
|
29
|
-
}),
|
|
30
|
-
|
|
31
|
-
}, [
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const e = o.current, r =
|
|
28
|
+
i.current = requestAnimationFrame(l.current);
|
|
29
|
+
}), g = S((n, e, r) => {
|
|
30
|
+
w(n, e, r);
|
|
31
|
+
}, [w]);
|
|
32
|
+
E(() => {
|
|
33
|
+
l.current = (n) => {
|
|
34
|
+
const e = o.current, r = d.current;
|
|
35
35
|
if (!e || !r) return;
|
|
36
|
-
const t = n -
|
|
37
|
-
if (
|
|
36
|
+
const t = n - R.current, c = e.context;
|
|
37
|
+
if (A)
|
|
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), g(t, u, c);
|
|
43
43
|
}
|
|
44
|
-
i.current = requestAnimationFrame(
|
|
44
|
+
i.current = requestAnimationFrame(l.current);
|
|
45
45
|
};
|
|
46
|
-
}, [a,
|
|
47
|
-
const
|
|
48
|
-
if (!
|
|
49
|
-
const n =
|
|
46
|
+
}, [a, A, g]);
|
|
47
|
+
const p = S(() => {
|
|
48
|
+
if (!m.current || !o.current) return;
|
|
49
|
+
const n = L ?? window.innerWidth, e = F ?? window.innerHeight, r = y ?? (_ ? window.devicePixelRatio : 1), t = Math.floor(n * r), c = Math.floor(e * r);
|
|
50
50
|
o.current.setSize(t, c, n, e);
|
|
51
|
-
}, [
|
|
52
|
-
return
|
|
53
|
-
if (!
|
|
54
|
-
const n = new v(
|
|
55
|
-
o.current = n,
|
|
56
|
-
const [[e, r]] = Object.entries(
|
|
57
|
-
|
|
51
|
+
}, [_, y, L, F]);
|
|
52
|
+
return E(() => {
|
|
53
|
+
if (!m.current) return;
|
|
54
|
+
const n = new v(m.current);
|
|
55
|
+
o.current = n, p();
|
|
56
|
+
const [[e, r]] = Object.entries(T);
|
|
57
|
+
n.createProgram(e, r), n.createBuffer(e, "a_position", new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1])), n.setAttributeOnce(e, "a_position", {
|
|
58
58
|
name: "a_position",
|
|
59
59
|
size: 2,
|
|
60
60
|
type: "FLOAT",
|
|
61
61
|
normalized: !1,
|
|
62
62
|
stride: 0,
|
|
63
63
|
offset: 0
|
|
64
|
-
}),
|
|
65
|
-
|
|
64
|
+
}), d.current = e;
|
|
65
|
+
const t = f[e];
|
|
66
|
+
return t && t.forEach((c) => {
|
|
67
|
+
n.registerUniformUpdater(e, c.name, c.type, c.updateFn);
|
|
68
|
+
}), R.current = performance.now(), i.current = requestAnimationFrame(l.current), window.addEventListener("resize", p), () => {
|
|
69
|
+
window.removeEventListener("resize", p), i.current && cancelAnimationFrame(i.current), n.destroyAll();
|
|
66
70
|
};
|
|
67
|
-
}, [
|
|
68
|
-
const n = o.current, e =
|
|
71
|
+
}, [T, p, f]), E(() => {
|
|
72
|
+
const n = o.current, e = d.current;
|
|
69
73
|
if (!n || !e) return;
|
|
70
|
-
const r =
|
|
74
|
+
const r = f[e];
|
|
71
75
|
r && r.forEach((t) => {
|
|
72
76
|
n.registerUniformUpdater(e, t.name, t.type, t.updateFn);
|
|
73
77
|
});
|
|
74
|
-
}, [
|
|
78
|
+
}, [f]), /* @__PURE__ */ U(
|
|
75
79
|
"canvas",
|
|
76
80
|
{
|
|
77
|
-
ref:
|
|
81
|
+
ref: m,
|
|
78
82
|
className: I,
|
|
79
|
-
style:
|
|
83
|
+
style: h
|
|
80
84
|
}
|
|
81
85
|
);
|
|
82
86
|
};
|
|
@@ -1,4 +1,6 @@
|
|
|
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
|
-
}) =>
|
|
4
|
+
}) => {
|
|
5
|
+
[x: string]: UniformUpdaterDef<import('../../types').UniformType>[];
|
|
6
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react"),c=require("../lib/createUniformUpdater.js"),U=(u,t,e)=>n.useMemo(()=>{const a=(e==null?void 0:e.skipDefaultUniforms)??!1?[]:c.createCommonUpdaters().filter(r=>r.name==="u_time"&&!("u_time"in t)||r.name==="u_resolution"&&!("u_resolution"in t));return Object.entries(t).forEach(([r,s])=>{const i=r.startsWith("u_")?r:`u_${r}`;a.push(c.createUniformUpdater(i,s.type,s.value))}),{[u]:a}},[u,t,e==null?void 0:e.skipDefaultUniforms]);exports.useUniformUpdaters=U;
|
|
@@ -1,26 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createCommonUpdaters as
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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)
|
|
1
|
+
import { useMemo as f } from "react";
|
|
2
|
+
import { createCommonUpdaters as c, createUniformUpdater as i } from "../lib/createUniformUpdater.mjs";
|
|
3
|
+
const n = (u, t, e) => f(() => {
|
|
4
|
+
const a = (e == null ? void 0 : e.skipDefaultUniforms) ?? !1 ? [] : c().filter(
|
|
5
|
+
(r) => r.name === "u_time" && !("u_time" in t) || r.name === "u_resolution" && !("u_resolution" in t)
|
|
16
6
|
);
|
|
17
|
-
Object.entries(
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
return e.current = { key: s, result: u }, u;
|
|
23
|
-
};
|
|
7
|
+
return Object.entries(t).forEach(([r, m]) => {
|
|
8
|
+
const s = r.startsWith("u_") ? r : `u_${r}`;
|
|
9
|
+
a.push(i(s, m.type, m.value));
|
|
10
|
+
}), { [u]: a };
|
|
11
|
+
}, [u, t, e == null ? void 0 : e.skipDefaultUniforms]);
|
|
24
12
|
export {
|
|
25
|
-
|
|
13
|
+
n as useUniformUpdaters
|
|
26
14
|
};
|