@lightningjs/renderer 3.0.0-beta6 → 3.0.0-beta7

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 (349) hide show
  1. package/COPYING +1 -0
  2. package/LICENSE +202 -202
  3. package/NOTICE +3 -3
  4. package/README.md +147 -147
  5. package/dist/src/core/CoreNode.d.ts +9 -0
  6. package/dist/src/core/CoreNode.js +17 -0
  7. package/dist/src/core/CoreNode.js.map +1 -1
  8. package/dist/src/core/CoreTextureManager.js +6 -0
  9. package/dist/src/core/CoreTextureManager.js.map +1 -1
  10. package/dist/src/core/Stage.d.ts +16 -0
  11. package/dist/src/core/Stage.js +39 -0
  12. package/dist/src/core/Stage.js.map +1 -1
  13. package/dist/src/core/lib/ImageWorker.js +1 -1
  14. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  15. package/dist/src/core/lib/utils.d.ts +1 -0
  16. package/dist/src/core/lib/utils.js +3 -0
  17. package/dist/src/core/lib/utils.js.map +1 -1
  18. package/dist/src/core/renderers/webgl/WebGlRenderer.d.ts +1 -2
  19. package/dist/src/core/renderers/webgl/WebGlRenderer.js +1 -3
  20. package/dist/src/core/renderers/webgl/WebGlRenderer.js.map +1 -1
  21. package/dist/src/core/renderers/webgl/internal/ShaderUtils.js +35 -35
  22. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  23. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  24. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  25. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  26. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  27. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  28. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  29. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  30. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  31. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  32. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  33. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  34. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  35. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  36. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  37. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  38. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  39. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  40. package/dist/src/core/shaders/webgl/Border.js +82 -82
  41. package/dist/src/core/shaders/webgl/Default.js +47 -47
  42. package/dist/src/core/shaders/webgl/DefaultBatched.js +61 -61
  43. package/dist/src/core/shaders/webgl/HolePunch.js +32 -32
  44. package/dist/src/core/shaders/webgl/LinearGradient.js +36 -36
  45. package/dist/src/core/shaders/webgl/RadialGradient.js +33 -33
  46. package/dist/src/core/shaders/webgl/Rounded.js +71 -71
  47. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +100 -100
  48. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +118 -118
  49. package/dist/src/core/shaders/webgl/RoundedWithShadow.js +54 -54
  50. package/dist/src/core/shaders/webgl/SdfShader.js +62 -62
  51. package/dist/src/core/shaders/webgl/Shadow.js +83 -83
  52. package/dist/src/core/temp.js +77 -0
  53. package/dist/src/core/temp.js.map +1 -0
  54. package/dist/src/core/textures/ImageTexture.js +5 -0
  55. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  56. package/dist/src/main-api/Renderer.js +1 -1
  57. package/dist/src/main-api/Renderer.js.map +1 -1
  58. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  59. package/exports/canvas-shaders.ts +28 -28
  60. package/exports/canvas.ts +45 -45
  61. package/exports/index.ts +90 -90
  62. package/exports/inspector.ts +24 -24
  63. package/exports/utils.ts +44 -44
  64. package/exports/webgl-shaders.ts +28 -28
  65. package/exports/webgl.ts +50 -50
  66. package/package.json +1 -2
  67. package/src/common/CommonTypes.ts +146 -146
  68. package/src/common/EventEmitter.ts +77 -77
  69. package/src/common/IAnimationController.ts +92 -92
  70. package/src/common/IEventEmitter.ts +28 -28
  71. package/src/core/CoreNode.test.ts +202 -202
  72. package/src/core/CoreNode.ts +2495 -2469
  73. package/src/core/CoreShaderManager.ts +188 -188
  74. package/src/core/CoreTextNode.ts +448 -448
  75. package/src/core/CoreTextureManager.ts +607 -600
  76. package/src/core/Stage.ts +797 -751
  77. package/src/core/TextureMemoryManager.ts +394 -394
  78. package/src/core/animations/AnimationManager.ts +38 -38
  79. package/src/core/animations/CoreAnimation.ts +284 -284
  80. package/src/core/animations/CoreAnimationController.ts +157 -157
  81. package/src/core/lib/ContextSpy.ts +41 -41
  82. package/src/core/lib/ImageWorker.ts +286 -280
  83. package/src/core/lib/Matrix3d.ts +244 -244
  84. package/src/core/lib/RenderCoords.ts +71 -71
  85. package/src/core/lib/WebGlContextWrapper.ts +1374 -1374
  86. package/src/core/lib/textureCompression.ts +152 -152
  87. package/src/core/lib/textureSvg.ts +78 -78
  88. package/src/core/lib/utils.ts +390 -386
  89. package/src/core/lib/validateImageBitmap.ts +87 -87
  90. package/src/core/platforms/Platform.ts +77 -77
  91. package/src/core/platforms/web/WebPlatform.ts +84 -84
  92. package/src/core/renderers/CoreContextTexture.ts +43 -43
  93. package/src/core/renderers/CoreRenderOp.ts +22 -22
  94. package/src/core/renderers/CoreRenderer.ts +109 -109
  95. package/src/core/renderers/CoreShaderNode.ts +165 -165
  96. package/src/core/renderers/CoreShaderProgram.ts +23 -23
  97. package/src/core/renderers/canvas/CanvasRenderer.ts +298 -298
  98. package/src/core/renderers/canvas/CanvasShaderNode.ts +99 -99
  99. package/src/core/renderers/canvas/CanvasTexture.ts +156 -156
  100. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +220 -220
  101. package/src/core/renderers/canvas/internal/ColorUtils.ts +85 -85
  102. package/src/core/renderers/webgl/WebGlCtxRenderTexture.ts +86 -86
  103. package/src/core/renderers/webgl/WebGlCtxSubTexture.ts +50 -50
  104. package/src/core/renderers/webgl/WebGlCtxTexture.ts +301 -301
  105. package/src/core/renderers/webgl/WebGlRenderOp.ts +161 -161
  106. package/src/core/renderers/webgl/WebGlRenderer.ts +748 -750
  107. package/src/core/renderers/webgl/WebGlShaderNode.ts +437 -437
  108. package/src/core/renderers/webgl/WebGlShaderProgram.ts +318 -318
  109. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  110. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  111. package/src/core/renderers/webgl/internal/ShaderUtils.ts +281 -281
  112. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  113. package/src/core/shaders/canvas/Border.ts +78 -78
  114. package/src/core/shaders/canvas/HolePunch.ts +62 -62
  115. package/src/core/shaders/canvas/LinearGradient.ts +69 -69
  116. package/src/core/shaders/canvas/RadialGradient.ts +113 -113
  117. package/src/core/shaders/canvas/Rounded.ts +55 -55
  118. package/src/core/shaders/canvas/RoundedWithBorder.ts +72 -72
  119. package/src/core/shaders/canvas/RoundedWithBorderAndShadow.ts +88 -88
  120. package/src/core/shaders/canvas/RoundedWithShadow.ts +69 -69
  121. package/src/core/shaders/canvas/Shadow.ts +52 -52
  122. package/src/core/shaders/canvas/utils/render.ts +151 -151
  123. package/src/core/shaders/templates/BorderTemplate.ts +115 -115
  124. package/src/core/shaders/templates/HolePunchTemplate.ts +82 -82
  125. package/src/core/shaders/templates/LinearGradientTemplate.ts +71 -71
  126. package/src/core/shaders/templates/RadialGradientTemplate.ts +81 -81
  127. package/src/core/shaders/templates/RoundedTemplate.ts +98 -98
  128. package/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.ts +38 -38
  129. package/src/core/shaders/templates/RoundedWithBorderTemplate.ts +35 -35
  130. package/src/core/shaders/templates/RoundedWithShadowTemplate.ts +35 -35
  131. package/src/core/shaders/templates/ShadowTemplate.ts +106 -106
  132. package/src/core/shaders/templates/shaderUtils.ts +47 -47
  133. package/src/core/shaders/webgl/Border.ts +116 -116
  134. package/src/core/shaders/webgl/Default.ts +89 -89
  135. package/src/core/shaders/webgl/DefaultBatched.ts +129 -129
  136. package/src/core/shaders/webgl/HolePunch.ts +78 -78
  137. package/src/core/shaders/webgl/LinearGradient.ts +81 -81
  138. package/src/core/shaders/webgl/RadialGradient.ts +84 -84
  139. package/src/core/shaders/webgl/Rounded.ts +117 -117
  140. package/src/core/shaders/webgl/RoundedWithBorder.ts +144 -144
  141. package/src/core/shaders/webgl/RoundedWithBorderAndShadow.ts +166 -166
  142. package/src/core/shaders/webgl/RoundedWithShadow.ts +98 -98
  143. package/src/core/shaders/webgl/SdfShader.ts +134 -134
  144. package/src/core/shaders/webgl/Shadow.ts +115 -115
  145. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  146. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  147. package/src/core/text-rendering/TrFontManager.ts +183 -183
  148. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -176
  149. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  150. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  151. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  152. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  153. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +94 -94
  154. package/src/core/text-rendering/font-face-types/utils.ts +39 -39
  155. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +514 -514
  156. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +863 -863
  157. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +849 -849
  158. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  159. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  160. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  161. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  162. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  163. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  164. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  165. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +497 -497
  166. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  167. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  168. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  169. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  170. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  171. package/src/core/text-rendering/renderers/TextRenderer.ts +567 -567
  172. package/src/core/textures/ColorTexture.ts +102 -102
  173. package/src/core/textures/ImageTexture.ts +417 -410
  174. package/src/core/textures/NoiseTexture.ts +104 -104
  175. package/src/core/textures/RenderTexture.ts +85 -85
  176. package/src/core/textures/SubTexture.ts +205 -205
  177. package/src/core/textures/Texture.ts +358 -358
  178. package/src/core/utils.ts +227 -227
  179. package/src/env.d.ts +7 -7
  180. package/src/main-api/INode.ts +100 -100
  181. package/src/main-api/Inspector.ts +522 -522
  182. package/src/main-api/Renderer.ts +673 -673
  183. package/src/main-api/utils.ts +45 -45
  184. package/src/utils.ts +267 -267
  185. package/dist/exports/core-api.d.ts +0 -74
  186. package/dist/exports/core-api.js +0 -96
  187. package/dist/exports/core-api.js.map +0 -1
  188. package/dist/exports/main-api.d.ts +0 -30
  189. package/dist/exports/main-api.js +0 -45
  190. package/dist/exports/main-api.js.map +0 -1
  191. package/dist/src/core/CoreExtension.d.ts +0 -12
  192. package/dist/src/core/CoreExtension.js +0 -29
  193. package/dist/src/core/CoreExtension.js.map +0 -1
  194. package/dist/src/core/CoreStuff.js +0 -138
  195. package/dist/src/core/CoreStuff.js.map +0 -1
  196. package/dist/src/core/CoreTexturizer.d.ts +0 -14
  197. package/dist/src/core/CoreTexturizer.js +0 -47
  198. package/dist/src/core/CoreTexturizer.js.map +0 -1
  199. package/dist/src/core/LngNode.d.ts +0 -736
  200. package/dist/src/core/LngNode.js +0 -1174
  201. package/dist/src/core/LngNode.js.map +0 -1
  202. package/dist/src/core/Matrix2DContext.d.ts +0 -15
  203. package/dist/src/core/Matrix2DContext.js +0 -45
  204. package/dist/src/core/Matrix2DContext.js.map +0 -1
  205. package/dist/src/core/ShaderNode.d.ts +0 -10
  206. package/dist/src/core/ShaderNode.js +0 -30
  207. package/dist/src/core/ShaderNode.js.map +0 -1
  208. package/dist/src/core/TextNode.d.ts +0 -103
  209. package/dist/src/core/TextNode.js +0 -331
  210. package/dist/src/core/TextNode.js.map +0 -1
  211. package/dist/src/core/lib/Coords.d.ts +0 -14
  212. package/dist/src/core/lib/Coords.js +0 -55
  213. package/dist/src/core/lib/Coords.js.map +0 -1
  214. package/dist/src/core/lib/glm/common.d.ts +0 -162
  215. package/dist/src/core/lib/glm/common.js +0 -81
  216. package/dist/src/core/lib/glm/common.js.map +0 -1
  217. package/dist/src/core/lib/glm/index.d.ts +0 -11
  218. package/dist/src/core/lib/glm/index.js +0 -30
  219. package/dist/src/core/lib/glm/index.js.map +0 -1
  220. package/dist/src/core/lib/glm/mat2.d.ts +0 -219
  221. package/dist/src/core/lib/glm/mat2.js +0 -396
  222. package/dist/src/core/lib/glm/mat2.js.map +0 -1
  223. package/dist/src/core/lib/glm/mat2d.d.ts +0 -237
  224. package/dist/src/core/lib/glm/mat2d.js +0 -442
  225. package/dist/src/core/lib/glm/mat2d.js.map +0 -1
  226. package/dist/src/core/lib/glm/mat3.d.ts +0 -283
  227. package/dist/src/core/lib/glm/mat3.js +0 -680
  228. package/dist/src/core/lib/glm/mat3.js.map +0 -1
  229. package/dist/src/core/lib/glm/mat4.d.ts +0 -550
  230. package/dist/src/core/lib/glm/mat4.js +0 -1802
  231. package/dist/src/core/lib/glm/mat4.js.map +0 -1
  232. package/dist/src/core/lib/glm/quat.d.ts +0 -363
  233. package/dist/src/core/lib/glm/quat.js +0 -693
  234. package/dist/src/core/lib/glm/quat.js.map +0 -1
  235. package/dist/src/core/lib/glm/quat2.d.ts +0 -356
  236. package/dist/src/core/lib/glm/quat2.js +0 -754
  237. package/dist/src/core/lib/glm/quat2.js.map +0 -1
  238. package/dist/src/core/lib/glm/vec2.d.ts +0 -365
  239. package/dist/src/core/lib/glm/vec2.js +0 -569
  240. package/dist/src/core/lib/glm/vec2.js.map +0 -1
  241. package/dist/src/core/lib/glm/vec3.d.ts +0 -406
  242. package/dist/src/core/lib/glm/vec3.js +0 -720
  243. package/dist/src/core/lib/glm/vec3.js.map +0 -1
  244. package/dist/src/core/lib/glm/vec4.d.ts +0 -330
  245. package/dist/src/core/lib/glm/vec4.js +0 -608
  246. package/dist/src/core/lib/glm/vec4.js.map +0 -1
  247. package/dist/src/core/renderers/CoreShaderManager.d.ts +0 -19
  248. package/dist/src/core/renderers/CoreShaderManager.js +0 -33
  249. package/dist/src/core/renderers/CoreShaderManager.js.map +0 -1
  250. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.d.ts +0 -27
  251. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.js +0 -82
  252. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.js.map +0 -1
  253. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.d.ts +0 -11
  254. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.js +0 -34
  255. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.js.map +0 -1
  256. package/dist/src/core/scene/Scene.d.ts +0 -59
  257. package/dist/src/core/scene/Scene.js +0 -106
  258. package/dist/src/core/scene/Scene.js.map +0 -1
  259. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.d.ts +0 -20
  260. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js +0 -55
  261. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js.map +0 -1
  262. package/dist/src/main-api/ICoreDriver.d.ts +0 -27
  263. package/dist/src/main-api/ICoreDriver.js +0 -20
  264. package/dist/src/main-api/ICoreDriver.js.map +0 -1
  265. package/dist/src/main-api/IRenderDriver.d.ts +0 -20
  266. package/dist/src/main-api/IRenderDriver.js +0 -20
  267. package/dist/src/main-api/IRenderDriver.js.map +0 -1
  268. package/dist/src/main-api/IShaderController.d.ts +0 -14
  269. package/dist/src/main-api/IShaderController.js +0 -30
  270. package/dist/src/main-api/IShaderController.js.map +0 -1
  271. package/dist/src/main-api/IShaderNode.d.ts +0 -17
  272. package/dist/src/main-api/IShaderNode.js +0 -19
  273. package/dist/src/main-api/IShaderNode.js.map +0 -1
  274. package/dist/src/main-api/RendererMain.d.ts +0 -375
  275. package/dist/src/main-api/RendererMain.js +0 -365
  276. package/dist/src/main-api/RendererMain.js.map +0 -1
  277. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.d.ts +0 -9
  278. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js +0 -38
  279. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js.map +0 -1
  280. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.d.ts +0 -56
  281. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js +0 -101
  282. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js.map +0 -1
  283. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.d.ts +0 -32
  284. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js +0 -28
  285. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js.map +0 -1
  286. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +0 -24
  287. package/dist/src/render-drivers/main/MainCoreDriver.js +0 -118
  288. package/dist/src/render-drivers/main/MainCoreDriver.js.map +0 -1
  289. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +0 -99
  290. package/dist/src/render-drivers/main/MainOnlyNode.js +0 -396
  291. package/dist/src/render-drivers/main/MainOnlyNode.js.map +0 -1
  292. package/dist/src/render-drivers/main/MainOnlyShaderController.d.ts +0 -6
  293. package/dist/src/render-drivers/main/MainOnlyShaderController.js +0 -15
  294. package/dist/src/render-drivers/main/MainOnlyShaderController.js.map +0 -1
  295. package/dist/src/render-drivers/main/MainOnlyShaderNode.d.ts +0 -7
  296. package/dist/src/render-drivers/main/MainOnlyShaderNode.js +0 -34
  297. package/dist/src/render-drivers/main/MainOnlyShaderNode.js.map +0 -1
  298. package/dist/src/render-drivers/main/MainOnlyTextNode.d.ts +0 -47
  299. package/dist/src/render-drivers/main/MainOnlyTextNode.js +0 -205
  300. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +0 -1
  301. package/dist/src/render-drivers/main/MainRenderDriver.d.ts +0 -17
  302. package/dist/src/render-drivers/main/MainRenderDriver.js +0 -88
  303. package/dist/src/render-drivers/main/MainRenderDriver.js.map +0 -1
  304. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +0 -90
  305. package/dist/src/render-drivers/threadx/NodeStruct.js +0 -281
  306. package/dist/src/render-drivers/threadx/NodeStruct.js.map +0 -1
  307. package/dist/src/render-drivers/threadx/SharedNode.d.ts +0 -39
  308. package/dist/src/render-drivers/threadx/SharedNode.js +0 -60
  309. package/dist/src/render-drivers/threadx/SharedNode.js.map +0 -1
  310. package/dist/src/render-drivers/threadx/TextNodeStruct.d.ts +0 -44
  311. package/dist/src/render-drivers/threadx/TextNodeStruct.js +0 -201
  312. package/dist/src/render-drivers/threadx/TextNodeStruct.js.map +0 -1
  313. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +0 -28
  314. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +0 -234
  315. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +0 -1
  316. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.d.ts +0 -20
  317. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js +0 -84
  318. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js.map +0 -1
  319. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +0 -44
  320. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +0 -154
  321. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +0 -1
  322. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.d.ts +0 -6
  323. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.js +0 -16
  324. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.js.map +0 -1
  325. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.d.ts +0 -7
  326. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.js +0 -15
  327. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.js.map +0 -1
  328. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.d.ts +0 -28
  329. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js +0 -55
  330. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js.map +0 -1
  331. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.d.ts +0 -21
  332. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.js +0 -198
  333. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.js.map +0 -1
  334. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +0 -70
  335. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js +0 -32
  336. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +0 -1
  337. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.d.ts +0 -19
  338. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +0 -177
  339. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +0 -1
  340. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.d.ts +0 -27
  341. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +0 -108
  342. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +0 -1
  343. package/dist/src/render-drivers/threadx/worker/renderer.d.ts +0 -1
  344. package/dist/src/render-drivers/threadx/worker/renderer.js +0 -145
  345. package/dist/src/render-drivers/threadx/worker/renderer.js.map +0 -1
  346. package/dist/src/render-drivers/utils.d.ts +0 -12
  347. package/dist/src/render-drivers/utils.js +0 -69
  348. package/dist/src/render-drivers/utils.js.map +0 -1
  349. /package/dist/src/core/{CoreStuff.d.ts → temp.d.ts} +0 -0
@@ -1,600 +1,607 @@
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 { TextureType, type Texture } from './textures/Texture.js';
28
- import { EventEmitter } from '../common/EventEmitter.js';
29
- import type { Stage } from './Stage.js';
30
- import {
31
- validateCreateImageBitmap,
32
- type CreateImageBitmapSupport,
33
- } from './lib/validateImageBitmap.js';
34
- import type { Platform } from './platforms/Platform.js';
35
-
36
- /**
37
- * Augmentable map of texture class types
38
- *
39
- * @remarks
40
- * This interface can be augmented by other modules/apps to add additional
41
- * texture types. The ones included directly here are the ones that are
42
- * included in the core library.
43
- */
44
- export interface TextureMap {
45
- ColorTexture: typeof ColorTexture;
46
- ImageTexture: typeof ImageTexture;
47
- NoiseTexture: typeof NoiseTexture;
48
- SubTexture: typeof SubTexture;
49
- RenderTexture: typeof RenderTexture;
50
- }
51
-
52
- export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
53
- ? Props
54
- : never;
55
-
56
- /**
57
- * Contains information about the texture manager's internal state
58
- * for debugging purposes.
59
- */
60
- export interface TextureManagerDebugInfo {
61
- keyCacheSize: number;
62
- }
63
-
64
- export interface TextureManagerSettings {
65
- numImageWorkers: number;
66
- createImageBitmapSupport: 'auto' | 'basic' | 'options' | 'full';
67
- }
68
-
69
- export type ResizeModeOptions =
70
- | {
71
- /**
72
- * Specifies that the image should be resized to cover the specified dimensions.
73
- */
74
- type: 'cover';
75
- /**
76
- * The horizontal clipping position
77
- * To clip the left, set clipX to 0. To clip the right, set clipX to 1.
78
- * clipX 0.5 will clip a equal amount from left and right
79
- *
80
- * @defaultValue 0.5
81
- */
82
- clipX?: number;
83
- /**
84
- * The vertical clipping position
85
- * To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
86
- * clipY 0.5 will clip a equal amount from top and bottom
87
- *
88
- * @defaultValue 0.5
89
- */
90
- clipY?: number;
91
- }
92
- | {
93
- /**
94
- * Specifies that the image should be resized to fit within the specified dimensions.
95
- */
96
- type: 'contain';
97
- };
98
-
99
- /**
100
- * Universal options for all texture types
101
- *
102
- * @remarks
103
- * Texture Options provide a way to specify options that are relevant to the
104
- * texture loading process (including caching) and specifically for how a
105
- * texture is rendered within a specific Node (or set of Nodes).
106
- *
107
- * They are not used in determining the cache key for a texture (except if
108
- * the `cacheKey` option is provided explicitly to oveerride the default
109
- * cache key for the texture instance) nor are they stored/referenced within
110
- * the texture instance itself. Instead, the options are stored/referenced
111
- * within individual Nodes. So a single texture instance can be used in
112
- * multiple Nodes each using a different set of options.
113
- */
114
- export interface TextureOptions {
115
- /**
116
- * Preload the texture immediately even if it's not being rendered to the
117
- * screen.
118
- *
119
- * @remarks
120
- * This allows the texture to be used immediately without any delay when it
121
- * is first needed for rendering. Otherwise the loading process will start
122
- * when the texture is first rendered, which may cause a delay in that texture
123
- * being shown properly.
124
- *
125
- * @defaultValue `false`
126
- */
127
- preload?: boolean;
128
-
129
- /**
130
- * Prevent clean up of the texture when it is no longer being used.
131
- *
132
- * @remarks
133
- * This is useful when you want to keep the texture in memory for later use.
134
- * Regardless of whether the texture is being used or not, it will not be
135
- * cleaned up.
136
- *
137
- * @defaultValue `false`
138
- */
139
- preventCleanup?: boolean;
140
-
141
- /**
142
- * Flip the texture horizontally when rendering
143
- *
144
- * @defaultValue `false`
145
- */
146
- flipX?: boolean;
147
-
148
- /**
149
- * Flip the texture vertically when rendering
150
- *
151
- * @defaultValue `false`
152
- */
153
- flipY?: boolean;
154
-
155
- /**
156
- * You can use resizeMode to determine the clipping automatically from the width
157
- * and height of the source texture. This can be convenient if you are unsure about
158
- * the exact image sizes but want the image to cover a specific area.
159
- *
160
- * The resize modes cover and contain are supported
161
- */
162
- resizeMode?: ResizeModeOptions;
163
- }
164
-
165
- export class CoreTextureManager extends EventEmitter {
166
- /**
167
- * Map of textures by cache key
168
- */
169
- keyCache: Map<string, Texture> = new Map();
170
-
171
- /**
172
- * Map of cache keys by texture
173
- */
174
- inverseKeyCache: WeakMap<Texture, string> = new WeakMap();
175
-
176
- /**
177
- * Map of texture constructors by their type name
178
- */
179
- txConstructors: Partial<TextureMap> = {};
180
-
181
- private downloadTextureSourceQueue: Array<Texture> = [];
182
- private priorityQueue: Array<Texture> = [];
183
- private uploadTextureQueue: Array<Texture> = [];
184
- private initialized = false;
185
- private stage: Stage;
186
- private numImageWorkers: number;
187
-
188
- public platform: Platform;
189
-
190
- imageWorkerManager: ImageWorkerManager | null = null;
191
- hasCreateImageBitmap = false;
192
- imageBitmapSupported = {
193
- basic: false,
194
- options: false,
195
- full: false,
196
- };
197
-
198
- hasWorker = !!self.Worker;
199
- /**
200
- * Renderer that this texture manager is associated with
201
- *
202
- * @remarks
203
- * This MUST be set before the texture manager is used. Otherwise errors
204
- * will occur when using the texture manager.
205
- */
206
- renderer!: CoreRenderer;
207
-
208
- /**
209
- * The current frame time in milliseconds
210
- *
211
- * @remarks
212
- * This is used to populate the `lastRenderableChangeTime` property of
213
- * {@link Texture} instances when their renderable state changes.
214
- *
215
- * Set by stage via `updateFrameTime` method.
216
- */
217
- frameTime = 0;
218
-
219
- constructor(stage: Stage, settings: TextureManagerSettings) {
220
- super();
221
-
222
- const { numImageWorkers, createImageBitmapSupport } = settings;
223
- this.stage = stage;
224
- this.platform = stage.platform;
225
- this.numImageWorkers = numImageWorkers;
226
-
227
- if (createImageBitmapSupport === 'auto') {
228
- validateCreateImageBitmap(this.platform)
229
- .then((result) => {
230
- this.initialize(result);
231
- })
232
- .catch((e) => {
233
- console.warn(
234
- '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
235
- );
236
-
237
- // initialized without image worker manager and createImageBitmap
238
- this.initialized = true;
239
- this.emit('initialized');
240
- });
241
- } else {
242
- this.initialize({
243
- basic: createImageBitmapSupport === 'basic',
244
- options: createImageBitmapSupport === 'options',
245
- full: createImageBitmapSupport === 'full',
246
- });
247
- }
248
-
249
- this.registerTextureType('ImageTexture', ImageTexture);
250
- this.registerTextureType('ColorTexture', ColorTexture);
251
- this.registerTextureType('NoiseTexture', NoiseTexture);
252
- this.registerTextureType('SubTexture', SubTexture);
253
- this.registerTextureType('RenderTexture', RenderTexture);
254
- }
255
-
256
- registerTextureType<Type extends keyof TextureMap>(
257
- textureType: Type,
258
- textureClass: TextureMap[Type],
259
- ): void {
260
- this.txConstructors[textureType] = textureClass;
261
- }
262
-
263
- private initialize(support: CreateImageBitmapSupport) {
264
- this.hasCreateImageBitmap =
265
- support.basic || support.options || support.full;
266
- this.imageBitmapSupported = support;
267
-
268
- if (this.hasCreateImageBitmap === false) {
269
- console.warn(
270
- '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
271
- );
272
- }
273
-
274
- if (
275
- this.hasCreateImageBitmap === true &&
276
- this.hasWorker === true &&
277
- this.numImageWorkers > 0
278
- ) {
279
- this.imageWorkerManager = new ImageWorkerManager(
280
- this.numImageWorkers,
281
- support,
282
- );
283
- } else {
284
- console.warn(
285
- '[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
286
- );
287
- }
288
-
289
- this.initialized = true;
290
- this.emit('initialized');
291
- }
292
-
293
- /**
294
- * Enqueue a texture for downloading its source image.
295
- */
296
- enqueueDownloadTextureSource(texture: Texture): void {
297
- if (!this.downloadTextureSourceQueue.includes(texture)) {
298
- this.downloadTextureSourceQueue.push(texture);
299
- }
300
- }
301
-
302
- /**
303
- * Enqueue a texture for uploading to the GPU.
304
- *
305
- * @param texture - The texture to upload
306
- */
307
- enqueueUploadTexture(texture: Texture): void {
308
- if (this.uploadTextureQueue.includes(texture) === false) {
309
- this.uploadTextureQueue.push(texture);
310
- }
311
- }
312
-
313
- /**
314
- * Create a texture
315
- *
316
- * @param textureType - The type of texture to create
317
- * @param props - The properties to use for the texture
318
- */
319
- createTexture<Type extends keyof TextureMap>(
320
- textureType: Type,
321
- props: ExtractProps<TextureMap[Type]>,
322
- ): InstanceType<TextureMap[Type]> {
323
- let texture: Texture | undefined;
324
- const TextureClass = this.txConstructors[textureType];
325
- if (!TextureClass) {
326
- throw new Error(`Texture type "${textureType}" is not registered`);
327
- }
328
-
329
- const cacheKey = TextureClass.makeCacheKey(props as any);
330
- if (cacheKey && this.keyCache.has(cacheKey)) {
331
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
332
- texture = this.keyCache.get(cacheKey)!;
333
- } else {
334
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
335
- texture = new TextureClass(this, props as any);
336
-
337
- if (cacheKey) {
338
- this.initTextureToCache(texture, cacheKey);
339
- }
340
- }
341
-
342
- return texture as InstanceType<TextureMap[Type]>;
343
- }
344
-
345
- orphanTexture(texture: Texture): void {
346
- // if it is part of the download or upload queue, remove it
347
- this.removeTextureFromQueue(texture);
348
-
349
- if (texture.type === TextureType.subTexture) {
350
- // ignore subtextures
351
- return;
352
- }
353
-
354
- this.stage.txMemManager.addToOrphanedTextures(texture);
355
- }
356
-
357
- /**
358
- * Override loadTexture to use the batched approach.
359
- *
360
- * @param texture - The texture to load
361
- * @param immediate - Whether to prioritize the texture for immediate loading
362
- */
363
- loadTexture(texture: Texture, priority?: boolean): void {
364
- this.stage.txMemManager.removeFromOrphanedTextures(texture);
365
-
366
- if (texture.type === TextureType.subTexture) {
367
- // ignore subtextures - they get loaded through their parent
368
- return;
369
- }
370
-
371
- // if the texture is already loaded, don't load it again
372
- if (
373
- texture.ctxTexture !== undefined &&
374
- texture.ctxTexture.state === 'loaded'
375
- ) {
376
- texture.setState('loaded');
377
- return;
378
- }
379
-
380
- // if the texture is already being processed, don't load it again
381
- if (
382
- this.downloadTextureSourceQueue.includes(texture) === true ||
383
- this.uploadTextureQueue.includes(texture) === true
384
- ) {
385
- return;
386
- }
387
-
388
- // if the texture is already loading, free it, this can happen if the texture is
389
- // orphaned and then reloaded
390
- if (
391
- texture.ctxTexture !== undefined &&
392
- texture.ctxTexture.state === 'loading'
393
- ) {
394
- // if the texture has texture data, queue it for upload
395
- if (texture.textureData !== null) {
396
- this.enqueueUploadTexture(texture);
397
- }
398
-
399
- // else we will have to re-download the texture
400
- texture.free();
401
- }
402
-
403
- // if we're not initialized, just queue the texture into the priority queue
404
- if (this.initialized === false) {
405
- this.priorityQueue.push(texture);
406
- return;
407
- }
408
-
409
- // these types of textures don't need to be downloaded
410
- // Technically the noise texture shouldn't either, but it's a special case
411
- // and not really used in production so who cares ¯\_(ツ)_/¯
412
- if (
413
- (texture.type === TextureType.color ||
414
- texture.type === TextureType.renderToTexture) &&
415
- texture.state !== 'initial'
416
- ) {
417
- texture.setState('fetched');
418
- this.enqueueUploadTexture(texture);
419
- return;
420
- }
421
-
422
- texture.setState('loading');
423
-
424
- // prioritize the texture for immediate loading
425
- if (priority === true) {
426
- texture
427
- .getTextureData()
428
- .then(() => {
429
- this.uploadTexture(texture);
430
- })
431
- .catch((err) => {
432
- console.error(err);
433
- });
434
- }
435
-
436
- // enqueue the texture for download and upload
437
- this.enqueueDownloadTextureSource(texture);
438
- }
439
-
440
- /**
441
- * Upload a texture to the GPU
442
- *
443
- * @param texture Texture to upload
444
- */
445
- uploadTexture(texture: Texture): void {
446
- if (
447
- this.stage.txMemManager.doNotExceedCriticalThreshold === true &&
448
- this.stage.txMemManager.criticalCleanupRequested === true
449
- ) {
450
- // we're at a critical memory threshold, don't upload textures
451
- this.enqueueUploadTexture(texture);
452
- return;
453
- }
454
-
455
- const coreContext = texture.loadCtxTexture();
456
- if (coreContext !== null && coreContext.state === 'loaded') {
457
- texture.setState('loaded');
458
- return;
459
- }
460
-
461
- coreContext.load();
462
- }
463
-
464
- /**
465
- * Check if a texture is being processed
466
- */
467
- isProcessingTexture(texture: Texture): boolean {
468
- return (
469
- this.downloadTextureSourceQueue.includes(texture) === true ||
470
- this.uploadTextureQueue.includes(texture) === true
471
- );
472
- }
473
-
474
- /**
475
- * Process a limited number of downloads and uploads.
476
- *
477
- * @param maxItems - The maximum number of items to process
478
- */
479
- processSome(maxProcessingTime: number): void {
480
- if (this.initialized === false) {
481
- return;
482
- }
483
-
484
- const startTime = this.platform.getTimeStamp();
485
-
486
- // Process priority queue
487
- while (
488
- this.priorityQueue.length > 0 &&
489
- this.platform.getTimeStamp() - startTime < maxProcessingTime
490
- ) {
491
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
492
- const texture = this.priorityQueue.pop()!;
493
- texture.getTextureData().then(() => {
494
- this.uploadTexture(texture);
495
- });
496
- }
497
-
498
- // Process uploads
499
- while (
500
- this.uploadTextureQueue.length > 0 &&
501
- this.platform.getTimeStamp() - startTime < maxProcessingTime
502
- ) {
503
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
504
- this.uploadTexture(this.uploadTextureQueue.pop()!);
505
- }
506
-
507
- // Process downloads
508
- while (
509
- this.downloadTextureSourceQueue.length > 0 &&
510
- this.platform.getTimeStamp() - startTime < maxProcessingTime
511
- ) {
512
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
513
- const texture = this.downloadTextureSourceQueue.shift()!;
514
- texture.getTextureData().then(() => {
515
- if (texture.state === 'fetched') {
516
- this.enqueueUploadTexture(texture);
517
- }
518
- });
519
- }
520
- }
521
-
522
- public hasUpdates(): boolean {
523
- return (
524
- this.downloadTextureSourceQueue.length > 0 ||
525
- this.uploadTextureQueue.length > 0
526
- );
527
- }
528
-
529
- /**
530
- * Initialize a texture to the cache
531
- *
532
- * @param texture Texture to cache
533
- * @param cacheKey Cache key for the texture
534
- */
535
- initTextureToCache(texture: Texture, cacheKey: string) {
536
- const { keyCache, inverseKeyCache } = this;
537
- keyCache.set(cacheKey, texture);
538
- inverseKeyCache.set(texture, cacheKey);
539
- }
540
-
541
- /**
542
- * Get a texture from the cache
543
- *
544
- * @param cacheKey
545
- */
546
- getTextureFromCache(cacheKey: string): Texture | undefined {
547
- return this.keyCache.get(cacheKey);
548
- }
549
-
550
- /**
551
- * Remove a texture from the cache
552
- *
553
- * @remarks
554
- * Called by Texture Cleanup when a texture is freed.
555
- *
556
- * @param texture
557
- */
558
- removeTextureFromCache(texture: Texture) {
559
- const { inverseKeyCache, keyCache } = this;
560
- const cacheKey = inverseKeyCache.get(texture);
561
- if (cacheKey) {
562
- keyCache.delete(cacheKey);
563
- }
564
- }
565
-
566
- /**
567
- * Remove texture from the queue's
568
- *
569
- * @param texture - The texture to remove
570
- */
571
- removeTextureFromQueue(texture: Texture): void {
572
- const downloadIndex = this.downloadTextureSourceQueue.indexOf(texture);
573
- if (downloadIndex !== -1) {
574
- this.downloadTextureSourceQueue.splice(downloadIndex, 1);
575
- }
576
-
577
- const uploadIndex = this.uploadTextureQueue.indexOf(texture);
578
- if (uploadIndex !== -1) {
579
- this.uploadTextureQueue.splice(uploadIndex, 1);
580
- }
581
- }
582
-
583
- /**
584
- * Resolve a parent texture from the cache or fallback to the provided texture.
585
- *
586
- * @param texture - The provided texture to resolve.
587
- * @returns The cached or provided texture.
588
- */
589
- resolveParentTexture(texture: ImageTexture): Texture {
590
- if (!texture?.props) {
591
- return texture;
592
- }
593
-
594
- const cacheKey = ImageTexture.makeCacheKey(texture.props);
595
- const cachedTexture = cacheKey
596
- ? this.getTextureFromCache(cacheKey)
597
- : undefined;
598
- return cachedTexture ?? texture;
599
- }
600
- }
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 { TextureType, type Texture } from './textures/Texture.js';
28
+ import { EventEmitter } from '../common/EventEmitter.js';
29
+ import type { Stage } from './Stage.js';
30
+ import {
31
+ validateCreateImageBitmap,
32
+ type CreateImageBitmapSupport,
33
+ } from './lib/validateImageBitmap.js';
34
+ import type { Platform } from './platforms/Platform.js';
35
+
36
+ /**
37
+ * Augmentable map of texture class types
38
+ *
39
+ * @remarks
40
+ * This interface can be augmented by other modules/apps to add additional
41
+ * texture types. The ones included directly here are the ones that are
42
+ * included in the core library.
43
+ */
44
+ export interface TextureMap {
45
+ ColorTexture: typeof ColorTexture;
46
+ ImageTexture: typeof ImageTexture;
47
+ NoiseTexture: typeof NoiseTexture;
48
+ SubTexture: typeof SubTexture;
49
+ RenderTexture: typeof RenderTexture;
50
+ }
51
+
52
+ export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
53
+ ? Props
54
+ : never;
55
+
56
+ /**
57
+ * Contains information about the texture manager's internal state
58
+ * for debugging purposes.
59
+ */
60
+ export interface TextureManagerDebugInfo {
61
+ keyCacheSize: number;
62
+ }
63
+
64
+ export interface TextureManagerSettings {
65
+ numImageWorkers: number;
66
+ createImageBitmapSupport: 'auto' | 'basic' | 'options' | 'full';
67
+ }
68
+
69
+ export type ResizeModeOptions =
70
+ | {
71
+ /**
72
+ * Specifies that the image should be resized to cover the specified dimensions.
73
+ */
74
+ type: 'cover';
75
+ /**
76
+ * The horizontal clipping position
77
+ * To clip the left, set clipX to 0. To clip the right, set clipX to 1.
78
+ * clipX 0.5 will clip a equal amount from left and right
79
+ *
80
+ * @defaultValue 0.5
81
+ */
82
+ clipX?: number;
83
+ /**
84
+ * The vertical clipping position
85
+ * To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
86
+ * clipY 0.5 will clip a equal amount from top and bottom
87
+ *
88
+ * @defaultValue 0.5
89
+ */
90
+ clipY?: number;
91
+ }
92
+ | {
93
+ /**
94
+ * Specifies that the image should be resized to fit within the specified dimensions.
95
+ */
96
+ type: 'contain';
97
+ };
98
+
99
+ /**
100
+ * Universal options for all texture types
101
+ *
102
+ * @remarks
103
+ * Texture Options provide a way to specify options that are relevant to the
104
+ * texture loading process (including caching) and specifically for how a
105
+ * texture is rendered within a specific Node (or set of Nodes).
106
+ *
107
+ * They are not used in determining the cache key for a texture (except if
108
+ * the `cacheKey` option is provided explicitly to oveerride the default
109
+ * cache key for the texture instance) nor are they stored/referenced within
110
+ * the texture instance itself. Instead, the options are stored/referenced
111
+ * within individual Nodes. So a single texture instance can be used in
112
+ * multiple Nodes each using a different set of options.
113
+ */
114
+ export interface TextureOptions {
115
+ /**
116
+ * Preload the texture immediately even if it's not being rendered to the
117
+ * screen.
118
+ *
119
+ * @remarks
120
+ * This allows the texture to be used immediately without any delay when it
121
+ * is first needed for rendering. Otherwise the loading process will start
122
+ * when the texture is first rendered, which may cause a delay in that texture
123
+ * being shown properly.
124
+ *
125
+ * @defaultValue `false`
126
+ */
127
+ preload?: boolean;
128
+
129
+ /**
130
+ * Prevent clean up of the texture when it is no longer being used.
131
+ *
132
+ * @remarks
133
+ * This is useful when you want to keep the texture in memory for later use.
134
+ * Regardless of whether the texture is being used or not, it will not be
135
+ * cleaned up.
136
+ *
137
+ * @defaultValue `false`
138
+ */
139
+ preventCleanup?: boolean;
140
+
141
+ /**
142
+ * Flip the texture horizontally when rendering
143
+ *
144
+ * @defaultValue `false`
145
+ */
146
+ flipX?: boolean;
147
+
148
+ /**
149
+ * Flip the texture vertically when rendering
150
+ *
151
+ * @defaultValue `false`
152
+ */
153
+ flipY?: boolean;
154
+
155
+ /**
156
+ * You can use resizeMode to determine the clipping automatically from the width
157
+ * and height of the source texture. This can be convenient if you are unsure about
158
+ * the exact image sizes but want the image to cover a specific area.
159
+ *
160
+ * The resize modes cover and contain are supported
161
+ */
162
+ resizeMode?: ResizeModeOptions;
163
+ }
164
+
165
+ export class CoreTextureManager extends EventEmitter {
166
+ /**
167
+ * Map of textures by cache key
168
+ */
169
+ keyCache: Map<string, Texture> = new Map();
170
+
171
+ /**
172
+ * Map of cache keys by texture
173
+ */
174
+ inverseKeyCache: WeakMap<Texture, string> = new WeakMap();
175
+
176
+ /**
177
+ * Map of texture constructors by their type name
178
+ */
179
+ txConstructors: Partial<TextureMap> = {};
180
+
181
+ private downloadTextureSourceQueue: Array<Texture> = [];
182
+ private priorityQueue: Array<Texture> = [];
183
+ private uploadTextureQueue: Array<Texture> = [];
184
+ private initialized = false;
185
+ private stage: Stage;
186
+ private numImageWorkers: number;
187
+
188
+ public platform: Platform;
189
+
190
+ imageWorkerManager: ImageWorkerManager | null = null;
191
+ hasCreateImageBitmap = false;
192
+ imageBitmapSupported = {
193
+ basic: false,
194
+ options: false,
195
+ full: false,
196
+ };
197
+
198
+ hasWorker = !!self.Worker;
199
+ /**
200
+ * Renderer that this texture manager is associated with
201
+ *
202
+ * @remarks
203
+ * This MUST be set before the texture manager is used. Otherwise errors
204
+ * will occur when using the texture manager.
205
+ */
206
+ renderer!: CoreRenderer;
207
+
208
+ /**
209
+ * The current frame time in milliseconds
210
+ *
211
+ * @remarks
212
+ * This is used to populate the `lastRenderableChangeTime` property of
213
+ * {@link Texture} instances when their renderable state changes.
214
+ *
215
+ * Set by stage via `updateFrameTime` method.
216
+ */
217
+ frameTime = 0;
218
+
219
+ constructor(stage: Stage, settings: TextureManagerSettings) {
220
+ super();
221
+
222
+ const { numImageWorkers, createImageBitmapSupport } = settings;
223
+ this.stage = stage;
224
+ this.platform = stage.platform;
225
+ this.numImageWorkers = numImageWorkers;
226
+
227
+ if (createImageBitmapSupport === 'auto') {
228
+ validateCreateImageBitmap(this.platform)
229
+ .then((result) => {
230
+ this.initialize(result);
231
+ })
232
+ .catch((e) => {
233
+ console.warn(
234
+ '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
235
+ );
236
+
237
+ // initialized without image worker manager and createImageBitmap
238
+ this.initialized = true;
239
+ this.emit('initialized');
240
+ });
241
+ } else {
242
+ this.initialize({
243
+ basic: createImageBitmapSupport === 'basic',
244
+ options: createImageBitmapSupport === 'options',
245
+ full: createImageBitmapSupport === 'full',
246
+ });
247
+ }
248
+
249
+ this.registerTextureType('ImageTexture', ImageTexture);
250
+ this.registerTextureType('ColorTexture', ColorTexture);
251
+ this.registerTextureType('NoiseTexture', NoiseTexture);
252
+ this.registerTextureType('SubTexture', SubTexture);
253
+ this.registerTextureType('RenderTexture', RenderTexture);
254
+ }
255
+
256
+ registerTextureType<Type extends keyof TextureMap>(
257
+ textureType: Type,
258
+ textureClass: TextureMap[Type],
259
+ ): void {
260
+ this.txConstructors[textureType] = textureClass;
261
+ }
262
+
263
+ private initialize(support: CreateImageBitmapSupport) {
264
+ this.hasCreateImageBitmap =
265
+ support.basic || support.options || support.full;
266
+ this.imageBitmapSupported = support;
267
+
268
+ if (this.hasCreateImageBitmap === false) {
269
+ console.warn(
270
+ '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
271
+ );
272
+ }
273
+
274
+ if (
275
+ this.hasCreateImageBitmap === true &&
276
+ this.hasWorker === true &&
277
+ this.numImageWorkers > 0
278
+ ) {
279
+ this.imageWorkerManager = new ImageWorkerManager(
280
+ this.numImageWorkers,
281
+ support,
282
+ );
283
+ } else {
284
+ console.warn(
285
+ '[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
286
+ );
287
+ }
288
+
289
+ this.initialized = true;
290
+ this.emit('initialized');
291
+ }
292
+
293
+ /**
294
+ * Enqueue a texture for downloading its source image.
295
+ */
296
+ enqueueDownloadTextureSource(texture: Texture): void {
297
+ if (!this.downloadTextureSourceQueue.includes(texture)) {
298
+ this.downloadTextureSourceQueue.push(texture);
299
+ }
300
+ }
301
+
302
+ /**
303
+ * Enqueue a texture for uploading to the GPU.
304
+ *
305
+ * @param texture - The texture to upload
306
+ */
307
+ enqueueUploadTexture(texture: Texture): void {
308
+ if (this.uploadTextureQueue.includes(texture) === false) {
309
+ this.uploadTextureQueue.push(texture);
310
+ }
311
+ }
312
+
313
+ /**
314
+ * Create a texture
315
+ *
316
+ * @param textureType - The type of texture to create
317
+ * @param props - The properties to use for the texture
318
+ */
319
+ createTexture<Type extends keyof TextureMap>(
320
+ textureType: Type,
321
+ props: ExtractProps<TextureMap[Type]>,
322
+ ): InstanceType<TextureMap[Type]> {
323
+ let texture: Texture | undefined;
324
+ const TextureClass = this.txConstructors[textureType];
325
+ if (!TextureClass) {
326
+ throw new Error(`Texture type "${textureType}" is not registered`);
327
+ }
328
+
329
+ const cacheKey = TextureClass.makeCacheKey(props as any);
330
+ if (cacheKey && this.keyCache.has(cacheKey)) {
331
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
332
+ texture = this.keyCache.get(cacheKey)!;
333
+ } else {
334
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
335
+ texture = new TextureClass(this, props as any);
336
+
337
+ if (cacheKey) {
338
+ this.initTextureToCache(texture, cacheKey);
339
+ }
340
+ }
341
+
342
+ return texture as InstanceType<TextureMap[Type]>;
343
+ }
344
+
345
+ orphanTexture(texture: Texture): void {
346
+ // if it is part of the download or upload queue, remove it
347
+ this.removeTextureFromQueue(texture);
348
+
349
+ if (texture.type === TextureType.subTexture) {
350
+ // ignore subtextures
351
+ return;
352
+ }
353
+
354
+ this.stage.txMemManager.addToOrphanedTextures(texture);
355
+ }
356
+
357
+ /**
358
+ * Override loadTexture to use the batched approach.
359
+ *
360
+ * @param texture - The texture to load
361
+ * @param immediate - Whether to prioritize the texture for immediate loading
362
+ */
363
+ loadTexture(texture: Texture, priority?: boolean): void {
364
+ this.stage.txMemManager.removeFromOrphanedTextures(texture);
365
+
366
+ if (texture.type === TextureType.subTexture) {
367
+ // ignore subtextures - they get loaded through their parent
368
+ return;
369
+ }
370
+
371
+ // if the texture is already loaded, don't load it again
372
+ if (
373
+ texture.ctxTexture !== undefined &&
374
+ texture.ctxTexture.state === 'loaded'
375
+ ) {
376
+ texture.setState('loaded');
377
+ return;
378
+ }
379
+
380
+ // if the texture is already being processed, don't load it again
381
+ if (
382
+ this.downloadTextureSourceQueue.includes(texture) === true ||
383
+ this.uploadTextureQueue.includes(texture) === true
384
+ ) {
385
+ return;
386
+ }
387
+
388
+ // if the texture is already loading, free it, this can happen if the texture is
389
+ // orphaned and then reloaded
390
+ if (
391
+ texture.ctxTexture !== undefined &&
392
+ texture.ctxTexture.state === 'loading'
393
+ ) {
394
+ // if the texture has texture data, queue it for upload
395
+ if (texture.textureData !== null) {
396
+ this.enqueueUploadTexture(texture);
397
+ return;
398
+ }
399
+
400
+ // else we will have to re-download the texture
401
+ texture.free();
402
+ }
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
+ // If the texture failed to load, we need to re-download it.
411
+ if (texture.state === 'failed') {
412
+ texture.free();
413
+ texture.freeTextureData();
414
+ }
415
+
416
+ // these types of textures don't need to be downloaded
417
+ // Technically the noise texture shouldn't either, but it's a special case
418
+ // and not really used in production so who cares ¯\_()_/¯
419
+ if (
420
+ (texture.type === TextureType.color ||
421
+ texture.type === TextureType.renderToTexture) &&
422
+ texture.state !== 'initial'
423
+ ) {
424
+ texture.setState('fetched');
425
+ this.enqueueUploadTexture(texture);
426
+ return;
427
+ }
428
+
429
+ texture.setState('loading');
430
+
431
+ // prioritize the texture for immediate loading
432
+ if (priority === true) {
433
+ texture
434
+ .getTextureData()
435
+ .then(() => {
436
+ this.uploadTexture(texture);
437
+ })
438
+ .catch((err) => {
439
+ console.error(err);
440
+ });
441
+ }
442
+
443
+ // enqueue the texture for download and upload
444
+ this.enqueueDownloadTextureSource(texture);
445
+ }
446
+
447
+ /**
448
+ * Upload a texture to the GPU
449
+ *
450
+ * @param texture Texture to upload
451
+ */
452
+ uploadTexture(texture: Texture): void {
453
+ if (
454
+ this.stage.txMemManager.doNotExceedCriticalThreshold === true &&
455
+ this.stage.txMemManager.criticalCleanupRequested === true
456
+ ) {
457
+ // we're at a critical memory threshold, don't upload textures
458
+ this.enqueueUploadTexture(texture);
459
+ return;
460
+ }
461
+
462
+ const coreContext = texture.loadCtxTexture();
463
+ if (coreContext !== null && coreContext.state === 'loaded') {
464
+ texture.setState('loaded');
465
+ return;
466
+ }
467
+
468
+ coreContext.load();
469
+ }
470
+
471
+ /**
472
+ * Check if a texture is being processed
473
+ */
474
+ isProcessingTexture(texture: Texture): boolean {
475
+ return (
476
+ this.downloadTextureSourceQueue.includes(texture) === true ||
477
+ this.uploadTextureQueue.includes(texture) === true
478
+ );
479
+ }
480
+
481
+ /**
482
+ * Process a limited number of downloads and uploads.
483
+ *
484
+ * @param maxItems - The maximum number of items to process
485
+ */
486
+ processSome(maxProcessingTime: number): void {
487
+ if (this.initialized === false) {
488
+ return;
489
+ }
490
+
491
+ const startTime = this.platform.getTimeStamp();
492
+
493
+ // Process priority queue
494
+ while (
495
+ this.priorityQueue.length > 0 &&
496
+ this.platform.getTimeStamp() - startTime < maxProcessingTime
497
+ ) {
498
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
499
+ const texture = this.priorityQueue.pop()!;
500
+ texture.getTextureData().then(() => {
501
+ this.uploadTexture(texture);
502
+ });
503
+ }
504
+
505
+ // Process uploads
506
+ while (
507
+ this.uploadTextureQueue.length > 0 &&
508
+ this.platform.getTimeStamp() - startTime < maxProcessingTime
509
+ ) {
510
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
511
+ this.uploadTexture(this.uploadTextureQueue.pop()!);
512
+ }
513
+
514
+ // Process downloads
515
+ while (
516
+ this.downloadTextureSourceQueue.length > 0 &&
517
+ this.platform.getTimeStamp() - startTime < maxProcessingTime
518
+ ) {
519
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
520
+ const texture = this.downloadTextureSourceQueue.shift()!;
521
+ texture.getTextureData().then(() => {
522
+ if (texture.state === 'fetched') {
523
+ this.enqueueUploadTexture(texture);
524
+ }
525
+ });
526
+ }
527
+ }
528
+
529
+ public hasUpdates(): boolean {
530
+ return (
531
+ this.downloadTextureSourceQueue.length > 0 ||
532
+ this.uploadTextureQueue.length > 0
533
+ );
534
+ }
535
+
536
+ /**
537
+ * Initialize a texture to the cache
538
+ *
539
+ * @param texture Texture to cache
540
+ * @param cacheKey Cache key for the texture
541
+ */
542
+ initTextureToCache(texture: Texture, cacheKey: string) {
543
+ const { keyCache, inverseKeyCache } = this;
544
+ keyCache.set(cacheKey, texture);
545
+ inverseKeyCache.set(texture, cacheKey);
546
+ }
547
+
548
+ /**
549
+ * Get a texture from the cache
550
+ *
551
+ * @param cacheKey
552
+ */
553
+ getTextureFromCache(cacheKey: string): Texture | undefined {
554
+ return this.keyCache.get(cacheKey);
555
+ }
556
+
557
+ /**
558
+ * Remove a texture from the cache
559
+ *
560
+ * @remarks
561
+ * Called by Texture Cleanup when a texture is freed.
562
+ *
563
+ * @param texture
564
+ */
565
+ removeTextureFromCache(texture: Texture) {
566
+ const { inverseKeyCache, keyCache } = this;
567
+ const cacheKey = inverseKeyCache.get(texture);
568
+ if (cacheKey) {
569
+ keyCache.delete(cacheKey);
570
+ }
571
+ }
572
+
573
+ /**
574
+ * Remove texture from the queue's
575
+ *
576
+ * @param texture - The texture to remove
577
+ */
578
+ removeTextureFromQueue(texture: Texture): void {
579
+ const downloadIndex = this.downloadTextureSourceQueue.indexOf(texture);
580
+ if (downloadIndex !== -1) {
581
+ this.downloadTextureSourceQueue.splice(downloadIndex, 1);
582
+ }
583
+
584
+ const uploadIndex = this.uploadTextureQueue.indexOf(texture);
585
+ if (uploadIndex !== -1) {
586
+ this.uploadTextureQueue.splice(uploadIndex, 1);
587
+ }
588
+ }
589
+
590
+ /**
591
+ * Resolve a parent texture from the cache or fallback to the provided texture.
592
+ *
593
+ * @param texture - The provided texture to resolve.
594
+ * @returns The cached or provided texture.
595
+ */
596
+ resolveParentTexture(texture: ImageTexture): Texture {
597
+ if (!texture?.props) {
598
+ return texture;
599
+ }
600
+
601
+ const cacheKey = ImageTexture.makeCacheKey(texture.props);
602
+ const cachedTexture = cacheKey
603
+ ? this.getTextureFromCache(cacheKey)
604
+ : undefined;
605
+ return cachedTexture ?? texture;
606
+ }
607
+ }