UNPKG

@pixi/core

Version:
261 lines (260 loc) 9.94 kB
import { RENDERER_TYPE } from "@pixi/constants"; import { ExtensionType, extensions } from "@pixi/extensions"; import { Matrix } from "@pixi/math"; import { settings } from "@pixi/settings"; import { deprecation, isWebGLSupported } from "@pixi/utils"; import { UniformGroup } from "./shader/UniformGroup.mjs"; import { SystemManager } from "./system/SystemManager.mjs"; const _Renderer = class _Renderer2 extends SystemManager { /** * @param {PIXI.IRendererOptions} [options] - See {@link PIXI.settings.RENDER_OPTIONS} for defaults. */ constructor(options) { super(), this.type = RENDERER_TYPE.WEBGL, options = Object.assign({}, settings.RENDER_OPTIONS, options), this.gl = null, this.CONTEXT_UID = 0, this.globalUniforms = new UniformGroup({ projectionMatrix: new Matrix() }, !0); const systemConfig = { runners: [ "init", "destroy", "contextChange", "resolutionChange", "reset", "update", "postrender", "prerender", "resize" ], systems: _Renderer2.__systems, priority: [ "_view", "textureGenerator", "background", "_plugin", "startup", // low level WebGL systems "context", "state", "texture", "buffer", "geometry", "framebuffer", "transformFeedback", // high level pixi specific rendering "mask", "scissor", "stencil", "projection", "textureGC", "filter", "renderTexture", "batch", "objectRenderer", "_multisample" ] }; 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); } /** * Create renderer if WebGL is available. Overrideable * by the **@pixi/canvas-renderer** package to allow fallback. * throws error if WebGL is not available. * @param options * @private */ static test(options) { return options?.forceCanvas ? !1 : isWebGLSupported(); } /** * Renders the object to its WebGL view. * @param displayObject - The object to be rendered. * @param {object} [options] - Object to use for render options. * @param {PIXI.RenderTexture} [options.renderTexture] - The render texture to render to. * @param {boolean} [options.clear=true] - Should the canvas be cleared before the new render. * @param {PIXI.Matrix} [options.transform] - A transform to apply to the render texture before rendering. * @param {boolean} [options.skipUpdateTransform=false] - Should we skip the update transform pass? */ render(displayObject, options) { this.objectRenderer.render(displayObject, options); } /** * Resizes the WebGL view to the specified width and height. * @param desiredScreenWidth - The desired width of the screen. * @param desiredScreenHeight - The desired height of the screen. */ resize(desiredScreenWidth, desiredScreenHeight) { this._view.resizeView(desiredScreenWidth, desiredScreenHeight); } /** * Resets the WebGL state so you can render things however you fancy! * @returns Returns itself. */ reset() { return this.runners.reset.emit(), this; } /** Clear the frame buffer. */ clear() { this.renderTexture.bind(), this.renderTexture.clear(); } /** * Removes everything from the renderer (event listeners, spritebatch, etc...) * @param [removeView=false] - Removes the Canvas element from the DOM. * See: https://github.com/pixijs/pixijs/issues/2233 */ destroy(removeView = !1) { this.runners.destroy.items.reverse(), this.emitWithCustomOptions(this.runners.destroy, { _view: removeView }), super.destroy(); } /** Collection of plugins */ get plugins() { return this._plugin.plugins; } /** The number of msaa samples of the canvas. */ get multisample() { return this._multisample.multisample; } /** * Same as view.width, actual number of pixels in the canvas by horizontal. * @member {number} * @readonly * @default 800 */ get width() { return this._view.element.width; } /** * Same as view.height, actual number of pixels in the canvas by vertical. * @default 600 */ get height() { return this._view.element.height; } /** The resolution / device pixel ratio of the renderer. */ get resolution() { return this._view.resolution; } set resolution(value) { this._view.resolution = value, this.runners.resolutionChange.emit(value); } /** Whether CSS dimensions of canvas view should be resized to screen dimensions automatically. */ get autoDensity() { return this._view.autoDensity; } /** The canvas element that everything is drawn to.*/ get view() { return this._view.element; } /** * Measurements of the screen. (0, 0, screenWidth, screenHeight). * * Its safe to use as filterArea or hitArea for the whole stage. * @member {PIXI.Rectangle} */ get screen() { return this._view.screen; } /** the last object rendered by the renderer. Useful for other plugins like interaction managers */ get lastObjectRendered() { return this.objectRenderer.lastObjectRendered; } /** Flag if we are rendering to the screen vs renderTexture */ get renderingToScreen() { return this.objectRenderer.renderingToScreen; } /** When logging Pixi to the console, this is the name we will show */ get rendererLogId() { return `WebGL ${this.context.webGLVersion}`; } /** * This sets weather the screen is totally cleared between each frame withthe background color and alpha * @deprecated since 7.0.0 */ get clearBeforeRender() { return deprecation("7.0.0", "renderer.clearBeforeRender has been deprecated, please use renderer.background.clearBeforeRender instead."), this.background.clearBeforeRender; } /** * Pass-thru setting for the canvas' context `alpha` property. This is typically * not something you need to fiddle with. If you want transparency, use `backgroundAlpha`. * @deprecated since 7.0.0 * @member {boolean} */ get useContextAlpha() { return deprecation("7.0.0", "renderer.useContextAlpha has been deprecated, please use renderer.context.premultipliedAlpha instead."), this.context.useContextAlpha; } /** * readonly drawing buffer preservation * we can only know this if Pixi created the context * @deprecated since 7.0.0 */ get preserveDrawingBuffer() { return deprecation("7.0.0", "renderer.preserveDrawingBuffer has been deprecated, we cannot truly know this unless pixi created the context"), this.context.preserveDrawingBuffer; } /** * The background color to fill if not transparent * @member {number} * @deprecated since 7.0.0 */ get backgroundColor() { return deprecation("7.0.0", "renderer.backgroundColor has been deprecated, use renderer.background.color instead."), this.background.color; } set backgroundColor(value) { deprecation("7.0.0", "renderer.backgroundColor has been deprecated, use renderer.background.color instead."), this.background.color = value; } /** * The background color alpha. Setting this to 0 will make the canvas transparent. * @member {number} * @deprecated since 7.0.0 */ get backgroundAlpha() { return deprecation("7.0.0", "renderer.backgroundAlpha has been deprecated, use renderer.background.alpha instead."), this.background.alpha; } /** * @deprecated since 7.0.0 */ set backgroundAlpha(value) { deprecation("7.0.0", "renderer.backgroundAlpha has been deprecated, use renderer.background.alpha instead."), this.background.alpha = value; } /** * @deprecated since 7.0.0 */ get powerPreference() { return deprecation("7.0.0", "renderer.powerPreference has been deprecated, we can only know this if pixi creates the context"), this.context.powerPreference; } /** * Useful function that returns a texture of the display object that can then be used to create sprites * This can be quite useful if your displayObject is complicated and needs to be reused multiple times. * @param displayObject - The displayObject the object will be generated from. * @param {IGenerateTextureOptions} options - Generate texture options. * @param {PIXI.Rectangle} options.region - The region of the displayObject, that shall be rendered, * if no region is specified, defaults to the local bounds of the displayObject. * @param {number} [options.resolution] - If not given, the renderer's resolution is used. * @param {PIXI.MSAA_QUALITY} [options.multisample] - If not given, the renderer's multisample is used. * @returns A texture of the graphics object. */ generateTexture(displayObject, options) { return this.textureGenerator.generateTexture(displayObject, options); } }; _Renderer.extension = { type: ExtensionType.Renderer, priority: 1 }, /** * Collection of installed plugins. These are included by default in PIXI, but can be excluded * by creating a custom build. Consult the README for more information about creating custom * builds and excluding plugins. * @private */ _Renderer.__plugins = {}, /** * The collection of installed systems. * @private */ _Renderer.__systems = {}; let Renderer = _Renderer; extensions.handleByMap(ExtensionType.RendererPlugin, Renderer.__plugins); extensions.handleByMap(ExtensionType.RendererSystem, Renderer.__systems); extensions.add(Renderer); export { Renderer }; //# sourceMappingURL=Renderer.mjs.map