BlueGit

timfa frameworkv3

Merge branch 'feature/draw-order-equal-layer-fix' into 'master'

Add configurable default texture filter for getTexture uploads.

See merge request Timfa/frameworkv3!14

timfa Tim Falken · pushed by timfa

Authored 2026-09-06 · 2 months ago

Hash
b122579368b274094f2dd1b8f03763d6dae47fd9
3 files changed +40 −9
framework/engine/gameobject.js +12 −4
@@ -41,12 +41,20 @@ class GameObject
41 41 if (!this.quadTree)
42 42 { return; }
43 43
44 const x = this.transform.position.x;
45 const y = this.transform.position.y;
46 // Skip reinsert when the object has not moved since the last check.
47 if (this.node && this._qtX === x && this._qtY === y)
48 { return; }
49 this._qtX = x;
50 this._qtY = y;
51
44 52 if (this.node)
45 53 {
46 if (this.transform.position.x < this.node.x1
47 || this.transform.position.x > this.node.x2
48 || this.transform.position.y < this.node.y1
49 || this.transform.position.y > this.node.y2)
54 if (x < this.node.x1
55 || x > this.node.x2
56 || y < this.node.y1
57 || y > this.node.y2)
50 58 {
51 59 this.quadTree.remove(this);
52 60 this.quadTree.add(this);
@@ -41,12 +41,20 @@ class GameObject
41 if (!this.quadTree)
41 if (!this.quadTree)
42 { return; }
42 { return; }
43
43
44 const x = this.transform.position.x;
45 const y = this.transform.position.y;
46 // Skip reinsert when the object has not moved since the last check.
47 if (this.node && this._qtX === x && this._qtY === y)
48 { return; }
49 this._qtX = x;
50 this._qtY = y;
51
44 if (this.node)
52 if (this.node)
45 {
53 {
46 if (this.transform.position.x < this.node.x1
54 if (x < this.node.x1
47 || this.transform.position.x > this.node.x2
55 || x > this.node.x2
48 || this.transform.position.y < this.node.y1
56 || y < this.node.y1
49 || this.transform.position.y > this.node.y2)
57 || y > this.node.y2)
50 {
58 {
51 this.quadTree.remove(this);
59 this.quadTree.remove(this);
52 this.quadTree.add(this);
60 this.quadTree.add(this);
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);
framework/engine/scene.js +12 −3
@@ -23,6 +23,9 @@ class Scene
23 23 * full in-place sort (cheaper than many splice reinserts on large lists).
24 24 */
25 25 _layerChangeFullSortThreshold = 64;
26 /** Reused per-draw-layer object lists (cleared + refilled each draw). */
27 _byLayer = null;
28 _byLayerNames = null;
26 29
27 30 g_primary = false;
28 31
@@ -513,8 +516,13 @@ class Scene
513 516 this.#ensureDrawOrder();
514 517 const sortedActiveObjects = this._sortedActiveObjects;
515 518
516 // Bucket visible objects once so empty bloom layers can be skipped entirely.
517 const byLayer = Object.create(null);
519 // Reuse per-layer buckets so we do not allocate new arrays every frame.
520 const byLayer = this._byLayer || (this._byLayer = Object.create(null));
521 const byLayerNames = this._byLayerNames || (this._byLayerNames = []);
522 for (let i = 0; i < byLayerNames.length; i++)
523 { byLayer[byLayerNames[i]].length = 0; }
524 byLayerNames.length = 0;
525
518 526 for (let i = 0; i < sortedActiveObjects.length; i++)
519 527 {
520 528 const obj = sortedActiveObjects[i];
@@ -528,7 +536,8 @@ class Scene
528 536 bucket = [];
529 537 byLayer[name] = bucket;
530 538 }
531
539 if (bucket.length === 0)
540 { byLayerNames.push(name); }
532 541 bucket.push(obj);
533 542 }
534 543
@@ -23,6 +23,9 @@ class Scene
23 * full in-place sort (cheaper than many splice reinserts on large lists).
23 * full in-place sort (cheaper than many splice reinserts on large lists).
24 */
24 */
25 _layerChangeFullSortThreshold = 64;
25 _layerChangeFullSortThreshold = 64;
26 /** Reused per-draw-layer object lists (cleared + refilled each draw). */
27 _byLayer = null;
28 _byLayerNames = null;
26
29
27 g_primary = false;
30 g_primary = false;
28
31
@@ -513,8 +516,13 @@ class Scene
513 this.#ensureDrawOrder();
516 this.#ensureDrawOrder();
514 const sortedActiveObjects = this._sortedActiveObjects;
517 const sortedActiveObjects = this._sortedActiveObjects;
515
518
516 // Bucket visible objects once so empty bloom layers can be skipped entirely.
519 // Reuse per-layer buckets so we do not allocate new arrays every frame.
517 const byLayer = Object.create(null);
520 const byLayer = this._byLayer || (this._byLayer = Object.create(null));
521 const byLayerNames = this._byLayerNames || (this._byLayerNames = []);
522 for (let i = 0; i < byLayerNames.length; i++)
523 { byLayer[byLayerNames[i]].length = 0; }
524 byLayerNames.length = 0;
525
518 for (let i = 0; i < sortedActiveObjects.length; i++)
526 for (let i = 0; i < sortedActiveObjects.length; i++)
519 {
527 {
520 const obj = sortedActiveObjects[i];
528 const obj = sortedActiveObjects[i];
@@ -528,7 +536,8 @@ class Scene
528 bucket = [];
536 bucket = [];
529 byLayer[name] = bucket;
537 byLayer[name] = bucket;
530 }
538 }
531
539 if (bucket.length === 0)
540 { byLayerNames.push(name); }
532 bucket.push(obj);
541 bucket.push(obj);
533 }
542 }
534
543