Simplify logic

This commit is contained in:
Andras Schmelczer 2026-06-10 21:41:13 +01:00
parent 0da4144b9d
commit cbd97605a5
2 changed files with 24 additions and 22 deletions

View file

@ -14,21 +14,27 @@ export class PaletteTexture extends Texture {
} }
public setPalette(colors: Array<vec3 | vec4>) { public setPalette(colors: Array<vec3 | vec4>) {
const canvas = document.createElement('canvas'); const data = new Uint8Array(this.paletteSize * 4);
canvas.width = this.paletteSize; const toByte = (v: number) => Math.min(255, Math.max(0, Math.round(v * 255)));
canvas.height = 1;
const ctx = canvas.getContext('2d')!;
const imageData = ctx.createImageData(this.paletteSize, 1);
colors.forEach((c, i) => { colors.forEach((c, i) => {
imageData.data[4 * i + 0] = c[0] * 255; data[4 * i + 0] = toByte(c[0]);
imageData.data[4 * i + 1] = c[1] * 255; data[4 * i + 1] = toByte(c[1]);
imageData.data[4 * i + 2] = c[2] * 255; data[4 * i + 2] = toByte(c[2]);
imageData.data[4 * i + 3] = c.length == 4 ? c[3] * 255 : 255; 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
);
} }
} }

View file

@ -22,18 +22,14 @@ export class LightsRenderPass extends RenderPass {
commonUniforms.uvToWorld commonUniforms.uvToWorld
); );
const halfViewAreaX = commonUniforms.worldAreaInView.x / 2;
const halfViewAreaY = commonUniforms.worldAreaInView.y / 2;
const drawablesNearTile = this.drawables.filter((l) => { const drawablesNearTile = this.drawables.filter((l) => {
const d = vec2.subtract( const dX = Math.abs(l.center.x - tileCenterWorldCoordinates.x) - halfViewAreaX;
vec2.create(), const dY = Math.abs(l.center.y - tileCenterWorldCoordinates.y) - halfViewAreaY;
[
Math.abs(l.center.x - tileCenterWorldCoordinates.x),
Math.abs(l.center.y - tileCenterWorldCoordinates.y),
],
vec2.scale(vec2.create(), commonUniforms.worldAreaInView, 0.5)
);
const distance = const distance =
vec2.length([Math.max(d.x, 0), Math.max(d.y, 0)]) + Math.hypot(Math.max(dX, 0), Math.max(dY, 0)) + Math.min(Math.max(dX, dY), 0);
Math.min(Math.max(d.x, d.y), 0.0);
l.setLightnessRatio(clamp01(1 - distance / this.lightCutoffDistance)); l.setLightnessRatio(clamp01(1 - distance / this.lightCutoffDistance));
return distance < this.lightCutoffDistance; return distance < this.lightCutoffDistance;