threejs-lessons-helper.js 32 KB

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