debugging-javascript.html 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Debugging JavaScript</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – Debugging JavaScript">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../../build/three.module.js"
  20. }
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <div class="container">
  26. <div class="lesson-title">
  27. <h1>Debugging JavaScript</h1>
  28. </div>
  29. <div class="lesson">
  30. <div class="lesson-main">
  31. <p>Most of this article is not directly about THREE.js but is
  32. rather about debugging JavaScript in general. It seemed important in
  33. that many people just starting with THREE.js are also just
  34. starting with JavaScript so I hope this can help them more easily
  35. solve any issues they run into.</p>
  36. <p>Debugging is a big topic and I probably can't begin to cover
  37. everything there is to know but if you're new to JavaScript
  38. then here's an attempt to give a few pointers. I strongly
  39. suggest you take some time to learn them. They'll help you
  40. enormously in your learning.</p>
  41. <h2 id="learn-your-browser-s-developer-tools">Learn your Browser's Developer Tools</h2>
  42. <p>All browsers have developer tools.
  43. <a href="https://developers.google.com/web/tools/chrome-devtools/">Chrome</a>,
  44. <a href="https://developer.mozilla.org/en-US/docs/Tools">Firefox</a>,
  45. <a href="https://developer.apple.com/safari/tools/">Safari</a>,
  46. <a href="https://docs.microsoft.com/en-us/microsoft-edge/devtools-guide">Edge</a>.</p>
  47. <p>In Chrome you can click the the <code class="notranslate" translate="no">⋮</code> icon, pick More Tools-&gt;Developer Tools
  48. to get to the developer tools. A keyboard shortcut is also shown there.</p>
  49. <div class="threejs_center"><img class="border" src="../resources/images/devtools-chrome.jpg" style="width: 789px;"></div>
  50. <p>In Firefox you click the <code class="notranslate" translate="no">☰</code> icon, pick "Web Developer", then pick
  51. "Toggle Tools"</p>
  52. <div class="threejs_center"><img class="border" src="../resources/images/devtools-firefox.jpg" style="width: 786px;"></div>
  53. <p>In Safari you first have to enable the Develop menu from the
  54. Advanced Safari Preferences.</p>
  55. <div class="threejs_center"><img class="border" src="../resources/images/devtools-enable-safari.jpg" style="width: 775px;"></div>
  56. <p>Then in the Develop menu you can pick "Show/Connect Web Inspector".</p>
  57. <div class="threejs_center"><img class="border" src="../resources/images/devtools-safari.jpg" style="width: 777px;"></div>
  58. <p>With Chrome you can also <a href="https://developers.google.com/web/tools/chrome-devtools/remote-debugging/">use Chrome on your computer to debug webpages running on Chrome on your Android phone or tablet</a>.
  59. Similarly with Safari you can
  60. <a href="https://www.google.com/search?q=safari+remote+debugging+ios">use your computer to debug webpages running on Safari on iPhones and iPads</a>.</p>
  61. <p>I'm most familiar with Chrome so this guide will be using Chrome
  62. as an example when referring to tools but most browsers have similar
  63. features so it should be easy to apply anything here to all browsers.</p>
  64. <h2 id="turn-off-the-cache">Turn off the cache</h2>
  65. <p>Browsers try to reuse data they've already downloaded. This is great
  66. for users so if you visit a website a second time many of the files
  67. used to display the site will not have be downloaded again.</p>
  68. <p>On the other hand this can be bad for web development. You change
  69. a file on your computer, reload the page, and you don't see the changes
  70. because the browser uses the version it got last time.</p>
  71. <p>One solution during web development is to turn off the cache. This
  72. way the browser will always get the newest versions of your files.</p>
  73. <p>First pick settings from the corner menu</p>
  74. <div class="threejs_center"><img class="border" src="../resources/images/devtools-chrome-settings.jpg" style="width: 778px"></div>
  75. <p>Then pick "Disable Cache (while DevTools is open)".</p>
  76. <div class="threejs_center"><img class="border" src="../resources/images/devtools-chrome-disable-cache.jpg" style="width: 779px"></div>
  77. <h2 id="use-the-javascript-console">Use the JavaScript console</h2>
  78. <p>Inside all devtools is a <em>console</em>. It shows warnings and error messages.</p>
  79. <p><strong> READ THE MESSAGES!! </strong></p>
  80. <p>Typically there should be only 1 or 2 messages.</p>
  81. <div class="threejs_center"><img class="border" src="../resources/images/devtools-no-errors.jpg" style="width: 779px"></div>
  82. <p>If you see any others <strong>READ THEM</strong>. For example:</p>
  83. <div class="threejs_center"><img class="border" src="../resources/images/devtools-errors.jpg" style="width: 779px"></div>
  84. <p>I mis-spelled "three" as "threee"</p>
  85. <p>You can also print your own info to the console with with <code class="notranslate" translate="no">console.log</code> as in</p>
  86. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">console.log(someObject.position.x, someObject.position.y, someObject.position.z);
  87. </pre>
  88. <p>Even cooler, if you log an object you can inspect it. For example if we log
  89. the root scene object from <a href="load-gltf.html">the gLTF article</a></p>
  90. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> {
  91. const gltfLoader = new GLTFLoader();
  92. gltfLoader.load('resources/models/cartoon_lowpoly_small_city_free_pack/scene.gltf', (gltf) =&gt; {
  93. const root = gltf.scene;
  94. scene.add(root);
  95. + console.log(root);
  96. </pre>
  97. <p>Then we can expand that object in the JavaScript console</p>
  98. <div class="threejs_center"><img class="border" src="../resources/images/devtools-console-object.gif"></div>
  99. <p>You can also use <code class="notranslate" translate="no">console.error</code> which reports the message in red
  100. in includes a stack trace.</p>
  101. <h2 id="put-data-on-screen">Put data on screen</h2>
  102. <p>Another obvious but often overlooked way is to add <code class="notranslate" translate="no">&lt;div&gt;</code> or <code class="notranslate" translate="no">&lt;pre&gt;</code> tags
  103. and put data in them.</p>
  104. <p>The most obvious way is to make some HTML elements</p>
  105. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;canvas id="c"&gt;&lt;/canvas&gt;
  106. +&lt;div id="debug"&gt;
  107. + &lt;div&gt;x:&lt;span id="x"&gt;&lt;/span&gt;&lt;/div&gt;
  108. + &lt;div&gt;y:&lt;span id="y"&gt;&lt;/span&gt;&lt;/div&gt;
  109. + &lt;div&gt;z:&lt;span id="z"&gt;&lt;/span&gt;&lt;/div&gt;
  110. +&lt;/div&gt;
  111. </pre>
  112. <p>Style them so they stay on top of the canvas. (assuming your canvas
  113. fills the page)</p>
  114. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;style&gt;
  115. #debug {
  116. position: absolute;
  117. left: 1em;
  118. top: 1em;
  119. padding: 1em;
  120. background: rgba(0, 0, 0, 0.8);
  121. color: white;
  122. font-family: monospace;
  123. }
  124. &lt;/style&gt;
  125. </pre>
  126. <p>And then looking the elements up and setting their content.</p>
  127. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">// at init time
  128. const xElem = document.querySelector('#x');
  129. const yElem = document.querySelector('#y');
  130. const zElem = document.querySelector('#z');
  131. // at render or update time
  132. xElem.textContent = someObject.position.x.toFixed(3);
  133. yElem.textContent = someObject.position.y.toFixed(3);
  134. zElem.textContent = someObject.position.z.toFixed(3);
  135. </pre>
  136. <p>This is more useful for real time values</p>
  137. <p></p><div translate="no" class="threejs_example_container notranslate">
  138. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/debug-js-html-elements.html"></iframe></div>
  139. <a class="threejs_center" href="/manual/examples/debug-js-html-elements.html" target="_blank">click here to open in a separate window</a>
  140. </div>
  141. <p></p>
  142. <p>Another way to put data on the screen is to make a clearing logger.
  143. I just made that term up but lots of games I've worked on have used this solution. The idea
  144. is you have a buffer that displays messages for only one frame.
  145. Any part of your code that wants to display data calls some function
  146. to add data to that buffer every frame. This is much less work
  147. than making an element per piece of data above.</p>
  148. <p>For example let's change the HTML from above to just this</p>
  149. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;canvas id="c"&gt;&lt;/canvas&gt;
  150. &lt;div id="debug"&gt;
  151. &lt;pre&gt;&lt;/pre&gt;
  152. &lt;/div&gt;
  153. </pre>
  154. <p>And let's make simple class to manage this <em>clear back buffer</em>.</p>
  155. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ClearingLogger {
  156. constructor(elem) {
  157. this.elem = elem;
  158. this.lines = [];
  159. }
  160. log(...args) {
  161. this.lines.push([...args].join(' '));
  162. }
  163. render() {
  164. this.elem.textContent = this.lines.join('\n');
  165. this.lines = [];
  166. }
  167. }
  168. </pre>
  169. <p>Then let's make a simple example that every time we click the mouse makes a mesh
  170. that moves in a random direction for 2 seconds. We'll start with one of the
  171. examples from the article on <a href="responsive.html">making things responsive</a></p>
  172. <p>Here's the code that adds a new <a href="/docs/#api/en/objects/Mesh"><code class="notranslate" translate="no">Mesh</code></a> every time we click the mouse</p>
  173. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const geometry = new THREE.SphereGeometry();
  174. const material = new THREE.MeshBasicMaterial({color: 'red'});
  175. const things = [];
  176. function rand(min, max) {
  177. if (max === undefined) {
  178. max = min;
  179. min = 0;
  180. }
  181. return Math.random() * (max - min) + min;
  182. }
  183. function createThing() {
  184. const mesh = new THREE.Mesh(geometry, material);
  185. scene.add(mesh);
  186. things.push({
  187. mesh,
  188. timer: 2,
  189. velocity: new THREE.Vector3(rand(-5, 5), rand(-5, 5), rand(-5, 5)),
  190. });
  191. }
  192. canvas.addEventListener('click', createThing);
  193. </pre>
  194. <p>And here's the code that moves the meshes we created, logs them,
  195. and removes them when their timer has run out</p>
  196. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const logger = new ClearingLogger(document.querySelector('#debug pre'));
  197. let then = 0;
  198. function render(now) {
  199. now *= 0.001; // convert to seconds
  200. const deltaTime = now - then;
  201. then = now;
  202. ...
  203. logger.log('fps:', (1 / deltaTime).toFixed(1));
  204. logger.log('num things:', things.length);
  205. for (let i = 0; i &lt; things.length;) {
  206. const thing = things[i];
  207. const mesh = thing.mesh;
  208. const pos = mesh.position;
  209. logger.log(
  210. 'timer:', thing.timer.toFixed(3),
  211. 'pos:', pos.x.toFixed(3), pos.y.toFixed(3), pos.z.toFixed(3));
  212. thing.timer -= deltaTime;
  213. if (thing.timer &lt;= 0) {
  214. // remove this thing. Note we don't advance `i`
  215. things.splice(i, 1);
  216. scene.remove(mesh);
  217. } else {
  218. mesh.position.addScaledVector(thing.velocity, deltaTime);
  219. ++i;
  220. }
  221. }
  222. renderer.render(scene, camera);
  223. logger.render();
  224. requestAnimationFrame(render);
  225. }
  226. </pre>
  227. <p>Now click the mouse a bunch in the example below</p>
  228. <p></p><div translate="no" class="threejs_example_container notranslate">
  229. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/debug-js-clearing-logger.html"></iframe></div>
  230. <a class="threejs_center" href="/manual/examples/debug-js-clearing-logger.html" target="_blank">click here to open in a separate window</a>
  231. </div>
  232. <p></p>
  233. <h2 id="query-parameters">Query Parameters</h2>
  234. <p>Another thing to remember is that webpages can have data passed
  235. into them either via query parameters or the anchor, sometimes called
  236. the search and the hash.</p>
  237. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">https://domain/path/?query#anchor
  238. </pre><p>You can use this to make features optional or pass in parameters.</p>
  239. <p>For example let's take the previous example and make it so
  240. the debug stuff only shows up if we put <code class="notranslate" translate="no">?debug=true</code> in the URL.</p>
  241. <p>First we need some code to parse the query string</p>
  242. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">/**
  243. * Returns the query parameters as a key/value object.
  244. * Example: If the query parameters are
  245. *
  246. * abc=123&amp;def=456&amp;name=gman
  247. *
  248. * Then `getQuery()` will return an object like
  249. *
  250. * {
  251. * abc: '123',
  252. * def: '456',
  253. * name: 'gman',
  254. * }
  255. */
  256. function getQuery() {
  257. return Object.fromEntries(new URLSearchParams(window.location.search).entries());
  258. }
  259. </pre>
  260. <p>Then we might make the debug element not show by default</p>
  261. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;canvas id="c"&gt;&lt;/canvas&gt;
  262. +&lt;div id="debug" style="display: none;"&gt;
  263. &lt;pre&gt;&lt;/pre&gt;
  264. &lt;/div&gt;
  265. </pre>
  266. <p>Then in the code we read the params and choose to un-hide the
  267. debug info if and only if <code class="notranslate" translate="no">?debug=true</code> is passed in</p>
  268. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const query = getQuery();
  269. const debug = query.debug === 'true';
  270. const logger = debug
  271. ? new ClearingLogger(document.querySelector('#debug pre'))
  272. : new DummyLogger();
  273. if (debug) {
  274. document.querySelector('#debug').style.display = '';
  275. }
  276. </pre>
  277. <p>We also made a <code class="notranslate" translate="no">DummyLogger</code> that does nothing and chose to use it if <code class="notranslate" translate="no">?debug=true</code> has not been passed in.</p>
  278. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class DummyLogger {
  279. log() {}
  280. render() {}
  281. }
  282. </pre>
  283. <p>You can see if we use this url:</p>
  284. <p><a target="_blank" href="../examples/debug-js-params.html">debug-js-params.html</a></p>
  285. <p>there is no debug info but if we use this url:</p>
  286. <p><a target="_blank" href="../examples/debug-js-params.html?debug=true">debug-js-params.html?debug=true</a></p>
  287. <p>there is debug info.</p>
  288. <p>Multiple parameters can be passed in by separating with '&amp;' as in <code class="notranslate" translate="no">somepage.html?someparam=somevalue&amp;someotherparam=someothervalue</code>.
  289. Using parameters like this we can pass in all kinds of options. Maybe <code class="notranslate" translate="no">speed=0.01</code> to slow down our app for making it easier to understand something or <code class="notranslate" translate="no">showHelpers=true</code> for whether or not to add helpers
  290. that show the lights, shadow, or camera frustum seen in other lessons.</p>
  291. <h2 id="learn-to-use-the-debugger">Learn to use the Debugger</h2>
  292. <p>Every browser has a debugger where you can pause your program
  293. step through line by line and inspect all the variables.</p>
  294. <p>Teaching you how to use a debugger is too big a topic for this
  295. article but here's a few links</p>
  296. <ul>
  297. <li><a href="https://developers.google.com/web/tools/chrome-devtools/javascript/">Get Started with Debugging JavaScript in Chrome DevTools</a></li>
  298. <li><a href="https://javascript.info/debugging-chrome">Debugging in Chrome</a></li>
  299. <li><a href="https://hackernoon.com/tips-and-tricks-for-debugging-in-chrome-developer-tools-458ade27c7ab">Tips and Tricks for Debugging in Chrome Developer Tools</a></li>
  300. </ul>
  301. <h2 id="check-for-nan-in-the-debugger-or-elsewhere">Check for <code class="notranslate" translate="no">NaN</code> in the debugger or elsewhere</h2>
  302. <p><code class="notranslate" translate="no">NaN</code> is short for Not A Number. It's what JavaScript will assign
  303. as a value when you do something that doesn't make sense mathwise.</p>
  304. <p>As a simple example</p>
  305. <div class="threejs_center"><img class="border" src="../resources/images/nan-banana.png" style="width: 180px;"></div>
  306. <p>Often when I'm making something and nothing appears on the screen
  307. I'll check some values and if I see <code class="notranslate" translate="no">NaN</code> I will instantly have a
  308. place to start looking.</p>
  309. <p>As an example when I first started making the path for the
  310. <a href="load-gltf.html">article about loading gLTF files</a> I made
  311. a curve using the <a href="/docs/#api/en/extras/curves/SplineCurve"><code class="notranslate" translate="no">SplineCurve</code></a> class which makes a 2D curve.</p>
  312. <p>I then used that curve to move the cars like this</p>
  313. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">curve.getPointAt(zeroToOnePointOnCurve, car.position);
  314. </pre>
  315. <p>Internally <code class="notranslate" translate="no">curve.getPointAt</code> calls the <code class="notranslate" translate="no">set</code> function
  316. on the object passed as the second argument. In this case that
  317. second argument is <code class="notranslate" translate="no">car.position</code> which is a <a href="/docs/#api/en/math/Vector3"><code class="notranslate" translate="no">Vector3</code></a>. <a href="/docs/#api/en/math/Vector3"><code class="notranslate" translate="no">Vector3</code></a>'s
  318. <code class="notranslate" translate="no">set</code> function requires 3 arguments, x, y, and z but <a href="/docs/#api/en/extras/curves/SplineCurve"><code class="notranslate" translate="no">SplineCurve</code></a> is a 2D curve
  319. and so it calls <code class="notranslate" translate="no">car.position.set</code> with just x and y.</p>
  320. <p>The result is that <code class="notranslate" translate="no">car.position.set</code> sets x to x, y to y, and z to <code class="notranslate" translate="no">undefined</code>.</p>
  321. <p>A quick glance in the debugger looking at the car's <code class="notranslate" translate="no">matrixWorld</code>
  322. showed a bunch of <code class="notranslate" translate="no">NaN</code> values.</p>
  323. <div class="threejs_center"><img class="border" src="../resources/images/debugging-nan.gif" style="width: 476px;"></div>
  324. <p>Seeing the matrix had <code class="notranslate" translate="no">NaN</code>s in it suggested something like <code class="notranslate" translate="no">position</code>,
  325. <code class="notranslate" translate="no">rotation</code>, <code class="notranslate" translate="no">scale</code> or some other function that affects that matrix had bad
  326. data. Working backward from their it was easy to track down the issue.</p>
  327. <p>In top of <code class="notranslate" translate="no">NaN</code> there's also <code class="notranslate" translate="no">Infinity</code> which is a similar sign there
  328. is a math bug somewhere.</p>
  329. <h2 id="look-in-the-code-">Look In the Code!</h2>
  330. <p>THREE.js is Open Source. Don't be afraid to look inside the code!
  331. You can look inside on <a href="https://github.com/mrdoob/three.js">github</a>.
  332. You can also look inside by stepping into functions in the debugger.</p>
  333. <h2 id="put-requestanimationframe-at-bottom-of-your-render-function-">Put <code class="notranslate" translate="no">requestAnimationFrame</code> at bottom of your render function.</h2>
  334. <p>I see this pattern often</p>
  335. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render() {
  336. requestAnimationFrame(render);
  337. // -- do stuff --
  338. renderer.render(scene, camera);
  339. }
  340. requestAnimationFrame(render);
  341. </pre>
  342. <p>I'd suggest that putting the call to <code class="notranslate" translate="no">requestAnimationFrame</code> at
  343. the bottom as in</p>
  344. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render() {
  345. // -- do stuff --
  346. renderer.render(scene, camera);
  347. requestAnimationFrame(render);
  348. }
  349. requestAnimationFrame(render);
  350. </pre>
  351. <p>The biggest reason is it means your code will stop if you have an error. Putting
  352. <code class="notranslate" translate="no">requestAnimationFrame</code> at the top means your code will keep running even if you
  353. have an error since you already requested another frame. IMO it's better to find
  354. those errors than to ignore them. They could easily be the reason something is
  355. not appearing as you expect it to but unless your code stops you might not even
  356. notice.</p>
  357. <h2 id="check-your-units-">Check your units!</h2>
  358. <p>This basically means knowing for example when to use degrees vs
  359. when to use radians. It's unfortunate that THREE.js does not
  360. consistently use the same units everywhere. Off the top of my head
  361. the camera's field of view is in degrees. All other angles are in
  362. radians.</p>
  363. <p>The other place to look out is your world unit size. Until
  364. recently 3D apps could choose any unit size they wanted. One app might choose
  365. 1 unit = 1cm. Another might choose 1 unit = 1 foot. It's actually still
  366. true that you can chose any units you want for certain applications.
  367. That said, THREE.js assumes 1 unit = 1 meter. This is important for
  368. things like physically based rendering which uses meters to compute
  369. lighting effects. It's also important for AR and VR which need to
  370. deal with real world units like where your phone is or where the VR
  371. controllers are.</p>
  372. <h2 id="making-a-minimal-complete-verifiable-example-for-stack-overflow">Making a <em>Minimal, Complete, Verifiable, Example</em> for Stack Overflow</h2>
  373. <p>If you decide to ask a question about THREE.js it's almost always
  374. required for you to provide an MCVE which stands for Minimal, Complete,
  375. Verifiable, Example.</p>
  376. <p>The <strong>Minimal</strong> part is important. Let's say you where having an issue with the
  377. path movement in the last example of the <a href="load-gltf.html">loading a gLTF
  378. article</a>. That example has many parts. Listing them out
  379. it has</p>
  380. <ol>
  381. <li>A bunch of HTML</li>
  382. <li>Some CSS</li>
  383. <li>Lights</li>
  384. <li>Shadows</li>
  385. <li>lil-gui code to manipulate shadows</li>
  386. <li>Code to load a .GLTF file</li>
  387. <li>Code to resize the canvas.</li>
  388. <li>Code to move the cars along paths</li>
  389. </ol>
  390. <p>That's pretty huge. If your question is only about the path following part you
  391. can remove most of the HTML as you only need a <code class="notranslate" translate="no">&lt;canvas&gt;</code> and a <code class="notranslate" translate="no">&lt;script&gt;</code> tag
  392. for THREE.js. You can remove the CSS and the resizing code. You can remove .GLTF
  393. code because you only care about the path. You can remove the lights and the
  394. shadows by using a <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a>. You can certainly remove the lil-gui
  395. code. The code makes a ground plane with a texture. It would be easier to use a
  396. <a href="/docs/#api/en/helpers/GridHelper"><code class="notranslate" translate="no">GridHelper</code></a>. Finally if our question is about moving things on a path we could
  397. just use cubes on the path instead of loaded car models.</p>
  398. <p>Here's a more minimal example taking all the above into account. It
  399. shrunk from 271 lines to 135. We might consider shrinking it even
  400. more by simplifying our path. Maybe a path with 3 or 4 points would
  401. work just as well as our path with 21 points.</p>
  402. <p></p><div translate="no" class="threejs_example_container notranslate">
  403. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/debugging-mcve.html"></iframe></div>
  404. <a class="threejs_center" href="/manual/examples/debugging-mcve.html" target="_blank">click here to open in a separate window</a>
  405. </div>
  406. <p></p>
  407. <p>I kept the <code class="notranslate" translate="no">OrbitController</code> just because it's useful for others
  408. to move the camera and figure out what's going on but depending
  409. on your issue you might be able to remove that as well.</p>
  410. <p>The best thing about making an MCVE is we'll often solve our own
  411. problem. The process of removing everything that's not needed and
  412. making the smallest example we can that reproduces the issue more
  413. often than not leads us to our bug.</p>
  414. <p>On top of that it's respectful of all the people's time who you are
  415. asking to look at your code on Stack Overflow. By making the minimal
  416. example you make it much easier for them to help you. You'll also
  417. learn in the process.</p>
  418. <p>Also important, when you go to Stack Overflow to post your question <strong>put your
  419. code <a href="https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/">in a snippet</a>.</strong>
  420. Of course you are welcome to use JSFiddle or Codepen or similar site to test out
  421. your MCVE but once you actually get to posting your question on Stack Overflow
  422. you're required to put the code to reproduce your issue <strong>in the question itself</strong>.
  423. By making a snippet you satisfy that requirement.</p>
  424. <p>Also note all the live examples on this site should run as snippets.
  425. Just copy the HTML, CSS, and JavaScript parts to their respective
  426. parts of the <a href="https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/">snippet editor</a>.
  427. Just remember to try to remove the parts that are not relevant to
  428. your issue and try to make your code the minimal amount needed.</p>
  429. <p>Follow these suggestions and you're far more likely to get help
  430. with your issue.</p>
  431. <h2 id="use-a-meshbasicmaterial-">Use a <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a></h2>
  432. <p>Because the <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a> uses no lights this is one way to
  433. remove reasons something might not be showing up. If your objects
  434. show up using <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a> but not with whatever materials
  435. you were using then you know the issue is likely with the materials
  436. or the lights and not some other part of the code.</p>
  437. <h2 id="check-your-near-and-far-settings-for-your-camera">Check your <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code> settings for your camera</h2>
  438. <p>A <a href="/docs/#api/en/cameras/PerspectiveCamera"><code class="notranslate" translate="no">PerspectiveCamera</code></a> has <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code> settings which are covered in the
  439. <a href="cameras.html">article on cameras</a>. Make sure they are set to fit the
  440. space that contains your objects. Maybe even just <strong>temporarily</strong> set them to
  441. something large like <code class="notranslate" translate="no">near</code> = 0.001 and <code class="notranslate" translate="no">far</code> = 1000000. You will likely run
  442. into depth resolution issues but you'll at least be able to see your objects
  443. provided they are in front of the camera.</p>
  444. <h2 id="check-your-scene-is-in-front-of-the-camera">Check your scene is in front of the camera</h2>
  445. <p>Sometimes things don't appear because they are not in front of the camera. If
  446. your camera is not controllable try adding camera control like the
  447. <code class="notranslate" translate="no">OrbitController</code> so you can look around and find your scene. Or, try framing
  448. the scene using code which is covered in <a href="load-obj.html">this article</a>.
  449. That code finds the size of part of the scene and then moves the camera and
  450. adjusts the <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code> settings to make it visible. You can then look in
  451. the debugger or add some <code class="notranslate" translate="no">console.log</code> messages to print the size and center of
  452. the scene.</p>
  453. <h2 id="put-something-in-front-of-the-camera">Put something in front of the camera</h2>
  454. <p>This is just another way of saying if all else fails start with
  455. something that works and then slowly add stuff back in. If you get
  456. a screen with nothing on it then try putting something directly in
  457. front of the camera. Make a sphere or box, give it a simple material
  458. like the <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a> and make sure you can get that on the screen.
  459. Then start adding things back a little at time and testing. Eventually
  460. you'll either reproduce your bug or you'll find it on the way.</p>
  461. <hr>
  462. <p>These were a few tips for debugging JavaScript. Let's also go
  463. over <a href="debugging-glsl.html">some tips for debugging GLSL</a>.</p>
  464. </div>
  465. </div>
  466. </div>
  467. <script src="../resources/prettify.js"></script>
  468. <script src="../resources/lesson.js"></script>
  469. </body></html>