threejs-lessons-helper.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. /* global define */
  32. (function(root, factory) { // eslint-disable-line
  33. if (typeof define === 'function' && define.amd) {
  34. // AMD. Register as an anonymous module.
  35. define([], function() {
  36. return factory.call(root);
  37. });
  38. } else {
  39. // Browser globals
  40. root.threejsLessonsHelper = factory.call(root);
  41. }
  42. }(this, function() {
  43. 'use strict'; // eslint-disable-line
  44. var topWindow = this;
  45. /**
  46. * Check if the page is embedded.
  47. * @param {Window?) w window to check
  48. * @return {boolean} True of we are in an iframe
  49. */
  50. function isInIFrame(w) {
  51. w = w || topWindow;
  52. return w !== w.top;
  53. }
  54. function updateCSSIfInIFrame() {
  55. if (isInIFrame()) {
  56. try {
  57. document.getElementsByTagName('html')[0].className = 'iframe';
  58. } catch (e) {
  59. // eslint-disable-line
  60. }
  61. try {
  62. document.body.className = 'iframe';
  63. } catch (e) {
  64. // eslint-disable-line
  65. }
  66. }
  67. }
  68. function isInEditor() {
  69. return window.location.href.substring(0, 4) === 'blob';
  70. }
  71. /**
  72. * Creates a webgl context. If creation fails it will
  73. * change the contents of the container of the <canvas>
  74. * tag to an error message with the correct links for WebGL.
  75. * @param {HTMLCanvasElement} canvas. The canvas element to
  76. * create a context from.
  77. * @param {WebGLContextCreationAttirbutes} opt_attribs Any
  78. * creation attributes you want to pass in.
  79. * @return {WebGLRenderingContext} The created context.
  80. * @memberOf module:webgl-utils
  81. */
  82. function showNeedWebGL(canvas) {
  83. var doc = canvas.ownerDocument;
  84. if (doc) {
  85. var div = doc.createElement('div');
  86. div.innerHTML = `
  87. <div style="
  88. position: absolute;
  89. left: 0;
  90. top: 0;
  91. background-color: #DEF;
  92. width: 100vw;
  93. height: 100vh;
  94. display: flex;
  95. flex-flow: column;
  96. justify-content: center;
  97. align-content: center;
  98. align-items: center;
  99. ">
  100. <div style="text-align: center;">
  101. It doesn't appear your browser supports WebGL.<br/>
  102. <a href="http://get.webgl.org" target="_blank">Click here for more information.</a>
  103. </div>
  104. </div>
  105. `;
  106. div = div.querySelector('div');
  107. doc.body.appendChild(div);
  108. }
  109. }
  110. var origConsole = {};
  111. function setupConsole() {
  112. var parent = document.createElement('div');
  113. parent.className = 'console';
  114. Object.assign(parent.style, {
  115. fontFamily: 'monospace',
  116. fontSize: 'medium',
  117. maxHeight: '50%',
  118. position: 'fixed',
  119. bottom: 0,
  120. left: 0,
  121. width: '100%',
  122. overflow: 'auto',
  123. background: '#DDD',
  124. });
  125. var numLinesRemaining = 100;
  126. var added = false;
  127. function addLine(type, str) {
  128. var div = document.createElement('div');
  129. div.textContent = str;
  130. div.className = type;
  131. parent.appendChild(div);
  132. if (!added) {
  133. added = true;
  134. document.body.appendChild(parent);
  135. }
  136. }
  137. function addLines(type, str) {
  138. if (numLinesRemaining) {
  139. --numLinesRemaining;
  140. addLine(type, str);
  141. }
  142. }
  143. function wrapFunc(obj, funcName) {
  144. var oldFn = obj[funcName];
  145. origConsole[funcName] = oldFn.bind(obj);
  146. return function() {
  147. addLines(funcName, [].join.call(arguments, ' '));
  148. oldFn.apply(obj, arguments);
  149. };
  150. }
  151. window.console.log = wrapFunc(window.console, 'log');
  152. window.console.warn = wrapFunc(window.console, 'warn');
  153. window.console.error = wrapFunc(window.console, 'error');
  154. }
  155. /**
  156. * Gets a WebGL context.
  157. * makes its backing store the size it is displayed.
  158. * @param {HTMLCanvasElement} canvas a canvas element.
  159. * @memberOf module:webgl-utils
  160. */
  161. var setupLesson = function(canvas) {
  162. // only once
  163. setupLesson = function() {};
  164. if (canvas) {
  165. canvas.addEventListener('webglcontextlost', function(e) {
  166. // the default is to do nothing. Preventing the default
  167. // means allowing context to be restored
  168. e.preventDefault();
  169. var div = document.createElement('div');
  170. div.className = 'contextlost';
  171. div.innerHTML = '<div>Context Lost: Click To Reload</div>';
  172. div.addEventListener('click', function() {
  173. window.location.reload();
  174. });
  175. document.body.appendChild(div);
  176. });
  177. canvas.addEventListener('webglcontextrestored', function() {
  178. // just reload the page. Easiest.
  179. window.location.reload();
  180. });
  181. }
  182. if (isInIFrame()) {
  183. updateCSSIfInIFrame();
  184. }
  185. };
  186. /**
  187. * Get's the iframe in the parent document
  188. * that is displaying the specified window .
  189. * @param {Window} window window to check.
  190. * @return {HTMLIFrameElement?) the iframe element if window is in an iframe
  191. */
  192. function getIFrameForWindow(window) {
  193. if (!isInIFrame(window)) {
  194. return;
  195. }
  196. var iframes = window.parent.document.getElementsByTagName('iframe');
  197. for (var ii = 0; ii < iframes.length; ++ii) {
  198. var iframe = iframes[ii];
  199. if (iframe.contentDocument === window.document) {
  200. return iframe; // eslint-disable-line
  201. }
  202. }
  203. }
  204. /**
  205. * Returns true if window is on screen. The main window is
  206. * always on screen windows in iframes might not be.
  207. * @param {Window} window the window to check.
  208. * @return {boolean} true if window is on screen.
  209. */
  210. function isFrameVisible(window) {
  211. try {
  212. var iframe = getIFrameForWindow(window);
  213. if (!iframe) {
  214. return true;
  215. }
  216. var bounds = iframe.getBoundingClientRect();
  217. var isVisible = bounds.top < window.parent.innerHeight && bounds.bottom >= 0 &&
  218. bounds.left < window.parent.innerWidth && bounds.right >= 0;
  219. return isVisible && isFrameVisible(window.parent);
  220. } catch (e) {
  221. return true; // We got a security error?
  222. }
  223. }
  224. /**
  225. * Returns true if element is on screen.
  226. * @param {HTMLElement} element the element to check.
  227. * @return {boolean} true if element is on screen.
  228. */
  229. function isOnScreen(element) {
  230. var isVisible = true;
  231. if (element) {
  232. var bounds = element.getBoundingClientRect();
  233. isVisible = bounds.top < topWindow.innerHeight && bounds.bottom >= 0;
  234. }
  235. return isVisible && isFrameVisible(topWindow);
  236. }
  237. // Replace requestAnimationFrame.
  238. if (topWindow.requestAnimationFrame) {
  239. topWindow.requestAnimationFrame = (function(oldRAF) {
  240. return function(callback, element) {
  241. var handler = function() {
  242. if (isOnScreen(element)) {
  243. oldRAF(callback, element);
  244. } else {
  245. oldRAF(handler, element);
  246. }
  247. };
  248. handler();
  249. };
  250. }(topWindow.requestAnimationFrame));
  251. }
  252. updateCSSIfInIFrame();
  253. //------------ [ from https://github.com/KhronosGroup/WebGLDeveloperTools ]
  254. /*
  255. ** Copyright (c) 2012 The Khronos Group Inc.
  256. **
  257. ** Permission is hereby granted, free of charge, to any person obtaining a
  258. ** copy of this software and/or associated documentation files (the
  259. ** "Materials"), to deal in the Materials without restriction, including
  260. ** without limitation the rights to use, copy, modify, merge, publish,
  261. ** distribute, sublicense, and/or sell copies of the Materials, and to
  262. ** permit persons to whom the Materials are furnished to do so, subject to
  263. ** the following conditions:
  264. **
  265. ** The above copyright notice and this permission notice shall be included
  266. ** in all copies or substantial portions of the Materials.
  267. **
  268. ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  269. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  270. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  271. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  272. ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  273. ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  274. ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
  275. */
  276. /**
  277. * Types of contexts we have added to map
  278. */
  279. var mappedContextTypes = {};
  280. /**
  281. * Map of numbers to names.
  282. * @type {Object}
  283. */
  284. var glEnums = {};
  285. /**
  286. * Map of names to numbers.
  287. * @type {Object}
  288. */
  289. var enumStringToValue = {};
  290. /**
  291. * Initializes this module. Safe to call more than once.
  292. * @param {!WebGLRenderingContext} ctx A WebGL context. If
  293. * you have more than one context it doesn't matter which one
  294. * you pass in, it is only used to pull out constants.
  295. */
  296. function addEnumsForContext(ctx, type) {
  297. if (!mappedContextTypes[type]) {
  298. mappedContextTypes[type] = true;
  299. for (var propertyName in ctx) {
  300. if (typeof ctx[propertyName] === 'number') {
  301. glEnums[ctx[propertyName]] = propertyName;
  302. enumStringToValue[propertyName] = ctx[propertyName];
  303. }
  304. }
  305. }
  306. }
  307. function enumArrayToString(enums) {
  308. if (enums.length) {
  309. var enumStrings = [];
  310. for (var i = 0; i < enums.length; ++i) {
  311. enums.push(glEnumToString(enums[i])); // eslint-disable-line
  312. }
  313. return '[' + enumStrings.join(', ') + ']';
  314. }
  315. return enumStrings.toString();
  316. }
  317. function makeBitFieldToStringFunc(enums) {
  318. return function(value) {
  319. var orResult = 0;
  320. var orEnums = [];
  321. for (var i = 0; i < enums.length; ++i) {
  322. var enumValue = enumStringToValue[enums[i]];
  323. if ((value & enumValue) !== 0) {
  324. orResult |= enumValue;
  325. orEnums.push(glEnumToString(enumValue)); // eslint-disable-line
  326. }
  327. }
  328. if (orResult === value) {
  329. return orEnums.join(' | ');
  330. } else {
  331. return glEnumToString(value); // eslint-disable-line
  332. }
  333. };
  334. }
  335. var destBufferBitFieldToString = makeBitFieldToStringFunc([
  336. 'COLOR_BUFFER_BIT',
  337. 'DEPTH_BUFFER_BIT',
  338. 'STENCIL_BUFFER_BIT',
  339. ]);
  340. /**
  341. * Which arguments are enums based on the number of arguments to the function.
  342. * So
  343. * 'texImage2D': {
  344. * 9: { 0:true, 2:true, 6:true, 7:true },
  345. * 6: { 0:true, 2:true, 3:true, 4:true },
  346. * },
  347. *
  348. * means if there are 9 arguments then 6 and 7 are enums, if there are 6
  349. * arguments 3 and 4 are enums. Maybe a function as well in which case
  350. * value is passed to function and returns a string
  351. *
  352. * @type {!Object.<number, (!Object.<number, string>|function)}
  353. */
  354. var glValidEnumContexts = {
  355. // Generic setters and getters
  356. 'enable': {1: { 0:true }},
  357. 'disable': {1: { 0:true }},
  358. 'getParameter': {1: { 0:true }},
  359. // Rendering
  360. 'drawArrays': {3:{ 0:true }},
  361. 'drawElements': {4:{ 0:true, 2:true }},
  362. 'drawArraysInstanced': {4: { 0:true }},
  363. 'drawElementsInstanced': {5: {0:true, 2: true }},
  364. 'drawRangeElements': {6: {0:true, 4: true }},
  365. // Shaders
  366. 'createShader': {1: { 0:true }},
  367. 'getShaderParameter': {2: { 1:true }},
  368. 'getProgramParameter': {2: { 1:true }},
  369. 'getShaderPrecisionFormat': {2: { 0: true, 1:true }},
  370. // Vertex attributes
  371. 'getVertexAttrib': {2: { 1:true }},
  372. 'vertexAttribPointer': {6: { 2:true }},
  373. 'vertexAttribIPointer': {5: { 2:true }}, // WebGL2
  374. // Textures
  375. 'bindTexture': {2: { 0:true }},
  376. 'activeTexture': {1: { 0:true }},
  377. 'getTexParameter': {2: { 0:true, 1:true }},
  378. 'texParameterf': {3: { 0:true, 1:true }},
  379. 'texParameteri': {3: { 0:true, 1:true, 2:true }},
  380. 'texImage2D': {
  381. 9: { 0:true, 2:true, 6:true, 7:true },
  382. 6: { 0:true, 2:true, 3:true, 4:true },
  383. 10: { 0:true, 2:true, 6:true, 7:true }, // WebGL2
  384. },
  385. 'texImage3D': {
  386. 10: { 0:true, 2:true, 7:true, 8:true }, // WebGL2
  387. 11: { 0:true, 2:true, 7:true, 8:true }, // WebGL2
  388. },
  389. 'texSubImage2D': {
  390. 9: { 0:true, 6:true, 7:true },
  391. 7: { 0:true, 4:true, 5:true },
  392. 10: { 0:true, 6:true, 7:true }, // WebGL2
  393. },
  394. 'texSubImage3D': {
  395. 11: { 0:true, 8:true, 9:true }, // WebGL2
  396. 12: { 0:true, 8:true, 9:true }, // WebGL2
  397. },
  398. 'texStorage2D': { 5: { 0:true, 2:true }}, // WebGL2
  399. 'texStorage3D': { 6: { 0:true, 2:true }}, // WebGL2
  400. 'copyTexImage2D': {8: { 0:true, 2:true }},
  401. 'copyTexSubImage2D': {8: { 0:true }},
  402. 'copyTexSubImage3D': {9: { 0:true }}, // WebGL2
  403. 'generateMipmap': {1: { 0:true }},
  404. 'compressedTexImage2D': {
  405. 7: { 0: true, 2:true },
  406. 8: { 0: true, 2:true }, // WebGL2
  407. },
  408. 'compressedTexSubImage2D': {
  409. 8: { 0: true, 6:true },
  410. 9: { 0: true, 6:true }, // WebGL2
  411. },
  412. 'compressedTexImage3D': {
  413. 8: { 0: true, 2: true, }, // WebGL2
  414. 9: { 0: true, 2: true, }, // WebGL2
  415. },
  416. 'compressedTexSubImage3D': {
  417. 9: { 0: true, 8: true, }, // WebGL2
  418. 10: { 0: true, 8: true, }, // WebGL2
  419. },
  420. // Buffer objects
  421. 'bindBuffer': {2: { 0:true }},
  422. 'bufferData': {
  423. 3: { 0:true, 2:true },
  424. 4: { 0:true, 2:true }, // WebGL2
  425. 5: { 0:true, 2:true }, // WebGL2
  426. },
  427. 'bufferSubData': {
  428. 3: { 0:true },
  429. 4: { 0:true }, // WebGL2
  430. 5: { 0:true }, // WebGL2
  431. },
  432. 'copyBufferSubData': {
  433. 5: { 0:true }, // WeBGL2
  434. },
  435. 'getBufferParameter': {2: { 0:true, 1:true }},
  436. 'getBufferSubData': {
  437. 3: { 0: true, }, // WebGL2
  438. 4: { 0: true, }, // WebGL2
  439. 5: { 0: true, }, // WebGL2
  440. },
  441. // Renderbuffers and framebuffers
  442. 'pixelStorei': {2: { 0:true, 1:true }},
  443. 'readPixels': {
  444. 7: { 4:true, 5:true },
  445. 8: { 4:true, 5:true }, // WebGL2
  446. },
  447. 'bindRenderbuffer': {2: { 0:true }},
  448. 'bindFramebuffer': {2: { 0:true }},
  449. 'blitFramebuffer': {10: { 8: destBufferBitFieldToString, 9:true }}, // WebGL2
  450. 'checkFramebufferStatus': {1: { 0:true }},
  451. 'framebufferRenderbuffer': {4: { 0:true, 1:true, 2:true }},
  452. 'framebufferTexture2D': {5: { 0:true, 1:true, 2:true }},
  453. 'framebufferTextureLayer': {5: {0:true, 1:true }}, // WebGL2
  454. 'getFramebufferAttachmentParameter': {3: { 0:true, 1:true, 2:true }},
  455. 'getInternalformatParameter': {3: {0:true, 1:true, 2:true }}, // WebGL2
  456. 'getRenderbufferParameter': {2: { 0:true, 1:true }},
  457. 'invalidateFramebuffer': {2: { 0:true, 1: enumArrayToString, }}, // WebGL2
  458. 'invalidateSubFramebuffer': {6: {0: true, 1: enumArrayToString, }}, // WebGL2
  459. 'readBuffer': {1: {0: true}}, // WebGL2
  460. 'renderbufferStorage': {4: { 0:true, 1:true }},
  461. 'renderbufferStorageMultisample': {5: { 0: true, 2: true }}, // WebGL2
  462. // Frame buffer operations (clear, blend, depth test, stencil)
  463. 'clear': {1: { 0: destBufferBitFieldToString }},
  464. 'depthFunc': {1: { 0:true }},
  465. 'blendFunc': {2: { 0:true, 1:true }},
  466. 'blendFuncSeparate': {4: { 0:true, 1:true, 2:true, 3:true }},
  467. 'blendEquation': {1: { 0:true }},
  468. 'blendEquationSeparate': {2: { 0:true, 1:true }},
  469. 'stencilFunc': {3: { 0:true }},
  470. 'stencilFuncSeparate': {4: { 0:true, 1:true }},
  471. 'stencilMaskSeparate': {2: { 0:true }},
  472. 'stencilOp': {3: { 0:true, 1:true, 2:true }},
  473. 'stencilOpSeparate': {4: { 0:true, 1:true, 2:true, 3:true }},
  474. // Culling
  475. 'cullFace': {1: { 0:true }},
  476. 'frontFace': {1: { 0:true }},
  477. // ANGLE_instanced_arrays extension
  478. 'drawArraysInstancedANGLE': {4: { 0:true }},
  479. 'drawElementsInstancedANGLE': {5: { 0:true, 2:true }},
  480. // EXT_blend_minmax extension
  481. 'blendEquationEXT': {1: { 0:true }},
  482. // Multiple Render Targets
  483. 'drawBuffersWebGL': {1: {0: enumArrayToString, }}, // WEBGL_draw_bufers
  484. 'drawBuffers': {1: {0: enumArrayToString, }}, // WebGL2
  485. 'clearBufferfv': {
  486. 4: {0: true }, // WebGL2
  487. 5: {0: true }, // WebGL2
  488. },
  489. 'clearBufferiv': {
  490. 4: {0: true }, // WebGL2
  491. 5: {0: true }, // WebGL2
  492. },
  493. 'clearBufferuiv': {
  494. 4: {0: true }, // WebGL2
  495. 5: {0: true }, // WebGL2
  496. },
  497. 'clearBufferfi': { 4: {0: true}}, // WebGL2
  498. // QueryObjects
  499. 'beginQuery': { 2: { 0: true }}, // WebGL2
  500. 'endQuery': { 1: { 0: true }}, // WebGL2
  501. 'getQuery': { 2: { 0: true, 1: true }}, // WebGL2
  502. 'getQueryParameter': { 2: { 1: true }}, // WebGL2
  503. // Sampler Objects
  504. 'samplerParameteri': { 3: { 1: true }}, // WebGL2
  505. 'samplerParameterf': { 3: { 1: true }}, // WebGL2
  506. 'getSamplerParameter': { 2: { 1: true }}, // WebGL2
  507. // Sync objects
  508. 'clientWaitSync': { 3: { 1: makeBitFieldToStringFunc(['SYNC_FLUSH_COMMANDS_BIT']) }}, // WebGL2
  509. 'fenceSync': { 2: { 0: true }}, // WebGL2
  510. 'getSyncParameter': { 2: { 1: true }}, // WebGL2
  511. // Transform Feedback
  512. 'bindTransformFeedback': { 2: { 0: true }}, // WebGL2
  513. 'beginTransformFeedback': { 1: { 0: true }}, // WebGL2
  514. // Uniform Buffer Objects and Transform Feedback Buffers
  515. 'bindBufferBase': { 3: { 0: true }}, // WebGL2
  516. 'bindBufferRange': { 5: { 0: true }}, // WebGL2
  517. 'getIndexedParameter': { 2: { 0: true }}, // WebGL2
  518. 'getActiveUniforms': { 3: { 2: true }}, // WebGL2
  519. 'getActiveUniformBlockParameter': { 3: { 2: true }}, // WebGL2
  520. };
  521. /**
  522. * Gets an string version of an WebGL enum.
  523. *
  524. * Example:
  525. * var str = WebGLDebugUtil.glEnumToString(ctx.getError());
  526. *
  527. * @param {number} value Value to return an enum for
  528. * @return {string} The string version of the enum.
  529. */
  530. function glEnumToString(value) {
  531. var name = glEnums[value];
  532. return (name !== undefined) ? ('gl.' + name) :
  533. ('/*UNKNOWN WebGL ENUM*/ 0x' + value.toString(16) + '');
  534. }
  535. /**
  536. * Returns the string version of a WebGL argument.
  537. * Attempts to convert enum arguments to strings.
  538. * @param {string} functionName the name of the WebGL function.
  539. * @param {number} numArgs the number of arguments passed to the function.
  540. * @param {number} argumentIndx the index of the argument.
  541. * @param {*} value The value of the argument.
  542. * @return {string} The value as a string.
  543. */
  544. function glFunctionArgToString(functionName, numArgs, argumentIndex, value) {
  545. var funcInfos = glValidEnumContexts[functionName];
  546. if (funcInfos !== undefined) {
  547. var funcInfo = funcInfos[numArgs];
  548. if (funcInfo !== undefined) {
  549. var argType = funcInfo[argumentIndex];
  550. if (argType) {
  551. if (typeof argType === 'function') {
  552. return argType(value);
  553. } else {
  554. return glEnumToString(value);
  555. }
  556. }
  557. }
  558. }
  559. if (value === null) {
  560. return 'null';
  561. } else if (value === undefined) {
  562. return 'undefined';
  563. } else {
  564. return value.toString();
  565. }
  566. }
  567. /**
  568. * Converts the arguments of a WebGL function to a string.
  569. * Attempts to convert enum arguments to strings.
  570. *
  571. * @param {string} functionName the name of the WebGL function.
  572. * @param {number} args The arguments.
  573. * @return {string} The arguments as a string.
  574. */
  575. function glFunctionArgsToString(functionName, args) {
  576. // apparently we can't do args.join(",");
  577. var argStrs = [];
  578. var numArgs = args.length;
  579. for (var ii = 0; ii < numArgs; ++ii) {
  580. argStrs.push(glFunctionArgToString(functionName, numArgs, ii, args[ii]));
  581. }
  582. return argStrs.join(', ');
  583. }
  584. function makePropertyWrapper(wrapper, original, propertyName) {
  585. wrapper.__defineGetter__(propertyName, function() { // eslint-disable-line
  586. return original[propertyName];
  587. });
  588. // TODO(gmane): this needs to handle properties that take more than
  589. // one value?
  590. wrapper.__defineSetter__(propertyName, function(value) { // eslint-disable-line
  591. original[propertyName] = value;
  592. });
  593. }
  594. /**
  595. * Given a WebGL context returns a wrapped context that calls
  596. * gl.getError after every command and calls a function if the
  597. * result is not gl.NO_ERROR.
  598. *
  599. * @param {!WebGLRenderingContext} ctx The webgl context to
  600. * wrap.
  601. * @param {!function(err, funcName, args): void} opt_onErrorFunc
  602. * The function to call when gl.getError returns an
  603. * error. If not specified the default function calls
  604. * console.log with a message.
  605. * @param {!function(funcName, args): void} opt_onFunc The
  606. * function to call when each webgl function is called.
  607. * You can use this to log all calls for example.
  608. * @param {!WebGLRenderingContext} opt_err_ctx The webgl context
  609. * to call getError on if different than ctx.
  610. */
  611. function makeDebugContext(ctx, options) {
  612. options = options || {};
  613. var errCtx = options.errCtx || ctx;
  614. var onFunc = options.funcFunc;
  615. var sharedState = options.sharedState || {
  616. numDrawCallsRemaining: options.maxDrawCalls || -1,
  617. wrappers: {},
  618. };
  619. options.sharedState = sharedState;
  620. var errorFunc = options.errorFunc || function(err, functionName, args) {
  621. console.error("WebGL error " + glEnumToString(err) + " in " + functionName + // eslint-disable-line
  622. '(' + glFunctionArgsToString(functionName, args) + ')');
  623. };
  624. // Holds booleans for each GL error so after we get the error ourselves
  625. // we can still return it to the client app.
  626. var glErrorShadow = { };
  627. var wrapper = {};
  628. function removeChecks() {
  629. Object.keys(sharedState.wrappers).forEach(function(name) {
  630. var pair = sharedState.wrappers[name];
  631. var wrapper = pair.wrapper;
  632. var orig = pair.orig;
  633. for (var propertyName in wrapper) {
  634. if (typeof wrapper[propertyName] === 'function') {
  635. wrapper[propertyName] = orig[propertyName].bind(orig);
  636. }
  637. }
  638. });
  639. }
  640. function checkMaxDrawCalls() {
  641. if (sharedState.numDrawCallsRemaining === 0) {
  642. removeChecks();
  643. }
  644. --sharedState.numDrawCallsRemaining;
  645. }
  646. function noop() {
  647. }
  648. // Makes a function that calls a WebGL function and then calls getError.
  649. function makeErrorWrapper(ctx, functionName) {
  650. var check = functionName.substring(0, 4) === 'draw' ? checkMaxDrawCalls : noop;
  651. return function() {
  652. if (onFunc) {
  653. onFunc(functionName, arguments);
  654. }
  655. var result = ctx[functionName].apply(ctx, arguments);
  656. var err = errCtx.getError();
  657. if (err !== 0) {
  658. glErrorShadow[err] = true;
  659. errorFunc(err, functionName, arguments);
  660. }
  661. check();
  662. return result;
  663. };
  664. }
  665. function makeGetExtensionWrapper(ctx, wrapped) {
  666. return function() {
  667. var extensionName = arguments[0];
  668. var ext = sharedState.wrappers[extensionName];
  669. if (!ext) {
  670. ext = wrapped.apply(ctx, arguments);
  671. if (ext) {
  672. var origExt = ext;
  673. ext = makeDebugContext(ext, options);
  674. sharedState.wrappers[extensionName] = { wrapper: ext, orig: origExt };
  675. addEnumsForContext(origExt, extensionName);
  676. }
  677. }
  678. return ext;
  679. };
  680. }
  681. // Make a an object that has a copy of every property of the WebGL context
  682. // but wraps all functions.
  683. for (var propertyName in ctx) {
  684. if (typeof ctx[propertyName] === 'function') {
  685. if (propertyName !== 'getExtension') {
  686. wrapper[propertyName] = makeErrorWrapper(ctx, propertyName);
  687. } else {
  688. var wrapped = makeErrorWrapper(ctx, propertyName);
  689. wrapper[propertyName] = makeGetExtensionWrapper(ctx, wrapped);
  690. }
  691. } else {
  692. makePropertyWrapper(wrapper, ctx, propertyName);
  693. }
  694. }
  695. // Override the getError function with one that returns our saved results.
  696. if (wrapper.getError) {
  697. wrapper.getError = function() {
  698. for (var err in glErrorShadow) {
  699. if (glErrorShadow.hasOwnProperty(err)) {
  700. if (glErrorShadow[err]) {
  701. glErrorShadow[err] = false;
  702. return err;
  703. }
  704. }
  705. }
  706. return ctx.NO_ERROR;
  707. };
  708. }
  709. if (wrapper.bindBuffer) {
  710. sharedState.wrappers['webgl'] = { wrapper: wrapper, orig: ctx };
  711. addEnumsForContext(ctx, ctx.bindBufferBase ? 'WebGL2' : 'WebGL');
  712. }
  713. return wrapper;
  714. }
  715. //------------
  716. function captureJSErrors() {
  717. // capture JavaScript Errors
  718. window.addEventListener('error', function(e) {
  719. var msg = e.message || e.error;
  720. var url = e.filename;
  721. var lineNo = e.lineno || 1;
  722. var colNo = e.colno || 1;
  723. var isUserScript = (url === window.location.href);
  724. if (isUserScript) {
  725. try {
  726. lineNo = window.parent.getActualLineNumberAndMoveTo(lineNo, colNo);
  727. } catch (ex) {
  728. origConsole.error(ex);
  729. }
  730. }
  731. console.error("line:", lineNo, ":", msg); // eslint-disable-line
  732. origConsole.error(e.error);
  733. });
  734. }
  735. // adapted from http://stackoverflow.com/a/2401861/128511
  736. function getBrowser() {
  737. var userAgent = navigator.userAgent;
  738. var m = userAgent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
  739. if (/trident/i.test(m[1])) {
  740. m = /\brv[ :]+(\d+)/g.exec(userAgent) || [];
  741. return {
  742. name: 'IE',
  743. version: m[1],
  744. };
  745. }
  746. if (m[1] === 'Chrome') {
  747. var temp = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
  748. if (temp) {
  749. return {
  750. name: temp[1].replace('OPR', 'Opera'),
  751. version: temp[2],
  752. };
  753. }
  754. }
  755. m = m[2] ? [m[1], m[2]] : [navigator.appName, navigator.appVersion, '-?'];
  756. var version = userAgent.match(/version\/(\d+)/i);
  757. if (version) {
  758. m.splice(1, 1, version[1]);
  759. }
  760. return {
  761. name: m[0],
  762. version: m[1],
  763. };
  764. }
  765. var isWebGLRE = /^(webgl|webgl2|experimental-webgl)$/i;
  766. function installWebGLLessonSetup() {
  767. HTMLCanvasElement.prototype.getContext = (function(oldFn) {
  768. return function() {
  769. var type = arguments[0];
  770. var isWebGL = isWebGLRE.test(type);
  771. if (isWebGL) {
  772. setupLesson(this);
  773. }
  774. var ctx = oldFn.apply(this, arguments);
  775. if (!ctx && isWebGL) {
  776. showNeedWebGL(this);
  777. }
  778. return ctx;
  779. };
  780. }(HTMLCanvasElement.prototype.getContext));
  781. }
  782. function installWebGLDebugContextCreator() {
  783. // capture GL errors
  784. HTMLCanvasElement.prototype.getContext = (function(oldFn) {
  785. return function() {
  786. var ctx = oldFn.apply(this, arguments);
  787. // Using bindTexture to see if it's WebGL. Could check for instanceof WebGLRenderingContext
  788. // but that might fail if wrapped by debugging extension
  789. if (ctx && ctx.bindTexture) {
  790. ctx = makeDebugContext(ctx, {
  791. maxDrawCalls: 100,
  792. errorFunc: function(err, funcName, args) {
  793. var numArgs = args.length;
  794. var enumedArgs = [].map.call(args, function(arg, ndx) {
  795. var str = glFunctionArgToString(funcName, numArgs, ndx, arg);
  796. // shorten because of long arrays
  797. if (str.length > 200) {
  798. str = str.substring(0, 200) + '...';
  799. }
  800. return str;
  801. });
  802. var browser = getBrowser();
  803. var lineNdx;
  804. var matcher;
  805. if ((/chrome|opera/i).test(browser.name)) {
  806. lineNdx = 3;
  807. matcher = function(line) {
  808. var m = /at ([^(]+)*\(*(.*?):(\d+):(\d+)/.exec(line);
  809. if (m) {
  810. var userFnName = m[1];
  811. var url = m[2];
  812. var lineNo = parseInt(m[3]);
  813. var colNo = parseInt(m[4]);
  814. if (url === '') {
  815. url = userFnName;
  816. userFnName = '';
  817. }
  818. return {
  819. url: url,
  820. lineNo: lineNo,
  821. colNo: colNo,
  822. funcName: userFnName,
  823. };
  824. }
  825. return undefined;
  826. };
  827. } else if ((/firefox|safari/i).test(browser.name)) {
  828. lineNdx = 2;
  829. matcher = function(line) {
  830. var m = /@(.*?):(\d+):(\d+)/.exec(line);
  831. if (m) {
  832. var url = m[1];
  833. var lineNo = parseInt(m[2]);
  834. var colNo = parseInt(m[3]);
  835. return {
  836. url: url,
  837. lineNo: lineNo,
  838. colNo: colNo,
  839. };
  840. }
  841. return undefined;
  842. };
  843. }
  844. var lineInfo = '';
  845. if (matcher) {
  846. try {
  847. var error = new Error();
  848. var lines = error.stack.split('\n');
  849. // window.fooLines = lines;
  850. // lines.forEach(function(line, ndx) {
  851. // origConsole.log("#", ndx, line);
  852. // });
  853. var info = matcher(lines[lineNdx]);
  854. if (info) {
  855. var lineNo = info.lineNo;
  856. var colNo = info.colNo;
  857. var url = info.url;
  858. var isUserScript = (url === window.location.href);
  859. if (isUserScript) {
  860. lineNo = window.parent.getActualLineNumberAndMoveTo(lineNo, colNo);
  861. }
  862. lineInfo = ' line:' + lineNo + ':' + colNo;
  863. }
  864. } catch (e) {
  865. origConsole.error(e);
  866. }
  867. }
  868. console.error( // eslint-disable-line
  869. 'WebGL error' + lineInfo, glEnumToString(err), 'in',
  870. funcName, '(', enumedArgs.join(', '), ')');
  871. },
  872. });
  873. }
  874. return ctx;
  875. };
  876. }(HTMLCanvasElement.prototype.getContext));
  877. }
  878. installWebGLLessonSetup();
  879. if (isInEditor()) {
  880. setupConsole();
  881. captureJSErrors();
  882. if (window.threejsLessonSettings === undefined || window.threejsLessonSettings.glDebug !== false) {
  883. installWebGLDebugContextCreator();
  884. }
  885. }
  886. return {
  887. setupLesson: setupLesson,
  888. showNeedWebGL: showNeedWebGL,
  889. makeDebugContext: makeDebugContext,
  890. glFunctionArgsToString: glFunctionArgsToString,
  891. };
  892. }));