debugging-javascript.html 28 KB

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