agora-electron-sdk 0.0.0-dev.1

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 (369) hide show
  1. package/CHANGELOG.md +315 -0
  2. package/LICENSE +21 -0
  3. package/README.md +143 -0
  4. package/gulpfile.js +52 -0
  5. package/js/AgoraSdk.js +64 -0
  6. package/js/Decoder/gpu-utils.js +74 -0
  7. package/js/Decoder/index.js +172 -0
  8. package/js/Private/AgoraBase.js +3985 -0
  9. package/js/Private/AgoraMediaBase.js +1014 -0
  10. package/js/Private/AgoraMediaPlayerTypes.js +316 -0
  11. package/js/Private/IAgoraH265Transcoder.js +36 -0
  12. package/js/Private/IAgoraLog.js +78 -0
  13. package/js/Private/IAgoraMediaEngine.js +32 -0
  14. package/js/Private/IAgoraMediaPlayer.js +16 -0
  15. package/js/Private/IAgoraMediaPlayerSource.js +3 -0
  16. package/js/Private/IAgoraMediaRecorder.js +10 -0
  17. package/js/Private/IAgoraMediaStreamingSource.js +74 -0
  18. package/js/Private/IAgoraMusicContentCenter.js +157 -0
  19. package/js/Private/IAgoraRhythmPlayer.js +62 -0
  20. package/js/Private/IAgoraRtcEngine.js +982 -0
  21. package/js/Private/IAgoraRtcEngineEx.js +19 -0
  22. package/js/Private/IAgoraSpatialAudio.js +24 -0
  23. package/js/Private/IAudioDeviceManager.js +20 -0
  24. package/js/Private/extension/AgoraBaseExtension.js +3 -0
  25. package/js/Private/extension/AgoraMediaBaseExtension.js +2 -0
  26. package/js/Private/extension/AgoraMediaPlayerTypesExtension.js +2 -0
  27. package/js/Private/extension/IAgoraH265TranscoderExtension.js +2 -0
  28. package/js/Private/extension/IAgoraLogExtension.js +2 -0
  29. package/js/Private/extension/IAgoraMediaEngineExtension.js +2 -0
  30. package/js/Private/extension/IAgoraMediaPlayerExtension.js +2 -0
  31. package/js/Private/extension/IAgoraMediaPlayerSourceExtension.js +2 -0
  32. package/js/Private/extension/IAgoraMediaRecorderExtension.js +2 -0
  33. package/js/Private/extension/IAgoraMusicContentCenterExtension.js +2 -0
  34. package/js/Private/extension/IAgoraRhythmPlayerExtension.js +2 -0
  35. package/js/Private/extension/IAgoraRtcEngineExExtension.js +2 -0
  36. package/js/Private/extension/IAgoraRtcEngineExtension.js +2 -0
  37. package/js/Private/extension/IAgoraSpatialAudioExtension.js +2 -0
  38. package/js/Private/extension/IAudioDeviceManagerExtension.js +2 -0
  39. package/js/Private/impl/AgoraBaseImpl.js +23 -0
  40. package/js/Private/impl/AgoraMediaBaseImpl.js +149 -0
  41. package/js/Private/impl/IAgoraH265TranscoderImpl.js +116 -0
  42. package/js/Private/impl/IAgoraMediaEngineImpl.js +337 -0
  43. package/js/Private/impl/IAgoraMediaPlayerImpl.js +950 -0
  44. package/js/Private/impl/IAgoraMediaPlayerSourceImpl.js +73 -0
  45. package/js/Private/impl/IAgoraMediaRecorderImpl.js +47 -0
  46. package/js/Private/impl/IAgoraMusicContentCenterImpl.js +422 -0
  47. package/js/Private/impl/IAgoraRtcEngineExImpl.js +1042 -0
  48. package/js/Private/impl/IAgoraRtcEngineImpl.js +5167 -0
  49. package/js/Private/impl/IAgoraSpatialAudioImpl.js +358 -0
  50. package/js/Private/impl/IAudioDeviceManagerImpl.js +375 -0
  51. package/js/Private/internal/AgoraH265TranscoderInternal.js +62 -0
  52. package/js/Private/internal/AgoraMediaBaseInternal.js +15 -0
  53. package/js/Private/internal/AudioDeviceManagerInternal.js +44 -0
  54. package/js/Private/internal/IrisApiEngine.js +424 -0
  55. package/js/Private/internal/LocalSpatialAudioEngineInternal.js +7 -0
  56. package/js/Private/internal/MediaEngineInternal.js +122 -0
  57. package/js/Private/internal/MediaPlayerInternal.js +194 -0
  58. package/js/Private/internal/MediaRecorderInternal.js +70 -0
  59. package/js/Private/internal/MusicContentCenterInternal.js +139 -0
  60. package/js/Private/internal/RtcEngineExInternal.js +430 -0
  61. package/js/Private/ipc/main.js +21 -0
  62. package/js/Private/ipc/renderer.js +21 -0
  63. package/js/Private/ti/AgoraBase-ti.js +40 -0
  64. package/js/Private/ti/AgoraMediaBase-ti.js +75 -0
  65. package/js/Private/ti/AgoraMediaPlayerTypes-ti.js +5 -0
  66. package/js/Private/ti/IAgoraH265Transcoder-ti.js +40 -0
  67. package/js/Private/ti/IAgoraLog-ti.js +5 -0
  68. package/js/Private/ti/IAgoraMediaEngine-ti.js +5 -0
  69. package/js/Private/ti/IAgoraMediaPlayer-ti.js +38 -0
  70. package/js/Private/ti/IAgoraMediaPlayerSource-ti.js +50 -0
  71. package/js/Private/ti/IAgoraMediaRecorder-ti.js +5 -0
  72. package/js/Private/ti/IAgoraMediaStreamingSource-ti.js +5 -0
  73. package/js/Private/ti/IAgoraMusicContentCenter-ti.js +42 -0
  74. package/js/Private/ti/IAgoraRhythmPlayer-ti.js +5 -0
  75. package/js/Private/ti/IAgoraRtcEngine-ti.js +141 -0
  76. package/js/Private/ti/IAgoraRtcEngineEx-ti.js +5 -0
  77. package/js/Private/ti/IAgoraSpatialAudio-ti.js +5 -0
  78. package/js/Private/ti/IAudioDeviceManager-ti.js +5 -0
  79. package/js/Renderer/AgoraView.js +161 -0
  80. package/js/Renderer/CapabilityManager.js +99 -0
  81. package/js/Renderer/IRenderer.js +136 -0
  82. package/js/Renderer/IRendererCache.js +75 -0
  83. package/js/Renderer/RendererCache.js +100 -0
  84. package/js/Renderer/RendererManager.js +301 -0
  85. package/js/Renderer/WebCodecsRenderer/index.js +109 -0
  86. package/js/Renderer/WebCodecsRendererCache.js +115 -0
  87. package/js/Renderer/WebGLRenderer/index.js +314 -0
  88. package/js/Renderer/WebGLRenderer/webgl-utils.js +1337 -0
  89. package/js/Renderer/YUVCanvasRenderer/index.js +46 -0
  90. package/js/Renderer/index.js +17 -0
  91. package/js/Types.js +67 -0
  92. package/js/Utils.js +187 -0
  93. package/package.json +167 -0
  94. package/scripts/bootstrap.js +29 -0
  95. package/scripts/build.js +37 -0
  96. package/scripts/buildJS.js +14 -0
  97. package/scripts/checkElectron.js +43 -0
  98. package/scripts/clean.js +16 -0
  99. package/scripts/download.js +105 -0
  100. package/scripts/downloadPrebuild.js +199 -0
  101. package/scripts/getConfig.js +73 -0
  102. package/scripts/logger.js +32 -0
  103. package/scripts/synclib.js +69 -0
  104. package/scripts/util.js +47 -0
  105. package/scripts/zipBuild.js +40 -0
  106. package/ts/AgoraSdk.ts +54 -0
  107. package/ts/Decoder/gpu-utils.ts +92 -0
  108. package/ts/Decoder/index.ts +206 -0
  109. package/ts/Private/AgoraBase.ts +5511 -0
  110. package/ts/Private/AgoraMediaBase.ts +1764 -0
  111. package/ts/Private/AgoraMediaPlayerTypes.ts +464 -0
  112. package/ts/Private/IAgoraH265Transcoder.ts +73 -0
  113. package/ts/Private/IAgoraLog.ts +91 -0
  114. package/ts/Private/IAgoraMediaEngine.ts +316 -0
  115. package/ts/Private/IAgoraMediaPlayer.ts +744 -0
  116. package/ts/Private/IAgoraMediaPlayerSource.ts +137 -0
  117. package/ts/Private/IAgoraMediaRecorder.ts +25 -0
  118. package/ts/Private/IAgoraMediaStreamingSource.ts +81 -0
  119. package/ts/Private/IAgoraMusicContentCenter.ts +455 -0
  120. package/ts/Private/IAgoraRhythmPlayer.ts +67 -0
  121. package/ts/Private/IAgoraRtcEngine.ts +7306 -0
  122. package/ts/Private/IAgoraRtcEngineEx.ts +992 -0
  123. package/ts/Private/IAgoraSpatialAudio.ts +250 -0
  124. package/ts/Private/IAudioDeviceManager.ts +342 -0
  125. package/ts/Private/extension/AgoraBaseExtension.ts +14 -0
  126. package/ts/Private/extension/AgoraMediaBaseExtension.ts +1 -0
  127. package/ts/Private/extension/AgoraMediaPlayerTypesExtension.ts +1 -0
  128. package/ts/Private/extension/IAgoraH265TranscoderExtension.ts +39 -0
  129. package/ts/Private/extension/IAgoraLogExtension.ts +1 -0
  130. package/ts/Private/extension/IAgoraMediaEngineExtension.ts +62 -0
  131. package/ts/Private/extension/IAgoraMediaPlayerExtension.ts +64 -0
  132. package/ts/Private/extension/IAgoraMediaPlayerSourceExtension.ts +1 -0
  133. package/ts/Private/extension/IAgoraMediaRecorderExtension.ts +59 -0
  134. package/ts/Private/extension/IAgoraMusicContentCenterExtension.ts +34 -0
  135. package/ts/Private/extension/IAgoraRhythmPlayerExtension.ts +1 -0
  136. package/ts/Private/extension/IAgoraRtcEngineExExtension.ts +1 -0
  137. package/ts/Private/extension/IAgoraRtcEngineExtension.ts +64 -0
  138. package/ts/Private/extension/IAgoraSpatialAudioExtension.ts +1 -0
  139. package/ts/Private/extension/IAudioDeviceManagerExtension.ts +1 -0
  140. package/ts/Private/impl/AgoraBaseImpl.ts +39 -0
  141. package/ts/Private/impl/AgoraMediaBaseImpl.ts +237 -0
  142. package/ts/Private/impl/IAgoraH265TranscoderImpl.ts +152 -0
  143. package/ts/Private/impl/IAgoraMediaEngineImpl.ts +491 -0
  144. package/ts/Private/impl/IAgoraMediaPlayerImpl.ts +1169 -0
  145. package/ts/Private/impl/IAgoraMediaPlayerSourceImpl.ts +94 -0
  146. package/ts/Private/impl/IAgoraMediaRecorderImpl.ts +59 -0
  147. package/ts/Private/impl/IAgoraMusicContentCenterImpl.ts +562 -0
  148. package/ts/Private/impl/IAgoraRtcEngineExImpl.ts +1672 -0
  149. package/ts/Private/impl/IAgoraRtcEngineImpl.ts +7352 -0
  150. package/ts/Private/impl/IAgoraSpatialAudioImpl.ts +495 -0
  151. package/ts/Private/impl/IAudioDeviceManagerImpl.ts +443 -0
  152. package/ts/Private/internal/AgoraH265TranscoderInternal.ts +97 -0
  153. package/ts/Private/internal/AgoraMediaBaseInternal.ts +15 -0
  154. package/ts/Private/internal/AudioDeviceManagerInternal.ts +46 -0
  155. package/ts/Private/internal/IrisApiEngine.ts +598 -0
  156. package/ts/Private/internal/LocalSpatialAudioEngineInternal.ts +3 -0
  157. package/ts/Private/internal/MediaEngineInternal.ts +190 -0
  158. package/ts/Private/internal/MediaPlayerInternal.ts +311 -0
  159. package/ts/Private/internal/MediaRecorderInternal.ts +100 -0
  160. package/ts/Private/internal/MusicContentCenterInternal.ts +208 -0
  161. package/ts/Private/internal/RtcEngineExInternal.ts +647 -0
  162. package/ts/Private/ipc/main.ts +22 -0
  163. package/ts/Private/ipc/renderer.ts +21 -0
  164. package/ts/Private/ti/AgoraBase-ti.ts +16 -0
  165. package/ts/Private/ti/AgoraMediaBase-ti.ts +58 -0
  166. package/ts/Private/ti/AgoraMediaPlayerTypes-ti.ts +11 -0
  167. package/ts/Private/ti/IAgoraH265Transcoder-ti.ts +16 -0
  168. package/ts/Private/ti/IAgoraLog-ti.ts +11 -0
  169. package/ts/Private/ti/IAgoraMediaEngine-ti.ts +11 -0
  170. package/ts/Private/ti/IAgoraMediaPlayer-ti.ts +14 -0
  171. package/ts/Private/ti/IAgoraMediaPlayerSource-ti.ts +26 -0
  172. package/ts/Private/ti/IAgoraMediaRecorder-ti.ts +11 -0
  173. package/ts/Private/ti/IAgoraMediaStreamingSource-ti.ts +11 -0
  174. package/ts/Private/ti/IAgoraMusicContentCenter-ti.ts +18 -0
  175. package/ts/Private/ti/IAgoraRhythmPlayer-ti.ts +11 -0
  176. package/ts/Private/ti/IAgoraRtcEngine-ti.ts +119 -0
  177. package/ts/Private/ti/IAgoraRtcEngineEx-ti.ts +11 -0
  178. package/ts/Private/ti/IAgoraSpatialAudio-ti.ts +11 -0
  179. package/ts/Private/ti/IAudioDeviceManager-ti.ts +11 -0
  180. package/ts/Renderer/AgoraView.ts +235 -0
  181. package/ts/Renderer/CapabilityManager.ts +126 -0
  182. package/ts/Renderer/IRenderer.ts +158 -0
  183. package/ts/Renderer/IRendererCache.ts +96 -0
  184. package/ts/Renderer/RendererCache.ts +124 -0
  185. package/ts/Renderer/RendererManager.ts +400 -0
  186. package/ts/Renderer/WebCodecsRenderer/index.ts +145 -0
  187. package/ts/Renderer/WebCodecsRendererCache.ts +137 -0
  188. package/ts/Renderer/WebGLRenderer/index.ts +489 -0
  189. package/ts/Renderer/WebGLRenderer/webgl-utils.js +1510 -0
  190. package/ts/Renderer/YUVCanvasRenderer/index.ts +67 -0
  191. package/ts/Renderer/index.ts +1 -0
  192. package/ts/Types.ts +249 -0
  193. package/ts/Utils.ts +197 -0
  194. package/types/AgoraSdk.d.ts +40 -0
  195. package/types/AgoraSdk.d.ts.map +1 -0
  196. package/types/Decoder/gpu-utils.d.ts +21 -0
  197. package/types/Decoder/gpu-utils.d.ts.map +1 -0
  198. package/types/Decoder/index.d.ts +26 -0
  199. package/types/Decoder/index.d.ts.map +1 -0
  200. package/types/Private/AgoraBase.d.ts +5310 -0
  201. package/types/Private/AgoraBase.d.ts.map +1 -0
  202. package/types/Private/AgoraMediaBase.d.ts +1668 -0
  203. package/types/Private/AgoraMediaBase.d.ts.map +1 -0
  204. package/types/Private/AgoraMediaPlayerTypes.d.ts +453 -0
  205. package/types/Private/AgoraMediaPlayerTypes.d.ts.map +1 -0
  206. package/types/Private/IAgoraH265Transcoder.d.ts +28 -0
  207. package/types/Private/IAgoraH265Transcoder.d.ts.map +1 -0
  208. package/types/Private/IAgoraLog.d.ts +89 -0
  209. package/types/Private/IAgoraLog.d.ts.map +1 -0
  210. package/types/Private/IAgoraMediaEngine.d.ts +250 -0
  211. package/types/Private/IAgoraMediaEngine.d.ts.map +1 -0
  212. package/types/Private/IAgoraMediaPlayer.d.ts +648 -0
  213. package/types/Private/IAgoraMediaPlayer.d.ts.map +1 -0
  214. package/types/Private/IAgoraMediaPlayerSource.d.ts +109 -0
  215. package/types/Private/IAgoraMediaPlayerSource.d.ts.map +1 -0
  216. package/types/Private/IAgoraMediaRecorder.d.ts +20 -0
  217. package/types/Private/IAgoraMediaRecorder.d.ts.map +1 -0
  218. package/types/Private/IAgoraMediaStreamingSource.d.ts +42 -0
  219. package/types/Private/IAgoraMediaStreamingSource.d.ts.map +1 -0
  220. package/types/Private/IAgoraMusicContentCenter.d.ts +381 -0
  221. package/types/Private/IAgoraMusicContentCenter.d.ts.map +1 -0
  222. package/types/Private/IAgoraRhythmPlayer.d.ts +65 -0
  223. package/types/Private/IAgoraRhythmPlayer.d.ts.map +1 -0
  224. package/types/Private/IAgoraRtcEngine.d.ts +6103 -0
  225. package/types/Private/IAgoraRtcEngine.d.ts.map +1 -0
  226. package/types/Private/IAgoraRtcEngineEx.d.ts +741 -0
  227. package/types/Private/IAgoraRtcEngineEx.d.ts.map +1 -0
  228. package/types/Private/IAgoraSpatialAudio.d.ts +196 -0
  229. package/types/Private/IAgoraSpatialAudio.d.ts.map +1 -0
  230. package/types/Private/IAudioDeviceManager.d.ts +312 -0
  231. package/types/Private/IAudioDeviceManager.d.ts.map +1 -0
  232. package/types/Private/extension/AgoraBaseExtension.d.ts +14 -0
  233. package/types/Private/extension/AgoraBaseExtension.d.ts.map +1 -0
  234. package/types/Private/extension/AgoraMediaBaseExtension.d.ts +2 -0
  235. package/types/Private/extension/AgoraMediaBaseExtension.d.ts.map +1 -0
  236. package/types/Private/extension/AgoraMediaPlayerTypesExtension.d.ts +2 -0
  237. package/types/Private/extension/AgoraMediaPlayerTypesExtension.d.ts.map +1 -0
  238. package/types/Private/extension/IAgoraH265TranscoderExtension.d.ts +24 -0
  239. package/types/Private/extension/IAgoraH265TranscoderExtension.d.ts.map +1 -0
  240. package/types/Private/extension/IAgoraLogExtension.d.ts +2 -0
  241. package/types/Private/extension/IAgoraLogExtension.d.ts.map +1 -0
  242. package/types/Private/extension/IAgoraMediaEngineExtension.d.ts +40 -0
  243. package/types/Private/extension/IAgoraMediaEngineExtension.d.ts.map +1 -0
  244. package/types/Private/extension/IAgoraMediaPlayerExtension.d.ts +46 -0
  245. package/types/Private/extension/IAgoraMediaPlayerExtension.d.ts.map +1 -0
  246. package/types/Private/extension/IAgoraMediaPlayerSourceExtension.d.ts +2 -0
  247. package/types/Private/extension/IAgoraMediaPlayerSourceExtension.d.ts.map +1 -0
  248. package/types/Private/extension/IAgoraMediaRecorderExtension.d.ts +44 -0
  249. package/types/Private/extension/IAgoraMediaRecorderExtension.d.ts.map +1 -0
  250. package/types/Private/extension/IAgoraMusicContentCenterExtension.d.ts +20 -0
  251. package/types/Private/extension/IAgoraMusicContentCenterExtension.d.ts.map +1 -0
  252. package/types/Private/extension/IAgoraRhythmPlayerExtension.d.ts +2 -0
  253. package/types/Private/extension/IAgoraRhythmPlayerExtension.d.ts.map +1 -0
  254. package/types/Private/extension/IAgoraRtcEngineExExtension.d.ts +2 -0
  255. package/types/Private/extension/IAgoraRtcEngineExExtension.d.ts.map +1 -0
  256. package/types/Private/extension/IAgoraRtcEngineExtension.d.ts +42 -0
  257. package/types/Private/extension/IAgoraRtcEngineExtension.d.ts.map +1 -0
  258. package/types/Private/extension/IAgoraSpatialAudioExtension.d.ts +2 -0
  259. package/types/Private/extension/IAgoraSpatialAudioExtension.d.ts.map +1 -0
  260. package/types/Private/extension/IAudioDeviceManagerExtension.d.ts +2 -0
  261. package/types/Private/extension/IAudioDeviceManagerExtension.d.ts.map +1 -0
  262. package/types/Private/impl/AgoraBaseImpl.d.ts +3 -0
  263. package/types/Private/impl/AgoraBaseImpl.d.ts.map +1 -0
  264. package/types/Private/impl/AgoraMediaBaseImpl.d.ts +14 -0
  265. package/types/Private/impl/AgoraMediaBaseImpl.d.ts.map +1 -0
  266. package/types/Private/impl/IAgoraH265TranscoderImpl.d.ts +15 -0
  267. package/types/Private/impl/IAgoraH265TranscoderImpl.d.ts.map +1 -0
  268. package/types/Private/impl/IAgoraMediaEngineImpl.d.ts +46 -0
  269. package/types/Private/impl/IAgoraMediaEngineImpl.d.ts.map +1 -0
  270. package/types/Private/impl/IAgoraMediaPlayerImpl.d.ts +143 -0
  271. package/types/Private/impl/IAgoraMediaPlayerImpl.d.ts.map +1 -0
  272. package/types/Private/impl/IAgoraMediaPlayerSourceImpl.d.ts +3 -0
  273. package/types/Private/impl/IAgoraMediaPlayerSourceImpl.d.ts.map +1 -0
  274. package/types/Private/impl/IAgoraMediaRecorderImpl.d.ts +11 -0
  275. package/types/Private/impl/IAgoraMediaRecorderImpl.d.ts.map +1 -0
  276. package/types/Private/impl/IAgoraMusicContentCenterImpl.d.ts +67 -0
  277. package/types/Private/impl/IAgoraMusicContentCenterImpl.d.ts.map +1 -0
  278. package/types/Private/impl/IAgoraRtcEngineExImpl.d.ts +116 -0
  279. package/types/Private/impl/IAgoraRtcEngineExImpl.d.ts.map +1 -0
  280. package/types/Private/impl/IAgoraRtcEngineImpl.d.ts +628 -0
  281. package/types/Private/impl/IAgoraRtcEngineImpl.d.ts.map +1 -0
  282. package/types/Private/impl/IAgoraSpatialAudioImpl.d.ts +47 -0
  283. package/types/Private/impl/IAgoraSpatialAudioImpl.d.ts.map +1 -0
  284. package/types/Private/impl/IAudioDeviceManagerImpl.d.ts +65 -0
  285. package/types/Private/impl/IAudioDeviceManagerImpl.d.ts.map +1 -0
  286. package/types/Private/internal/AgoraH265TranscoderInternal.d.ts +14 -0
  287. package/types/Private/internal/AgoraH265TranscoderInternal.d.ts.map +1 -0
  288. package/types/Private/internal/AgoraMediaBaseInternal.d.ts +8 -0
  289. package/types/Private/internal/AgoraMediaBaseInternal.d.ts.map +1 -0
  290. package/types/Private/internal/AudioDeviceManagerInternal.d.ts +9 -0
  291. package/types/Private/internal/AudioDeviceManagerInternal.d.ts.map +1 -0
  292. package/types/Private/internal/IrisApiEngine.d.ts +69 -0
  293. package/types/Private/internal/IrisApiEngine.d.ts.map +1 -0
  294. package/types/Private/internal/LocalSpatialAudioEngineInternal.d.ts +4 -0
  295. package/types/Private/internal/LocalSpatialAudioEngineInternal.d.ts.map +1 -0
  296. package/types/Private/internal/MediaEngineInternal.d.ts +23 -0
  297. package/types/Private/internal/MediaEngineInternal.d.ts.map +1 -0
  298. package/types/Private/internal/MediaPlayerInternal.d.ts +30 -0
  299. package/types/Private/internal/MediaPlayerInternal.d.ts.map +1 -0
  300. package/types/Private/internal/MediaRecorderInternal.d.ts +16 -0
  301. package/types/Private/internal/MediaRecorderInternal.d.ts.map +1 -0
  302. package/types/Private/internal/MusicContentCenterInternal.d.ts +31 -0
  303. package/types/Private/internal/MusicContentCenterInternal.d.ts.map +1 -0
  304. package/types/Private/internal/RtcEngineExInternal.d.ts +73 -0
  305. package/types/Private/internal/RtcEngineExInternal.d.ts.map +1 -0
  306. package/types/Private/ipc/main.d.ts +2 -0
  307. package/types/Private/ipc/main.d.ts.map +1 -0
  308. package/types/Private/ipc/renderer.d.ts +3 -0
  309. package/types/Private/ipc/renderer.d.ts.map +1 -0
  310. package/types/Private/ti/AgoraBase-ti.d.ts +8 -0
  311. package/types/Private/ti/AgoraBase-ti.d.ts.map +1 -0
  312. package/types/Private/ti/AgoraMediaBase-ti.d.ts +15 -0
  313. package/types/Private/ti/AgoraMediaBase-ti.d.ts.map +1 -0
  314. package/types/Private/ti/AgoraMediaPlayerTypes-ti.d.ts +7 -0
  315. package/types/Private/ti/AgoraMediaPlayerTypes-ti.d.ts.map +1 -0
  316. package/types/Private/ti/IAgoraH265Transcoder-ti.d.ts +8 -0
  317. package/types/Private/ti/IAgoraH265Transcoder-ti.d.ts.map +1 -0
  318. package/types/Private/ti/IAgoraLog-ti.d.ts +7 -0
  319. package/types/Private/ti/IAgoraLog-ti.d.ts.map +1 -0
  320. package/types/Private/ti/IAgoraMediaEngine-ti.d.ts +7 -0
  321. package/types/Private/ti/IAgoraMediaEngine-ti.d.ts.map +1 -0
  322. package/types/Private/ti/IAgoraMediaPlayer-ti.d.ts +8 -0
  323. package/types/Private/ti/IAgoraMediaPlayer-ti.d.ts.map +1 -0
  324. package/types/Private/ti/IAgoraMediaPlayerSource-ti.d.ts +8 -0
  325. package/types/Private/ti/IAgoraMediaPlayerSource-ti.d.ts.map +1 -0
  326. package/types/Private/ti/IAgoraMediaRecorder-ti.d.ts +7 -0
  327. package/types/Private/ti/IAgoraMediaRecorder-ti.d.ts.map +1 -0
  328. package/types/Private/ti/IAgoraMediaStreamingSource-ti.d.ts +7 -0
  329. package/types/Private/ti/IAgoraMediaStreamingSource-ti.d.ts.map +1 -0
  330. package/types/Private/ti/IAgoraMusicContentCenter-ti.d.ts +8 -0
  331. package/types/Private/ti/IAgoraMusicContentCenter-ti.d.ts.map +1 -0
  332. package/types/Private/ti/IAgoraRhythmPlayer-ti.d.ts +7 -0
  333. package/types/Private/ti/IAgoraRhythmPlayer-ti.d.ts.map +1 -0
  334. package/types/Private/ti/IAgoraRtcEngine-ti.d.ts +10 -0
  335. package/types/Private/ti/IAgoraRtcEngine-ti.d.ts.map +1 -0
  336. package/types/Private/ti/IAgoraRtcEngineEx-ti.d.ts +7 -0
  337. package/types/Private/ti/IAgoraRtcEngineEx-ti.d.ts.map +1 -0
  338. package/types/Private/ti/IAgoraSpatialAudio-ti.d.ts +7 -0
  339. package/types/Private/ti/IAgoraSpatialAudio-ti.d.ts.map +1 -0
  340. package/types/Private/ti/IAudioDeviceManager-ti.d.ts +7 -0
  341. package/types/Private/ti/IAudioDeviceManager-ti.d.ts.map +1 -0
  342. package/types/Renderer/AgoraView.d.ts +76 -0
  343. package/types/Renderer/AgoraView.d.ts.map +1 -0
  344. package/types/Renderer/CapabilityManager.d.ts +20 -0
  345. package/types/Renderer/CapabilityManager.d.ts.map +1 -0
  346. package/types/Renderer/IRenderer.d.ts +20 -0
  347. package/types/Renderer/IRenderer.d.ts.map +1 -0
  348. package/types/Renderer/IRendererCache.d.ts +26 -0
  349. package/types/Renderer/IRendererCache.d.ts.map +1 -0
  350. package/types/Renderer/RendererCache.d.ts +24 -0
  351. package/types/Renderer/RendererCache.d.ts.map +1 -0
  352. package/types/Renderer/RendererManager.d.ts +66 -0
  353. package/types/Renderer/RendererManager.d.ts.map +1 -0
  354. package/types/Renderer/WebCodecsRenderer/index.d.ts +14 -0
  355. package/types/Renderer/WebCodecsRenderer/index.d.ts.map +1 -0
  356. package/types/Renderer/WebCodecsRendererCache.d.ts +15 -0
  357. package/types/Renderer/WebCodecsRendererCache.d.ts.map +1 -0
  358. package/types/Renderer/WebGLRenderer/index.d.ts +27 -0
  359. package/types/Renderer/WebGLRenderer/index.d.ts.map +1 -0
  360. package/types/Renderer/WebGLRenderer/webgl-utils.d.ts +1 -0
  361. package/types/Renderer/WebGLRenderer/webgl-utils.d.ts.map +1 -0
  362. package/types/Renderer/YUVCanvasRenderer/index.d.ts +9 -0
  363. package/types/Renderer/YUVCanvasRenderer/index.d.ts.map +1 -0
  364. package/types/Renderer/index.d.ts +2 -0
  365. package/types/Renderer/index.d.ts.map +1 -0
  366. package/types/Types.d.ts +177 -0
  367. package/types/Types.d.ts.map +1 -0
  368. package/types/Utils.d.ts +50 -0
  369. package/types/Utils.d.ts.map +1 -0
@@ -0,0 +1,1337 @@
1
+ /*
2
+ * Copyright 2012, Gregg Tavares.
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are
7
+ * met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above
12
+ * copyright notice, this list of conditions and the following disclaimer
13
+ * in the documentation and/or other materials provided with the
14
+ * distribution.
15
+ * * Neither the name of Gregg Tavares. nor the names of his
16
+ * contributors may be used to endorse or promote products derived from
17
+ * this software without specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+ 'use strict';
32
+ (function (root, factory) {
33
+ if (typeof define === 'function' && define.amd) {
34
+ // AMD. Register as an anonymous module.
35
+ define([], factory);
36
+ }
37
+ else {
38
+ // Browser globals
39
+ var lib = factory.call(root);
40
+ Object.keys(lib).forEach(function (key) {
41
+ root[key] = lib[key];
42
+ });
43
+ }
44
+ })(this, function () {
45
+ var topWindow = this;
46
+ /** @module webgl-utils */
47
+ /**
48
+ * Wrapped logging function.
49
+ * @param {string} msg The message to log.
50
+ */
51
+ function error(msg) {
52
+ if (topWindow.console) {
53
+ if (topWindow.console.error) {
54
+ topWindow.console.error(msg);
55
+ }
56
+ else if (topWindow.console.log) {
57
+ topWindow.console.log(msg);
58
+ }
59
+ }
60
+ }
61
+ /**
62
+ * Check if the page is embedded.
63
+ * @param {Window?) w window to check
64
+ * @return {boolean} True of we are in an iframe
65
+ */
66
+ function isInIFrame(w) {
67
+ w = w || topWindow;
68
+ return w !== w.top;
69
+ }
70
+ /**
71
+ * Creates the HTLM for a failure message
72
+ * @param {string} canvasContainerId id of container of th
73
+ * canvas.
74
+ * @return {string} The html.
75
+ */
76
+ function makeFailHTML(msg) {
77
+ return ('' +
78
+ '<table style="background-color: #8CE; width: 100%; height: 100%;"><tr>' +
79
+ '<td align="center">' +
80
+ '<div style="display: table-cell; vertical-align: middle;">' +
81
+ '<div style="">' +
82
+ msg +
83
+ '</div>' +
84
+ '</div>' +
85
+ '</td></tr></table>');
86
+ }
87
+ /**
88
+ * Mesasge for getting a webgl browser
89
+ * @type {string}
90
+ */
91
+ var GET_A_WEBGL_BROWSER = '' +
92
+ 'This page requires a browser that supports WebGL.<br/>' +
93
+ '<a href="http://get.webgl.org">Click here to upgrade your browser.</a>';
94
+ /**
95
+ * Mesasge for need better hardware
96
+ * @type {string}
97
+ */
98
+ var OTHER_PROBLEM = '' +
99
+ "It doesn't appear your computer can support WebGL.<br/>" +
100
+ '<a href="http://get.webgl.org/troubleshooting/">Click here for more information.</a>';
101
+ /**
102
+ * Creates a webgl context.
103
+ * @param {HTMLCanvasElement} canvas The canvas tag to get
104
+ * context from. If one is not passed in one will be
105
+ * created.
106
+ * @return {WebGLRenderingContext} The created context.
107
+ */
108
+ function create3DContext(canvas, opt_attribs) {
109
+ var names = ['webgl', 'experimental-webgl'];
110
+ var context = null;
111
+ for (var ii = 0; ii < names.length; ++ii) {
112
+ try {
113
+ context = canvas.getContext(names[ii], opt_attribs);
114
+ }
115
+ catch (e) { } // eslint-disable-line
116
+ if (context) {
117
+ break;
118
+ }
119
+ }
120
+ return context;
121
+ }
122
+ /**
123
+ * Creates a webgl context. If creation fails it will
124
+ * change the contents of the container of the <canvas>
125
+ * tag to an error message with the correct links for WebGL.
126
+ * @param {HTMLCanvasElement} canvas. The canvas element to
127
+ * create a context from.
128
+ * @param {WebGLContextCreationAttirbutes} opt_attribs Any
129
+ * creation attributes you want to pass in.
130
+ * @return {WebGLRenderingContext} The created context.
131
+ * @memberOf module:webgl-utils
132
+ */
133
+ function setupWebGL(canvas, opt_attribs) {
134
+ function showLink(str) {
135
+ var container = canvas.parentNode;
136
+ if (container) {
137
+ container.innerHTML = makeFailHTML(str);
138
+ }
139
+ }
140
+ if (!topWindow.WebGLRenderingContext) {
141
+ showLink(GET_A_WEBGL_BROWSER);
142
+ return null;
143
+ }
144
+ var context = create3DContext(canvas, opt_attribs);
145
+ if (!context) {
146
+ showLink(OTHER_PROBLEM);
147
+ }
148
+ return context;
149
+ }
150
+ function updateCSSIfInIFrame() {
151
+ if (isInIFrame()) {
152
+ document.body.className = 'iframe';
153
+ }
154
+ }
155
+ /**
156
+ * @typedef {Object} GetWebGLContextOptions
157
+ * @property {boolean} [dontResize] by default `getWebGLContext` will resize the canvas to match the size it's displayed.
158
+ * @property {boolean} [noTitle] by default inserts a copy of the `<title>` content into the page
159
+ * @memberOf module:webgl-utils
160
+ */
161
+ /**
162
+ * Gets a WebGL context.
163
+ * makes its backing store the size it is displayed.
164
+ * @param {HTMLCanvasElement} canvas a canvas element.
165
+ * @param {WebGLContextCreationAttirbutes} [opt_attribs] optional webgl context creation attributes
166
+ * @param {module:webgl-utils.GetWebGLContextOptions} [opt_options] options
167
+ * @memberOf module:webgl-utils
168
+ */
169
+ function getWebGLContext(canvas, opt_attribs, opt_options) {
170
+ var options = opt_options || {};
171
+ if (isInIFrame()) {
172
+ updateCSSIfInIFrame();
173
+ // make the canvas backing store the size it's displayed.
174
+ if (!options.dontResize && options.resize !== false) {
175
+ var width = canvas.clientWidth;
176
+ var height = canvas.clientHeight;
177
+ canvas.width = width;
178
+ canvas.height = height;
179
+ }
180
+ }
181
+ else if (!options.noTitle && options.title !== false) {
182
+ var title = document.title;
183
+ var h1 = document.createElement('h1');
184
+ h1.innerText = title;
185
+ document.body.insertBefore(h1, document.body.children[0]);
186
+ }
187
+ var gl = setupWebGL(canvas, opt_attribs);
188
+ return gl;
189
+ }
190
+ /**
191
+ * Error Callback
192
+ * @callback ErrorCallback
193
+ * @param {string} msg error message.
194
+ * @memberOf module:webgl-utils
195
+ */
196
+ /**
197
+ * Loads a shader.
198
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
199
+ * @param {string} shaderSource The shader source.
200
+ * @param {number} shaderType The type of shader.
201
+ * @param {module:webgl-utils.ErrorCallback} opt_errorCallback callback for errors.
202
+ * @return {WebGLShader} The created shader.
203
+ */
204
+ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
205
+ var errFn = opt_errorCallback || error;
206
+ // Create the shader object
207
+ var shader = gl.createShader(shaderType);
208
+ // Load the shader source
209
+ gl.shaderSource(shader, shaderSource);
210
+ // Compile the shader
211
+ gl.compileShader(shader);
212
+ // Check the compile status
213
+ var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
214
+ if (!compiled) {
215
+ // Something went wrong during compilation; get the error
216
+ var lastError = gl.getShaderInfoLog(shader);
217
+ errFn("*** Error compiling shader '" + shader + "':" + lastError);
218
+ gl.deleteShader(shader);
219
+ return null;
220
+ }
221
+ return shader;
222
+ }
223
+ /**
224
+ * Creates a program, attaches shaders, binds attrib locations, links the
225
+ * program and calls useProgram.
226
+ * @param {WebGLShader[]} shaders The shaders to attach
227
+ * @param {string[]} [opt_attribs] An array of attribs names. Locations will be assigned by index if not passed in
228
+ * @param {number[]} [opt_locations] The locations for the. A parallel array to opt_attribs letting you assign locations.
229
+ * @param {module:webgl-utils.ErrorCallback} opt_errorCallback callback for errors. By default it just prints an error to the console
230
+ * on error. If you want something else pass an callback. It's passed an error message.
231
+ * @memberOf module:webgl-utils
232
+ */
233
+ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallback) {
234
+ var errFn = opt_errorCallback || error;
235
+ var program = gl.createProgram();
236
+ shaders.forEach(function (shader) {
237
+ gl.attachShader(program, shader);
238
+ });
239
+ if (opt_attribs) {
240
+ obj_attrib.forEach(function (attrib, ndx) {
241
+ gl.bindAttribLocation(program, opt_locations ? opt_locations[ndx] : ndx, attrib);
242
+ });
243
+ }
244
+ gl.linkProgram(program);
245
+ // Check the link status
246
+ var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
247
+ if (!linked) {
248
+ // something went wrong with the link
249
+ var lastError = gl.getProgramInfoLog(program);
250
+ errFn('Error in program linking:' + lastError);
251
+ gl.deleteProgram(program);
252
+ return null;
253
+ }
254
+ return program;
255
+ }
256
+ /**
257
+ * Loads a shader from a script tag.
258
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
259
+ * @param {string} scriptId The id of the script tag.
260
+ * @param {number} opt_shaderType The type of shader. If not passed in it will
261
+ * be derived from the type of the script tag.
262
+ * @param {module:webgl-utils.ErrorCallback} opt_errorCallback callback for errors.
263
+ * @return {WebGLShader} The created shader.
264
+ */
265
+ function createShaderFromScript(gl, scriptId, opt_shaderType, opt_errorCallback) {
266
+ var shaderSource = '';
267
+ var shaderType;
268
+ var shaderScript = document.getElementById(scriptId);
269
+ if (!shaderScript) {
270
+ throw '*** Error: unknown script element' + scriptId;
271
+ }
272
+ shaderSource = shaderScript.text;
273
+ if (!opt_shaderType) {
274
+ if (shaderScript.type === 'x-shader/x-vertex') {
275
+ shaderType = gl.VERTEX_SHADER;
276
+ }
277
+ else if (shaderScript.type === 'x-shader/x-fragment') {
278
+ shaderType = gl.FRAGMENT_SHADER;
279
+ }
280
+ else if (shaderType !== gl.VERTEX_SHADER &&
281
+ shaderType !== gl.FRAGMENT_SHADER) {
282
+ throw '*** Error: unknown shader type';
283
+ }
284
+ }
285
+ return loadShader(gl, shaderSource, opt_shaderType ? opt_shaderType : shaderType, opt_errorCallback);
286
+ }
287
+ var defaultShaderType = ['VERTEX_SHADER', 'FRAGMENT_SHADER'];
288
+ /**
289
+ * Creates a program from 2 script tags.
290
+ *
291
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
292
+ * to use.
293
+ * @param {string[]} shaderScriptIds Array of ids of the script
294
+ * tags for the shaders. The first is assumed to be the
295
+ * vertex shader, the second the fragment shader.
296
+ * @param {string[]} [opt_attribs] An array of attribs names. Locations will be assigned by index if not passed in
297
+ * @param {number[]} [opt_locations] The locations for the. A parallel array to opt_attribs letting you assign locations.
298
+ * @param {module:webgl-utils.ErrorCallback} opt_errorCallback callback for errors. By default it just prints an error to the console
299
+ * on error. If you want something else pass an callback. It's passed an error message.
300
+ * @return {WebGLProgram} The created program.
301
+ * @memberOf module:webgl-utils
302
+ */
303
+ function createProgramFromScripts(gl, shaderScriptIds, opt_attribs, opt_locations, opt_errorCallback) {
304
+ var shaders = [];
305
+ for (var ii = 0; ii < shaderScriptIds.length; ++ii) {
306
+ shaders.push(createShaderFromScript(gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], opt_errorCallback));
307
+ }
308
+ return createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallback);
309
+ }
310
+ /**
311
+ * Creates a program from 2 sources.
312
+ *
313
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
314
+ * to use.
315
+ * @param {string[]} shaderSourcess Array of sources for the
316
+ * shaders. The first is assumed to be the vertex shader,
317
+ * the second the fragment shader.
318
+ * @param {string[]} [opt_attribs] An array of attribs names. Locations will be assigned by index if not passed in
319
+ * @param {number[]} [opt_locations] The locations for the. A parallel array to opt_attribs letting you assign locations.
320
+ * @param {module:webgl-utils.ErrorCallback} opt_errorCallback callback for errors. By default it just prints an error to the console
321
+ * on error. If you want something else pass an callback. It's passed an error message.
322
+ * @return {WebGLProgram} The created program.
323
+ * @memberOf module:webgl-utils
324
+ */
325
+ function createProgramFromSources(gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback) {
326
+ var shaders = [];
327
+ for (var ii = 0; ii < shaderSources.length; ++ii) {
328
+ shaders.push(loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]], opt_errorCallback));
329
+ }
330
+ return createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallback);
331
+ }
332
+ /**
333
+ * Returns the corresponding bind point for a given sampler type
334
+ */
335
+ function getBindPointForSamplerType(gl, type) {
336
+ if (type === gl.SAMPLER_2D)
337
+ return gl.TEXTURE_2D; // eslint-disable-line
338
+ if (type === gl.SAMPLER_CUBE)
339
+ return gl.TEXTURE_CUBE_MAP; // eslint-disable-line
340
+ }
341
+ /**
342
+ * @typedef {Object.<string, function>} Setters
343
+ */
344
+ /**
345
+ * Creates setter functions for all uniforms of a shader
346
+ * program.
347
+ *
348
+ * @see {@link module:webgl-utils.setUniforms}
349
+ *
350
+ * @param {WebGLProgram} program the program to create setters for.
351
+ * @returns {Object.<string, function>} an object with a setter by name for each uniform
352
+ * @memberOf module:webgl-utils
353
+ */
354
+ function createUniformSetters(gl, program) {
355
+ var textureUnit = 0;
356
+ /**
357
+ * Creates a setter for a uniform of the given program with it's
358
+ * location embedded in the setter.
359
+ * @param {WebGLProgram} program
360
+ * @param {WebGLUniformInfo} uniformInfo
361
+ * @returns {function} the created setter.
362
+ */
363
+ function createUniformSetter(program, uniformInfo) {
364
+ var location = gl.getUniformLocation(program, uniformInfo.name);
365
+ var type = uniformInfo.type;
366
+ // Check if this uniform is an array
367
+ var isArray = uniformInfo.size > 1 && uniformInfo.name.substr(-3) === '[0]';
368
+ if (type === gl.FLOAT && isArray) {
369
+ return function (v) {
370
+ gl.uniform1fv(location, v);
371
+ };
372
+ }
373
+ if (type === gl.FLOAT) {
374
+ return function (v) {
375
+ gl.uniform1f(location, v);
376
+ };
377
+ }
378
+ if (type === gl.FLOAT_VEC2) {
379
+ return function (v) {
380
+ gl.uniform2fv(location, v);
381
+ };
382
+ }
383
+ if (type === gl.FLOAT_VEC3) {
384
+ return function (v) {
385
+ gl.uniform3fv(location, v);
386
+ };
387
+ }
388
+ if (type === gl.FLOAT_VEC4) {
389
+ return function (v) {
390
+ gl.uniform4fv(location, v);
391
+ };
392
+ }
393
+ if (type === gl.INT && isArray) {
394
+ return function (v) {
395
+ gl.uniform1iv(location, v);
396
+ };
397
+ }
398
+ if (type === gl.INT) {
399
+ return function (v) {
400
+ gl.uniform1i(location, v);
401
+ };
402
+ }
403
+ if (type === gl.INT_VEC2) {
404
+ return function (v) {
405
+ gl.uniform2iv(location, v);
406
+ };
407
+ }
408
+ if (type === gl.INT_VEC3) {
409
+ return function (v) {
410
+ gl.uniform3iv(location, v);
411
+ };
412
+ }
413
+ if (type === gl.INT_VEC4) {
414
+ return function (v) {
415
+ gl.uniform4iv(location, v);
416
+ };
417
+ }
418
+ if (type === gl.BOOL) {
419
+ return function (v) {
420
+ gl.uniform1iv(location, v);
421
+ };
422
+ }
423
+ if (type === gl.BOOL_VEC2) {
424
+ return function (v) {
425
+ gl.uniform2iv(location, v);
426
+ };
427
+ }
428
+ if (type === gl.BOOL_VEC3) {
429
+ return function (v) {
430
+ gl.uniform3iv(location, v);
431
+ };
432
+ }
433
+ if (type === gl.BOOL_VEC4) {
434
+ return function (v) {
435
+ gl.uniform4iv(location, v);
436
+ };
437
+ }
438
+ if (type === gl.FLOAT_MAT2) {
439
+ return function (v) {
440
+ gl.uniformMatrix2fv(location, false, v);
441
+ };
442
+ }
443
+ if (type === gl.FLOAT_MAT3) {
444
+ return function (v) {
445
+ gl.uniformMatrix3fv(location, false, v);
446
+ };
447
+ }
448
+ if (type === gl.FLOAT_MAT4) {
449
+ return function (v) {
450
+ gl.uniformMatrix4fv(location, false, v);
451
+ };
452
+ }
453
+ if ((type === gl.SAMPLER_2D || type === gl.SAMPLER_CUBE) && isArray) {
454
+ var units = [];
455
+ for (var ii = 0; ii < info.size; ++ii) {
456
+ units.push(textureUnit++);
457
+ }
458
+ return (function (bindPoint, units) {
459
+ return function (textures) {
460
+ gl.uniform1iv(location, units);
461
+ textures.forEach(function (texture, index) {
462
+ gl.activeTexture(gl.TEXTURE0 + units[index]);
463
+ gl.bindTexture(bindPoint, texture);
464
+ });
465
+ };
466
+ })(getBindPointForSamplerType(gl, type), units);
467
+ }
468
+ if (type === gl.SAMPLER_2D || type === gl.SAMPLER_CUBE) {
469
+ return (function (bindPoint, unit) {
470
+ return function (texture) {
471
+ gl.uniform1i(location, unit);
472
+ gl.activeTexture(gl.TEXTURE0 + unit);
473
+ gl.bindTexture(bindPoint, texture);
474
+ };
475
+ })(getBindPointForSamplerType(gl, type), textureUnit++);
476
+ }
477
+ throw 'unknown type: 0x' + type.toString(16); // we should never get here.
478
+ }
479
+ var uniformSetters = {};
480
+ var numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
481
+ for (var ii = 0; ii < numUniforms; ++ii) {
482
+ var uniformInfo = gl.getActiveUniform(program, ii);
483
+ if (!uniformInfo) {
484
+ break;
485
+ }
486
+ var name = uniformInfo.name;
487
+ // remove the array suffix.
488
+ if (name.substr(-3) === '[0]') {
489
+ name = name.substr(0, name.length - 3);
490
+ }
491
+ var setter = createUniformSetter(program, uniformInfo);
492
+ uniformSetters[name] = setter;
493
+ }
494
+ return uniformSetters;
495
+ }
496
+ /**
497
+ * Set uniforms and binds related textures.
498
+ *
499
+ * example:
500
+ *
501
+ * var programInfo = createProgramInfo(
502
+ * gl, ["some-vs", "some-fs");
503
+ *
504
+ * var tex1 = gl.createTexture();
505
+ * var tex2 = gl.createTexture();
506
+ *
507
+ * ... assume we setup the textures with data ...
508
+ *
509
+ * var uniforms = {
510
+ * u_someSampler: tex1,
511
+ * u_someOtherSampler: tex2,
512
+ * u_someColor: [1,0,0,1],
513
+ * u_somePosition: [0,1,1],
514
+ * u_someMatrix: [
515
+ * 1,0,0,0,
516
+ * 0,1,0,0,
517
+ * 0,0,1,0,
518
+ * 0,0,0,0,
519
+ * ],
520
+ * };
521
+ *
522
+ * gl.useProgram(program);
523
+ *
524
+ * This will automatically bind the textures AND set the
525
+ * uniforms.
526
+ *
527
+ * setUniforms(programInfo.uniformSetters, uniforms);
528
+ *
529
+ * For the example above it is equivalent to
530
+ *
531
+ * var texUnit = 0;
532
+ * gl.activeTexture(gl.TEXTURE0 + texUnit);
533
+ * gl.bindTexture(gl.TEXTURE_2D, tex1);
534
+ * gl.uniform1i(u_someSamplerLocation, texUnit++);
535
+ * gl.activeTexture(gl.TEXTURE0 + texUnit);
536
+ * gl.bindTexture(gl.TEXTURE_2D, tex2);
537
+ * gl.uniform1i(u_someSamplerLocation, texUnit++);
538
+ * gl.uniform4fv(u_someColorLocation, [1, 0, 0, 1]);
539
+ * gl.uniform3fv(u_somePositionLocation, [0, 1, 1]);
540
+ * gl.uniformMatrix4fv(u_someMatrix, false, [
541
+ * 1,0,0,0,
542
+ * 0,1,0,0,
543
+ * 0,0,1,0,
544
+ * 0,0,0,0,
545
+ * ]);
546
+ *
547
+ * Note it is perfectly reasonable to call `setUniforms` multiple times. For example
548
+ *
549
+ * var uniforms = {
550
+ * u_someSampler: tex1,
551
+ * u_someOtherSampler: tex2,
552
+ * };
553
+ *
554
+ * var moreUniforms {
555
+ * u_someColor: [1,0,0,1],
556
+ * u_somePosition: [0,1,1],
557
+ * u_someMatrix: [
558
+ * 1,0,0,0,
559
+ * 0,1,0,0,
560
+ * 0,0,1,0,
561
+ * 0,0,0,0,
562
+ * ],
563
+ * };
564
+ *
565
+ * setUniforms(programInfo.uniformSetters, uniforms);
566
+ * setUniforms(programInfo.uniformSetters, moreUniforms);
567
+ *
568
+ * @param {Object.<string, function>} setters the setters returned from
569
+ * `createUniformSetters`.
570
+ * @param {Object.<string, value>} an object with values for the
571
+ * uniforms.
572
+ * @memberOf module:webgl-utils
573
+ */
574
+ function setUniforms(setters, values) {
575
+ Object.keys(values).forEach(function (name) {
576
+ var setter = setters[name];
577
+ if (setter) {
578
+ setter(values[name]);
579
+ }
580
+ });
581
+ }
582
+ /**
583
+ * Creates setter functions for all attributes of a shader
584
+ * program. You can pass this to {@link module:webgl-utils.setBuffersAndAttributes} to set all your buffers and attributes.
585
+ *
586
+ * @see {@link module:webgl-utils.setAttributes} for example
587
+ * @param {WebGLProgram} program the program to create setters for.
588
+ * @return {Object.<string, function>} an object with a setter for each attribute by name.
589
+ * @memberOf module:webgl-utils
590
+ */
591
+ function createAttributeSetters(gl, program) {
592
+ var attribSetters = {};
593
+ function createAttribSetter(index) {
594
+ return function (b) {
595
+ gl.bindBuffer(gl.ARRAY_BUFFER, b.buffer);
596
+ gl.enableVertexAttribArray(index);
597
+ gl.vertexAttribPointer(index, b.numComponents || b.size, b.type || gl.FLOAT, b.normalize || false, b.stride || 0, b.offset || 0);
598
+ };
599
+ }
600
+ var numAttribs = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
601
+ for (var ii = 0; ii < numAttribs; ++ii) {
602
+ var attribInfo = gl.getActiveAttrib(program, ii);
603
+ if (!attribInfo) {
604
+ break;
605
+ }
606
+ var index = gl.getAttribLocation(program, attribInfo.name);
607
+ attribSetters[attribInfo.name] = createAttribSetter(index);
608
+ }
609
+ return attribSetters;
610
+ }
611
+ /**
612
+ * Sets attributes and binds buffers (deprecated... use {@link module:webgl-utils.setBuffersAndAttributes})
613
+ *
614
+ * Example:
615
+ *
616
+ * var program = createProgramFromScripts(
617
+ * gl, ["some-vs", "some-fs");
618
+ *
619
+ * var attribSetters = createAttributeSetters(program);
620
+ *
621
+ * var positionBuffer = gl.createBuffer();
622
+ * var texcoordBuffer = gl.createBuffer();
623
+ *
624
+ * var attribs = {
625
+ * a_position: {buffer: positionBuffer, numComponents: 3},
626
+ * a_texcoord: {buffer: texcoordBuffer, numComponents: 2},
627
+ * };
628
+ *
629
+ * gl.useProgram(program);
630
+ *
631
+ * This will automatically bind the buffers AND set the
632
+ * attributes.
633
+ *
634
+ * setAttributes(attribSetters, attribs);
635
+ *
636
+ * Properties of attribs. For each attrib you can add
637
+ * properties:
638
+ *
639
+ * * type: the type of data in the buffer. Default = gl.FLOAT
640
+ * * normalize: whether or not to normalize the data. Default = false
641
+ * * stride: the stride. Default = 0
642
+ * * offset: offset into the buffer. Default = 0
643
+ *
644
+ * For example if you had 3 value float positions, 2 value
645
+ * float texcoord and 4 value uint8 colors you'd setup your
646
+ * attribs like this
647
+ *
648
+ * var attribs = {
649
+ * a_position: {buffer: positionBuffer, numComponents: 3},
650
+ * a_texcoord: {buffer: texcoordBuffer, numComponents: 2},
651
+ * a_color: {
652
+ * buffer: colorBuffer,
653
+ * numComponents: 4,
654
+ * type: gl.UNSIGNED_BYTE,
655
+ * normalize: true,
656
+ * },
657
+ * };
658
+ *
659
+ * @param {Object.<string, function>} setters Attribute setters as returned from createAttributeSetters
660
+ * @param {Object.<string, module:webgl-utils.AttribInfo>} buffers AttribInfos mapped by attribute name.
661
+ * @memberOf module:webgl-utils
662
+ * @deprecated use {@link module:webgl-utils.setBuffersAndAttributes}
663
+ */
664
+ function setAttributes(setters, buffers) {
665
+ Object.keys(buffers).forEach(function (name) {
666
+ var setter = setters[name];
667
+ if (setter) {
668
+ setter(buffers[name]);
669
+ }
670
+ });
671
+ }
672
+ /**
673
+ * @typedef {Object} ProgramInfo
674
+ * @property {WebGLProgram} program A shader program
675
+ * @property {Object<string, function>} uniformSetters: object of setters as returned from createUniformSetters,
676
+ * @property {Object<string, function>} attribSetters: object of setters as returned from createAttribSetters,
677
+ * @memberOf module:webgl-utils
678
+ */
679
+ /**
680
+ * Creates a ProgramInfo from 2 sources.
681
+ *
682
+ * A ProgramInfo contains
683
+ *
684
+ * programInfo = {
685
+ * program: WebGLProgram,
686
+ * uniformSetters: object of setters as returned from createUniformSetters,
687
+ * attribSetters: object of setters as returned from createAttribSetters,
688
+ * }
689
+ *
690
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
691
+ * to use.
692
+ * @param {string[]} shaderSourcess Array of sources for the
693
+ * shaders or ids. The first is assumed to be the vertex shader,
694
+ * the second the fragment shader.
695
+ * @param {string[]} [opt_attribs] An array of attribs names. Locations will be assigned by index if not passed in
696
+ * @param {number[]} [opt_locations] The locations for the. A parallel array to opt_attribs letting you assign locations.
697
+ * @param {module:webgl-utils.ErrorCallback} opt_errorCallback callback for errors. By default it just prints an error to the console
698
+ * on error. If you want something else pass an callback. It's passed an error message.
699
+ * @return {module:webgl-utils.ProgramInfo} The created program.
700
+ * @memberOf module:webgl-utils
701
+ */
702
+ function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback) {
703
+ shaderSources = shaderSources.map(function (source) {
704
+ var script = document.getElementById(source);
705
+ return script ? script.text : source;
706
+ });
707
+ var program = createProgramFromSources(gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback);
708
+ if (!program) {
709
+ return null;
710
+ }
711
+ var uniformSetters = createUniformSetters(gl, program);
712
+ var attribSetters = createAttributeSetters(gl, program);
713
+ return {
714
+ program: program,
715
+ uniformSetters: uniformSetters,
716
+ attribSetters: attribSetters,
717
+ };
718
+ }
719
+ /**
720
+ * Sets attributes and buffers including the `ELEMENT_ARRAY_BUFFER` if appropriate
721
+ *
722
+ * Example:
723
+ *
724
+ * var programInfo = createProgramInfo(
725
+ * gl, ["some-vs", "some-fs");
726
+ *
727
+ * var arrays = {
728
+ * position: { numComponents: 3, data: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0], },
729
+ * texcoord: { numComponents: 2, data: [0, 0, 0, 1, 1, 0, 1, 1], },
730
+ * };
731
+ *
732
+ * var bufferInfo = createBufferInfoFromArrays(gl, arrays);
733
+ *
734
+ * gl.useProgram(programInfo.program);
735
+ *
736
+ * This will automatically bind the buffers AND set the
737
+ * attributes.
738
+ *
739
+ * setBuffersAndAttributes(programInfo.attribSetters, bufferInfo);
740
+ *
741
+ * For the example above it is equivilent to
742
+ *
743
+ * gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
744
+ * gl.enableVertexAttribArray(a_positionLocation);
745
+ * gl.vertexAttribPointer(a_positionLocation, 3, gl.FLOAT, false, 0, 0);
746
+ * gl.bindBuffer(gl.ARRAY_BUFFER, texcoordBuffer);
747
+ * gl.enableVertexAttribArray(a_texcoordLocation);
748
+ * gl.vertexAttribPointer(a_texcoordLocation, 4, gl.FLOAT, false, 0, 0);
749
+ *
750
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext.
751
+ * @param {Object.<string, function>} setters Attribute setters as returned from `createAttributeSetters`
752
+ * @param {module:webgl-utils.BufferInfo} buffers a BufferInfo as returned from `createBufferInfoFromArrays`.
753
+ * @memberOf module:webgl-utils
754
+ */
755
+ function setBuffersAndAttributes(gl, setters, buffers) {
756
+ setAttributes(setters, buffers.attribs);
757
+ if (buffers.indices) {
758
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices);
759
+ }
760
+ }
761
+ // Add your prefix here.
762
+ var browserPrefixes = ['', 'MOZ_', 'OP_', 'WEBKIT_'];
763
+ /**
764
+ * Given an extension name like WEBGL_compressed_texture_s3tc
765
+ * returns the supported version extension, like
766
+ * WEBKIT_WEBGL_compressed_teture_s3tc
767
+ * @param {string} name Name of extension to look for
768
+ * @return {WebGLExtension} The extension or undefined if not
769
+ * found.
770
+ * @memberOf module:webgl-utils
771
+ */
772
+ function getExtensionWithKnownPrefixes(gl, name) {
773
+ for (var ii = 0; ii < browserPrefixes.length; ++ii) {
774
+ var prefixedName = browserPrefixes[ii] + name;
775
+ var ext = gl.getExtension(prefixedName);
776
+ if (ext) {
777
+ return ext;
778
+ }
779
+ }
780
+ }
781
+ /**
782
+ * Resize a canvas to match the size its displayed.
783
+ * @param {HTMLCanvasElement} canvas The canvas to resize.
784
+ * @param {boolean} true if the canvas was resized.
785
+ * @memberOf module:webgl-utils
786
+ */
787
+ function resizeCanvasToDisplaySize(canvas, multiplier) {
788
+ multiplier = multiplier || 1;
789
+ var width = canvas.clientWidth * multiplier;
790
+ var height = canvas.clientHeight * multiplier;
791
+ if (canvas.width !== width || canvas.height !== height) {
792
+ canvas.width = width;
793
+ canvas.height = height;
794
+ return true;
795
+ }
796
+ return false;
797
+ }
798
+ /**
799
+ * Get's the iframe in the parent document
800
+ * that is displaying the specified window .
801
+ * @param {Window} window window to check.
802
+ * @return {HTMLIFrameElement?) the iframe element if window is in an iframe
803
+ */
804
+ function getIFrameForWindow(window) {
805
+ if (!isInIFrame(window)) {
806
+ return;
807
+ }
808
+ var iframes = window.parent.document.getElementsByTagName('iframe');
809
+ for (var ii = 0; ii < iframes.length; ++ii) {
810
+ var iframe = iframes[ii];
811
+ if (iframe.contentDocument === window.document) {
812
+ return iframe; // eslint-disable-line
813
+ }
814
+ }
815
+ }
816
+ /**
817
+ * Returns true if window is on screen. The main window is
818
+ * always on screen windows in iframes might not be.
819
+ * @param {Window} window the window to check.
820
+ * @return {boolean} true if window is on screen.
821
+ */
822
+ function isFrameVisible(window) {
823
+ try {
824
+ var iframe = getIFrameForWindow(window);
825
+ if (!iframe) {
826
+ return true;
827
+ }
828
+ var bounds = iframe.getBoundingClientRect();
829
+ var isVisible = bounds.top < window.parent.innerHeight &&
830
+ bounds.bottom >= 0 &&
831
+ bounds.left < window.parent.innerWidth &&
832
+ bounds.right >= 0;
833
+ return isVisible && isFrameVisible(window.parent);
834
+ }
835
+ catch (e) {
836
+ return true; // We got a security error?
837
+ }
838
+ }
839
+ /**
840
+ * Returns true if element is on screen.
841
+ * @param {HTMLElement} element the element to check.
842
+ * @return {boolean} true if element is on screen.
843
+ */
844
+ function isOnScreen(element) {
845
+ var isVisible = true;
846
+ if (element) {
847
+ var bounds = element.getBoundingClientRect();
848
+ isVisible = bounds.top < topWindow.innerHeight && bounds.bottom >= 0;
849
+ }
850
+ return isVisible && isFrameVisible(topWindow);
851
+ }
852
+ // Add `push` to a typed array. It just keeps a 'cursor'
853
+ // and allows use to `push` values into the array so we
854
+ // don't have to manually compute offsets
855
+ function augmentTypedArray(typedArray, numComponents) {
856
+ var cursor = 0;
857
+ typedArray.push = function () {
858
+ for (var ii = 0; ii < arguments.length; ++ii) {
859
+ var value = arguments[ii];
860
+ if (value instanceof Array ||
861
+ (value.buffer && value.buffer instanceof ArrayBuffer)) {
862
+ for (var jj = 0; jj < value.length; ++jj) {
863
+ typedArray[cursor++] = value[jj];
864
+ }
865
+ }
866
+ else {
867
+ typedArray[cursor++] = value;
868
+ }
869
+ }
870
+ };
871
+ typedArray.reset = function (opt_index) {
872
+ cursor = opt_index || 0;
873
+ };
874
+ typedArray.numComponents = numComponents;
875
+ Object.defineProperty(typedArray, 'numElements', {
876
+ get: function () {
877
+ return (this.length / this.numComponents) | 0;
878
+ },
879
+ });
880
+ return typedArray;
881
+ }
882
+ /**
883
+ * creates a typed array with a `push` function attached
884
+ * so that you can easily *push* values.
885
+ *
886
+ * `push` can take multiple arguments. If an argument is an array each element
887
+ * of the array will be added to the typed array.
888
+ *
889
+ * Example:
890
+ *
891
+ * var array = createAugmentedTypedArray(3, 2); // creates a Float32Array with 6 values
892
+ * array.push(1, 2, 3);
893
+ * array.push([4, 5, 6]);
894
+ * // array now contains [1, 2, 3, 4, 5, 6]
895
+ *
896
+ * Also has `numComponents` and `numElements` properties.
897
+ *
898
+ * @param {number} numComponents number of components
899
+ * @param {number} numElements number of elements. The total size of the array will be `numComponents * numElements`.
900
+ * @param {constructor} opt_type A constructor for the type. Default = `Float32Array`.
901
+ * @return {ArrayBuffer} A typed array.
902
+ * @memberOf module:webgl-utils
903
+ */
904
+ function createAugmentedTypedArray(numComponents, numElements, opt_type) {
905
+ var Type = opt_type || Float32Array;
906
+ return augmentTypedArray(new Type(numComponents * numElements), numComponents);
907
+ }
908
+ function createBufferFromTypedArray(gl, array, type, drawType) {
909
+ type = type || gl.ARRAY_BUFFER;
910
+ var buffer = gl.createBuffer();
911
+ gl.bindBuffer(type, buffer);
912
+ gl.bufferData(type, array, drawType || gl.STATIC_DRAW);
913
+ return buffer;
914
+ }
915
+ function allButIndices(name) {
916
+ return name !== 'indices';
917
+ }
918
+ function createMapping(obj) {
919
+ var mapping = {};
920
+ Object.keys(obj)
921
+ .filter(allButIndices)
922
+ .forEach(function (key) {
923
+ mapping['a_' + key] = key;
924
+ });
925
+ return mapping;
926
+ }
927
+ function getGLTypeForTypedArray(gl, typedArray) {
928
+ if (typedArray instanceof Int8Array) {
929
+ return gl.BYTE;
930
+ } // eslint-disable-line
931
+ if (typedArray instanceof Uint8Array) {
932
+ return gl.UNSIGNED_BYTE;
933
+ } // eslint-disable-line
934
+ if (typedArray instanceof Int16Array) {
935
+ return gl.SHORT;
936
+ } // eslint-disable-line
937
+ if (typedArray instanceof Uint16Array) {
938
+ return gl.UNSIGNED_SHORT;
939
+ } // eslint-disable-line
940
+ if (typedArray instanceof Int32Array) {
941
+ return gl.INT;
942
+ } // eslint-disable-line
943
+ if (typedArray instanceof Uint32Array) {
944
+ return gl.UNSIGNED_INT;
945
+ } // eslint-disable-line
946
+ if (typedArray instanceof Float32Array) {
947
+ return gl.FLOAT;
948
+ } // eslint-disable-line
949
+ throw 'unsupported typed array type';
950
+ }
951
+ // This is really just a guess. Though I can't really imagine using
952
+ // anything else? Maybe for some compression?
953
+ function getNormalizationForTypedArray(typedArray) {
954
+ if (typedArray instanceof Int8Array) {
955
+ return true;
956
+ } // eslint-disable-line
957
+ if (typedArray instanceof Uint8Array) {
958
+ return true;
959
+ } // eslint-disable-line
960
+ return false;
961
+ }
962
+ function isArrayBuffer(a) {
963
+ return a.buffer && a.buffer instanceof ArrayBuffer;
964
+ }
965
+ function guessNumComponentsFromName(name, length) {
966
+ var numComponents;
967
+ if (name.indexOf('coord') >= 0) {
968
+ numComponents = 2;
969
+ }
970
+ else if (name.indexOf('color') >= 0) {
971
+ numComponents = 4;
972
+ }
973
+ else {
974
+ numComponents = 3; // position, normals, indices ...
975
+ }
976
+ if (length % numComponents > 0) {
977
+ throw 'can not guess numComponents. You should specify it.';
978
+ }
979
+ return numComponents;
980
+ }
981
+ function makeTypedArray(array, name) {
982
+ if (isArrayBuffer(array)) {
983
+ return array;
984
+ }
985
+ if (Array.isArray(array)) {
986
+ array = {
987
+ data: array,
988
+ };
989
+ }
990
+ if (!array.numComponents) {
991
+ array.numComponents = guessNumComponentsFromName(name, array.length);
992
+ }
993
+ var type = array.type;
994
+ if (!type) {
995
+ if (name === 'indices') {
996
+ type = Uint16Array;
997
+ }
998
+ }
999
+ var typedArray = createAugmentedTypedArray(array.numComponents, (array.data.length / array.numComponents) | 0, type);
1000
+ typedArray.push(array.data);
1001
+ return typedArray;
1002
+ }
1003
+ /**
1004
+ * @typedef {Object} AttribInfo
1005
+ * @property {number} [numComponents] the number of components for this attribute.
1006
+ * @property {number} [size] the number of components for this attribute.
1007
+ * @property {number} [type] the type of the attribute (eg. `gl.FLOAT`, `gl.UNSIGNED_BYTE`, etc...) Default = `gl.FLOAT`
1008
+ * @property {boolean} [normalized] whether or not to normalize the data. Default = false
1009
+ * @property {number} [offset] offset into buffer in bytes. Default = 0
1010
+ * @property {number} [stride] the stride in bytes per element. Default = 0
1011
+ * @property {WebGLBuffer} buffer the buffer that contains the data for this attribute
1012
+ * @memberOf module:webgl-utils
1013
+ */
1014
+ /**
1015
+ * Creates a set of attribute data and WebGLBuffers from set of arrays
1016
+ *
1017
+ * Given
1018
+ *
1019
+ * var arrays = {
1020
+ * position: { numComponents: 3, data: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0], },
1021
+ * texcoord: { numComponents: 2, data: [0, 0, 0, 1, 1, 0, 1, 1], },
1022
+ * normal: { numComponents: 3, data: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1], },
1023
+ * color: { numComponents: 4, data: [255, 255, 255, 255, 255, 0, 0, 255, 0, 0, 255, 255], type: Uint8Array, },
1024
+ * indices: { numComponents: 3, data: [0, 1, 2, 1, 2, 3], },
1025
+ * };
1026
+ *
1027
+ * returns something like
1028
+ *
1029
+ * var attribs = {
1030
+ * a_position: { numComponents: 3, type: gl.FLOAT, normalize: false, buffer: WebGLBuffer, },
1031
+ * a_texcoord: { numComponents: 2, type: gl.FLOAT, normalize: false, buffer: WebGLBuffer, },
1032
+ * a_normal: { numComponents: 3, type: gl.FLOAT, normalize: false, buffer: WebGLBuffer, },
1033
+ * a_color: { numComponents: 4, type: gl.UNSIGNED_BYTE, normalize: true, buffer: WebGLBuffer, },
1034
+ * };
1035
+ *
1036
+ * @param {WebGLRenderingContext} gl The webgl rendering context.
1037
+ * @param {Object.<string, array|typedarray>} arrays The arrays
1038
+ * @param {Object.<string, string>} [opt_mapping] mapping from attribute name to array name.
1039
+ * if not specified defaults to "a_name" -> "name".
1040
+ * @return {Object.<string, module:webgl-utils.AttribInfo>} the attribs
1041
+ * @memberOf module:webgl-utils
1042
+ */
1043
+ function createAttribsFromArrays(gl, arrays, opt_mapping) {
1044
+ var mapping = opt_mapping || createMapping(arrays);
1045
+ var attribs = {};
1046
+ Object.keys(mapping).forEach(function (attribName) {
1047
+ var bufferName = mapping[attribName];
1048
+ var array = makeTypedArray(arrays[bufferName], bufferName);
1049
+ attribs[attribName] = {
1050
+ buffer: createBufferFromTypedArray(gl, array),
1051
+ numComponents: array.numComponents || guessNumComponentsFromName(bufferName),
1052
+ type: getGLTypeForTypedArray(gl, array),
1053
+ normalize: getNormalizationForTypedArray(array),
1054
+ };
1055
+ });
1056
+ return attribs;
1057
+ }
1058
+ /**
1059
+ * tries to get the number of elements from a set of arrays.
1060
+ */
1061
+ function getNumElementsFromNonIndexedArrays(arrays) {
1062
+ var key = Object.keys(arrays)[0];
1063
+ var array = arrays[key];
1064
+ if (isArrayBuffer(array)) {
1065
+ return array.numElements;
1066
+ }
1067
+ else {
1068
+ return array.data.length / array.numComponents;
1069
+ }
1070
+ }
1071
+ /**
1072
+ * @typedef {Object} BufferInfo
1073
+ * @property {number} numElements The number of elements to pass to `gl.drawArrays` or `gl.drawElements`.
1074
+ * @property {WebGLBuffer} [indices] The indices `ELEMENT_ARRAY_BUFFER` if any indices exist.
1075
+ * @property {Object.<string, module:webgl-utils.AttribInfo>} attribs The attribs approriate to call `setAttributes`
1076
+ * @memberOf module:webgl-utils
1077
+ */
1078
+ /**
1079
+ * Creates a BufferInfo from an object of arrays.
1080
+ *
1081
+ * This can be passed to {@link module:webgl-utils.setBuffersAndAttributes} and to
1082
+ * {@link module:webgl-utils:drawBufferInfo}.
1083
+ *
1084
+ * Given an object like
1085
+ *
1086
+ * var arrays = {
1087
+ * position: { numComponents: 3, data: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0], },
1088
+ * texcoord: { numComponents: 2, data: [0, 0, 0, 1, 1, 0, 1, 1], },
1089
+ * normal: { numComponents: 3, data: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1], },
1090
+ * indices: { numComponents: 3, data: [0, 1, 2, 1, 2, 3], },
1091
+ * };
1092
+ *
1093
+ * Creates an BufferInfo like this
1094
+ *
1095
+ * bufferInfo = {
1096
+ * numElements: 4, // or whatever the number of elements is
1097
+ * indices: WebGLBuffer, // this property will not exist if there are no indices
1098
+ * attribs: {
1099
+ * a_position: { buffer: WebGLBuffer, numComponents: 3, },
1100
+ * a_normal: { buffer: WebGLBuffer, numComponents: 3, },
1101
+ * a_texcoord: { buffer: WebGLBuffer, numComponents: 2, },
1102
+ * },
1103
+ * };
1104
+ *
1105
+ * The properties of arrays can be JavaScript arrays in which case the number of components
1106
+ * will be guessed.
1107
+ *
1108
+ * var arrays = {
1109
+ * position: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0],
1110
+ * texcoord: [0, 0, 0, 1, 1, 0, 1, 1],
1111
+ * normal: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1],
1112
+ * indices: [0, 1, 2, 1, 2, 3],
1113
+ * };
1114
+ *
1115
+ * They can also by TypedArrays
1116
+ *
1117
+ * var arrays = {
1118
+ * position: new Float32Array([0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0]),
1119
+ * texcoord: new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]),
1120
+ * normal: new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]),
1121
+ * indices: new Uint16Array([0, 1, 2, 1, 2, 3]),
1122
+ * };
1123
+ *
1124
+ * Or augmentedTypedArrays
1125
+ *
1126
+ * var positions = createAugmentedTypedArray(3, 4);
1127
+ * var texcoords = createAugmentedTypedArray(2, 4);
1128
+ * var normals = createAugmentedTypedArray(3, 4);
1129
+ * var indices = createAugmentedTypedArray(3, 2, Uint16Array);
1130
+ *
1131
+ * positions.push([0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0]);
1132
+ * texcoords.push([0, 0, 0, 1, 1, 0, 1, 1]);
1133
+ * normals.push([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]);
1134
+ * indices.push([0, 1, 2, 1, 2, 3]);
1135
+ *
1136
+ * var arrays = {
1137
+ * position: positions,
1138
+ * texcoord: texcoords,
1139
+ * normal: normals,
1140
+ * indices: indices,
1141
+ * };
1142
+ *
1143
+ * For the last example it is equivalent to
1144
+ *
1145
+ * var bufferInfo = {
1146
+ * attribs: {
1147
+ * a_position: { numComponents: 3, buffer: gl.createBuffer(), },
1148
+ * a_texcoods: { numComponents: 2, buffer: gl.createBuffer(), },
1149
+ * a_normals: { numComponents: 3, buffer: gl.createBuffer(), },
1150
+ * },
1151
+ * indices: gl.createBuffer(),
1152
+ * numElements: 6,
1153
+ * };
1154
+ *
1155
+ * gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.attribs.a_position.buffer);
1156
+ * gl.bufferData(gl.ARRAY_BUFFER, arrays.position, gl.STATIC_DRAW);
1157
+ * gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.attribs.a_texcoord.buffer);
1158
+ * gl.bufferData(gl.ARRAY_BUFFER, arrays.texcoord, gl.STATIC_DRAW);
1159
+ * gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.attribs.a_normal.buffer);
1160
+ * gl.bufferData(gl.ARRAY_BUFFER, arrays.normal, gl.STATIC_DRAW);
1161
+ * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferInfo.indices);
1162
+ * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, arrays.indices, gl.STATIC_DRAW);
1163
+ *
1164
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
1165
+ * @param {Object.<string, array|object|typedarray>} arrays Your data
1166
+ * @param {Object.<string, string>} [opt_mapping] an optional mapping of attribute to array name.
1167
+ * If not passed in it's assumed the array names will be mapped to an attibute
1168
+ * of the same name with "a_" prefixed to it. An other words.
1169
+ *
1170
+ * var arrays = {
1171
+ * position: ...,
1172
+ * texcoord: ...,
1173
+ * normal: ...,
1174
+ * indices: ...,
1175
+ * };
1176
+ *
1177
+ * bufferInfo = createBufferInfoFromArrays(gl, arrays);
1178
+ *
1179
+ * Is the same as
1180
+ *
1181
+ * var arrays = {
1182
+ * position: ...,
1183
+ * texcoord: ...,
1184
+ * normal: ...,
1185
+ * indices: ...,
1186
+ * };
1187
+ *
1188
+ * var mapping = {
1189
+ * a_position: "position",
1190
+ * a_texcoord: "texcoord",
1191
+ * a_normal: "normal",
1192
+ * };
1193
+ *
1194
+ * bufferInfo = createBufferInfoFromArrays(gl, arrays, mapping);
1195
+ *
1196
+ * @return {module:webgl-utils.BufferInfo} A BufferInfo
1197
+ * @memberOf module:webgl-utils
1198
+ */
1199
+ function createBufferInfoFromArrays(gl, arrays, opt_mapping) {
1200
+ var bufferInfo = {
1201
+ attribs: createAttribsFromArrays(gl, arrays, opt_mapping),
1202
+ };
1203
+ var indices = arrays.indices;
1204
+ if (indices) {
1205
+ indices = makeTypedArray(indices, 'indices');
1206
+ bufferInfo.indices = createBufferFromTypedArray(gl, indices, gl.ELEMENT_ARRAY_BUFFER);
1207
+ bufferInfo.numElements = indices.length;
1208
+ }
1209
+ else {
1210
+ bufferInfo.numElements = getNumElementsFromNonIndexedArrays(arrays);
1211
+ }
1212
+ return bufferInfo;
1213
+ }
1214
+ /**
1215
+ * Creates buffers from typed arrays
1216
+ *
1217
+ * Given something like this
1218
+ *
1219
+ * var arrays = {
1220
+ * positions: [1, 2, 3],
1221
+ * normals: [0, 0, 1],
1222
+ * }
1223
+ *
1224
+ * returns something like
1225
+ *
1226
+ * buffers = {
1227
+ * positions: WebGLBuffer,
1228
+ * normals: WebGLBuffer,
1229
+ * }
1230
+ *
1231
+ * If the buffer is named 'indices' it will be made an ELEMENT_ARRAY_BUFFER.
1232
+ *
1233
+ * @param {WebGLRenderingContext) gl A WebGLRenderingContext.
1234
+ * @param {Object<string, array|typedarray>} arrays
1235
+ * @return {Object<string, WebGLBuffer>} returns an object with one WebGLBuffer per array
1236
+ * @memberOf module:webgl-utils
1237
+ */
1238
+ function createBuffersFromArrays(gl, arrays) {
1239
+ var buffers = {};
1240
+ Object.keys(arrays).forEach(function (key) {
1241
+ var type = key === 'indices' ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
1242
+ var array = makeTypedArray(arrays[key], name);
1243
+ buffers[key] = createBufferFromTypedArray(gl, array, type);
1244
+ });
1245
+ // hrm
1246
+ if (arrays.indices) {
1247
+ buffers.numElements = arrays.indices.length;
1248
+ }
1249
+ else if (arrays.position) {
1250
+ buffers.numElements = arrays.position.length / 3;
1251
+ }
1252
+ return buffers;
1253
+ }
1254
+ /**
1255
+ * Calls `gl.drawElements` or `gl.drawArrays`, whichever is appropriate
1256
+ *
1257
+ * normally you'd call `gl.drawElements` or `gl.drawArrays` yourself
1258
+ * but calling this means if you switch from indexed data to non-indexed
1259
+ * data you don't have to remember to update your draw call.
1260
+ *
1261
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
1262
+ * @param {enum} type eg (gl.TRIANGLES, gl.LINES, gl.POINTS, gl.TRIANGLE_STRIP, ...)
1263
+ * @param {module:webgl-utils.BufferInfo} bufferInfo as returned from createBufferInfoFromArrays
1264
+ * @param {number} [count] An optional count. Defaults to bufferInfo.numElements
1265
+ * @param {number} [offset] An optional offset. Defaults to 0.
1266
+ * @memberOf module:webgl-utils
1267
+ */
1268
+ function drawBufferInfo(gl, type, bufferInfo, count, offset) {
1269
+ var indices = bufferInfo.indices;
1270
+ var numElements = count === undefined ? bufferInfo.numElements : count;
1271
+ offset = offset === undefined ? offset : 0;
1272
+ if (indices) {
1273
+ gl.drawElements(type, numElements, gl.UNSIGNED_SHORT, offset);
1274
+ }
1275
+ else {
1276
+ gl.drawArrays(type, offset, numElements);
1277
+ }
1278
+ }
1279
+ /**
1280
+ * @typedef {Object} DrawObject
1281
+ * @property {module:webgl-utils.ProgramInfo} programInfo A ProgramInfo as returned from createProgramInfo
1282
+ * @property {module:webgl-utils.BufferInfo} bufferInfo A BufferInfo as returned from createBufferInfoFromArrays
1283
+ * @property {Object<string, ?>} uniforms The values for the uniforms
1284
+ * @memberOf module:webgl-utils
1285
+ */
1286
+ /**
1287
+ * Draws a list of objects
1288
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
1289
+ * @param {DrawObject[]} objectsToDraw an array of objects to draw.
1290
+ * @memberOf module:webgl-utils
1291
+ */
1292
+ function drawObjectList(gl, objectsToDraw) {
1293
+ var lastUsedProgramInfo = null;
1294
+ var lastUsedBufferInfo = null;
1295
+ objectsToDraw.forEach(function (object) {
1296
+ var programInfo = object.programInfo;
1297
+ var bufferInfo = object.bufferInfo;
1298
+ var bindBuffers = false;
1299
+ if (programInfo !== lastUsedProgramInfo) {
1300
+ lastUsedProgramInfo = programInfo;
1301
+ gl.useProgram(programInfo.program);
1302
+ bindBuffers = true;
1303
+ }
1304
+ // Setup all the needed attributes.
1305
+ if (bindBuffers || bufferInfo !== lastUsedBufferInfo) {
1306
+ lastUsedBufferInfo = bufferInfo;
1307
+ setBuffersAndAttributes(gl, programInfo.attribSetters, bufferInfo);
1308
+ }
1309
+ // Set the uniforms.
1310
+ setUniforms(programInfo.uniformSetters, object.uniforms);
1311
+ // Draw
1312
+ drawBufferInfo(gl, gl.TRIANGLES, bufferInfo);
1313
+ });
1314
+ }
1315
+ return {
1316
+ createAugmentedTypedArray: createAugmentedTypedArray,
1317
+ createAttribsFromArrays: createAttribsFromArrays,
1318
+ createBuffersFromArrays: createBuffersFromArrays,
1319
+ createBufferInfoFromArrays: createBufferInfoFromArrays,
1320
+ createAttributeSetters: createAttributeSetters,
1321
+ createProgram: createProgram,
1322
+ createProgramFromScripts: createProgramFromScripts,
1323
+ createProgramFromSources: createProgramFromSources,
1324
+ createProgramInfo: createProgramInfo,
1325
+ createUniformSetters: createUniformSetters,
1326
+ drawBufferInfo: drawBufferInfo,
1327
+ drawObjectList: drawObjectList,
1328
+ getWebGLContext: getWebGLContext,
1329
+ updateCSSIfInIFrame: updateCSSIfInIFrame,
1330
+ getExtensionWithKnownPrefixes: getExtensionWithKnownPrefixes,
1331
+ resizeCanvasToDisplaySize: resizeCanvasToDisplaySize,
1332
+ setAttributes: setAttributes,
1333
+ setBuffersAndAttributes: setBuffersAndAttributes,
1334
+ setUniforms: setUniforms,
1335
+ setupWebGL: setupWebGL,
1336
+ };
1337
+ });