threejs-lessons-helper.js 31 KB

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