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