Fix runtime tile system: gl out of scope in rebuildGroundMosaic

The function used bare 'gl' but it's a module-level closure, so
gl wasn't defined inside the .then() callback. Added groundGl
module variable, set it in initGroundSystem, used it everywhere
in rebuildGroundMosaic instead of bare 'gl'.
This commit is contained in:
Richard Zink committed 2026-10-07 20:49:59 +02:00
1 parent 979134102d
commit 19139b01c7
1 file changed
+14 -12
+14 -12
View File
@@ -279,6 +279,7 @@ var GROUND_BASE_Y = 710;
var groundCanvas = null;
var groundCtx = null;
var groundTexture = null;
var groundGl = null; // WebGL context, set by initGroundSystem
var groundCenterTile = { x: GROUND_BASE_X, y: GROUND_BASE_Y };
var groundLoadedTiles = {}; // "x_y" -> true
var groundLastCheck = 0;
@@ -329,9 +330,9 @@ function rebuildGroundMosaic(centerTileX, centerTileY) {
}
}
return Promise.all(promises).then(function() {
if (groundTexture) {
gl.bindTexture(gl.TEXTURE_2D, groundTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, groundCanvas);
if (groundTexture && groundGl) {
groundGl.bindTexture(gl.TEXTURE_2D, groundTexture);
groundGl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, groundCanvas);
}
});
}
@@ -355,8 +356,9 @@ function maybeRefetchGround(cameraWorldX, cameraWorldZ, gl) {
// Initialise the ground state. Must be called once with the WebGL
// context before clouds3dRender uses it.
function initGroundSystem(gl) {
function initGroundSystem(glCtx) {
if (groundCanvas) return; // already initialised
groundGl = glCtx;
groundCanvas = document.createElement('canvas');
groundCanvas.width = GROUND_TILE_SIZE * GROUND_MOSAIC_SIZE;
groundCanvas.height = GROUND_TILE_SIZE * GROUND_MOSAIC_SIZE;
@@ -365,14 +367,14 @@ function initGroundSystem(gl) {
// shows the right base colour
groundCtx.fillStyle = '#7c9aa1';
groundCtx.fillRect(0, 0, groundCanvas.width, groundCanvas.height);
groundTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, groundTexture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, groundCanvas);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
groundTexture = groundGl.createTexture();
groundGl.bindTexture(gl.TEXTURE_2D, groundTexture);
groundGl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
groundGl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, groundCanvas);
groundGl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
groundGl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
groundGl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
groundGl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
// Kick off initial fetch
rebuildGroundMosaic(GROUND_BASE_X, GROUND_BASE_Y);
}