diff --git a/src/graphics/graphics-library/texture/palette-texture.ts b/src/graphics/graphics-library/texture/palette-texture.ts index faee8a1..b60699e 100644 --- a/src/graphics/graphics-library/texture/palette-texture.ts +++ b/src/graphics/graphics-library/texture/palette-texture.ts @@ -14,21 +14,27 @@ export class PaletteTexture extends Texture { } public setPalette(colors: Array) { - const canvas = document.createElement('canvas'); - canvas.width = this.paletteSize; - canvas.height = 1; - - const ctx = canvas.getContext('2d')!; - const imageData = ctx.createImageData(this.paletteSize, 1); + const data = new Uint8Array(this.paletteSize * 4); + const toByte = (v: number) => Math.min(255, Math.max(0, Math.round(v * 255))); colors.forEach((c, i) => { - imageData.data[4 * i + 0] = c[0] * 255; - imageData.data[4 * i + 1] = c[1] * 255; - imageData.data[4 * i + 2] = c[2] * 255; - imageData.data[4 * i + 3] = c.length == 4 ? c[3] * 255 : 255; + data[4 * i + 0] = toByte(c[0]); + data[4 * i + 1] = toByte(c[1]); + data[4 * i + 2] = toByte(c[2]); + data[4 * i + 3] = c.length == 4 ? toByte(c[3]) : 255; }); - ctx.putImageData(imageData, 0, 0); - this.setImage(canvas); + this.bind(); + this.gl.texImage2D( + this.gl.TEXTURE_2D, + 0, + this.gl.RGBA, + this.paletteSize, + 1, + 0, + this.gl.RGBA, + this.gl.UNSIGNED_BYTE, + data + ); } } diff --git a/src/graphics/rendering/render-pass/lights-render-pass.ts b/src/graphics/rendering/render-pass/lights-render-pass.ts index 8adcb29..ac1eaa1 100644 --- a/src/graphics/rendering/render-pass/lights-render-pass.ts +++ b/src/graphics/rendering/render-pass/lights-render-pass.ts @@ -22,18 +22,14 @@ export class LightsRenderPass extends RenderPass { commonUniforms.uvToWorld ); + const halfViewAreaX = commonUniforms.worldAreaInView.x / 2; + const halfViewAreaY = commonUniforms.worldAreaInView.y / 2; + const drawablesNearTile = this.drawables.filter((l) => { - const d = vec2.subtract( - vec2.create(), - [ - Math.abs(l.center.x - tileCenterWorldCoordinates.x), - Math.abs(l.center.y - tileCenterWorldCoordinates.y), - ], - vec2.scale(vec2.create(), commonUniforms.worldAreaInView, 0.5) - ); + const dX = Math.abs(l.center.x - tileCenterWorldCoordinates.x) - halfViewAreaX; + const dY = Math.abs(l.center.y - tileCenterWorldCoordinates.y) - halfViewAreaY; const distance = - vec2.length([Math.max(d.x, 0), Math.max(d.y, 0)]) + - Math.min(Math.max(d.x, d.y), 0.0); + Math.hypot(Math.max(dX, 0), Math.max(dY, 0)) + Math.min(Math.max(dX, dY), 0); l.setLightnessRatio(clamp01(1 - distance / this.lightCutoffDistance)); return distance < this.lightCutoffDistance;