bg2e-js 2.3.11 → 2.3.13
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/bg2e-js.js +356 -326
- package/dist/bg2e-js.js.map +1 -1
- package/package.json +56 -56
- package/src/app/AppController.ts +39 -39
- package/src/app/Bg2KeyboardEvent.ts +54 -54
- package/src/app/Bg2MouseEvent.ts +82 -82
- package/src/app/Bg2TouchEvent.ts +18 -18
- package/src/app/Canvas.ts +108 -108
- package/src/app/EventBase.ts +10 -10
- package/src/app/MainLoop.ts +273 -273
- package/src/app/index.ts +24 -24
- package/src/base/Color.ts +134 -134
- package/src/base/Environment.ts +183 -183
- package/src/base/Light.ts +192 -192
- package/src/base/Material.ts +620 -620
- package/src/base/PolyList.ts +365 -365
- package/src/base/Texture.ts +620 -620
- package/src/base/index.ts +81 -81
- package/src/db/Bg2LoaderPlugin.ts +143 -143
- package/src/db/DBPluginApi.ts +48 -48
- package/src/db/Loader.ts +116 -116
- package/src/db/LoaderPlugin.ts +34 -34
- package/src/db/MtlParser.ts +7 -7
- package/src/db/ObjLoaderPlugin.ts +54 -54
- package/src/db/ObjParser.ts +252 -252
- package/src/db/ObjWriterPlugin.ts +18 -18
- package/src/db/VitscnjLoaderPlugin.ts +112 -112
- package/src/db/Writer.ts +52 -52
- package/src/db/WriterPlugin.ts +22 -22
- package/src/db/index.ts +44 -44
- package/src/debug/DebugRenderer.ts +173 -173
- package/src/debug/WebGLTextureViewer.ts +75 -75
- package/src/debug/index.ts +6 -6
- package/src/index.html +11 -11
- package/src/index.ts +33 -33
- package/src/manipulation/SelectionBuffer.ts +81 -81
- package/src/manipulation/SelectionHighlight.ts +105 -84
- package/src/manipulation/SelectionIdAssignVisitor.ts +96 -96
- package/src/manipulation/SelectionManager.ts +196 -188
- package/src/manipulation/SelectionMode.ts +6 -6
- package/src/math/Mat3.ts +259 -259
- package/src/math/Mat4.ts +710 -710
- package/src/math/MatrixStrategy.ts +25 -25
- package/src/math/Quat.ts +65 -65
- package/src/math/Vec.ts +753 -753
- package/src/math/constants.ts +46 -46
- package/src/math/functions.ts +103 -103
- package/src/math/index.ts +74 -74
- package/src/phsics/joint.ts +137 -137
- package/src/primitives/arrow.ts +57 -57
- package/src/primitives/cone.ts +138 -138
- package/src/primitives/cube.ts +60 -60
- package/src/primitives/cylinder.ts +216 -216
- package/src/primitives/index.ts +13 -13
- package/src/primitives/plane.ts +31 -31
- package/src/primitives/sphere.ts +809 -809
- package/src/react/useBg2e.ts +69 -69
- package/src/render/BRDFIntegrationMap.ts +4 -4
- package/src/render/Environment.ts +135 -135
- package/src/render/FrameBuffer.ts +35 -35
- package/src/render/MaterialRenderer.ts +34 -34
- package/src/render/Pipeline.ts +108 -108
- package/src/render/PolyListRenderer.ts +47 -47
- package/src/render/RenderBuffer.ts +197 -197
- package/src/render/RenderQueue.ts +198 -198
- package/src/render/RenderState.ts +116 -116
- package/src/render/Renderer.ts +248 -248
- package/src/render/SceneAppController.ts +250 -250
- package/src/render/SceneRenderer.ts +387 -387
- package/src/render/Shader.ts +32 -32
- package/src/render/ShadowRenderer.ts +176 -176
- package/src/render/SkyCube.ts +105 -105
- package/src/render/SkySphere.ts +117 -117
- package/src/render/TextureMergerRenderer.ts +70 -70
- package/src/render/TextureRenderer.ts +34 -34
- package/src/render/index.ts +67 -67
- package/src/render/webgl/FrameBuffer.ts +9 -9
- package/src/render/webgl/MaterialRenderer.ts +112 -112
- package/src/render/webgl/Pipeline.ts +88 -88
- package/src/render/webgl/PolyListRenderer.ts +260 -260
- package/src/render/webgl/RenderBuffer.ts +226 -226
- package/src/render/webgl/Renderer.ts +262 -262
- package/src/render/webgl/SceneRenderer.ts +67 -67
- package/src/render/webgl/ShaderProgram.ts +424 -424
- package/src/render/webgl/ShadowRenderer.ts +6 -6
- package/src/render/webgl/SkyCube.ts +15 -15
- package/src/render/webgl/SkySphere.ts +15 -15
- package/src/render/webgl/State.ts +152 -152
- package/src/render/webgl/TextureRenderer.ts +167 -167
- package/src/render/webgl/VertexBuffer.ts +137 -137
- package/src/render/webgl/index.ts +35 -35
- package/src/scene/Camera.ts +458 -458
- package/src/scene/Chain.ts +44 -44
- package/src/scene/ChainJoint.ts +58 -58
- package/src/scene/Component.ts +177 -177
- package/src/scene/ComponentMap.ts +106 -106
- package/src/scene/Drawable.ts +154 -154
- package/src/scene/EnvironmentComponent.ts +141 -141
- package/src/scene/FindNodeVisitor.ts +59 -59
- package/src/scene/LightComponent.ts +154 -154
- package/src/scene/MatrixState.ts +46 -46
- package/src/scene/Node.ts +328 -328
- package/src/scene/NodeVisitor.ts +15 -15
- package/src/scene/OrbitCameraController.ts +450 -450
- package/src/scene/SmoothOrbitCameraController.ts +99 -99
- package/src/scene/Transform.ts +73 -73
- package/src/scene/index.ts +60 -60
- package/src/shaders/BasicDiffuseColorShader.ts +111 -111
- package/src/shaders/BasicPBRLightShader.ts +276 -276
- package/src/shaders/DebugRenderShader.ts +97 -97
- package/src/shaders/DepthRenderShader.ts +127 -127
- package/src/shaders/IrradianceMapCubeShader.ts +115 -115
- package/src/shaders/PBRLightIBLShader.ts +486 -486
- package/src/shaders/PickSelectionShader.ts +101 -101
- package/src/shaders/PresentDebugFramebufferShader.ts +118 -118
- package/src/shaders/PresentTextureShader.ts +99 -99
- package/src/shaders/SelectionHighlightShader.ts +143 -127
- package/src/shaders/ShaderFunction.ts +318 -318
- package/src/shaders/SkyCubeShader.ts +93 -93
- package/src/shaders/SkySphereShader.ts +102 -102
- package/src/shaders/SpecularMapCubeShader.ts +164 -164
- package/src/shaders/TextureMergerShader.ts +171 -171
- package/src/shaders/index.ts +36 -36
- package/src/shaders/webgl/color_correction.glsl +47 -47
- package/src/shaders/webgl/constants.glsl +6 -6
- package/src/shaders/webgl/index.ts +70 -70
- package/src/shaders/webgl/normal_map.glsl +9 -9
- package/src/shaders/webgl/pbr.glsl +173 -173
- package/src/shaders/webgl/uniforms.glsl +91 -91
- package/src/shaders/webgl_shader_lib.ts +213 -213
- package/src/tools/BinaryResourceProvider.ts +14 -14
- package/src/tools/ImageResourceProvider.ts +66 -66
- package/src/tools/MaterialModifier.ts +446 -446
- package/src/tools/Resource.ts +203 -203
- package/src/tools/ResourceProvider.ts +69 -69
- package/src/tools/TextResourceProvider.ts +24 -24
- package/src/tools/TextureCache.ts +51 -51
- package/src/tools/TextureResourceDatabase.ts +100 -100
- package/src/tools/UserAgent.ts +362 -362
- package/src/tools/VideoResourceProvider.ts +50 -50
- package/src/tools/WriteStrategy.ts +22 -22
- package/src/tools/base64.ts +11 -11
- package/src/tools/crypto.ts +19 -19
- package/src/tools/endiantess.ts +13 -13
- package/src/tools/image.ts +18 -18
- package/src/tools/index.ts +41 -41
- package/src/tools/processType.ts +39 -39
- package/src/vite-env.d.ts +12 -12
|
@@ -1,424 +1,424 @@
|
|
|
1
|
-
import Mat4 from "../../math/Mat4";
|
|
2
|
-
import Mat3 from "../../math/Mat3";
|
|
3
|
-
import { TextureTarget, TextureTargetName } from "../../base/Texture";
|
|
4
|
-
import type PolyListRenderer from "./PolyListRenderer";
|
|
5
|
-
import type TextureRenderer from "./TextureRenderer";
|
|
6
|
-
import Vec from "../../math/Vec";
|
|
7
|
-
|
|
8
|
-
export const ShaderType = {
|
|
9
|
-
VERTEX: 0,
|
|
10
|
-
FRAGMENT: 1
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
interface WebGLProgramWithExtras extends WebGLProgram {
|
|
14
|
-
__shaderProgram__?: ShaderProgram;
|
|
15
|
-
__id__?: symbol;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const g_programId = new WeakMap<WebGLProgram, symbol>();
|
|
19
|
-
|
|
20
|
-
function getProgramId(program: WebGLProgram): symbol {
|
|
21
|
-
let id = g_programId.get(program);
|
|
22
|
-
if (!id) {
|
|
23
|
-
id = Symbol();
|
|
24
|
-
g_programId.set(program, id);
|
|
25
|
-
}
|
|
26
|
-
return id;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
export default class ShaderProgram {
|
|
31
|
-
private _gl: WebGLRenderingContext;
|
|
32
|
-
private _name: string;
|
|
33
|
-
private _program: WebGLProgram | null;
|
|
34
|
-
private _attribLocations: { [key: string]: number };
|
|
35
|
-
private _uniformLocations: { [key: string]: WebGLUniformLocation | null };
|
|
36
|
-
private _failed: boolean;
|
|
37
|
-
|
|
38
|
-
protected destroy(): void {
|
|
39
|
-
delete (this._program as WebGLProgramWithExtras).__shaderProgram__;
|
|
40
|
-
this._gl.deleteProgram(this._program);
|
|
41
|
-
this._program = null;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
static Create(gl: WebGLRenderingContext, name: string, vertexCode: string | string[], fragmentCode: string | string[]): ShaderProgram {
|
|
45
|
-
if (!vertexCode || !fragmentCode) {
|
|
46
|
-
throw new Error("ShaderProgram.Create(): Invalid vertex or fragment code");
|
|
47
|
-
}
|
|
48
|
-
const result = new ShaderProgram(gl, name);
|
|
49
|
-
if (!Array.isArray(vertexCode)) {
|
|
50
|
-
vertexCode = [vertexCode];
|
|
51
|
-
}
|
|
52
|
-
if (!Array.isArray(fragmentCode)) {
|
|
53
|
-
fragmentCode = [fragmentCode];
|
|
54
|
-
}
|
|
55
|
-
vertexCode.forEach(shaderCode => result.attachVertexSource(shaderCode));
|
|
56
|
-
fragmentCode.forEach(shaderCode => result.attachFragmentSource(shaderCode));
|
|
57
|
-
result.link();
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
static GetShaderProgram(glProgram: WebGLProgram): ShaderProgram | null {
|
|
62
|
-
return (glProgram as WebGLProgramWithExtras).__shaderProgram__ || null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
static Delete(shaderProgram: ShaderProgram): void {
|
|
66
|
-
shaderProgram.destroy();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
constructor(gl: WebGLRenderingContext, name: string = "") {
|
|
70
|
-
this._gl = gl;
|
|
71
|
-
this._name = name;
|
|
72
|
-
this._program = gl.createProgram();
|
|
73
|
-
(this._program as WebGLProgramWithExtras).__id__ = getProgramId(this._program);
|
|
74
|
-
(this._program as WebGLProgramWithExtras).__shaderProgram__ = this;
|
|
75
|
-
this._attribLocations = {};
|
|
76
|
-
this._uniformLocations = {};
|
|
77
|
-
this._failed = false;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
get program(): WebGLProgram | null {
|
|
81
|
-
return this._program;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
get name(): string {
|
|
85
|
-
return this._name;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
attachSource(src: string, type: number): void {
|
|
89
|
-
if (this._failed || !this._program) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const gl = this._gl;
|
|
94
|
-
if (type === ShaderType.VERTEX) {
|
|
95
|
-
type = gl.VERTEX_SHADER;
|
|
96
|
-
}
|
|
97
|
-
else if (type === ShaderType.FRAGMENT) {
|
|
98
|
-
type = gl.FRAGMENT_SHADER;
|
|
99
|
-
}
|
|
100
|
-
const shader = gl.createShader(type);
|
|
101
|
-
if (!shader) return;
|
|
102
|
-
gl.shaderSource(shader, src);
|
|
103
|
-
gl.compileShader(shader);
|
|
104
|
-
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
105
|
-
this._failed = true;
|
|
106
|
-
const debugCode = src.split(/\r?\n/)
|
|
107
|
-
.map((line,i) => `${i + 1} | ${line}`)
|
|
108
|
-
.join('\n');
|
|
109
|
-
throw new Error(`Error compiling shader: \n${gl.getShaderInfoLog(shader)}
|
|
110
|
-
${debugCode}
|
|
111
|
-
`);
|
|
112
|
-
}
|
|
113
|
-
gl.attachShader(this._program, shader);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
attachVertexSource(src: string): void {
|
|
117
|
-
this.attachSource(src, ShaderType.VERTEX);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
attachFragmentSource(src: string): void {
|
|
121
|
-
this.attachSource(src, ShaderType.FRAGMENT);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
link(): void {
|
|
125
|
-
if (this._failed || !this._program) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
const gl = this._gl;
|
|
129
|
-
gl.linkProgram(this._program);
|
|
130
|
-
if (!gl.getProgramParameter(this._program, gl.LINK_STATUS)) {
|
|
131
|
-
throw new Error(`Error linking program: \n${gl.getProgramInfoLog(this._program)}`);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
useProgram(): void {
|
|
136
|
-
this._gl.useProgram(this._program);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
getAttribLocation(name: string): number {
|
|
140
|
-
if (!this._program) return -1;
|
|
141
|
-
this._attribLocations[name] = this._attribLocations[name] || this._gl.getAttribLocation(this._program, name);
|
|
142
|
-
return this._attribLocations[name];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
checkInvalidLocations(): boolean {
|
|
146
|
-
let status = true;
|
|
147
|
-
for (const name in this._attribLocations) {
|
|
148
|
-
if (this._attribLocations[name] == -1) {
|
|
149
|
-
console.warn(`Invalid attrib location for name '${name}'`);
|
|
150
|
-
status = false;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return status;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
getUniformLocation(name: string): WebGLUniformLocation | null {
|
|
157
|
-
if (!this._program) return null;
|
|
158
|
-
this._uniformLocations[name] = this._gl.getUniformLocation(this._program, name);
|
|
159
|
-
return this._uniformLocations[name];
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
vertexAttribPointer(name: string, size: number, format: number, normalize: boolean, stride: number, offset: number): void {
|
|
163
|
-
const location = this.getAttribLocation(name);
|
|
164
|
-
this._gl.vertexAttribPointer(location, size, format, normalize, stride, offset);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
enableVertexAttribArray(name: string): void {
|
|
168
|
-
const location = this.getAttribLocation(name);
|
|
169
|
-
this._gl.enableVertexAttribArray(location);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
positionAttribPointer({ name, stride, size = 3, offset = 0, enable = false, bytesPerElement = Float32Array.BYTES_PER_ELEMENT }: {
|
|
173
|
-
name: string;
|
|
174
|
-
stride: number;
|
|
175
|
-
size?: number;
|
|
176
|
-
offset?: number;
|
|
177
|
-
enable?: boolean;
|
|
178
|
-
bytesPerElement?: number;
|
|
179
|
-
}): void {
|
|
180
|
-
this.vertexAttribPointer(name, size, this._gl.FLOAT, false, stride * bytesPerElement, offset * bytesPerElement);
|
|
181
|
-
if (enable) {
|
|
182
|
-
this.enableVertexAttribArray(name);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
normalAttribPointer({ name, size = 3, stride, offset = 0, enable = false, bytesPerElement = Float32Array.BYTES_PER_ELEMENT }: {
|
|
187
|
-
name: string;
|
|
188
|
-
size?: number;
|
|
189
|
-
stride: number;
|
|
190
|
-
offset?: number;
|
|
191
|
-
enable?: boolean;
|
|
192
|
-
bytesPerElement?: number;
|
|
193
|
-
}): void {
|
|
194
|
-
this.vertexAttribPointer(name, size, this._gl.FLOAT, true, stride * bytesPerElement, offset * bytesPerElement);
|
|
195
|
-
if (enable) {
|
|
196
|
-
this.enableVertexAttribArray(name);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
tangentAttribPointer({ name, size = 3, stride, offset = 0, enable = false, bytesPerElement = Float32Array.BYTES_PER_ELEMENT }: {
|
|
201
|
-
name: string;
|
|
202
|
-
size?: number;
|
|
203
|
-
stride: number;
|
|
204
|
-
offset?: number;
|
|
205
|
-
enable?: boolean;
|
|
206
|
-
bytesPerElement?: number;
|
|
207
|
-
}): void {
|
|
208
|
-
this.vertexAttribPointer(name, size, this._gl.FLOAT, true, stride * bytesPerElement, offset * bytesPerElement);
|
|
209
|
-
if (enable) {
|
|
210
|
-
this.enableVertexAttribArray(name);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
texCoordAttribPointer({ name, stride, offset, enable = false, bytesPerElement = Float32Array.BYTES_PER_ELEMENT }: {
|
|
215
|
-
name: string;
|
|
216
|
-
stride: number;
|
|
217
|
-
offset: number;
|
|
218
|
-
enable?: boolean;
|
|
219
|
-
bytesPerElement?: number;
|
|
220
|
-
}): void {
|
|
221
|
-
this.vertexAttribPointer(name, 2, this._gl.FLOAT, false, stride * bytesPerElement, offset * bytesPerElement);
|
|
222
|
-
if (enable) {
|
|
223
|
-
this.enableVertexAttribArray(name);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
colorAttribPointer({ name, size = 4, stride, offset = 0, enable = false, bytesPerElement = Float32Array.BYTES_PER_ELEMENT }: {
|
|
228
|
-
name: string;
|
|
229
|
-
size?: number;
|
|
230
|
-
stride: number;
|
|
231
|
-
offset?: number;
|
|
232
|
-
enable?: boolean;
|
|
233
|
-
bytesPerElement?: number;
|
|
234
|
-
}): void {
|
|
235
|
-
this.vertexAttribPointer(name, size, this._gl.FLOAT, false, stride * bytesPerElement, offset * bytesPerElement);
|
|
236
|
-
if (enable) {
|
|
237
|
-
this.enableVertexAttribArray(name);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
uniformMatrix2fv(name: string, transpose: boolean, value: Float32List): void {
|
|
242
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
243
|
-
this._gl.uniformMatrix2fv(location, transpose, value);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
uniformMatrix3fv(name: string, transpose: boolean, value: Float32List): void {
|
|
247
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
248
|
-
this._gl.uniformMatrix3fv(location, transpose, value);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
uniformMatrix4fv(name: string, transpose: boolean, value: Float32List): void {
|
|
252
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
253
|
-
this._gl.uniformMatrix4fv(location, transpose, value);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
uniform1f(name: string, v0: number): void {
|
|
257
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
258
|
-
this._gl.uniform1f(location, v0);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
uniform1fv(name: string, value: Float32List): void {
|
|
262
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
263
|
-
this._gl.uniform1fv(location, value);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
uniform1i(name: string, v0: number): void {
|
|
267
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
268
|
-
this._gl.uniform1i(location, v0);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
uniform1iv(name: string, value: Int32List): void {
|
|
272
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
273
|
-
this._gl.uniform1iv(location, value);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
uniform2f(name: string, v0: number, v1: number): void {
|
|
277
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
278
|
-
this._gl.uniform2f(location, v0, v1);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
uniform2fv(name: string, value: Float32List): void {
|
|
282
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
283
|
-
this._gl.uniform2fv(location, value);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
uniform2i(name: string, v0: number, v1: number): void {
|
|
287
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
288
|
-
this._gl.uniform2i(location, v0, v1);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
uniform2iv(name: string, value: Int32List): void {
|
|
292
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
293
|
-
this._gl.uniform2iv(location, value);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
uniform3f(name: string, v0: number, v1: number, v2: number): void {
|
|
297
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
298
|
-
this._gl.uniform3f(location, v0, v1, v2);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
uniform3fv(name: string, value: Float32List): void {
|
|
302
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
303
|
-
this._gl.uniform3fv(location, value);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
uniform3i(name: string, v0: number, v1: number, v2: number): void {
|
|
307
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
308
|
-
this._gl.uniform3i(location, v0, v1, v2);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
uniform3iv(name: string, value: Int32List): void {
|
|
312
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
313
|
-
this._gl.uniform3iv(location, value);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
uniform4f(name: string, v0: number, v1: number, v2: number, v3: number): void {
|
|
317
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
318
|
-
this._gl.uniform4f(location, v0, v1, v2, v3);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
uniform4fv(name: string, value: Float32List): void {
|
|
322
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
323
|
-
this._gl.uniform4fv(location, value);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
uniform4i(name: string, v0: number, v1: number, v2: number, v3: number): void {
|
|
327
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
328
|
-
this._gl.uniform4i(location, v0, v1, v2, v3);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
uniform4iv(name: string, value: Int32List): void {
|
|
332
|
-
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
333
|
-
this._gl.uniform4iv(location, value);
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// Utility functions
|
|
337
|
-
bindAttribs(polyListRenderer: PolyListRenderer, {
|
|
338
|
-
position,
|
|
339
|
-
normal = null,
|
|
340
|
-
tex0 = null,
|
|
341
|
-
tex1 = null,
|
|
342
|
-
tex2 = null,
|
|
343
|
-
color = null,
|
|
344
|
-
tangent = null
|
|
345
|
-
}: {
|
|
346
|
-
position: string;
|
|
347
|
-
normal?: string | null;
|
|
348
|
-
tex0?: string | null;
|
|
349
|
-
tex1?: string | null;
|
|
350
|
-
tex2?: string | null;
|
|
351
|
-
color?: string | null;
|
|
352
|
-
tangent?: string | null;
|
|
353
|
-
}): void {
|
|
354
|
-
this.positionAttribPointer(polyListRenderer.positionAttribParams(position));
|
|
355
|
-
if (normal) {
|
|
356
|
-
this.normalAttribPointer(polyListRenderer.normalAttribParams(normal));
|
|
357
|
-
}
|
|
358
|
-
if (tex0) {
|
|
359
|
-
this.texCoordAttribPointer(polyListRenderer.texCoord0AttribParams(tex0));
|
|
360
|
-
}
|
|
361
|
-
if (tex1) {
|
|
362
|
-
this.texCoordAttribPointer(polyListRenderer.texCoord1AttribParams(tex1));
|
|
363
|
-
}
|
|
364
|
-
if (tex2) {
|
|
365
|
-
this.texCoordAttribPointer(polyListRenderer.texCoord2AttribParams(tex2));
|
|
366
|
-
}
|
|
367
|
-
if (tangent) {
|
|
368
|
-
this.tangentAttribPointer(polyListRenderer.tangentAttribParams(tangent));
|
|
369
|
-
}
|
|
370
|
-
if (color) {
|
|
371
|
-
this.colorAttribPointer(polyListRenderer.colorAttribParams(color));
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
bindMatrix(uniformName: string, matrix: Mat4 | Mat3): void {
|
|
376
|
-
if (matrix instanceof Mat4) {
|
|
377
|
-
this.uniformMatrix4fv(uniformName, false, matrix);
|
|
378
|
-
}
|
|
379
|
-
else if (matrix instanceof Mat3) {
|
|
380
|
-
this.uniformMatrix3fv(uniformName, false, matrix);
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
bindVector(uniformName: string, vec: Vec | number[]): void {
|
|
385
|
-
switch (vec.length) {
|
|
386
|
-
case 2:
|
|
387
|
-
this.uniform2fv(uniformName, vec);
|
|
388
|
-
break;
|
|
389
|
-
case 3:
|
|
390
|
-
this.uniform3fv(uniformName, vec);
|
|
391
|
-
break;
|
|
392
|
-
case 4:
|
|
393
|
-
this.uniform4fv(uniformName, vec);
|
|
394
|
-
break;
|
|
395
|
-
default:
|
|
396
|
-
throw new Error("ShaderProgram.bindVector(): invalid vector size");
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
bindTexture(uniformName: string, textureRenderer: TextureRenderer, textureUnit: number): void {
|
|
401
|
-
const gl = this._gl;
|
|
402
|
-
const webglTexture = textureRenderer.getApiObject();
|
|
403
|
-
|
|
404
|
-
gl.activeTexture(gl.TEXTURE0 + textureUnit);
|
|
405
|
-
switch (textureRenderer.texture.target) {
|
|
406
|
-
case TextureTarget.TEXTURE_2D:
|
|
407
|
-
gl.bindTexture(gl.TEXTURE_2D, webglTexture);
|
|
408
|
-
break;
|
|
409
|
-
case TextureTarget.CUBE_MAP:
|
|
410
|
-
gl.bindTexture(gl.TEXTURE_CUBE_MAP, webglTexture);
|
|
411
|
-
break;
|
|
412
|
-
}
|
|
413
|
-
this.uniform1i(uniformName, textureUnit);
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
validate(): void {
|
|
417
|
-
if (!this._program) return;
|
|
418
|
-
const gl = this._gl;
|
|
419
|
-
gl.validateProgram(this._program);
|
|
420
|
-
if (!gl.getProgramParameter(this._program, gl.VALIDATE_STATUS)) {
|
|
421
|
-
throw new Error(`Error validating program:\n${gl.getProgramInfoLog(this._program)}`);
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
1
|
+
import Mat4 from "../../math/Mat4";
|
|
2
|
+
import Mat3 from "../../math/Mat3";
|
|
3
|
+
import { TextureTarget, TextureTargetName } from "../../base/Texture";
|
|
4
|
+
import type PolyListRenderer from "./PolyListRenderer";
|
|
5
|
+
import type TextureRenderer from "./TextureRenderer";
|
|
6
|
+
import Vec from "../../math/Vec";
|
|
7
|
+
|
|
8
|
+
export const ShaderType = {
|
|
9
|
+
VERTEX: 0,
|
|
10
|
+
FRAGMENT: 1
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
interface WebGLProgramWithExtras extends WebGLProgram {
|
|
14
|
+
__shaderProgram__?: ShaderProgram;
|
|
15
|
+
__id__?: symbol;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const g_programId = new WeakMap<WebGLProgram, symbol>();
|
|
19
|
+
|
|
20
|
+
function getProgramId(program: WebGLProgram): symbol {
|
|
21
|
+
let id = g_programId.get(program);
|
|
22
|
+
if (!id) {
|
|
23
|
+
id = Symbol();
|
|
24
|
+
g_programId.set(program, id);
|
|
25
|
+
}
|
|
26
|
+
return id;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export default class ShaderProgram {
|
|
31
|
+
private _gl: WebGLRenderingContext;
|
|
32
|
+
private _name: string;
|
|
33
|
+
private _program: WebGLProgram | null;
|
|
34
|
+
private _attribLocations: { [key: string]: number };
|
|
35
|
+
private _uniformLocations: { [key: string]: WebGLUniformLocation | null };
|
|
36
|
+
private _failed: boolean;
|
|
37
|
+
|
|
38
|
+
protected destroy(): void {
|
|
39
|
+
delete (this._program as WebGLProgramWithExtras).__shaderProgram__;
|
|
40
|
+
this._gl.deleteProgram(this._program);
|
|
41
|
+
this._program = null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static Create(gl: WebGLRenderingContext, name: string, vertexCode: string | string[], fragmentCode: string | string[]): ShaderProgram {
|
|
45
|
+
if (!vertexCode || !fragmentCode) {
|
|
46
|
+
throw new Error("ShaderProgram.Create(): Invalid vertex or fragment code");
|
|
47
|
+
}
|
|
48
|
+
const result = new ShaderProgram(gl, name);
|
|
49
|
+
if (!Array.isArray(vertexCode)) {
|
|
50
|
+
vertexCode = [vertexCode];
|
|
51
|
+
}
|
|
52
|
+
if (!Array.isArray(fragmentCode)) {
|
|
53
|
+
fragmentCode = [fragmentCode];
|
|
54
|
+
}
|
|
55
|
+
vertexCode.forEach(shaderCode => result.attachVertexSource(shaderCode));
|
|
56
|
+
fragmentCode.forEach(shaderCode => result.attachFragmentSource(shaderCode));
|
|
57
|
+
result.link();
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static GetShaderProgram(glProgram: WebGLProgram): ShaderProgram | null {
|
|
62
|
+
return (glProgram as WebGLProgramWithExtras).__shaderProgram__ || null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static Delete(shaderProgram: ShaderProgram): void {
|
|
66
|
+
shaderProgram.destroy();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
constructor(gl: WebGLRenderingContext, name: string = "") {
|
|
70
|
+
this._gl = gl;
|
|
71
|
+
this._name = name;
|
|
72
|
+
this._program = gl.createProgram();
|
|
73
|
+
(this._program as WebGLProgramWithExtras).__id__ = getProgramId(this._program);
|
|
74
|
+
(this._program as WebGLProgramWithExtras).__shaderProgram__ = this;
|
|
75
|
+
this._attribLocations = {};
|
|
76
|
+
this._uniformLocations = {};
|
|
77
|
+
this._failed = false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get program(): WebGLProgram | null {
|
|
81
|
+
return this._program;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get name(): string {
|
|
85
|
+
return this._name;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
attachSource(src: string, type: number): void {
|
|
89
|
+
if (this._failed || !this._program) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const gl = this._gl;
|
|
94
|
+
if (type === ShaderType.VERTEX) {
|
|
95
|
+
type = gl.VERTEX_SHADER;
|
|
96
|
+
}
|
|
97
|
+
else if (type === ShaderType.FRAGMENT) {
|
|
98
|
+
type = gl.FRAGMENT_SHADER;
|
|
99
|
+
}
|
|
100
|
+
const shader = gl.createShader(type);
|
|
101
|
+
if (!shader) return;
|
|
102
|
+
gl.shaderSource(shader, src);
|
|
103
|
+
gl.compileShader(shader);
|
|
104
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
105
|
+
this._failed = true;
|
|
106
|
+
const debugCode = src.split(/\r?\n/)
|
|
107
|
+
.map((line,i) => `${i + 1} | ${line}`)
|
|
108
|
+
.join('\n');
|
|
109
|
+
throw new Error(`Error compiling shader: \n${gl.getShaderInfoLog(shader)}
|
|
110
|
+
${debugCode}
|
|
111
|
+
`);
|
|
112
|
+
}
|
|
113
|
+
gl.attachShader(this._program, shader);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
attachVertexSource(src: string): void {
|
|
117
|
+
this.attachSource(src, ShaderType.VERTEX);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
attachFragmentSource(src: string): void {
|
|
121
|
+
this.attachSource(src, ShaderType.FRAGMENT);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
link(): void {
|
|
125
|
+
if (this._failed || !this._program) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const gl = this._gl;
|
|
129
|
+
gl.linkProgram(this._program);
|
|
130
|
+
if (!gl.getProgramParameter(this._program, gl.LINK_STATUS)) {
|
|
131
|
+
throw new Error(`Error linking program: \n${gl.getProgramInfoLog(this._program)}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
useProgram(): void {
|
|
136
|
+
this._gl.useProgram(this._program);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
getAttribLocation(name: string): number {
|
|
140
|
+
if (!this._program) return -1;
|
|
141
|
+
this._attribLocations[name] = this._attribLocations[name] || this._gl.getAttribLocation(this._program, name);
|
|
142
|
+
return this._attribLocations[name];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
checkInvalidLocations(): boolean {
|
|
146
|
+
let status = true;
|
|
147
|
+
for (const name in this._attribLocations) {
|
|
148
|
+
if (this._attribLocations[name] == -1) {
|
|
149
|
+
console.warn(`Invalid attrib location for name '${name}'`);
|
|
150
|
+
status = false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return status;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
getUniformLocation(name: string): WebGLUniformLocation | null {
|
|
157
|
+
if (!this._program) return null;
|
|
158
|
+
this._uniformLocations[name] = this._gl.getUniformLocation(this._program, name);
|
|
159
|
+
return this._uniformLocations[name];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
vertexAttribPointer(name: string, size: number, format: number, normalize: boolean, stride: number, offset: number): void {
|
|
163
|
+
const location = this.getAttribLocation(name);
|
|
164
|
+
this._gl.vertexAttribPointer(location, size, format, normalize, stride, offset);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
enableVertexAttribArray(name: string): void {
|
|
168
|
+
const location = this.getAttribLocation(name);
|
|
169
|
+
this._gl.enableVertexAttribArray(location);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
positionAttribPointer({ name, stride, size = 3, offset = 0, enable = false, bytesPerElement = Float32Array.BYTES_PER_ELEMENT }: {
|
|
173
|
+
name: string;
|
|
174
|
+
stride: number;
|
|
175
|
+
size?: number;
|
|
176
|
+
offset?: number;
|
|
177
|
+
enable?: boolean;
|
|
178
|
+
bytesPerElement?: number;
|
|
179
|
+
}): void {
|
|
180
|
+
this.vertexAttribPointer(name, size, this._gl.FLOAT, false, stride * bytesPerElement, offset * bytesPerElement);
|
|
181
|
+
if (enable) {
|
|
182
|
+
this.enableVertexAttribArray(name);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
normalAttribPointer({ name, size = 3, stride, offset = 0, enable = false, bytesPerElement = Float32Array.BYTES_PER_ELEMENT }: {
|
|
187
|
+
name: string;
|
|
188
|
+
size?: number;
|
|
189
|
+
stride: number;
|
|
190
|
+
offset?: number;
|
|
191
|
+
enable?: boolean;
|
|
192
|
+
bytesPerElement?: number;
|
|
193
|
+
}): void {
|
|
194
|
+
this.vertexAttribPointer(name, size, this._gl.FLOAT, true, stride * bytesPerElement, offset * bytesPerElement);
|
|
195
|
+
if (enable) {
|
|
196
|
+
this.enableVertexAttribArray(name);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
tangentAttribPointer({ name, size = 3, stride, offset = 0, enable = false, bytesPerElement = Float32Array.BYTES_PER_ELEMENT }: {
|
|
201
|
+
name: string;
|
|
202
|
+
size?: number;
|
|
203
|
+
stride: number;
|
|
204
|
+
offset?: number;
|
|
205
|
+
enable?: boolean;
|
|
206
|
+
bytesPerElement?: number;
|
|
207
|
+
}): void {
|
|
208
|
+
this.vertexAttribPointer(name, size, this._gl.FLOAT, true, stride * bytesPerElement, offset * bytesPerElement);
|
|
209
|
+
if (enable) {
|
|
210
|
+
this.enableVertexAttribArray(name);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
texCoordAttribPointer({ name, stride, offset, enable = false, bytesPerElement = Float32Array.BYTES_PER_ELEMENT }: {
|
|
215
|
+
name: string;
|
|
216
|
+
stride: number;
|
|
217
|
+
offset: number;
|
|
218
|
+
enable?: boolean;
|
|
219
|
+
bytesPerElement?: number;
|
|
220
|
+
}): void {
|
|
221
|
+
this.vertexAttribPointer(name, 2, this._gl.FLOAT, false, stride * bytesPerElement, offset * bytesPerElement);
|
|
222
|
+
if (enable) {
|
|
223
|
+
this.enableVertexAttribArray(name);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
colorAttribPointer({ name, size = 4, stride, offset = 0, enable = false, bytesPerElement = Float32Array.BYTES_PER_ELEMENT }: {
|
|
228
|
+
name: string;
|
|
229
|
+
size?: number;
|
|
230
|
+
stride: number;
|
|
231
|
+
offset?: number;
|
|
232
|
+
enable?: boolean;
|
|
233
|
+
bytesPerElement?: number;
|
|
234
|
+
}): void {
|
|
235
|
+
this.vertexAttribPointer(name, size, this._gl.FLOAT, false, stride * bytesPerElement, offset * bytesPerElement);
|
|
236
|
+
if (enable) {
|
|
237
|
+
this.enableVertexAttribArray(name);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
uniformMatrix2fv(name: string, transpose: boolean, value: Float32List): void {
|
|
242
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
243
|
+
this._gl.uniformMatrix2fv(location, transpose, value);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
uniformMatrix3fv(name: string, transpose: boolean, value: Float32List): void {
|
|
247
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
248
|
+
this._gl.uniformMatrix3fv(location, transpose, value);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
uniformMatrix4fv(name: string, transpose: boolean, value: Float32List): void {
|
|
252
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
253
|
+
this._gl.uniformMatrix4fv(location, transpose, value);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
uniform1f(name: string, v0: number): void {
|
|
257
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
258
|
+
this._gl.uniform1f(location, v0);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
uniform1fv(name: string, value: Float32List): void {
|
|
262
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
263
|
+
this._gl.uniform1fv(location, value);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
uniform1i(name: string, v0: number): void {
|
|
267
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
268
|
+
this._gl.uniform1i(location, v0);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
uniform1iv(name: string, value: Int32List): void {
|
|
272
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
273
|
+
this._gl.uniform1iv(location, value);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
uniform2f(name: string, v0: number, v1: number): void {
|
|
277
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
278
|
+
this._gl.uniform2f(location, v0, v1);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
uniform2fv(name: string, value: Float32List): void {
|
|
282
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
283
|
+
this._gl.uniform2fv(location, value);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
uniform2i(name: string, v0: number, v1: number): void {
|
|
287
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
288
|
+
this._gl.uniform2i(location, v0, v1);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
uniform2iv(name: string, value: Int32List): void {
|
|
292
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
293
|
+
this._gl.uniform2iv(location, value);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
uniform3f(name: string, v0: number, v1: number, v2: number): void {
|
|
297
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
298
|
+
this._gl.uniform3f(location, v0, v1, v2);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
uniform3fv(name: string, value: Float32List): void {
|
|
302
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
303
|
+
this._gl.uniform3fv(location, value);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
uniform3i(name: string, v0: number, v1: number, v2: number): void {
|
|
307
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
308
|
+
this._gl.uniform3i(location, v0, v1, v2);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
uniform3iv(name: string, value: Int32List): void {
|
|
312
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
313
|
+
this._gl.uniform3iv(location, value);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
uniform4f(name: string, v0: number, v1: number, v2: number, v3: number): void {
|
|
317
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
318
|
+
this._gl.uniform4f(location, v0, v1, v2, v3);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
uniform4fv(name: string, value: Float32List): void {
|
|
322
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
323
|
+
this._gl.uniform4fv(location, value);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
uniform4i(name: string, v0: number, v1: number, v2: number, v3: number): void {
|
|
327
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
328
|
+
this._gl.uniform4i(location, v0, v1, v2, v3);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
uniform4iv(name: string, value: Int32List): void {
|
|
332
|
+
const location = this._uniformLocations[name] || this.getUniformLocation(name);
|
|
333
|
+
this._gl.uniform4iv(location, value);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Utility functions
|
|
337
|
+
bindAttribs(polyListRenderer: PolyListRenderer, {
|
|
338
|
+
position,
|
|
339
|
+
normal = null,
|
|
340
|
+
tex0 = null,
|
|
341
|
+
tex1 = null,
|
|
342
|
+
tex2 = null,
|
|
343
|
+
color = null,
|
|
344
|
+
tangent = null
|
|
345
|
+
}: {
|
|
346
|
+
position: string;
|
|
347
|
+
normal?: string | null;
|
|
348
|
+
tex0?: string | null;
|
|
349
|
+
tex1?: string | null;
|
|
350
|
+
tex2?: string | null;
|
|
351
|
+
color?: string | null;
|
|
352
|
+
tangent?: string | null;
|
|
353
|
+
}): void {
|
|
354
|
+
this.positionAttribPointer(polyListRenderer.positionAttribParams(position));
|
|
355
|
+
if (normal) {
|
|
356
|
+
this.normalAttribPointer(polyListRenderer.normalAttribParams(normal));
|
|
357
|
+
}
|
|
358
|
+
if (tex0) {
|
|
359
|
+
this.texCoordAttribPointer(polyListRenderer.texCoord0AttribParams(tex0));
|
|
360
|
+
}
|
|
361
|
+
if (tex1) {
|
|
362
|
+
this.texCoordAttribPointer(polyListRenderer.texCoord1AttribParams(tex1));
|
|
363
|
+
}
|
|
364
|
+
if (tex2) {
|
|
365
|
+
this.texCoordAttribPointer(polyListRenderer.texCoord2AttribParams(tex2));
|
|
366
|
+
}
|
|
367
|
+
if (tangent) {
|
|
368
|
+
this.tangentAttribPointer(polyListRenderer.tangentAttribParams(tangent));
|
|
369
|
+
}
|
|
370
|
+
if (color) {
|
|
371
|
+
this.colorAttribPointer(polyListRenderer.colorAttribParams(color));
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
bindMatrix(uniformName: string, matrix: Mat4 | Mat3): void {
|
|
376
|
+
if (matrix instanceof Mat4) {
|
|
377
|
+
this.uniformMatrix4fv(uniformName, false, matrix);
|
|
378
|
+
}
|
|
379
|
+
else if (matrix instanceof Mat3) {
|
|
380
|
+
this.uniformMatrix3fv(uniformName, false, matrix);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
bindVector(uniformName: string, vec: Vec | number[]): void {
|
|
385
|
+
switch (vec.length) {
|
|
386
|
+
case 2:
|
|
387
|
+
this.uniform2fv(uniformName, vec);
|
|
388
|
+
break;
|
|
389
|
+
case 3:
|
|
390
|
+
this.uniform3fv(uniformName, vec);
|
|
391
|
+
break;
|
|
392
|
+
case 4:
|
|
393
|
+
this.uniform4fv(uniformName, vec);
|
|
394
|
+
break;
|
|
395
|
+
default:
|
|
396
|
+
throw new Error("ShaderProgram.bindVector(): invalid vector size");
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
bindTexture(uniformName: string, textureRenderer: TextureRenderer, textureUnit: number): void {
|
|
401
|
+
const gl = this._gl;
|
|
402
|
+
const webglTexture = textureRenderer.getApiObject();
|
|
403
|
+
|
|
404
|
+
gl.activeTexture(gl.TEXTURE0 + textureUnit);
|
|
405
|
+
switch (textureRenderer.texture.target) {
|
|
406
|
+
case TextureTarget.TEXTURE_2D:
|
|
407
|
+
gl.bindTexture(gl.TEXTURE_2D, webglTexture);
|
|
408
|
+
break;
|
|
409
|
+
case TextureTarget.CUBE_MAP:
|
|
410
|
+
gl.bindTexture(gl.TEXTURE_CUBE_MAP, webglTexture);
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
this.uniform1i(uniformName, textureUnit);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
validate(): void {
|
|
417
|
+
if (!this._program) return;
|
|
418
|
+
const gl = this._gl;
|
|
419
|
+
gl.validateProgram(this._program);
|
|
420
|
+
if (!gl.getProgramParameter(this._program, gl.VALIDATE_STATUS)) {
|
|
421
|
+
throw new Error(`Error validating program:\n${gl.getProgramInfoLog(this._program)}`);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|