UNPKG

@pixi/core

Version:
117 lines (112 loc) 4.04 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var extensions = require('@pixi/extensions'); var math = require('@pixi/math'); var settings = require('@pixi/settings'); var AbstractMaskSystem = require('./AbstractMaskSystem.js'); const tempMatrix = new math.Matrix(); const rectPool = []; const _ScissorSystem = class extends AbstractMaskSystem.AbstractMaskSystem { constructor(renderer) { super(renderer); this.glConst = settings.settings.ADAPTER.getWebGLRenderingContext().SCISSOR_TEST; } getStackLength() { const maskData = this.maskStack[this.maskStack.length - 1]; if (maskData) { return maskData._scissorCounter; } return 0; } calcScissorRect(maskData) { if (maskData._scissorRectLocal) { return; } const prevData = maskData._scissorRect; const { maskObject } = maskData; const { renderer } = this; const renderTextureSystem = renderer.renderTexture; const rect = maskObject.getBounds(true, rectPool.pop() ?? new math.Rectangle()); this.roundFrameToPixels(rect, renderTextureSystem.current ? renderTextureSystem.current.resolution : renderer.resolution, renderTextureSystem.sourceFrame, renderTextureSystem.destinationFrame, renderer.projection.transform); if (prevData) { rect.fit(prevData); } maskData._scissorRectLocal = rect; } static isMatrixRotated(matrix) { if (!matrix) { return false; } const { a, b, c, d } = matrix; return (Math.abs(b) > 1e-4 || Math.abs(c) > 1e-4) && (Math.abs(a) > 1e-4 || Math.abs(d) > 1e-4); } testScissor(maskData) { const { maskObject } = maskData; if (!maskObject.isFastRect || !maskObject.isFastRect()) { return false; } if (_ScissorSystem.isMatrixRotated(maskObject.worldTransform)) { return false; } if (_ScissorSystem.isMatrixRotated(this.renderer.projection.transform)) { return false; } this.calcScissorRect(maskData); const rect = maskData._scissorRectLocal; return rect.width > 0 && rect.height > 0; } roundFrameToPixels(frame, resolution, bindingSourceFrame, bindingDestinationFrame, transform) { if (_ScissorSystem.isMatrixRotated(transform)) { return; } transform = transform ? tempMatrix.copyFrom(transform) : tempMatrix.identity(); transform.translate(-bindingSourceFrame.x, -bindingSourceFrame.y).scale(bindingDestinationFrame.width / bindingSourceFrame.width, bindingDestinationFrame.height / bindingSourceFrame.height).translate(bindingDestinationFrame.x, bindingDestinationFrame.y); this.renderer.filter.transformAABB(transform, frame); frame.fit(bindingDestinationFrame); frame.x = Math.round(frame.x * resolution); frame.y = Math.round(frame.y * resolution); frame.width = Math.round(frame.width * resolution); frame.height = Math.round(frame.height * resolution); } push(maskData) { if (!maskData._scissorRectLocal) { this.calcScissorRect(maskData); } const { gl } = this.renderer; if (!maskData._scissorRect) { gl.enable(gl.SCISSOR_TEST); } maskData._scissorCounter++; maskData._scissorRect = maskData._scissorRectLocal; this._useCurrent(); } pop(maskData) { const { gl } = this.renderer; if (maskData) { rectPool.push(maskData._scissorRectLocal); } if (this.getStackLength() > 0) { this._useCurrent(); } else { gl.disable(gl.SCISSOR_TEST); } } _useCurrent() { const rect = this.maskStack[this.maskStack.length - 1]._scissorRect; let y; if (this.renderer.renderTexture.current) { y = rect.y; } else { y = this.renderer.height - rect.height - rect.y; } this.renderer.gl.scissor(rect.x, y, rect.width, rect.height); } }; let ScissorSystem = _ScissorSystem; ScissorSystem.extension = { type: extensions.ExtensionType.RendererSystem, name: "scissor" }; extensions.extensions.add(ScissorSystem); exports.ScissorSystem = ScissorSystem; //# sourceMappingURL=ScissorSystem.js.map