From fe4f8e09e352ef8058ab30d8e9fc22ca16f777a1 Mon Sep 17 00:00:00 2001 From: Bill Date: Tue, 31 Aug 2021 23:47:28 -0400 Subject: [PATCH] Update avatar palette --- src/core/utils/PaletteMapFilter.ts | 46 +++++++++++++++++------------- src/nitro/avatar/AvatarImage.ts | 19 ++++++------ 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/core/utils/PaletteMapFilter.ts b/src/core/utils/PaletteMapFilter.ts index a95502fc..957054bb 100644 --- a/src/core/utils/PaletteMapFilter.ts +++ b/src/core/utils/PaletteMapFilter.ts @@ -19,25 +19,27 @@ uniform int channel; void main(void) { vec4 currentColor = texture2D(uSampler, vTextureCoord); - if(channel == 0) + vec4 adjusted = currentColor; + + if(currentColor.a > 0.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); + if(channel == 0) + { + vec2 index = vec2(int(currentColor.r), 0.5); + adjusted = texture2D(lut, index); + } else if(channel == 1) { + vec2 index = vec2(int(currentColor.g), 0.5); + adjusted = texture2D(lut, index); + } else if(channel == 2) { + vec2 index = vec2(int(currentColor.b), 0.5); + adjusted = texture2D(lut, index); + } else if(channel == 3) { + vec2 index = vec2(int(currentColor.a), 0.5); + adjusted = texture2D(lut, index); + } } - + + gl_FragColor = vec4(adjusted.r, adjusted.g, adjusted.b, currentColor.a); }`; export class PaletteMapFilter extends NitroFilter @@ -50,15 +52,19 @@ export class PaletteMapFilter extends NitroFilter super(vertex, fragment); 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._lut = NitroTexture.fromBuffer(Float32Array.from(lut), lut.length / 4, 1); + + // this._lut.baseTexture.mipmap = MIPMAP_MODES.ON; + // this._lut.baseTexture.wrapMode = WRAP_MODES.CLAMP; + this.uniforms.lut = this._lut; this.uniforms.channel = this._channel; - this.uniforms.lut.baseTexture.mipmap = false; } private getLutForPalette(data: number[]): number[] { const lut = []; + for(let i = 0; i < data.length; i++) { // R @@ -70,7 +76,7 @@ export class PaletteMapFilter extends NitroFilter // A lut[(i * 4) + 3] = ((data[i] >> 24) & 0xFF) / 0xff; } - console.log(lut); + return lut; } diff --git a/src/nitro/avatar/AvatarImage.ts b/src/nitro/avatar/AvatarImage.ts index df7c47d5..919c6f33 100644 --- a/src/nitro/avatar/AvatarImage.ts +++ b/src/nitro/avatar/AvatarImage.ts @@ -5,7 +5,6 @@ import { Rectangle } from '@pixi/math'; import { Sprite } from '@pixi/sprite'; import { NitroContainer, NitroSprite } from '../../core'; import { AdvancedMap } from '../../core/utils/AdvancedMap'; -import { PaletteMapFilter } from '../../core/utils/PaletteMapFilter'; import { IGraphicAsset } from '../../room/object/visualization/utils/IGraphicAsset'; import { TextureUtils } from '../../room/utils/TextureUtils'; import { Nitro } from '../Nitro'; @@ -396,7 +395,7 @@ export class AvatarImage implements IAvatarImage, IAvatarEffectListener { this.convertToGrayscale(container); - container.filters.push(new PaletteMapFilter(this._avatarSpriteData.reds, [], [], [])); + //container.filters.push(new PaletteMapFilter(this._avatarSpriteData.reds, [], [], [])); } if(!cache) @@ -418,13 +417,13 @@ export class AvatarImage implements IAvatarImage, IAvatarEffectListener if(!this._reusableTexture) return null; - // if(this._avatarSpriteData) - // { - // if(this._avatarSpriteData.paletteIsGrayscale) - // { - // this._reusableTexture = this.applyPalette(this._reusableTexture, this._avatarSpriteData.reds, [], []); - // } - // } + if(this._avatarSpriteData) + { + if(this._avatarSpriteData.paletteIsGrayscale) + { + this._reusableTexture = this.applyPalette(this._reusableTexture, this._avatarSpriteData.reds, [], []); + } + } this._image = this._reusableTexture; this._changes = false; @@ -434,7 +433,6 @@ export class AvatarImage implements IAvatarImage, IAvatarEffectListener public applyPalette(texture: RenderTexture, reds: number[] = [], greens: number[] = [], blues: number[] = []): RenderTexture { - const textureCanvas = TextureUtils.generateCanvas(texture); const textureCtx = textureCanvas.getContext('2d'); const textureImageData = textureCtx.getImageData(0, 0, textureCanvas.width, textureCanvas.height); @@ -470,7 +468,6 @@ export class AvatarImage implements IAvatarImage, IAvatarEffectListener data[ i + 1] = ((paletteColor >> 8) & 0xFF); data[ i + 2] = (paletteColor & 0xFF); } - } textureCtx.putImageData(textureImageData, 0, 0);