From e96869d7eb1d02063c0a33c33967d65a78041701 Mon Sep 17 00:00:00 2001 From: billsonnn Date: Tue, 26 Mar 2024 22:10:07 -0400 Subject: [PATCH] Fix pet palettes --- packages/assets/src/GraphicAssetPalette.ts | 22 +++++++++---------- .../room/src/renderer/utils/ExtendedSprite.ts | 8 ++++--- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/assets/src/GraphicAssetPalette.ts b/packages/assets/src/GraphicAssetPalette.ts index a0e84048..57652fd7 100644 --- a/packages/assets/src/GraphicAssetPalette.ts +++ b/packages/assets/src/GraphicAssetPalette.ts @@ -1,5 +1,5 @@ import { IGraphicAssetPalette } from '@nitrots/api'; -import { TextureUtils } from '@nitrots/utils'; +import { GetRenderer } from '@nitrots/utils'; import { Texture } from 'pixi.js'; export class GraphicAssetPalette implements IGraphicAssetPalette @@ -20,18 +20,13 @@ export class GraphicAssetPalette implements IGraphicAssetPalette public applyPalette(texture: Texture): Texture { - const pixelOutput = TextureUtils.getPixels(texture); - const pixels = pixelOutput?.pixels; - - if(!pixels) return texture; - - const canvas = document.createElement('canvas'); + const canvas = GetRenderer().texture.generateCanvas(texture); const ctx = canvas.getContext('2d'); - const imageData = ctx.createImageData(texture.width, texture.height); + const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); - for(let i = 0; i < pixels.length; i += 4) + for(let i = 0; i < imageData.data.length; i += 4) { - let paletteColor = this._palette[pixels[i + 1]]; + let paletteColor = this._palette[imageData.data[i + 1]]; if(paletteColor === undefined) paletteColor = [0, 0, 0]; @@ -42,7 +37,12 @@ export class GraphicAssetPalette implements IGraphicAssetPalette ctx.putImageData(imageData, 0, 0); - return Texture.from(canvas); + const newTexture = Texture.from(canvas); + + //@ts-ignore + newTexture.source.hitMap = imageData.data; + + return newTexture; } public get primaryColor(): number diff --git a/packages/room/src/renderer/utils/ExtendedSprite.ts b/packages/room/src/renderer/utils/ExtendedSprite.ts index d3bb75ee..1d668f2e 100644 --- a/packages/room/src/renderer/utils/ExtendedSprite.ts +++ b/packages/room/src/renderer/utils/ExtendedSprite.ts @@ -1,5 +1,5 @@ import { AlphaTolerance } from '@nitrots/api'; -import { TextureUtils } from '@nitrots/utils'; +import { GetRenderer } from '@nitrots/utils'; import { Point, Sprite, Texture, TextureSource } from 'pixi.js'; export class ExtendedSprite extends Sprite @@ -78,10 +78,12 @@ export class ExtendedSprite extends Sprite if(!textureSource) return false; const texture = new Texture(textureSource); - const pixels = TextureUtils.getPixels(texture); + const canvas = GetRenderer().texture.generateCanvas(texture); + const ctx = canvas.getContext('2d'); + const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); //@ts-ignore - textureSource.hitMap = pixels.pixels; + textureSource.hitMap = imageData.data; texture.destroy(); return true;