1 line
12 KiB
Plaintext
1 line
12 KiB
Plaintext
{"version":3,"file":"HTMLTextPipe.mjs","sources":["../../../src/scene/text-html/HTMLTextPipe.ts"],"sourcesContent":["import { ExtensionType } from '../../extensions/Extensions';\nimport { Texture } from '../../rendering/renderers/shared/texture/Texture';\nimport { updateQuadBounds } from '../../utils/data/updateQuadBounds';\nimport { BigPool } from '../../utils/pool/PoolGroup';\nimport { BatchableSprite } from '../sprite/BatchableSprite';\n\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 { Container } from '../container/Container';\nimport type { HTMLText } from './HTMLText';\nimport type { HTMLTextStyle } from './HtmlTextStyle';\n\nexport class HTMLTextPipe implements RenderPipe<HTMLText>\n{\n /** @ignore */\n public static extension = {\n type: [\n ExtensionType.WebGLPipes,\n ExtensionType.WebGPUPipes,\n ExtensionType.CanvasPipes,\n ],\n name: 'htmlText',\n } as const;\n\n private _renderer: Renderer;\n\n private _gpuText: Record<number, {\n textureNeedsUploading: boolean;\n generatingTexture: boolean;\n texture: Texture,\n currentKey: string,\n batchableSprite: BatchableSprite,\n }> = Object.create(null);\n\n private readonly _destroyRenderableBound = this.destroyRenderable.bind(this) as (renderable: Container) => void;\n\n constructor(renderer: Renderer)\n {\n this._renderer = renderer;\n this._renderer.runners.resolutionChange.add(this);\n }\n\n public resolutionChange()\n {\n for (const i in this._gpuText)\n {\n const gpuText = this._gpuText[i];\n\n if (!gpuText) continue;\n\n const text = gpuText.batchableSprite.renderable as HTMLText;\n\n if (text._autoResolution)\n {\n text._resolution = this._renderer.resolution;\n text.onViewUpdate();\n }\n }\n }\n\n public validateRenderable(htmlText: HTMLText): boolean\n {\n const gpuText = this._getGpuText(htmlText);\n\n const newKey = htmlText._getKey();\n\n if (gpuText.textureNeedsUploading)\n {\n gpuText.textureNeedsUploading = false;\n\n return true;\n }\n\n if (gpuText.currentKey !== newKey)\n {\n // TODO - could look into optimising this a tad!\n // if its a single texture, then we could just swap it?\n // same for CanvasText..\n return true;\n }\n\n return false;\n }\n\n public addRenderable(htmlText: HTMLText, instructionSet: InstructionSet)\n {\n const gpuText = this._getGpuText(htmlText);\n\n const batchableSprite = gpuText.batchableSprite;\n\n if (htmlText._didTextUpdate)\n {\n this._updateText(htmlText);\n }\n\n this._renderer.renderPipes.batch.addToBatch(batchableSprite, instructionSet);\n }\n\n public updateRenderable(htmlText: HTMLText)\n {\n const gpuText = this._getGpuText(htmlText);\n const batchableSprite = gpuText.batchableSprite;\n\n if (htmlText._didTextUpdate)\n {\n this._updateText(htmlText);\n }\n\n batchableSprite._batcher.updateElement(batchableSprite);\n }\n\n public destroyRenderable(htmlText: HTMLText)\n {\n htmlText.off('destroyed', this._destroyRenderableBound);\n this._destroyRenderableById(htmlText.uid);\n }\n\n private _destroyRenderableById(htmlTextUid: number)\n {\n const gpuText = this._gpuText[htmlTextUid];\n\n this._renderer.htmlText.decreaseReferenceCount(gpuText.currentKey);\n\n BigPool.return(gpuText.batchableSprite);\n\n this._gpuText[htmlTextUid] = null;\n }\n\n private _updateText(htmlText: HTMLText)\n {\n const newKey = htmlText._getKey();\n const gpuText = this._getGpuText(htmlText);\n const batchableSprite = gpuText.batchableSprite;\n\n if (gpuText.currentKey !== newKey)\n {\n this._updateGpuText(htmlText).catch((e) =>\n {\n console.error(e);\n });\n }\n\n htmlText._didTextUpdate = false;\n\n const padding = htmlText._style.padding;\n\n updateQuadBounds(batchableSprite.bounds, htmlText._anchor, batchableSprite.texture, padding);\n }\n\n private async _updateGpuText(htmlText: HTMLText)\n {\n htmlText._didTextUpdate = false;\n\n const gpuText = this._getGpuText(htmlText);\n\n if (gpuText.generatingTexture) return;\n\n const newKey = htmlText._getKey();\n\n this._renderer.htmlText.decreaseReferenceCount(gpuText.currentKey);\n\n gpuText.generatingTexture = true;\n\n gpuText.currentKey = newKey;\n\n const resolution = htmlText.resolution ?? this._renderer.resolution;\n\n const texture = await this._renderer.htmlText.getManagedTexture(\n htmlText.text,\n resolution,\n htmlText._style as HTMLTextStyle,\n htmlText._getKey()\n );\n\n const batchableSprite = gpuText.batchableSprite;\n\n batchableSprite.texture = gpuText.texture = texture;\n\n gpuText.generatingTexture = false;\n\n gpuText.textureNeedsUploading = true;\n htmlText.onViewUpdate();\n\n const padding = htmlText._style.padding;\n\n updateQuadBounds(batchableSprite.bounds, htmlText._anchor, batchableSprite.texture, padding);\n }\n\n private _getGpuText(htmlText: HTMLText)\n {\n return this._gpuText[htmlText.uid] || this.initGpuText(htmlText);\n }\n\n public initGpuText(htmlText: HTMLText)\n {\n const gpuTextData: HTMLTextPipe['_gpuText'][number] = {\n texture: Texture.EMPTY,\n currentKey: '--',\n batchableSprite: BigPool.get(BatchableSprite),\n textureNeedsUploading: false,\n generatingTexture: false,\n };\n\n const batchableSprite = gpuTextData.batchableSprite;\n\n batchableSprite.renderable = htmlText;\n batchableSprite.transform = htmlText.groupTransform;\n batchableSprite.texture = Texture.EMPTY;\n batchableSprite.bounds = { minX: 0, maxX: 1, minY: 0, maxY: 0 };\n batchableSprite.roundPixels = (this._renderer._roundPixels | htmlText._roundPixels) as 0 | 1;\n\n htmlText._resolution = htmlText._autoResolution ? this._renderer.resolution : htmlText.resolution;\n this._gpuText[htmlText.uid] = gpuTextData;\n // TODO perhaps manage this outside this pipe? (a bit like how we update / add)\n htmlText.on('destroyed', this._destroyRenderableBound);\n\n return gpuTextData;\n }\n\n public destroy()\n {\n for (const i in this._gpuText)\n {\n this._destroyRenderableById(i as unknown as number);\n }\n\n this._gpuText = null;\n this._renderer = null;\n }\n}\n\n"],"names":[],"mappings":";;;;;;;AAaO,MAAM,YACb,CAAA;AAAA,EAuBI,YAAY,QACZ,EAAA;AAXA,IAAQ,IAAA,CAAA,QAAA,mBAMI,MAAA,CAAA,MAAA,CAAO,IAAI,CAAA,CAAA;AAEvB,IAAA,IAAA,CAAiB,uBAA0B,GAAA,IAAA,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