micugl 0.0.9 → 0.0.12
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/index.d.ts +7 -0
- package/dist/core/lib/createShaderConfig.d.ts +16 -0
- package/dist/core/lib/vectorUtils.d.ts +8 -0
- package/dist/core/managers/FBOManager.d.ts +21 -0
- package/dist/core/managers/FBOManager.js +1 -0
- package/dist/core/managers/FBOManager.mjs +127 -0
- package/dist/core/managers/WebGLManager.d.ts +35 -0
- package/dist/core/managers/WebGLManager.js +1 -0
- package/dist/core/managers/WebGLManager.mjs +281 -0
- package/dist/core/systems/Passes.d.ts +11 -0
- package/dist/core/systems/Passes.js +1 -0
- package/dist/core/systems/Passes.mjs +84 -0
- package/dist/core/systems/Postprocessing.d.ts +31 -0
- package/dist/core/systems/Postprocessing.js +1 -0
- package/dist/core/systems/Postprocessing.mjs +135 -0
- package/dist/core.js +1 -0
- package/dist/core.mjs +20 -0
- package/dist/index.d.ts +3 -49
- package/dist/index.js +1 -1
- package/dist/index.mjs +14 -14
- package/dist/react/components/base/BasePingPongShaderComponent.d.ts +21 -0
- package/dist/react/components/base/BaseShaderComponent.d.ts +15 -0
- package/dist/react/components/engine/PingPongShaderEngine.d.ts +12 -0
- package/dist/react/components/engine/ShaderEngine.d.ts +18 -0
- package/dist/react/components/engine/ShaderEngine.js +1 -0
- package/dist/react/components/engine/ShaderEngine.mjs +82 -0
- package/dist/react/components/index.d.ts +0 -0
- package/dist/react/hooks/index.d.ts +3 -0
- package/dist/react/hooks/useDarkMode.d.ts +1 -0
- package/dist/react/hooks/usePingPongPasses.d.ts +21 -0
- package/dist/react/hooks/useUniformUpdaters.d.ts +4 -0
- package/dist/react/index.d.ts +8 -0
- package/dist/react/lib/createUniformUpdater.d.ts +8 -0
- package/dist/react.js +1 -0
- package/dist/react.mjs +20 -0
- package/dist/types.d.ts +164 -204
- package/package.json +7 -46
- package/dist/MarbleScene.d.ts +0 -27
- package/dist/RippleScene.d.ts +0 -20
- package/dist/examples/Marble/MarbleScene.d.ts +0 -20
- package/dist/examples/Marble/MarbleScene.js +0 -1
- package/dist/examples/Marble/MarbleScene.mjs +0 -71
- package/dist/examples/Marble/marbleShaders.d.ts +0 -2
- package/dist/examples/Marble/marbleShaders.js +0 -129
- package/dist/examples/Marble/marbleShaders.mjs +0 -139
- package/dist/examples/Ripple/RippleScene.d.ts +0 -13
- package/dist/examples/Ripple/RippleScene.js +0 -1
- package/dist/examples/Ripple/RippleScene.mjs +0 -109
- package/dist/examples/Ripple/rippleShaders.d.ts +0 -3
- package/dist/examples/Ripple/rippleShaders.js +0 -80
- package/dist/examples/Ripple/rippleShaders.mjs +0 -94
- package/dist/examples/index.d.ts +0 -4
- package/dist/examples/index.js +0 -1
- package/dist/examples/index.mjs +0 -13
- package/dist/marbleShaders.d.ts +0 -5
- package/dist/rippleShaders.d.ts +0 -7
- package/dist/src/core/managers/FBOManager.js +0 -1
- package/dist/src/core/managers/FBOManager.mjs +0 -120
- package/dist/src/core/managers/WebGLManager.js +0 -1
- package/dist/src/core/managers/WebGLManager.mjs +0 -273
- package/dist/src/core/systems/Passes.js +0 -1
- package/dist/src/core/systems/Passes.mjs +0 -78
- package/dist/src/core/systems/Postprocessing.js +0 -1
- package/dist/src/core/systems/Postprocessing.mjs +0 -128
- package/dist/src/react/components/engine/ShaderEngine.js +0 -1
- package/dist/src/react/components/engine/ShaderEngine.mjs +0 -106
- package/dist/types.js +0 -1
- package/dist/types.mjs +0 -1
- /package/dist/{src/core → core}/lib/createShaderConfig.js +0 -0
- /package/dist/{src/core → core}/lib/createShaderConfig.mjs +0 -0
- /package/dist/{src/core → core}/lib/vectorUtils.js +0 -0
- /package/dist/{src/core → core}/lib/vectorUtils.mjs +0 -0
- /package/dist/{src/react → react}/components/base/BasePingPongShaderComponent.js +0 -0
- /package/dist/{src/react → react}/components/base/BasePingPongShaderComponent.mjs +0 -0
- /package/dist/{src/react → react}/components/base/BaseShaderComponent.js +0 -0
- /package/dist/{src/react → react}/components/base/BaseShaderComponent.mjs +0 -0
- /package/dist/{src/react → react}/components/engine/PingPongShaderEngine.js +0 -0
- /package/dist/{src/react → react}/components/engine/PingPongShaderEngine.mjs +0 -0
- /package/dist/{src/react → react}/hooks/useDarkMode.js +0 -0
- /package/dist/{src/react → react}/hooks/useDarkMode.mjs +0 -0
- /package/dist/{src/react → react}/hooks/usePingPongPasses.js +0 -0
- /package/dist/{src/react → react}/hooks/usePingPongPasses.mjs +0 -0
- /package/dist/{src/react → react}/hooks/useUniformUpdaters.js +0 -0
- /package/dist/{src/react → react}/hooks/useUniformUpdaters.mjs +0 -0
- /package/dist/{src/react → react}/lib/createUniformUpdater.js +0 -0
- /package/dist/{src/react → react}/lib/createUniformUpdater.mjs +0 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { createTypedFloat32Array, mat2, mat3, mat4, vec2, vec3, vec4 } from './lib/vectorUtils';
|
|
2
|
+
export { createShaderConfig } from './lib/createShaderConfig';
|
|
3
|
+
export { FBOManager } from './managers/FBOManager';
|
|
4
|
+
export { WebGLManager } from './managers/WebGLManager';
|
|
5
|
+
export { Passes } from './systems/Passes';
|
|
6
|
+
export { Postprocessing } from './systems/Postprocessing';
|
|
7
|
+
export type { AttributeConfig, BufferData, FramebufferOptions, FramebufferResources, PingPongState, RenderOptions, RenderPass, ShaderAttributeLocations, ShaderProgramConfig, ShaderRenderCallback, ShaderResources, ShaderUniformLocations, TextureOptions, UniformConfig, UniformType, UniformUpdateFn, WebGLExtensionName, WebGLExtensionTypes } from '../types';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ShaderProgramConfig, UniformType } from '..';
|
|
2
|
+
export interface ShaderConfigOptions {
|
|
3
|
+
vertexShader: string;
|
|
4
|
+
fragmentShader: string;
|
|
5
|
+
uniformNames?: Record<string, UniformType>;
|
|
6
|
+
attributeConfigs?: {
|
|
7
|
+
name: string;
|
|
8
|
+
size: number;
|
|
9
|
+
type: 'FLOAT' | 'BYTE' | 'SHORT' | 'UNSIGNED_BYTE' | 'UNSIGNED_SHORT';
|
|
10
|
+
normalized?: boolean;
|
|
11
|
+
stride?: number;
|
|
12
|
+
offset?: number;
|
|
13
|
+
instanced?: boolean;
|
|
14
|
+
}[];
|
|
15
|
+
}
|
|
16
|
+
export declare const createShaderConfig: (options: ShaderConfigOptions) => ShaderProgramConfig;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Float32Array2, Float32Array3, Float32Array4, Float32Array9, Float32Array16, Mat2, Mat3, Mat4, TypedFloat32Array, Vec2, Vec3, Vec4 } from '../../types';
|
|
2
|
+
export declare function createTypedFloat32Array<N extends number>(length: N): TypedFloat32Array<N>;
|
|
3
|
+
export declare function vec2(values?: Vec2): Float32Array2;
|
|
4
|
+
export declare function vec3(values?: Vec3): Float32Array3;
|
|
5
|
+
export declare function vec4(values?: Vec4): Float32Array4;
|
|
6
|
+
export declare function mat2(values?: Mat2): Float32Array4;
|
|
7
|
+
export declare function mat3(values?: Mat3): Float32Array9;
|
|
8
|
+
export declare function mat4(values?: Mat4): Float32Array16;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FramebufferOptions, FramebufferResources, TextureOptions } from '..';
|
|
2
|
+
export declare class FBOManager {
|
|
3
|
+
private gl;
|
|
4
|
+
private resources;
|
|
5
|
+
private floatTexturesSupported;
|
|
6
|
+
private floatTextureExtension;
|
|
7
|
+
constructor(gl: WebGLRenderingContext);
|
|
8
|
+
createFramebuffer(id: string, options: FramebufferOptions): FramebufferResources;
|
|
9
|
+
createTexture(options: TextureOptions): WebGLTexture;
|
|
10
|
+
bindFramebuffer(id: string | null, textureIndex?: number): void;
|
|
11
|
+
bindTexture(id: string, textureUnit: number, textureIndex?: number): void;
|
|
12
|
+
swapTextures(id: string): void;
|
|
13
|
+
getPingPongIndices(id: string): {
|
|
14
|
+
read: number;
|
|
15
|
+
write: number;
|
|
16
|
+
};
|
|
17
|
+
resizeFramebuffer(id: string, width: number, height: number): void;
|
|
18
|
+
destroy(id: string): void;
|
|
19
|
+
destroyAll(): void;
|
|
20
|
+
isFloatTexturesSupported(): boolean;
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var h=Object.defineProperty;var F=(n,t,e)=>t in n?h(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var i=(n,t,e)=>F(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class d{constructor(t){i(this,"gl");i(this,"resources",new Map);i(this,"floatTexturesSupported");i(this,"floatTextureExtension",null);this.gl=t,this.floatTextureExtension=t.getExtension("OES_texture_float"),this.floatTexturesSupported=!!this.floatTextureExtension,t.getExtension("OES_texture_float_linear")}createFramebuffer(t,e){const r=this.gl,{width:s,height:u,textureCount:o=2,textureOptions:c={}}=e,a=[];for(let T=0;T<o;T++){const x=this.createTexture({width:s,height:u,...c});a.push(x)}const f=r.createFramebuffer();if(!f)throw new Error("Failed to create framebuffer");const E={framebuffer:f,textures:a,currentTextureIndex:0,width:s,height:u};return this.resources.set(t,E),E}createTexture(t){const e=this.gl,{width:r,height:s,internalFormat:u=e.RGBA,format:o=e.RGBA,type:c=this.floatTexturesSupported?e.FLOAT:e.UNSIGNED_BYTE,minFilter:a=e.NEAREST,magFilter:f=e.NEAREST,wrapS:E=e.CLAMP_TO_EDGE,wrapT:T=e.CLAMP_TO_EDGE,generateMipmap:x=!1}=t,l=e.createTexture();if(!l)throw new Error("Failed to create texture");return e.bindTexture(e.TEXTURE_2D,l),e.texImage2D(e.TEXTURE_2D,0,u,r,s,0,o,c,null),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,a),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,f),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,E),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,T),x&&e.generateMipmap(e.TEXTURE_2D),l}bindFramebuffer(t,e){const r=this.gl;if(t===null){r.bindFramebuffer(r.FRAMEBUFFER,null);return}const s=this.resources.get(t);if(!s)throw new Error(`Framebuffer with id ${t} not found`);const u=e??s.currentTextureIndex;r.bindFramebuffer(r.FRAMEBUFFER,s.framebuffer),r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,s.textures[u],0);const o=r.checkFramebufferStatus(r.FRAMEBUFFER);if(o!==r.FRAMEBUFFER_COMPLETE)throw new Error(`Framebuffer is not complete: ${o}`)}bindTexture(t,e,r){const s=this.gl,u=this.resources.get(t);if(!u)throw new Error(`Framebuffer with id ${t} not found`);const o=r??u.currentTextureIndex;s.activeTexture(s.TEXTURE0+e),s.bindTexture(s.TEXTURE_2D,u.textures[o])}swapTextures(t){const e=this.resources.get(t);if(!e)throw new Error(`Framebuffer with id ${t} not found`);e.currentTextureIndex=(e.currentTextureIndex+1)%e.textures.length}getPingPongIndices(t){const e=this.resources.get(t);if(!e)throw new Error(`Framebuffer with id ${t} not found`);const r=e.currentTextureIndex,s=(r+1)%e.textures.length;return{read:r,write:s}}resizeFramebuffer(t,e,r){const s=this.gl,u=this.resources.get(t);if(!u)throw new Error(`Framebuffer with id ${t} not found`);u.width===e&&u.height===r||(u.textures.forEach(o=>{s.bindTexture(s.TEXTURE_2D,o),s.texImage2D(s.TEXTURE_2D,0,s.RGBA,e,r,0,s.RGBA,this.floatTexturesSupported?s.FLOAT:s.UNSIGNED_BYTE,null)}),u.width=e,u.height=r)}destroy(t){const e=this.gl,r=this.resources.get(t);r&&(r.textures.forEach(s=>{e.deleteTexture(s)}),e.deleteFramebuffer(r.framebuffer),this.resources.delete(t))}destroyAll(){Array.from(this.resources.keys()).forEach(t=>{this.destroy(t)})}isFloatTexturesSupported(){return this.floatTexturesSupported}}exports.FBOManager=d;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
var l = Object.defineProperty;
|
|
2
|
+
var F = (n, r, e) => r in n ? l(n, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[r] = e;
|
|
3
|
+
var i = (n, r, e) => F(n, typeof r != "symbol" ? r + "" : r, e);
|
|
4
|
+
class d {
|
|
5
|
+
constructor(r) {
|
|
6
|
+
i(this, "gl");
|
|
7
|
+
i(this, "resources", /* @__PURE__ */ new Map());
|
|
8
|
+
i(this, "floatTexturesSupported");
|
|
9
|
+
i(this, "floatTextureExtension", null);
|
|
10
|
+
this.gl = r, this.floatTextureExtension = r.getExtension("OES_texture_float"), this.floatTexturesSupported = !!this.floatTextureExtension, r.getExtension("OES_texture_float_linear");
|
|
11
|
+
}
|
|
12
|
+
createFramebuffer(r, e) {
|
|
13
|
+
const t = this.gl, { width: s, height: u, textureCount: o = 2, textureOptions: c = {} } = e, f = [];
|
|
14
|
+
for (let T = 0; T < o; T++) {
|
|
15
|
+
const x = this.createTexture({
|
|
16
|
+
width: s,
|
|
17
|
+
height: u,
|
|
18
|
+
...c
|
|
19
|
+
});
|
|
20
|
+
f.push(x);
|
|
21
|
+
}
|
|
22
|
+
const E = t.createFramebuffer();
|
|
23
|
+
if (!E)
|
|
24
|
+
throw new Error("Failed to create framebuffer");
|
|
25
|
+
const a = {
|
|
26
|
+
framebuffer: E,
|
|
27
|
+
textures: f,
|
|
28
|
+
currentTextureIndex: 0,
|
|
29
|
+
width: s,
|
|
30
|
+
height: u
|
|
31
|
+
};
|
|
32
|
+
return this.resources.set(r, a), a;
|
|
33
|
+
}
|
|
34
|
+
createTexture(r) {
|
|
35
|
+
const e = this.gl, {
|
|
36
|
+
width: t,
|
|
37
|
+
height: s,
|
|
38
|
+
internalFormat: u = e.RGBA,
|
|
39
|
+
format: o = e.RGBA,
|
|
40
|
+
type: c = this.floatTexturesSupported ? e.FLOAT : e.UNSIGNED_BYTE,
|
|
41
|
+
minFilter: f = e.NEAREST,
|
|
42
|
+
magFilter: E = e.NEAREST,
|
|
43
|
+
wrapS: a = e.CLAMP_TO_EDGE,
|
|
44
|
+
wrapT: T = e.CLAMP_TO_EDGE,
|
|
45
|
+
generateMipmap: x = !1
|
|
46
|
+
} = r, h = e.createTexture();
|
|
47
|
+
if (!h)
|
|
48
|
+
throw new Error("Failed to create texture");
|
|
49
|
+
return e.bindTexture(e.TEXTURE_2D, h), e.texImage2D(e.TEXTURE_2D, 0, u, t, s, 0, o, c, null), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, f), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, E), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, a), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, T), x && e.generateMipmap(e.TEXTURE_2D), h;
|
|
50
|
+
}
|
|
51
|
+
bindFramebuffer(r, e) {
|
|
52
|
+
const t = this.gl;
|
|
53
|
+
if (r === null) {
|
|
54
|
+
t.bindFramebuffer(t.FRAMEBUFFER, null);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const s = this.resources.get(r);
|
|
58
|
+
if (!s)
|
|
59
|
+
throw new Error(`Framebuffer with id ${r} not found`);
|
|
60
|
+
const u = e ?? s.currentTextureIndex;
|
|
61
|
+
t.bindFramebuffer(t.FRAMEBUFFER, s.framebuffer), t.framebufferTexture2D(
|
|
62
|
+
t.FRAMEBUFFER,
|
|
63
|
+
t.COLOR_ATTACHMENT0,
|
|
64
|
+
t.TEXTURE_2D,
|
|
65
|
+
s.textures[u],
|
|
66
|
+
0
|
|
67
|
+
);
|
|
68
|
+
const o = t.checkFramebufferStatus(t.FRAMEBUFFER);
|
|
69
|
+
if (o !== t.FRAMEBUFFER_COMPLETE)
|
|
70
|
+
throw new Error(`Framebuffer is not complete: ${o}`);
|
|
71
|
+
}
|
|
72
|
+
bindTexture(r, e, t) {
|
|
73
|
+
const s = this.gl, u = this.resources.get(r);
|
|
74
|
+
if (!u)
|
|
75
|
+
throw new Error(`Framebuffer with id ${r} not found`);
|
|
76
|
+
const o = t ?? u.currentTextureIndex;
|
|
77
|
+
s.activeTexture(s.TEXTURE0 + e), s.bindTexture(s.TEXTURE_2D, u.textures[o]);
|
|
78
|
+
}
|
|
79
|
+
swapTextures(r) {
|
|
80
|
+
const e = this.resources.get(r);
|
|
81
|
+
if (!e)
|
|
82
|
+
throw new Error(`Framebuffer with id ${r} not found`);
|
|
83
|
+
e.currentTextureIndex = (e.currentTextureIndex + 1) % e.textures.length;
|
|
84
|
+
}
|
|
85
|
+
getPingPongIndices(r) {
|
|
86
|
+
const e = this.resources.get(r);
|
|
87
|
+
if (!e)
|
|
88
|
+
throw new Error(`Framebuffer with id ${r} not found`);
|
|
89
|
+
const t = e.currentTextureIndex, s = (t + 1) % e.textures.length;
|
|
90
|
+
return { read: t, write: s };
|
|
91
|
+
}
|
|
92
|
+
resizeFramebuffer(r, e, t) {
|
|
93
|
+
const s = this.gl, u = this.resources.get(r);
|
|
94
|
+
if (!u)
|
|
95
|
+
throw new Error(`Framebuffer with id ${r} not found`);
|
|
96
|
+
u.width === e && u.height === t || (u.textures.forEach((o) => {
|
|
97
|
+
s.bindTexture(s.TEXTURE_2D, o), s.texImage2D(
|
|
98
|
+
s.TEXTURE_2D,
|
|
99
|
+
0,
|
|
100
|
+
s.RGBA,
|
|
101
|
+
e,
|
|
102
|
+
t,
|
|
103
|
+
0,
|
|
104
|
+
s.RGBA,
|
|
105
|
+
this.floatTexturesSupported ? s.FLOAT : s.UNSIGNED_BYTE,
|
|
106
|
+
null
|
|
107
|
+
);
|
|
108
|
+
}), u.width = e, u.height = t);
|
|
109
|
+
}
|
|
110
|
+
destroy(r) {
|
|
111
|
+
const e = this.gl, t = this.resources.get(r);
|
|
112
|
+
t && (t.textures.forEach((s) => {
|
|
113
|
+
e.deleteTexture(s);
|
|
114
|
+
}), e.deleteFramebuffer(t.framebuffer), this.resources.delete(r));
|
|
115
|
+
}
|
|
116
|
+
destroyAll() {
|
|
117
|
+
Array.from(this.resources.keys()).forEach((r) => {
|
|
118
|
+
this.destroy(r);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
isFloatTexturesSupported() {
|
|
122
|
+
return this.floatTexturesSupported;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export {
|
|
126
|
+
d as FBOManager
|
|
127
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AttributeConfig, RenderOptions, ShaderProgramConfig, ShaderResources, UniformType, UniformUpdateFn, WebGLExtensionName, WebGLExtensionTypes, FBOManager } from '..';
|
|
2
|
+
import { UniformTypeMap } from '../../types';
|
|
3
|
+
declare global {
|
|
4
|
+
interface WebGLRenderingContext {
|
|
5
|
+
vertexAttribDivisor: ((index: number, divisor: number) => void) | undefined;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export declare class WebGLManager {
|
|
9
|
+
private gl;
|
|
10
|
+
private fboManager;
|
|
11
|
+
resources: Map<string, ShaderResources>;
|
|
12
|
+
private compileCache;
|
|
13
|
+
private uniformUpdateFns;
|
|
14
|
+
private extensions;
|
|
15
|
+
constructor(canvas: HTMLCanvasElement, options?: WebGLContextAttributes);
|
|
16
|
+
getExtension<K extends WebGLExtensionName>(name: K): WebGLExtensionTypes[K] | null;
|
|
17
|
+
createProgram(id: string, config: ShaderProgramConfig): ShaderResources;
|
|
18
|
+
private getOrCompileShader;
|
|
19
|
+
private compileShader;
|
|
20
|
+
createBuffer(programId: string, attributeName: string, data: Float32Array | Uint8Array | Uint16Array): WebGLBuffer;
|
|
21
|
+
updateBuffer(programId: string, attributeName: string, data: Float32Array | Uint8Array | Uint16Array): void;
|
|
22
|
+
registerUniformUpdater<T extends UniformType>(programId: string, uniformName: string, type: T, updateFn: UniformUpdateFn<T>): void;
|
|
23
|
+
updateUniforms(programId: string, time: number): void;
|
|
24
|
+
setSize(width: number, height: number, useDevicePixelRatio?: boolean): void;
|
|
25
|
+
prepareRender(programId: string, options?: RenderOptions): void;
|
|
26
|
+
fastRender(programId: string, time: number, clear?: boolean): void;
|
|
27
|
+
setUniform<T extends UniformType>(programId: string, uniformName: string, value: UniformTypeMap[T], type: T): void;
|
|
28
|
+
setAttributeOnce(programId: string, attributeName: string, config: AttributeConfig): void;
|
|
29
|
+
drawArrays(mode: number, first: number, count: number): void;
|
|
30
|
+
drawElements(mode: number, count: number, type: number, offset: number): void;
|
|
31
|
+
destroy(programId: string): void;
|
|
32
|
+
destroyAll(): void;
|
|
33
|
+
get context(): WebGLRenderingContext;
|
|
34
|
+
get fbo(): FBOManager;
|
|
35
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var w=Object.defineProperty;var b=(m,r,o)=>r in m?w(m,r,{enumerable:!0,configurable:!0,writable:!0,value:o}):m[r]=o;var d=(m,r,o)=>b(m,typeof r!="symbol"?r+"":r,o);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const E=require("./FBOManager.js");class v{constructor(r,o){d(this,"gl");d(this,"fboManager");d(this,"resources",new Map);d(this,"compileCache",new Map);d(this,"uniformUpdateFns",new Map);d(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=!0){const e=this.gl.canvas,s=t&&window.devicePixelRatio||1,i=Math.floor(r*s),n=Math.floor(o*s);(e.width!==i||e.height!==n)&&(e.width=i,e.height=n,e.style.width=`${r}px`,e.style.height=`${o}px`,this.gl.viewport(0,0,i,n))}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=v;
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
var w = Object.defineProperty;
|
|
2
|
+
var b = (m, e, o) => e in m ? w(m, e, { enumerable: !0, configurable: !0, writable: !0, value: o }) : m[e] = o;
|
|
3
|
+
var d = (m, e, o) => b(m, typeof e != "symbol" ? e + "" : e, o);
|
|
4
|
+
import { FBOManager as E } from "./FBOManager.mjs";
|
|
5
|
+
class A {
|
|
6
|
+
constructor(e, o) {
|
|
7
|
+
d(this, "gl");
|
|
8
|
+
d(this, "fboManager");
|
|
9
|
+
d(this, "resources", /* @__PURE__ */ new Map());
|
|
10
|
+
d(this, "compileCache", /* @__PURE__ */ new Map());
|
|
11
|
+
d(this, "uniformUpdateFns", /* @__PURE__ */ new Map());
|
|
12
|
+
d(this, "extensions", /* @__PURE__ */ new Map());
|
|
13
|
+
const t = {
|
|
14
|
+
alpha: !1,
|
|
15
|
+
depth: !1,
|
|
16
|
+
stencil: !1,
|
|
17
|
+
antialias: !1,
|
|
18
|
+
powerPreference: "low-power",
|
|
19
|
+
preserveDrawingBuffer: !1
|
|
20
|
+
}, r = e.getContext("webgl", { ...t, ...o });
|
|
21
|
+
if (!r)
|
|
22
|
+
throw new Error("WebGL not supported");
|
|
23
|
+
this.gl = r, this.fboManager = new E(r), this.getExtension("OES_texture_float"), this.getExtension("OES_texture_float_linear"), this.getExtension("OES_vertex_array_object"), this.getExtension("ANGLE_instanced_arrays");
|
|
24
|
+
}
|
|
25
|
+
getExtension(e) {
|
|
26
|
+
if (this.extensions.has(e))
|
|
27
|
+
return this.extensions.get(e);
|
|
28
|
+
const o = this.gl.getExtension(e);
|
|
29
|
+
return this.extensions.set(e, o), o;
|
|
30
|
+
}
|
|
31
|
+
createProgram(e, o) {
|
|
32
|
+
const { vertexShader: t, fragmentShader: r, uniforms: s, attributes: i } = o, n = this.gl, h = this.getOrCompileShader("vertex:" + t, n.VERTEX_SHADER, t), l = this.getOrCompileShader("fragment:" + r, n.FRAGMENT_SHADER, r), f = n.createProgram();
|
|
33
|
+
if (!f)
|
|
34
|
+
throw new Error("Failed to create WebGL program");
|
|
35
|
+
if (n.attachShader(f, h), n.attachShader(f, l), n.linkProgram(f), !n.getProgramParameter(f, n.LINK_STATUS)) {
|
|
36
|
+
const g = n.getProgramInfoLog(f);
|
|
37
|
+
throw n.deleteProgram(f), new Error(`Could not link shader program: ${g}`);
|
|
38
|
+
}
|
|
39
|
+
const c = {};
|
|
40
|
+
for (const g of s)
|
|
41
|
+
c[g.name] = n.getUniformLocation(f, g.name);
|
|
42
|
+
const u = {};
|
|
43
|
+
if (i)
|
|
44
|
+
for (const g of i)
|
|
45
|
+
u[g.name] = n.getAttribLocation(f, g.name);
|
|
46
|
+
const a = {
|
|
47
|
+
program: f,
|
|
48
|
+
uniforms: c,
|
|
49
|
+
attributes: u,
|
|
50
|
+
buffers: {}
|
|
51
|
+
};
|
|
52
|
+
return this.resources.set(e, a), this.uniformUpdateFns.set(e, /* @__PURE__ */ new Map()), a;
|
|
53
|
+
}
|
|
54
|
+
getOrCompileShader(e, o, t) {
|
|
55
|
+
if (this.compileCache.has(e)) {
|
|
56
|
+
const s = this.compileCache.get(e);
|
|
57
|
+
if (s)
|
|
58
|
+
return s;
|
|
59
|
+
}
|
|
60
|
+
const r = this.compileShader(o, t);
|
|
61
|
+
return this.compileCache.set(e, r), r;
|
|
62
|
+
}
|
|
63
|
+
compileShader(e, o) {
|
|
64
|
+
const t = this.gl, r = t.createShader(e);
|
|
65
|
+
if (!r)
|
|
66
|
+
throw new Error("Failed to create shader");
|
|
67
|
+
if (t.shaderSource(r, o), t.compileShader(r), !t.getShaderParameter(r, t.COMPILE_STATUS)) {
|
|
68
|
+
const s = t.getShaderInfoLog(r);
|
|
69
|
+
throw t.deleteShader(r), new Error(`Shader compilation failed: ${s}`);
|
|
70
|
+
}
|
|
71
|
+
return r;
|
|
72
|
+
}
|
|
73
|
+
createBuffer(e, o, t) {
|
|
74
|
+
const r = this.gl, s = this.resources.get(e);
|
|
75
|
+
if (!s)
|
|
76
|
+
throw new Error(`Program with id ${e} not found`);
|
|
77
|
+
const i = r.createBuffer();
|
|
78
|
+
if (!i)
|
|
79
|
+
throw new Error("Failed to create buffer");
|
|
80
|
+
return r.bindBuffer(r.ARRAY_BUFFER, i), r.bufferData(r.ARRAY_BUFFER, t, r.STATIC_DRAW), s.buffers[o] = { buffer: i, data: t }, i;
|
|
81
|
+
}
|
|
82
|
+
updateBuffer(e, o, t) {
|
|
83
|
+
const r = this.gl, s = this.resources.get(e);
|
|
84
|
+
if (!s)
|
|
85
|
+
throw new Error(`Program with id ${e} not found`);
|
|
86
|
+
const i = s.buffers[o];
|
|
87
|
+
if (!i)
|
|
88
|
+
throw new Error(`Buffer for attribute ${o} not found`);
|
|
89
|
+
r.bindBuffer(r.ARRAY_BUFFER, i.buffer), r.bufferData(r.ARRAY_BUFFER, t, r.STATIC_DRAW), i.data = t;
|
|
90
|
+
}
|
|
91
|
+
registerUniformUpdater(e, o, t, r) {
|
|
92
|
+
const s = this.resources.get(e);
|
|
93
|
+
if (!s)
|
|
94
|
+
throw new Error(`Program with id ${e} not found`);
|
|
95
|
+
const i = this.uniformUpdateFns.get(e);
|
|
96
|
+
if (!i)
|
|
97
|
+
throw new Error(`Program uniforms for id ${e} not found`);
|
|
98
|
+
const n = s.uniforms[o];
|
|
99
|
+
if (n === null)
|
|
100
|
+
return;
|
|
101
|
+
const h = this.gl;
|
|
102
|
+
let l;
|
|
103
|
+
switch (t) {
|
|
104
|
+
case "int":
|
|
105
|
+
l = (f, c, u) => {
|
|
106
|
+
const a = r(f, c, u);
|
|
107
|
+
return h.uniform1i(n, a), a;
|
|
108
|
+
};
|
|
109
|
+
break;
|
|
110
|
+
case "float":
|
|
111
|
+
l = (f, c, u) => {
|
|
112
|
+
const a = r(f, c, u);
|
|
113
|
+
return h.uniform1f(n, a), a;
|
|
114
|
+
};
|
|
115
|
+
break;
|
|
116
|
+
case "sampler2D":
|
|
117
|
+
l = (f, c, u) => {
|
|
118
|
+
const a = r(f, c, u);
|
|
119
|
+
return h.uniform1i(n, a), a;
|
|
120
|
+
};
|
|
121
|
+
break;
|
|
122
|
+
case "vec2":
|
|
123
|
+
l = (f, c, u) => {
|
|
124
|
+
const a = r(f, c, u);
|
|
125
|
+
return h.uniform2fv(n, a), a;
|
|
126
|
+
};
|
|
127
|
+
break;
|
|
128
|
+
case "vec3":
|
|
129
|
+
l = (f, c, u) => {
|
|
130
|
+
const a = r(f, c, u);
|
|
131
|
+
return h.uniform3fv(n, a), a;
|
|
132
|
+
};
|
|
133
|
+
break;
|
|
134
|
+
case "vec4":
|
|
135
|
+
l = (f, c, u) => {
|
|
136
|
+
const a = r(f, c, u);
|
|
137
|
+
return h.uniform4fv(n, a), a;
|
|
138
|
+
};
|
|
139
|
+
break;
|
|
140
|
+
case "mat2":
|
|
141
|
+
l = (f, c, u) => {
|
|
142
|
+
const a = r(f, c, u);
|
|
143
|
+
return h.uniformMatrix2fv(n, !1, a), a;
|
|
144
|
+
};
|
|
145
|
+
break;
|
|
146
|
+
case "mat3":
|
|
147
|
+
l = (f, c, u) => {
|
|
148
|
+
const a = r(f, c, u);
|
|
149
|
+
return h.uniformMatrix3fv(n, !1, a), a;
|
|
150
|
+
};
|
|
151
|
+
break;
|
|
152
|
+
case "mat4":
|
|
153
|
+
l = (f, c, u) => {
|
|
154
|
+
const a = r(f, c, u);
|
|
155
|
+
return h.uniformMatrix4fv(n, !1, a), a;
|
|
156
|
+
};
|
|
157
|
+
break;
|
|
158
|
+
default:
|
|
159
|
+
throw new Error(`Unsupported uniform type: ${t}`);
|
|
160
|
+
}
|
|
161
|
+
i.set(o, l);
|
|
162
|
+
}
|
|
163
|
+
updateUniforms(e, o) {
|
|
164
|
+
const t = this.uniformUpdateFns.get(e);
|
|
165
|
+
if (!t)
|
|
166
|
+
return;
|
|
167
|
+
const r = this.gl.canvas, s = r.width, i = r.height;
|
|
168
|
+
t.forEach((n) => {
|
|
169
|
+
n(o, s, i);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
setSize(e, o, t = !0) {
|
|
173
|
+
const r = this.gl.canvas, s = t && window.devicePixelRatio || 1, i = Math.floor(e * s), n = Math.floor(o * s);
|
|
174
|
+
(r.width !== i || r.height !== n) && (r.width = i, r.height = n, r.style.width = `${e}px`, r.style.height = `${o}px`, this.gl.viewport(0, 0, i, n));
|
|
175
|
+
}
|
|
176
|
+
prepareRender(e, o = {}) {
|
|
177
|
+
const { clear: t = !0, clearColor: r = [0, 0, 0, 1] } = o, s = this.gl, i = this.resources.get(e);
|
|
178
|
+
if (!i)
|
|
179
|
+
throw new Error(`Program with id ${e} not found`);
|
|
180
|
+
s.useProgram(i.program), t && (s.clearColor(...r), s.clear(s.COLOR_BUFFER_BIT));
|
|
181
|
+
}
|
|
182
|
+
fastRender(e, o, t = !0) {
|
|
183
|
+
const r = this.gl, s = this.resources.get(e);
|
|
184
|
+
if (!s)
|
|
185
|
+
throw new Error(`Program with id ${e} not found`);
|
|
186
|
+
r.useProgram(s.program), t && r.clear(r.COLOR_BUFFER_BIT), this.updateUniforms(e, o);
|
|
187
|
+
}
|
|
188
|
+
setUniform(e, o, t, r) {
|
|
189
|
+
const s = this.gl, i = this.resources.get(e);
|
|
190
|
+
if (!i)
|
|
191
|
+
throw new Error(`Program with id ${e} not found`);
|
|
192
|
+
const n = i.uniforms[o];
|
|
193
|
+
if (n !== null)
|
|
194
|
+
switch (s.useProgram(i.program), r) {
|
|
195
|
+
case "float":
|
|
196
|
+
s.uniform1f(n, t);
|
|
197
|
+
break;
|
|
198
|
+
case "vec2":
|
|
199
|
+
s.uniform2fv(n, t);
|
|
200
|
+
break;
|
|
201
|
+
case "vec3":
|
|
202
|
+
s.uniform3fv(n, t);
|
|
203
|
+
break;
|
|
204
|
+
case "vec4":
|
|
205
|
+
s.uniform4fv(n, t);
|
|
206
|
+
break;
|
|
207
|
+
case "int":
|
|
208
|
+
s.uniform1i(n, t);
|
|
209
|
+
break;
|
|
210
|
+
case "mat2":
|
|
211
|
+
s.uniformMatrix2fv(n, !1, t);
|
|
212
|
+
break;
|
|
213
|
+
case "mat3":
|
|
214
|
+
s.uniformMatrix3fv(n, !1, t);
|
|
215
|
+
break;
|
|
216
|
+
case "mat4":
|
|
217
|
+
s.uniformMatrix4fv(n, !1, t);
|
|
218
|
+
break;
|
|
219
|
+
case "sampler2D":
|
|
220
|
+
s.uniform1i(n, t);
|
|
221
|
+
break;
|
|
222
|
+
default:
|
|
223
|
+
throw new Error(`Unsupported uniform type: ${r}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
setAttributeOnce(e, o, t) {
|
|
227
|
+
const r = this.gl, s = this.resources.get(e);
|
|
228
|
+
if (!s)
|
|
229
|
+
throw new Error(`Program with id ${e} not found`);
|
|
230
|
+
const i = s.attributes[o];
|
|
231
|
+
if (i === -1) {
|
|
232
|
+
console.warn(`Attribute ${o} not found or is unused`);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const n = s.buffers[o];
|
|
236
|
+
if (!n)
|
|
237
|
+
throw new Error(`Buffer for attribute ${o} not found`);
|
|
238
|
+
if (r.bindBuffer(r.ARRAY_BUFFER, n.buffer), r.enableVertexAttribArray(i), r.vertexAttribPointer(
|
|
239
|
+
i,
|
|
240
|
+
t.size,
|
|
241
|
+
r[t.type],
|
|
242
|
+
t.normalized,
|
|
243
|
+
t.stride,
|
|
244
|
+
t.offset
|
|
245
|
+
), t.instanced) {
|
|
246
|
+
const h = this.getExtension("ANGLE_instanced_arrays");
|
|
247
|
+
if (h != null && h.vertexAttribDivisorANGLE)
|
|
248
|
+
h.vertexAttribDivisorANGLE(i, 1);
|
|
249
|
+
else if (r.vertexAttribDivisor)
|
|
250
|
+
r.vertexAttribDivisor(i, 1);
|
|
251
|
+
else
|
|
252
|
+
throw new Error("Instanced rendering not supported");
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
drawArrays(e, o, t) {
|
|
256
|
+
this.gl.drawArrays(e, o, t);
|
|
257
|
+
}
|
|
258
|
+
drawElements(e, o, t, r) {
|
|
259
|
+
this.gl.drawElements(e, o, t, r);
|
|
260
|
+
}
|
|
261
|
+
destroy(e) {
|
|
262
|
+
const o = this.gl, t = this.resources.get(e);
|
|
263
|
+
t && (Object.values(t.buffers).forEach(({ buffer: r }) => {
|
|
264
|
+
o.deleteBuffer(r);
|
|
265
|
+
}), o.deleteProgram(t.program), this.resources.delete(e), this.uniformUpdateFns.delete(e));
|
|
266
|
+
}
|
|
267
|
+
destroyAll() {
|
|
268
|
+
for (const e of Array.from(this.resources.keys()))
|
|
269
|
+
this.destroy(e);
|
|
270
|
+
this.compileCache.clear(), this.fboManager.destroyAll();
|
|
271
|
+
}
|
|
272
|
+
get context() {
|
|
273
|
+
return this.gl;
|
|
274
|
+
}
|
|
275
|
+
get fbo() {
|
|
276
|
+
return this.fboManager;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
export {
|
|
280
|
+
A as WebGLManager
|
|
281
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { RenderPass, WebGLManager } from '..';
|
|
2
|
+
export declare class Passes {
|
|
3
|
+
private webglManager;
|
|
4
|
+
private passes;
|
|
5
|
+
private pingPongIds;
|
|
6
|
+
constructor(webglManager: WebGLManager);
|
|
7
|
+
addPass(pass: RenderPass): void;
|
|
8
|
+
clearPasses(): void;
|
|
9
|
+
execute(time: number): void;
|
|
10
|
+
initializeResources(): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var d=Object.defineProperty;var p=(n,e,a)=>e in n?d(n,e,{enumerable:!0,configurable:!0,writable:!0,value:a}):n[e]=a;var o=(n,e,a)=>p(n,typeof e!="symbol"?e+"":e,a);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class u{constructor(e){o(this,"webglManager");o(this,"passes",[]);o(this,"pingPongIds",new Set);this.webglManager=e}addPass(e){this.passes.push(e),e.outputFramebuffer&&this.pingPongIds.add(e.outputFramebuffer),e.inputTextures.forEach(a=>{a.bindingType==="readwrite"&&this.pingPongIds.add(a.id)})}clearPasses(){this.passes=[],this.pingPongIds.clear()}execute(e){const a=this.webglManager.context,i=this.webglManager.fbo;for(const s of this.passes){if(s.outputFramebuffer)if(this.pingPongIds.has(s.outputFramebuffer)){const{write:r}=i.getPingPongIndices(s.outputFramebuffer);i.bindFramebuffer(s.outputFramebuffer,r)}else i.bindFramebuffer(s.outputFramebuffer);else i.bindFramebuffer(null);this.webglManager.prepareRender(s.programId,s.renderOptions),s.inputTextures.forEach(r=>{let t=r.bindingType==="read"?0:1;if(this.pingPongIds.has(r.id)){const{read:g,write:f}=i.getPingPongIndices(r.id);t=r.bindingType==="read"||r.bindingType==="readwrite"?g:f}i.bindTexture(r.id,r.textureUnit,t),this.webglManager.setUniform(s.programId,`u_${r.id}`,r.textureUnit,"sampler2D")}),this.webglManager.updateUniforms(s.programId,e),s.uniforms&&Object.entries(s.uniforms).forEach(([r,t])=>{const g=typeof t.value=="function"?t.value(e,a.canvas.width,a.canvas.height):t.value;this.webglManager.setUniform(s.programId,r,g,t.type)}),a.drawArrays(a.TRIANGLE_STRIP,0,4),s.outputFramebuffer&&this.pingPongIds.has(s.outputFramebuffer)&&i.swapTextures(s.outputFramebuffer),s.inputTextures.forEach(r=>{r.bindingType==="readwrite"&&this.pingPongIds.has(r.id)&&i.swapTextures(r.id)})}}initializeResources(){for(const e of this.passes){const a=this.webglManager.resources.get(e.programId);a&&!a.buffers.a_position&&(this.webglManager.createBuffer(e.programId,"a_position",new Float32Array([-1,-1,1,-1,-1,1,1,1])),this.webglManager.setAttributeOnce(e.programId,"a_position",{name:"a_position",size:2,type:"FLOAT",normalized:!1,stride:0,offset:0}))}}}exports.Passes=u;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
var d = Object.defineProperty;
|
|
2
|
+
var p = (t, e, a) => e in t ? d(t, e, { enumerable: !0, configurable: !0, writable: !0, value: a }) : t[e] = a;
|
|
3
|
+
var o = (t, e, a) => p(t, typeof e != "symbol" ? e + "" : e, a);
|
|
4
|
+
class b {
|
|
5
|
+
constructor(e) {
|
|
6
|
+
o(this, "webglManager");
|
|
7
|
+
o(this, "passes", []);
|
|
8
|
+
o(this, "pingPongIds", /* @__PURE__ */ new Set());
|
|
9
|
+
this.webglManager = e;
|
|
10
|
+
}
|
|
11
|
+
addPass(e) {
|
|
12
|
+
this.passes.push(e), e.outputFramebuffer && this.pingPongIds.add(e.outputFramebuffer), e.inputTextures.forEach((a) => {
|
|
13
|
+
a.bindingType === "readwrite" && this.pingPongIds.add(a.id);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
clearPasses() {
|
|
17
|
+
this.passes = [], this.pingPongIds.clear();
|
|
18
|
+
}
|
|
19
|
+
execute(e) {
|
|
20
|
+
const a = this.webglManager.context, i = this.webglManager.fbo;
|
|
21
|
+
for (const s of this.passes) {
|
|
22
|
+
if (s.outputFramebuffer)
|
|
23
|
+
if (this.pingPongIds.has(s.outputFramebuffer)) {
|
|
24
|
+
const { write: r } = i.getPingPongIndices(s.outputFramebuffer);
|
|
25
|
+
i.bindFramebuffer(s.outputFramebuffer, r);
|
|
26
|
+
} else
|
|
27
|
+
i.bindFramebuffer(s.outputFramebuffer);
|
|
28
|
+
else
|
|
29
|
+
i.bindFramebuffer(null);
|
|
30
|
+
this.webglManager.prepareRender(s.programId, s.renderOptions), s.inputTextures.forEach((r) => {
|
|
31
|
+
let n = r.bindingType === "read" ? 0 : 1;
|
|
32
|
+
if (this.pingPongIds.has(r.id)) {
|
|
33
|
+
const { read: g, write: f } = i.getPingPongIndices(r.id);
|
|
34
|
+
n = r.bindingType === "read" || r.bindingType === "readwrite" ? g : f;
|
|
35
|
+
}
|
|
36
|
+
i.bindTexture(r.id, r.textureUnit, n), this.webglManager.setUniform(
|
|
37
|
+
s.programId,
|
|
38
|
+
`u_${r.id}`,
|
|
39
|
+
r.textureUnit,
|
|
40
|
+
"sampler2D"
|
|
41
|
+
);
|
|
42
|
+
}), this.webglManager.updateUniforms(s.programId, e), s.uniforms && Object.entries(s.uniforms).forEach(([r, n]) => {
|
|
43
|
+
const g = typeof n.value == "function" ? n.value(e, a.canvas.width, a.canvas.height) : n.value;
|
|
44
|
+
this.webglManager.setUniform(
|
|
45
|
+
s.programId,
|
|
46
|
+
r,
|
|
47
|
+
g,
|
|
48
|
+
n.type
|
|
49
|
+
);
|
|
50
|
+
}), a.drawArrays(a.TRIANGLE_STRIP, 0, 4), s.outputFramebuffer && this.pingPongIds.has(s.outputFramebuffer) && i.swapTextures(s.outputFramebuffer), s.inputTextures.forEach((r) => {
|
|
51
|
+
r.bindingType === "readwrite" && this.pingPongIds.has(r.id) && i.swapTextures(r.id);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
initializeResources() {
|
|
56
|
+
for (const e of this.passes) {
|
|
57
|
+
const a = this.webglManager.resources.get(e.programId);
|
|
58
|
+
a && !a.buffers.a_position && (this.webglManager.createBuffer(
|
|
59
|
+
e.programId,
|
|
60
|
+
"a_position",
|
|
61
|
+
new Float32Array([
|
|
62
|
+
-1,
|
|
63
|
+
-1,
|
|
64
|
+
1,
|
|
65
|
+
-1,
|
|
66
|
+
-1,
|
|
67
|
+
1,
|
|
68
|
+
1,
|
|
69
|
+
1
|
|
70
|
+
])
|
|
71
|
+
), this.webglManager.setAttributeOnce(e.programId, "a_position", {
|
|
72
|
+
name: "a_position",
|
|
73
|
+
size: 2,
|
|
74
|
+
type: "FLOAT",
|
|
75
|
+
normalized: !1,
|
|
76
|
+
stride: 0,
|
|
77
|
+
offset: 0
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export {
|
|
83
|
+
b as Passes
|
|
84
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { WebGLManager } from '../managers/WebGLManager';
|
|
2
|
+
import { FramebufferOptions, RenderPass, ShaderProgramConfig, UniformParam } from '../../types';
|
|
3
|
+
export interface PostProcessEffect {
|
|
4
|
+
id: string;
|
|
5
|
+
programId: string;
|
|
6
|
+
shaderConfig: ShaderProgramConfig;
|
|
7
|
+
uniforms: Record<string, UniformParam>;
|
|
8
|
+
enabled: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface PostProcessChain {
|
|
11
|
+
id: string;
|
|
12
|
+
effects: PostProcessEffect[];
|
|
13
|
+
inputFramebufferId: string;
|
|
14
|
+
outputFramebufferId: string | null;
|
|
15
|
+
intermediateFramebufferIds: string[];
|
|
16
|
+
}
|
|
17
|
+
export declare class Postprocessing {
|
|
18
|
+
private webglManager;
|
|
19
|
+
private effects;
|
|
20
|
+
private chains;
|
|
21
|
+
private defaultFramebufferOptions;
|
|
22
|
+
constructor(webglManager: WebGLManager);
|
|
23
|
+
registerEffect(effect: PostProcessEffect): void;
|
|
24
|
+
removeEffect(effectId: string): void;
|
|
25
|
+
createChain(chainId: string, effectIds: string[], inputFramebufferId: string, outputFramebufferId?: string | null, framebufferOptions?: FramebufferOptions): void;
|
|
26
|
+
removeChain(chainId: string): void;
|
|
27
|
+
generatePasses(chainId: string, _time: number): RenderPass[];
|
|
28
|
+
process(chainId: string, time: number): void;
|
|
29
|
+
resizeFramebuffers(width: number, height: number): void;
|
|
30
|
+
destroyAll(): void;
|
|
31
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var m=Object.defineProperty;var w=(u,e,t)=>e in u?m(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var h=(u,e,t)=>w(u,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class F{constructor(e){h(this,"webglManager");h(this,"effects",new Map);h(this,"chains",new Map);h(this,"defaultFramebufferOptions",{width:0,height:0,textureCount:2,textureOptions:{minFilter:WebGLRenderingContext.LINEAR,magFilter:WebGLRenderingContext.LINEAR}});this.webglManager=e}registerEffect(e){this.effects.has(e.id)&&console.warn(`Effect with id ${e.id} already exists, overwriting`),this.webglManager.resources.has(e.programId)||this.webglManager.createProgram(e.programId,e.shaderConfig),this.effects.set(e.id,e)}removeEffect(e){if(!this.effects.has(e)){console.warn(`Effect with id ${e} not found`);return}this.effects.delete(e)}createChain(e,t,r,s=null,a=this.defaultFramebufferOptions){this.chains.has(e)&&console.warn(`Chain with id ${e} already exists, overwriting`);const i=[];for(const o of t){const f=this.effects.get(o);if(!f)throw new Error(`Effect with id ${o} not found`);i.push(f)}const n=[];for(let o=0;o<i.length-1;o++){const f=`${e}-intermediate-${o}`;this.webglManager.fbo.createFramebuffer(f,a),n.push(f)}this.chains.set(e,{id:e,effects:i,inputFramebufferId:r,outputFramebufferId:s,intermediateFramebufferIds:n})}removeChain(e){const t=this.chains.get(e);if(!t){console.warn(`Chain with id ${e} not found`);return}t.intermediateFramebufferIds.forEach(r=>{this.webglManager.fbo.destroy(r)}),this.chains.delete(e)}generatePasses(e,t){const r=this.chains.get(e);if(!r)throw new Error(`Chain with id ${e} not found`);const s=[],a=r.effects.filter(i=>i.enabled);return a.length===0?[{programId:"copy-shader",inputTextures:[{id:r.inputFramebufferId,textureUnit:0,bindingType:"read"}],outputFramebuffer:r.outputFramebufferId,renderOptions:{clear:!0}}]:(a.forEach((i,n)=>{const o=n===0,f=n===a.length-1,b=o?r.inputFramebufferId:r.intermediateFramebufferIds[n-1],l=f?r.outputFramebufferId:r.intermediateFramebufferIds[n],g={};Object.entries(i.uniforms).forEach(([c,d])=>{const p=c.startsWith("u_")?c:`u_${c}`;g[p]={type:d.type,value:d.value}}),s.push({programId:i.programId,inputTextures:[{id:b,textureUnit:0,bindingType:"read"}],outputFramebuffer:l,uniforms:g,renderOptions:{clear:!0}})}),s)}process(e,t){const r=this.generatePasses(e,t);for(const s of r)s.outputFramebuffer?this.webglManager.fbo.bindFramebuffer(s.outputFramebuffer):this.webglManager.fbo.bindFramebuffer(null),this.webglManager.prepareRender(s.programId,s.renderOptions),s.inputTextures.forEach(a=>{this.webglManager.fbo.bindTexture(a.id,a.textureUnit),this.webglManager.setUniform(s.programId,`u_texture${a.textureUnit}`,a.textureUnit,"sampler2D")}),s.uniforms&&Object.entries(s.uniforms).forEach(([a,i])=>{const n=typeof i.value=="function"?i.value(t,this.webglManager.context.canvas.width,this.webglManager.context.canvas.height):i.value;this.webglManager.setUniform(s.programId,a,n,i.type)}),this.webglManager.context.drawArrays(this.webglManager.context.TRIANGLE_STRIP,0,4)}resizeFramebuffers(e,t){for(const r of this.chains.values())for(const s of r.intermediateFramebufferIds)this.webglManager.fbo.resizeFramebuffer(s,e,t)}destroyAll(){for(const e of this.chains.values())for(const t of e.intermediateFramebufferIds)this.webglManager.fbo.destroy(t);this.chains.clear(),this.effects.clear()}}exports.Postprocessing=F;
|