@lightningjs/renderer 3.0.0-beta4 → 3.0.0-beta5

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 (335) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +147 -147
  4. package/dist/exports/core-api.d.ts +74 -0
  5. package/dist/exports/core-api.js +96 -0
  6. package/dist/exports/core-api.js.map +1 -0
  7. package/dist/exports/main-api.d.ts +30 -0
  8. package/dist/exports/main-api.js +45 -0
  9. package/dist/exports/main-api.js.map +1 -0
  10. package/dist/src/core/CoreExtension.d.ts +12 -0
  11. package/dist/src/core/CoreExtension.js +29 -0
  12. package/dist/src/core/CoreExtension.js.map +1 -0
  13. package/dist/src/core/CoreNode.js +1 -1
  14. package/dist/src/core/CoreNode.js.map +1 -1
  15. package/dist/src/core/CoreStuff.js +138 -0
  16. package/dist/src/core/CoreStuff.js.map +1 -0
  17. package/dist/src/core/CoreTextNode.js +1 -0
  18. package/dist/src/core/CoreTextNode.js.map +1 -1
  19. package/dist/src/core/CoreTexturizer.d.ts +14 -0
  20. package/dist/src/core/CoreTexturizer.js +47 -0
  21. package/dist/src/core/CoreTexturizer.js.map +1 -0
  22. package/dist/src/core/LngNode.d.ts +736 -0
  23. package/dist/src/core/LngNode.js +1174 -0
  24. package/dist/src/core/LngNode.js.map +1 -0
  25. package/dist/src/core/Matrix2DContext.d.ts +15 -0
  26. package/dist/src/core/Matrix2DContext.js +45 -0
  27. package/dist/src/core/Matrix2DContext.js.map +1 -0
  28. package/dist/src/core/ShaderNode.d.ts +10 -0
  29. package/dist/src/core/ShaderNode.js +30 -0
  30. package/dist/src/core/ShaderNode.js.map +1 -0
  31. package/dist/src/core/TextNode.d.ts +103 -0
  32. package/dist/src/core/TextNode.js +331 -0
  33. package/dist/src/core/TextNode.js.map +1 -0
  34. package/dist/src/core/lib/Coords.d.ts +14 -0
  35. package/dist/src/core/lib/Coords.js +55 -0
  36. package/dist/src/core/lib/Coords.js.map +1 -0
  37. package/dist/src/core/lib/glm/common.d.ts +162 -0
  38. package/dist/src/core/lib/glm/common.js +81 -0
  39. package/dist/src/core/lib/glm/common.js.map +1 -0
  40. package/dist/src/core/lib/glm/index.d.ts +11 -0
  41. package/dist/src/core/lib/glm/index.js +30 -0
  42. package/dist/src/core/lib/glm/index.js.map +1 -0
  43. package/dist/src/core/lib/glm/mat2.d.ts +219 -0
  44. package/dist/src/core/lib/glm/mat2.js +396 -0
  45. package/dist/src/core/lib/glm/mat2.js.map +1 -0
  46. package/dist/src/core/lib/glm/mat2d.d.ts +237 -0
  47. package/dist/src/core/lib/glm/mat2d.js +442 -0
  48. package/dist/src/core/lib/glm/mat2d.js.map +1 -0
  49. package/dist/src/core/lib/glm/mat3.d.ts +283 -0
  50. package/dist/src/core/lib/glm/mat3.js +680 -0
  51. package/dist/src/core/lib/glm/mat3.js.map +1 -0
  52. package/dist/src/core/lib/glm/mat4.d.ts +550 -0
  53. package/dist/src/core/lib/glm/mat4.js +1802 -0
  54. package/dist/src/core/lib/glm/mat4.js.map +1 -0
  55. package/dist/src/core/lib/glm/quat.d.ts +363 -0
  56. package/dist/src/core/lib/glm/quat.js +693 -0
  57. package/dist/src/core/lib/glm/quat.js.map +1 -0
  58. package/dist/src/core/lib/glm/quat2.d.ts +356 -0
  59. package/dist/src/core/lib/glm/quat2.js +754 -0
  60. package/dist/src/core/lib/glm/quat2.js.map +1 -0
  61. package/dist/src/core/lib/glm/vec2.d.ts +365 -0
  62. package/dist/src/core/lib/glm/vec2.js +569 -0
  63. package/dist/src/core/lib/glm/vec2.js.map +1 -0
  64. package/dist/src/core/lib/glm/vec3.d.ts +406 -0
  65. package/dist/src/core/lib/glm/vec3.js +720 -0
  66. package/dist/src/core/lib/glm/vec3.js.map +1 -0
  67. package/dist/src/core/lib/glm/vec4.d.ts +330 -0
  68. package/dist/src/core/lib/glm/vec4.js +608 -0
  69. package/dist/src/core/lib/glm/vec4.js.map +1 -0
  70. package/dist/src/core/renderers/CoreShaderManager.d.ts +19 -0
  71. package/dist/src/core/renderers/CoreShaderManager.js +33 -0
  72. package/dist/src/core/renderers/CoreShaderManager.js.map +1 -0
  73. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.d.ts +27 -0
  74. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.js +82 -0
  75. package/dist/src/core/renderers/webgl/WebGlCoreShaderManager.js.map +1 -0
  76. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.d.ts +11 -0
  77. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.js +34 -0
  78. package/dist/src/core/renderers/webgl/WebGlCoreShaderProgram.js.map +1 -0
  79. package/dist/src/core/renderers/webgl/internal/ShaderUtils.js +35 -35
  80. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  81. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  82. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  83. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  84. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  85. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  86. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  87. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  88. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  89. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  90. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  91. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  92. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  93. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  94. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  95. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  96. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  97. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  98. package/dist/src/core/scene/Scene.d.ts +59 -0
  99. package/dist/src/core/scene/Scene.js +106 -0
  100. package/dist/src/core/scene/Scene.js.map +1 -0
  101. package/dist/src/core/shaders/webgl/Border.js +59 -59
  102. package/dist/src/core/shaders/webgl/Default.js +47 -47
  103. package/dist/src/core/shaders/webgl/DefaultBatched.js +61 -61
  104. package/dist/src/core/shaders/webgl/HolePunch.js +32 -32
  105. package/dist/src/core/shaders/webgl/LinearGradient.js +36 -36
  106. package/dist/src/core/shaders/webgl/RadialGradient.js +33 -33
  107. package/dist/src/core/shaders/webgl/Rounded.js +71 -71
  108. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +66 -66
  109. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +79 -79
  110. package/dist/src/core/shaders/webgl/RoundedWithShadow.js +54 -54
  111. package/dist/src/core/shaders/webgl/SdfShader.js +62 -62
  112. package/dist/src/core/shaders/webgl/Shadow.js +83 -83
  113. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.d.ts +20 -0
  114. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js +55 -0
  115. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js.map +1 -0
  116. package/dist/src/main-api/ICoreDriver.d.ts +27 -0
  117. package/dist/src/main-api/ICoreDriver.js +20 -0
  118. package/dist/src/main-api/ICoreDriver.js.map +1 -0
  119. package/dist/src/main-api/IRenderDriver.d.ts +20 -0
  120. package/dist/src/main-api/IRenderDriver.js +20 -0
  121. package/dist/src/main-api/IRenderDriver.js.map +1 -0
  122. package/dist/src/main-api/IShaderController.d.ts +14 -0
  123. package/dist/src/main-api/IShaderController.js +30 -0
  124. package/dist/src/main-api/IShaderController.js.map +1 -0
  125. package/dist/src/main-api/IShaderNode.d.ts +17 -0
  126. package/dist/src/main-api/IShaderNode.js +19 -0
  127. package/dist/src/main-api/IShaderNode.js.map +1 -0
  128. package/dist/src/main-api/RendererMain.d.ts +375 -0
  129. package/dist/src/main-api/RendererMain.js +365 -0
  130. package/dist/src/main-api/RendererMain.js.map +1 -0
  131. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.d.ts +9 -0
  132. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js +38 -0
  133. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js.map +1 -0
  134. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.d.ts +56 -0
  135. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js +101 -0
  136. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js.map +1 -0
  137. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.d.ts +32 -0
  138. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js +28 -0
  139. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js.map +1 -0
  140. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +24 -0
  141. package/dist/src/render-drivers/main/MainCoreDriver.js +118 -0
  142. package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -0
  143. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +99 -0
  144. package/dist/src/render-drivers/main/MainOnlyNode.js +396 -0
  145. package/dist/src/render-drivers/main/MainOnlyNode.js.map +1 -0
  146. package/dist/src/render-drivers/main/MainOnlyShaderController.d.ts +6 -0
  147. package/dist/src/render-drivers/main/MainOnlyShaderController.js +15 -0
  148. package/dist/src/render-drivers/main/MainOnlyShaderController.js.map +1 -0
  149. package/dist/src/render-drivers/main/MainOnlyShaderNode.d.ts +7 -0
  150. package/dist/src/render-drivers/main/MainOnlyShaderNode.js +34 -0
  151. package/dist/src/render-drivers/main/MainOnlyShaderNode.js.map +1 -0
  152. package/dist/src/render-drivers/main/MainOnlyTextNode.d.ts +47 -0
  153. package/dist/src/render-drivers/main/MainOnlyTextNode.js +205 -0
  154. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +1 -0
  155. package/dist/src/render-drivers/main/MainRenderDriver.d.ts +17 -0
  156. package/dist/src/render-drivers/main/MainRenderDriver.js +88 -0
  157. package/dist/src/render-drivers/main/MainRenderDriver.js.map +1 -0
  158. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +90 -0
  159. package/dist/src/render-drivers/threadx/NodeStruct.js +281 -0
  160. package/dist/src/render-drivers/threadx/NodeStruct.js.map +1 -0
  161. package/dist/src/render-drivers/threadx/SharedNode.d.ts +39 -0
  162. package/dist/src/render-drivers/threadx/SharedNode.js +60 -0
  163. package/dist/src/render-drivers/threadx/SharedNode.js.map +1 -0
  164. package/dist/src/render-drivers/threadx/TextNodeStruct.d.ts +44 -0
  165. package/dist/src/render-drivers/threadx/TextNodeStruct.js +201 -0
  166. package/dist/src/render-drivers/threadx/TextNodeStruct.js.map +1 -0
  167. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +28 -0
  168. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +234 -0
  169. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -0
  170. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.d.ts +20 -0
  171. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js +84 -0
  172. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js.map +1 -0
  173. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +44 -0
  174. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +154 -0
  175. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +1 -0
  176. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.d.ts +6 -0
  177. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.js +16 -0
  178. package/dist/src/render-drivers/threadx/ThreadXMainShaderController.js.map +1 -0
  179. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.d.ts +7 -0
  180. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.js +15 -0
  181. package/dist/src/render-drivers/threadx/ThreadXMainShaderNode.js.map +1 -0
  182. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.d.ts +28 -0
  183. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js +55 -0
  184. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js.map +1 -0
  185. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.d.ts +21 -0
  186. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.js +198 -0
  187. package/dist/src/render-drivers/threadx/ThreadXRenderDriver.js.map +1 -0
  188. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +70 -0
  189. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js +32 -0
  190. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +1 -0
  191. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.d.ts +19 -0
  192. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +177 -0
  193. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +1 -0
  194. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.d.ts +27 -0
  195. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +108 -0
  196. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +1 -0
  197. package/dist/src/render-drivers/threadx/worker/renderer.d.ts +1 -0
  198. package/dist/src/render-drivers/threadx/worker/renderer.js +145 -0
  199. package/dist/src/render-drivers/threadx/worker/renderer.js.map +1 -0
  200. package/dist/src/render-drivers/utils.d.ts +12 -0
  201. package/dist/src/render-drivers/utils.js +69 -0
  202. package/dist/src/render-drivers/utils.js.map +1 -0
  203. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  204. package/exports/canvas-shaders.ts +28 -28
  205. package/exports/canvas.ts +45 -45
  206. package/exports/index.ts +90 -90
  207. package/exports/inspector.ts +24 -24
  208. package/exports/utils.ts +44 -44
  209. package/exports/webgl-shaders.ts +28 -28
  210. package/exports/webgl.ts +50 -50
  211. package/package.json +2 -1
  212. package/scripts/please-use-pnpm.js +13 -13
  213. package/src/common/CommonTypes.ts +146 -146
  214. package/src/common/EventEmitter.ts +77 -77
  215. package/src/common/IAnimationController.ts +92 -92
  216. package/src/common/IEventEmitter.ts +28 -28
  217. package/src/core/CoreNode.test.ts +203 -203
  218. package/src/core/CoreNode.ts +2494 -2494
  219. package/src/core/CoreShaderManager.ts +188 -188
  220. package/src/core/CoreTextNode.ts +449 -448
  221. package/src/core/CoreTextureManager.ts +601 -601
  222. package/src/core/Stage.ts +754 -754
  223. package/src/core/TextureMemoryManager.ts +395 -395
  224. package/src/core/animations/AnimationManager.ts +38 -38
  225. package/src/core/animations/CoreAnimation.ts +284 -284
  226. package/src/core/animations/CoreAnimationController.ts +157 -157
  227. package/src/core/lib/ContextSpy.ts +41 -41
  228. package/src/core/lib/ImageWorker.ts +280 -280
  229. package/src/core/lib/Matrix3d.ts +244 -244
  230. package/src/core/lib/RenderCoords.ts +71 -71
  231. package/src/core/lib/WebGlContextWrapper.ts +1374 -1374
  232. package/src/core/lib/textureCompression.ts +152 -152
  233. package/src/core/lib/textureSvg.ts +78 -78
  234. package/src/core/lib/utils.ts +386 -386
  235. package/src/core/lib/validateImageBitmap.ts +87 -87
  236. package/src/core/platform.ts +64 -64
  237. package/src/core/platforms/Platform.ts +77 -77
  238. package/src/core/platforms/web/WebPlatform.ts +84 -84
  239. package/src/core/renderers/CoreContextTexture.ts +43 -43
  240. package/src/core/renderers/CoreRenderOp.ts +22 -22
  241. package/src/core/renderers/CoreRenderer.ts +109 -109
  242. package/src/core/renderers/CoreShaderNode.ts +165 -165
  243. package/src/core/renderers/CoreShaderProgram.ts +23 -23
  244. package/src/core/renderers/canvas/CanvasRenderer.ts +298 -298
  245. package/src/core/renderers/canvas/CanvasShaderNode.ts +99 -99
  246. package/src/core/renderers/canvas/CanvasTexture.ts +156 -156
  247. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +220 -220
  248. package/src/core/renderers/canvas/internal/ColorUtils.ts +85 -85
  249. package/src/core/renderers/webgl/WebGlCtxRenderTexture.ts +86 -86
  250. package/src/core/renderers/webgl/WebGlCtxSubTexture.ts +50 -50
  251. package/src/core/renderers/webgl/WebGlCtxTexture.ts +301 -301
  252. package/src/core/renderers/webgl/WebGlRenderOp.ts +161 -161
  253. package/src/core/renderers/webgl/WebGlRenderer.ts +750 -750
  254. package/src/core/renderers/webgl/WebGlShaderNode.ts +437 -437
  255. package/src/core/renderers/webgl/WebGlShaderProgram.ts +318 -318
  256. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  257. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  258. package/src/core/renderers/webgl/internal/ShaderUtils.ts +281 -281
  259. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  260. package/src/core/shaders/canvas/Border.ts +78 -78
  261. package/src/core/shaders/canvas/HolePunch.ts +62 -62
  262. package/src/core/shaders/canvas/LinearGradient.ts +69 -69
  263. package/src/core/shaders/canvas/RadialGradient.ts +113 -113
  264. package/src/core/shaders/canvas/Rounded.ts +55 -55
  265. package/src/core/shaders/canvas/RoundedWithBorder.ts +68 -68
  266. package/src/core/shaders/canvas/RoundedWithBorderAndShadow.ts +88 -88
  267. package/src/core/shaders/canvas/RoundedWithShadow.ts +69 -69
  268. package/src/core/shaders/canvas/Shadow.ts +52 -52
  269. package/src/core/shaders/canvas/utils/render.ts +151 -151
  270. package/src/core/shaders/templates/BorderTemplate.ts +115 -115
  271. package/src/core/shaders/templates/HolePunchTemplate.ts +82 -82
  272. package/src/core/shaders/templates/LinearGradientTemplate.ts +71 -71
  273. package/src/core/shaders/templates/RadialGradientTemplate.ts +81 -81
  274. package/src/core/shaders/templates/RoundedTemplate.ts +98 -98
  275. package/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.ts +38 -38
  276. package/src/core/shaders/templates/RoundedWithBorderTemplate.ts +35 -35
  277. package/src/core/shaders/templates/RoundedWithShadowTemplate.ts +35 -35
  278. package/src/core/shaders/templates/ShadowTemplate.ts +106 -106
  279. package/src/core/shaders/templates/shaderUtils.ts +47 -47
  280. package/src/core/shaders/webgl/Border.ts +96 -96
  281. package/src/core/shaders/webgl/Default.ts +89 -89
  282. package/src/core/shaders/webgl/DefaultBatched.ts +129 -129
  283. package/src/core/shaders/webgl/HolePunch.ts +78 -78
  284. package/src/core/shaders/webgl/LinearGradient.ts +81 -81
  285. package/src/core/shaders/webgl/RadialGradient.ts +84 -84
  286. package/src/core/shaders/webgl/Rounded.ts +117 -117
  287. package/src/core/shaders/webgl/RoundedWithBorder.ts +114 -114
  288. package/src/core/shaders/webgl/RoundedWithBorderAndShadow.ts +133 -133
  289. package/src/core/shaders/webgl/RoundedWithShadow.ts +98 -98
  290. package/src/core/shaders/webgl/SdfShader.ts +134 -134
  291. package/src/core/shaders/webgl/Shadow.ts +115 -115
  292. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  293. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  294. package/src/core/text-rendering/TrFontManager.ts +183 -183
  295. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -176
  296. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  297. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  298. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  299. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  300. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +94 -94
  301. package/src/core/text-rendering/font-face-types/utils.ts +39 -39
  302. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +514 -514
  303. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +863 -863
  304. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +846 -846
  305. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  306. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  307. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  308. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  309. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  310. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  311. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  312. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +497 -497
  313. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  314. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  315. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  316. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  317. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  318. package/src/core/text-rendering/renderers/TextRenderer.ts +567 -567
  319. package/src/core/textures/ColorTexture.ts +102 -102
  320. package/src/core/textures/ImageTexture.ts +410 -410
  321. package/src/core/textures/NoiseTexture.ts +104 -104
  322. package/src/core/textures/RenderTexture.ts +85 -85
  323. package/src/core/textures/SubTexture.ts +205 -205
  324. package/src/core/textures/Texture.ts +358 -358
  325. package/src/core/utils.ts +227 -227
  326. package/src/env.d.ts +7 -7
  327. package/src/main-api/INode.ts +100 -100
  328. package/src/main-api/Inspector.ts +522 -522
  329. package/src/main-api/Renderer.ts +675 -675
  330. package/src/main-api/utils.ts +45 -45
  331. package/src/utils.ts +267 -267
  332. package/COPYING +0 -1
  333. package/dist/src/core/temp.js +0 -77
  334. package/dist/src/core/temp.js.map +0 -1
  335. /package/dist/src/core/{temp.d.ts → CoreStuff.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
+ }