| 1 | /** |
| 2 | * Fullscreen WebGL raycast view on frameworkv3. |
| 3 | * Wall strips: one source texel column per screen column, NEAREST, tinted for shading. |
| 4 | */ |
| 5 | class RaycastScene extends Scene |
| 6 | { |
| 7 | constructor (width, height) |
| 8 | { |
| 9 | super(0, 0, width, height, Scene.DisplayModes.absolute); |
| 10 | this.backgroundColor = { r: 0, g: 0, b: 0 }; |
| 11 | this.tintShader = null; |
| 12 | this.solidTexture = null; |
| 13 | this.gunImages = null; |
| 14 | this._lookBound = false; |
| 15 | this.floorTexMap = null; |
| 16 | this.floorImages = null; |
| 17 | this._floorCanvas = null; |
| 18 | this._floorCtx = null; |
| 19 | this._floorTex = null; |
| 20 | } |
| 21 | |
| 22 | onLoad () |
| 23 | { |
| 24 | const gl = this.glContext.gl; |
| 25 | this.glContext.setDefaultTextureFilter(gl.NEAREST, gl.NEAREST); |
| 26 | |
| 27 | this.tintShader = new Shader(Shader.DEFAULT_VERT, [ |
| 28 | "precision mediump float;", |
| 29 | "uniform sampler2D u_texture;", |
| 30 | "uniform float u_alpha;", |
| 31 | "uniform vec3 u_tint;", |
| 32 | "varying vec2 v_texCoord;", |
| 33 | "void main() {", |
| 34 | " vec4 color = texture2D(u_texture, v_texCoord);", |
| 35 | " gl_FragColor = vec4(color.rgb * u_tint, color.a * u_alpha);", |
| 36 | "}" |
| 37 | ].join("\n")); |
| 38 | |
| 39 | // 1x1 white for solid (untextured) walls. |
| 40 | const c = document.createElement("canvas"); |
| 41 | c.width = 1; |
| 42 | c.height = 1; |
| 43 | const ctx = c.getContext("2d"); |
| 44 | ctx.fillStyle = "#ffffff"; |
| 45 | ctx.fillRect(0, 0, 1, 1); |
| 46 | this.solidTexture = this.glContext.getTexture(c); |
| 47 | |
| 48 | this.bindPointerLock(); |
| 49 | } |
| 50 | |
| 51 | bindPointerLock () |
| 52 | { |
| 53 | if (this._lookBound) |
| 54 | { return; } |
| 55 | this._lookBound = true; |
| 56 | const canvas = this.cv_canvas; |
| 57 | canvas.addEventListener("click", function () |
| 58 | { |
| 59 | if (canvas.requestPointerLock) |
| 60 | { canvas.requestPointerLock(); } |
| 61 | }); |
| 62 | document.addEventListener("mousemove", function (e) |
| 63 | { |
| 64 | if (document.pointerLockElement === canvas) |
| 65 | { Player.applyLookDelta(e.movementX || 0, e.movementY || 0); } |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | update (deltaTime) |
| 70 | { |
| 71 | super.update(deltaTime); |
| 72 | Player.update(deltaTime); |
| 73 | if (typeof NpcSystem !== "undefined") |
| 74 | { NpcSystem.update(deltaTime); } |
| 75 | if (Player.horizontalResolution > this.real_size.x) |
| 76 | { Player.horizontalResolution = this.real_size.x; } |
| 77 | } |
| 78 | |
| 79 | draw () |
| 80 | { |
| 81 | if (this.destroyed || !this.glContext) |
| 82 | { return; } |
| 83 | |
| 84 | this.g_fixSize(); |
| 85 | const glc = this.glContext; |
| 86 | glc.endOffscreenPass(); |
| 87 | glc.clear(0, 0, 0, 1); |
| 88 | |
| 89 | this.drawFloorCeiling(); |
| 90 | const wallDepth = this.drawColumns(); |
| 91 | if (typeof NpcSystem !== "undefined" && NpcSystem.ready) |
| 92 | { NpcSystem.draw(this, Player, wallDepth); } |
| 93 | this.drawGun(); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | setFloorTextures (texMap) |
| 98 | { |
| 99 | this.floorTexMap = texMap; |
| 100 | } |
| 101 | |
| 102 | setFloorImages (imgs) |
| 103 | { |
| 104 | this.floorImages = imgs; |
| 105 | if (typeof FloorGpu !== "undefined") |
| 106 | { FloorGpu.invalidateGrid(); } |
| 107 | } |
| 108 | |
| 109 | drawFloorCeiling () |
| 110 | { |
| 111 | if (!World.level || !World.level.floors) |
| 112 | { return; } |
| 113 | const w = Math.max(1, Math.round(this.real_size.x)); |
| 114 | const h = Math.max(1, Math.round(this.real_size.y)); |
| 115 | const glc = this.glContext; |
| 116 | if (typeof FloorGpu !== "undefined" && this.floorImages |
| 117 | && FloorGpu.draw(glc, w, h, Player, World.level, this.floorImages, World.size)) |
| 118 | { return; } |
| 119 | // CPU fallback (tests / missing GPU sources) |
| 120 | if (!this.floorTexMap) |
| 121 | { return; } |
| 122 | if (!this._floorCanvas || this._floorCanvas.width !== w || this._floorCanvas.height !== h) |
| 123 | { |
| 124 | this._floorCanvas = document.createElement("canvas"); |
| 125 | this._floorCanvas.width = w; |
| 126 | this._floorCanvas.height = h; |
| 127 | this._floorCtx = this._floorCanvas.getContext("2d"); |
| 128 | this._floorTex = null; |
| 129 | } |
| 130 | const img = this._floorCtx.createImageData(w, h); |
| 131 | FloorCast.render(img, w, h, Player, World.level, this.floorTexMap, World.size); |
| 132 | this._floorCtx.putImageData(img, 0, 0); |
| 133 | const gl = glc.gl; |
| 134 | if (this._floorTex) |
| 135 | { |
| 136 | gl.bindTexture(gl.TEXTURE_2D, this._floorTex); |
| 137 | gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._floorCanvas); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | glc.setDefaultTextureFilter(gl.NEAREST, gl.NEAREST); |
| 142 | this._floorTex = glc.getTexture(this._floorCanvas); |
| 143 | } |
| 144 | glc.drawSprite({ |
| 145 | texture: this._floorTex, |
| 146 | x: w / 2, |
| 147 | y: h / 2, |
| 148 | w: w, |
| 149 | h: h, |
| 150 | rotationDeg: 0, |
| 151 | alpha: 1, |
| 152 | uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 } |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | faceLight (dir) |
| 157 | { |
| 158 | switch (dir) |
| 159 | { |
| 160 | case "east": return 0.9; |
| 161 | case "west": return 0.8; |
| 162 | case "south": return 0.7; |
| 163 | default: return 1.0; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | drawColumns () |
| 168 | { |
| 169 | const glc = this.glContext; |
| 170 | const w = this.real_size.x; |
| 171 | const h = this.real_size.y; |
| 172 | const cols = Math.max(1, Math.floor(Player.horizontalResolution)); |
| 173 | const widthLine = w / cols; |
| 174 | const wallDepth = new Float32Array(Math.max(1, Math.round(w))); |
| 175 | for (var zi = 0; zi < wallDepth.length; zi++) |
| 176 | { wallDepth[zi] = 1e9; } |
| 177 | |
| 178 | for (var i = 0; i < cols; i++) |
| 179 | { |
| 180 | var camX = Camera.camXFromColumn(i, cols); |
| 181 | var rot = Camera.rayAngle(Player.rotation, camX); |
| 182 | if (rot < 0) { rot += Math.PI * 2; } |
| 183 | if (rot > Math.PI * 2) { rot -= Math.PI * 2; } |
| 184 | |
| 185 | var rayHit = World.rayCast(Player.x, Player.y, rot); |
| 186 | if (!rayHit || !rayHit.tile) |
| 187 | { continue; } |
| 188 | |
| 189 | // Perpendicular (camera-plane) depth — same space as NPC z-buffer. |
| 190 | var dist = Camera.perpFromEuclid(rayHit.distance, Player.rotation, camX); |
| 191 | if (dist < 0.0001) |
| 192 | { dist = 0.0001; } |
| 193 | |
| 194 | var xLine = i * widthLine; |
| 195 | var wallH = (World.size * h) / dist; |
| 196 | // Same horizon/pitch as FloorCast + NPC feet. |
| 197 | var yCenter = (h / 2) + (-Player.pitch * h * 0.25); |
| 198 | |
| 199 | var tint; |
| 200 | var lights = World.level && World.level.lights; |
| 201 | if (typeof Lighting !== "undefined" && lights && lights.length) |
| 202 | { |
| 203 | var nrm = Lighting.faceNormal(rayHit.dir); |
| 204 | // Nudge into open space so LOS does not treat the wall cell as a blocker. |
| 205 | var hitPos = { |
| 206 | x: rayHit.endX + nrm.x * 0.6, |
| 207 | y: rayHit.endY + nrm.y * 0.6, |
| 208 | z: World.size * 0.5 |
| 209 | }; |
| 210 | var ambient = World.level.ambient != null ? World.level.ambient : Lighting.DEFAULT_AMBIENT; |
| 211 | var los = { |
| 212 | tiles: World.level.tiles, |
| 213 | width: World.level.width, |
| 214 | height: World.level.height, |
| 215 | cellSize: World.size |
| 216 | }; |
| 217 | var lit = Lighting.shadeRgb(hitPos, nrm, lights, ambient, los); |
| 218 | var face = this.faceLight(rayHit.dir); |
| 219 | tint = [ |
| 220 | Math.min(2.2, lit.r * (0.65 + 0.35 * face)), |
| 221 | Math.min(2.2, lit.g * (0.65 + 0.35 * face)), |
| 222 | Math.min(2.2, lit.b * (0.65 + 0.35 * face)) |
| 223 | ]; |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | var light = this.faceLight(rayHit.dir); |
| 228 | tint = [light, light, light]; |
| 229 | } |
| 230 | var htmlImage = null; |
| 231 | var texW = 1; |
| 232 | |
| 233 | // Z-buffer for NPC occlusion (per screen pixel column). |
| 234 | var x0 = Math.max(0, Math.floor(xLine)); |
| 235 | var x1 = Math.min(wallDepth.length, Math.ceil(xLine + widthLine)); |
| 236 | for (var xz = x0; xz < x1; xz++) |
| 237 | { |
| 238 | if (dist < wallDepth[xz]) |
| 239 | { wallDepth[xz] = dist; } |
| 240 | } |
| 241 | |
| 242 | if (rayHit.tile.Image && rayHit.tile.Image.g_image) |
| 243 | { |
| 244 | htmlImage = rayHit.tile.Image.g_image; |
| 245 | texW = htmlImage.width || 1; |
| 246 | var uv = textureColumnUv(rayHit.tileX, texW); |
| 247 | glc.drawSprite({ |
| 248 | texture: glc.getTexture(htmlImage), |
| 249 | x: xLine + widthLine / 2, |
| 250 | y: yCenter, |
| 251 | w: widthLine, |
| 252 | h: wallH, |
| 253 | rotationDeg: 0, |
| 254 | alpha: 1, |
| 255 | uvRect: { u0: uv.u0, v0: uv.v0, u1: uv.u1, v1: uv.v1 }, |
| 256 | shader: this.tintShader, |
| 257 | uniforms: { u_tint: tint } |
| 258 | }); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | var c = rayHit.tile.Color || { R: 255, G: 255, B: 255 }; |
| 263 | tint = [(c.R / 255) * light, (c.G / 255) * light, (c.B / 255) * light]; |
| 264 | glc.drawSprite({ |
| 265 | texture: this.solidTexture, |
| 266 | x: xLine + widthLine / 2, |
| 267 | y: yCenter, |
| 268 | w: widthLine, |
| 269 | h: wallH, |
| 270 | rotationDeg: 0, |
| 271 | alpha: 1, |
| 272 | uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 }, |
| 273 | shader: this.tintShader, |
| 274 | uniforms: { u_tint: tint } |
| 275 | }); |
| 276 | } |
| 277 | } |
| 278 | return wallDepth; |
| 279 | } |
| 280 | |
| 281 | drawGun () |
| 282 | { |
| 283 | const images = this.game && this.game.images; |
| 284 | if (!images) |
| 285 | { return; } |
| 286 | var gunImg = null; |
| 287 | switch (Player.currentGun) |
| 288 | { |
| 289 | case 0: gunImg = images.shotgun; break; |
| 290 | case 1: gunImg = images.machinegun; break; |
| 291 | case 2: gunImg = images.pulserifle; break; |
| 292 | } |
| 293 | if (!gunImg || !gunImg.g_image) |
| 294 | { return; } |
| 295 | |
| 296 | var p = this.real_size.y / 1080; |
| 297 | var gw = p * gunImg.g_image.width; |
| 298 | var gh = p * gunImg.g_image.height; |
| 299 | var gx = this.real_size.x * Player.gunX + gw / 2; |
| 300 | var gy = this.real_size.y * Player.gunY + gh / 2; |
| 301 | |
| 302 | gunImg.draw( |
| 303 | this, |
| 304 | gx, |
| 305 | gy, |
| 306 | gw, |
| 307 | gh, |
| 308 | 0, |
| 309 | false, |
| 310 | false, |
| 311 | 1, |
| 312 | 0 |
| 313 | ); |
| 314 | } |
| 315 | } |