UNPKG

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