/** * GPU floor/ceiling + point lights (WebGL1 fragment shader via frameworkv3 drawSprite). * Uses the same camera-plane rays + perp rowDist as Camera / FloorCast (no fish-eye). */ var FloorGpu = { MAX_LIGHTS: 8, _shader: null, _whiteTex: null, _gridTex: null, _gridW: 0, _gridH: 0, _gridKey: "", _stoneTex: null, _woodTex: null, _stoneSrc: null, _woodSrc: null, _ensureShader: function () { if (this._shader) { return this._shader; } var frag = [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform sampler2D u_stone;", "uniform sampler2D u_wood;", "uniform sampler2D u_grid;", "uniform float u_alpha;", "uniform vec2 u_resolution;", "uniform vec2 u_player;", "uniform vec2 u_dir;", "uniform vec2 u_plane;", "uniform float u_pitch;", "uniform float u_cellSize;", "uniform float u_posZ;", "uniform vec2 u_levelSize;", "uniform float u_ambient;", "uniform float u_lightCount;", "uniform vec4 u_l0;", "uniform vec4 u_l1;", "uniform vec4 u_l2;", "uniform vec4 u_l3;", "uniform vec4 u_l4;", "uniform vec4 u_l5;", "uniform vec4 u_l6;", "uniform vec4 u_l7;", "uniform vec3 u_c0;", "uniform vec3 u_c1;", "uniform vec3 u_c2;", "uniform vec3 u_c3;", "uniform vec3 u_c4;", "uniform vec3 u_c5;", "uniform vec3 u_c6;", "uniform vec3 u_c7;", "uniform vec2 u_a0;", "uniform vec2 u_a1;", "uniform vec2 u_a2;", "uniform vec2 u_a3;", "uniform vec2 u_a4;", "uniform vec2 u_a5;", "uniform vec2 u_a6;", "uniform vec2 u_a7;", "varying vec2 v_texCoord;", "vec4 lightPos(int i) {", " if (i==0) return u_l0; if (i==1) return u_l1; if (i==2) return u_l2; if (i==3) return u_l3;", " if (i==4) return u_l4; if (i==5) return u_l5; if (i==6) return u_l6; return u_l7;", "}", "vec3 lightCol(int i) {", " if (i==0) return u_c0; if (i==1) return u_c1; if (i==2) return u_c2; if (i==3) return u_c3;", " if (i==4) return u_c4; if (i==5) return u_c5; if (i==6) return u_c6; return u_c7;", "}", "vec2 lightAtt(int i) {", " if (i==0) return u_a0; if (i==1) return u_a1; if (i==2) return u_a2; if (i==3) return u_a3;", " if (i==4) return u_a4; if (i==5) return u_a5; if (i==6) return u_a6; return u_a7;", "}", "void main() {", " float sx = gl_FragCoord.x;", " float sy = u_resolution.y - gl_FragCoord.y;", " float mid = u_resolution.y * 0.5 + (-u_pitch * u_resolution.y * 0.25);", " float p = sy - mid;", " if (abs(p) < 0.5) { gl_FragColor = vec4(0.0, 0.0, 0.0, u_alpha); return; }", " float isCeil = step(p, 0.0);", " float rowDist = u_posZ / abs(p);", " float camX = (2.0 * sx / u_resolution.x) - 1.0;", " vec2 rayDir = u_dir + u_plane * camX;", " float wx = u_player.x + rayDir.x * rowDist;", " float wy = u_player.y + rayDir.y * rowDist;", " float cx = floor(wx / u_cellSize);", " float cy = floor(wy / u_cellSize);", " if (cx < 0.0 || cy < 0.0 || cx >= u_levelSize.x || cy >= u_levelSize.y) {", " gl_FragColor = vec4(0.0, 0.0, 0.0, u_alpha); return;", " }", " vec2 guv = (vec2(cx, cy) + 0.5) / u_levelSize;", " vec4 cell = texture2D(u_grid, guv);", " float floorId = floor(cell.r * 255.0 + 0.5);", " float ceilId = floor(cell.g * 255.0 + 0.5);", " float tileId = floor(cell.b * 255.0 + 0.5);", " float texId = mix(floorId, ceilId, isCeil);", " if (texId < 0.5) {", " if (tileId > 2.5) texId = 2.0;", " else if (tileId > 0.5) texId = 1.0;", " }", " if (texId < 0.5) { gl_FragColor = vec4(0.0, 0.0, 0.0, u_alpha); return; }", " vec2 tuv = fract(vec2(wx, wy) / u_cellSize);", " vec3 albedo = mix(texture2D(u_stone, tuv).rgb, texture2D(u_wood, tuv).rgb, step(1.5, texId));", " float wz = mix(0.0, u_cellSize, isCeil);", " vec3 nrm = mix(vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, -1.0), isCeil);", " vec3 lit = vec3(u_ambient);", " for (int i = 0; i < 8; i++) {", " if (float(i) >= u_lightCount) break;", " vec4 Lp = lightPos(i);", " vec3 toL = Lp.xyz - vec3(wx, wy, wz);", " float dist = length(toL);", " vec2 attP = lightAtt(i);", " float t = 1.0 - dist / max(attP.x, 0.001);", " if (t <= 0.0) continue;", " float att = t;", " if (attP.y > 1.5) att = t * t;", " if (attP.y > 2.5) att = att * t;", " vec3 ld = toL / max(dist, 0.0001);", " float ndotl = dot(nrm, ld);", " if (ndotl <= 0.0) continue;", " lit += Lp.w * att * ndotl * lightCol(i);", " }", " gl_FragColor = vec4(min(albedo * lit, vec3(1.0)), u_alpha);", "}" ].join("\n"); this._shader = new Shader(Shader.DEFAULT_VERT, frag); return this._shader; }, _texIdByte: function (id) { if (id === "stone") return 1; if (id === "wood") return 2; return 0; }, _uploadGrid: function (gl, level) { var w = level.width; var h = level.height; var key = (level.id || "") + "|" + w + "x" + h + "|" + (level._rev || 0); if (this._gridTex && this._gridKey === key && this._gridW === w && this._gridH === h) { return; } var data = new Uint8Array(w * h * 4); var floors = level.floors || []; var ceilings = level.ceilings || []; var tiles = level.tiles || []; for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { var i = x + y * w; var o = i * 4; data[o] = this._texIdByte(floors[i]); data[o + 1] = this._texIdByte(ceilings[i]); data[o + 2] = tiles[i] || 0; data[o + 3] = 255; } } if (!this._gridTex) { this._gridTex = gl.createTexture(); } gl.bindTexture(gl.TEXTURE_2D, this._gridTex); gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 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.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, data); this._gridW = w; this._gridH = h; this._gridKey = key; }, _uploadAlbedo: function (gl, htmlImg, which) { var prev = which === "stone" ? this._stoneSrc : this._woodSrc; var tex = which === "stone" ? this._stoneTex : this._woodTex; if (tex && prev === htmlImg) { return tex; } if (!tex) { tex = gl.createTexture(); } gl.bindTexture(gl.TEXTURE_2D, tex); gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); // CLAMP + fract() in shader: WebGL1 forbids REPEAT on NPOT (stone 400²). 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.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, htmlImg); if (which === "stone") { this._stoneTex = tex; this._stoneSrc = htmlImg; } else { this._woodTex = tex; this._woodSrc = htmlImg; } return tex; }, _ensureWhite: function (glc) { if (this._whiteTex) { return this._whiteTex; } var c = document.createElement("canvas"); c.width = 1; c.height = 1; var ctx = c.getContext("2d"); ctx.fillStyle = "#ffffff"; ctx.fillRect(0, 0, 1, 1); this._whiteTex = glc.getTexture(c); return this._whiteTex; }, invalidateGrid: function () { this._gridKey = ""; }, draw: function (glc, screenW, screenH, player, level, floorImages, cellSize) { if (!glc || !level || !floorImages || !floorImages.stone || !floorImages.wood) { return false; } if (typeof Shader === "undefined") { return false; } cellSize = cellSize || 32; var gl = glc.gl; var shader = this._ensureShader(); this._uploadGrid(gl, level); this._uploadAlbedo(gl, floorImages.stone, "stone"); this._uploadAlbedo(gl, floorImages.wood, "wood"); var white = this._ensureWhite(glc); var f = Camera.forward(player.rotation); var plane = Camera.plane(player.rotation); var lights = level.lights || []; var n = Math.min(lights.length, this.MAX_LIGHTS); var uniforms = { u_resolution: [screenW, screenH], u_player: [player.x, player.y], u_dir: [f.x, f.y], u_plane: [plane.x, plane.y], u_pitch: player.pitch || 0, u_cellSize: cellSize, u_posZ: (typeof FloorCast !== "undefined") ? FloorCast.posZ(screenH, cellSize) : (cellSize * 0.5) * screenH, u_levelSize: [level.width, level.height], u_ambient: level.ambient != null ? level.ambient : 0.38, u_lightCount: n }; var i; for (i = 0; i < this.MAX_LIGHTS; i++) { if (i < n) { var L = lights[i]; uniforms["u_l" + i] = [ L.x, L.y, L.z != null ? L.z : 16, L.intensity == null ? 1 : L.intensity ]; uniforms["u_c" + i] = [ L.color ? L.color[0] : 1, L.color ? L.color[1] : 1, L.color ? L.color[2] : 1 ]; uniforms["u_a" + i] = [ L.range || 64, L.falloff == null ? 2 : L.falloff ]; } else { uniforms["u_l" + i] = [0, 0, 0, 0]; uniforms["u_c" + i] = [0, 0, 0]; uniforms["u_a" + i] = [1, 2]; } } glc.drawSprite({ texture: white, shader: shader, x: screenW / 2, y: screenH / 2, w: screenW, h: screenH, rotationDeg: 0, alpha: 1, uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 }, textures: { u_stone: this._stoneTex, u_wood: this._woodTex, u_grid: this._gridTex }, uniforms: uniforms }); return true; } }; if (typeof module !== "undefined" && module.exports) { module.exports = FloorGpu; }