@lightningjs/renderer 2.10.0 → 2.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (327) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +147 -147
  4. package/dist/exports/core-api.d.ts +74 -0
  5. package/dist/exports/core-api.js +96 -0
  6. package/dist/exports/core-api.js.map +1 -0
  7. package/dist/exports/main-api.d.ts +30 -0
  8. package/dist/exports/main-api.js +45 -0
  9. package/dist/exports/main-api.js.map +1 -0
  10. package/dist/src/core/CoreExtension.d.ts +12 -0
  11. package/dist/src/core/CoreExtension.js +29 -0
  12. package/dist/src/core/CoreExtension.js.map +1 -0
  13. package/dist/src/core/CoreStuff.d.ts +1 -0
  14. package/dist/src/core/CoreStuff.js +138 -0
  15. package/dist/src/core/CoreStuff.js.map +1 -0
  16. package/dist/src/core/CoreTextureManager.d.ts +4 -1
  17. package/dist/src/core/CoreTextureManager.js +14 -3
  18. package/dist/src/core/CoreTextureManager.js.map +1 -1
  19. package/dist/src/core/LngNode.d.ts +736 -0
  20. package/dist/src/core/LngNode.js +1174 -0
  21. package/dist/src/core/LngNode.js.map +1 -0
  22. package/dist/src/core/Matrix2DContext.d.ts +15 -0
  23. package/dist/src/core/Matrix2DContext.js +45 -0
  24. package/dist/src/core/Matrix2DContext.js.map +1 -0
  25. package/dist/src/core/ShaderNode.d.ts +10 -0
  26. package/dist/src/core/ShaderNode.js +30 -0
  27. package/dist/src/core/ShaderNode.js.map +1 -0
  28. package/dist/src/core/Stage.js +1 -1
  29. package/dist/src/core/Stage.js.map +1 -1
  30. package/dist/src/core/TextNode.d.ts +103 -0
  31. package/dist/src/core/TextNode.js +331 -0
  32. package/dist/src/core/TextNode.js.map +1 -0
  33. package/dist/src/core/TextureMemoryManager.d.ts +30 -0
  34. package/dist/src/core/TextureMemoryManager.js +70 -37
  35. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  36. package/dist/src/core/lib/Coords.d.ts +14 -0
  37. package/dist/src/core/lib/Coords.js +55 -0
  38. package/dist/src/core/lib/Coords.js.map +1 -0
  39. package/dist/src/core/lib/glm/common.d.ts +162 -0
  40. package/dist/src/core/lib/glm/common.js +81 -0
  41. package/dist/src/core/lib/glm/common.js.map +1 -0
  42. package/dist/src/core/lib/glm/index.d.ts +11 -0
  43. package/dist/src/core/lib/glm/index.js +30 -0
  44. package/dist/src/core/lib/glm/index.js.map +1 -0
  45. package/dist/src/core/lib/glm/mat2.d.ts +219 -0
  46. package/dist/src/core/lib/glm/mat2.js +396 -0
  47. package/dist/src/core/lib/glm/mat2.js.map +1 -0
  48. package/dist/src/core/lib/glm/mat2d.d.ts +237 -0
  49. package/dist/src/core/lib/glm/mat2d.js +442 -0
  50. package/dist/src/core/lib/glm/mat2d.js.map +1 -0
  51. package/dist/src/core/lib/glm/mat3.d.ts +283 -0
  52. package/dist/src/core/lib/glm/mat3.js +680 -0
  53. package/dist/src/core/lib/glm/mat3.js.map +1 -0
  54. package/dist/src/core/lib/glm/mat4.d.ts +550 -0
  55. package/dist/src/core/lib/glm/mat4.js +1802 -0
  56. package/dist/src/core/lib/glm/mat4.js.map +1 -0
  57. package/dist/src/core/lib/glm/quat.d.ts +363 -0
  58. package/dist/src/core/lib/glm/quat.js +693 -0
  59. package/dist/src/core/lib/glm/quat.js.map +1 -0
  60. package/dist/src/core/lib/glm/quat2.d.ts +356 -0
  61. package/dist/src/core/lib/glm/quat2.js +754 -0
  62. package/dist/src/core/lib/glm/quat2.js.map +1 -0
  63. package/dist/src/core/lib/glm/vec2.d.ts +365 -0
  64. package/dist/src/core/lib/glm/vec2.js +569 -0
  65. package/dist/src/core/lib/glm/vec2.js.map +1 -0
  66. package/dist/src/core/lib/glm/vec3.d.ts +406 -0
  67. package/dist/src/core/lib/glm/vec3.js +720 -0
  68. package/dist/src/core/lib/glm/vec3.js.map +1 -0
  69. package/dist/src/core/lib/glm/vec4.d.ts +330 -0
  70. package/dist/src/core/lib/glm/vec4.js +608 -0
  71. package/dist/src/core/lib/glm/vec4.js.map +1 -0
  72. package/dist/src/core/lib/textureSvg.js +1 -1
  73. package/dist/src/core/lib/textureSvg.js.map +1 -1
  74. package/dist/src/core/renderers/CoreShaderManager.d.ts +19 -0
  75. package/dist/src/core/renderers/CoreShaderManager.js +33 -0
  76. package/dist/src/core/renderers/CoreShaderManager.js.map +1 -0
  77. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +4 -2
  78. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -1
  79. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +4 -4
  80. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -1
  81. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +9 -12
  82. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  83. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.d.ts +27 -0
  84. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.js +82 -0
  85. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.js.map +1 -0
  86. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.d.ts +11 -0
  87. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.js +34 -0
  88. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.js.map +1 -0
  89. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  90. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  91. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  92. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  93. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  94. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  95. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  96. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  97. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  98. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  99. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  100. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  101. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  102. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  103. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  104. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  105. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  106. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  107. package/dist/src/core/scene/Scene.d.ts +59 -0
  108. package/dist/src/core/scene/Scene.js +106 -0
  109. package/dist/src/core/scene/Scene.js.map +1 -0
  110. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.d.ts +20 -0
  111. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js +55 -0
  112. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js.map +1 -0
  113. package/dist/src/core/textures/ColorTexture.js +1 -1
  114. package/dist/src/core/textures/ColorTexture.js.map +1 -1
  115. package/dist/src/core/textures/ImageTexture.js +3 -3
  116. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  117. package/dist/src/core/textures/NoiseTexture.js +1 -1
  118. package/dist/src/core/textures/NoiseTexture.js.map +1 -1
  119. package/dist/src/core/textures/RenderTexture.js +1 -1
  120. package/dist/src/core/textures/RenderTexture.js.map +1 -1
  121. package/dist/src/core/textures/SubTexture.d.ts +4 -0
  122. package/dist/src/core/textures/SubTexture.js +54 -16
  123. package/dist/src/core/textures/SubTexture.js.map +1 -1
  124. package/dist/src/core/textures/Texture.d.ts +2 -34
  125. package/dist/src/core/textures/Texture.js +13 -69
  126. package/dist/src/core/textures/Texture.js.map +1 -1
  127. package/dist/src/main-api/ICoreDriver.d.ts +27 -0
  128. package/dist/src/main-api/ICoreDriver.js +20 -0
  129. package/dist/src/main-api/ICoreDriver.js.map +1 -0
  130. package/dist/src/main-api/IRenderDriver.d.ts +20 -0
  131. package/dist/src/main-api/IRenderDriver.js +20 -0
  132. package/dist/src/main-api/IRenderDriver.js.map +1 -0
  133. package/dist/src/main-api/IShaderController.d.ts +14 -0
  134. package/dist/src/main-api/IShaderController.js +30 -0
  135. package/dist/src/main-api/IShaderController.js.map +1 -0
  136. package/dist/src/main-api/IShaderNode.d.ts +17 -0
  137. package/dist/src/main-api/IShaderNode.js +19 -0
  138. package/dist/src/main-api/IShaderNode.js.map +1 -0
  139. package/dist/src/main-api/Renderer.js +1 -0
  140. package/dist/src/main-api/Renderer.js.map +1 -1
  141. package/dist/src/main-api/RendererMain.d.ts +375 -0
  142. package/dist/src/main-api/RendererMain.js +365 -0
  143. package/dist/src/main-api/RendererMain.js.map +1 -0
  144. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.d.ts +9 -0
  145. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js +38 -0
  146. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js.map +1 -0
  147. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.d.ts +56 -0
  148. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js +101 -0
  149. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js.map +1 -0
  150. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.d.ts +32 -0
  151. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js +28 -0
  152. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js.map +1 -0
  153. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +24 -0
  154. package/dist/src/render-drivers/main/MainCoreDriver.js +118 -0
  155. package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -0
  156. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +99 -0
  157. package/dist/src/render-drivers/main/MainOnlyNode.js +396 -0
  158. package/dist/src/render-drivers/main/MainOnlyNode.js.map +1 -0
  159. package/dist/src/render-drivers/main/MainOnlyShaderController.d.ts +6 -0
  160. package/dist/src/render-drivers/main/MainOnlyShaderController.js +15 -0
  161. package/dist/src/render-drivers/main/MainOnlyShaderController.js.map +1 -0
  162. package/dist/src/render-drivers/main/MainOnlyShaderNode.d.ts +7 -0
  163. package/dist/src/render-drivers/main/MainOnlyShaderNode.js +34 -0
  164. package/dist/src/render-drivers/main/MainOnlyShaderNode.js.map +1 -0
  165. package/dist/src/render-drivers/main/MainOnlyTextNode.d.ts +47 -0
  166. package/dist/src/render-drivers/main/MainOnlyTextNode.js +205 -0
  167. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +1 -0
  168. package/dist/src/render-drivers/main/MainRenderDriver.d.ts +17 -0
  169. package/dist/src/render-drivers/main/MainRenderDriver.js +88 -0
  170. package/dist/src/render-drivers/main/MainRenderDriver.js.map +1 -0
  171. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +90 -0
  172. package/dist/src/render-drivers/threadx/NodeStruct.js +281 -0
  173. package/dist/src/render-drivers/threadx/NodeStruct.js.map +1 -0
  174. package/dist/src/render-drivers/threadx/SharedNode.d.ts +39 -0
  175. package/dist/src/render-drivers/threadx/SharedNode.js +60 -0
  176. package/dist/src/render-drivers/threadx/SharedNode.js.map +1 -0
  177. package/dist/src/render-drivers/threadx/TextNodeStruct.d.ts +44 -0
  178. package/dist/src/render-drivers/threadx/TextNodeStruct.js +201 -0
  179. package/dist/src/render-drivers/threadx/TextNodeStruct.js.map +1 -0
  180. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +28 -0
  181. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +234 -0
  182. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -0
  183. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.d.ts +20 -0
  184. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js +84 -0
  185. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js.map +1 -0
  186. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +44 -0
  187. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +154 -0
  188. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +1 -0
  189. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.d.ts +6 -0
  190. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.js +16 -0
  191. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.js.map +1 -0
  192. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.d.ts +7 -0
  193. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.js +15 -0
  194. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.js.map +1 -0
  195. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.d.ts +28 -0
  196. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js +55 -0
  197. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js.map +1 -0
  198. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.d.ts +21 -0
  199. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.js +198 -0
  200. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.js.map +1 -0
  201. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +70 -0
  202. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js +32 -0
  203. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +1 -0
  204. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.d.ts +19 -0
  205. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +177 -0
  206. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +1 -0
  207. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.d.ts +27 -0
  208. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +108 -0
  209. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +1 -0
  210. package/dist/src/render-drivers/threadx/worker/renderer.d.ts +1 -0
  211. package/dist/src/render-drivers/threadx/worker/renderer.js +145 -0
  212. package/dist/src/render-drivers/threadx/worker/renderer.js.map +1 -0
  213. package/dist/src/render-drivers/utils.d.ts +12 -0
  214. package/dist/src/render-drivers/utils.js +69 -0
  215. package/dist/src/render-drivers/utils.js.map +1 -0
  216. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  217. package/exports/canvas.ts +39 -39
  218. package/exports/index.ts +89 -89
  219. package/exports/inspector.ts +24 -24
  220. package/exports/utils.ts +44 -44
  221. package/exports/webgl.ts +38 -38
  222. package/package.json +2 -1
  223. package/scripts/please-use-pnpm.js +13 -13
  224. package/src/common/CommonTypes.ts +139 -139
  225. package/src/common/EventEmitter.ts +77 -77
  226. package/src/common/IAnimationController.ts +92 -92
  227. package/src/common/IEventEmitter.ts +28 -28
  228. package/src/core/CoreNode.test.ts +199 -199
  229. package/src/core/CoreNode.ts +2335 -2335
  230. package/src/core/CoreShaderManager.ts +292 -292
  231. package/src/core/CoreTextNode.ts +455 -455
  232. package/src/core/CoreTextureManager.ts +561 -545
  233. package/src/core/Stage.ts +700 -700
  234. package/src/core/TextureMemoryManager.ts +310 -277
  235. package/src/core/animations/AnimationManager.ts +38 -38
  236. package/src/core/animations/CoreAnimation.ts +340 -340
  237. package/src/core/animations/CoreAnimationController.ts +157 -157
  238. package/src/core/lib/ContextSpy.ts +41 -41
  239. package/src/core/lib/ImageWorker.ts +270 -270
  240. package/src/core/lib/Matrix3d.ts +244 -244
  241. package/src/core/lib/RenderCoords.ts +86 -86
  242. package/src/core/lib/WebGlContextWrapper.ts +1322 -1322
  243. package/src/core/lib/textureCompression.ts +152 -152
  244. package/src/core/lib/textureSvg.ts +78 -78
  245. package/src/core/lib/utils.ts +310 -310
  246. package/src/core/platform.ts +61 -61
  247. package/src/core/renderers/CoreContextTexture.ts +43 -43
  248. package/src/core/renderers/CoreRenderOp.ts +22 -22
  249. package/src/core/renderers/CoreRenderer.ts +114 -114
  250. package/src/core/renderers/CoreShader.ts +41 -41
  251. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +371 -369
  252. package/src/core/renderers/canvas/CanvasCoreTexture.ts +150 -150
  253. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  254. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  255. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  256. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +79 -79
  257. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -50
  258. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +295 -298
  259. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  260. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +805 -805
  261. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  262. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  263. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  264. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  265. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  266. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  267. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  268. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  269. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  270. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  271. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  272. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  273. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  274. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  275. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  276. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  277. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  278. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  279. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  280. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  281. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  282. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  283. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  284. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  285. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  286. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  287. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  288. package/src/core/text-rendering/TrFontManager.ts +183 -183
  289. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -176
  290. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  291. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  292. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  293. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  294. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +94 -94
  295. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -509
  296. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +808 -808
  297. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  298. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  299. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  300. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  301. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  302. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  303. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  304. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  305. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +408 -408
  306. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  307. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  308. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  309. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  310. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  311. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  312. package/src/core/textures/ColorTexture.ts +102 -102
  313. package/src/core/textures/ImageTexture.ts +379 -379
  314. package/src/core/textures/NoiseTexture.ts +104 -104
  315. package/src/core/textures/RenderTexture.ts +85 -85
  316. package/src/core/textures/SubTexture.ts +225 -179
  317. package/src/core/textures/Texture.ts +347 -435
  318. package/src/core/utils.ts +227 -227
  319. package/src/env.d.ts +7 -7
  320. package/src/main-api/DynamicShaderController.ts +104 -104
  321. package/src/main-api/INode.ts +101 -101
  322. package/src/main-api/Inspector.ts +505 -505
  323. package/src/main-api/Renderer.ts +698 -694
  324. package/src/main-api/ShaderController.ts +80 -80
  325. package/src/main-api/utils.ts +45 -45
  326. package/src/utils.ts +248 -248
  327. package/COPYING +0 -1
@@ -1,545 +1,561 @@
1
- /*
2
- * If not stated otherwise in this file or this component's LICENSE file the
3
- * following copyright and licenses apply:
4
- *
5
- * Copyright 2023 Comcast Cable Communications Management, LLC.
6
- *
7
- * Licensed under the Apache License, Version 2.0 (the License);
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- */
19
-
20
- import { ImageWorkerManager } from './lib/ImageWorker.js';
21
- import type { CoreRenderer } from './renderers/CoreRenderer.js';
22
- import { ColorTexture } from './textures/ColorTexture.js';
23
- import { ImageTexture } from './textures/ImageTexture.js';
24
- import { NoiseTexture } from './textures/NoiseTexture.js';
25
- import { SubTexture } from './textures/SubTexture.js';
26
- import { RenderTexture } from './textures/RenderTexture.js';
27
- import type { Texture } from './textures/Texture.js';
28
- import { EventEmitter } from '../common/EventEmitter.js';
29
- import { getTimeStamp } from './platform.js';
30
-
31
- /**
32
- * Augmentable map of texture class types
33
- *
34
- * @remarks
35
- * This interface can be augmented by other modules/apps to add additional
36
- * texture types. The ones included directly here are the ones that are
37
- * included in the core library.
38
- */
39
- export interface TextureMap {
40
- ColorTexture: typeof ColorTexture;
41
- ImageTexture: typeof ImageTexture;
42
- NoiseTexture: typeof NoiseTexture;
43
- SubTexture: typeof SubTexture;
44
- RenderTexture: typeof RenderTexture;
45
- }
46
-
47
- export interface CreateImageBitmapSupport {
48
- basic: boolean; // Supports createImageBitmap(image)
49
- options: boolean; // Supports createImageBitmap(image, options)
50
- full: boolean; // Supports createImageBitmap(image, sx, sy, sw, sh, options)
51
- }
52
-
53
- export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
54
- ? Props
55
- : never;
56
-
57
- /**
58
- * Contains information about the texture manager's internal state
59
- * for debugging purposes.
60
- */
61
- export interface TextureManagerDebugInfo {
62
- keyCacheSize: number;
63
- }
64
-
65
- export type ResizeModeOptions =
66
- | {
67
- /**
68
- * Specifies that the image should be resized to cover the specified dimensions.
69
- */
70
- type: 'cover';
71
- /**
72
- * The horizontal clipping position
73
- * To clip the left, set clipX to 0. To clip the right, set clipX to 1.
74
- * clipX 0.5 will clip a equal amount from left and right
75
- *
76
- * @defaultValue 0.5
77
- */
78
- clipX?: number;
79
- /**
80
- * The vertical clipping position
81
- * To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
82
- * clipY 0.5 will clip a equal amount from top and bottom
83
- *
84
- * @defaultValue 0.5
85
- */
86
- clipY?: number;
87
- }
88
- | {
89
- /**
90
- * Specifies that the image should be resized to fit within the specified dimensions.
91
- */
92
- type: 'contain';
93
- };
94
-
95
- /**
96
- * Universal options for all texture types
97
- *
98
- * @remarks
99
- * Texture Options provide a way to specify options that are relevant to the
100
- * texture loading process (including caching) and specifically for how a
101
- * texture is rendered within a specific Node (or set of Nodes).
102
- *
103
- * They are not used in determining the cache key for a texture (except if
104
- * the `cacheKey` option is provided explicitly to oveerride the default
105
- * cache key for the texture instance) nor are they stored/referenced within
106
- * the texture instance itself. Instead, the options are stored/referenced
107
- * within individual Nodes. So a single texture instance can be used in
108
- * multiple Nodes each using a different set of options.
109
- */
110
- export interface TextureOptions {
111
- /**
112
- * Preload the texture immediately even if it's not being rendered to the
113
- * screen.
114
- *
115
- * @remarks
116
- * This allows the texture to be used immediately without any delay when it
117
- * is first needed for rendering. Otherwise the loading process will start
118
- * when the texture is first rendered, which may cause a delay in that texture
119
- * being shown properly.
120
- *
121
- * @defaultValue `false`
122
- */
123
- preload?: boolean;
124
-
125
- /**
126
- * Prevent clean up of the texture when it is no longer being used.
127
- *
128
- * @remarks
129
- * This is useful when you want to keep the texture in memory for later use.
130
- * Regardless of whether the texture is being used or not, it will not be
131
- * cleaned up.
132
- *
133
- * @defaultValue `false`
134
- */
135
- preventCleanup?: boolean;
136
-
137
- /**
138
- * Flip the texture horizontally when rendering
139
- *
140
- * @defaultValue `false`
141
- */
142
- flipX?: boolean;
143
-
144
- /**
145
- * Flip the texture vertically when rendering
146
- *
147
- * @defaultValue `false`
148
- */
149
- flipY?: boolean;
150
-
151
- /**
152
- * You can use resizeMode to determine the clipping automatically from the width
153
- * and height of the source texture. This can be convenient if you are unsure about
154
- * the exact image sizes but want the image to cover a specific area.
155
- *
156
- * The resize modes cover and contain are supported
157
- */
158
- resizeMode?: ResizeModeOptions;
159
- }
160
-
161
- export class CoreTextureManager extends EventEmitter {
162
- /**
163
- * Map of textures by cache key
164
- */
165
- keyCache: Map<string, Texture> = new Map();
166
-
167
- /**
168
- * Map of cache keys by texture
169
- */
170
- inverseKeyCache: WeakMap<Texture, string> = new WeakMap();
171
-
172
- /**
173
- * Map of texture constructors by their type name
174
- */
175
- txConstructors: Partial<TextureMap> = {};
176
-
177
- private downloadTextureSourceQueue: Array<Texture> = [];
178
- private priorityQueue: Array<Texture> = [];
179
- private uploadTextureQueue: Array<Texture> = [];
180
- private initialized = false;
181
-
182
- imageWorkerManager: ImageWorkerManager | null = null;
183
- hasCreateImageBitmap = !!self.createImageBitmap;
184
- imageBitmapSupported = {
185
- basic: false,
186
- options: false,
187
- full: false,
188
- };
189
-
190
- hasWorker = !!self.Worker;
191
- /**
192
- * Renderer that this texture manager is associated with
193
- *
194
- * @remarks
195
- * This MUST be set before the texture manager is used. Otherwise errors
196
- * will occur when using the texture manager.
197
- */
198
- renderer!: CoreRenderer;
199
-
200
- /**
201
- * The current frame time in milliseconds
202
- *
203
- * @remarks
204
- * This is used to populate the `lastRenderableChangeTime` property of
205
- * {@link Texture} instances when their renderable state changes.
206
- *
207
- * Set by stage via `updateFrameTime` method.
208
- */
209
- frameTime = 0;
210
-
211
- constructor(numImageWorkers: number) {
212
- super();
213
- this.validateCreateImageBitmap()
214
- .then((result) => {
215
- this.hasCreateImageBitmap =
216
- result.basic || result.options || result.full;
217
- this.imageBitmapSupported = result;
218
-
219
- if (!this.hasCreateImageBitmap) {
220
- console.warn(
221
- '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
222
- );
223
- }
224
-
225
- if (
226
- this.hasCreateImageBitmap &&
227
- this.hasWorker &&
228
- numImageWorkers > 0
229
- ) {
230
- this.imageWorkerManager = new ImageWorkerManager(
231
- numImageWorkers,
232
- result,
233
- );
234
- } else {
235
- console.warn(
236
- '[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
237
- );
238
- }
239
-
240
- this.initialized = true;
241
- this.emit('initialized');
242
- })
243
- .catch((e) => {
244
- console.warn(
245
- '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
246
- );
247
-
248
- // initialized without image worker manager and createImageBitmap
249
- this.initialized = true;
250
- this.emit('initialized');
251
- });
252
-
253
- this.registerTextureType('ImageTexture', ImageTexture);
254
- this.registerTextureType('ColorTexture', ColorTexture);
255
- this.registerTextureType('NoiseTexture', NoiseTexture);
256
- this.registerTextureType('SubTexture', SubTexture);
257
- this.registerTextureType('RenderTexture', RenderTexture);
258
- }
259
-
260
- private async validateCreateImageBitmap(): Promise<CreateImageBitmapSupport> {
261
- // Test if createImageBitmap is supported using a simple 1x1 PNG image
262
- // prettier-ignore
263
- const pngBinaryData = new Uint8Array([
264
- 0x89, 0x50, 0x4e, 0x47,
265
- 0x0d, 0x0a, 0x1a, 0x0a, // PNG signature
266
- 0x00, 0x00, 0x00, 0x0d, // IHDR chunk length
267
- 0x49, 0x48, 0x44, 0x52, // "IHDR" chunk type
268
- 0x00, 0x00, 0x00, 0x01, // Width: 1
269
- 0x00, 0x00, 0x00, 0x01, // Height: 1
270
- 0x01, // Bit depth: 1
271
- 0x03, // Color type: Indexed
272
- 0x00, // Compression method: Deflate
273
- 0x00, // Filter method: None
274
- 0x00, // Interlace method: None
275
- 0x25, 0xdb, 0x56, 0xca, // CRC for IHDR
276
- 0x00, 0x00, 0x00, 0x03, // PLTE chunk length
277
- 0x50, 0x4c, 0x54, 0x45, // "PLTE" chunk type
278
- 0x00, 0x00, 0x00, // Palette entry: Black
279
- 0xa7, 0x7a, 0x3d, 0xda, // CRC for PLTE
280
- 0x00, 0x00, 0x00, 0x01, // tRNS chunk length
281
- 0x74, 0x52, 0x4e, 0x53, // "tRNS" chunk type
282
- 0x00, // Transparency for black: Fully transparent
283
- 0x40, 0xe6, 0xd8, 0x66, // CRC for tRNS
284
- 0x00, 0x00, 0x00, 0x0a, // IDAT chunk length
285
- 0x49, 0x44, 0x41, 0x54, // "IDAT" chunk type
286
- 0x08, 0xd7, // Deflate header
287
- 0x63, 0x60, 0x00, 0x00,
288
- 0x00, 0x02, 0x00, 0x01, // Zlib-compressed data
289
- 0xe2, 0x21, 0xbc, 0x33, // CRC for IDAT
290
- 0x00, 0x00, 0x00, 0x00, // IEND chunk length
291
- 0x49, 0x45, 0x4e, 0x44, // "IEND" chunk type
292
- 0xae, 0x42, 0x60, 0x82, // CRC for IEND
293
- ]);
294
-
295
- const support: CreateImageBitmapSupport = {
296
- basic: false,
297
- options: false,
298
- full: false,
299
- };
300
-
301
- // Test basic createImageBitmap support
302
- const blob = new Blob([pngBinaryData], { type: 'image/png' });
303
- const bitmap = await createImageBitmap(blob);
304
- bitmap.close?.();
305
- support.basic = true;
306
-
307
- // Test createImageBitmap with options support
308
- try {
309
- const options = { premultiplyAlpha: 'none' as const };
310
- const bitmapWithOptions = await createImageBitmap(blob, options);
311
- bitmapWithOptions.close?.();
312
- support.options = true;
313
- } catch (e) {
314
- /* ignore */
315
- }
316
-
317
- // Test createImageBitmap with full options support
318
- try {
319
- const bitmapWithFullOptions = await createImageBitmap(blob, 0, 0, 1, 1, {
320
- premultiplyAlpha: 'none',
321
- });
322
- bitmapWithFullOptions.close?.();
323
- support.full = true;
324
- } catch (e) {
325
- /* ignore */
326
- }
327
-
328
- return support;
329
- }
330
-
331
- registerTextureType<Type extends keyof TextureMap>(
332
- textureType: Type,
333
- textureClass: TextureMap[Type],
334
- ): void {
335
- this.txConstructors[textureType] = textureClass;
336
- }
337
-
338
- /**
339
- * Enqueue a texture for downloading its source image.
340
- */
341
- enqueueDownloadTextureSource(texture: Texture): void {
342
- if (!this.downloadTextureSourceQueue.includes(texture)) {
343
- this.downloadTextureSourceQueue.push(texture);
344
- }
345
- }
346
-
347
- /**
348
- * Enqueue a texture for uploading to the GPU.
349
- *
350
- * @param texture - The texture to upload
351
- */
352
- enqueueUploadTexture(texture: Texture): void {
353
- if (this.uploadTextureQueue.includes(texture) === false) {
354
- this.uploadTextureQueue.push(texture);
355
- }
356
- }
357
-
358
- /**
359
- * Create a texture
360
- *
361
- * @param textureType - The type of texture to create
362
- * @param props - The properties to use for the texture
363
- */
364
- createTexture<Type extends keyof TextureMap>(
365
- textureType: Type,
366
- props: ExtractProps<TextureMap[Type]>,
367
- ): InstanceType<TextureMap[Type]> {
368
- let texture: Texture | undefined;
369
- const TextureClass = this.txConstructors[textureType];
370
- if (!TextureClass) {
371
- throw new Error(`Texture type "${textureType}" is not registered`);
372
- }
373
-
374
- const cacheKey = TextureClass.makeCacheKey(props as any);
375
- if (cacheKey && this.keyCache.has(cacheKey)) {
376
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
377
- texture = this.keyCache.get(cacheKey)!;
378
- } else {
379
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
380
- texture = new TextureClass(this, props as any);
381
-
382
- if (cacheKey) {
383
- this.initTextureToCache(texture, cacheKey);
384
- }
385
- }
386
-
387
- return texture as InstanceType<TextureMap[Type]>;
388
- }
389
-
390
- /**
391
- * Override loadTexture to use the batched approach.
392
- *
393
- * @param texture - The texture to load
394
- * @param immediate - Whether to prioritize the texture for immediate loading
395
- */
396
- loadTexture(texture: Texture, priority?: boolean): void {
397
- if (texture.state === 'loaded' || texture.state === 'loading') {
398
- return;
399
- }
400
-
401
- texture.setSourceState('loading');
402
- texture.setCoreCtxState('loading');
403
-
404
- // if we're not initialized, just queue the texture into the priority queue
405
- if (this.initialized === false) {
406
- this.priorityQueue.push(texture);
407
- return;
408
- }
409
-
410
- // prioritize the texture for immediate loading
411
- if (priority === true) {
412
- texture
413
- .getTextureData()
414
- .then(() => {
415
- this.uploadTexture(texture);
416
- })
417
- .catch((err) => {
418
- console.error(err);
419
- });
420
- }
421
-
422
- // enqueue the texture for download and upload
423
- this.enqueueDownloadTextureSource(texture);
424
- }
425
-
426
- /**
427
- * Upload a texture to the GPU
428
- *
429
- * @param texture Texture to upload
430
- */
431
- uploadTexture(texture: Texture): void {
432
- const coreContext = texture.loadCtxTexture();
433
- coreContext.load();
434
- }
435
-
436
- /**
437
- * Process a limited number of downloads and uploads.
438
- *
439
- * @param maxItems - The maximum number of items to process
440
- */
441
- processSome(maxProcessingTime: number): void {
442
- if (this.initialized === false) {
443
- return;
444
- }
445
-
446
- const startTime = getTimeStamp();
447
-
448
- // Process priority queue
449
- while (
450
- this.priorityQueue.length > 0 &&
451
- getTimeStamp() - startTime < maxProcessingTime
452
- ) {
453
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
454
- const texture = this.priorityQueue.shift()!;
455
- texture.getTextureData().then(() => {
456
- this.uploadTexture(texture);
457
- });
458
- }
459
-
460
- // Process uploads
461
- while (
462
- this.uploadTextureQueue.length > 0 &&
463
- getTimeStamp() - startTime < maxProcessingTime
464
- ) {
465
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
466
- this.uploadTexture(this.uploadTextureQueue.shift()!);
467
- }
468
-
469
- // Process downloads
470
- while (
471
- this.downloadTextureSourceQueue.length > 0 &&
472
- getTimeStamp() - startTime < maxProcessingTime
473
- ) {
474
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
475
- const texture = this.downloadTextureSourceQueue.shift()!;
476
- queueMicrotask(() => {
477
- texture.getTextureData().then(() => {
478
- this.enqueueUploadTexture(texture);
479
- });
480
- });
481
- }
482
- }
483
-
484
- public hasUpdates(): boolean {
485
- return (
486
- this.downloadTextureSourceQueue.length > 0 ||
487
- this.uploadTextureQueue.length > 0
488
- );
489
- }
490
-
491
- /**
492
- * Initialize a texture to the cache
493
- *
494
- * @param texture Texture to cache
495
- * @param cacheKey Cache key for the texture
496
- */
497
- initTextureToCache(texture: Texture, cacheKey: string) {
498
- const { keyCache, inverseKeyCache } = this;
499
- keyCache.set(cacheKey, texture);
500
- inverseKeyCache.set(texture, cacheKey);
501
- }
502
-
503
- /**
504
- * Get a texture from the cache
505
- *
506
- * @param cacheKey
507
- */
508
- getTextureFromCache(cacheKey: string): Texture | undefined {
509
- return this.keyCache.get(cacheKey);
510
- }
511
-
512
- /**
513
- * Remove a texture from the cache
514
- *
515
- * @remarks
516
- * Called by Texture Cleanup when a texture is freed.
517
- *
518
- * @param texture
519
- */
520
- removeTextureFromCache(texture: Texture) {
521
- const { inverseKeyCache, keyCache } = this;
522
- const cacheKey = inverseKeyCache.get(texture);
523
- if (cacheKey) {
524
- keyCache.delete(cacheKey);
525
- }
526
- }
527
-
528
- /**
529
- * Resolve a parent texture from the cache or fallback to the provided texture.
530
- *
531
- * @param texture - The provided texture to resolve.
532
- * @returns The cached or provided texture.
533
- */
534
- resolveParentTexture(texture: ImageTexture): Texture {
535
- if (!texture?.props) {
536
- return texture;
537
- }
538
-
539
- const cacheKey = ImageTexture.makeCacheKey(texture.props);
540
- const cachedTexture = cacheKey
541
- ? this.getTextureFromCache(cacheKey)
542
- : undefined;
543
- return cachedTexture ?? texture;
544
- }
545
- }
1
+ /*
2
+ * If not stated otherwise in this file or this component's LICENSE file the
3
+ * following copyright and licenses apply:
4
+ *
5
+ * Copyright 2023 Comcast Cable Communications Management, LLC.
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the License);
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ import { ImageWorkerManager } from './lib/ImageWorker.js';
21
+ import type { CoreRenderer } from './renderers/CoreRenderer.js';
22
+ import { ColorTexture } from './textures/ColorTexture.js';
23
+ import { ImageTexture } from './textures/ImageTexture.js';
24
+ import { NoiseTexture } from './textures/NoiseTexture.js';
25
+ import { SubTexture } from './textures/SubTexture.js';
26
+ import { RenderTexture } from './textures/RenderTexture.js';
27
+ import type { Texture } from './textures/Texture.js';
28
+ import { EventEmitter } from '../common/EventEmitter.js';
29
+ import { getTimeStamp } from './platform.js';
30
+ import type { Stage } from './Stage.js';
31
+
32
+ /**
33
+ * Augmentable map of texture class types
34
+ *
35
+ * @remarks
36
+ * This interface can be augmented by other modules/apps to add additional
37
+ * texture types. The ones included directly here are the ones that are
38
+ * included in the core library.
39
+ */
40
+ export interface TextureMap {
41
+ ColorTexture: typeof ColorTexture;
42
+ ImageTexture: typeof ImageTexture;
43
+ NoiseTexture: typeof NoiseTexture;
44
+ SubTexture: typeof SubTexture;
45
+ RenderTexture: typeof RenderTexture;
46
+ }
47
+
48
+ export interface CreateImageBitmapSupport {
49
+ basic: boolean; // Supports createImageBitmap(image)
50
+ options: boolean; // Supports createImageBitmap(image, options)
51
+ full: boolean; // Supports createImageBitmap(image, sx, sy, sw, sh, options)
52
+ }
53
+
54
+ export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
55
+ ? Props
56
+ : never;
57
+
58
+ /**
59
+ * Contains information about the texture manager's internal state
60
+ * for debugging purposes.
61
+ */
62
+ export interface TextureManagerDebugInfo {
63
+ keyCacheSize: number;
64
+ }
65
+
66
+ export type ResizeModeOptions =
67
+ | {
68
+ /**
69
+ * Specifies that the image should be resized to cover the specified dimensions.
70
+ */
71
+ type: 'cover';
72
+ /**
73
+ * The horizontal clipping position
74
+ * To clip the left, set clipX to 0. To clip the right, set clipX to 1.
75
+ * clipX 0.5 will clip a equal amount from left and right
76
+ *
77
+ * @defaultValue 0.5
78
+ */
79
+ clipX?: number;
80
+ /**
81
+ * The vertical clipping position
82
+ * To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
83
+ * clipY 0.5 will clip a equal amount from top and bottom
84
+ *
85
+ * @defaultValue 0.5
86
+ */
87
+ clipY?: number;
88
+ }
89
+ | {
90
+ /**
91
+ * Specifies that the image should be resized to fit within the specified dimensions.
92
+ */
93
+ type: 'contain';
94
+ };
95
+
96
+ /**
97
+ * Universal options for all texture types
98
+ *
99
+ * @remarks
100
+ * Texture Options provide a way to specify options that are relevant to the
101
+ * texture loading process (including caching) and specifically for how a
102
+ * texture is rendered within a specific Node (or set of Nodes).
103
+ *
104
+ * They are not used in determining the cache key for a texture (except if
105
+ * the `cacheKey` option is provided explicitly to oveerride the default
106
+ * cache key for the texture instance) nor are they stored/referenced within
107
+ * the texture instance itself. Instead, the options are stored/referenced
108
+ * within individual Nodes. So a single texture instance can be used in
109
+ * multiple Nodes each using a different set of options.
110
+ */
111
+ export interface TextureOptions {
112
+ /**
113
+ * Preload the texture immediately even if it's not being rendered to the
114
+ * screen.
115
+ *
116
+ * @remarks
117
+ * This allows the texture to be used immediately without any delay when it
118
+ * is first needed for rendering. Otherwise the loading process will start
119
+ * when the texture is first rendered, which may cause a delay in that texture
120
+ * being shown properly.
121
+ *
122
+ * @defaultValue `false`
123
+ */
124
+ preload?: boolean;
125
+
126
+ /**
127
+ * Prevent clean up of the texture when it is no longer being used.
128
+ *
129
+ * @remarks
130
+ * This is useful when you want to keep the texture in memory for later use.
131
+ * Regardless of whether the texture is being used or not, it will not be
132
+ * cleaned up.
133
+ *
134
+ * @defaultValue `false`
135
+ */
136
+ preventCleanup?: boolean;
137
+
138
+ /**
139
+ * Flip the texture horizontally when rendering
140
+ *
141
+ * @defaultValue `false`
142
+ */
143
+ flipX?: boolean;
144
+
145
+ /**
146
+ * Flip the texture vertically when rendering
147
+ *
148
+ * @defaultValue `false`
149
+ */
150
+ flipY?: boolean;
151
+
152
+ /**
153
+ * You can use resizeMode to determine the clipping automatically from the width
154
+ * and height of the source texture. This can be convenient if you are unsure about
155
+ * the exact image sizes but want the image to cover a specific area.
156
+ *
157
+ * The resize modes cover and contain are supported
158
+ */
159
+ resizeMode?: ResizeModeOptions;
160
+ }
161
+
162
+ export class CoreTextureManager extends EventEmitter {
163
+ /**
164
+ * Map of textures by cache key
165
+ */
166
+ keyCache: Map<string, Texture> = new Map();
167
+
168
+ /**
169
+ * Map of cache keys by texture
170
+ */
171
+ inverseKeyCache: WeakMap<Texture, string> = new WeakMap();
172
+
173
+ /**
174
+ * Map of texture constructors by their type name
175
+ */
176
+ txConstructors: Partial<TextureMap> = {};
177
+
178
+ private downloadTextureSourceQueue: Array<Texture> = [];
179
+ private priorityQueue: Array<Texture> = [];
180
+ private uploadTextureQueue: Array<Texture> = [];
181
+ private initialized = false;
182
+ private stage: Stage;
183
+
184
+ imageWorkerManager: ImageWorkerManager | null = null;
185
+ hasCreateImageBitmap = !!self.createImageBitmap;
186
+ imageBitmapSupported = {
187
+ basic: false,
188
+ options: false,
189
+ full: false,
190
+ };
191
+
192
+ hasWorker = !!self.Worker;
193
+ /**
194
+ * Renderer that this texture manager is associated with
195
+ *
196
+ * @remarks
197
+ * This MUST be set before the texture manager is used. Otherwise errors
198
+ * will occur when using the texture manager.
199
+ */
200
+ renderer!: CoreRenderer;
201
+
202
+ /**
203
+ * The current frame time in milliseconds
204
+ *
205
+ * @remarks
206
+ * This is used to populate the `lastRenderableChangeTime` property of
207
+ * {@link Texture} instances when their renderable state changes.
208
+ *
209
+ * Set by stage via `updateFrameTime` method.
210
+ */
211
+ frameTime = 0;
212
+
213
+ constructor(stage: Stage, numImageWorkers: number) {
214
+ super();
215
+ this.stage = stage;
216
+ this.validateCreateImageBitmap()
217
+ .then((result) => {
218
+ this.hasCreateImageBitmap =
219
+ result.basic || result.options || result.full;
220
+ this.imageBitmapSupported = result;
221
+
222
+ if (!this.hasCreateImageBitmap) {
223
+ console.warn(
224
+ '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
225
+ );
226
+ }
227
+
228
+ if (
229
+ this.hasCreateImageBitmap &&
230
+ this.hasWorker &&
231
+ numImageWorkers > 0
232
+ ) {
233
+ this.imageWorkerManager = new ImageWorkerManager(
234
+ numImageWorkers,
235
+ result,
236
+ );
237
+ } else {
238
+ console.warn(
239
+ '[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
240
+ );
241
+ }
242
+
243
+ this.initialized = true;
244
+ this.emit('initialized');
245
+ })
246
+ .catch((e) => {
247
+ console.warn(
248
+ '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
249
+ );
250
+
251
+ // initialized without image worker manager and createImageBitmap
252
+ this.initialized = true;
253
+ this.emit('initialized');
254
+ });
255
+
256
+ this.registerTextureType('ImageTexture', ImageTexture);
257
+ this.registerTextureType('ColorTexture', ColorTexture);
258
+ this.registerTextureType('NoiseTexture', NoiseTexture);
259
+ this.registerTextureType('SubTexture', SubTexture);
260
+ this.registerTextureType('RenderTexture', RenderTexture);
261
+ }
262
+
263
+ private async validateCreateImageBitmap(): Promise<CreateImageBitmapSupport> {
264
+ // Test if createImageBitmap is supported using a simple 1x1 PNG image
265
+ // prettier-ignore
266
+ const pngBinaryData = new Uint8Array([
267
+ 0x89, 0x50, 0x4e, 0x47,
268
+ 0x0d, 0x0a, 0x1a, 0x0a, // PNG signature
269
+ 0x00, 0x00, 0x00, 0x0d, // IHDR chunk length
270
+ 0x49, 0x48, 0x44, 0x52, // "IHDR" chunk type
271
+ 0x00, 0x00, 0x00, 0x01, // Width: 1
272
+ 0x00, 0x00, 0x00, 0x01, // Height: 1
273
+ 0x01, // Bit depth: 1
274
+ 0x03, // Color type: Indexed
275
+ 0x00, // Compression method: Deflate
276
+ 0x00, // Filter method: None
277
+ 0x00, // Interlace method: None
278
+ 0x25, 0xdb, 0x56, 0xca, // CRC for IHDR
279
+ 0x00, 0x00, 0x00, 0x03, // PLTE chunk length
280
+ 0x50, 0x4c, 0x54, 0x45, // "PLTE" chunk type
281
+ 0x00, 0x00, 0x00, // Palette entry: Black
282
+ 0xa7, 0x7a, 0x3d, 0xda, // CRC for PLTE
283
+ 0x00, 0x00, 0x00, 0x01, // tRNS chunk length
284
+ 0x74, 0x52, 0x4e, 0x53, // "tRNS" chunk type
285
+ 0x00, // Transparency for black: Fully transparent
286
+ 0x40, 0xe6, 0xd8, 0x66, // CRC for tRNS
287
+ 0x00, 0x00, 0x00, 0x0a, // IDAT chunk length
288
+ 0x49, 0x44, 0x41, 0x54, // "IDAT" chunk type
289
+ 0x08, 0xd7, // Deflate header
290
+ 0x63, 0x60, 0x00, 0x00,
291
+ 0x00, 0x02, 0x00, 0x01, // Zlib-compressed data
292
+ 0xe2, 0x21, 0xbc, 0x33, // CRC for IDAT
293
+ 0x00, 0x00, 0x00, 0x00, // IEND chunk length
294
+ 0x49, 0x45, 0x4e, 0x44, // "IEND" chunk type
295
+ 0xae, 0x42, 0x60, 0x82, // CRC for IEND
296
+ ]);
297
+
298
+ const support: CreateImageBitmapSupport = {
299
+ basic: false,
300
+ options: false,
301
+ full: false,
302
+ };
303
+
304
+ // Test basic createImageBitmap support
305
+ const blob = new Blob([pngBinaryData], { type: 'image/png' });
306
+ const bitmap = await createImageBitmap(blob);
307
+ bitmap.close?.();
308
+ support.basic = true;
309
+
310
+ // Test createImageBitmap with options support
311
+ try {
312
+ const options = { premultiplyAlpha: 'none' as const };
313
+ const bitmapWithOptions = await createImageBitmap(blob, options);
314
+ bitmapWithOptions.close?.();
315
+ support.options = true;
316
+ } catch (e) {
317
+ /* ignore */
318
+ }
319
+
320
+ // Test createImageBitmap with full options support
321
+ try {
322
+ const bitmapWithFullOptions = await createImageBitmap(blob, 0, 0, 1, 1, {
323
+ premultiplyAlpha: 'none',
324
+ });
325
+ bitmapWithFullOptions.close?.();
326
+ support.full = true;
327
+ } catch (e) {
328
+ /* ignore */
329
+ }
330
+
331
+ return support;
332
+ }
333
+
334
+ registerTextureType<Type extends keyof TextureMap>(
335
+ textureType: Type,
336
+ textureClass: TextureMap[Type],
337
+ ): void {
338
+ this.txConstructors[textureType] = textureClass;
339
+ }
340
+
341
+ /**
342
+ * Enqueue a texture for downloading its source image.
343
+ */
344
+ enqueueDownloadTextureSource(texture: Texture): void {
345
+ if (!this.downloadTextureSourceQueue.includes(texture)) {
346
+ this.downloadTextureSourceQueue.push(texture);
347
+ }
348
+ }
349
+
350
+ /**
351
+ * Enqueue a texture for uploading to the GPU.
352
+ *
353
+ * @param texture - The texture to upload
354
+ */
355
+ enqueueUploadTexture(texture: Texture): void {
356
+ if (this.uploadTextureQueue.includes(texture) === false) {
357
+ this.uploadTextureQueue.push(texture);
358
+ }
359
+ }
360
+
361
+ /**
362
+ * Create a texture
363
+ *
364
+ * @param textureType - The type of texture to create
365
+ * @param props - The properties to use for the texture
366
+ */
367
+ createTexture<Type extends keyof TextureMap>(
368
+ textureType: Type,
369
+ props: ExtractProps<TextureMap[Type]>,
370
+ ): InstanceType<TextureMap[Type]> {
371
+ let texture: Texture | undefined;
372
+ const TextureClass = this.txConstructors[textureType];
373
+ if (!TextureClass) {
374
+ throw new Error(`Texture type "${textureType}" is not registered`);
375
+ }
376
+
377
+ const cacheKey = TextureClass.makeCacheKey(props as any);
378
+ if (cacheKey && this.keyCache.has(cacheKey)) {
379
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
380
+ texture = this.keyCache.get(cacheKey)!;
381
+ } else {
382
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
383
+ texture = new TextureClass(this, props as any);
384
+
385
+ if (cacheKey) {
386
+ this.initTextureToCache(texture, cacheKey);
387
+ }
388
+ }
389
+
390
+ return texture as InstanceType<TextureMap[Type]>;
391
+ }
392
+
393
+ orphanTexture(texture: Texture): void {
394
+ this.stage.txMemManager.addToOrphanedTextures(texture);
395
+ }
396
+
397
+ /**
398
+ * Override loadTexture to use the batched approach.
399
+ *
400
+ * @param texture - The texture to load
401
+ * @param immediate - Whether to prioritize the texture for immediate loading
402
+ */
403
+ loadTexture(texture: Texture, priority?: boolean): void {
404
+ this.stage.txMemManager.removeFromOrphanedTextures(texture);
405
+
406
+ if (texture.state === 'loaded' || texture.state === 'loading') {
407
+ return;
408
+ }
409
+
410
+ texture.setState('loading');
411
+
412
+ // if we're not initialized, just queue the texture into the priority queue
413
+ if (this.initialized === false) {
414
+ this.priorityQueue.push(texture);
415
+ return;
416
+ }
417
+
418
+ // prioritize the texture for immediate loading
419
+ if (priority === true) {
420
+ texture
421
+ .getTextureData()
422
+ .then(() => {
423
+ this.uploadTexture(texture);
424
+ })
425
+ .catch((err) => {
426
+ console.error(err);
427
+ });
428
+ }
429
+
430
+ // enqueue the texture for download and upload
431
+ this.enqueueDownloadTextureSource(texture);
432
+ }
433
+
434
+ /**
435
+ * Upload a texture to the GPU
436
+ *
437
+ * @param texture Texture to upload
438
+ */
439
+ uploadTexture(texture: Texture): void {
440
+ const coreContext = texture.loadCtxTexture();
441
+ coreContext.load();
442
+ }
443
+
444
+ /**
445
+ * Process a limited number of downloads and uploads.
446
+ *
447
+ * @param maxItems - The maximum number of items to process
448
+ */
449
+ processSome(maxProcessingTime: number): void {
450
+ if (this.initialized === false) {
451
+ return;
452
+ }
453
+
454
+ const startTime = getTimeStamp();
455
+
456
+ // Process priority queue
457
+ while (
458
+ this.priorityQueue.length > 0 &&
459
+ getTimeStamp() - startTime < maxProcessingTime
460
+ ) {
461
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
462
+ const texture = this.priorityQueue.shift()!;
463
+ texture.getTextureData().then(() => {
464
+ if (texture.state === 'failed') {
465
+ return;
466
+ }
467
+
468
+ this.uploadTexture(texture);
469
+ });
470
+ }
471
+
472
+ // Process uploads
473
+ while (
474
+ this.uploadTextureQueue.length > 0 &&
475
+ getTimeStamp() - startTime < maxProcessingTime
476
+ ) {
477
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
478
+ this.uploadTexture(this.uploadTextureQueue.shift()!);
479
+ }
480
+
481
+ // Process downloads
482
+ while (
483
+ this.downloadTextureSourceQueue.length > 0 &&
484
+ getTimeStamp() - startTime < maxProcessingTime
485
+ ) {
486
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
487
+ const texture = this.downloadTextureSourceQueue.shift()!;
488
+ queueMicrotask(() => {
489
+ texture.getTextureData().then(() => {
490
+ if (texture.state === 'failed') {
491
+ return;
492
+ }
493
+
494
+ this.enqueueUploadTexture(texture);
495
+ });
496
+ });
497
+ }
498
+ }
499
+
500
+ public hasUpdates(): boolean {
501
+ return (
502
+ this.downloadTextureSourceQueue.length > 0 ||
503
+ this.uploadTextureQueue.length > 0
504
+ );
505
+ }
506
+
507
+ /**
508
+ * Initialize a texture to the cache
509
+ *
510
+ * @param texture Texture to cache
511
+ * @param cacheKey Cache key for the texture
512
+ */
513
+ initTextureToCache(texture: Texture, cacheKey: string) {
514
+ const { keyCache, inverseKeyCache } = this;
515
+ keyCache.set(cacheKey, texture);
516
+ inverseKeyCache.set(texture, cacheKey);
517
+ }
518
+
519
+ /**
520
+ * Get a texture from the cache
521
+ *
522
+ * @param cacheKey
523
+ */
524
+ getTextureFromCache(cacheKey: string): Texture | undefined {
525
+ return this.keyCache.get(cacheKey);
526
+ }
527
+
528
+ /**
529
+ * Remove a texture from the cache
530
+ *
531
+ * @remarks
532
+ * Called by Texture Cleanup when a texture is freed.
533
+ *
534
+ * @param texture
535
+ */
536
+ removeTextureFromCache(texture: Texture) {
537
+ const { inverseKeyCache, keyCache } = this;
538
+ const cacheKey = inverseKeyCache.get(texture);
539
+ if (cacheKey) {
540
+ keyCache.delete(cacheKey);
541
+ }
542
+ }
543
+
544
+ /**
545
+ * Resolve a parent texture from the cache or fallback to the provided texture.
546
+ *
547
+ * @param texture - The provided texture to resolve.
548
+ * @returns The cached or provided texture.
549
+ */
550
+ resolveParentTexture(texture: ImageTexture): Texture {
551
+ if (!texture?.props) {
552
+ return texture;
553
+ }
554
+
555
+ const cacheKey = ImageTexture.makeCacheKey(texture.props);
556
+ const cachedTexture = cacheKey
557
+ ? this.getTextureFromCache(cacheKey)
558
+ : undefined;
559
+ return cachedTexture ?? texture;
560
+ }
561
+ }