diff --git a/vendor/clouds3d.js b/vendor/clouds3d.js index 31ab434..c9a82a3 100644 --- a/vendor/clouds3d.js +++ b/vendor/clouds3d.js @@ -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); }