@lightningjs/renderer 3.0.0-beta10 → 3.0.0-beta12

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