Move to Renderer V2

This commit is contained in:
duckietm
2024-04-03 09:27:56 +02:00
parent 110c3ad393
commit b3134ce50b
4080 changed files with 115593 additions and 66375 deletions
@@ -0,0 +1,57 @@
import { IGraphicAssetPalette } from '@nitrots/api';
import { GetRenderer } from '@nitrots/utils';
import { Texture } from 'pixi.js';
export class GraphicAssetPalette implements IGraphicAssetPalette
{
private _palette: [number, number, number][];
private _primaryColor: number;
private _secondaryColor: number;
constructor(palette: [number, number, number][], primaryColor: number, secondaryColor: number)
{
this._palette = palette;
while(this._palette.length < 256) this._palette.push([0, 0, 0]);
this._primaryColor = primaryColor;
this._secondaryColor = secondaryColor;
}
public applyPalette(texture: Texture): Texture
{
const canvas = GetRenderer().texture.generateCanvas(texture);
const ctx = canvas.getContext('2d');
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
for(let i = 0; i < imageData.data.length; i += 4)
{
let paletteColor = this._palette[imageData.data[i + 1]];
if(paletteColor === undefined) paletteColor = [0, 0, 0];
imageData.data[i] = paletteColor[0];
imageData.data[i + 1] = paletteColor[1];
imageData.data[i + 2] = paletteColor[2];
}
ctx.putImageData(imageData, 0, 0);
const newTexture = Texture.from(canvas);
//@ts-ignore
newTexture.source.hitMap = imageData.data;
return newTexture;
}
public get primaryColor(): number
{
return this._primaryColor;
}
public get secondaryColor(): number
{
return this._secondaryColor;
}
}