2021-08-03 17:54:33 +02:00
|
|
|
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';
|
2023-01-03 07:04:48 +01:00
|
|
|
import { Matrix, Rectangle } from '@pixi/math';
|
|
|
|
import { settings } from '@pixi/settings';
|
|
|
|
import { Sprite } from '@pixi/sprite';
|
2022-10-30 05:02:19 +01:00
|
|
|
import { PixiApplicationProxy } from './PixiApplicationProxy';
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
export class TextureUtils
|
|
|
|
{
|
2023-01-03 07:04:48 +01:00
|
|
|
public static generateTexture(displayObject: DisplayObject, region: Rectangle = null, scaleMode: number = null, resolution: number = 1): RenderTexture
|
2021-03-17 03:02:09 +01:00
|
|
|
{
|
2022-11-02 23:44:30 +01:00
|
|
|
if(!displayObject) return null;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
2023-01-03 07:04:48 +01:00
|
|
|
if(scaleMode === null) scaleMode = settings.SCALE_MODE;
|
|
|
|
|
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
|
|
|
{
|
2022-11-02 23:44:30 +01:00
|
|
|
if(!image) return null;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
return Texture.from(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static generateImage(target: DisplayObject | RenderTexture): HTMLImageElement
|
|
|
|
{
|
2022-11-02 23:44:30 +01:00
|
|
|
if(!target) return null;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
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
|
|
|
|
{
|
2022-11-02 23:44:30 +01:00
|
|
|
if(!target) return null;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
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
|
|
|
|
{
|
2022-11-02 23:44:30 +01:00
|
|
|
if(!target) return null;
|
2021-07-13 22:34:41 +02:00
|
|
|
|
2021-08-03 17:54:33 +02:00
|
|
|
return this.getExtractor().canvas(target);
|
2021-03-17 03:02:09 +01:00
|
|
|
}
|
|
|
|
|
2023-01-03 07:04:48 +01:00
|
|
|
public static clearRenderTexture(renderTexture: RenderTexture): RenderTexture
|
|
|
|
{
|
|
|
|
if(!renderTexture) return null;
|
|
|
|
|
|
|
|
return this.writeToRenderTexture(new Sprite(Texture.EMPTY), renderTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static createRenderTexture(width: number, height: number): RenderTexture
|
|
|
|
{
|
|
|
|
if((width < 0) || (height < 0)) return null;
|
|
|
|
|
|
|
|
return RenderTexture.create({
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static createAndFillRenderTexture(width: number, height: number, color: number = 16777215): RenderTexture
|
|
|
|
{
|
|
|
|
if((width < 0) || (height < 0)) return null;
|
|
|
|
|
|
|
|
const renderTexture = this.createRenderTexture(width, height);
|
|
|
|
|
|
|
|
return this.clearAndFillRenderTexture(renderTexture, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static createAndWriteRenderTexture(width: number, height: number, displayObject: DisplayObject, transform: Matrix = null): RenderTexture
|
|
|
|
{
|
|
|
|
if((width < 0) || (height < 0)) return null;
|
|
|
|
|
|
|
|
const renderTexture = this.createRenderTexture(width, height);
|
|
|
|
|
|
|
|
return this.writeToRenderTexture(displayObject, renderTexture, true, transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static clearAndFillRenderTexture(renderTexture: RenderTexture, color: number = 16777215): RenderTexture
|
|
|
|
{
|
|
|
|
if(!renderTexture) return null;
|
|
|
|
|
|
|
|
const sprite = new Sprite(Texture.WHITE);
|
|
|
|
|
|
|
|
sprite.tint = color;
|
|
|
|
|
|
|
|
sprite.width = renderTexture.width;
|
|
|
|
sprite.height = renderTexture.height;
|
|
|
|
|
|
|
|
return this.writeToRenderTexture(sprite, renderTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static writeToRenderTexture(displayObject: DisplayObject, renderTexture: RenderTexture, clear: boolean = true, transform: Matrix = null): RenderTexture
|
|
|
|
{
|
|
|
|
if(!displayObject || !renderTexture) return null;
|
|
|
|
|
|
|
|
this.getRenderer().render(displayObject, {
|
|
|
|
renderTexture,
|
|
|
|
clear,
|
|
|
|
transform
|
|
|
|
});
|
|
|
|
|
|
|
|
return renderTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static getPixels(displayObject: DisplayObject | RenderTexture, frame: Rectangle = null): Uint8Array
|
|
|
|
{
|
|
|
|
return this.getExtractor().pixels(displayObject);
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:34:41 +02:00
|
|
|
public static getRenderer(): Renderer | AbstractRenderer
|
2021-03-17 03:02:09 +01:00
|
|
|
{
|
2022-10-30 05:02:19 +01:00
|
|
|
return PixiApplicationProxy.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
|
|
|
}
|
|
|
|
}
|