/** * Fullscreen WebGL raycast view on frameworkv3. * Wall strips: one source texel column per screen column, NEAREST, tinted for shading. */ class RaycastScene extends Scene { constructor (width, height) { super(0, 0, width, height, Scene.DisplayModes.absolute); this.backgroundColor = { r: 0, g: 0, b: 0 }; this.tintShader = null; this.solidTexture = null; this.gunImages = null; this._lookBound = false; this.floorTexMap = null; this._floorCanvas = null; this._floorCtx = null; this._floorTex = null; } onLoad () { const gl = this.glContext.gl; this.glContext.setDefaultTextureFilter(gl.NEAREST, gl.NEAREST); this.tintShader = new Shader(Shader.DEFAULT_VERT, [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform float u_alpha;", "uniform vec3 u_tint;", "varying vec2 v_texCoord;", "void main() {", " vec4 color = texture2D(u_texture, v_texCoord);", " gl_FragColor = vec4(color.rgb * u_tint, color.a * u_alpha);", "}" ].join("\n")); // 1x1 white for solid (untextured) walls. const c = document.createElement("canvas"); c.width = 1; c.height = 1; const ctx = c.getContext("2d"); ctx.fillStyle = "#ffffff"; ctx.fillRect(0, 0, 1, 1); this.solidTexture = this.glContext.getTexture(c); this.bindPointerLock(); } bindPointerLock () { if (this._lookBound) { return; } this._lookBound = true; const canvas = this.cv_canvas; canvas.addEventListener("click", function () { if (canvas.requestPointerLock) { canvas.requestPointerLock(); } }); document.addEventListener("mousemove", function (e) { if (document.pointerLockElement === canvas) { Player.applyLookDelta(e.movementX || 0, e.movementY || 0); } }); } update (deltaTime) { super.update(deltaTime); Player.update(deltaTime); if (typeof NpcSystem !== "undefined") { NpcSystem.update(deltaTime); } if (Player.horizontalResolution > this.real_size.x) { Player.horizontalResolution = this.real_size.x; } } draw () { if (this.destroyed || !this.glContext) { return; } this.g_fixSize(); const glc = this.glContext; glc.endOffscreenPass(); glc.clear(0, 0, 0, 1); this.drawFloorCeiling(); const wallDepth = this.drawColumns(); if (typeof NpcSystem !== "undefined" && NpcSystem.ready) { NpcSystem.draw(this, Player, wallDepth); } this.drawGun(); } setFloorTextures (texMap) { this.floorTexMap = texMap; } drawFloorCeiling () { if (!this.floorTexMap || !World.level || !World.level.floors) { return; } const w = Math.max(1, Math.round(this.real_size.x)); const h = Math.max(1, Math.round(this.real_size.y)); if (!this._floorCanvas || this._floorCanvas.width !== w || this._floorCanvas.height !== h) { this._floorCanvas = document.createElement("canvas"); this._floorCanvas.width = w; this._floorCanvas.height = h; this._floorCtx = this._floorCanvas.getContext("2d"); this._floorTex = null; } const img = this._floorCtx.createImageData(w, h); FloorCast.render(img, w, h, Player, World.level, this.floorTexMap, World.size); this._floorCtx.putImageData(img, 0, 0); const glc = this.glContext; const gl = glc.gl; if (this._floorTex) { gl.bindTexture(gl.TEXTURE_2D, this._floorTex); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._floorCanvas); } else { glc.setDefaultTextureFilter(gl.NEAREST, gl.NEAREST); this._floorTex = glc.getTexture(this._floorCanvas); } glc.drawSprite({ texture: this._floorTex, x: w / 2, y: h / 2, w: w, h: h, rotationDeg: 0, alpha: 1, uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 } }); } faceLight (dir) { switch (dir) { case "east": return 0.9; case "west": return 0.8; case "south": return 0.7; default: return 1.0; } } drawColumns () { const glc = this.glContext; const w = this.real_size.x; const h = this.real_size.y; const cols = Math.max(1, Math.floor(Player.horizontalResolution)); const rad = Math.PI / (cols * 2); const widthLine = w / cols; const wallDepth = new Float32Array(Math.max(1, Math.round(w))); for (var zi = 0; zi < wallDepth.length; zi++) { wallDepth[zi] = 1e9; } for (var i = 0; i < cols; i++) { var offsetP = -0.5 + (i / cols); var offset = offsetP * cols; var rotationalOffset = offset * rad; var rot = Player.rotation + rotationalOffset; if (rot < 0) { rot += Math.PI * 2; } if (rot > Math.PI * 2) { rot -= Math.PI * 2; } var rayHit = World.rayCast(Player.x, Player.y, rot); if (!rayHit || !rayHit.tile) { continue; } var light = this.faceLight(rayHit.dir); var deltaAngle = Player.rotation - rot; var dist = rayHit.distance * Math.cos(deltaAngle); if (dist < 0.0001) { dist = 0.0001; } var xLine = i * widthLine; var wallH = (World.size * h) / dist; // Same horizon/pitch as FloorCast + NPC feet. var yCenter = (h / 2) + (-Player.pitch * h * 0.25); var tint = [light, light, light]; var htmlImage = null; var texW = 1; // Z-buffer for NPC occlusion (per screen pixel column). var x0 = Math.max(0, Math.floor(xLine)); var x1 = Math.min(wallDepth.length, Math.ceil(xLine + widthLine)); for (var xz = x0; xz < x1; xz++) { if (dist < wallDepth[xz]) { wallDepth[xz] = dist; } } if (rayHit.tile.Image && rayHit.tile.Image.g_image) { htmlImage = rayHit.tile.Image.g_image; texW = htmlImage.width || 1; var uv = textureColumnUv(rayHit.tileX, texW); glc.drawSprite({ texture: glc.getTexture(htmlImage), x: xLine + widthLine / 2, y: yCenter, w: widthLine, h: wallH, rotationDeg: 0, alpha: 1, uvRect: { u0: uv.u0, v0: uv.v0, u1: uv.u1, v1: uv.v1 }, shader: this.tintShader, uniforms: { u_tint: tint } }); } else { var c = rayHit.tile.Color || { R: 255, G: 255, B: 255 }; tint = [(c.R / 255) * light, (c.G / 255) * light, (c.B / 255) * light]; glc.drawSprite({ texture: this.solidTexture, x: xLine + widthLine / 2, y: yCenter, w: widthLine, h: wallH, rotationDeg: 0, alpha: 1, uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 }, shader: this.tintShader, uniforms: { u_tint: tint } }); } } return wallDepth; } drawGun () { const images = this.game && this.game.images; if (!images) { return; } var gunImg = null; switch (Player.currentGun) { case 0: gunImg = images.shotgun; break; case 1: gunImg = images.machinegun; break; case 2: gunImg = images.pulserifle; break; } if (!gunImg || !gunImg.g_image) { return; } var p = this.real_size.y / 1080; var gw = p * gunImg.g_image.width; var gh = p * gunImg.g_image.height; var gx = this.real_size.x * Player.gunX + gw / 2; var gy = this.real_size.y * Player.gunY + gh / 2; gunImg.draw( this, gx, gy, gw, gh, 0, false, false, 1, 0 ); } }