{"version":3,"file":"CanvasTilingSpritePipe.mjs","sources":["../../../../src/scene/sprite-tiling/canvas/CanvasTilingSpritePipe.ts"],"sourcesContent":["import { ExtensionType } from '../../../extensions/Extensions';\nimport { Matrix } from '../../../maths/matrix/Matrix';\nimport { Point } from '../../../maths/point/Point';\nimport { canvasUtils } from '../../../rendering/renderers/canvas/utils/canvasUtils';\nimport { bgr2rgb } from '../../../scene/container/container-mixins/getGlobalMixin';\nimport { multiplyHexColors } from '../../../scene/container/utils/multiplyHexColors';\n\nimport type { CanvasRenderer } from '../../../rendering/renderers/canvas/CanvasRenderer';\nimport type { InstructionSet } from '../../../rendering/renderers/shared/instructions/InstructionSet';\nimport type { RenderPipe } from '../../../rendering/renderers/shared/instructions/RenderPipe';\nimport type { Renderer } from '../../../rendering/renderers/types';\nimport type { TilingSprite } from '../TilingSprite';\n\n// Reusable objects to avoid allocations (matching v7 approach)\nconst worldMatrix = new Matrix();\nconst patternMatrix = new Matrix();\nconst patternRect = [new Point(), new Point(), new Point(), new Point()];\n\n/** @internal */\nexport class CanvasTilingSpritePipe implements RenderPipe<TilingSprite>\n{\n    /** @ignore */\n    public static extension = {\n        type: [\n            ExtensionType.CanvasPipes,\n        ],\n        name: 'tilingSprite',\n    } as const;\n\n    private _renderer: CanvasRenderer;\n\n    constructor(renderer: Renderer)\n    {\n        this._renderer = renderer as CanvasRenderer;\n    }\n\n    public validateRenderable(_renderable: TilingSprite): boolean\n    {\n        return false;\n    }\n\n    public addRenderable(tilingSprite: TilingSprite, instructionSet: InstructionSet)\n    {\n        this._renderer.renderPipes.batch.break(instructionSet);\n        instructionSet.add(tilingSprite);\n    }\n\n    public updateRenderable(_tilingSprite: TilingSprite)\n    {\n        // no-op for canvas\n    }\n\n    public execute(tilingSprite: TilingSprite)\n    {\n        const renderer = this._renderer;\n        const contextSystem = renderer.canvasContext;\n        const context = contextSystem.activeContext;\n\n        context.save();\n\n        contextSystem.setBlendMode(tilingSprite.groupBlendMode);\n\n        const globalColor = renderer.globalUniforms.globalUniformData?.worldColor ?? 0xFFFFFFFF;\n        const groupColorAlpha = tilingSprite.groupColorAlpha;\n\n        const globalAlpha = ((globalColor >>> 24) & 0xFF) / 255;\n        const groupAlphaValue = ((groupColorAlpha >>> 24) & 0xFF) / 255;\n\n        const filterAlpha = (renderer.filter as { alphaMultiplier?: number } | null)?.alphaMultiplier ?? 1;\n        const alpha = globalAlpha * groupAlphaValue * filterAlpha;\n\n        if (alpha <= 0)\n        {\n            context.restore();\n\n            return;\n        }\n\n        context.globalAlpha = alpha;\n\n        const globalTint = globalColor & 0xFFFFFF;\n        const groupTintBGR = groupColorAlpha & 0xFFFFFF;\n\n        const tint = bgr2rgb(multiplyHexColors(groupTintBGR, globalTint));\n\n        const texture = tilingSprite.texture;\n        const pattern = canvasUtils.getTintedPattern(texture, tint);\n\n        const width = tilingSprite.width;\n        const height = tilingSprite.height;\n        const transform = tilingSprite.groupTransform;\n        const resolution = texture.source._resolution ?? texture.source.resolution ?? 1;\n\n        // Build patternMatrix from tileTransform (following v7 approach exactly)\n        // patternMatrix = tileTransform x shiftTransform x scaleTransform\n        patternMatrix.copyFrom(tilingSprite._tileTransform.matrix);\n\n        // Apply anchor shift if not applying anchor to texture (v7: uvRespectAnchor)\n        if (!tilingSprite.applyAnchorToTexture)\n        {\n            patternMatrix.translate(-tilingSprite.anchor.x * width, -tilingSprite.anchor.y * height);\n        }\n\n        // Apply resolution scaling\n        patternMatrix.scale(1 / resolution, 1 / resolution);\n\n        // Build worldMatrix = transform * patternMatrix (v7 used two prepends on identity)\n        worldMatrix.identity();\n        worldMatrix.prepend(patternMatrix);\n        worldMatrix.prepend(transform);\n\n        const roundPixels = (renderer._roundPixels | tilingSprite._roundPixels) as 0 | 1;\n\n        contextSystem.setContextTransform(worldMatrix, roundPixels === 1);\n\n        context.fillStyle = pattern;\n\n        // Calculate rect corners in local space\n        const lx = tilingSprite.anchor.x * -width;\n        const ly = tilingSprite.anchor.y * -height;\n\n        patternRect[0].set(lx, ly);\n        patternRect[1].set(lx + width, ly);\n        patternRect[2].set(lx + width, ly + height);\n        patternRect[3].set(lx, ly + height);\n\n        // Map rect corners from local space to pattern space\n        for (let i = 0; i < 4; i++)\n        {\n            patternMatrix.applyInverse(patternRect[i], patternRect[i]);\n        }\n\n        // Draw path in pattern space\n        context.beginPath();\n        context.moveTo(patternRect[0].x, patternRect[0].y);\n\n        for (let i = 1; i < 4; i++)\n        {\n            context.lineTo(patternRect[i].x, patternRect[i].y);\n        }\n\n        context.closePath();\n        context.fill();\n\n        context.restore();\n    }\n\n    public destroy()\n    {\n        this._renderer = null;\n    }\n}\n"],"names":[],"mappings":";;;;;;;;AAcA,MAAM,WAAA,GAAc,IAAI,MAAA,EAAO;AAC/B,MAAM,aAAA,GAAgB,IAAI,MAAA,EAAO;AACjC,MAAM,WAAA,GAAc,CAAC,IAAI,KAAA,EAAM,EAAG,IAAI,KAAA,EAAM,EAAG,IAAI,KAAA,EAAM,EAAG,IAAI,OAAO,CAAA;AAGhE,MAAM,sBAAA,CACb;AAAA,EAWI,YAAY,QAAA,EACZ;AACI,IAAA,IAAA,CAAK,SAAA,GAAY,QAAA;AAAA,EACrB;AAAA,EAEO,mBAAmB,WAAA,EAC1B;AACI,IAAA,OAAO,KAAA;AAAA,EACX;AAAA,EAEO,aAAA,CAAc,cAA4B,cAAA,EACjD;AACI,IAAA,IAAA,CAAK,SAAA,CAAU,WAAA,CAAY,KAAA,CAAM,KAAA,CAAM,cAAc,CAAA;AACrD,IAAA,cAAA,CAAe,IAAI,YAAY,CAAA;AAAA,EACnC;AAAA,EAEO,iBAAiB,aAAA,EACxB;AAAA,EAEA;AAAA,EAEO,QAAQ,YAAA,EACf;AACI,IAAA,MAAM,WAAW,IAAA,CAAK,SAAA;AACtB,IAAA,MAAM,gBAAgB,QAAA,CAAS,aAAA;AAC/B,IAAA,MAAM,UAAU,aAAA,CAAc,aAAA;AAE9B,IAAA,OAAA,CAAQ,IAAA,EAAK;AAEb,IAAA,aAAA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