Fix shader: pass uFlightDir as uniform instead of window access

The shader had the JS code 'window.__flightDir.dx' baked into the
GLSL string, which GLSL can't parse. Added a uFlightDir uniform
(vec2) and pass the dx/dz from JS via gl.uniform2f. Shader reads
uFlightDir.x and uFlightDir.y.
This commit is contained in:
Richard Zink committed 2026-10-08 06:18:56 +02:00
1 parent f21a967643
commit e17076a273
1 file changed
+7 -1
+7 -1
View File
@@ -56,6 +56,7 @@ var FRAG_SRC = [
'uniform sampler2D uGround;',
'uniform vec2 uGroundCenter;', // current mosaic centre (world units)
'uniform vec2 uGroundSize;', // mosaic side extents in X and Z (world units)
'uniform vec2 uFlightDir;', // camera motion direction (dx, dz)
'uniform vec3 uSky;',
'uniform vec3 uCloud;',
'uniform vec3 uCloudShadow;',
@@ -143,7 +144,7 @@ var FRAG_SRC = [
// Default: drift along +Z (north). For east: drift along +X. For south: -Z, etc.
'float fdx = (window.__flightDir ? window.__flightDir.dx : 0.0);',
'float fdz = (window.__flightDir ? window.__flightDir.dz : 1.0);',
'vec3 ro = vec3(drift * fdx, camY, drift * fdz);',
'vec3 ro = vec3(drift * uFlightDir.x, camY, drift * uFlightDir.y);',
// Ray direction: tilt more downward so the visible ground fits
// within the mosaic. At uv.y = 0 the ray goes ~26° below
// horizontal, putting the visible ground at tGround < 5
@@ -251,6 +252,7 @@ function init(canvas) {
var uGround = gl.getUniformLocation(prog, 'uGround');
var uGroundCenter = gl.getUniformLocation(prog, 'uGroundCenter');
var uGroundSize = gl.getUniformLocation(prog, 'uGroundSize');
var uFlightDir = gl.getUniformLocation(prog, 'uFlightDir');
var uSky = gl.getUniformLocation(prog, 'uSky');
var uCloud = gl.getUniformLocation(prog, 'uCloud');
var uCloudShadow = gl.getUniformLocation(prog, 'uCloudShadow');
@@ -265,6 +267,7 @@ function init(canvas) {
gl: gl, prog: prog, buf: buf, aPos: aPos,
uRes: uRes, uTime: uTime, uGround: uGround,
uGroundCenter: uGroundCenter, uGroundSize: uGroundSize,
uFlightDir: uFlightDir,
uSky: uSky, uCloud: uCloud,
uCloudShadow: uCloudShadow, uLight: uLight, uBg: uBg,
uSpeed: uSpeed, uDensity: uDensity, uDayProgress: uDayProgress,
@@ -542,6 +545,9 @@ window.clouds3dRender = function(target) {
};
gl.uniform2f(s.uGroundCenter, centerWorld.x, centerWorld.y);
gl.uniform2f(s.uGroundSize, GROUND_CORRIDOR_PERP, GROUND_CORRIDOR_LONG);
var flightDir = (window.__flightDir && (window.__flightDir.dx !== undefined))
? window.__flightDir : { dx: 0, dz: 1 };
gl.uniform2f(s.uFlightDir, flightDir.dx, flightDir.dz);
var drift = now * speed * 0.12;
if (performance.now() - groundLastCheck > 1500) {