@lightningjs/renderer 3.0.0-beta9 → 3.0.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 (541) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +230 -147
  4. package/dist/exports/canvas.d.ts +1 -1
  5. package/dist/exports/canvas.js +1 -1
  6. package/dist/exports/canvas.js.map +1 -1
  7. package/dist/exports/index.d.ts +5 -6
  8. package/dist/exports/index.js +3 -5
  9. package/dist/exports/index.js.map +1 -1
  10. package/dist/exports/platform.d.ts +7 -0
  11. package/dist/exports/platform.js +27 -0
  12. package/dist/exports/platform.js.map +1 -0
  13. package/dist/exports/webgl.d.ts +2 -1
  14. package/dist/exports/webgl.js +2 -1
  15. package/dist/exports/webgl.js.map +1 -1
  16. package/dist/src/common/CommonTypes.d.ts +15 -3
  17. package/dist/src/core/Autosizer.d.ts +35 -0
  18. package/dist/src/core/Autosizer.js +196 -0
  19. package/dist/src/core/Autosizer.js.map +1 -0
  20. package/dist/src/core/CoreNode.d.ts +107 -59
  21. package/dist/src/core/CoreNode.js +524 -282
  22. package/dist/src/core/CoreNode.js.map +1 -1
  23. package/dist/src/core/CoreTextNode.d.ts +73 -88
  24. package/dist/src/core/CoreTextNode.js +383 -236
  25. package/dist/src/core/CoreTextNode.js.map +1 -1
  26. package/dist/src/core/CoreTextureManager.d.ts +19 -17
  27. package/dist/src/core/CoreTextureManager.js +52 -120
  28. package/dist/src/core/CoreTextureManager.js.map +1 -1
  29. package/dist/src/core/Stage.d.ts +63 -9
  30. package/dist/src/core/Stage.js +233 -133
  31. package/dist/src/core/Stage.js.map +1 -1
  32. package/dist/src/core/TextureError.d.ts +11 -0
  33. package/dist/src/core/TextureError.js +37 -0
  34. package/dist/src/core/TextureError.js.map +1 -0
  35. package/dist/src/core/TextureMemoryManager.d.ts +2 -4
  36. package/dist/src/core/TextureMemoryManager.js +80 -113
  37. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  38. package/dist/src/core/animations/CoreAnimation.d.ts +3 -3
  39. package/dist/src/core/animations/CoreAnimation.js +3 -3
  40. package/dist/src/core/animations/CoreAnimation.js.map +1 -1
  41. package/dist/src/core/animations/CoreAnimationController.d.ts +1 -1
  42. package/dist/src/core/animations/CoreAnimationController.js +4 -2
  43. package/dist/src/core/animations/CoreAnimationController.js.map +1 -1
  44. package/dist/src/core/lib/ImageWorker.js +1 -1
  45. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  46. package/dist/src/core/lib/WebGlContextWrapper.d.ts +69 -97
  47. package/dist/src/core/lib/WebGlContextWrapper.js +192 -152
  48. package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
  49. package/dist/src/core/lib/collectionUtils.d.ts +4 -0
  50. package/dist/src/core/lib/collectionUtils.js +72 -0
  51. package/dist/src/core/lib/collectionUtils.js.map +1 -0
  52. package/dist/src/core/lib/colorCache.d.ts +1 -0
  53. package/dist/src/core/lib/colorCache.js +19 -0
  54. package/dist/src/core/lib/colorCache.js.map +1 -0
  55. package/dist/src/core/lib/colorParser.d.ts +21 -0
  56. package/dist/src/core/lib/colorParser.js +72 -0
  57. package/dist/src/core/lib/colorParser.js.map +1 -0
  58. package/dist/src/core/lib/textureCompression.d.ts +14 -2
  59. package/dist/src/core/lib/textureCompression.js +311 -67
  60. package/dist/src/core/lib/textureCompression.js.map +1 -1
  61. package/dist/src/core/lib/utils.d.ts +6 -6
  62. package/dist/src/core/lib/utils.js +21 -65
  63. package/dist/src/core/lib/utils.js.map +1 -1
  64. package/dist/src/core/lib/validateImageBitmap.d.ts +1 -2
  65. package/dist/src/core/lib/validateImageBitmap.js +4 -4
  66. package/dist/src/core/lib/validateImageBitmap.js.map +1 -1
  67. package/dist/src/core/platform.d.ts +10 -0
  68. package/dist/src/core/platform.js +84 -0
  69. package/dist/src/core/platform.js.map +1 -0
  70. package/dist/src/core/platforms/GlContextWrapper.d.ts +136 -0
  71. package/{src/core/text-rendering/TextRenderingUtils.ts → dist/src/core/platforms/GlContextWrapper.js} +32 -36
  72. package/dist/src/core/platforms/GlContextWrapper.js.map +1 -0
  73. package/dist/src/core/platforms/Platform.d.ts +78 -12
  74. package/dist/src/core/platforms/Platform.js +18 -0
  75. package/dist/src/core/platforms/Platform.js.map +1 -1
  76. package/dist/src/core/platforms/web/WebGlContextWrapper.d.ts +776 -0
  77. package/dist/src/core/platforms/web/WebGlContextWrapper.js +1208 -0
  78. package/dist/src/core/platforms/web/WebGlContextWrapper.js.map +1 -0
  79. package/dist/src/core/platforms/web/WebPlatform.d.ts +17 -2
  80. package/dist/src/core/platforms/web/WebPlatform.js +158 -13
  81. package/dist/src/core/platforms/web/WebPlatform.js.map +1 -1
  82. package/dist/src/core/platforms/web/WebPlatformChrome50.d.ts +19 -0
  83. package/dist/src/core/platforms/web/WebPlatformChrome50.js +54 -0
  84. package/dist/src/core/platforms/web/WebPlatformChrome50.js.map +1 -0
  85. package/dist/src/core/platforms/web/WebPlatformLegacy.d.ts +20 -0
  86. package/dist/src/core/platforms/web/WebPlatformLegacy.js +105 -0
  87. package/dist/src/core/platforms/web/WebPlatformLegacy.js.map +1 -0
  88. package/dist/src/core/platforms/web/WebPlatformNext.d.ts +21 -0
  89. package/dist/src/core/platforms/web/WebPlatformNext.js +52 -0
  90. package/dist/src/core/platforms/web/WebPlatformNext.js.map +1 -0
  91. package/dist/src/core/platforms/web/lib/ImageWorker.d.ts +21 -0
  92. package/dist/src/core/platforms/web/lib/ImageWorker.js +136 -0
  93. package/dist/src/core/platforms/web/lib/ImageWorker.js.map +1 -0
  94. package/dist/src/core/platforms/web/lib/ImageWorkerDefault.d.ts +6 -0
  95. package/dist/src/core/platforms/web/lib/ImageWorkerDefault.js +92 -0
  96. package/dist/src/core/platforms/web/lib/ImageWorkerDefault.js.map +1 -0
  97. package/dist/src/core/platforms/web/lib/ImageWorkerLegacy.d.ts +1 -0
  98. package/dist/src/core/platforms/web/lib/ImageWorkerLegacy.js +63 -0
  99. package/dist/src/core/platforms/web/lib/ImageWorkerLegacy.js.map +1 -0
  100. package/dist/src/core/platforms/web/lib/ImageWorkerNoOptions.d.ts +7 -0
  101. package/dist/src/core/platforms/web/lib/ImageWorkerNoOptions.js +75 -0
  102. package/dist/src/core/platforms/web/lib/ImageWorkerNoOptions.js.map +1 -0
  103. package/dist/src/core/platforms/web/lib/createImageBitmap.d.ts +1 -0
  104. package/dist/src/core/platforms/web/lib/createImageBitmap.js +27 -0
  105. package/dist/src/core/platforms/web/lib/createImageBitmap.js.map +1 -0
  106. package/dist/src/core/platforms/web/lib/textureCompression.d.ts +26 -0
  107. package/dist/src/core/platforms/web/lib/textureCompression.js +301 -0
  108. package/dist/src/core/platforms/web/lib/textureCompression.js.map +1 -0
  109. package/dist/src/core/platforms/web/lib/textureSvg.d.ts +7 -0
  110. package/dist/src/core/platforms/web/lib/textureSvg.js +51 -0
  111. package/dist/src/core/platforms/web/lib/textureSvg.js.map +1 -0
  112. package/dist/src/core/platforms/web/lib/utils.d.ts +5 -0
  113. package/dist/src/core/platforms/web/lib/utils.js +86 -0
  114. package/dist/src/core/platforms/web/lib/utils.js.map +1 -0
  115. package/dist/src/core/renderers/CoreContextTexture.d.ts +2 -1
  116. package/dist/src/core/renderers/CoreContextTexture.js.map +1 -1
  117. package/dist/src/core/renderers/CoreRenderer.d.ts +4 -40
  118. package/dist/src/core/renderers/CoreRenderer.js +3 -4
  119. package/dist/src/core/renderers/CoreRenderer.js.map +1 -1
  120. package/dist/src/core/renderers/CoreShader.d.ts +9 -0
  121. package/{src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts → dist/src/core/renderers/CoreShader.js} +28 -32
  122. package/dist/src/core/renderers/CoreShader.js.map +1 -0
  123. package/dist/src/core/renderers/CoreShaderNode.d.ts +10 -0
  124. package/dist/src/core/renderers/CoreShaderNode.js +19 -2
  125. package/dist/src/core/renderers/CoreShaderNode.js.map +1 -1
  126. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +33 -0
  127. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +250 -0
  128. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -0
  129. package/dist/src/core/renderers/canvas/CanvasCoreTexture.d.ts +17 -0
  130. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +125 -0
  131. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -0
  132. package/dist/src/core/renderers/canvas/CanvasRenderer.d.ts +5 -6
  133. package/dist/src/core/renderers/canvas/CanvasRenderer.js +67 -91
  134. package/dist/src/core/renderers/canvas/CanvasRenderer.js.map +1 -1
  135. package/dist/src/core/renderers/canvas/CanvasShaderNode.d.ts +1 -2
  136. package/dist/src/core/renderers/canvas/CanvasShaderNode.js +5 -4
  137. package/dist/src/core/renderers/canvas/CanvasShaderNode.js.map +1 -1
  138. package/dist/src/core/renderers/canvas/CanvasTexture.d.ts +3 -2
  139. package/dist/src/core/renderers/canvas/CanvasTexture.js +17 -13
  140. package/dist/src/core/renderers/canvas/CanvasTexture.js.map +1 -1
  141. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.d.ts +13 -0
  142. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js +113 -192
  143. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js.map +1 -1
  144. package/dist/src/core/renderers/canvas/internal/ColorUtils.d.ts +0 -2
  145. package/dist/src/core/renderers/canvas/internal/ColorUtils.js +0 -14
  146. package/dist/src/core/renderers/canvas/internal/ColorUtils.js.map +1 -1
  147. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.d.ts +10 -0
  148. package/{src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts → dist/src/core/renderers/canvas/shaders/UnsupportedShader.js} +43 -40
  149. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js.map +1 -0
  150. package/dist/src/core/renderers/webgl/SdfRenderOp.d.ts +33 -0
  151. package/dist/src/core/renderers/webgl/SdfRenderOp.js +98 -0
  152. package/dist/src/core/renderers/webgl/SdfRenderOp.js.map +1 -0
  153. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.d.ts +12 -0
  154. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js +58 -0
  155. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js.map +1 -0
  156. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.d.ts +9 -0
  157. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js +38 -0
  158. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js.map +1 -0
  159. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +69 -0
  160. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +272 -0
  161. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -0
  162. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.d.ts +34 -0
  163. package/dist/src/core/renderers/webgl/{WebGlRenderOp.js → WebGlCoreRenderOp.js} +43 -47
  164. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js.map +1 -0
  165. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +133 -0
  166. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +649 -0
  167. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -0
  168. package/dist/src/core/renderers/webgl/WebGlCoreShader.d.ts +78 -0
  169. package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.d.ts +1 -0
  170. package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.js +2 -0
  171. package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.js.map +1 -0
  172. package/dist/src/core/renderers/webgl/WebGlCoreShader.js +202 -0
  173. package/dist/src/core/renderers/webgl/WebGlCoreShader.js.map +1 -0
  174. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.d.ts +4 -2
  175. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js +14 -6
  176. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js.map +1 -1
  177. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.d.ts +15 -2
  178. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.js +34 -5
  179. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.js.map +1 -1
  180. package/dist/src/core/renderers/webgl/WebGlCtxTexture.d.ts +19 -9
  181. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js +101 -49
  182. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js.map +1 -1
  183. package/dist/src/core/renderers/webgl/WebGlRenderer.d.ts +11 -23
  184. package/dist/src/core/renderers/webgl/WebGlRenderer.js +124 -133
  185. package/dist/src/core/renderers/webgl/WebGlRenderer.js.map +1 -1
  186. package/dist/src/core/renderers/webgl/WebGlShaderNode.d.ts +4 -6
  187. package/dist/src/core/renderers/webgl/WebGlShaderNode.js +3 -3
  188. package/dist/src/core/renderers/webgl/WebGlShaderNode.js.map +1 -1
  189. package/dist/src/core/renderers/webgl/WebGlShaderProgram.d.ts +7 -7
  190. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js +60 -38
  191. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js.map +1 -1
  192. package/dist/src/core/renderers/webgl/internal/RendererUtils.d.ts +4 -4
  193. package/dist/src/core/renderers/webgl/internal/RendererUtils.js +1 -2
  194. package/dist/src/core/renderers/webgl/internal/RendererUtils.js.map +1 -1
  195. package/dist/src/core/renderers/webgl/internal/ShaderUtils.d.ts +3 -3
  196. package/dist/src/core/renderers/webgl/internal/ShaderUtils.js +38 -37
  197. package/dist/src/core/renderers/webgl/internal/ShaderUtils.js.map +1 -1
  198. package/dist/src/core/renderers/webgl/shaders/DefaultShader.d.ts +9 -0
  199. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +84 -0
  200. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js.map +1 -0
  201. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.d.ts +10 -0
  202. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +108 -0
  203. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js.map +1 -0
  204. package/dist/src/core/renderers/webgl/shaders/DynamicShader.d.ts +29 -0
  205. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +408 -0
  206. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js.map +1 -0
  207. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.d.ts +28 -0
  208. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +126 -0
  209. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js.map +1 -0
  210. package/dist/src/core/renderers/webgl/shaders/SdfShader.d.ts +47 -0
  211. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +148 -0
  212. package/dist/src/core/renderers/webgl/shaders/SdfShader.js.map +1 -0
  213. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.d.ts +31 -0
  214. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +71 -0
  215. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js.map +1 -0
  216. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.d.ts +30 -0
  217. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +58 -0
  218. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js.map +1 -0
  219. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.d.ts +31 -0
  220. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +71 -0
  221. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js.map +1 -0
  222. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.d.ts +31 -0
  223. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +71 -0
  224. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js.map +1 -0
  225. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.d.ts +31 -0
  226. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +71 -0
  227. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js.map +1 -0
  228. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.d.ts +9 -0
  229. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.js +136 -0
  230. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.js.map +1 -0
  231. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.d.ts +36 -0
  232. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +85 -0
  233. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js.map +1 -0
  234. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.d.ts +45 -0
  235. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +104 -0
  236. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js.map +1 -0
  237. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.d.ts +22 -0
  238. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +45 -0
  239. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js.map +1 -0
  240. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.d.ts +58 -0
  241. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +80 -0
  242. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js.map +1 -0
  243. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.d.ts +34 -0
  244. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +113 -0
  245. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js.map +1 -0
  246. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.d.ts +40 -0
  247. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +126 -0
  248. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js.map +1 -0
  249. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.d.ts +61 -0
  250. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +127 -0
  251. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js.map +1 -0
  252. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.d.ts +40 -0
  253. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +71 -0
  254. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js.map +1 -0
  255. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.d.ts +115 -0
  256. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.js +61 -0
  257. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.js.map +1 -0
  258. package/dist/src/core/shaders/canvas/Border.d.ts +8 -2
  259. package/dist/src/core/shaders/canvas/Border.js +64 -25
  260. package/dist/src/core/shaders/canvas/Border.js.map +1 -1
  261. package/dist/src/core/shaders/canvas/HolePunch.js +4 -3
  262. package/dist/src/core/shaders/canvas/HolePunch.js.map +1 -1
  263. package/dist/src/core/shaders/canvas/LinearGradient.js +7 -5
  264. package/dist/src/core/shaders/canvas/LinearGradient.js.map +1 -1
  265. package/dist/src/core/shaders/canvas/RadialGradient.js +12 -10
  266. package/dist/src/core/shaders/canvas/RadialGradient.js.map +1 -1
  267. package/dist/src/core/shaders/canvas/Rounded.js +3 -3
  268. package/dist/src/core/shaders/canvas/Rounded.js.map +1 -1
  269. package/dist/src/core/shaders/canvas/RoundedWithBorder.d.ts +6 -3
  270. package/dist/src/core/shaders/canvas/RoundedWithBorder.js +41 -11
  271. package/dist/src/core/shaders/canvas/RoundedWithBorder.js.map +1 -1
  272. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.d.ts +2 -3
  273. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js +46 -9
  274. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js.map +1 -1
  275. package/dist/src/core/shaders/canvas/RoundedWithShadow.js +6 -5
  276. package/dist/src/core/shaders/canvas/RoundedWithShadow.js.map +1 -1
  277. package/dist/src/core/shaders/canvas/Shadow.js +4 -2
  278. package/dist/src/core/shaders/canvas/Shadow.js.map +1 -1
  279. package/dist/src/core/shaders/canvas/utils/render.d.ts +1 -1
  280. package/dist/src/core/shaders/canvas/utils/render.js +31 -18
  281. package/dist/src/core/shaders/canvas/utils/render.js.map +1 -1
  282. package/dist/src/core/shaders/templates/BorderTemplate.d.ts +11 -1
  283. package/dist/src/core/shaders/templates/BorderTemplate.js +30 -10
  284. package/dist/src/core/shaders/templates/BorderTemplate.js.map +1 -1
  285. package/dist/src/core/shaders/templates/HolePunchTemplate.d.ts +2 -2
  286. package/dist/src/core/shaders/templates/HolePunchTemplate.js +2 -2
  287. package/dist/src/core/shaders/templates/HolePunchTemplate.js.map +1 -1
  288. package/dist/src/core/shaders/templates/RadialGradientTemplate.d.ts +8 -6
  289. package/dist/src/core/shaders/templates/RadialGradientTemplate.js +2 -2
  290. package/dist/src/core/shaders/templates/RadialGradientTemplate.js.map +1 -1
  291. package/dist/src/core/shaders/webgl/Border.js +138 -84
  292. package/dist/src/core/shaders/webgl/Border.js.map +1 -1
  293. package/dist/src/core/shaders/webgl/Default.js +46 -47
  294. package/dist/src/core/shaders/webgl/Default.js.map +1 -1
  295. package/dist/src/core/shaders/webgl/DefaultBatched.js +61 -61
  296. package/dist/src/core/shaders/webgl/HolePunch.js +34 -34
  297. package/dist/src/core/shaders/webgl/HolePunch.js.map +1 -1
  298. package/dist/src/core/shaders/webgl/LinearGradient.js +60 -36
  299. package/dist/src/core/shaders/webgl/LinearGradient.js.map +1 -1
  300. package/dist/src/core/shaders/webgl/RadialGradient.js +60 -37
  301. package/dist/src/core/shaders/webgl/RadialGradient.js.map +1 -1
  302. package/dist/src/core/shaders/webgl/Rounded.js +74 -72
  303. package/dist/src/core/shaders/webgl/Rounded.js.map +1 -1
  304. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +172 -113
  305. package/dist/src/core/shaders/webgl/RoundedWithBorder.js.map +1 -1
  306. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +191 -132
  307. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js.map +1 -1
  308. package/dist/src/core/shaders/webgl/RoundedWithShadow.js +57 -55
  309. package/dist/src/core/shaders/webgl/RoundedWithShadow.js.map +1 -1
  310. package/dist/src/core/shaders/webgl/SdfShader.d.ts +0 -2
  311. package/dist/src/core/shaders/webgl/SdfShader.js +56 -66
  312. package/dist/src/core/shaders/webgl/SdfShader.js.map +1 -1
  313. package/dist/src/core/shaders/webgl/Shadow.js +89 -83
  314. package/dist/src/core/shaders/webgl/Shadow.js.map +1 -1
  315. package/dist/src/core/text-rendering/CanvasFontHandler.d.ts +59 -0
  316. package/dist/src/core/text-rendering/CanvasFontHandler.js +224 -0
  317. package/dist/src/core/text-rendering/CanvasFontHandler.js.map +1 -0
  318. package/dist/src/core/text-rendering/CanvasTextRenderer.d.ts +17 -0
  319. package/dist/src/core/text-rendering/CanvasTextRenderer.js +157 -0
  320. package/dist/src/core/text-rendering/CanvasTextRenderer.js.map +1 -0
  321. package/dist/src/core/text-rendering/SdfFontHandler.d.ts +182 -0
  322. package/dist/src/core/text-rendering/SdfFontHandler.js +379 -0
  323. package/dist/src/core/text-rendering/SdfFontHandler.js.map +1 -0
  324. package/dist/src/core/text-rendering/SdfTextRenderer.d.ts +17 -0
  325. package/dist/src/core/text-rendering/SdfTextRenderer.js +301 -0
  326. package/dist/src/core/text-rendering/SdfTextRenderer.js.map +1 -0
  327. package/dist/src/core/text-rendering/TextLayoutEngine.d.ts +18 -0
  328. package/dist/src/core/text-rendering/TextLayoutEngine.js +380 -0
  329. package/dist/src/core/text-rendering/TextLayoutEngine.js.map +1 -0
  330. package/dist/src/core/text-rendering/TextRenderer.d.ts +383 -0
  331. package/{src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts → dist/src/core/text-rendering/TextRenderer.js} +20 -38
  332. package/dist/src/core/text-rendering/TextRenderer.js.map +1 -0
  333. package/dist/src/core/text-rendering/TextTextureRendererUtils.js.map +1 -1
  334. package/dist/src/core/text-rendering/Utils.d.ts +30 -0
  335. package/dist/src/core/text-rendering/Utils.js +84 -0
  336. package/dist/src/core/text-rendering/Utils.js.map +1 -0
  337. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +2 -2
  338. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  339. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +0 -5
  340. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  341. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.d.ts +1 -7
  342. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js +2 -50
  343. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js.map +1 -1
  344. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.d.ts +3 -2
  345. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js +87 -46
  346. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
  347. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.d.ts +1 -1
  348. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +8 -66
  349. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
  350. package/dist/src/core/text-rendering/renderers/TextRenderer.d.ts +4 -14
  351. package/dist/src/core/text-rendering/renderers/TextRenderer.js +0 -3
  352. package/dist/src/core/text-rendering/renderers/TextRenderer.js.map +1 -1
  353. package/dist/src/core/textures/ColorTexture.d.ts +1 -1
  354. package/dist/src/core/textures/ColorTexture.js +3 -4
  355. package/dist/src/core/textures/ColorTexture.js.map +1 -1
  356. package/dist/src/core/textures/ImageTexture.d.ts +33 -14
  357. package/dist/src/core/textures/ImageTexture.js +46 -125
  358. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  359. package/dist/src/core/textures/NoiseTexture.d.ts +3 -3
  360. package/dist/src/core/textures/NoiseTexture.js +8 -8
  361. package/dist/src/core/textures/NoiseTexture.js.map +1 -1
  362. package/dist/src/core/textures/RenderTexture.d.ts +7 -7
  363. package/dist/src/core/textures/RenderTexture.js +12 -12
  364. package/dist/src/core/textures/RenderTexture.js.map +1 -1
  365. package/dist/src/core/textures/SubTexture.d.ts +6 -8
  366. package/dist/src/core/textures/SubTexture.js +22 -40
  367. package/dist/src/core/textures/SubTexture.js.map +1 -1
  368. package/dist/src/core/textures/Texture.d.ts +74 -15
  369. package/dist/src/core/textures/Texture.js +131 -19
  370. package/dist/src/core/textures/Texture.js.map +1 -1
  371. package/dist/src/core/utils.d.ts +2 -1
  372. package/dist/src/core/utils.js +1 -1
  373. package/dist/src/core/utils.js.map +1 -1
  374. package/dist/src/main-api/DynamicShaderController.d.ts +29 -0
  375. package/dist/src/main-api/DynamicShaderController.js +58 -0
  376. package/dist/src/main-api/DynamicShaderController.js.map +1 -0
  377. package/dist/src/main-api/Inspector.d.ts +129 -1
  378. package/dist/src/main-api/Inspector.js +462 -23
  379. package/dist/src/main-api/Inspector.js.map +1 -1
  380. package/dist/src/main-api/Renderer.d.ts +223 -41
  381. package/dist/src/main-api/Renderer.js +107 -62
  382. package/dist/src/main-api/Renderer.js.map +1 -1
  383. package/dist/src/main-api/ShaderController.d.ts +31 -0
  384. package/dist/src/main-api/ShaderController.js +37 -0
  385. package/dist/src/main-api/ShaderController.js.map +1 -0
  386. package/dist/src/utils.d.ts +0 -2
  387. package/dist/src/utils.js +0 -36
  388. package/dist/src/utils.js.map +1 -1
  389. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  390. package/exports/canvas-shaders.ts +28 -28
  391. package/exports/canvas.ts +45 -45
  392. package/exports/index.ts +90 -90
  393. package/exports/inspector.ts +24 -24
  394. package/exports/platform.ts +31 -0
  395. package/exports/utils.ts +50 -50
  396. package/exports/webgl-shaders.ts +28 -28
  397. package/exports/webgl.ts +52 -50
  398. package/package.json +16 -15
  399. package/src/common/CommonTypes.ts +163 -146
  400. package/src/common/EventEmitter.ts +77 -77
  401. package/src/common/IAnimationController.ts +92 -92
  402. package/src/common/IEventEmitter.ts +28 -28
  403. package/src/core/Autosizer.ts +224 -0
  404. package/src/core/CoreNode.test.ts +365 -202
  405. package/src/core/CoreNode.ts +2785 -2489
  406. package/src/core/CoreShaderManager.ts +188 -188
  407. package/src/core/CoreTextNode.test.ts +311 -0
  408. package/src/core/CoreTextNode.ts +607 -451
  409. package/src/core/CoreTextureManager.ts +484 -548
  410. package/src/core/Stage.ts +927 -800
  411. package/src/core/TextureError.ts +46 -0
  412. package/src/core/TextureMemoryManager.ts +418 -462
  413. package/src/core/animations/AnimationManager.ts +38 -38
  414. package/src/core/animations/CoreAnimation.ts +290 -291
  415. package/src/core/animations/CoreAnimationController.ts +169 -166
  416. package/src/core/lib/ContextSpy.ts +41 -41
  417. package/src/core/lib/Matrix3d.ts +244 -244
  418. package/src/core/lib/RenderCoords.ts +71 -71
  419. package/src/core/lib/collectionUtils.ts +83 -0
  420. package/src/core/lib/colorCache.ts +20 -0
  421. package/src/core/{renderers/canvas/internal/ColorUtils.ts → lib/colorParser.ts} +85 -85
  422. package/src/core/lib/utils.ts +337 -390
  423. package/src/core/platforms/GlContextWrapper.ts +291 -0
  424. package/src/core/platforms/Platform.ts +176 -77
  425. package/src/core/{lib → platforms/web}/WebGlContextWrapper.ts +1547 -1368
  426. package/src/core/platforms/web/WebPlatform.ts +306 -84
  427. package/src/core/platforms/web/WebPlatformChrome50.ts +63 -0
  428. package/src/core/platforms/web/WebPlatformLegacy.ts +150 -0
  429. package/src/core/platforms/web/WebPlatformNext.ts +57 -0
  430. package/src/core/platforms/web/lib/ImageWorker.ts +192 -0
  431. package/src/core/platforms/web/lib/ImageWorkerDefault.ts +117 -0
  432. package/src/core/platforms/web/lib/ImageWorkerLegacy.ts +87 -0
  433. package/src/core/platforms/web/lib/ImageWorkerNoOptions.ts +99 -0
  434. package/src/core/platforms/web/lib/createImageBitmap.ts +40 -0
  435. package/src/core/platforms/web/lib/textureCompression.ts +391 -0
  436. package/src/core/{lib → platforms/web/lib}/textureSvg.ts +66 -78
  437. package/src/core/platforms/web/lib/utils.ts +105 -0
  438. package/src/core/renderers/CoreContextTexture.ts +44 -43
  439. package/src/core/renderers/CoreRenderOp.ts +22 -22
  440. package/src/core/renderers/CoreRenderer.ts +71 -110
  441. package/src/core/renderers/CoreShaderNode.ts +202 -175
  442. package/src/core/renderers/CoreShaderProgram.ts +23 -23
  443. package/src/core/renderers/canvas/CanvasRenderer.ts +258 -302
  444. package/src/core/renderers/canvas/CanvasShaderNode.ts +95 -96
  445. package/src/core/renderers/canvas/CanvasTexture.ts +160 -156
  446. package/src/core/renderers/webgl/SdfRenderOp.ts +106 -0
  447. package/src/core/renderers/webgl/WebGlCtxRenderTexture.ts +89 -86
  448. package/src/core/renderers/webgl/WebGlCtxSubTexture.ts +95 -50
  449. package/src/core/renderers/webgl/WebGlCtxTexture.test.ts +113 -0
  450. package/src/core/renderers/webgl/WebGlCtxTexture.ts +350 -298
  451. package/src/core/renderers/webgl/WebGlRenderer.ts +727 -747
  452. package/src/core/renderers/webgl/WebGlShaderNode.ts +430 -435
  453. package/src/core/renderers/webgl/WebGlShaderProgram.ts +364 -341
  454. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  455. package/src/core/renderers/webgl/internal/RendererUtils.ts +150 -155
  456. package/src/core/renderers/webgl/internal/ShaderUtils.ts +283 -281
  457. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  458. package/src/core/shaders/canvas/Border.ts +132 -78
  459. package/src/core/shaders/canvas/HolePunch.ts +56 -62
  460. package/src/core/shaders/canvas/LinearGradient.ts +73 -71
  461. package/src/core/shaders/canvas/RadialGradient.ts +96 -99
  462. package/src/core/shaders/canvas/Rounded.ts +55 -55
  463. package/src/core/shaders/canvas/RoundedWithBorder.ts +122 -74
  464. package/src/core/shaders/canvas/RoundedWithBorderAndShadow.ts +136 -90
  465. package/src/core/shaders/canvas/RoundedWithShadow.ts +71 -70
  466. package/src/core/shaders/canvas/Shadow.ts +54 -52
  467. package/src/core/shaders/canvas/utils/render.ts +160 -151
  468. package/src/core/shaders/templates/BorderTemplate.ts +145 -115
  469. package/src/core/shaders/templates/HolePunchTemplate.ts +82 -82
  470. package/src/core/shaders/templates/LinearGradientTemplate.ts +71 -71
  471. package/src/core/shaders/templates/RadialGradientTemplate.ts +83 -81
  472. package/src/core/shaders/templates/RoundedTemplate.ts +98 -98
  473. package/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.ts +38 -38
  474. package/src/core/shaders/templates/RoundedWithBorderTemplate.ts +35 -35
  475. package/src/core/shaders/templates/RoundedWithShadowTemplate.ts +35 -35
  476. package/src/core/shaders/templates/ShadowTemplate.ts +106 -106
  477. package/src/core/shaders/utils.ts +46 -46
  478. package/src/core/shaders/webgl/Border.ts +169 -116
  479. package/src/core/shaders/webgl/Default.ts +88 -89
  480. package/src/core/shaders/webgl/DefaultBatched.ts +129 -129
  481. package/src/core/shaders/webgl/HolePunch.ts +75 -75
  482. package/src/core/shaders/webgl/LinearGradient.ts +106 -82
  483. package/src/core/shaders/webgl/RadialGradient.ts +108 -85
  484. package/src/core/shaders/webgl/Rounded.ts +115 -117
  485. package/src/core/shaders/webgl/RoundedWithBorder.ts +210 -155
  486. package/src/core/shaders/webgl/RoundedWithBorderAndShadow.ts +234 -175
  487. package/src/core/shaders/webgl/RoundedWithShadow.ts +96 -98
  488. package/src/core/shaders/webgl/SdfShader.ts +122 -134
  489. package/src/core/shaders/webgl/Shadow.ts +121 -115
  490. package/src/core/text-rendering/CanvasFontHandler.ts +304 -0
  491. package/src/core/text-rendering/CanvasTextRenderer.ts +255 -0
  492. package/src/core/text-rendering/SdfFontHandler.ts +581 -0
  493. package/src/core/text-rendering/SdfTextRenderer.ts +403 -0
  494. package/src/core/text-rendering/TextLayoutEngine.ts +672 -0
  495. package/src/core/text-rendering/TextRenderer.ts +444 -0
  496. package/src/core/text-rendering/Utils.ts +99 -0
  497. package/src/core/text-rendering/tests/TextLayoutEngine.test.ts +453 -0
  498. package/src/core/textures/ColorTexture.ts +104 -102
  499. package/src/core/textures/ImageTexture.ts +292 -418
  500. package/src/core/textures/NoiseTexture.ts +106 -104
  501. package/src/core/textures/RenderTexture.ts +87 -85
  502. package/src/core/textures/SubTexture.ts +184 -205
  503. package/src/core/textures/Texture.ts +524 -381
  504. package/src/core/utils.ts +229 -227
  505. package/src/env.d.ts +7 -7
  506. package/src/main-api/INode.ts +100 -100
  507. package/src/main-api/Inspector.ts +1278 -569
  508. package/src/main-api/Renderer.ts +1030 -818
  509. package/src/main-api/utils.ts +45 -45
  510. package/src/utils.ts +220 -267
  511. package/COPYING +0 -1
  512. package/dist/src/core/renderers/webgl/WebGlRenderOp.d.ts +0 -44
  513. package/dist/src/core/renderers/webgl/WebGlRenderOp.js.map +0 -1
  514. package/src/core/lib/ImageWorker.ts +0 -286
  515. package/src/core/lib/textureCompression.ts +0 -152
  516. package/src/core/lib/validateImageBitmap.ts +0 -87
  517. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +0 -220
  518. package/src/core/renderers/webgl/WebGlRenderOp.ts +0 -161
  519. package/src/core/text-rendering/TextTextureRendererUtils.ts +0 -263
  520. package/src/core/text-rendering/TrFontManager.ts +0 -183
  521. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +0 -176
  522. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +0 -139
  523. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +0 -173
  524. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +0 -171
  525. package/src/core/text-rendering/font-face-types/TrFontFace.ts +0 -187
  526. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +0 -94
  527. package/src/core/text-rendering/font-face-types/utils.ts +0 -39
  528. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +0 -514
  529. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +0 -863
  530. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +0 -793
  531. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +0 -48
  532. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +0 -66
  533. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +0 -52
  534. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +0 -117
  535. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +0 -133
  536. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +0 -497
  537. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +0 -49
  538. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +0 -52
  539. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +0 -205
  540. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +0 -93
  541. package/src/core/text-rendering/renderers/TextRenderer.ts +0 -567
@@ -1,1368 +1,1547 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-return */
2
- /* eslint-disable @typescript-eslint/no-explicit-any */
3
- /* eslint-disable @typescript-eslint/no-unsafe-argument */
4
-
5
- import { assertTruthy } from '../../utils.js';
6
- import type {
7
- Vec2,
8
- Vec3,
9
- Vec4,
10
- } from '../renderers/webgl/internal/ShaderUtils.js';
11
- import { isWebGl2 } from '../renderers/webgl/internal/WebGlUtils.js';
12
-
13
- /**
14
- * Optimized WebGL Context Wrapper
15
- *
16
- * @remarks
17
- * This class contains the subset of the WebGLRenderingContext & WebGL2RenderingContext
18
- * API that is used by the renderer. Select high volume WebGL methods include
19
- * caching optimizations to avoid making WebGL calls if the state is already set
20
- * to the desired value.
21
- *
22
- * While most methods contained are direct passthroughs to the WebGL context,
23
- * some methods combine multiple WebGL calls into one for convenience, modify
24
- * arguments to be more convenient, or are replaced by more specific methods.
25
- *
26
- * Not all methods are optimized. Only methods that are called frequently
27
- * and/or have a high cost are optimized.
28
- *
29
- * A subset of GLenum constants are also exposed as properties on this class
30
- * for convenience.
31
- */
32
- export class WebGlContextWrapper {
33
- //#region Cached WebGL State
34
- private activeTextureUnit = 0;
35
- private texture2dUnits: Array<WebGLTexture | null>;
36
- private texture2dParams: WeakMap<
37
- WebGLTexture,
38
- Record<number, number | undefined>
39
- > = new WeakMap();
40
- private scissorEnabled;
41
- private scissorX: number;
42
- private scissorY: number;
43
- private scissorWidth: number;
44
- private scissorHeight: number;
45
- private blendEnabled;
46
- private blendSrcRgb: number;
47
- private blendDstRgb: number;
48
- private blendSrcAlpha: number;
49
- private blendDstAlpha: number;
50
- private boundArrayBuffer: WebGLBuffer | null;
51
- private boundElementArrayBuffer: WebGLBuffer | null;
52
- private curProgram: WebGLProgram | null;
53
- private curUniformLocations: Record<string, WebGLUniformLocation> = {};
54
- //#endregion Cached WebGL State
55
-
56
- //#region Canvas
57
- public readonly canvas;
58
- //#endregion Canvas
59
-
60
- //#region WebGL Enums
61
- public readonly MAX_RENDERBUFFER_SIZE;
62
- public readonly MAX_TEXTURE_SIZE;
63
- public readonly MAX_VIEWPORT_DIMS;
64
- public readonly MAX_VERTEX_TEXTURE_IMAGE_UNITS;
65
- public readonly MAX_TEXTURE_IMAGE_UNITS;
66
- public readonly MAX_COMBINED_TEXTURE_IMAGE_UNITS;
67
- public readonly MAX_VERTEX_ATTRIBS;
68
- public readonly MAX_VARYING_VECTORS;
69
- public readonly MAX_VERTEX_UNIFORM_VECTORS;
70
- public readonly MAX_FRAGMENT_UNIFORM_VECTORS;
71
- public readonly TEXTURE_MAG_FILTER;
72
- public readonly TEXTURE_MIN_FILTER;
73
- public readonly TEXTURE_WRAP_S;
74
- public readonly TEXTURE_WRAP_T;
75
- public readonly LINEAR;
76
- public readonly CLAMP_TO_EDGE;
77
- public readonly RGB;
78
- public readonly RGBA;
79
- public readonly UNSIGNED_BYTE;
80
- public readonly UNPACK_PREMULTIPLY_ALPHA_WEBGL;
81
- public readonly UNPACK_FLIP_Y_WEBGL;
82
- public readonly FLOAT;
83
- public readonly TRIANGLES;
84
- public readonly UNSIGNED_SHORT;
85
- public readonly ONE;
86
- public readonly ONE_MINUS_SRC_ALPHA;
87
- public readonly VERTEX_SHADER;
88
- public readonly FRAGMENT_SHADER;
89
- public readonly STATIC_DRAW;
90
- public readonly COMPILE_STATUS;
91
- public readonly LINK_STATUS;
92
- public readonly DYNAMIC_DRAW;
93
- public readonly COLOR_ATTACHMENT0;
94
- public readonly INVALID_ENUM: number;
95
- public readonly INVALID_OPERATION: number;
96
- //#endregion WebGL Enums
97
-
98
- constructor(private gl: WebGLRenderingContext | WebGL2RenderingContext) {
99
- // The following code extracts the current state of the WebGL context
100
- // to our local JavaScript cached version of it. This is so we can
101
- // avoid making WebGL calls if we don't need to.
102
- // We could assume that the WebGL context is in a default state, but
103
- // in the future we may want to support restoring a broken WebGL context
104
- // and this will help with that.
105
- this.activeTextureUnit =
106
- (gl.getParameter(gl.ACTIVE_TEXTURE) as number) - gl.TEXTURE0;
107
- const maxTextureUnits = gl.getParameter(
108
- gl.MAX_TEXTURE_IMAGE_UNITS,
109
- ) as number;
110
- // save current texture units
111
- this.texture2dUnits = new Array<undefined>(maxTextureUnits)
112
- .fill(undefined)
113
- .map((_, i) => {
114
- this.activeTexture(i);
115
- return gl.getParameter(gl.TEXTURE_BINDING_2D) as WebGLTexture;
116
- });
117
- // restore active texture unit
118
- this.activeTexture(this.activeTextureUnit);
119
- this.scissorEnabled = gl.isEnabled(gl.SCISSOR_TEST);
120
-
121
- const scissorBox = gl.getParameter(gl.SCISSOR_BOX) as [
122
- number,
123
- number,
124
- number,
125
- number,
126
- ];
127
- this.scissorX = scissorBox[0];
128
- this.scissorY = scissorBox[1];
129
- this.scissorWidth = scissorBox[2];
130
- this.scissorHeight = scissorBox[3];
131
-
132
- this.blendEnabled = gl.isEnabled(gl.BLEND);
133
- this.blendSrcRgb = gl.getParameter(gl.BLEND_SRC_RGB) as number;
134
- this.blendDstRgb = gl.getParameter(gl.BLEND_DST_RGB) as number;
135
- this.blendSrcAlpha = gl.getParameter(gl.BLEND_SRC_ALPHA) as number;
136
- this.blendDstAlpha = gl.getParameter(gl.BLEND_DST_ALPHA) as number;
137
-
138
- this.boundArrayBuffer = gl.getParameter(
139
- gl.ARRAY_BUFFER_BINDING,
140
- ) as WebGLBuffer;
141
- this.boundElementArrayBuffer = gl.getParameter(
142
- gl.ELEMENT_ARRAY_BUFFER_BINDING,
143
- ) as WebGLBuffer;
144
-
145
- this.curProgram = gl.getParameter(
146
- gl.CURRENT_PROGRAM,
147
- ) as WebGLProgram | null;
148
-
149
- this.canvas = gl.canvas;
150
-
151
- // Extract GLenums
152
- this.MAX_RENDERBUFFER_SIZE = gl.MAX_RENDERBUFFER_SIZE;
153
- this.MAX_TEXTURE_SIZE = gl.MAX_TEXTURE_SIZE;
154
- this.MAX_VIEWPORT_DIMS = gl.MAX_VIEWPORT_DIMS;
155
- this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
156
- this.MAX_TEXTURE_IMAGE_UNITS = gl.MAX_TEXTURE_IMAGE_UNITS;
157
- this.MAX_COMBINED_TEXTURE_IMAGE_UNITS = gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS;
158
- this.MAX_VERTEX_ATTRIBS = gl.MAX_VERTEX_ATTRIBS;
159
- this.MAX_VARYING_VECTORS = gl.MAX_VARYING_VECTORS;
160
- this.MAX_VERTEX_UNIFORM_VECTORS = gl.MAX_VERTEX_UNIFORM_VECTORS;
161
- this.MAX_FRAGMENT_UNIFORM_VECTORS = gl.MAX_FRAGMENT_UNIFORM_VECTORS;
162
- this.TEXTURE_MAG_FILTER = gl.TEXTURE_MAG_FILTER;
163
- this.TEXTURE_MIN_FILTER = gl.TEXTURE_MIN_FILTER;
164
- this.TEXTURE_WRAP_S = gl.TEXTURE_WRAP_S;
165
- this.TEXTURE_WRAP_T = gl.TEXTURE_WRAP_T;
166
- this.LINEAR = gl.LINEAR;
167
- this.CLAMP_TO_EDGE = gl.CLAMP_TO_EDGE;
168
- this.RGB = gl.RGB;
169
- this.RGBA = gl.RGBA;
170
- this.UNSIGNED_BYTE = gl.UNSIGNED_BYTE;
171
- this.UNPACK_PREMULTIPLY_ALPHA_WEBGL = gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL;
172
- this.UNPACK_FLIP_Y_WEBGL = gl.UNPACK_FLIP_Y_WEBGL;
173
- this.FLOAT = gl.FLOAT;
174
- this.TRIANGLES = gl.TRIANGLES;
175
- this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
176
- this.ONE = gl.ONE;
177
- this.ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA;
178
- this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
179
- this.TRIANGLES = gl.TRIANGLES;
180
- this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
181
- this.VERTEX_SHADER = gl.VERTEX_SHADER;
182
- this.FRAGMENT_SHADER = gl.FRAGMENT_SHADER;
183
- this.STATIC_DRAW = gl.STATIC_DRAW;
184
- this.COMPILE_STATUS = gl.COMPILE_STATUS;
185
- this.LINK_STATUS = gl.LINK_STATUS;
186
- this.DYNAMIC_DRAW = gl.DYNAMIC_DRAW;
187
- this.COLOR_ATTACHMENT0 = gl.COLOR_ATTACHMENT0;
188
- this.INVALID_ENUM = gl.INVALID_ENUM;
189
- this.INVALID_OPERATION = gl.INVALID_OPERATION;
190
- }
191
- /**
192
- * Returns true if the WebGL context is WebGL2
193
- *
194
- * @returns
195
- */
196
- isWebGl2() {
197
- return isWebGl2(this.gl);
198
- }
199
-
200
- /**
201
- * ```
202
- * gl.activeTexture(textureUnit + gl.TEXTURE0);
203
- * ```
204
- *
205
- * @remarks
206
- * **WebGL Difference**: `textureUnit` is based from 0, not `gl.TEXTURE0`.
207
- *
208
- * @param textureUnit
209
- */
210
- activeTexture(textureUnit: number) {
211
- if (this.activeTextureUnit !== textureUnit) {
212
- this.gl.activeTexture(textureUnit + this.gl.TEXTURE0);
213
- this.activeTextureUnit = textureUnit;
214
- }
215
- }
216
-
217
- /**
218
- * ```
219
- * gl.bindTexture(gl.TEXTURE_2D, texture);
220
- * ```
221
- * @remarks
222
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
223
- *
224
- * @param texture
225
- */
226
- bindTexture(texture: WebGLTexture | null) {
227
- if (this.texture2dUnits[this.activeTextureUnit] === texture) {
228
- return;
229
- }
230
- this.texture2dUnits[this.activeTextureUnit] = texture;
231
-
232
- this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
233
- }
234
-
235
- private _getActiveTexture(): WebGLTexture | null {
236
- return this.texture2dUnits[this.activeTextureUnit]!;
237
- }
238
-
239
- /**
240
- * ```
241
- * gl.texParameteri(gl.TEXTURE_2D, pname, param);
242
- * ```
243
- * @remarks
244
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
245
- *
246
- * @param pname
247
- * @param param
248
- * @returns
249
- */
250
- texParameteri(pname: number, param: number) {
251
- const activeTexture = this._getActiveTexture();
252
- if (!activeTexture) {
253
- throw new Error('No active texture');
254
- }
255
- let textureParams = this.texture2dParams.get(activeTexture);
256
- if (!textureParams) {
257
- textureParams = {};
258
- this.texture2dParams.set(activeTexture, textureParams);
259
- }
260
- if (textureParams[pname] === param) {
261
- return;
262
- }
263
- textureParams[pname] = param;
264
- this.gl.texParameteri(this.gl.TEXTURE_2D, pname, param);
265
- }
266
-
267
- /**
268
- * ```
269
- * gl.texImage2D(
270
- * gl.TEXTURE_2D,
271
- * level,
272
- * internalFormat,
273
- * width,
274
- * height,
275
- * border,
276
- * format,
277
- * type,
278
- * pixels,
279
- * );
280
- * ```
281
- * @remarks
282
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
283
- *
284
- * @param level
285
- * @param internalFormat
286
- * @param width
287
- * @param height
288
- * @param border
289
- * @param format
290
- * @param type
291
- * @param pixels
292
- */
293
- texImage2D(
294
- level: GLint,
295
- internalformat: GLint,
296
- width: GLsizei,
297
- height: GLsizei,
298
- border: GLint,
299
- format: GLenum,
300
- type: GLenum,
301
- pixels: ArrayBufferView | null,
302
- ): void;
303
- texImage2D(
304
- level: GLint,
305
- internalformat: GLint,
306
- format: GLenum,
307
- type: GLenum,
308
- source: TexImageSource | Uint8Array,
309
- ): void;
310
- texImage2D(
311
- level: any,
312
- internalFormat: any,
313
- widthOrFormat: any,
314
- heightOrType: any,
315
- borderOrSource: any,
316
- format?: any,
317
- type?: any,
318
- pixels?: any,
319
- ) {
320
- if (format) {
321
- this.gl.texImage2D(
322
- this.gl.TEXTURE_2D,
323
- level,
324
- internalFormat,
325
- widthOrFormat,
326
- heightOrType,
327
- borderOrSource,
328
- format,
329
- type,
330
- pixels,
331
- );
332
- } else {
333
- this.gl.texImage2D(
334
- this.gl.TEXTURE_2D,
335
- level,
336
- internalFormat,
337
- widthOrFormat,
338
- heightOrType,
339
- borderOrSource,
340
- );
341
- }
342
- }
343
- /**
344
- * ```
345
- * gl.compressedTexImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, data);
346
- * ```
347
- *
348
- * @remarks
349
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
350
- */
351
-
352
- compressedTexImage2D(
353
- level: GLint,
354
- internalformat: GLenum,
355
- width: GLsizei,
356
- height: GLsizei,
357
- border: GLint,
358
- data?: ArrayBufferView,
359
- ): void {
360
- this.gl.compressedTexImage2D(
361
- this.gl.TEXTURE_2D,
362
- level,
363
- internalformat,
364
- width,
365
- height,
366
- border,
367
- data as ArrayBufferView,
368
- );
369
- }
370
- /**
371
- * ```
372
- * gl.pixelStorei(pname, param);
373
- * ```
374
- *
375
- * @param pname
376
- * @param param
377
- */
378
- pixelStorei(pname: GLenum, param: GLint | GLboolean) {
379
- this.gl.pixelStorei(pname, param);
380
- }
381
-
382
- /**
383
- * ```
384
- * gl.generateMipmap(gl.TEXTURE_2D);
385
- * ```
386
- *
387
- * @remarks
388
- * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
389
- */
390
- generateMipmap() {
391
- this.gl.generateMipmap(this.gl.TEXTURE_2D);
392
- }
393
-
394
- /**
395
- * ```
396
- * gl.createTexture();
397
- * ```
398
- *
399
- * @returns
400
- */
401
- createTexture() {
402
- return this.gl.createTexture();
403
- }
404
-
405
- /**
406
- * ```
407
- * gl.deleteTexture(texture);
408
- * ```
409
- *
410
- * @param texture
411
- */
412
- deleteTexture(texture: WebGLTexture | null) {
413
- if (texture) {
414
- this.texture2dParams.delete(texture);
415
- }
416
- this.gl.deleteTexture(texture);
417
- }
418
-
419
- /**
420
- * ```
421
- * gl.deleteFramebuffer(framebuffer);
422
- *
423
- * @param framebuffer
424
- */
425
- deleteFramebuffer(framebuffer: WebGLFramebuffer | null) {
426
- this.gl.deleteFramebuffer(framebuffer);
427
- }
428
-
429
- /**
430
- * ```
431
- * gl.viewport(x, y, width, height);
432
- * ```
433
- */
434
- viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
435
- this.gl.viewport(x, y, width, height);
436
- }
437
-
438
- /**
439
- * ```
440
- * gl.clearColor(red, green, blue, alpha);
441
- * ```
442
- *
443
- * @param red
444
- * @param green
445
- * @param blue
446
- * @param alpha
447
- */
448
- clearColor(red: GLclampf, green: GLclampf, blue: GLclampf, alpha: GLclampf) {
449
- this.gl.clearColor(red, green, blue, alpha);
450
- }
451
-
452
- /**
453
- * ```
454
- * gl["enable"|"disable"](gl.SCISSOR_TEST);
455
- * ```
456
- * @param enable
457
- */
458
- setScissorTest(enable: boolean) {
459
- if (enable === this.scissorEnabled) {
460
- return;
461
- }
462
- if (enable) {
463
- this.gl.enable(this.gl.SCISSOR_TEST);
464
- } else {
465
- this.gl.disable(this.gl.SCISSOR_TEST);
466
- }
467
- this.scissorEnabled = enable;
468
- }
469
-
470
- /**
471
- * ```
472
- * gl.scissor(x, y, width, height);
473
- * ```
474
- *
475
- * @param x
476
- * @param y
477
- * @param width
478
- * @param height
479
- */
480
- scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
481
- if (
482
- x !== this.scissorX ||
483
- y !== this.scissorY ||
484
- width !== this.scissorWidth ||
485
- height !== this.scissorHeight
486
- ) {
487
- this.gl.scissor(x, y, width, height);
488
- this.scissorX = x;
489
- this.scissorY = y;
490
- this.scissorWidth = width;
491
- this.scissorHeight = height;
492
- }
493
- }
494
-
495
- /**
496
- * ```
497
- * gl["enable"|"disable"](gl.BLEND);
498
- * ```
499
- *
500
- * @param blend
501
- * @returns
502
- */
503
- setBlend(blend: boolean) {
504
- if (blend === this.blendEnabled) {
505
- return;
506
- }
507
- if (blend) {
508
- this.gl.enable(this.gl.BLEND);
509
- } else {
510
- this.gl.disable(this.gl.BLEND);
511
- }
512
- this.blendEnabled = blend;
513
- }
514
-
515
- /**
516
- * ```
517
- * gl.blendFunc(src, dst);
518
- * ```
519
- *
520
- * @param src
521
- * @param dst
522
- */
523
- blendFunc(src: GLenum, dst: GLenum) {
524
- if (
525
- src !== this.blendSrcRgb ||
526
- dst !== this.blendDstRgb ||
527
- src !== this.blendSrcAlpha ||
528
- dst !== this.blendDstAlpha
529
- ) {
530
- this.gl.blendFunc(src, dst);
531
- this.blendSrcRgb = src;
532
- this.blendDstRgb = dst;
533
- this.blendSrcAlpha = src;
534
- this.blendDstAlpha = dst;
535
- }
536
- }
537
-
538
- /**
539
- * ```
540
- * gl.createBuffer();
541
- * ```
542
- *
543
- * @returns
544
- */
545
- createBuffer() {
546
- return this.gl.createBuffer();
547
- }
548
-
549
- /**
550
- * ```
551
- * gl.createFramebuffer();
552
- * ```
553
- * @returns
554
- */
555
- createFramebuffer() {
556
- return this.gl.createFramebuffer();
557
- }
558
-
559
- /**
560
- * ```
561
- * gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
562
- * ```
563
- *
564
- * @param framebuffer
565
- */
566
- bindFramebuffer(framebuffer: WebGLFramebuffer | null) {
567
- this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, framebuffer);
568
- }
569
-
570
- /**
571
- * ```
572
- * gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
573
- * ```
574
- * @remarks
575
- * **WebGL Difference**: Bind target is always `gl.FRAMEBUFFER` and textarget is always `gl.TEXTURE_2D`
576
- */
577
-
578
- framebufferTexture2D(
579
- attachment: GLenum,
580
- texture: WebGLTexture | null,
581
- level: GLint,
582
- ) {
583
- const gl = this.gl;
584
- gl.framebufferTexture2D(
585
- gl.FRAMEBUFFER,
586
- attachment,
587
- gl.TEXTURE_2D,
588
- texture,
589
- level,
590
- );
591
- }
592
-
593
- /**
594
- * ```
595
- * gl.clear(gl.COLOR_BUFFER_BIT);
596
- * ```
597
- *
598
- * @remarks
599
- * **WebGL Difference**: Clear mask is always `gl.COLOR_BUFFER_BIT`
600
- */
601
- clear() {
602
- this.gl.clear(this.gl.COLOR_BUFFER_BIT);
603
- }
604
-
605
- /**
606
- * ```
607
- * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
608
- * gl.bufferData(gl.ARRAY_BUFFER, data, usage);
609
- * ```
610
- *
611
- * @remarks
612
- * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
613
- *
614
- * @param buffer
615
- * @param data
616
- * @param usage
617
- */
618
- arrayBufferData(
619
- buffer: WebGLBuffer | null,
620
- data: ArrayBufferView,
621
- usage: GLenum,
622
- ) {
623
- if (this.boundArrayBuffer !== buffer) {
624
- this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);
625
- this.boundArrayBuffer = buffer;
626
- }
627
- this.gl.bufferData(this.gl.ARRAY_BUFFER, data, usage);
628
- }
629
-
630
- /**
631
- * ```
632
- * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
633
- * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
634
- * ```
635
- * @remarks
636
- * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
637
- *
638
- * @param buffer
639
- * @param data
640
- * @param usage
641
- */
642
- elementArrayBufferData(
643
- buffer: WebGLBuffer | null,
644
- data: ArrayBufferView,
645
- usage: GLenum,
646
- ) {
647
- if (this.boundElementArrayBuffer !== buffer) {
648
- this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, buffer);
649
- this.boundElementArrayBuffer = buffer;
650
- }
651
- this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, data, usage);
652
- }
653
-
654
- /**
655
- * ```
656
- * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
657
- * gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
658
- * ```
659
- *
660
- * @remarks
661
- * **WebGL Combo**: `gl.bindBuffer` and `gl.vertexAttribPointer` are combined into one function.
662
- *
663
- * @param buffer
664
- * @param index
665
- * @param size
666
- * @param type
667
- * @param normalized
668
- * @param stride
669
- * @param offset
670
- */
671
- vertexAttribPointer(
672
- buffer: WebGLBuffer,
673
- index: GLuint,
674
- size: GLint,
675
- type: GLenum,
676
- normalized: GLboolean,
677
- stride: GLsizei,
678
- offset: GLintptr,
679
- ) {
680
- if (this.boundArrayBuffer !== buffer) {
681
- this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);
682
- this.boundArrayBuffer = buffer;
683
- }
684
- this.gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
685
- }
686
-
687
- /**
688
- * Returns object with Attribute names as key and numbers as location values
689
- *
690
- * @param program
691
- * @returns object with numbers
692
- */
693
- getUniformLocations(
694
- program: WebGLProgram,
695
- ): Record<string, WebGLUniformLocation> {
696
- const gl = this.gl;
697
- const length = gl.getProgramParameter(
698
- program,
699
- gl.ACTIVE_UNIFORMS,
700
- ) as number;
701
- const result = {} as Record<string, WebGLUniformLocation>;
702
- for (let i = 0; i < length; i++) {
703
- const info = gl.getActiveUniform(program, i) as WebGLActiveInfo;
704
- //remove bracket + value from uniform name;
705
- let name = info.name.replace(/\[.*?\]/g, '');
706
- result[name] = gl.getUniformLocation(
707
- program,
708
- name,
709
- ) as WebGLUniformLocation;
710
- }
711
- return result;
712
- }
713
-
714
- /**
715
- * Returns object with Attribute names as key and numbers as location values
716
- * @param program
717
- * @returns object with numbers
718
- */
719
- getAttributeLocations(program: WebGLProgram): Record<string, number> {
720
- const gl = this.gl;
721
- const length = gl.getProgramParameter(
722
- program,
723
- gl.ACTIVE_ATTRIBUTES,
724
- ) as number;
725
- const result = {} as Record<string, number>;
726
- for (let i = 0; i < length; i++) {
727
- const { name } = gl.getActiveAttrib(program, i) as WebGLActiveInfo;
728
- result[name] = i;
729
- }
730
- return result;
731
- }
732
-
733
- /**
734
- * ```
735
- * gl.useProgram(program);
736
- * ```
737
- *
738
- * @param program
739
- * @returns
740
- */
741
- useProgram(
742
- program: WebGLProgram | null,
743
- uniformLocations: Record<string, WebGLUniformLocation>,
744
- ) {
745
- if (this.curProgram === program) {
746
- return;
747
- }
748
- this.gl.useProgram(program);
749
- this.curProgram = program;
750
- this.curUniformLocations = uniformLocations;
751
- }
752
-
753
- /**
754
- * Sets the value of a single float uniform variable.
755
- *
756
- * @param location - The location of the uniform variable.
757
- * @param v0 - The value to set.
758
- */
759
- uniform1f(location: string, v0: number) {
760
- this.gl.uniform1f(this.curUniformLocations[location] || null, v0);
761
- }
762
-
763
- /**
764
- * Sets the value of a float array uniform variable.
765
- *
766
- * @param location - The location of the uniform variable.
767
- * @param value - The array of values to set.
768
- */
769
- uniform1fv(location: string, value: Float32Array) {
770
- this.gl.uniform1fv(this.curUniformLocations[location] || null, value);
771
- }
772
-
773
- /**
774
- * Sets the value of a single integer uniform variable.
775
- *
776
- * @param location - The location of the uniform variable.
777
- * @param v0 - The value to set.
778
- */
779
- uniform1i(location: string, v0: number) {
780
- this.gl.uniform1i(this.curUniformLocations[location] || null, v0);
781
- }
782
-
783
- /**
784
- * Sets the value of an integer array uniform variable.
785
- *
786
- * @param location - The location of the uniform variable.
787
- * @param value - The array of values to set.
788
- */
789
- uniform1iv(location: string, value: Int32Array) {
790
- this.gl.uniform1iv(this.curUniformLocations[location] || null, value);
791
- }
792
-
793
- /**
794
- * Sets the value of a vec2 uniform variable.
795
- *
796
- * @param location - The location of the uniform variable.
797
- * @param v0 - The first component of the vector.
798
- * @param v1 - The second component of the vector.
799
- */
800
- uniform2f(location: string, v0: number, v1: number) {
801
- this.gl.uniform2f(this.curUniformLocations[location] || null, v0, v1);
802
- }
803
-
804
- /**
805
- * Sets the value of a vec3 uniform variable.
806
- *
807
- * @param location - The location of the uniform variable.
808
- * @param v - array of 4 numbers.
809
- */
810
- uniform2fa(location: string, value: Vec2) {
811
- this.gl.uniform2f(
812
- this.curUniformLocations[location] || null,
813
- value[0],
814
- value[1],
815
- );
816
- }
817
-
818
- /**
819
- * Sets the value of a vec2 array uniform variable.
820
- *
821
- * @param location - The location of the uniform variable.
822
- * @param value - The array of vec2 values to set.
823
- */
824
- uniform2fv(location: string, value: Float32Array) {
825
- this.gl.uniform2fv(this.curUniformLocations[location] || null, value);
826
- }
827
-
828
- /**
829
- * Sets the value of a ivec2 uniform variable.
830
- *
831
- * @param location - The location of the uniform variable.
832
- * @param v0 - The first component of the vector.
833
- * @param v1 - The second component of the vector.
834
- */
835
- uniform2i(location: string, v0: number, v1: number) {
836
- this.gl.uniform2i(this.curUniformLocations[location] || null, v0, v1);
837
- }
838
-
839
- /**
840
- * Sets the value of an ivec2 array uniform variable.
841
- *
842
- * @param location - The location of the uniform variable.
843
- * @param value - The array of ivec2 values to set.
844
- */
845
- uniform2iv(location: string, value: Int32Array) {
846
- this.gl.uniform2iv(this.curUniformLocations[location] || null, value);
847
- }
848
-
849
- /**
850
- * Sets the value of a vec3 uniform variable.
851
- *
852
- * @param location - The location of the uniform variable.
853
- * @param v0 - The first component of the vector.
854
- * @param v1 - The second component of the vector.
855
- * @param v2 - The third component of the vector.
856
- */
857
- uniform3f(location: string, v0: number, v1: number, v2: number) {
858
- this.gl.uniform3f(this.curUniformLocations[location] || null, v0, v1, v2);
859
- }
860
-
861
- /**
862
- * Sets the value of a vec3 uniform variable.
863
- *
864
- * @param location - The location of the uniform variable.
865
- * @param v - array of 4 numbers.
866
- */
867
- uniform3fa(location: string, value: Vec3) {
868
- this.gl.uniform3f(
869
- this.curUniformLocations[location] || null,
870
- value[0],
871
- value[1],
872
- value[2],
873
- );
874
- }
875
-
876
- /**
877
- * Sets the value of a vec3 array uniform variable.
878
- *
879
- * @param location - The location of the uniform variable.
880
- * @param value - The array of vec3 values to set.
881
- */
882
- uniform3fv(location: string, value: Float32Array) {
883
- this.gl.uniform3fv(this.curUniformLocations[location] || null, value);
884
- }
885
-
886
- /**
887
- * Sets the value of a ivec3 uniform variable.
888
- *
889
- * @param location - The location of the uniform variable.
890
- * @param v0 - The first component of the vector.
891
- * @param v1 - The second component of the vector.
892
- * @param v2 - The third component of the vector.
893
- */
894
- uniform3i(location: string, v0: number, v1: number, v2: number) {
895
- this.gl.uniform3i(this.curUniformLocations[location] || null, v0, v1, v2);
896
- }
897
-
898
- /**
899
- * Sets the value of an ivec3 array uniform variable.
900
- *
901
- * @param location - The location of the uniform variable.
902
- * @param value - The array of ivec3 values to set.
903
- */
904
- uniform3iv(location: string, value: Int32Array) {
905
- this.gl.uniform3iv(this.curUniformLocations[location] || null, value);
906
- }
907
-
908
- /**
909
- * Sets the value of a vec4 uniform variable.
910
- *
911
- * @param location - The location of the uniform variable.
912
- * @param v0 - The first component of the vector.
913
- * @param v1 - The second component of the vector.
914
- * @param v2 - The third component of the vector.
915
- * @param v3 - The fourth component of the vector.
916
- */
917
- uniform4f(location: string, v0: number, v1: number, v2: number, v3: number) {
918
- this.gl.uniform4f(
919
- this.curUniformLocations[location] || null,
920
- v0,
921
- v1,
922
- v2,
923
- v3,
924
- );
925
- }
926
-
927
- /**
928
- * Sets the value of a vec4 uniform variable.
929
- *
930
- * @param location - The location of the uniform variable.
931
- * @param v - array of 4 numbers.
932
- */
933
- uniform4fa(location: string, value: Vec4) {
934
- this.gl.uniform4f(
935
- this.curUniformLocations[location] || null,
936
- value[0],
937
- value[1],
938
- value[2],
939
- value[3],
940
- );
941
- }
942
-
943
- /**
944
- * Sets the value of a vec4 array uniform variable.
945
- *
946
- * @param location - The location of the uniform variable.
947
- * @param value - The array of vec4 values to set.
948
- */
949
- uniform4fv(location: string, value: Float32Array) {
950
- this.gl.uniform4fv(this.curUniformLocations[location] || null, value);
951
- }
952
-
953
- /**
954
- * Sets the value of a ivec4 uniform variable.
955
- *
956
- * @param location - The location of the uniform variable.
957
- * @param v0 - The first component of the vector.
958
- * @param v1 - The second component of the vector.
959
- * @param v2 - The third component of the vector.
960
- * @param v3 - The fourth component of the vector.
961
- */
962
- uniform4i(location: string, v0: number, v1: number, v2: number, v3: number) {
963
- this.gl.uniform4i(
964
- this.curUniformLocations[location] || null,
965
- v0,
966
- v1,
967
- v2,
968
- v3,
969
- );
970
- }
971
-
972
- /**
973
- * Sets the value of an ivec4 array uniform variable.
974
- *
975
- * @param location - The location of the uniform variable.
976
- * @param value - The array of ivec4 values to set.
977
- */
978
- uniform4iv(location: string, value: Int32Array) {
979
- this.gl.uniform4iv(this.curUniformLocations[location] || null, value);
980
- }
981
-
982
- /**
983
- * Sets the value of a mat2 uniform variable.
984
- *
985
- * @param location - The location of the uniform variable.
986
- * @param transpose - Whether to transpose the matrix.
987
- * @param value - The array of mat2 values to set.
988
- */
989
- uniformMatrix2fv(location: string, value: Float32Array) {
990
- this.gl.uniformMatrix2fv(
991
- this.curUniformLocations[location] || null,
992
- false,
993
- value,
994
- );
995
- }
996
-
997
- /**
998
- * Sets the value of a mat2 uniform variable.
999
- * @param location - The location of the uniform variable.
1000
- * @param value - The array of mat2 values to set.
1001
- */
1002
- uniformMatrix3fv(location: string, value: Float32Array) {
1003
- this.gl.uniformMatrix3fv(
1004
- this.curUniformLocations[location] || null,
1005
- false,
1006
- value,
1007
- );
1008
- }
1009
-
1010
- /**
1011
- * Sets the value of a mat4 uniform variable.
1012
- * @param location - The location of the uniform variable.
1013
- * @param value - The array of mat4 values to set.
1014
- */
1015
- uniformMatrix4fv(location: string, value: Float32Array) {
1016
- this.gl.uniformMatrix4fv(
1017
- this.curUniformLocations[location] || null,
1018
- false,
1019
- value,
1020
- );
1021
- }
1022
-
1023
- /**
1024
- * ```
1025
- * gl.getParameter(pname);
1026
- * ```
1027
- *
1028
- * @param pname
1029
- * @returns
1030
- */
1031
- getParameter(pname: GLenum): any {
1032
- return this.gl.getParameter(pname);
1033
- }
1034
-
1035
- /**
1036
- * ```
1037
- * gl.drawElements(mode, count, type, offset);
1038
- * ```
1039
- *
1040
- * @param mode
1041
- * @param count
1042
- * @param type
1043
- * @param offset
1044
- */
1045
- drawElements(mode: GLenum, count: GLsizei, type: GLenum, offset: GLintptr) {
1046
- this.gl.drawElements(mode, count, type, offset);
1047
- }
1048
-
1049
- /**
1050
- * ```
1051
- * gl.drawArrays(mode, first, count);
1052
- * ```
1053
- *
1054
- * @param name
1055
- * @returns
1056
- */
1057
- getExtension(name: string) {
1058
- return this.gl.getExtension(name);
1059
- }
1060
-
1061
- /**
1062
- * ```
1063
- * gl.getError(type);
1064
- * ```
1065
- *
1066
- * @returns
1067
- */
1068
- getError() {
1069
- return this.gl.getError();
1070
- }
1071
-
1072
- /**
1073
- * ```
1074
- * gl.createVertexArray();
1075
- * ```
1076
- *
1077
- * @returns
1078
- */
1079
- createVertexArray() {
1080
- if (this.gl instanceof WebGL2RenderingContext) {
1081
- return this.gl.createVertexArray();
1082
- }
1083
- return undefined;
1084
- }
1085
-
1086
- /**
1087
- * ```
1088
- * gl.bindVertexArray(vertexArray);
1089
- * ```
1090
- *
1091
- * @param vertexArray
1092
- */
1093
- bindVertexArray(vertexArray: WebGLVertexArrayObject | null) {
1094
- if (this.gl instanceof WebGL2RenderingContext) {
1095
- this.gl.bindVertexArray(vertexArray);
1096
- }
1097
- }
1098
-
1099
- /**
1100
- * ```
1101
- * gl.getAttribLocation(program, name);
1102
- * ```
1103
- *
1104
- * @param program
1105
- * @param name
1106
- * @returns
1107
- */
1108
- getAttribLocation(program: WebGLProgram, name: string) {
1109
- return this.gl.getAttribLocation(program, name);
1110
- }
1111
-
1112
- /**
1113
- * ```
1114
- * gl.getUniformLocation(program, name);
1115
- * ```
1116
- *
1117
- * @param program
1118
- * @param name
1119
- * @returns
1120
- */
1121
- getUniformLocation(program: WebGLProgram, name: string) {
1122
- return this.gl.getUniformLocation(program, name);
1123
- }
1124
-
1125
- /**
1126
- * ```
1127
- * gl.enableVertexAttribArray(index);
1128
- * ```
1129
- *
1130
- * @param index
1131
- */
1132
- enableVertexAttribArray(index: number) {
1133
- this.gl.enableVertexAttribArray(index);
1134
- }
1135
-
1136
- /**
1137
- * ```
1138
- * gl.disableVertexAttribArray(index);
1139
- * ```
1140
- *
1141
- * @param index
1142
- */
1143
- disableVertexAttribArray(index: number) {
1144
- this.gl.disableVertexAttribArray(index);
1145
- }
1146
-
1147
- /**
1148
- * ```
1149
- * gl.createShader(type);
1150
- * ```
1151
- *
1152
- * @param type
1153
- * @returns
1154
- */
1155
- createShader(type: number) {
1156
- return this.gl.createShader(type);
1157
- }
1158
-
1159
- /**
1160
- * ```
1161
- * gl.compileShader(shader);
1162
- * ```
1163
- *
1164
- * @param shader
1165
- * @returns
1166
- */
1167
- compileShader(shader: WebGLShader) {
1168
- this.gl.compileShader(shader);
1169
- }
1170
-
1171
- /**
1172
- * ```
1173
- * gl.attachShader(program, shader);
1174
- * ```
1175
- *
1176
- * @param program
1177
- * @param shader
1178
- */
1179
- attachShader(program: WebGLProgram, shader: WebGLShader) {
1180
- this.gl.attachShader(program, shader);
1181
- }
1182
-
1183
- /**
1184
- * ```
1185
- * gl.linkProgram(program);
1186
- * ```
1187
- *
1188
- * @param program
1189
- */
1190
- linkProgram(program: WebGLProgram) {
1191
- this.gl.linkProgram(program);
1192
- }
1193
-
1194
- /**
1195
- * ```
1196
- * gl.deleteProgram(shader);
1197
- * ```
1198
- *
1199
- * @param shader
1200
- */
1201
- deleteProgram(shader: WebGLProgram) {
1202
- this.gl.deleteProgram(shader);
1203
- }
1204
-
1205
- /**
1206
- * ```
1207
- * gl.getShaderParameter(shader, pname);
1208
- * ```
1209
- *
1210
- * @param shader
1211
- * @param pname
1212
- */
1213
- getShaderParameter(shader: WebGLShader, pname: GLenum) {
1214
- return this.gl.getShaderParameter(shader, pname);
1215
- }
1216
-
1217
- /**
1218
- * ```
1219
- * gl.getShaderInfoLog(shader);
1220
- * ```
1221
- *
1222
- * @param shader
1223
- */
1224
- getShaderInfoLog(shader: WebGLShader) {
1225
- return this.gl.getShaderInfoLog(shader);
1226
- }
1227
-
1228
- /**
1229
- * ```
1230
- * gl.createProgram();
1231
- * ```
1232
- *
1233
- * @returns
1234
- */
1235
- createProgram() {
1236
- return this.gl.createProgram();
1237
- }
1238
-
1239
- /**
1240
- * ```
1241
- * gl.getProgramParameter(program, pname);
1242
- * ```
1243
- *
1244
- * @param program
1245
- * @param pname
1246
- * @returns
1247
- */
1248
- getProgramParameter(program: WebGLProgram, pname: GLenum) {
1249
- return this.gl.getProgramParameter(program, pname);
1250
- }
1251
-
1252
- /**
1253
- * ```
1254
- * gl.getProgramInfoLog(program);
1255
- * ```
1256
- *
1257
- * @param program
1258
- * @returns
1259
- */
1260
- getProgramInfoLog(program: WebGLProgram) {
1261
- return this.gl.getProgramInfoLog(program);
1262
- }
1263
-
1264
- /**
1265
- * ```
1266
- * gl.shaderSource(shader, source);
1267
- * ```
1268
- *
1269
- * @param shader
1270
- * @param source
1271
- */
1272
- shaderSource(shader: WebGLShader, source: string) {
1273
- this.gl.shaderSource(shader, source);
1274
- }
1275
-
1276
- /**
1277
- * ```
1278
- * gl.deleteShader(shader);
1279
- * ```
1280
- *
1281
- * @param shader
1282
- */
1283
- deleteShader(shader: WebGLShader) {
1284
- this.gl.deleteShader(shader);
1285
- }
1286
-
1287
- /**
1288
- * ```
1289
- * gl.deleteBuffer(buffer);
1290
- * ```
1291
- *
1292
- * @param buffer - The buffer to delete
1293
- */
1294
- deleteBuffer(buffer: WebGLBuffer) {
1295
- const { gl } = this;
1296
- gl.deleteBuffer(buffer);
1297
-
1298
- // Reset bound buffers if they match the deleted buffer
1299
- if (this.boundArrayBuffer === buffer) {
1300
- this.boundArrayBuffer = null;
1301
- }
1302
- }
1303
-
1304
- /**
1305
- * ```
1306
- * gl.deleteVertexArray(vertexArray);
1307
- * ```
1308
- *
1309
- * @param vertexArray - The vertex array object to delete
1310
- */
1311
- deleteVertexArray(vertexArray: WebGLVertexArrayObject) {
1312
- if (this.isWebGl2()) {
1313
- (this.gl as WebGL2RenderingContext).deleteVertexArray(vertexArray);
1314
- }
1315
- }
1316
- }
1317
-
1318
- // prettier-ignore
1319
- type IsUniformMethod<MethodName, MethodType> = MethodName extends `uniform${string}`
1320
- ?
1321
- MethodType extends (location: WebGLUniformLocation | null, ...args: any[]) => void
1322
- ? true
1323
- : false
1324
- : false;
1325
-
1326
- // prettier-ignore
1327
- export type UniformMethodMap = {
1328
- [Key in keyof WebGLRenderingContext as IsUniformMethod<Key, WebGLRenderingContext[Key]> extends true ? Key : never]: WebGLRenderingContext[Key] extends (
1329
- location: WebGLUniformLocation | null,
1330
- ...args: infer T
1331
- ) => void
1332
- ? T
1333
- : never;
1334
- };
1335
-
1336
- /**
1337
- * Compare two arrays for equality.
1338
- *
1339
- * @remarks
1340
- * This function will not try to compare nested arrays or Float32Arrays and
1341
- * instead will always return false when they are encountered.
1342
- *
1343
- * @param a
1344
- * @param b
1345
- * @returns
1346
- */
1347
- export function compareArrays<T>(a: T[], b: T[]): boolean {
1348
- if (a.length !== b.length) {
1349
- return false;
1350
- }
1351
-
1352
- let result = false;
1353
- for (let i = 0; i < a.length; i++) {
1354
- if (Array.isArray(a[i]) || a[i] instanceof Float32Array) {
1355
- result = false;
1356
- break;
1357
- }
1358
-
1359
- if (a[i] !== b[i]) {
1360
- result = false;
1361
- break;
1362
- }
1363
-
1364
- result = true;
1365
- }
1366
-
1367
- return result;
1368
- }
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 2026 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
+ /* eslint-disable @typescript-eslint/no-unsafe-return */
21
+ /* eslint-disable @typescript-eslint/no-explicit-any */
22
+ /* eslint-disable @typescript-eslint/no-unsafe-argument */
23
+
24
+ import type {
25
+ Vec2,
26
+ Vec3,
27
+ Vec4,
28
+ } from '../../renderers/webgl/internal/ShaderUtils.js';
29
+ import { isWebGl2 } from '../../renderers/webgl/internal/WebGlUtils.js';
30
+ import { GlContextWrapper } from '../GlContextWrapper.js';
31
+ import type { CompressedData } from '../../textures/Texture.js';
32
+
33
+ /**
34
+ * Optimized WebGL Context Wrapper
35
+ *
36
+ * @remarks
37
+ * This class contains the subset of the WebGLRenderingContext & WebGL2RenderingContext
38
+ * API that is used by the renderer. Select high volume WebGL methods include
39
+ * caching optimizations to avoid making WebGL calls if the state is already set
40
+ * to the desired value.
41
+ *
42
+ * While most methods contained are direct passthroughs to the WebGL context,
43
+ * some methods combine multiple WebGL calls into one for convenience, modify
44
+ * arguments to be more convenient, or are replaced by more specific methods.
45
+ *
46
+ * Not all methods are optimized. Only methods that are called frequently
47
+ * and/or have a high cost are optimized.
48
+ *
49
+ * A subset of GLenum constants are also exposed as properties on this class
50
+ * for convenience.
51
+ */
52
+ export class WebGlContextWrapper extends GlContextWrapper {
53
+ //#region Cached WebGL State
54
+ private activeTextureUnit = 0;
55
+ private texture2dUnits: Array<WebGLTexture | null>;
56
+ private texture2dParams: WeakMap<
57
+ WebGLTexture,
58
+ Record<number, number | undefined>
59
+ > = new WeakMap();
60
+ private scissorEnabled;
61
+ private scissorX: number;
62
+ private scissorY: number;
63
+ private scissorWidth: number;
64
+ private scissorHeight: number;
65
+ private blendEnabled;
66
+ private blendSrcRgb: number;
67
+ private blendDstRgb: number;
68
+ private blendSrcAlpha: number;
69
+ private blendDstAlpha: number;
70
+ private boundArrayBuffer: WebGLBuffer | null;
71
+ private boundElementArrayBuffer: WebGLBuffer | null;
72
+ private curProgram: WebGLProgram | null;
73
+ private curUniformLocations: Record<string, WebGLUniformLocation> = {};
74
+ //#endregion Cached WebGL State
75
+
76
+ //#region Canvas
77
+ public readonly canvas;
78
+ //#endregion Canvas
79
+
80
+ //#region WebGL Enums
81
+ public readonly MAX_RENDERBUFFER_SIZE;
82
+ public readonly MAX_TEXTURE_SIZE;
83
+ public readonly MAX_VIEWPORT_DIMS;
84
+ public readonly MAX_VERTEX_TEXTURE_IMAGE_UNITS;
85
+ public readonly MAX_TEXTURE_IMAGE_UNITS;
86
+ public readonly MAX_COMBINED_TEXTURE_IMAGE_UNITS;
87
+ public readonly MAX_VERTEX_ATTRIBS;
88
+ public readonly MAX_VARYING_VECTORS;
89
+ public readonly MAX_VERTEX_UNIFORM_VECTORS;
90
+ public readonly MAX_FRAGMENT_UNIFORM_VECTORS;
91
+ public readonly TEXTURE_MAG_FILTER;
92
+ public readonly TEXTURE_MIN_FILTER;
93
+ public readonly TEXTURE_WRAP_S;
94
+ public readonly TEXTURE_WRAP_T;
95
+ public readonly LINEAR;
96
+ public readonly LINEAR_MIPMAP_LINEAR;
97
+ public readonly CLAMP_TO_EDGE;
98
+ public readonly RGB;
99
+ public readonly RGBA;
100
+ public readonly UNSIGNED_BYTE;
101
+ public readonly UNPACK_PREMULTIPLY_ALPHA_WEBGL;
102
+ public readonly UNPACK_FLIP_Y_WEBGL;
103
+ public readonly FLOAT;
104
+ public readonly TRIANGLES;
105
+ public readonly UNSIGNED_SHORT;
106
+ public readonly ONE;
107
+ public readonly ONE_MINUS_SRC_ALPHA;
108
+ public readonly VERTEX_SHADER;
109
+ public readonly FRAGMENT_SHADER;
110
+ public readonly STATIC_DRAW;
111
+ public readonly COMPILE_STATUS;
112
+ public readonly LINK_STATUS;
113
+ public readonly DYNAMIC_DRAW;
114
+ public readonly COLOR_ATTACHMENT0;
115
+ public readonly INVALID_ENUM: number;
116
+ public readonly INVALID_OPERATION: number;
117
+ //#endregion WebGL Enums
118
+
119
+ constructor(private gl: WebGLRenderingContext | WebGL2RenderingContext) {
120
+ super();
121
+ // The following code extracts the current state of the WebGL context
122
+ // to our local JavaScript cached version of it. This is so we can
123
+ // avoid making WebGL calls if we don't need to.
124
+ // We could assume that the WebGL context is in a default state, but
125
+ // in the future we may want to support restoring a broken WebGL context
126
+ // and this will help with that.
127
+ this.activeTextureUnit =
128
+ (gl.getParameter(gl.ACTIVE_TEXTURE) as number) - gl.TEXTURE0;
129
+ const maxTextureUnits = gl.getParameter(
130
+ gl.MAX_TEXTURE_IMAGE_UNITS,
131
+ ) as number;
132
+ // save current texture units
133
+ this.texture2dUnits = new Array<undefined>(maxTextureUnits)
134
+ .fill(undefined)
135
+ .map((_, i) => {
136
+ this.activeTexture(i);
137
+ return gl.getParameter(gl.TEXTURE_BINDING_2D) as WebGLTexture;
138
+ });
139
+ // restore active texture unit
140
+ this.activeTexture(this.activeTextureUnit);
141
+ this.scissorEnabled = gl.isEnabled(gl.SCISSOR_TEST);
142
+
143
+ const scissorBox = gl.getParameter(gl.SCISSOR_BOX) as [
144
+ number,
145
+ number,
146
+ number,
147
+ number,
148
+ ];
149
+ this.scissorX = scissorBox[0];
150
+ this.scissorY = scissorBox[1];
151
+ this.scissorWidth = scissorBox[2];
152
+ this.scissorHeight = scissorBox[3];
153
+
154
+ this.blendEnabled = gl.isEnabled(gl.BLEND);
155
+ this.blendSrcRgb = gl.getParameter(gl.BLEND_SRC_RGB) as number;
156
+ this.blendDstRgb = gl.getParameter(gl.BLEND_DST_RGB) as number;
157
+ this.blendSrcAlpha = gl.getParameter(gl.BLEND_SRC_ALPHA) as number;
158
+ this.blendDstAlpha = gl.getParameter(gl.BLEND_DST_ALPHA) as number;
159
+
160
+ this.boundArrayBuffer = gl.getParameter(
161
+ gl.ARRAY_BUFFER_BINDING,
162
+ ) as WebGLBuffer;
163
+ this.boundElementArrayBuffer = gl.getParameter(
164
+ gl.ELEMENT_ARRAY_BUFFER_BINDING,
165
+ ) as WebGLBuffer;
166
+
167
+ this.curProgram = gl.getParameter(
168
+ gl.CURRENT_PROGRAM,
169
+ ) as WebGLProgram | null;
170
+
171
+ this.canvas = gl.canvas;
172
+
173
+ // Extract GLenums
174
+ this.MAX_RENDERBUFFER_SIZE = gl.MAX_RENDERBUFFER_SIZE;
175
+ this.MAX_TEXTURE_SIZE = gl.MAX_TEXTURE_SIZE;
176
+ this.MAX_VIEWPORT_DIMS = gl.MAX_VIEWPORT_DIMS;
177
+ this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
178
+ this.MAX_TEXTURE_IMAGE_UNITS = gl.MAX_TEXTURE_IMAGE_UNITS;
179
+ this.MAX_COMBINED_TEXTURE_IMAGE_UNITS = gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS;
180
+ this.MAX_VERTEX_ATTRIBS = gl.MAX_VERTEX_ATTRIBS;
181
+ this.MAX_VARYING_VECTORS = gl.MAX_VARYING_VECTORS;
182
+ this.MAX_VERTEX_UNIFORM_VECTORS = gl.MAX_VERTEX_UNIFORM_VECTORS;
183
+ this.MAX_FRAGMENT_UNIFORM_VECTORS = gl.MAX_FRAGMENT_UNIFORM_VECTORS;
184
+ this.TEXTURE_MAG_FILTER = gl.TEXTURE_MAG_FILTER;
185
+ this.TEXTURE_MIN_FILTER = gl.TEXTURE_MIN_FILTER;
186
+ this.TEXTURE_WRAP_S = gl.TEXTURE_WRAP_S;
187
+ this.TEXTURE_WRAP_T = gl.TEXTURE_WRAP_T;
188
+ this.LINEAR = gl.LINEAR;
189
+ this.LINEAR_MIPMAP_LINEAR = gl.LINEAR_MIPMAP_LINEAR;
190
+ this.CLAMP_TO_EDGE = gl.CLAMP_TO_EDGE;
191
+ this.RGB = gl.RGB;
192
+ this.RGBA = gl.RGBA;
193
+ this.UNSIGNED_BYTE = gl.UNSIGNED_BYTE;
194
+ this.UNPACK_PREMULTIPLY_ALPHA_WEBGL = gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL;
195
+ this.UNPACK_FLIP_Y_WEBGL = gl.UNPACK_FLIP_Y_WEBGL;
196
+ this.FLOAT = gl.FLOAT;
197
+ this.TRIANGLES = gl.TRIANGLES;
198
+ this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
199
+ this.ONE = gl.ONE;
200
+ this.ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA;
201
+ this.MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS;
202
+ this.TRIANGLES = gl.TRIANGLES;
203
+ this.UNSIGNED_SHORT = gl.UNSIGNED_SHORT;
204
+ this.VERTEX_SHADER = gl.VERTEX_SHADER;
205
+ this.FRAGMENT_SHADER = gl.FRAGMENT_SHADER;
206
+ this.STATIC_DRAW = gl.STATIC_DRAW;
207
+ this.COMPILE_STATUS = gl.COMPILE_STATUS;
208
+ this.LINK_STATUS = gl.LINK_STATUS;
209
+ this.DYNAMIC_DRAW = gl.DYNAMIC_DRAW;
210
+ this.COLOR_ATTACHMENT0 = gl.COLOR_ATTACHMENT0;
211
+ this.INVALID_ENUM = gl.INVALID_ENUM;
212
+ this.INVALID_OPERATION = gl.INVALID_OPERATION;
213
+ }
214
+ /**
215
+ * Returns true if the WebGL context is WebGL2
216
+ *
217
+ * @returns
218
+ */
219
+ isWebGl2() {
220
+ return isWebGl2(this.gl);
221
+ }
222
+
223
+ /**
224
+ * ```
225
+ * gl.activeTexture(textureUnit + gl.TEXTURE0);
226
+ * ```
227
+ *
228
+ * @remarks
229
+ * **WebGL Difference**: `textureUnit` is based from 0, not `gl.TEXTURE0`.
230
+ *
231
+ * @param textureUnit
232
+ */
233
+ activeTexture(textureUnit: number) {
234
+ if (this.activeTextureUnit !== textureUnit) {
235
+ this.gl.activeTexture(textureUnit + this.gl.TEXTURE0);
236
+ this.activeTextureUnit = textureUnit;
237
+ }
238
+ }
239
+
240
+ /**
241
+ * ```
242
+ * gl.bindTexture(gl.TEXTURE_2D, texture);
243
+ * ```
244
+ * @remarks
245
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
246
+ *
247
+ * @param texture
248
+ */
249
+ bindTexture(texture: WebGLTexture | null) {
250
+ if (this.texture2dUnits[this.activeTextureUnit] === texture) {
251
+ return;
252
+ }
253
+ this.texture2dUnits[this.activeTextureUnit] = texture;
254
+
255
+ this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
256
+ }
257
+
258
+ private _getActiveTexture(): WebGLTexture | null {
259
+ return this.texture2dUnits[this.activeTextureUnit]!;
260
+ }
261
+
262
+ /**
263
+ * ```
264
+ * gl.texParameteri(gl.TEXTURE_2D, pname, param);
265
+ * ```
266
+ * @remarks
267
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
268
+ *
269
+ * @param pname
270
+ * @param param
271
+ * @returns
272
+ */
273
+ texParameteri(pname: number, param: number) {
274
+ const activeTexture = this._getActiveTexture();
275
+ if (!activeTexture) {
276
+ throw new Error('No active texture');
277
+ }
278
+ let textureParams = this.texture2dParams.get(activeTexture);
279
+ if (!textureParams) {
280
+ textureParams = {};
281
+ this.texture2dParams.set(activeTexture, textureParams);
282
+ }
283
+ if (textureParams[pname] === param) {
284
+ return;
285
+ }
286
+ textureParams[pname] = param;
287
+ this.gl.texParameteri(this.gl.TEXTURE_2D, pname, param);
288
+ }
289
+
290
+ /**
291
+ * ```
292
+ * gl.texImage2D(
293
+ * gl.TEXTURE_2D,
294
+ * level,
295
+ * internalFormat,
296
+ * width,
297
+ * height,
298
+ * border,
299
+ * format,
300
+ * type,
301
+ * pixels,
302
+ * );
303
+ * ```
304
+ * @remarks
305
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
306
+ *
307
+ * @param level
308
+ * @param internalFormat
309
+ * @param width
310
+ * @param height
311
+ * @param border
312
+ * @param format
313
+ * @param type
314
+ * @param pixels
315
+ */
316
+ texImage2D(
317
+ level: GLint,
318
+ internalformat: GLint,
319
+ width: GLsizei,
320
+ height: GLsizei,
321
+ border: GLint,
322
+ format: GLenum,
323
+ type: GLenum,
324
+ pixels: ArrayBufferView | null,
325
+ ): void;
326
+ texImage2D(
327
+ level: GLint,
328
+ internalformat: GLint,
329
+ format: GLenum,
330
+ type: GLenum,
331
+ source: TexImageSource | Uint8Array,
332
+ ): void;
333
+ texImage2D(
334
+ level: any,
335
+ internalFormat: any,
336
+ widthOrFormat: any,
337
+ heightOrType: any,
338
+ borderOrSource: any,
339
+ format?: any,
340
+ type?: any,
341
+ pixels?: any,
342
+ ) {
343
+ if (format) {
344
+ this.gl.texImage2D(
345
+ this.gl.TEXTURE_2D,
346
+ level,
347
+ internalFormat,
348
+ widthOrFormat,
349
+ heightOrType,
350
+ borderOrSource,
351
+ format,
352
+ type,
353
+ pixels,
354
+ );
355
+ } else {
356
+ this.gl.texImage2D(
357
+ this.gl.TEXTURE_2D,
358
+ level,
359
+ internalFormat,
360
+ widthOrFormat,
361
+ heightOrType,
362
+ borderOrSource,
363
+ );
364
+ }
365
+ }
366
+ /**
367
+ * ```
368
+ * gl.compressedTexImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, data);
369
+ * ```
370
+ *
371
+ * @remarks
372
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
373
+ */
374
+
375
+ compressedTexImage2D(
376
+ level: GLint,
377
+ internalformat: GLenum,
378
+ width: GLsizei,
379
+ height: GLsizei,
380
+ border: GLint,
381
+ data?: ArrayBufferView,
382
+ ): void {
383
+ this.gl.compressedTexImage2D(
384
+ this.gl.TEXTURE_2D,
385
+ level,
386
+ internalformat,
387
+ width,
388
+ height,
389
+ border,
390
+ data as ArrayBufferView,
391
+ );
392
+ }
393
+ /**
394
+ * ```
395
+ * gl.pixelStorei(pname, param);
396
+ * ```
397
+ *
398
+ * @param pname
399
+ * @param param
400
+ */
401
+ pixelStorei(pname: GLenum, param: GLint | GLboolean) {
402
+ this.gl.pixelStorei(pname, param);
403
+ }
404
+
405
+ /**
406
+ * ```
407
+ * gl.generateMipmap(gl.TEXTURE_2D);
408
+ * ```
409
+ *
410
+ * @remarks
411
+ * **WebGL Difference**: Bind target is always `gl.TEXTURE_2D`
412
+ */
413
+ generateMipmap() {
414
+ this.gl.generateMipmap(this.gl.TEXTURE_2D);
415
+ }
416
+
417
+ /**
418
+ * ```
419
+ * gl.createTexture();
420
+ * ```
421
+ *
422
+ * @returns
423
+ */
424
+ createTexture() {
425
+ return this.gl.createTexture();
426
+ }
427
+
428
+ /**
429
+ * ```
430
+ * gl.deleteTexture(texture);
431
+ * ```
432
+ *
433
+ * @param texture
434
+ */
435
+ deleteTexture(texture: WebGLTexture | null) {
436
+ if (texture) {
437
+ this.texture2dParams.delete(texture);
438
+ }
439
+ this.gl.deleteTexture(texture);
440
+ }
441
+
442
+ /**
443
+ * ```
444
+ * gl.deleteFramebuffer(framebuffer);
445
+ *
446
+ * @param framebuffer
447
+ */
448
+ deleteFramebuffer(framebuffer: WebGLFramebuffer | null) {
449
+ this.gl.deleteFramebuffer(framebuffer);
450
+ }
451
+
452
+ /**
453
+ * ```
454
+ * gl.viewport(x, y, width, height);
455
+ * ```
456
+ */
457
+ viewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
458
+ this.gl.viewport(x, y, width, height);
459
+ }
460
+
461
+ /**
462
+ * ```
463
+ * gl.clearColor(red, green, blue, alpha);
464
+ * ```
465
+ *
466
+ * @param red
467
+ * @param green
468
+ * @param blue
469
+ * @param alpha
470
+ */
471
+ clearColor(red: GLclampf, green: GLclampf, blue: GLclampf, alpha: GLclampf) {
472
+ this.gl.clearColor(red, green, blue, alpha);
473
+ }
474
+
475
+ /**
476
+ * ```
477
+ * gl["enable"|"disable"](gl.SCISSOR_TEST);
478
+ * ```
479
+ * @param enable
480
+ */
481
+ setScissorTest(enable: boolean) {
482
+ if (enable === this.scissorEnabled) {
483
+ return;
484
+ }
485
+ if (enable) {
486
+ this.gl.enable(this.gl.SCISSOR_TEST);
487
+ } else {
488
+ this.gl.disable(this.gl.SCISSOR_TEST);
489
+ }
490
+ this.scissorEnabled = enable;
491
+ }
492
+
493
+ /**
494
+ * ```
495
+ * gl.scissor(x, y, width, height);
496
+ * ```
497
+ *
498
+ * @param x
499
+ * @param y
500
+ * @param width
501
+ * @param height
502
+ */
503
+ scissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
504
+ if (
505
+ x !== this.scissorX ||
506
+ y !== this.scissorY ||
507
+ width !== this.scissorWidth ||
508
+ height !== this.scissorHeight
509
+ ) {
510
+ this.gl.scissor(x, y, width, height);
511
+ this.scissorX = x;
512
+ this.scissorY = y;
513
+ this.scissorWidth = width;
514
+ this.scissorHeight = height;
515
+ }
516
+ }
517
+
518
+ /**
519
+ * ```
520
+ * gl["enable"|"disable"](gl.BLEND);
521
+ * ```
522
+ *
523
+ * @param blend
524
+ * @returns
525
+ */
526
+ setBlend(blend: boolean) {
527
+ if (blend === this.blendEnabled) {
528
+ return;
529
+ }
530
+ if (blend) {
531
+ this.gl.enable(this.gl.BLEND);
532
+ } else {
533
+ this.gl.disable(this.gl.BLEND);
534
+ }
535
+ this.blendEnabled = blend;
536
+ }
537
+
538
+ /**
539
+ * ```
540
+ * gl.blendFunc(src, dst);
541
+ * ```
542
+ *
543
+ * @param src
544
+ * @param dst
545
+ */
546
+ blendFunc(src: GLenum, dst: GLenum) {
547
+ if (
548
+ src !== this.blendSrcRgb ||
549
+ dst !== this.blendDstRgb ||
550
+ src !== this.blendSrcAlpha ||
551
+ dst !== this.blendDstAlpha
552
+ ) {
553
+ this.gl.blendFunc(src, dst);
554
+ this.blendSrcRgb = src;
555
+ this.blendDstRgb = dst;
556
+ this.blendSrcAlpha = src;
557
+ this.blendDstAlpha = dst;
558
+ }
559
+ }
560
+
561
+ /**
562
+ * ```
563
+ * gl.createBuffer();
564
+ * ```
565
+ *
566
+ * @returns
567
+ */
568
+ createBuffer() {
569
+ return this.gl.createBuffer();
570
+ }
571
+
572
+ /**
573
+ * ```
574
+ * gl.createFramebuffer();
575
+ * ```
576
+ * @returns
577
+ */
578
+ createFramebuffer() {
579
+ return this.gl.createFramebuffer();
580
+ }
581
+
582
+ /**
583
+ * ```
584
+ * gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
585
+ * ```
586
+ *
587
+ * @param framebuffer
588
+ */
589
+ bindFramebuffer(framebuffer: WebGLFramebuffer | null) {
590
+ this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, framebuffer);
591
+ }
592
+
593
+ /**
594
+ * ```
595
+ * gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
596
+ * ```
597
+ * @remarks
598
+ * **WebGL Difference**: Bind target is always `gl.FRAMEBUFFER` and textarget is always `gl.TEXTURE_2D`
599
+ */
600
+
601
+ framebufferTexture2D(
602
+ attachment: GLenum,
603
+ texture: WebGLTexture | null,
604
+ level: GLint,
605
+ ) {
606
+ const gl = this.gl;
607
+ gl.framebufferTexture2D(
608
+ gl.FRAMEBUFFER,
609
+ attachment,
610
+ gl.TEXTURE_2D,
611
+ texture,
612
+ level,
613
+ );
614
+ }
615
+
616
+ /**
617
+ * ```
618
+ * gl.clear(gl.COLOR_BUFFER_BIT);
619
+ * ```
620
+ *
621
+ * @remarks
622
+ * **WebGL Difference**: Clear mask is always `gl.COLOR_BUFFER_BIT`
623
+ */
624
+ clear() {
625
+ this.gl.clear(this.gl.COLOR_BUFFER_BIT);
626
+ }
627
+
628
+ /**
629
+ * ```
630
+ * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
631
+ * gl.bufferData(gl.ARRAY_BUFFER, data, usage);
632
+ * ```
633
+ *
634
+ * @remarks
635
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
636
+ *
637
+ * @param buffer
638
+ * @param data
639
+ * @param usage
640
+ */
641
+ arrayBufferData(
642
+ buffer: WebGLBuffer | null,
643
+ data: ArrayBufferView,
644
+ usage: GLenum,
645
+ ) {
646
+ if (this.boundArrayBuffer !== buffer) {
647
+ this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);
648
+ this.boundArrayBuffer = buffer;
649
+ }
650
+ this.gl.bufferData(this.gl.ARRAY_BUFFER, data, usage);
651
+ }
652
+
653
+ /**
654
+ * ```
655
+ * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
656
+ * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, usage);
657
+ * ```
658
+ * @remarks
659
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.bufferData` are combined into one function.
660
+ *
661
+ * @param buffer
662
+ * @param data
663
+ * @param usage
664
+ */
665
+ elementArrayBufferData(
666
+ buffer: WebGLBuffer | null,
667
+ data: ArrayBufferView,
668
+ usage: GLenum,
669
+ ) {
670
+ if (this.boundElementArrayBuffer !== buffer) {
671
+ this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, buffer);
672
+ this.boundElementArrayBuffer = buffer;
673
+ }
674
+ this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, data, usage);
675
+ }
676
+
677
+ /**
678
+ * ```
679
+ * gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
680
+ * gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
681
+ * ```
682
+ *
683
+ * @remarks
684
+ * **WebGL Combo**: `gl.bindBuffer` and `gl.vertexAttribPointer` are combined into one function.
685
+ *
686
+ * @param buffer
687
+ * @param index
688
+ * @param size
689
+ * @param type
690
+ * @param normalized
691
+ * @param stride
692
+ * @param offset
693
+ */
694
+ vertexAttribPointer(
695
+ buffer: WebGLBuffer,
696
+ index: GLuint,
697
+ size: GLint,
698
+ type: GLenum,
699
+ normalized: GLboolean,
700
+ stride: GLsizei,
701
+ offset: GLintptr,
702
+ ) {
703
+ if (this.boundArrayBuffer !== buffer) {
704
+ this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);
705
+ this.boundArrayBuffer = buffer;
706
+ }
707
+ this.gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
708
+ }
709
+
710
+ /**
711
+ * Returns object with Attribute names as key and numbers as location values
712
+ *
713
+ * @param program
714
+ * @returns object with numbers
715
+ */
716
+ getUniformLocations(
717
+ program: WebGLProgram,
718
+ ): Record<string, WebGLUniformLocation> {
719
+ const gl = this.gl;
720
+ const length = gl.getProgramParameter(
721
+ program,
722
+ gl.ACTIVE_UNIFORMS,
723
+ ) as number;
724
+ const result = {} as Record<string, WebGLUniformLocation>;
725
+ for (let i = 0; i < length; i++) {
726
+ const info = gl.getActiveUniform(program, i) as WebGLActiveInfo;
727
+ //remove bracket + value from uniform name;
728
+ let name = info.name.replace(/\[.*?\]/g, '');
729
+ result[name] = gl.getUniformLocation(
730
+ program,
731
+ name,
732
+ ) as WebGLUniformLocation;
733
+ }
734
+ return result;
735
+ }
736
+
737
+ /**
738
+ * Returns object with Attribute names as key and numbers as location values
739
+ * @param program
740
+ * @returns object with numbers
741
+ */
742
+ getAttributeLocations(program: WebGLProgram): string[] {
743
+ const gl = this.gl;
744
+ const length = gl.getProgramParameter(
745
+ program,
746
+ gl.ACTIVE_ATTRIBUTES,
747
+ ) as number;
748
+
749
+ const result: string[] = [];
750
+ for (let i = 0; i < length; i++) {
751
+ const { name } = gl.getActiveAttrib(program, i) as WebGLActiveInfo;
752
+ result[gl.getAttribLocation(program, name)] = name;
753
+ }
754
+ return result;
755
+ }
756
+
757
+ /**
758
+ * ```
759
+ * gl.useProgram(program);
760
+ * ```
761
+ *
762
+ * @param program
763
+ * @returns
764
+ */
765
+ useProgram(
766
+ program: WebGLProgram | null,
767
+ uniformLocations: Record<string, WebGLUniformLocation>,
768
+ ) {
769
+ if (this.curProgram === program) {
770
+ return;
771
+ }
772
+ this.gl.useProgram(program);
773
+ this.curProgram = program;
774
+ this.curUniformLocations = uniformLocations;
775
+ }
776
+
777
+ /**
778
+ * Sets the value of a single float uniform variable.
779
+ *
780
+ * @param location - The location of the uniform variable.
781
+ * @param v0 - The value to set.
782
+ */
783
+ uniform1f(location: string, v0: number) {
784
+ this.gl.uniform1f(this.curUniformLocations[location] || null, v0);
785
+ }
786
+
787
+ /**
788
+ * Sets the value of a float array uniform variable.
789
+ *
790
+ * @param location - The location of the uniform variable.
791
+ * @param value - The array of values to set.
792
+ */
793
+ uniform1fv(location: string, value: Float32Array) {
794
+ this.gl.uniform1fv(this.curUniformLocations[location] || null, value);
795
+ }
796
+
797
+ /**
798
+ * Sets the value of a single integer uniform variable.
799
+ *
800
+ * @param location - The location of the uniform variable.
801
+ * @param v0 - The value to set.
802
+ */
803
+ uniform1i(location: string, v0: number) {
804
+ this.gl.uniform1i(this.curUniformLocations[location] || null, v0);
805
+ }
806
+
807
+ /**
808
+ * Sets the value of an integer array uniform variable.
809
+ *
810
+ * @param location - The location of the uniform variable.
811
+ * @param value - The array of values to set.
812
+ */
813
+ uniform1iv(location: string, value: Int32Array) {
814
+ this.gl.uniform1iv(this.curUniformLocations[location] || null, value);
815
+ }
816
+
817
+ /**
818
+ * Sets the value of a vec2 uniform variable.
819
+ *
820
+ * @param location - The location of the uniform variable.
821
+ * @param v0 - The first component of the vector.
822
+ * @param v1 - The second component of the vector.
823
+ */
824
+ uniform2f(location: string, v0: number, v1: number) {
825
+ this.gl.uniform2f(this.curUniformLocations[location] || null, v0, v1);
826
+ }
827
+
828
+ /**
829
+ * Sets the value of a vec3 uniform variable.
830
+ *
831
+ * @param location - The location of the uniform variable.
832
+ * @param v - array of 4 numbers.
833
+ */
834
+ uniform2fa(location: string, value: Vec2) {
835
+ this.gl.uniform2f(
836
+ this.curUniformLocations[location] || null,
837
+ value[0],
838
+ value[1],
839
+ );
840
+ }
841
+
842
+ /**
843
+ * Sets the value of a vec2 array uniform variable.
844
+ *
845
+ * @param location - The location of the uniform variable.
846
+ * @param value - The array of vec2 values to set.
847
+ */
848
+ uniform2fv(location: string, value: Float32Array) {
849
+ this.gl.uniform2fv(this.curUniformLocations[location] || null, value);
850
+ }
851
+
852
+ /**
853
+ * Sets the value of a ivec2 uniform variable.
854
+ *
855
+ * @param location - The location of the uniform variable.
856
+ * @param v0 - The first component of the vector.
857
+ * @param v1 - The second component of the vector.
858
+ */
859
+ uniform2i(location: string, v0: number, v1: number) {
860
+ this.gl.uniform2i(this.curUniformLocations[location] || null, v0, v1);
861
+ }
862
+
863
+ /**
864
+ * Sets the value of an ivec2 array uniform variable.
865
+ *
866
+ * @param location - The location of the uniform variable.
867
+ * @param value - The array of ivec2 values to set.
868
+ */
869
+ uniform2iv(location: string, value: Int32Array) {
870
+ this.gl.uniform2iv(this.curUniformLocations[location] || null, value);
871
+ }
872
+
873
+ /**
874
+ * Sets the value of a vec3 uniform variable.
875
+ *
876
+ * @param location - The location of the uniform variable.
877
+ * @param v0 - The first component of the vector.
878
+ * @param v1 - The second component of the vector.
879
+ * @param v2 - The third component of the vector.
880
+ */
881
+ uniform3f(location: string, v0: number, v1: number, v2: number) {
882
+ this.gl.uniform3f(this.curUniformLocations[location] || null, v0, v1, v2);
883
+ }
884
+
885
+ /**
886
+ * Sets the value of a vec3 uniform variable.
887
+ *
888
+ * @param location - The location of the uniform variable.
889
+ * @param v - array of 4 numbers.
890
+ */
891
+ uniform3fa(location: string, value: Vec3) {
892
+ this.gl.uniform3f(
893
+ this.curUniformLocations[location] || null,
894
+ value[0],
895
+ value[1],
896
+ value[2],
897
+ );
898
+ }
899
+
900
+ /**
901
+ * Sets the value of a vec3 array uniform variable.
902
+ *
903
+ * @param location - The location of the uniform variable.
904
+ * @param value - The array of vec3 values to set.
905
+ */
906
+ uniform3fv(location: string, value: Float32Array) {
907
+ this.gl.uniform3fv(this.curUniformLocations[location] || null, value);
908
+ }
909
+
910
+ /**
911
+ * Sets the value of a ivec3 uniform variable.
912
+ *
913
+ * @param location - The location of the uniform variable.
914
+ * @param v0 - The first component of the vector.
915
+ * @param v1 - The second component of the vector.
916
+ * @param v2 - The third component of the vector.
917
+ */
918
+ uniform3i(location: string, v0: number, v1: number, v2: number) {
919
+ this.gl.uniform3i(this.curUniformLocations[location] || null, v0, v1, v2);
920
+ }
921
+
922
+ /**
923
+ * Sets the value of an ivec3 array uniform variable.
924
+ *
925
+ * @param location - The location of the uniform variable.
926
+ * @param value - The array of ivec3 values to set.
927
+ */
928
+ uniform3iv(location: string, value: Int32Array) {
929
+ this.gl.uniform3iv(this.curUniformLocations[location] || null, value);
930
+ }
931
+
932
+ /**
933
+ * Sets the value of a vec4 uniform variable.
934
+ *
935
+ * @param location - The location of the uniform variable.
936
+ * @param v0 - The first component of the vector.
937
+ * @param v1 - The second component of the vector.
938
+ * @param v2 - The third component of the vector.
939
+ * @param v3 - The fourth component of the vector.
940
+ */
941
+ uniform4f(location: string, v0: number, v1: number, v2: number, v3: number) {
942
+ this.gl.uniform4f(
943
+ this.curUniformLocations[location] || null,
944
+ v0,
945
+ v1,
946
+ v2,
947
+ v3,
948
+ );
949
+ }
950
+
951
+ /**
952
+ * Sets the value of a vec4 uniform variable.
953
+ *
954
+ * @param location - The location of the uniform variable.
955
+ * @param v - array of 4 numbers.
956
+ */
957
+ uniform4fa(location: string, value: Vec4) {
958
+ this.gl.uniform4f(
959
+ this.curUniformLocations[location] || null,
960
+ value[0],
961
+ value[1],
962
+ value[2],
963
+ value[3],
964
+ );
965
+ }
966
+
967
+ /**
968
+ * Sets the value of a vec4 array uniform variable.
969
+ *
970
+ * @param location - The location of the uniform variable.
971
+ * @param value - The array of vec4 values to set.
972
+ */
973
+ uniform4fv(location: string, value: Float32Array) {
974
+ this.gl.uniform4fv(this.curUniformLocations[location] || null, value);
975
+ }
976
+
977
+ /**
978
+ * Sets the value of a ivec4 uniform variable.
979
+ *
980
+ * @param location - The location of the uniform variable.
981
+ * @param v0 - The first component of the vector.
982
+ * @param v1 - The second component of the vector.
983
+ * @param v2 - The third component of the vector.
984
+ * @param v3 - The fourth component of the vector.
985
+ */
986
+ uniform4i(location: string, v0: number, v1: number, v2: number, v3: number) {
987
+ this.gl.uniform4i(
988
+ this.curUniformLocations[location] || null,
989
+ v0,
990
+ v1,
991
+ v2,
992
+ v3,
993
+ );
994
+ }
995
+
996
+ /**
997
+ * Sets the value of an ivec4 array uniform variable.
998
+ *
999
+ * @param location - The location of the uniform variable.
1000
+ * @param value - The array of ivec4 values to set.
1001
+ */
1002
+ uniform4iv(location: string, value: Int32Array) {
1003
+ this.gl.uniform4iv(this.curUniformLocations[location] || null, value);
1004
+ }
1005
+
1006
+ /**
1007
+ * Sets the value of a mat2 uniform variable.
1008
+ *
1009
+ * @param location - The location of the uniform variable.
1010
+ * @param transpose - Whether to transpose the matrix.
1011
+ * @param value - The array of mat2 values to set.
1012
+ */
1013
+ uniformMatrix2fv(location: string, value: Float32Array) {
1014
+ this.gl.uniformMatrix2fv(
1015
+ this.curUniformLocations[location] || null,
1016
+ false,
1017
+ value,
1018
+ );
1019
+ }
1020
+
1021
+ /**
1022
+ * Sets the value of a mat2 uniform variable.
1023
+ * @param location - The location of the uniform variable.
1024
+ * @param value - The array of mat2 values to set.
1025
+ */
1026
+ uniformMatrix3fv(location: string, value: Float32Array) {
1027
+ this.gl.uniformMatrix3fv(
1028
+ this.curUniformLocations[location] || null,
1029
+ false,
1030
+ value,
1031
+ );
1032
+ }
1033
+
1034
+ /**
1035
+ * Sets the value of a mat4 uniform variable.
1036
+ * @param location - The location of the uniform variable.
1037
+ * @param value - The array of mat4 values to set.
1038
+ */
1039
+ uniformMatrix4fv(location: string, value: Float32Array) {
1040
+ this.gl.uniformMatrix4fv(
1041
+ this.curUniformLocations[location] || null,
1042
+ false,
1043
+ value,
1044
+ );
1045
+ }
1046
+
1047
+ /**
1048
+ * ```
1049
+ * gl.getParameter(pname);
1050
+ * ```
1051
+ *
1052
+ * @param pname
1053
+ * @returns
1054
+ */
1055
+ getParameter(pname: GLenum): any {
1056
+ return this.gl.getParameter(pname);
1057
+ }
1058
+
1059
+ /**
1060
+ * ```
1061
+ * gl.drawElements(mode, count, type, offset);
1062
+ * ```
1063
+ *
1064
+ * @param mode
1065
+ * @param count
1066
+ * @param type
1067
+ * @param offset
1068
+ */
1069
+ drawElements(mode: GLenum, count: GLsizei, type: GLenum, offset: GLintptr) {
1070
+ this.gl.drawElements(mode, count, type, offset);
1071
+ }
1072
+
1073
+ /**
1074
+ * ```
1075
+ * gl.drawArrays(mode, first, count);
1076
+ * ```
1077
+ *
1078
+ * @param mode
1079
+ * @param first
1080
+ * @param count
1081
+ */
1082
+ drawArrays(mode: GLenum, first: GLint, count: GLsizei) {
1083
+ this.gl.drawArrays(mode, first, count);
1084
+ }
1085
+
1086
+ /**
1087
+ * ```
1088
+ * gl.drawArrays(mode, first, count);
1089
+ * ```
1090
+ *
1091
+ * @param name
1092
+ * @returns
1093
+ */
1094
+ getExtension(name: string) {
1095
+ return this.gl.getExtension(name);
1096
+ }
1097
+
1098
+ /**
1099
+ * ```
1100
+ * gl.getError(type);
1101
+ * ```
1102
+ *
1103
+ * @returns
1104
+ */
1105
+ getError() {
1106
+ return this.gl.getError();
1107
+ }
1108
+
1109
+ /**
1110
+ * ```
1111
+ * gl.createVertexArray();
1112
+ * ```
1113
+ *
1114
+ * @returns
1115
+ */
1116
+ createVertexArray() {
1117
+ if (this.gl instanceof WebGL2RenderingContext) {
1118
+ return this.gl.createVertexArray();
1119
+ }
1120
+ return undefined;
1121
+ }
1122
+
1123
+ /**
1124
+ * ```
1125
+ * gl.bindVertexArray(vertexArray);
1126
+ * ```
1127
+ *
1128
+ * @param vertexArray
1129
+ */
1130
+ bindVertexArray(vertexArray: WebGLVertexArrayObject | null) {
1131
+ if (this.gl instanceof WebGL2RenderingContext) {
1132
+ this.gl.bindVertexArray(vertexArray);
1133
+ }
1134
+ }
1135
+
1136
+ /**
1137
+ * ```
1138
+ * gl.getAttribLocation(program, name);
1139
+ * ```
1140
+ *
1141
+ * @param program
1142
+ * @param name
1143
+ * @returns
1144
+ */
1145
+ getAttribLocation(program: WebGLProgram, name: string) {
1146
+ return this.gl.getAttribLocation(program, name);
1147
+ }
1148
+
1149
+ /**
1150
+ * ```
1151
+ * gl.getUniformLocation(program, name);
1152
+ * ```
1153
+ *
1154
+ * @param program
1155
+ * @param name
1156
+ * @returns
1157
+ */
1158
+ getUniformLocation(program: WebGLProgram, name: string) {
1159
+ return this.gl.getUniformLocation(program, name);
1160
+ }
1161
+
1162
+ /**
1163
+ * ```
1164
+ * gl.enableVertexAttribArray(index);
1165
+ * ```
1166
+ *
1167
+ * @param index
1168
+ */
1169
+ enableVertexAttribArray(index: number) {
1170
+ this.gl.enableVertexAttribArray(index);
1171
+ }
1172
+
1173
+ /**
1174
+ * ```
1175
+ * gl.disableVertexAttribArray(index);
1176
+ * ```
1177
+ *
1178
+ * @param index
1179
+ */
1180
+ disableVertexAttribArray(index: number) {
1181
+ this.gl.disableVertexAttribArray(index);
1182
+ }
1183
+
1184
+ /**
1185
+ * ```
1186
+ * gl.createShader(type);
1187
+ * ```
1188
+ *
1189
+ * @param type
1190
+ * @returns
1191
+ */
1192
+ createShader(type: number) {
1193
+ return this.gl.createShader(type);
1194
+ }
1195
+
1196
+ /**
1197
+ * ```
1198
+ * gl.compileShader(shader);
1199
+ * ```
1200
+ *
1201
+ * @param shader
1202
+ * @returns
1203
+ */
1204
+ compileShader(shader: WebGLShader) {
1205
+ this.gl.compileShader(shader);
1206
+ }
1207
+
1208
+ /**
1209
+ * ```
1210
+ * gl.attachShader(program, shader);
1211
+ * ```
1212
+ *
1213
+ * @param program
1214
+ * @param shader
1215
+ */
1216
+ attachShader(program: WebGLProgram, shader: WebGLShader) {
1217
+ this.gl.attachShader(program, shader);
1218
+ }
1219
+
1220
+ /**
1221
+ * ```
1222
+ * gl.linkProgram(program);
1223
+ * ```
1224
+ *
1225
+ * @param program
1226
+ */
1227
+ linkProgram(program: WebGLProgram) {
1228
+ this.gl.linkProgram(program);
1229
+ }
1230
+
1231
+ /**
1232
+ * ```
1233
+ * gl.deleteProgram(shader);
1234
+ * ```
1235
+ *
1236
+ * @param shader
1237
+ */
1238
+ deleteProgram(shader: WebGLProgram) {
1239
+ this.gl.deleteProgram(shader);
1240
+ }
1241
+
1242
+ /**
1243
+ * ```
1244
+ * gl.getShaderParameter(shader, pname);
1245
+ * ```
1246
+ *
1247
+ * @param shader
1248
+ * @param pname
1249
+ */
1250
+ getShaderParameter(shader: WebGLShader, pname: GLenum) {
1251
+ return this.gl.getShaderParameter(shader, pname);
1252
+ }
1253
+
1254
+ /**
1255
+ * ```
1256
+ * gl.getShaderInfoLog(shader);
1257
+ * ```
1258
+ *
1259
+ * @param shader
1260
+ */
1261
+ getShaderInfoLog(shader: WebGLShader) {
1262
+ return this.gl.getShaderInfoLog(shader);
1263
+ }
1264
+
1265
+ /**
1266
+ * ```
1267
+ * gl.createProgram();
1268
+ * ```
1269
+ *
1270
+ * @returns
1271
+ */
1272
+ createProgram() {
1273
+ return this.gl.createProgram();
1274
+ }
1275
+
1276
+ /**
1277
+ * ```
1278
+ * gl.getProgramParameter(program, pname);
1279
+ * ```
1280
+ *
1281
+ * @param program
1282
+ * @param pname
1283
+ * @returns
1284
+ */
1285
+ getProgramParameter(program: WebGLProgram, pname: GLenum) {
1286
+ return this.gl.getProgramParameter(program, pname);
1287
+ }
1288
+
1289
+ /**
1290
+ * ```
1291
+ * gl.getProgramInfoLog(program);
1292
+ * ```
1293
+ *
1294
+ * @param program
1295
+ * @returns
1296
+ */
1297
+ getProgramInfoLog(program: WebGLProgram) {
1298
+ return this.gl.getProgramInfoLog(program);
1299
+ }
1300
+
1301
+ /**
1302
+ * ```
1303
+ * gl.shaderSource(shader, source);
1304
+ * ```
1305
+ *
1306
+ * @param shader
1307
+ * @param source
1308
+ */
1309
+ shaderSource(shader: WebGLShader, source: string) {
1310
+ this.gl.shaderSource(shader, source);
1311
+ }
1312
+
1313
+ /**
1314
+ * ```
1315
+ * gl.deleteShader(shader);
1316
+ * ```
1317
+ *
1318
+ * @param shader
1319
+ */
1320
+ deleteShader(shader: WebGLShader) {
1321
+ this.gl.deleteShader(shader);
1322
+ }
1323
+
1324
+ /**
1325
+ * ```
1326
+ * gl.deleteBuffer(buffer);
1327
+ * ```
1328
+ *
1329
+ * @param buffer - The buffer to delete
1330
+ */
1331
+ deleteBuffer(buffer: WebGLBuffer) {
1332
+ const { gl } = this;
1333
+ gl.deleteBuffer(buffer);
1334
+
1335
+ // Reset bound buffers if they match the deleted buffer
1336
+ if (this.boundArrayBuffer === buffer) {
1337
+ this.boundArrayBuffer = null;
1338
+ }
1339
+ }
1340
+
1341
+ /**
1342
+ * ```
1343
+ * gl.deleteVertexArray(vertexArray);
1344
+ * ```
1345
+ *
1346
+ * @param vertexArray - The vertex array object to delete
1347
+ */
1348
+ deleteVertexArray(vertexArray: WebGLVertexArrayObject) {
1349
+ if (this.isWebGl2()) {
1350
+ (this.gl as WebGL2RenderingContext).deleteVertexArray(vertexArray);
1351
+ }
1352
+ }
1353
+
1354
+ /**
1355
+ * Check for WebGL errors and return error information
1356
+ * @param operation Description of the operation for error reporting
1357
+ * @returns Object with error information or null if no error
1358
+ */
1359
+ checkError(
1360
+ operation: string,
1361
+ ): { error: number; errorName: string; message: string } | null {
1362
+ const error = this.getError();
1363
+ if (error !== 0) {
1364
+ // 0 is GL_NO_ERROR
1365
+ let errorName = 'UNKNOWN_ERROR';
1366
+ switch (error) {
1367
+ case this.INVALID_ENUM:
1368
+ errorName = 'INVALID_ENUM';
1369
+ break;
1370
+ case 0x0501: // GL_INVALID_VALUE
1371
+ errorName = 'INVALID_VALUE';
1372
+ break;
1373
+ case this.INVALID_OPERATION:
1374
+ errorName = 'INVALID_OPERATION';
1375
+ break;
1376
+ case 0x0505: // GL_OUT_OF_MEMORY
1377
+ errorName = 'OUT_OF_MEMORY';
1378
+ break;
1379
+ case 0x9242: // GL_CONTEXT_LOST_WEBGL
1380
+ errorName = 'CONTEXT_LOST_WEBGL';
1381
+ break;
1382
+ }
1383
+
1384
+ const message = `WebGL ${errorName} (0x${error.toString(
1385
+ 16,
1386
+ )}) during ${operation}`;
1387
+ return { error, errorName, message };
1388
+ }
1389
+ return null;
1390
+ }
1391
+
1392
+ /**
1393
+ *
1394
+ * Compressed Textures support
1395
+ */
1396
+ uploadKTX(texture: WebGLTexture, data: CompressedData) {
1397
+ const { glInternalFormat, mipmaps, w: width, h: height, blockInfo } = data;
1398
+ if (mipmaps === undefined) {
1399
+ return;
1400
+ }
1401
+
1402
+ this.bindTexture(texture);
1403
+
1404
+ const blockWidth = blockInfo.width;
1405
+ const blockHeight = blockInfo.height;
1406
+ let w = width;
1407
+ let h = height;
1408
+
1409
+ for (let i = 0; i < mipmaps!.length; i++) {
1410
+ let view = new Uint8Array(mipmaps![i]!);
1411
+
1412
+ const uploadW = Math.ceil(w / blockWidth) * blockWidth;
1413
+ const uploadH = Math.ceil(h / blockHeight) * blockHeight;
1414
+
1415
+ const expectedBytes =
1416
+ Math.ceil(w / blockWidth) *
1417
+ Math.ceil(h / blockHeight) *
1418
+ blockInfo.bytes;
1419
+
1420
+ if (view.byteLength < expectedBytes) {
1421
+ const padded = new Uint8Array(expectedBytes);
1422
+ padded.set(view);
1423
+ view = padded;
1424
+ }
1425
+
1426
+ this.compressedTexImage2D(i, glInternalFormat, uploadW, uploadH, 0, view);
1427
+
1428
+ w = Math.max(1, w >> 1);
1429
+ h = Math.max(1, h >> 1);
1430
+ }
1431
+
1432
+ this.texParameteri(this.TEXTURE_WRAP_S, this.CLAMP_TO_EDGE);
1433
+ this.texParameteri(this.TEXTURE_WRAP_T, this.CLAMP_TO_EDGE);
1434
+ this.texParameteri(this.TEXTURE_MAG_FILTER, this.LINEAR);
1435
+ this.texParameteri(
1436
+ this.TEXTURE_MIN_FILTER,
1437
+ mipmaps!.length > 1 ? this.LINEAR_MIPMAP_LINEAR : this.LINEAR,
1438
+ );
1439
+ }
1440
+
1441
+ uploadPVR(texture: WebGLTexture, data: CompressedData) {
1442
+ const { glInternalFormat, mipmaps, w: width, h: height } = data;
1443
+ if (mipmaps === undefined) {
1444
+ return;
1445
+ }
1446
+ this.bindTexture(texture);
1447
+
1448
+ let w = width;
1449
+ let h = height;
1450
+
1451
+ for (let i = 0; i < mipmaps!.length; i++) {
1452
+ this.compressedTexImage2D(
1453
+ i,
1454
+ glInternalFormat,
1455
+ w,
1456
+ h,
1457
+ 0,
1458
+ new Uint8Array(mipmaps[i]!),
1459
+ );
1460
+
1461
+ w = Math.max(1, w >> 1);
1462
+ h = Math.max(1, h >> 1);
1463
+ }
1464
+
1465
+ this.texParameteri(this.TEXTURE_WRAP_S, this.CLAMP_TO_EDGE);
1466
+ this.texParameteri(this.TEXTURE_WRAP_T, this.CLAMP_TO_EDGE);
1467
+ this.texParameteri(this.TEXTURE_MAG_FILTER, this.LINEAR);
1468
+ this.texParameteri(
1469
+ this.TEXTURE_MIN_FILTER,
1470
+ mipmaps.length > 1 ? this.LINEAR_MIPMAP_LINEAR : this.LINEAR,
1471
+ );
1472
+ }
1473
+
1474
+ uploadASTC(texture: WebGLTexture, data: CompressedData) {
1475
+ if (this.getExtension('WEBGL_compressed_texture_astc') === null) {
1476
+ throw new Error('ASTC compressed textures not supported by this device');
1477
+ }
1478
+
1479
+ this.bindTexture(texture);
1480
+
1481
+ const { glInternalFormat, mipmaps, w, h } = data;
1482
+ if (mipmaps === undefined) {
1483
+ return;
1484
+ }
1485
+
1486
+ const view = new Uint8Array(mipmaps[0]!);
1487
+
1488
+ this.compressedTexImage2D(0, glInternalFormat, w, h, 0, view);
1489
+ // ASTC textures MUST use no mipmaps unless stored
1490
+ this.texParameteri(this.TEXTURE_WRAP_S, this.CLAMP_TO_EDGE);
1491
+ this.texParameteri(this.TEXTURE_WRAP_T, this.CLAMP_TO_EDGE);
1492
+ this.texParameteri(this.TEXTURE_MAG_FILTER, this.LINEAR);
1493
+ this.texParameteri(this.TEXTURE_MIN_FILTER, this.LINEAR);
1494
+ }
1495
+ }
1496
+
1497
+ // prettier-ignore
1498
+ type IsUniformMethod<MethodName, MethodType> = MethodName extends `uniform${string}`
1499
+ ?
1500
+ MethodType extends (location: WebGLUniformLocation | null, ...args: any[]) => void
1501
+ ? true
1502
+ : false
1503
+ : false;
1504
+
1505
+ // prettier-ignore
1506
+ export type UniformMethodMap = {
1507
+ [Key in keyof WebGLRenderingContext as IsUniformMethod<Key, WebGLRenderingContext[Key]> extends true ? Key : never]: WebGLRenderingContext[Key] extends (
1508
+ location: WebGLUniformLocation | null,
1509
+ ...args: infer T
1510
+ ) => void
1511
+ ? T
1512
+ : never;
1513
+ };
1514
+
1515
+ /**
1516
+ * Compare two arrays for equality.
1517
+ *
1518
+ * @remarks
1519
+ * This function will not try to compare nested arrays or Float32Arrays and
1520
+ * instead will always return false when they are encountered.
1521
+ *
1522
+ * @param a
1523
+ * @param b
1524
+ * @returns
1525
+ */
1526
+ export function compareArrays<T>(a: T[], b: T[]): boolean {
1527
+ if (a.length !== b.length) {
1528
+ return false;
1529
+ }
1530
+
1531
+ let result = false;
1532
+ for (let i = 0; i < a.length; i++) {
1533
+ if (Array.isArray(a[i]) || a[i] instanceof Float32Array) {
1534
+ result = false;
1535
+ break;
1536
+ }
1537
+
1538
+ if (a[i] !== b[i]) {
1539
+ result = false;
1540
+ break;
1541
+ }
1542
+
1543
+ result = true;
1544
+ }
1545
+
1546
+ return result;
1547
+ }