BlueGit

timfa frameworkv3

Add configurable default texture filter for getTexture uploads.

Games can set NEAREST once for pixel art instead of patching filter parameters on every texture lookup.

timfa Tim Falken · pushed by timfa

Authored 2026-09-06 · 2 months ago

Hash
089bb8ab9faab473c4a594f2b0236fd41b71ae90
Parent
98c7509b
1 file changed +16 −2
framework/engine/gl/glContext.js +16 −2
@@ -22,6 +22,9 @@ class GLContext
22 22 this.defaultShader = new Shader(Shader.DEFAULT_VERT, Shader.DEFAULT_FRAG);
23 23 this.screenCopyShader = new Shader(Shader.SCREEN_VERT, Shader.SCREEN_COPY_FRAG);
24 24 this.textures = new WeakMap();
25 // Defaults for getTexture uploads. Games can switch to NEAREST for pixel art.
26 this.textureMinFilter = gl.LINEAR;
27 this.textureMagFilter = gl.LINEAR;
25 28
26 29 // Reused per-sprite uploads (avoid allocating Float32Array every draw).
27 30 this._uvData = new Float32Array(8);
@@ -360,6 +363,17 @@ class GLContext
360 363 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
361 364 }
362 365
366 /**
367 * Default min/mag filter applied when creating textures in {@link getTexture}.
368 * Does not rewrite textures that were already uploaded.
369 */
370 setDefaultTextureFilter (minFilter, magFilter)
371 {
372 const gl = this.gl;
373 this.textureMinFilter = minFilter ?? gl.LINEAR;
374 this.textureMagFilter = magFilter ?? gl.LINEAR;
375 }
376
363 377 getTexture (htmlImage)
364 378 {
365 379 if (!htmlImage)
@@ -374,8 +388,8 @@ class GLContext
374 388 gl.bindTexture(gl.TEXTURE_2D, texture);
375 389 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
376 390 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
377 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
378 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
391 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, this.textureMinFilter);
392 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, this.textureMagFilter);
379 393 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, htmlImage);
380 394
381 395 this.textures.set(htmlImage, texture);
@@ -22,6 +22,9 @@ class GLContext
22 this.defaultShader = new Shader(Shader.DEFAULT_VERT, Shader.DEFAULT_FRAG);
22 this.defaultShader = new Shader(Shader.DEFAULT_VERT, Shader.DEFAULT_FRAG);
23 this.screenCopyShader = new Shader(Shader.SCREEN_VERT, Shader.SCREEN_COPY_FRAG);
23 this.screenCopyShader = new Shader(Shader.SCREEN_VERT, Shader.SCREEN_COPY_FRAG);
24 this.textures = new WeakMap();
24 this.textures = new WeakMap();
25 // Defaults for getTexture uploads. Games can switch to NEAREST for pixel art.
26 this.textureMinFilter = gl.LINEAR;
27 this.textureMagFilter = gl.LINEAR;
25
28
26 // Reused per-sprite uploads (avoid allocating Float32Array every draw).
29 // Reused per-sprite uploads (avoid allocating Float32Array every draw).
27 this._uvData = new Float32Array(8);
30 this._uvData = new Float32Array(8);
@@ -360,6 +363,17 @@ class GLContext
360 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
363 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
361 }
364 }
362
365
366 /**
367 * Default min/mag filter applied when creating textures in {@link getTexture}.
368 * Does not rewrite textures that were already uploaded.
369 */
370 setDefaultTextureFilter (minFilter, magFilter)
371 {
372 const gl = this.gl;
373 this.textureMinFilter = minFilter ?? gl.LINEAR;
374 this.textureMagFilter = magFilter ?? gl.LINEAR;
375 }
376
363 getTexture (htmlImage)
377 getTexture (htmlImage)
364 {
378 {
365 if (!htmlImage)
379 if (!htmlImage)
@@ -374,8 +388,8 @@ class GLContext
374 gl.bindTexture(gl.TEXTURE_2D, texture);
388 gl.bindTexture(gl.TEXTURE_2D, texture);
375 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
389 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
376 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
390 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
377 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
391 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, this.textureMinFilter);
378 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
392 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, this.textureMagFilter);
379 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, htmlImage);
393 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, htmlImage);
380
394
381 this.textures.set(htmlImage, texture);
395 this.textures.set(htmlImage, texture);