UNPKG

9.94 kBJavaScriptView Raw
1import { RENDERER_TYPE } from "@pixi/constants";
2import { ExtensionType, extensions } from "@pixi/extensions";
3import { Matrix } from "@pixi/math";
4import { settings } from "@pixi/settings";
5import { deprecation, isWebGLSupported } from "@pixi/utils";
6import { UniformGroup } from "./shader/UniformGroup.mjs";
7import { SystemManager } from "./system/SystemManager.mjs";
8const _Renderer = class _Renderer2 extends SystemManager {
9 /**
10 * @param {PIXI.IRendererOptions} [options] - See {@link PIXI.settings.RENDER_OPTIONS} for defaults.
11 */
12 constructor(options) {
13 super(), this.type = RENDERER_TYPE.WEBGL, options = Object.assign({}, settings.RENDER_OPTIONS, options), this.gl = null, this.CONTEXT_UID = 0, this.globalUniforms = new UniformGroup({
14 projectionMatrix: new Matrix()
15 }, !0);
16 const systemConfig = {
17 runners: [
18 "init",
19 "destroy",
20 "contextChange",
21 "resolutionChange",
22 "reset",
23 "update",
24 "postrender",
25 "prerender",
26 "resize"
27 ],
28 systems: _Renderer2.__systems,
29 priority: [
30 "_view",
31 "textureGenerator",
32 "background",
33 "_plugin",
34 "startup",
35 // low level WebGL systems
36 "context",
37 "state",
38 "texture",
39 "buffer",
40 "geometry",
41 "framebuffer",
42 "transformFeedback",
43 // high level pixi specific rendering
44 "mask",
45 "scissor",
46 "stencil",
47 "projection",
48 "textureGC",
49 "filter",
50 "renderTexture",
51 "batch",
52 "objectRenderer",
53 "_multisample"
54 ]
55 };
56 this.setup(systemConfig), "useContextAlpha" in options && (deprecation("7.0.0", "options.useContextAlpha is deprecated, use options.premultipliedAlpha and options.backgroundAlpha instead"), options.premultipliedAlpha = options.useContextAlpha && options.useContextAlpha !== "notMultiplied", options.backgroundAlpha = options.useContextAlpha === !1 ? 1 : options.backgroundAlpha), this._plugin.rendererPlugins = _Renderer2.__plugins, this.options = options, this.startup.run(this.options);
57 }
58 /**
59 * Create renderer if WebGL is available. Overrideable
60 * by the **@pixi/canvas-renderer** package to allow fallback.
61 * throws error if WebGL is not available.
62 * @param options
63 * @private
64 */
65 static test(options) {
66 return options?.forceCanvas ? !1 : isWebGLSupported();
67 }
68 /**
69 * Renders the object to its WebGL view.
70 * @param displayObject - The object to be rendered.
71 * @param {object} [options] - Object to use for render options.
72 * @param {PIXI.RenderTexture} [options.renderTexture] - The render texture to render to.
73 * @param {boolean} [options.clear=true] - Should the canvas be cleared before the new render.
74 * @param {PIXI.Matrix} [options.transform] - A transform to apply to the render texture before rendering.
75 * @param {boolean} [options.skipUpdateTransform=false] - Should we skip the update transform pass?
76 */
77 render(displayObject, options) {
78 this.objectRenderer.render(displayObject, options);
79 }
80 /**
81 * Resizes the WebGL view to the specified width and height.
82 * @param desiredScreenWidth - The desired width of the screen.
83 * @param desiredScreenHeight - The desired height of the screen.
84 */
85 resize(desiredScreenWidth, desiredScreenHeight) {
86 this._view.resizeView(desiredScreenWidth, desiredScreenHeight);
87 }
88 /**
89 * Resets the WebGL state so you can render things however you fancy!
90 * @returns Returns itself.
91 */
92 reset() {
93 return this.runners.reset.emit(), this;
94 }
95 /** Clear the frame buffer. */
96 clear() {
97 this.renderTexture.bind(), this.renderTexture.clear();
98 }
99 /**
100 * Removes everything from the renderer (event listeners, spritebatch, etc...)
101 * @param [removeView=false] - Removes the Canvas element from the DOM.
102 * See: https://github.com/pixijs/pixijs/issues/2233
103 */
104 destroy(removeView = !1) {
105 this.runners.destroy.items.reverse(), this.emitWithCustomOptions(this.runners.destroy, {
106 _view: removeView
107 }), super.destroy();
108 }
109 /** Collection of plugins */
110 get plugins() {
111 return this._plugin.plugins;
112 }
113 /** The number of msaa samples of the canvas. */
114 get multisample() {
115 return this._multisample.multisample;
116 }
117 /**
118 * Same as view.width, actual number of pixels in the canvas by horizontal.
119 * @member {number}
120 * @readonly
121 * @default 800
122 */
123 get width() {
124 return this._view.element.width;
125 }
126 /**
127 * Same as view.height, actual number of pixels in the canvas by vertical.
128 * @default 600
129 */
130 get height() {
131 return this._view.element.height;
132 }
133 /** The resolution / device pixel ratio of the renderer. */
134 get resolution() {
135 return this._view.resolution;
136 }
137 set resolution(value) {
138 this._view.resolution = value, this.runners.resolutionChange.emit(value);
139 }
140 /** Whether CSS dimensions of canvas view should be resized to screen dimensions automatically. */
141 get autoDensity() {
142 return this._view.autoDensity;
143 }
144 /** The canvas element that everything is drawn to.*/
145 get view() {
146 return this._view.element;
147 }
148 /**
149 * Measurements of the screen. (0, 0, screenWidth, screenHeight).
150 *
151 * Its safe to use as filterArea or hitArea for the whole stage.
152 * @member {PIXI.Rectangle}
153 */
154 get screen() {
155 return this._view.screen;
156 }
157 /** the last object rendered by the renderer. Useful for other plugins like interaction managers */
158 get lastObjectRendered() {
159 return this.objectRenderer.lastObjectRendered;
160 }
161 /** Flag if we are rendering to the screen vs renderTexture */
162 get renderingToScreen() {
163 return this.objectRenderer.renderingToScreen;
164 }
165 /** When logging Pixi to the console, this is the name we will show */
166 get rendererLogId() {
167 return `WebGL ${this.context.webGLVersion}`;
168 }
169 /**
170 * This sets weather the screen is totally cleared between each frame withthe background color and alpha
171 * @deprecated since 7.0.0
172 */
173 get clearBeforeRender() {
174 return deprecation("7.0.0", "renderer.clearBeforeRender has been deprecated, please use renderer.background.clearBeforeRender instead."), this.background.clearBeforeRender;
175 }
176 /**
177 * Pass-thru setting for the canvas' context `alpha` property. This is typically
178 * not something you need to fiddle with. If you want transparency, use `backgroundAlpha`.
179 * @deprecated since 7.0.0
180 * @member {boolean}
181 */
182 get useContextAlpha() {
183 return deprecation("7.0.0", "renderer.useContextAlpha has been deprecated, please use renderer.context.premultipliedAlpha instead."), this.context.useContextAlpha;
184 }
185 /**
186 * readonly drawing buffer preservation
187 * we can only know this if Pixi created the context
188 * @deprecated since 7.0.0
189 */
190 get preserveDrawingBuffer() {
191 return deprecation("7.0.0", "renderer.preserveDrawingBuffer has been deprecated, we cannot truly know this unless pixi created the context"), this.context.preserveDrawingBuffer;
192 }
193 /**
194 * The background color to fill if not transparent
195 * @member {number}
196 * @deprecated since 7.0.0
197 */
198 get backgroundColor() {
199 return deprecation("7.0.0", "renderer.backgroundColor has been deprecated, use renderer.background.color instead."), this.background.color;
200 }
201 set backgroundColor(value) {
202 deprecation("7.0.0", "renderer.backgroundColor has been deprecated, use renderer.background.color instead."), this.background.color = value;
203 }
204 /**
205 * The background color alpha. Setting this to 0 will make the canvas transparent.
206 * @member {number}
207 * @deprecated since 7.0.0
208 */
209 get backgroundAlpha() {
210 return deprecation("7.0.0", "renderer.backgroundAlpha has been deprecated, use renderer.background.alpha instead."), this.background.alpha;
211 }
212 /**
213 * @deprecated since 7.0.0
214 */
215 set backgroundAlpha(value) {
216 deprecation("7.0.0", "renderer.backgroundAlpha has been deprecated, use renderer.background.alpha instead."), this.background.alpha = value;
217 }
218 /**
219 * @deprecated since 7.0.0
220 */
221 get powerPreference() {
222 return deprecation("7.0.0", "renderer.powerPreference has been deprecated, we can only know this if pixi creates the context"), this.context.powerPreference;
223 }
224 /**
225 * Useful function that returns a texture of the display object that can then be used to create sprites
226 * This can be quite useful if your displayObject is complicated and needs to be reused multiple times.
227 * @param displayObject - The displayObject the object will be generated from.
228 * @param {IGenerateTextureOptions} options - Generate texture options.
229 * @param {PIXI.Rectangle} options.region - The region of the displayObject, that shall be rendered,
230 * if no region is specified, defaults to the local bounds of the displayObject.
231 * @param {number} [options.resolution] - If not given, the renderer's resolution is used.
232 * @param {PIXI.MSAA_QUALITY} [options.multisample] - If not given, the renderer's multisample is used.
233 * @returns A texture of the graphics object.
234 */
235 generateTexture(displayObject, options) {
236 return this.textureGenerator.generateTexture(displayObject, options);
237 }
238};
239_Renderer.extension = {
240 type: ExtensionType.Renderer,
241 priority: 1
242}, /**
243* Collection of installed plugins. These are included by default in PIXI, but can be excluded
244* by creating a custom build. Consult the README for more information about creating custom
245* builds and excluding plugins.
246* @private
247*/
248_Renderer.__plugins = {}, /**
249* The collection of installed systems.
250* @private
251*/
252_Renderer.__systems = {};
253let Renderer = _Renderer;
254extensions.handleByMap(ExtensionType.RendererPlugin, Renderer.__plugins);
255extensions.handleByMap(ExtensionType.RendererSystem, Renderer.__systems);
256extensions.add(Renderer);
257export {
258 Renderer
259};
260//# sourceMappingURL=Renderer.mjs.map