Skip to content

Commit ffed203

Browse files
committed
Fix textureIndex in shader.
1 parent ce37664 commit ffed203

File tree

16 files changed

+142
-119
lines changed

16 files changed

+142
-119
lines changed

build/index.js

Lines changed: 11 additions & 7 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: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bun.lockb

-8 Bytes
Binary file not shown.

src/GLEngine.ts

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@ import {
1313
INDEX_LOC,
1414
TRANSFORM_LOC,
1515
SLOT_SIZE_LOC,
16-
CAM_LOC,
17-
MAX_TEXTURE_SIZE_LOC,
18-
} from './gl/attributes/Contants';
16+
} from './gl/attributes/Constants';
1917
import { TextureManager } from './gl/texture/TextureManager';
2018
import { ImageManager } from 'gl/texture/ImageManager';
2119
import vertexShader from 'generated/src/gl/resources/vertexShader.txt';
2220
import fragmentShader from 'generated/src/gl/resources/fragmentShader.txt';
2321
import { replaceTilda } from 'gl/utils/replaceTilda';
2422
import Matrix from 'gl/transform/Matrix';
2523
import { GLCamera } from 'gl/camera/GLCamera';
26-
//import { TextureSlotAllocator } from 'gl/texture/TextureSlotAllocator';
27-
import { TextureSlotAllocator } from 'texture-slot-allocator/src';
2824

2925
const DEFAULT_ATTRIBUTES: WebGLContextAttributes = {
3026
alpha: true,
@@ -72,7 +68,6 @@ export class GLEngine extends Disposable {
7268
canvas: HTMLCanvasElement;
7369

7470
textureManager: TextureManager;
75-
textureAllocator: TextureSlotAllocator
7671
imageManager: ImageManager;
7772
camera: GLCamera;
7873

@@ -89,8 +84,7 @@ export class GLEngine extends Disposable {
8984
new GLAttributeBuffers(this.gl, this.programs),
9085
);
9186

92-
this.textureManager = new TextureManager(this.gl);
93-
this.textureAllocator = new TextureSlotAllocator();
87+
this.textureManager = new TextureManager(this.gl, this.uniforms);
9488
this.imageManager = new ImageManager();
9589
this.camera = new GLCamera(this.gl, this.uniforms);
9690

@@ -101,8 +95,7 @@ export class GLEngine extends Disposable {
10195
this.canvas.width = this.canvas.offsetWidth * 2;
10296
this.canvas.height = this.canvas.offsetHeight * 2;
10397
this.gl.viewport(
104-
0,
105-
0,
98+
0, 0,
10699
this.gl.drawingBufferWidth,
107100
this.gl.drawingBufferHeight,
108101
);
@@ -123,11 +116,8 @@ export class GLEngine extends Disposable {
123116
replaceTilda(fragmentShader, replacementMap),
124117
);
125118

126-
console.log(performance.now(), 11);
127119
this.programs.useProgram(PROGRAM_NAME);
128-
console.log(this.uniforms.getUniformLocation(CAM_LOC));
129120

130-
console.log(performance.now(), 22);
131121
// enable depth
132122
this.gl.enable(GL.DEPTH_TEST);
133123
this.gl.depthFunc(GL.LESS);
@@ -143,7 +133,6 @@ export class GLEngine extends Disposable {
143133
this.gl.drawingBufferHeight,
144134
);
145135

146-
console.log(performance.now(), 33);
147136
{
148137
/*
149138
0 1
@@ -189,7 +178,6 @@ export class GLEngine extends Disposable {
189178
GL.STATIC_DRAW,
190179
);
191180
}
192-
console.log(performance.now(), 44);
193181

194182
{
195183
const location = TRANSFORM_LOC;
@@ -228,18 +216,13 @@ export class GLEngine extends Disposable {
228216
...Matrix.create().translate(2, -1, 3).rotateX(-Math.PI / 2).scale(1, 1, 1).getMatrix(),
229217

230218
// sprite
231-
...Matrix.create().translate(0, 0, 0).getMatrix(),
219+
...Matrix.create().getMatrix(),
232220
]),
233221
0,
234222
GL.DYNAMIC_DRAW,
235223
);
236224

237-
const loc = this.uniforms.getUniformLocation(MAX_TEXTURE_SIZE_LOC);
238-
if (loc) {
239-
this.gl.uniform1f(loc, this.textureAllocator.maxTextureSize);
240-
}
241-
242-
console.log(performance.now(), 55);
225+
this.textureManager.initialize();
243226
}
244227

245228
await this.loadLogoTexture();
@@ -269,17 +252,15 @@ export class GLEngine extends Disposable {
269252
.translate(cameraPosition[0], cameraPosition[1], cameraPosition[2])
270253
.multiply(invertedCamTurnMatrix)
271254
.multiply(invertedCamTiltMatrix)
272-
.translate(0, 0, -2)
255+
.translate(0, 0, -.9)
273256
.getMatrix()
274257
]),
275-
(4 * 4 * Float32Array.BYTES_PER_ELEMENT) * 7,
258+
4 * 4 * Float32Array.BYTES_PER_ELEMENT * 7,
276259
);
277260
}
278261

279262
async loadLogoTexture() {
280-
const maxTextureSize = this.textureAllocator.maxTextureSize;
281-
const TEXTURE_SLOT_SIZE = maxTextureSize;
282-
const LOGO_SIZE = maxTextureSize / 4;
263+
const LOGO_SIZE = 512;
283264
await this.imageManager.drawImage('logo', (ctx) => {
284265
const { canvas } = ctx;
285266
canvas.width = LOGO_SIZE;
@@ -333,24 +314,34 @@ export class GLEngine extends Disposable {
333314
ctx.stroke();
334315
});
335316

336-
console.log(performance.now(), 1);
337-
const slot = this.textureAllocator.allocate(TEXTURE_SLOT_SIZE, TEXTURE_SLOT_SIZE);
317+
await this.imageManager.drawImage('hud', (ctx) => {
318+
const { canvas } = ctx;
319+
canvas.width = LOGO_SIZE;
320+
canvas.height = LOGO_SIZE;
321+
ctx.imageSmoothingEnabled = true;
322+
ctx.fillStyle = '#ddd';
323+
ctx.lineWidth = canvas.width / 50;
324+
325+
ctx.strokeStyle = 'black';
326+
ctx.fillStyle = 'silver';
327+
328+
// face
329+
ctx.beginPath();
330+
ctx.rect(30, 30, canvas.width - 60, canvas.height - 60);
331+
ctx.stroke();
332+
});
333+
338334
const logoMediaInfo = this.imageManager.getMedia('logo');
339-
console.log(performance.now(), 2);
340-
this.textureManager.applyImageToSlot(logoMediaInfo, slot)
341-
console.log(performance.now(), 3);
335+
const { slot } = this.textureManager.allocateSlotForImage(logoMediaInfo);
342336

343-
const groundSlot = this.textureAllocator.allocate(TEXTURE_SLOT_SIZE, TEXTURE_SLOT_SIZE);
344-
console.log(performance.now(), 4);
345337
const groundMediaInfo = this.imageManager.getMedia('ground');
346-
console.log(performance.now(), 5);
347-
this.textureManager.applyImageToSlot(groundMediaInfo, groundSlot)
348-
console.log(performance.now(), 6);
338+
const { slot: groundSlot } = this.textureManager.allocateSlotForImage(groundMediaInfo);
349339

350-
console.log(slot, groundSlot);
340+
const hudMediaInfo = this.imageManager.getMedia('hud');
341+
const { slot: hudSlot } = this.textureManager.allocateSlotForImage(hudMediaInfo);
351342

352-
console.log(performance.now(), 7);
353-
this.textureManager.generateMipMap("TEXTURE0");
343+
344+
this.textureManager.generateMipMaps();
354345

355346
{
356347
const location = SLOT_SIZE_LOC;
@@ -381,7 +372,7 @@ export class GLEngine extends Disposable {
381372
...groundSlot.size, groundSlot.slotNumber,
382373
...groundSlot.size, groundSlot.slotNumber,
383374

384-
...slot.size, slot.slotNumber,
375+
...hudSlot.size, hudSlot.slotNumber,
385376
]),
386377
0,
387378
GL.DYNAMIC_DRAW,

src/gl/VertexArray.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Disposable } from '../disposable/Disposable';
2-
import { GL } from './attributes/Contants';
2+
import { GL } from './attributes/Constants';
33

44
export class VertexArray extends Disposable {
55
private gl: GL;

src/gl/attributes/Contants.ts renamed to src/gl/attributes/Constants.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ export const SLOT_SIZE_LOC: LocationName = 'slotSize_and_number';
1818
export const CAM_LOC: LocationName = 'cam';
1919
export const PROJECTION_LOC: LocationName = 'projection';
2020
export const MAX_TEXTURE_SIZE_LOC: LocationName = 'maxTextureSize';
21-
22-
export const DEBUG = false;
21+
export const TEXTURE_UNIFORM_LOC: LocationName = 'uTextures';

src/gl/attributes/GLAttributeBuffers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Disposable } from '../../disposable/Disposable';
22
import { GLPrograms } from '../programs/GLPrograms';
3-
import { GL } from './Contants';
3+
import { GL } from './Constants';
44

55
export interface BufferInfo {
66
buffer: WebGLBuffer;

src/gl/camera/GLCamera.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { mat4 } from "gl-matrix";
2-
import { CAM_LOC, GL, PROJECTION_LOC } from "gl/attributes/Contants";
2+
import { CAM_LOC, GL, PROJECTION_LOC } from "gl/attributes/Constants";
33
import Matrix from "gl/transform/Matrix";
44
import { GLUniforms } from "gl/uniforms/GLUniforms";
55

66
export class GLCamera {
77
gl: GL;
88
uniforms: GLUniforms;
99
cameraMatrix: mat4 = Matrix.create().translate(0, 0, -1).getMatrix();
10-
perspectiveMatrix: mat4 = Matrix.create().getMatrix();
11-
orthoMatrix: mat4 = Matrix.create().getMatrix();
12-
projectionMatrix: mat4 = Matrix.create().getMatrix();
10+
private perspectiveMatrix: mat4 = Matrix.create().getMatrix();
11+
private orthoMatrix: mat4 = Matrix.create().getMatrix();
12+
private projectionMatrix: mat4 = Matrix.create().getMatrix();
1313
private perspectiveLevel: number = 1;
1414

1515
constructor(gl: GL, uniforms: GLUniforms) {
@@ -19,7 +19,7 @@ export class GLCamera {
1919

2020

2121
configPerspectiveMatrix(ratio: number) {
22-
this.perspectiveMatrix = Matrix.create().perspective(45, ratio, 0.1, 1000).getMatrix();
22+
this.perspectiveMatrix = Matrix.create().perspective(45, ratio, 0.01, 1000).getMatrix();
2323
this.updatePerspective();
2424
}
2525

@@ -38,9 +38,7 @@ export class GLCamera {
3838
mat4.mul(this.camMatrix, this.camMatrix, this.cameraMatrix);
3939

4040
const loc = this.uniforms.getUniformLocation(CAM_LOC);
41-
if (loc) {
42-
this.gl.uniformMatrix4fv(loc, false, this.camMatrix);
43-
}
41+
this.gl.uniformMatrix4fv(loc, false, this.camMatrix);
4442
}
4543

4644

@@ -52,9 +50,7 @@ export class GLCamera {
5250
}
5351
Matrix.combineMat4(this.projectionMatrix, this.orthoMatrix, this.perspectiveMatrix, this.perspectiveLevel);
5452
const loc = this.uniforms.getUniformLocation(PROJECTION_LOC);
55-
if (loc) {
56-
this.gl.uniformMatrix4fv(loc, false, this.projectionMatrix);
57-
}
53+
this.gl.uniformMatrix4fv(loc, false, this.projectionMatrix);
5854

5955
this.refresh();
6056
}

src/gl/programs/GLProgram.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GL } from 'gl/attributes/Contants';
1+
import { GL } from 'gl/attributes/Constants';
22
import { Disposable } from '../../disposable/Disposable';
33

44
export class GLProgram extends Disposable {

src/gl/programs/GLPrograms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GL } from 'gl/attributes/Contants';
1+
import { GL } from 'gl/attributes/Constants';
22
import { Disposable } from '../../disposable/Disposable';
33
import { GLProgram } from './GLProgram';
44

src/gl/resources/vertexShader.vert

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ void main() {
2525
vec2 tex = position_and_tex.zw;
2626
vec2 slotSize = slotSize_and_number.xy;
2727
float slotNumber = slotSize_and_number.z;
28-
vec2 slotOffset = vec2(mod(slotNumber, slotSize.x), floor(slotNumber / slotSize.x));
28+
float maxCols = maxTextureSize / slotSize.x;
29+
float maxRows = maxTextureSize / slotSize.y;
30+
float slotX = mod(slotNumber, maxCols);
31+
float slotY = mod(floor(slotNumber / maxCols), maxRows);
2932

3033
gl_Position = projection * cam * transform * vec4(position, 0.0, 1.0);
31-
vTex = (slotOffset + tex) * (slotSize / maxTextureSize);
32-
textureIndex = 0.;
34+
vTex = (vec2(slotX, slotY) + tex) * slotSize / maxTextureSize;
35+
textureIndex = floor(slotNumber / (maxCols * maxRows));
3336
}

src/gl/texture/TextureManager.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { beforeEach, describe, expect, it, jest } from 'bun:test';
22
import { TextureManager } from './TextureManager';
33
import "../../test/GLMock";
44
import "../../test/MockCanvas";
5-
import { GL } from 'gl/attributes/Contants';
5+
import { GL } from 'gl/attributes/Constants';
6+
import { GLUniforms } from 'gl/uniforms/GLUniforms';
67

78
describe('TextureManager', () => {
89
let gl: Partial<GL>;
10+
let uniforms: Partial<GLUniforms>;
911
let textureManager: TextureManager;
1012

1113
beforeEach(() => {
@@ -14,9 +16,16 @@ describe('TextureManager', () => {
1416
createTexture: jest.fn().mockReturnValue({}),
1517
bindTexture: jest.fn(),
1618
texImage2D: jest.fn(),
19+
getParameter: jest.fn().mockReturnValue(16),
20+
uniform1iv: jest.fn(),
21+
uniform1f: jest.fn(),
1722
};
1823

19-
textureManager = new TextureManager(gl as GL);
24+
uniforms = {
25+
getUniformLocation: jest.fn(),
26+
};
27+
28+
textureManager = new TextureManager(gl as GL, uniforms as GLUniforms);
2029
});
2130

2231
it('should construct properly', () => {
@@ -43,4 +52,11 @@ describe('TextureManager', () => {
4352

4453
expect(gl.createTexture).not.toHaveBeenCalled();
4554
});
55+
56+
it('should initialize', () => {
57+
textureManager.initialize();
58+
expect(gl.getParameter);
59+
expect(gl.uniform1iv);
60+
expect(gl.uniform1f);
61+
});
4662
});

0 commit comments

Comments
 (0)