UNPKG

2.55 kBJavaScriptView Raw
1import { ExtensionType, extensions } from "@pixi/extensions";
2import { Transform, Rectangle, Matrix } from "@pixi/math";
3import { RenderTexture } from "./RenderTexture.mjs";
4const tempTransform = new Transform(), tempRect = new Rectangle();
5class GenerateTextureSystem {
6 constructor(renderer) {
7 this.renderer = renderer, this._tempMatrix = new Matrix();
8 }
9 /**
10 * A Useful function that returns a texture of the display object that can then be used to create sprites
11 * This can be quite useful if your displayObject is complicated and needs to be reused multiple times.
12 * @param displayObject - The displayObject the object will be generated from.
13 * @param {IGenerateTextureOptions} options - Generate texture options.
14 * @param {PIXI.Rectangle} options.region - The region of the displayObject, that shall be rendered,
15 * if no region is specified, defaults to the local bounds of the displayObject.
16 * @param {number} [options.resolution] - If not given, the renderer's resolution is used.
17 * @param {PIXI.MSAA_QUALITY} [options.multisample] - If not given, the renderer's multisample is used.
18 * @returns a shiny new texture of the display object passed in
19 */
20 generateTexture(displayObject, options) {
21 const { region: manualRegion, ...textureOptions } = options || {}, region = manualRegion?.copyTo(tempRect) || displayObject.getLocalBounds(tempRect, !0), resolution = textureOptions.resolution || this.renderer.resolution;
22 region.width = Math.max(region.width, 1 / resolution), region.height = Math.max(region.height, 1 / resolution), textureOptions.width = region.width, textureOptions.height = region.height, textureOptions.resolution = resolution, textureOptions.multisample ?? (textureOptions.multisample = this.renderer.multisample);
23 const renderTexture = RenderTexture.create(textureOptions);
24 this._tempMatrix.tx = -region.x, this._tempMatrix.ty = -region.y;
25 const transform = displayObject.transform;
26 return displayObject.transform = tempTransform, this.renderer.render(displayObject, {
27 renderTexture,
28 transform: this._tempMatrix,
29 skipUpdateTransform: !!displayObject.parent,
30 blit: !0
31 }), displayObject.transform = transform, renderTexture;
32 }
33 destroy() {
34 }
35}
36GenerateTextureSystem.extension = {
37 type: [
38 ExtensionType.RendererSystem,
39 ExtensionType.CanvasRendererSystem
40 ],
41 name: "textureGenerator"
42};
43extensions.add(GenerateTextureSystem);
44export {
45 GenerateTextureSystem
46};
47//# sourceMappingURL=GenerateTextureSystem.mjs.map