@lightningjs/renderer 2.9.2 → 2.11.0

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