Skip to content

Commit cb01800

Browse files
committed
Add sprite hud code.
1 parent d2cec67 commit cb01800

File tree

5 files changed

+61
-13
lines changed

5 files changed

+61
-13
lines changed

build/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/index.js.map

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/GLEngine.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ export class GLEngine extends Disposable {
128128
// enable depth
129129
this.gl.enable(GL.DEPTH_TEST);
130130
this.gl.depthFunc(GL.LESS);
131+
this.gl.clearDepth(1.0);
132+
131133
// enable blend
132134
this.gl.enable(GL.BLEND);
133135
this.gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
@@ -220,7 +222,8 @@ export class GLEngine extends Disposable {
220222
...Matrix.create().translate(-2, -1, 3).rotateX(-Math.PI / 2).scale(1, 1, 1).getMatrix(),
221223
...Matrix.create().translate(2, -1, 3).rotateX(-Math.PI / 2).scale(1, 1, 1).getMatrix(),
222224

223-
225+
// sprite
226+
...Matrix.create().translate(0, 0, 0).getMatrix(),
224227
]),
225228
0,
226229
GL.DYNAMIC_DRAW,
@@ -233,6 +236,34 @@ export class GLEngine extends Disposable {
233236
this.camera.refresh();
234237
}
235238

239+
syncHud() {
240+
this.attributeBuffers.bindBuffer(GL.ARRAY_BUFFER, this.attributeBuffers.getAttributeBuffer(TRANSFORM_LOC));
241+
242+
const cameraPosition = [
243+
-this.camera.cameraMatrix[12],
244+
-this.camera.cameraMatrix[13],
245+
-this.camera.cameraMatrix[14]
246+
];
247+
248+
// Create a copy of camTurnMatrix and invert it
249+
const invertedCamTurnMatrix = Matrix.create().copy(this.camera.camTurnMatrix).invert().getMatrix();
250+
const invertedCamTiltMatrix = Matrix.create().copy(this.camera.camTiltMatrix).invert().getMatrix();
251+
252+
this.attributeBuffers.bufferSubData(
253+
GL.ARRAY_BUFFER,
254+
Float32Array.from([
255+
// sprite
256+
...Matrix.create()
257+
.translate(cameraPosition[0], cameraPosition[1], cameraPosition[2])
258+
.multiply(invertedCamTurnMatrix)
259+
.multiply(invertedCamTiltMatrix)
260+
.translate(0, 0, -2)
261+
.getMatrix()
262+
]),
263+
(4 * 4 * Float32Array.BYTES_PER_ELEMENT) * 7,
264+
);
265+
}
266+
236267
async loadLogoTexture() {
237268
const TEXTURE_SLOT_SIZE = 512;
238269
const LOGO_SIZE = 2048;
@@ -329,6 +360,8 @@ export class GLEngine extends Disposable {
329360
...groundSlot.size, groundSlot.slotNumber,
330361
...groundSlot.size, groundSlot.slotNumber,
331362
...groundSlot.size, groundSlot.slotNumber,
363+
364+
...slot.size, slot.slotNumber,
332365
]),
333366
0,
334367
GL.DYNAMIC_DRAW,
@@ -347,10 +380,10 @@ export class GLEngine extends Disposable {
347380
}
348381

349382
refresh() {
350-
this.gl.clearDepth(1.0);
351383
this.gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
352384
const vertexCount = 6;
353-
const instanceCount = 7;
385+
const instanceCount = 8;
386+
this.syncHud();
354387
this.drawElementsInstanced(vertexCount, instanceCount);
355388
}
356389

src/gl/camera/GLCamera.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export class GLCamera {
2828
this.updatePerspective();
2929
}
3030

31-
private camTiltMatrix = Matrix.create().getMatrix();
32-
private camTurnMatrix = Matrix.create().getMatrix();
31+
camTiltMatrix = Matrix.create().getMatrix();
32+
camTurnMatrix = Matrix.create().getMatrix();
3333
private camMatrix: mat4 = mat4.create();
3434
refresh() {
3535
// camMatrix = camTiltMatrix * camTurnMatrix * cameraMatrix;

src/gl/transform/Matrix.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ class Matrix {
1313
return new Matrix();
1414
}
1515

16+
public copy(matrix: mat4): Matrix {
17+
mat4.copy(this.matrix, matrix);
18+
return this;
19+
}
20+
21+
public invert(): Matrix {
22+
mat4.invert(this.matrix, this.matrix);
23+
return this;
24+
}
25+
26+
public multiply(matrix: mat4): Matrix {
27+
mat4.multiply(this.matrix, this.matrix, matrix);
28+
return this;
29+
}
30+
1631
public translate(x: number, y: number, z: number): Matrix {
1732
mat4.translate(this.matrix, this.matrix, [x, y, z]);
1833
return this;

0 commit comments

Comments
 (0)