@fonsecabarreto/genesis-gl-core 0.1.0
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/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/Camera-DY_8gx3C.d.ts +45 -0
- package/dist/Core/classes/Material.d.ts +3 -0
- package/dist/Core/classes/Material.js +9 -0
- package/dist/Core/classes/Material.js.map +1 -0
- package/dist/Core/classes/Model.d.ts +5 -0
- package/dist/Core/classes/Model.js +7 -0
- package/dist/Core/classes/Model.js.map +1 -0
- package/dist/Core/classes/Renderer.d.ts +30 -0
- package/dist/Core/classes/Renderer.js +11 -0
- package/dist/Core/classes/Renderer.js.map +1 -0
- package/dist/Core/classes/Scene.d.ts +37 -0
- package/dist/Core/classes/Scene.js +7 -0
- package/dist/Core/classes/Scene.js.map +1 -0
- package/dist/Core/classes/Viewport.d.ts +37 -0
- package/dist/Core/classes/Viewport.js +7 -0
- package/dist/Core/classes/Viewport.js.map +1 -0
- package/dist/Core/domain/interfaces/Vectors.d.ts +4 -0
- package/dist/Core/domain/interfaces/Vectors.js +1 -0
- package/dist/Core/domain/interfaces/Vectors.js.map +1 -0
- package/dist/Core/index.d.ts +10 -0
- package/dist/Core/index.js +51 -0
- package/dist/Core/index.js.map +1 -0
- package/dist/Core/utils/get-overlap.d.ts +3 -0
- package/dist/Core/utils/get-overlap.js +11 -0
- package/dist/Core/utils/get-overlap.js.map +1 -0
- package/dist/Core/utils/load-glb.d.ts +101 -0
- package/dist/Core/utils/load-glb.js +697 -0
- package/dist/Core/utils/load-glb.js.map +1 -0
- package/dist/Core/utils/parse-obj.d.ts +10 -0
- package/dist/Core/utils/parse-obj.js +183 -0
- package/dist/Core/utils/parse-obj.js.map +1 -0
- package/dist/Editor/index.d.ts +364 -0
- package/dist/Editor/index.js +1737 -0
- package/dist/Editor/index.js.map +1 -0
- package/dist/Game/controls/KeyboardInput.d.ts +8 -0
- package/dist/Game/controls/KeyboardInput.js +7 -0
- package/dist/Game/controls/KeyboardInput.js.map +1 -0
- package/dist/Game/index.d.ts +45 -0
- package/dist/Game/index.js +353 -0
- package/dist/Game/index.js.map +1 -0
- package/dist/KeyboardControl-5w7Vm0J0.d.ts +18 -0
- package/dist/KeyboardInput-DTsfj3tE.d.ts +166 -0
- package/dist/Material-BGLkldxv.d.ts +74 -0
- package/dist/Model-CQvDXd-b.d.ts +302 -0
- package/dist/WebGLCore-DR7ZHJB0.d.ts +22 -0
- package/dist/chunk-3ULETMWF.js +144 -0
- package/dist/chunk-3ULETMWF.js.map +1 -0
- package/dist/chunk-5TAAXI6S.js +330 -0
- package/dist/chunk-5TAAXI6S.js.map +1 -0
- package/dist/chunk-6LS6AO5H.js +296 -0
- package/dist/chunk-6LS6AO5H.js.map +1 -0
- package/dist/chunk-JK2HEZAT.js +317 -0
- package/dist/chunk-JK2HEZAT.js.map +1 -0
- package/dist/chunk-P7QOKDLY.js +57 -0
- package/dist/chunk-P7QOKDLY.js.map +1 -0
- package/dist/chunk-QCQVJCSR.js +968 -0
- package/dist/chunk-QCQVJCSR.js.map +1 -0
- package/dist/chunk-SUNYSY45.js +81 -0
- package/dist/chunk-SUNYSY45.js.map +1 -0
- package/package.json +83 -0
|
@@ -0,0 +1,968 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Material
|
|
3
|
+
} from "./chunk-6LS6AO5H.js";
|
|
4
|
+
|
|
5
|
+
// src/Core/classes/Renderer.ts
|
|
6
|
+
import { mat4 as mat42 } from "gl-matrix";
|
|
7
|
+
|
|
8
|
+
// src/Core/utils/compute-bounds.ts
|
|
9
|
+
function computeVertexBounds(vertices) {
|
|
10
|
+
if (!vertices || vertices.length === 0) {
|
|
11
|
+
return { min: [0, 0, 0], max: [0, 0, 0] };
|
|
12
|
+
}
|
|
13
|
+
let minX = Infinity, minY = Infinity, minZ = Infinity;
|
|
14
|
+
let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
|
|
15
|
+
for (let i = 0; i < vertices.length; i += 3) {
|
|
16
|
+
const x = vertices[i];
|
|
17
|
+
const y = vertices[i + 1];
|
|
18
|
+
const z = vertices[i + 2];
|
|
19
|
+
if (x < minX) minX = x;
|
|
20
|
+
if (y < minY) minY = y;
|
|
21
|
+
if (z < minZ) minZ = z;
|
|
22
|
+
if (x > maxX) maxX = x;
|
|
23
|
+
if (y > maxY) maxY = y;
|
|
24
|
+
if (z > maxZ) maxZ = z;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
min: [minX, minY, minZ],
|
|
28
|
+
max: [maxX, maxY, maxZ]
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/Core/classes/Mesh.ts
|
|
33
|
+
var GL_LINES = 1;
|
|
34
|
+
var GL_TRIANGLES = 4;
|
|
35
|
+
var INITIAL_BOUNDING_BOX = { min: [0, 0, 0], max: [0, 0, 0] };
|
|
36
|
+
var Mesh = class _Mesh {
|
|
37
|
+
name;
|
|
38
|
+
vertices;
|
|
39
|
+
normals;
|
|
40
|
+
texCoords;
|
|
41
|
+
indices;
|
|
42
|
+
mode;
|
|
43
|
+
material;
|
|
44
|
+
// ── Skinning data (optional) ─────────────────────────────────
|
|
45
|
+
/** Per-vertex joint indices (vec4 per vertex, 4 influences). */
|
|
46
|
+
jointIndices = null;
|
|
47
|
+
/** Per-vertex joint weights (vec4 per vertex, 4 influences). */
|
|
48
|
+
jointWeights = null;
|
|
49
|
+
// WebGL buffers aggregate
|
|
50
|
+
buffers = null;
|
|
51
|
+
boundingBox = INITIAL_BOUNDING_BOX;
|
|
52
|
+
isCollidable = true;
|
|
53
|
+
isInitialized = false;
|
|
54
|
+
constructor(name, vertices, normals, material, texCoords = new Float32Array(), indices = null) {
|
|
55
|
+
this.mode = GL_TRIANGLES;
|
|
56
|
+
this.name = name;
|
|
57
|
+
this.normals = normals;
|
|
58
|
+
this.indices = indices;
|
|
59
|
+
this.vertices = vertices;
|
|
60
|
+
this.texCoords = texCoords;
|
|
61
|
+
this.material = material;
|
|
62
|
+
this.computeBounds();
|
|
63
|
+
}
|
|
64
|
+
setMode(newMode) {
|
|
65
|
+
this.mode = newMode;
|
|
66
|
+
}
|
|
67
|
+
/** Whether this mesh has skinning data for skeletal animation. */
|
|
68
|
+
get isSkinned() {
|
|
69
|
+
return this.jointIndices !== null && this.jointWeights !== null;
|
|
70
|
+
}
|
|
71
|
+
computeBounds() {
|
|
72
|
+
this.boundingBox = computeVertexBounds(this.vertices);
|
|
73
|
+
}
|
|
74
|
+
clone() {
|
|
75
|
+
const clonedMesh = new _Mesh(
|
|
76
|
+
this.name,
|
|
77
|
+
new Float32Array(this.vertices),
|
|
78
|
+
new Float32Array(this.normals),
|
|
79
|
+
this.material,
|
|
80
|
+
// shallow ref — call Material.clone() if independent material is needed
|
|
81
|
+
new Float32Array(this.texCoords),
|
|
82
|
+
this.indices ? this.indices instanceof Uint16Array ? new Uint16Array(this.indices) : new Uint32Array(this.indices) : null
|
|
83
|
+
);
|
|
84
|
+
clonedMesh.setMode(this.mode);
|
|
85
|
+
clonedMesh.isCollidable = this.isCollidable;
|
|
86
|
+
if (this.jointIndices)
|
|
87
|
+
clonedMesh.jointIndices = new Float32Array(this.jointIndices);
|
|
88
|
+
if (this.jointWeights)
|
|
89
|
+
clonedMesh.jointWeights = new Float32Array(this.jointWeights);
|
|
90
|
+
return clonedMesh;
|
|
91
|
+
}
|
|
92
|
+
/** Initialize GPU buffers safely */
|
|
93
|
+
initBuffer(webglCore) {
|
|
94
|
+
const gl = webglCore.getRenderingContext();
|
|
95
|
+
if (!gl) {
|
|
96
|
+
console.warn(
|
|
97
|
+
`[Mesh:${this.name}] GL context not available, skipping buffer init.`
|
|
98
|
+
);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (this.isInitialized) return;
|
|
102
|
+
this.buffers = new MeshBuffers();
|
|
103
|
+
this.buffers.init(gl, this);
|
|
104
|
+
this.isInitialized = true;
|
|
105
|
+
}
|
|
106
|
+
/** Dispose of GPU buffers safely */
|
|
107
|
+
dispose(webglCore) {
|
|
108
|
+
const gl = webglCore.getRenderingContext();
|
|
109
|
+
if (!gl || !this.buffers) return;
|
|
110
|
+
this.buffers.dispose(gl);
|
|
111
|
+
this.buffers = null;
|
|
112
|
+
this.isInitialized = false;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
var MeshBuffers = class {
|
|
116
|
+
vao = null;
|
|
117
|
+
vertexBuffer = null;
|
|
118
|
+
normalBuffer = null;
|
|
119
|
+
texCoordBuffer = null;
|
|
120
|
+
indexBuffer = null;
|
|
121
|
+
jointIndexBuffer = null;
|
|
122
|
+
jointWeightBuffer = null;
|
|
123
|
+
init(gl, mesh) {
|
|
124
|
+
if (!gl) throw new Error("Cannot init MeshBuffers: invalid GL context");
|
|
125
|
+
const isWebGL2 = gl instanceof WebGL2RenderingContext;
|
|
126
|
+
const gl2 = isWebGL2 ? gl : null;
|
|
127
|
+
if (gl2) {
|
|
128
|
+
this.vao = gl2.createVertexArray();
|
|
129
|
+
gl2.bindVertexArray(this.vao);
|
|
130
|
+
}
|
|
131
|
+
const program = mesh.material?.program;
|
|
132
|
+
const aPosition = program ? gl.getAttribLocation(program, "aPosition") : 0;
|
|
133
|
+
const aNormal = program ? gl.getAttribLocation(program, "aNormal") : 1;
|
|
134
|
+
const aTexCoord = program ? gl.getAttribLocation(program, "aTexCoord") : 2;
|
|
135
|
+
this.vertexBuffer = gl.createBuffer();
|
|
136
|
+
if (!this.vertexBuffer) throw new Error("Failed to create vertex buffer");
|
|
137
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
|
138
|
+
gl.bufferData(gl.ARRAY_BUFFER, mesh.vertices, gl.STATIC_DRAW);
|
|
139
|
+
if (aPosition !== -1) {
|
|
140
|
+
gl.enableVertexAttribArray(aPosition);
|
|
141
|
+
gl.vertexAttribPointer(aPosition, 3, gl.FLOAT, false, 0, 0);
|
|
142
|
+
}
|
|
143
|
+
this.normalBuffer = gl.createBuffer();
|
|
144
|
+
if (!this.normalBuffer) throw new Error("Failed to create normal buffer");
|
|
145
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
|
|
146
|
+
gl.bufferData(gl.ARRAY_BUFFER, mesh.normals, gl.STATIC_DRAW);
|
|
147
|
+
if (aNormal !== -1) {
|
|
148
|
+
gl.enableVertexAttribArray(aNormal);
|
|
149
|
+
gl.vertexAttribPointer(aNormal, 3, gl.FLOAT, false, 0, 0);
|
|
150
|
+
}
|
|
151
|
+
if (mesh.texCoords.length > 0) {
|
|
152
|
+
this.texCoordBuffer = gl.createBuffer();
|
|
153
|
+
if (!this.texCoordBuffer)
|
|
154
|
+
throw new Error("Failed to create texCoord buffer");
|
|
155
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer);
|
|
156
|
+
gl.bufferData(gl.ARRAY_BUFFER, mesh.texCoords, gl.STATIC_DRAW);
|
|
157
|
+
if (aTexCoord !== -1) {
|
|
158
|
+
gl.enableVertexAttribArray(aTexCoord);
|
|
159
|
+
gl.vertexAttribPointer(aTexCoord, 2, gl.FLOAT, false, 0, 0);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (mesh.indices && mesh.indices.length > 0) {
|
|
163
|
+
this.indexBuffer = gl.createBuffer();
|
|
164
|
+
if (!this.indexBuffer) throw new Error("Failed to create index buffer");
|
|
165
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
|
|
166
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, mesh.indices, gl.STATIC_DRAW);
|
|
167
|
+
}
|
|
168
|
+
const aJointIndices = program ? gl.getAttribLocation(program, "aJointIndices") : -1;
|
|
169
|
+
if (mesh.jointIndices && mesh.jointIndices.length > 0 && aJointIndices !== -1) {
|
|
170
|
+
this.jointIndexBuffer = gl.createBuffer();
|
|
171
|
+
if (!this.jointIndexBuffer)
|
|
172
|
+
throw new Error("Failed to create joint index buffer");
|
|
173
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.jointIndexBuffer);
|
|
174
|
+
gl.bufferData(gl.ARRAY_BUFFER, mesh.jointIndices, gl.STATIC_DRAW);
|
|
175
|
+
gl.enableVertexAttribArray(aJointIndices);
|
|
176
|
+
gl.vertexAttribPointer(aJointIndices, 4, gl.FLOAT, false, 0, 0);
|
|
177
|
+
}
|
|
178
|
+
const aJointWeights = program ? gl.getAttribLocation(program, "aJointWeights") : -1;
|
|
179
|
+
if (mesh.jointWeights && mesh.jointWeights.length > 0 && aJointWeights !== -1) {
|
|
180
|
+
this.jointWeightBuffer = gl.createBuffer();
|
|
181
|
+
if (!this.jointWeightBuffer)
|
|
182
|
+
throw new Error("Failed to create joint weight buffer");
|
|
183
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.jointWeightBuffer);
|
|
184
|
+
gl.bufferData(gl.ARRAY_BUFFER, mesh.jointWeights, gl.STATIC_DRAW);
|
|
185
|
+
gl.enableVertexAttribArray(aJointWeights);
|
|
186
|
+
gl.vertexAttribPointer(aJointWeights, 4, gl.FLOAT, false, 0, 0);
|
|
187
|
+
}
|
|
188
|
+
if (gl2) {
|
|
189
|
+
gl2.bindVertexArray(null);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
dispose(gl) {
|
|
193
|
+
if (!gl) {
|
|
194
|
+
console.warn("Cannot dispose MeshBuffers: invalid GL context");
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const isWebGL2 = gl instanceof WebGL2RenderingContext;
|
|
198
|
+
if (this.vertexBuffer) gl.deleteBuffer(this.vertexBuffer);
|
|
199
|
+
if (this.normalBuffer) gl.deleteBuffer(this.normalBuffer);
|
|
200
|
+
if (this.texCoordBuffer) gl.deleteBuffer(this.texCoordBuffer);
|
|
201
|
+
if (this.indexBuffer) gl.deleteBuffer(this.indexBuffer);
|
|
202
|
+
if (this.jointIndexBuffer) gl.deleteBuffer(this.jointIndexBuffer);
|
|
203
|
+
if (this.jointWeightBuffer) gl.deleteBuffer(this.jointWeightBuffer);
|
|
204
|
+
if (isWebGL2 && this.vao) {
|
|
205
|
+
gl.deleteVertexArray(this.vao);
|
|
206
|
+
}
|
|
207
|
+
this.vertexBuffer = this.normalBuffer = this.texCoordBuffer = this.indexBuffer = this.jointIndexBuffer = this.jointWeightBuffer = this.vao = null;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// src/Core/shaders/index.ts
|
|
212
|
+
var vsSource = (
|
|
213
|
+
/* glsl */
|
|
214
|
+
`
|
|
215
|
+
#define MAX_JOINTS 128
|
|
216
|
+
|
|
217
|
+
attribute vec3 aPosition;
|
|
218
|
+
attribute vec3 aNormal;
|
|
219
|
+
attribute vec2 aTexCoord;
|
|
220
|
+
|
|
221
|
+
// \u2500\u2500 Skinning attributes (vec4: up to 4 joint influences per vertex) \u2500\u2500
|
|
222
|
+
attribute vec4 aJointIndices;
|
|
223
|
+
attribute vec4 aJointWeights;
|
|
224
|
+
|
|
225
|
+
uniform mat4 uModel;
|
|
226
|
+
uniform mat4 uView;
|
|
227
|
+
uniform mat4 uProjection;
|
|
228
|
+
|
|
229
|
+
// \u2500\u2500 Skinning uniforms \u2500\u2500
|
|
230
|
+
uniform bool uUseSkinning;
|
|
231
|
+
uniform mat4 uJointMatrices[MAX_JOINTS];
|
|
232
|
+
|
|
233
|
+
varying vec3 vNormal;
|
|
234
|
+
varying vec3 vPosition;
|
|
235
|
+
varying vec2 vTexCoord;
|
|
236
|
+
|
|
237
|
+
void main() {
|
|
238
|
+
vTexCoord = aTexCoord;
|
|
239
|
+
|
|
240
|
+
vec4 pos = vec4(aPosition, 1.0);
|
|
241
|
+
vec3 norm = aNormal;
|
|
242
|
+
|
|
243
|
+
if (uUseSkinning) {
|
|
244
|
+
mat4 skinMat =
|
|
245
|
+
aJointWeights.x * uJointMatrices[int(aJointIndices.x)] +
|
|
246
|
+
aJointWeights.y * uJointMatrices[int(aJointIndices.y)] +
|
|
247
|
+
aJointWeights.z * uJointMatrices[int(aJointIndices.z)] +
|
|
248
|
+
aJointWeights.w * uJointMatrices[int(aJointIndices.w)];
|
|
249
|
+
pos = skinMat * pos;
|
|
250
|
+
norm = mat3(skinMat) * norm;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
vNormal = mat3(uModel) * norm;
|
|
254
|
+
|
|
255
|
+
vec4 worldPosition = uModel * pos;
|
|
256
|
+
vPosition = worldPosition.xyz;
|
|
257
|
+
gl_Position = uProjection * uView * worldPosition;
|
|
258
|
+
}
|
|
259
|
+
`
|
|
260
|
+
);
|
|
261
|
+
var fsSource = (
|
|
262
|
+
/* glsl */
|
|
263
|
+
`
|
|
264
|
+
precision mediump float;
|
|
265
|
+
|
|
266
|
+
#define MAX_LIGHTS 5
|
|
267
|
+
|
|
268
|
+
varying vec3 vNormal;
|
|
269
|
+
varying vec3 vPosition;
|
|
270
|
+
varying vec2 vTexCoord;
|
|
271
|
+
|
|
272
|
+
uniform vec3 uLightDirection[MAX_LIGHTS];
|
|
273
|
+
uniform vec3 uLightColor[MAX_LIGHTS];
|
|
274
|
+
uniform float uLightIntensity[MAX_LIGHTS];
|
|
275
|
+
uniform int uLightType[MAX_LIGHTS]; // 0=directional, 1=point, 2=ambient
|
|
276
|
+
uniform int uLightCount;
|
|
277
|
+
uniform vec3 uLightPosition[MAX_LIGHTS];
|
|
278
|
+
uniform float uLightConstant[MAX_LIGHTS];
|
|
279
|
+
uniform float uLightLinear[MAX_LIGHTS];
|
|
280
|
+
uniform float uLightQuadratic[MAX_LIGHTS];
|
|
281
|
+
|
|
282
|
+
uniform vec4 uColor;
|
|
283
|
+
uniform bool uUnlit;
|
|
284
|
+
uniform bool uUseTexture;
|
|
285
|
+
uniform sampler2D uTexture;
|
|
286
|
+
|
|
287
|
+
uniform vec3 uViewPosition;
|
|
288
|
+
uniform float uShininess;
|
|
289
|
+
uniform vec3 uSpecularColor;
|
|
290
|
+
|
|
291
|
+
void main() {
|
|
292
|
+
vec4 baseColor = uColor;
|
|
293
|
+
if (uUseTexture) {
|
|
294
|
+
baseColor *= texture2D(uTexture, vTexCoord);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (uUnlit) {
|
|
298
|
+
gl_FragColor = baseColor;
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
vec3 norm = normalize(vNormal);
|
|
303
|
+
vec3 totalDiffuse = vec3(0.0);
|
|
304
|
+
vec3 totalAmbient = vec3(0.0);
|
|
305
|
+
vec3 totalSpecular = vec3(0.0);
|
|
306
|
+
|
|
307
|
+
vec3 viewDir = normalize(uViewPosition - vPosition);
|
|
308
|
+
|
|
309
|
+
for (int i = 0; i < MAX_LIGHTS; i++) {
|
|
310
|
+
if (i >= uLightCount) break;
|
|
311
|
+
|
|
312
|
+
if (uLightType[i] == 2) {
|
|
313
|
+
totalAmbient += uLightColor[i] * uLightIntensity[i];
|
|
314
|
+
} else {
|
|
315
|
+
vec3 lightDir;
|
|
316
|
+
float attenuation = 1.0;
|
|
317
|
+
|
|
318
|
+
if (uLightType[i] == 0) {
|
|
319
|
+
// Directional light
|
|
320
|
+
lightDir = normalize(-uLightDirection[i]);
|
|
321
|
+
} else {
|
|
322
|
+
// Point light
|
|
323
|
+
vec3 lightVec = uLightPosition[i] - vPosition;
|
|
324
|
+
float distance = length(lightVec);
|
|
325
|
+
lightDir = normalize(lightVec);
|
|
326
|
+
attenuation = 1.0 / (
|
|
327
|
+
uLightConstant[i] +
|
|
328
|
+
uLightLinear[i] * distance +
|
|
329
|
+
uLightQuadratic[i] * distance * distance
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Diffuse
|
|
334
|
+
float diff = max(dot(norm, lightDir), 0.0);
|
|
335
|
+
totalDiffuse += diff * uLightColor[i] * uLightIntensity[i] * attenuation;
|
|
336
|
+
|
|
337
|
+
// Specular (Blinn-Phong) \u2014 softer, more natural highlights
|
|
338
|
+
vec3 halfDir = normalize(lightDir + viewDir);
|
|
339
|
+
float spec = pow(max(dot(norm, halfDir), 0.0), uShininess);
|
|
340
|
+
totalSpecular += spec * uSpecularColor * uLightIntensity[i] * attenuation;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
vec3 lightingResult =
|
|
345
|
+
totalAmbient * baseColor.rgb +
|
|
346
|
+
totalDiffuse * baseColor.rgb +
|
|
347
|
+
totalSpecular;
|
|
348
|
+
|
|
349
|
+
gl_FragColor = vec4(lightingResult, baseColor.a);
|
|
350
|
+
}
|
|
351
|
+
`
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
// src/Core/classes/WebGLCore.ts
|
|
355
|
+
var WebGLCore = class {
|
|
356
|
+
gl;
|
|
357
|
+
program;
|
|
358
|
+
canvas;
|
|
359
|
+
isWebGL2;
|
|
360
|
+
/**
|
|
361
|
+
* Initialise the WebGL rendering core.
|
|
362
|
+
*
|
|
363
|
+
* @param canvasOrId - An `HTMLCanvasElement` or the DOM `id` of one.
|
|
364
|
+
* Defaults to `'glcanvas'`.
|
|
365
|
+
*/
|
|
366
|
+
constructor(canvasOrId = "glcanvas") {
|
|
367
|
+
const canvas = typeof canvasOrId === "string" ? document.getElementById(canvasOrId) : canvasOrId;
|
|
368
|
+
if (!canvas) throw new Error("Canvas not found");
|
|
369
|
+
this.canvas = canvas;
|
|
370
|
+
canvas.width = window.innerWidth;
|
|
371
|
+
canvas.height = window.innerHeight;
|
|
372
|
+
this.gl = canvas.getContext("webgl2") ?? canvas.getContext("webgl");
|
|
373
|
+
if (!this.gl) throw new Error("WebGL not supported in this browser!");
|
|
374
|
+
this.isWebGL2 = this.gl instanceof WebGL2RenderingContext;
|
|
375
|
+
this.gl.viewport(0, 0, canvas.width, canvas.height);
|
|
376
|
+
const program = this.createProgram(this.gl);
|
|
377
|
+
if (!program) throw new Error("Failed to create program");
|
|
378
|
+
this.program = program;
|
|
379
|
+
}
|
|
380
|
+
getProgram() {
|
|
381
|
+
return this.program;
|
|
382
|
+
}
|
|
383
|
+
getRenderingContext() {
|
|
384
|
+
return this.gl;
|
|
385
|
+
}
|
|
386
|
+
createProgram(gl) {
|
|
387
|
+
const vertexShader = this.compileShader(gl, gl.VERTEX_SHADER, vsSource);
|
|
388
|
+
const fragmentShader = this.compileShader(gl, gl.FRAGMENT_SHADER, fsSource);
|
|
389
|
+
if (!vertexShader || !fragmentShader)
|
|
390
|
+
throw new Error("Failed to create shaders");
|
|
391
|
+
const program = gl.createProgram();
|
|
392
|
+
if (!program) return null;
|
|
393
|
+
gl.attachShader(program, vertexShader);
|
|
394
|
+
gl.attachShader(program, fragmentShader);
|
|
395
|
+
gl.linkProgram(program);
|
|
396
|
+
gl.deleteShader(vertexShader);
|
|
397
|
+
gl.deleteShader(fragmentShader);
|
|
398
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
399
|
+
console.error(gl.getProgramInfoLog(program));
|
|
400
|
+
throw new Error(gl.getProgramInfoLog(program) || "Program link error");
|
|
401
|
+
}
|
|
402
|
+
return program;
|
|
403
|
+
}
|
|
404
|
+
compileShader(gl, type, source) {
|
|
405
|
+
const shader = gl.createShader(type);
|
|
406
|
+
if (!shader) return null;
|
|
407
|
+
gl.shaderSource(shader, source);
|
|
408
|
+
gl.compileShader(shader);
|
|
409
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
410
|
+
console.error(gl.getShaderInfoLog(shader));
|
|
411
|
+
gl.deleteShader(shader);
|
|
412
|
+
throw new Error(gl.getShaderInfoLog(shader) || "Shader compile error");
|
|
413
|
+
}
|
|
414
|
+
return shader;
|
|
415
|
+
}
|
|
416
|
+
resize(width, height) {
|
|
417
|
+
this.canvas.width = width;
|
|
418
|
+
this.canvas.height = height;
|
|
419
|
+
this.gl.viewport(0, 0, width, height);
|
|
420
|
+
}
|
|
421
|
+
dispose() {
|
|
422
|
+
const { gl, program } = this;
|
|
423
|
+
if (program) gl.deleteProgram(program);
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
// src/Core/classes/Skeleton.ts
|
|
428
|
+
import { mat4, quat, vec3 } from "gl-matrix";
|
|
429
|
+
var MAX_JOINTS = 128;
|
|
430
|
+
var Skeleton = class _Skeleton {
|
|
431
|
+
/** Ordered joint list (parent always precedes child). */
|
|
432
|
+
joints;
|
|
433
|
+
/**
|
|
434
|
+
* Flat `Float32Array` of `joints.length` column-major `mat4` values.
|
|
435
|
+
* Updated by {@link computeJointMatrices} — upload directly to
|
|
436
|
+
* `uJointMatrices[0]`.
|
|
437
|
+
*/
|
|
438
|
+
jointMatrices;
|
|
439
|
+
/**
|
|
440
|
+
* Global transform of the non-joint ancestor nodes above the skeleton
|
|
441
|
+
* root. In many exported models the armature sits under a scene-root
|
|
442
|
+
* node that carries scale / rotation (e.g. cm → m conversion). This
|
|
443
|
+
* transform is pre-multiplied into root-joint globals so that
|
|
444
|
+
* `global * inverseBindMatrix` evaluates to identity in the bind pose.
|
|
445
|
+
*/
|
|
446
|
+
rootTransform;
|
|
447
|
+
constructor(joints, rootTransform) {
|
|
448
|
+
if (joints.length > MAX_JOINTS) {
|
|
449
|
+
console.warn(
|
|
450
|
+
`[Skeleton] ${joints.length} joints exceeds MAX_JOINTS (${MAX_JOINTS}). Extra joints will be ignored by the shader.`
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
this.joints = joints;
|
|
454
|
+
this.jointMatrices = new Float32Array(joints.length * 16);
|
|
455
|
+
this.rootTransform = rootTransform ?? mat4.create();
|
|
456
|
+
}
|
|
457
|
+
/** Number of joints in the skeleton. */
|
|
458
|
+
get jointCount() {
|
|
459
|
+
return this.joints.length;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Walk the joint hierarchy, composing global transforms, then multiply by
|
|
463
|
+
* each joint's inverse-bind matrix. The result is stored in
|
|
464
|
+
* {@link jointMatrices} ready for shader upload.
|
|
465
|
+
*
|
|
466
|
+
* @param animatedPoses - Optional per-joint local transforms from an
|
|
467
|
+
* {@link AnimationClip}. Map keys are **joint array indices** (not glTF
|
|
468
|
+
* node indices).
|
|
469
|
+
*/
|
|
470
|
+
computeJointMatrices(animatedPoses) {
|
|
471
|
+
const globals = new Array(this.joints.length);
|
|
472
|
+
const local = mat4.create();
|
|
473
|
+
for (let i = 0; i < this.joints.length; i++) {
|
|
474
|
+
const joint = this.joints[i];
|
|
475
|
+
const pose = animatedPoses?.get(i);
|
|
476
|
+
const t = pose?.t ?? joint.localTranslation;
|
|
477
|
+
const r = pose?.r ?? joint.localRotation;
|
|
478
|
+
const s = pose?.s ?? joint.localScale;
|
|
479
|
+
mat4.fromRotationTranslationScale(local, r, t, s);
|
|
480
|
+
if (joint.parentIndex >= 0) {
|
|
481
|
+
globals[i] = mat4.create();
|
|
482
|
+
mat4.multiply(globals[i], globals[joint.parentIndex], local);
|
|
483
|
+
} else {
|
|
484
|
+
globals[i] = mat4.create();
|
|
485
|
+
mat4.multiply(globals[i], this.rootTransform, local);
|
|
486
|
+
}
|
|
487
|
+
const final = mat4.create();
|
|
488
|
+
mat4.multiply(final, globals[i], joint.inverseBindMatrix);
|
|
489
|
+
this.jointMatrices.set(final, i * 16);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Create a deep clone of this skeleton (useful when instancing models).
|
|
494
|
+
*/
|
|
495
|
+
clone() {
|
|
496
|
+
const clonedJoints = this.joints.map((j) => ({
|
|
497
|
+
name: j.name,
|
|
498
|
+
nodeIndex: j.nodeIndex,
|
|
499
|
+
parentIndex: j.parentIndex,
|
|
500
|
+
localTranslation: vec3.clone(j.localTranslation),
|
|
501
|
+
localRotation: quat.clone(j.localRotation),
|
|
502
|
+
localScale: vec3.clone(j.localScale),
|
|
503
|
+
inverseBindMatrix: mat4.clone(j.inverseBindMatrix)
|
|
504
|
+
}));
|
|
505
|
+
return new _Skeleton(clonedJoints, mat4.clone(this.rootTransform));
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
// src/Core/classes/AnimationClip.ts
|
|
510
|
+
import { quat as quat2, vec3 as vec32 } from "gl-matrix";
|
|
511
|
+
var AnimationClip = class _AnimationClip {
|
|
512
|
+
/** Human-readable name (e.g. "Walk", "Idle"). */
|
|
513
|
+
name;
|
|
514
|
+
/** All channels in this clip. */
|
|
515
|
+
channels;
|
|
516
|
+
/** Total duration in seconds (derived from the maximum keyframe time). */
|
|
517
|
+
duration;
|
|
518
|
+
/** Current playback time in seconds. */
|
|
519
|
+
currentTime = 0;
|
|
520
|
+
/** Whether the clip loops when it reaches the end. */
|
|
521
|
+
loop = true;
|
|
522
|
+
/** Playback speed multiplier (1 = normal, 2 = double, −1 = reverse). */
|
|
523
|
+
speed = 1;
|
|
524
|
+
/** Whether the clip is currently advancing. */
|
|
525
|
+
playing = false;
|
|
526
|
+
constructor(name, channels, duration) {
|
|
527
|
+
this.name = name;
|
|
528
|
+
this.channels = channels;
|
|
529
|
+
this.duration = duration;
|
|
530
|
+
}
|
|
531
|
+
/** Start / resume playback. */
|
|
532
|
+
play() {
|
|
533
|
+
this.playing = true;
|
|
534
|
+
}
|
|
535
|
+
/** Pause playback (keeps current time). */
|
|
536
|
+
pause() {
|
|
537
|
+
this.playing = false;
|
|
538
|
+
}
|
|
539
|
+
/** Stop and rewind to the beginning. */
|
|
540
|
+
stop() {
|
|
541
|
+
this.playing = false;
|
|
542
|
+
this.currentTime = 0;
|
|
543
|
+
}
|
|
544
|
+
/** Rewind to the beginning without changing play state. */
|
|
545
|
+
reset() {
|
|
546
|
+
this.currentTime = 0;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Advance the playhead by `deltaTime` seconds (respects {@link speed}).
|
|
550
|
+
* Automatically loops or clamps depending on {@link loop}.
|
|
551
|
+
*/
|
|
552
|
+
update(deltaTime) {
|
|
553
|
+
if (!this.playing || this.duration <= 0) return;
|
|
554
|
+
this.currentTime += deltaTime * this.speed;
|
|
555
|
+
if (this.loop) {
|
|
556
|
+
this.currentTime = (this.currentTime % this.duration + this.duration) % this.duration;
|
|
557
|
+
} else {
|
|
558
|
+
if (this.currentTime >= this.duration) {
|
|
559
|
+
this.currentTime = this.duration;
|
|
560
|
+
this.playing = false;
|
|
561
|
+
} else if (this.currentTime < 0) {
|
|
562
|
+
this.currentTime = 0;
|
|
563
|
+
this.playing = false;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Sample every channel at the current playhead and return a map of
|
|
569
|
+
* joint-index → local pose.
|
|
570
|
+
*/
|
|
571
|
+
sample() {
|
|
572
|
+
const result = /* @__PURE__ */ new Map();
|
|
573
|
+
for (const channel of this.channels) {
|
|
574
|
+
if (!result.has(channel.jointIndex)) {
|
|
575
|
+
result.set(channel.jointIndex, {});
|
|
576
|
+
}
|
|
577
|
+
const entry = result.get(channel.jointIndex);
|
|
578
|
+
const value = sampleChannel(channel, this.currentTime);
|
|
579
|
+
switch (channel.path) {
|
|
580
|
+
case "translation":
|
|
581
|
+
entry.t = value;
|
|
582
|
+
break;
|
|
583
|
+
case "rotation":
|
|
584
|
+
entry.r = value;
|
|
585
|
+
break;
|
|
586
|
+
case "scale":
|
|
587
|
+
entry.s = value;
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return result;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Create an independent copy of this clip (shares the underlying sampler
|
|
595
|
+
* data but has its own playback state).
|
|
596
|
+
*/
|
|
597
|
+
clone() {
|
|
598
|
+
const copy = new _AnimationClip(this.name, this.channels, this.duration);
|
|
599
|
+
copy.loop = this.loop;
|
|
600
|
+
copy.speed = this.speed;
|
|
601
|
+
return copy;
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
function readValue(values, index, numComponents) {
|
|
605
|
+
const off = index * numComponents;
|
|
606
|
+
if (numComponents === 4) {
|
|
607
|
+
return quat2.fromValues(
|
|
608
|
+
values[off],
|
|
609
|
+
values[off + 1],
|
|
610
|
+
values[off + 2],
|
|
611
|
+
values[off + 3]
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
return vec32.fromValues(values[off], values[off + 1], values[off + 2]);
|
|
615
|
+
}
|
|
616
|
+
function sampleChannel(channel, time) {
|
|
617
|
+
const { sampler, path } = channel;
|
|
618
|
+
const { times, values, interpolation } = sampler;
|
|
619
|
+
const numComponents = path === "rotation" ? 4 : 3;
|
|
620
|
+
if (times.length === 0) {
|
|
621
|
+
return numComponents === 4 ? quat2.create() : vec32.create();
|
|
622
|
+
}
|
|
623
|
+
if (time <= times[0]) {
|
|
624
|
+
return readValue(values, 0, numComponents);
|
|
625
|
+
}
|
|
626
|
+
if (time >= times[times.length - 1]) {
|
|
627
|
+
return readValue(values, times.length - 1, numComponents);
|
|
628
|
+
}
|
|
629
|
+
let lo = 0;
|
|
630
|
+
let hi = times.length - 1;
|
|
631
|
+
while (lo < hi - 1) {
|
|
632
|
+
const mid = lo + hi >>> 1;
|
|
633
|
+
if (times[mid] <= time) {
|
|
634
|
+
lo = mid;
|
|
635
|
+
} else {
|
|
636
|
+
hi = mid;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
const t0 = times[lo];
|
|
640
|
+
const t1 = times[hi];
|
|
641
|
+
const factor = (time - t0) / (t1 - t0);
|
|
642
|
+
const v0 = readValue(values, lo, numComponents);
|
|
643
|
+
const v1 = readValue(values, hi, numComponents);
|
|
644
|
+
if (interpolation === "STEP") {
|
|
645
|
+
return v0;
|
|
646
|
+
}
|
|
647
|
+
if (path === "rotation") {
|
|
648
|
+
const out2 = quat2.create();
|
|
649
|
+
quat2.slerp(out2, v0, v1, factor);
|
|
650
|
+
return out2;
|
|
651
|
+
}
|
|
652
|
+
const out = vec32.create();
|
|
653
|
+
vec32.lerp(out, v0, v1, factor);
|
|
654
|
+
return out;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// src/Core/utils/create-wire-box.ts
|
|
658
|
+
function createWireBox(min, max, material) {
|
|
659
|
+
const [minX, minY, minZ] = min;
|
|
660
|
+
const [maxX, maxY, maxZ] = max;
|
|
661
|
+
const vertices = [
|
|
662
|
+
minX,
|
|
663
|
+
minY,
|
|
664
|
+
minZ,
|
|
665
|
+
maxX,
|
|
666
|
+
minY,
|
|
667
|
+
minZ,
|
|
668
|
+
maxX,
|
|
669
|
+
maxY,
|
|
670
|
+
minZ,
|
|
671
|
+
minX,
|
|
672
|
+
maxY,
|
|
673
|
+
minZ,
|
|
674
|
+
minX,
|
|
675
|
+
minY,
|
|
676
|
+
maxZ,
|
|
677
|
+
maxX,
|
|
678
|
+
minY,
|
|
679
|
+
maxZ,
|
|
680
|
+
maxX,
|
|
681
|
+
maxY,
|
|
682
|
+
maxZ,
|
|
683
|
+
minX,
|
|
684
|
+
maxY,
|
|
685
|
+
maxZ
|
|
686
|
+
];
|
|
687
|
+
const indices = [
|
|
688
|
+
0,
|
|
689
|
+
1,
|
|
690
|
+
1,
|
|
691
|
+
2,
|
|
692
|
+
2,
|
|
693
|
+
3,
|
|
694
|
+
3,
|
|
695
|
+
0,
|
|
696
|
+
// bottom face
|
|
697
|
+
4,
|
|
698
|
+
5,
|
|
699
|
+
5,
|
|
700
|
+
6,
|
|
701
|
+
6,
|
|
702
|
+
7,
|
|
703
|
+
7,
|
|
704
|
+
4,
|
|
705
|
+
// top face
|
|
706
|
+
0,
|
|
707
|
+
4,
|
|
708
|
+
1,
|
|
709
|
+
5,
|
|
710
|
+
2,
|
|
711
|
+
6,
|
|
712
|
+
3,
|
|
713
|
+
7
|
|
714
|
+
// vertical edges
|
|
715
|
+
];
|
|
716
|
+
const lineVertices = [];
|
|
717
|
+
for (let i = 0; i < indices.length; i++) {
|
|
718
|
+
const idx = indices[i];
|
|
719
|
+
lineVertices.push(
|
|
720
|
+
vertices[idx * 3],
|
|
721
|
+
vertices[idx * 3 + 1],
|
|
722
|
+
vertices[idx * 3 + 2]
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
const normals = new Float32Array(lineVertices.length);
|
|
726
|
+
const mesh = new Mesh(
|
|
727
|
+
"hitbox",
|
|
728
|
+
new Float32Array(lineVertices),
|
|
729
|
+
normals,
|
|
730
|
+
material,
|
|
731
|
+
new Float32Array(),
|
|
732
|
+
null
|
|
733
|
+
);
|
|
734
|
+
mesh.setMode(GL_LINES);
|
|
735
|
+
return mesh;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// src/Core/classes/Renderer.ts
|
|
739
|
+
var computeModelMatrix = (model) => {
|
|
740
|
+
const m = mat42.create();
|
|
741
|
+
mat42.translate(m, m, model.translation);
|
|
742
|
+
mat42.rotateX(m, m, model.rotation[0]);
|
|
743
|
+
mat42.rotateY(m, m, model.rotation[1]);
|
|
744
|
+
mat42.rotateZ(m, m, model.rotation[2]);
|
|
745
|
+
mat42.scale(m, m, model.scale);
|
|
746
|
+
return m;
|
|
747
|
+
};
|
|
748
|
+
var Renderer = class {
|
|
749
|
+
constructor(webglCore, viewport) {
|
|
750
|
+
this.webglCore = webglCore;
|
|
751
|
+
this.viewport = viewport;
|
|
752
|
+
}
|
|
753
|
+
webglCore;
|
|
754
|
+
viewport;
|
|
755
|
+
/** Set to `true` to render wireframe bounding boxes for debugging. */
|
|
756
|
+
debug = false;
|
|
757
|
+
// Cached debug materials to avoid per-frame GPU allocations
|
|
758
|
+
_modelHitboxMaterial = null;
|
|
759
|
+
_meshHitboxMaterial = null;
|
|
760
|
+
/** Lazily-created material for model-level hitboxes (red). */
|
|
761
|
+
get modelHitboxMaterial() {
|
|
762
|
+
if (!this._modelHitboxMaterial) {
|
|
763
|
+
this._modelHitboxMaterial = new Material(this.webglCore);
|
|
764
|
+
this._modelHitboxMaterial.setColorHex("#ff0404ff");
|
|
765
|
+
this._modelHitboxMaterial.unlit = true;
|
|
766
|
+
}
|
|
767
|
+
return this._modelHitboxMaterial;
|
|
768
|
+
}
|
|
769
|
+
/** Lazily-created material for mesh-level hitboxes (green). */
|
|
770
|
+
get meshHitboxMaterial() {
|
|
771
|
+
if (!this._meshHitboxMaterial) {
|
|
772
|
+
this._meshHitboxMaterial = new Material(this.webglCore);
|
|
773
|
+
this._meshHitboxMaterial.setColorHex("#04ff0cff");
|
|
774
|
+
this._meshHitboxMaterial.unlit = true;
|
|
775
|
+
}
|
|
776
|
+
return this._meshHitboxMaterial;
|
|
777
|
+
}
|
|
778
|
+
clearFrame(color = [0.2, 0.5, 0.95, 1]) {
|
|
779
|
+
const gl = this.webglCore.getRenderingContext();
|
|
780
|
+
if (!gl) return;
|
|
781
|
+
gl.clearColor(...color);
|
|
782
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
|
783
|
+
gl.enable(gl.DEPTH_TEST);
|
|
784
|
+
}
|
|
785
|
+
render(scene) {
|
|
786
|
+
const gl = this.webglCore.getRenderingContext();
|
|
787
|
+
if (!gl) return;
|
|
788
|
+
const models = scene.getModels();
|
|
789
|
+
const lights = scene.getLights();
|
|
790
|
+
const camera = this.viewport.camera;
|
|
791
|
+
const viewPosition = camera.getComputedPosition();
|
|
792
|
+
this.clearFrame();
|
|
793
|
+
const viewMatrix = camera.getViewMatrix();
|
|
794
|
+
const projMatrix = camera.getProjectionMatrix();
|
|
795
|
+
const activeProgram = null;
|
|
796
|
+
for (const model of models) {
|
|
797
|
+
this.drawModel(
|
|
798
|
+
gl,
|
|
799
|
+
model,
|
|
800
|
+
lights,
|
|
801
|
+
viewPosition,
|
|
802
|
+
viewMatrix,
|
|
803
|
+
projMatrix,
|
|
804
|
+
activeProgram
|
|
805
|
+
);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
drawModel(gl, model, lights, viewPosition, viewMatrix, projMatrix, activeProgram) {
|
|
809
|
+
const modelMatrix = computeModelMatrix(model);
|
|
810
|
+
const hasSkeleton = model.skeleton !== null;
|
|
811
|
+
for (const mesh of model.meshes) {
|
|
812
|
+
activeProgram = this.drawMesh(
|
|
813
|
+
mesh,
|
|
814
|
+
lights,
|
|
815
|
+
viewPosition,
|
|
816
|
+
modelMatrix,
|
|
817
|
+
viewMatrix,
|
|
818
|
+
projMatrix,
|
|
819
|
+
activeProgram,
|
|
820
|
+
hasSkeleton && mesh.isSkinned ? model : null
|
|
821
|
+
);
|
|
822
|
+
if (this.debug) {
|
|
823
|
+
this.drawHitBoxForMesh(
|
|
824
|
+
mesh,
|
|
825
|
+
modelMatrix,
|
|
826
|
+
viewMatrix,
|
|
827
|
+
projMatrix,
|
|
828
|
+
activeProgram
|
|
829
|
+
);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
if (this.debug) {
|
|
833
|
+
this.drawHitBox(model, modelMatrix, activeProgram);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
drawMesh(mesh, lights, viewPosition, modelMatrix, viewMatrix, projMatrix, activeProgram, skinnedModel = null) {
|
|
837
|
+
const gl = this.webglCore.getRenderingContext();
|
|
838
|
+
if (!gl) return activeProgram;
|
|
839
|
+
if (!mesh.material) return activeProgram;
|
|
840
|
+
mesh.initBuffer(this.webglCore);
|
|
841
|
+
const { program, uniformLocations } = mesh.material;
|
|
842
|
+
if (program !== activeProgram) {
|
|
843
|
+
gl.useProgram(program);
|
|
844
|
+
activeProgram = program;
|
|
845
|
+
if (uniformLocations["uView"])
|
|
846
|
+
gl.uniformMatrix4fv(uniformLocations["uView"], false, viewMatrix);
|
|
847
|
+
if (uniformLocations["uProjection"])
|
|
848
|
+
gl.uniformMatrix4fv(uniformLocations["uProjection"], false, projMatrix);
|
|
849
|
+
}
|
|
850
|
+
const isWebGL2 = gl instanceof WebGL2RenderingContext;
|
|
851
|
+
if (isWebGL2 && mesh.buffers?.vao) {
|
|
852
|
+
gl.bindVertexArray(
|
|
853
|
+
mesh.buffers.vao
|
|
854
|
+
);
|
|
855
|
+
} else {
|
|
856
|
+
const aPosition = mesh.material.attribLocations["aPosition"];
|
|
857
|
+
if (aPosition !== -1 && mesh.buffers?.vertexBuffer) {
|
|
858
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, mesh.buffers.vertexBuffer);
|
|
859
|
+
gl.enableVertexAttribArray(aPosition);
|
|
860
|
+
gl.vertexAttribPointer(aPosition, 3, gl.FLOAT, false, 0, 0);
|
|
861
|
+
}
|
|
862
|
+
const aNormal = mesh.material.attribLocations["aNormal"];
|
|
863
|
+
if (aNormal !== -1 && mesh.buffers?.normalBuffer) {
|
|
864
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, mesh.buffers.normalBuffer);
|
|
865
|
+
gl.enableVertexAttribArray(aNormal);
|
|
866
|
+
gl.vertexAttribPointer(aNormal, 3, gl.FLOAT, false, 0, 0);
|
|
867
|
+
}
|
|
868
|
+
const aTexCoord = mesh.material.attribLocations["aTexCoord"];
|
|
869
|
+
if (aTexCoord !== -1 && mesh.buffers?.texCoordBuffer) {
|
|
870
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, mesh.buffers.texCoordBuffer);
|
|
871
|
+
gl.enableVertexAttribArray(aTexCoord);
|
|
872
|
+
gl.vertexAttribPointer(aTexCoord, 2, gl.FLOAT, false, 0, 0);
|
|
873
|
+
}
|
|
874
|
+
const aJointIndices = mesh.material.attribLocations["aJointIndices"];
|
|
875
|
+
if (aJointIndices !== -1 && mesh.buffers?.jointIndexBuffer) {
|
|
876
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, mesh.buffers.jointIndexBuffer);
|
|
877
|
+
gl.enableVertexAttribArray(aJointIndices);
|
|
878
|
+
gl.vertexAttribPointer(aJointIndices, 4, gl.FLOAT, false, 0, 0);
|
|
879
|
+
}
|
|
880
|
+
const aJointWeights = mesh.material.attribLocations["aJointWeights"];
|
|
881
|
+
if (aJointWeights !== -1 && mesh.buffers?.jointWeightBuffer) {
|
|
882
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, mesh.buffers.jointWeightBuffer);
|
|
883
|
+
gl.enableVertexAttribArray(aJointWeights);
|
|
884
|
+
gl.vertexAttribPointer(aJointWeights, 4, gl.FLOAT, false, 0, 0);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
mesh.material.apply(gl, lights, viewPosition);
|
|
888
|
+
const uUseSkinning = uniformLocations["uUseSkinning"];
|
|
889
|
+
const skeleton = skinnedModel?.skeleton ?? null;
|
|
890
|
+
if (skeleton && mesh.isSkinned) {
|
|
891
|
+
if (uUseSkinning) gl.uniform1i(uUseSkinning, 1);
|
|
892
|
+
const uJointMatrices = uniformLocations["uJointMatrices[0]"];
|
|
893
|
+
if (uJointMatrices) {
|
|
894
|
+
gl.uniformMatrix4fv(uJointMatrices, false, skeleton.jointMatrices);
|
|
895
|
+
}
|
|
896
|
+
} else {
|
|
897
|
+
if (uUseSkinning) gl.uniform1i(uUseSkinning, 0);
|
|
898
|
+
}
|
|
899
|
+
const uModel = mesh.material.uniformLocations["uModel"];
|
|
900
|
+
if (uModel) gl.uniformMatrix4fv(uModel, false, modelMatrix);
|
|
901
|
+
if (mesh.buffers?.indexBuffer && mesh.indices) {
|
|
902
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, mesh.buffers.indexBuffer);
|
|
903
|
+
const type = mesh.indices instanceof Uint32Array ? gl.UNSIGNED_INT : gl.UNSIGNED_SHORT;
|
|
904
|
+
gl.drawElements(mesh.mode, mesh.indices.length, type, 0);
|
|
905
|
+
} else {
|
|
906
|
+
gl.drawArrays(mesh.mode, 0, mesh.vertices.length / 3);
|
|
907
|
+
}
|
|
908
|
+
if (isWebGL2 && mesh.buffers?.vao) {
|
|
909
|
+
gl.bindVertexArray(null);
|
|
910
|
+
} else {
|
|
911
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
912
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
|
|
913
|
+
}
|
|
914
|
+
return activeProgram;
|
|
915
|
+
}
|
|
916
|
+
drawHitBox(model, modelMatrix, activeProgram) {
|
|
917
|
+
const hitboxMesh = createWireBox(
|
|
918
|
+
model.boundingBox.min,
|
|
919
|
+
model.boundingBox.max,
|
|
920
|
+
this.modelHitboxMaterial
|
|
921
|
+
);
|
|
922
|
+
hitboxMesh.isCollidable = false;
|
|
923
|
+
const camera = this.viewport.camera;
|
|
924
|
+
const viewMatrix = camera.getViewMatrix();
|
|
925
|
+
const projMatrix = camera.getProjectionMatrix();
|
|
926
|
+
const identity = mat42.create();
|
|
927
|
+
this.drawMesh(
|
|
928
|
+
hitboxMesh,
|
|
929
|
+
[],
|
|
930
|
+
camera.position,
|
|
931
|
+
identity,
|
|
932
|
+
viewMatrix,
|
|
933
|
+
projMatrix,
|
|
934
|
+
activeProgram
|
|
935
|
+
);
|
|
936
|
+
}
|
|
937
|
+
drawHitBoxForMesh(mesh, modelMatrix, viewMatrix, projMatrix, activeProgram) {
|
|
938
|
+
const gl = this.webglCore.getRenderingContext();
|
|
939
|
+
if (!gl || !mesh.boundingBox) return;
|
|
940
|
+
const { min, max } = mesh.boundingBox;
|
|
941
|
+
const wireBox = createWireBox(min, max, this.meshHitboxMaterial);
|
|
942
|
+
wireBox.isCollidable = false;
|
|
943
|
+
const camera = this.viewport.camera;
|
|
944
|
+
this.drawMesh(
|
|
945
|
+
wireBox,
|
|
946
|
+
[],
|
|
947
|
+
camera.position,
|
|
948
|
+
modelMatrix,
|
|
949
|
+
viewMatrix,
|
|
950
|
+
projMatrix,
|
|
951
|
+
activeProgram
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
};
|
|
955
|
+
|
|
956
|
+
export {
|
|
957
|
+
GL_LINES,
|
|
958
|
+
GL_TRIANGLES,
|
|
959
|
+
INITIAL_BOUNDING_BOX,
|
|
960
|
+
Mesh,
|
|
961
|
+
MeshBuffers,
|
|
962
|
+
WebGLCore,
|
|
963
|
+
Renderer,
|
|
964
|
+
MAX_JOINTS,
|
|
965
|
+
Skeleton,
|
|
966
|
+
AnimationClip
|
|
967
|
+
};
|
|
968
|
+
//# sourceMappingURL=chunk-QCQVJCSR.js.map
|