From fac319a6ddb070a5968d8881349dde657c8661ac Mon Sep 17 00:00:00 2001 From: dank074 Date: Tue, 31 Aug 2021 19:21:36 -0500 Subject: [PATCH] pls help bill --- src/core/utils/PaletteMapFilter.ts | 77 ++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/src/core/utils/PaletteMapFilter.ts b/src/core/utils/PaletteMapFilter.ts index 81e48196..a95502fc 100644 --- a/src/core/utils/PaletteMapFilter.ts +++ b/src/core/utils/PaletteMapFilter.ts @@ -1,4 +1,4 @@ -import { NitroFilter } from './proxy'; +import { NitroFilter, NitroTexture } from './proxy'; const vertex = ` attribute vec2 aVertexPosition; @@ -14,52 +14,81 @@ void main(void) const fragment = ` varying vec2 vTextureCoord; uniform sampler2D uSampler; -uint8_t reds[256]; -uint8_t greens[256]; -uint8_t blues[256]; -uint8_t alphas[256]; +uniform sampler2D lut; +uniform int channel; void main(void) { vec4 currentColor = texture2D(uSampler, vTextureCoord); - vec4 newColor = + if(channel == 0) + { + vec2 index = vec2(int(currentColor.r * 255.0), 0.5); + vec4 newColor = texture2D(lut, index); + gl_FragColor = vec4(newColor.r, newColor.g, newColor.b, currentColor.a); + } else if(channel == 1) { + vec2 index = vec2(int(currentColor.g * 255.0), 0.5); + vec4 newColor = texture2D(lut, index); + gl_FragColor = vec4(newColor.r, newColor.g, newColor.b, currentColor.a); + } else if(channel == 2) { + vec2 index = vec2(int(currentColor.b * 255.0), 0.5); + vec4 newColor = texture2D(lut, index); + gl_FragColor = vec4(newColor.r, newColor.g, newColor.b, currentColor.a); + } else if(channel == 3) { + vec2 index = vec2(int(currentColor.a * 255.0), 0.5); + vec4 newColor = texture2D(lut, index); + gl_FragColor = vec4(newColor.r, newColor.g, newColor.b, currentColor.a); + } - gl_FragColor = vec4(newColor.r, newColor.g, newColor.b, newColor.a); }`; export class PaletteMapFilter extends NitroFilter { - private _reds: number[]; - private _greens: number[]; - private _blues: number[]; - private _alphas: number[]; + private _lut: NitroTexture; + private _channel: number; constructor(reds: number[], greens: number[], blues: number[], alphas: number[]) { super(vertex, fragment); - - this._reds = reds; - this._greens = greens; - this._blues = blues; - this._alphas = alphas; + this._channel = this.getChannelForPalette(reds, greens, blues, alphas); + const lut = this.getLutForPalette(reds); + this._lut = NitroTexture.fromBuffer(Uint8Array.from(lut), lut.length / 4, 1); + this.uniforms.lut = this._lut; + this.uniforms.channel = this._channel; + this.uniforms.lut.baseTexture.mipmap = false; } - public get reds(): number[] + private getLutForPalette(data: number[]): number[] { - return this._reds; + const lut = []; + for(let i = 0; i < data.length; i++) + { + // R + lut[(i * 4)] = ((data[i] >> 16) & 0xFF) / 0xff; + // G + lut[(i * 4) + 1] = ((data[i] >> 8) & 0xFF) / 0xff; + // B + lut[(i * 4) + 2] = (data[i] & 0xFF) / 0xff; + // A + lut[(i * 4) + 3] = ((data[i] >> 24) & 0xFF) / 0xff; + } + console.log(lut); + return lut; } - public get blues(): number[] + private getChannelForPalette(reds: number[], greens: number[], blues: number[], alphas: number[]): number { - return this._blues; + if(reds.length === 256) return 0; + if(greens.length === 256) return 1; + if(blues.length === 256) return 2; + if(alphas.length === 256) return 3; } - public get greens(): number[] + public get lut(): NitroTexture { - return this._greens; + return this._lut; } - public get alphas(): number[] + public get channel(): number { - return this._alphas; + return this._channel; } }