@lightningjs/renderer 3.0.0-beta8 → 3.0.0

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