2021-07-13 22:34:41 +02:00
|
|
|
import { Extract } from '@pixi/extract';
|
2021-07-14 09:10:54 +02:00
|
|
|
import { AbstractRenderer, DisplayObject, Rectangle, Renderer, RenderTexture, Resource, SCALE_MODES, Texture } from 'pixi.js';
|
2021-03-17 03:02:09 +01:00
|
|
|
import { Nitro } from '../../nitro/Nitro';
|
|
|
|
|
|
|
|
export class TextureUtils
|
|
|
|
{
|
|
|
|
private static _renderer: Renderer = null;
|
|
|
|
|
|
|
|
public static generateTexture(displayObject: DisplayObject, region: Rectangle = null, scaleMode: number = SCALE_MODES.NEAREST, resolution: number = 1): RenderTexture
|
|
|
|
{
|
|
|
|
if(!displayObject) return null;
|
|
|
|
|
|
|
|
return TextureUtils.getRenderer().generateTexture(displayObject, scaleMode, resolution, region);
|
|
|
|
}
|
|
|
|
|
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-07-13 22:34:41 +02:00
|
|
|
const extract = (TextureUtils.getRenderer().plugins.extract as Extract);
|
|
|
|
|
|
|
|
return extract.image(target);
|
2021-03-17 03:02:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static generateImageUrl(target: DisplayObject | RenderTexture): string
|
|
|
|
{
|
|
|
|
if(!target) return null;
|
|
|
|
|
2021-07-13 22:34:41 +02:00
|
|
|
const extract = (TextureUtils.getRenderer().plugins.extract as Extract);
|
|
|
|
|
|
|
|
return extract.base64(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static generateCanvas(target: DisplayObject | RenderTexture): HTMLCanvasElement
|
|
|
|
{
|
|
|
|
if(!target) return null;
|
|
|
|
|
|
|
|
const extract = (TextureUtils.getRenderer().plugins.extract as Extract);
|
|
|
|
|
|
|
|
return extract.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
|
|
|
{
|
|
|
|
if(!TextureUtils._renderer) return Nitro.instance.renderer;
|
|
|
|
|
|
|
|
return TextureUtils._renderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static setRenderer(renderer: Renderer): void
|
|
|
|
{
|
|
|
|
TextureUtils._renderer = renderer;
|
|
|
|
}
|
|
|
|
}
|