nitro-renderer/src/room/utils/TextureUtils.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-08-03 17:54:33 +02:00
import { SCALE_MODES } from '@pixi/constants';
import { AbstractRenderer, Renderer, RenderTexture, Resource, Texture } from '@pixi/core';
import { DisplayObject } from '@pixi/display';
2021-07-13 22:34:41 +02:00
import { Extract } from '@pixi/extract';
2021-08-03 17:54:33 +02:00
import { Rectangle } from '@pixi/math';
2021-03-17 03:02:09 +01:00
import { Nitro } from '../../nitro/Nitro';
export class TextureUtils
{
public static generateTexture(displayObject: DisplayObject, region: Rectangle = null, scaleMode: number = SCALE_MODES.NEAREST, resolution: number = 1): RenderTexture
{
if(!displayObject) return null;
2021-08-03 17:54:33 +02:00
return this.getRenderer().generateTexture(displayObject, {
2021-08-03 02:08:22 +02:00
scaleMode,
resolution,
region
});
2021-03-17 03:02:09 +01:00
}
2021-07-14 09:10:54 +02:00
public static generateTextureFromImage(image: HTMLImageElement): Texture<Resource>
2021-03-17 03:02:09 +01:00
{
if(!image) return null;
return Texture.from(image);
}
public static generateImage(target: DisplayObject | RenderTexture): HTMLImageElement
{
if(!target) return null;
2021-08-03 17:54:33 +02:00
return this.getExtractor().image(target);
2021-03-17 03:02:09 +01:00
}
public static generateImageUrl(target: DisplayObject | RenderTexture): string
{
if(!target) return null;
2021-08-03 17:54:33 +02:00
return this.getExtractor().base64(target);
2021-07-13 22:34:41 +02:00
}
public static generateCanvas(target: DisplayObject | RenderTexture): HTMLCanvasElement
{
if(!target) return null;
2021-08-03 17:54:33 +02:00
return this.getExtractor().canvas(target);
2021-03-17 03:02:09 +01:00
}
2021-07-13 22:34:41 +02:00
public static getRenderer(): Renderer | AbstractRenderer
2021-03-17 03:02:09 +01:00
{
2021-08-03 17:54:33 +02:00
return Nitro.instance.renderer;
2021-03-17 03:02:09 +01:00
}
2021-08-03 17:54:33 +02:00
public static getExtractor(): Extract
2021-03-17 03:02:09 +01:00
{
2021-08-03 17:54:33 +02:00
return (this.getRenderer().plugins.extract as Extract);
2021-03-17 03:02:09 +01:00
}
}