@lightningjs/renderer 3.0.0-beta6 → 3.0.0-beta7

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