-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
/
Copy pathdebugging-javascript.html
530 lines (487 loc) · 29.5 KB
/
debugging-javascript.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>调试 JavaScript</title>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@threejs">
<meta name="twitter:title" content="Three.js – Debugging JavaScript">
<meta property="og:image" content="https://threejs.org/files/share.png">
<link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
<link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
<link rel="stylesheet" href="../resources/lesson.css">
<link rel="stylesheet" href="../resources/lang.css">
<script type="importmap">
{
"imports": {
"three": "../../build/three.module.js"
}
}
</script>
</head>
<body>
<div class="container">
<div class="lesson-title">
<h1>调试 JavaScript</h1>
</div>
<div class="lesson">
<div class="lesson-main">
<p>
总的来说这篇文章的大部分是关于如何调试JavaScript的,而不是直接关于THREE.js的。这似乎很重要,因为很多刚开始学习THREE.js的人同时也刚开始学JavaScript,所以我希望这可以帮助他们更轻松地解决遇到的任何问题。
</p>
<p>调试是一个大话题,我可能无法介绍出所有需要了解的内容,但如果您是JavaScript新手,我在这里会尝试给您一些建议。我强烈建议你花点时间来学习它们。它们会对你的学习有很大帮助。</p>
<h2 id="learn-your-browser-s-developer-tools">了解你的浏览器开发者工具</h2>
<p>所有的浏览器都有开发者工具。
<a href="https://developers.google.com/web/tools/chrome-devtools/">Chrome</a>,
<a href="https://developer.mozilla.org/en-US/docs/Tools">Firefox</a>,
<a href="https://developer.apple.com/safari/tools/">Safari</a>,
<a href="https://docs.microsoft.com/en-us/microsoft-edge/devtools-guide">Edge</a>.
</p>
<p>在Chrome浏览器中你可以点击<code class="notranslate" translate="no">⋮</code>标志, 选择 More
Tools->Developer Tools
来打开开发者工具。快捷键也显示在了上面。</p>
<div class="threejs_center"><img class="border" src="../resources/images/devtools-chrome.jpg"
style="width: 789px;"></div>
<p>在Firefox中你可以点击<code class="notranslate" translate="no">☰</code> 标志, 选择 "Web Developer", 接着选择
"Toggle Tools"</p>
<div class="threejs_center"><img class="border" src="../resources/images/devtools-firefox.jpg"
style="width: 786px;"></div>
<p>在Safari中你首先要从Advanced Safari Preferences中打开 Develop menu。</p>
<div class="threejs_center"><img class="border" src="../resources/images/devtools-enable-safari.jpg"
style="width: 775px;"></div>
<p>接着在Develop菜单你可以选择"Show/Connect Web Inspector"。</p>
<div class="threejs_center"><img class="border" src="../resources/images/devtools-safari.jpg"
style="width: 777px;"></div>
<p>在Chrome中你也可以<a
href="https://developers.google.com/web/tools/chrome-devtools/remote-debugging/">使用电脑端的开发者工具来调试运行在安卓手机或者平板的chrome浏览器上的网页</a>.
同样的在Safari中你可以
<a
href="https://www.google.com/search?q=safari+remote+debugging+ios">使用电脑端调试iPhones和iPads的Safari上的网页</a>.
</p>
<p>我对Chrome最为熟悉,因此本指南在提到开发者工具时将以Chrome为例,但大多数浏览器都有类似的功能,因此应该很容易将此处的任何内容应用于所有浏览器。</p>
<h2 id="turn-off-the-cache">关闭缓存</h2>
<p>浏览器试图重用他们已经下载的数据。这对用户来说非常好,因此,如果您再次访问网站,许多用于显示网站的文件将不会被再次下载。</p>
<p>但是这可能不利于web开发。如果您改变了加载的资源,并重新加载了页面,但由于浏览器使用了上次缓存的版本,因此可能会看不到变动。</p>
<p>在开发过程中一个解决方案是关闭缓存。这样,浏览器将始终获取文件的最新版本。</p>
<p>首先在右上角点击设置按钮</p>
<div class="threejs_center"><img class="border" src="../resources/images/devtools-chrome-settings.jpg"
style="width: 778px"></div>
<p>接着选择"Disable Cache (while DevTools is open)".</p>
<div class="threejs_center"><img class="border"
src="../resources/images/devtools-chrome-disable-cache.jpg" style="width: 779px"></div>
<h2 id="use-the-javascript-console">使用JavaScript控制台</h2>
<p>在所有开发者工具中都有 <em>控制台</em>。 它显示了警告和错误信息。</p>
<p><strong>读这些信息!! </strong></p>
<p>通常应该只有 1 或 2 条信息。</p>
<div class="threejs_center"><img class="border" src="../resources/images/devtools-no-errors.jpg"
style="width: 779px"></div>
<p>如果你看到有其他的消息,请务必 <strong>读它们</strong>。 例如:</p>
<div class="threejs_center"><img class="border" src="../resources/images/devtools-errors.jpg"
style="width: 779px"></div>
<p>我把 "three" 错拼成了 "threee"</p>
<p>通过<code class="notranslate" translate="no">console.log</code> 方法,你也可以打印你自己信息到控制台,比如</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">console.log(someObject.position.x, someObject.position.y, someObject.position.z);
</pre>
<p>更酷的是,如果你打印了一个object你可以检查它。 例如,如果从<a href="load-gltf.html">加载gLTF</a>这篇文章中打印了一个根场景object</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no"> {
const gltfLoader = new GLTFLoader();
gltfLoader.load('resources/models/cartoon_lowpoly_small_city_free_pack/scene.gltf', (gltf) => {
const root = gltf.scene;
scene.add(root);
+ console.log(root);
</pre>
<p>我们接着就可以在JavaScript控制台中展开那个object</p>
<div class="threejs_center"><img class="border" src="../resources/images/devtools-console-object.gif">
</div>
<p>你也可以使用 <code class="notranslate" translate="no">console.error</code> 来输出错误信息,该方法会打印一条红色的信息并附上错误栈</p>
<h2 id="put-data-on-screen">在屏幕上显示数据</h2>
<p>另一个直观的但是经常被忽视的方式是通过添加<code class="notranslate" translate="no"><div></code> 或 <code
class="notranslate" translate="no"><pre></code> 标签
并将要显示的数据放在标签内。</p>
<p>最简单的方法就是写一些HTML元素</p>
<pre class="prettyprint showlinemods notranslate lang-html" translate="no"><canvas id="c"></canvas>
+<div id="debug">
+ <div>x:<span id="x"></span></div>
+ <div>y:<span id="y"></span></div>
+ <div>z:<span id="z"></span></div>
+</div>
</pre>
<p>给它们添加些样式让它们显示在canvas元素之上(假设你的canvas元素充满了整个页面)。</p>
<pre class="prettyprint showlinemods notranslate lang-html" translate="no"><style>
#debug {
position: absolute;
left: 1em;
top: 1em;
padding: 1em;
background: rgba(0, 0, 0, 0.8);
color: white;
font-family: monospace;
}
</style>
</pre>
<p>然后查找元素并设置其内容。</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">// at init time
const xElem = document.querySelector('#x');
const yElem = document.querySelector('#y');
const zElem = document.querySelector('#z');
// at render or update time
xElem.textContent = someObject.position.x.toFixed(3);
yElem.textContent = someObject.position.y.toFixed(3);
zElem.textContent = someObject.position.z.toFixed(3);
</pre>
<p>这在展示实时数据时更有用</p>
<p></p>
<div translate="no" class="threejs_example_container notranslate">
<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>
<a class="threejs_center" href="/manual/examples/debug-js-html-elements.html"
target="_blank">点击这里在新窗口中打开</a>
</div>
<p></p>
<p>
将数据显示在屏幕上的另一种方法是制作一个”清除记录器“。这是我编的一个术语,我参与的很多游戏中都使用了这个方法。其思想是您有一个只显示一帧消息的缓冲区,有显示数据需求的时候,就会调用一些函数向该缓冲区添加每一帧的数据。相对于上一种方法为每个数据都添加一个html元素,这种方法会减少更多工作量。
</p>
<p>举个例子,我们把上面的html代码调整为这样</p>
<pre class="prettyprint showlinemods notranslate lang-html" translate="no"><canvas id="c"></canvas>
<div id="debug">
<pre></pre>
</div>
</pre>
<p>接着我们只做一个简单的类来管理这些 <em>“clear back buffer”</em>。</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ClearingLogger {
constructor(elem) {
this.elem = elem;
this.lines = [];
}
log(...args) {
this.lines.push([...args].join(' '));
}
render() {
this.elem.textContent = this.lines.join('\n');
this.lines = [];
}
}
</pre>
<p>接着我们做一个简单的例子:每次我们点击鼠标后,就会产生一个出现2秒钟的物体,并朝着一个随机的方向移动。我们将从一篇文章中的一个示例开始 <a href="responsive.html">making
things
responsive</a></p>
<p>下面是每次点击鼠标都新增一个<a href="/docs/#api/en/objects/Mesh"><code class="notranslate"
translate="no">Mesh</code></a>
的代码</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">const geometry = new THREE.SphereGeometry();
const material = new THREE.MeshBasicMaterial({color: 'red'});
const things = [];
function rand(min, max) {
if (max === undefined) {
max = min;
min = 0;
}
return Math.random() * (max - min) + min;
}
function createThing() {
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
things.push({
mesh,
timer: 2,
velocity: new THREE.Vector3(rand(-5, 5), rand(-5, 5), rand(-5, 5)),
});
}
canvas.addEventListener('click', createThing);
</pre>
<p>接着下面的代码描述了如何移动我们创造出来的mesh,记录它们的信息以及当它们的计时器到期的时候删除它们</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">const logger = new ClearingLogger(document.querySelector('#debug pre'));
let then = 0;
function render(now) {
now *= 0.001; // convert to seconds
const deltaTime = now - then;
then = now;
...
logger.log('fps:', (1 / deltaTime).toFixed(1));
logger.log('num things:', things.length);
for (let i = 0; i < things.length;) {
const thing = things[i];
const mesh = thing.mesh;
const pos = mesh.position;
logger.log(
'timer:', thing.timer.toFixed(3),
'pos:', pos.x.toFixed(3), pos.y.toFixed(3), pos.z.toFixed(3));
thing.timer -= deltaTime;
if (thing.timer <= 0) {
// remove this thing. Note we don't advance `i`
things.splice(i, 1);
scene.remove(mesh);
} else {
mesh.position.addScaledVector(thing.velocity, deltaTime);
++i;
}
}
renderer.render(scene, camera);
logger.render();
requestAnimationFrame(render);
}
</pre>
<p>现在在下面的例子中点击几次鼠标</p>
<p></p>
<div translate="no" class="threejs_example_container notranslate">
<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>
<a class="threejs_center" href="/manual/examples/debug-js-clearing-logger.html"
target="_blank">点击这里在新窗口中打开</a>
</div>
<p></p>
<h2 id="query-parameters">查询参数</h2>
<p>另一件需要记住的事情是,网页可以通过查询参数或锚(有时称为搜索和散列)将数据传递到网页中。</p>
<pre class="prettyprint showlinemods notranslate notranslate" translate="no">https://domain/path/?query#anchor
</pre>
<p>可以使用这个参数使一些功能开启或关闭或传入参数。</p>
<p>以上一个例子为例,我们可以做到只有向URL中传入<code class="notranslate"
translate="no">?debug=true</code>参数的时候,上面的例子才会显示调试数据</p>
<p>首先我们需要一些代码来解析携带查询参数字符串</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">/**
* Returns the query parameters as a key/value object.
* Example: If the query parameters are
*
* abc=123&def=456&name=gman
*
* Then `getQuery()` will return an object like
*
* {
* abc: '123',
* def: '456',
* name: 'gman',
* }
*/
function getQuery() {
return Object.fromEntries(new URLSearchParams(window.location.search).entries());
}
</pre>
<p>接着我们可能想让显示调试信息的元素在默认情况下不出现</p>
<pre class="prettyprint showlinemods notranslate lang-html" translate="no"><canvas id="c"></canvas>
+<div id="debug" style="display: none;">
<pre></pre>
</div>
</pre>
<p>接着在代码中我们通过获取特定参数来决定当且仅当<code class="notranslate" translate="no">?debug=true</code>传入的时候才显示我们的调试信息</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">const query = getQuery();
const debug = query.debug === 'true';
const logger = debug
? new ClearingLogger(document.querySelector('#debug pre'))
: new DummyLogger();
if (debug) {
document.querySelector('#debug').style.display = '';
}
</pre>
<p>同时我们也写了一个什么事情也不做的<code class="notranslate" translate="no">DummyLogger</code>,并在<code
class="notranslate" translate="no">?debug=true</code> 没有被传入的时候使用它。</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">class DummyLogger {
log() {}
render() {}
}
</pre>
<p>你可以看到在这个URL里</p>
<p><a target="_blank" href="../examples/debug-js-params.html">debug-js-params.html</a></p>
<p>没有调试信息。但是当我们使用这个url的时候:</p>
<p><a target="_blank"
href="../examples/debug-js-params.html?debug=true">debug-js-params.html?debug=true</a></p>
<p>就包含了调试信息</p>
<p>多个参数传递的时候需要以'&'分隔。例如<code class="notranslate"
translate="no">somepage.html?someparam=somevalue&someotherparam=someothervalue</code>.
使用这样的参数我们就可以传入各种各样的选项。例如传入<code class="notranslate" translate="no">speed=0.01</code>
以降低app速度来更好的理解某个过程。或者传入
<code class="notranslate" translate="no">showHelpers=true</code>
来决定是否添加一个helper来辅助显示其他课程中提到过的光线、阴影或者相机的视锥
</p>
<h2 id="learn-to-use-the-debugger">学习使用调试器</h2>
<p>每个浏览器都有一个调试器,您可以在其中逐行暂停程序并检查所有变量。</p>
<p>教您如何使用调试器对于本文来说是一个太大的主题,但这里有几个链接</p>
<ul>
<li><a href="https://developers.google.com/web/tools/chrome-devtools/javascript/">Get Started with
Debugging
JavaScript in Chrome DevTools</a></li>
<li><a href="https://javascript.info/debugging-chrome">Debugging in Chrome</a></li>
<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>
</ul>
<h2 id="check-for-nan-in-the-debugger-or-elsewhere">在调试器或其他地方检查<code class="notranslate"
translate="no">NaN</code>值</h2>
<p><code class="notranslate" translate="no">NaN</code> 不是数字的缩写。当你做了一些不符合数学的事情时,JavaScript会把它指定为一个值。</p>
<p>举一个简单的例子</p>
<div class="threejs_center"><img class="border" src="../resources/images/nan-banana.png"
style="width: 180px;">
</div>
<p>通常,当我在制作一些东西时,屏幕上什么也没有出现,我会检查一些值,如果我看到<code class="notranslate" translate="no">NaN</code>
我会立即知道问题所在</p>
<p>举一个例子,当我第一次在
<a href="load-gltf.html">article about loading gLTF files</a> 文章中创建路径的时候,我使用<a
href="/docs/#api/en/extras/curves/SplineCurve"><code class="notranslate"
translate="no">SplineCurve</code></a> ——一个专门制作2D曲线的类,做了一条曲线。
</p>
<p>接着我使用这条曲线使汽车像这样移动</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">curve.getPointAt(zeroToOnePointOnCurve, car.position);
</pre>
<p>在内部 <code class="notranslate" translate="no">curve.getPointAt</code> 方法将调用传入的第二个参数对象上的 <code
class="notranslate" translate="no">set</code> 函数。在例子中第二个变量是<code class="notranslate"
translate="no">car.position</code>也就是一个<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>的
<code class="notranslate" translate="no">set</code> 函数需要三个参数, x, y, 和 z 但是 <a
href="/docs/#api/en/extras/curves/SplineCurve"><code class="notranslate"
translate="no">SplineCurve</code></a> 是一个2D曲线,因此它仅向<code class="notranslate"
translate="no">car.position.set</code>方法传入了x和y值。
</p>
<p>结果<code class="notranslate" translate="no">car.position.set</code> 方法将x设置为了x,y设置成了y,z设置成了<code
class="notranslate" translate="no">undefined</code>.</p>
<p>在调试器中简单查看一下汽车的<code class="notranslate" translate="no">matrixWorld</code>
就会发现许多的 <code class="notranslate" translate="no">NaN</code> 值。</p>
<div class="threejs_center"><img class="border" src="../resources/images/debugging-nan.gif"
style="width: 476px;"></div>
<p>看到矩阵中包含<code class="notranslate" translate="no">NaN</code>值就表明一些像
<code class="notranslate" translate="no">position</code>,
<code class="notranslate" translate="no">rotation</code>, <code class="notranslate"
translate="no">scale</code> 或者其他的会影响矩阵的函数就有坏数据。根据这一点来找bug就会很容易发现问题所在。
</p>
<p>除了<code class="notranslate" translate="no">NaN</code> 之外还有<code class="notranslate"
translate="no">Infinity</code>,也能表明一些地方有了数学bug</p>
<h2 id="look-in-the-code-">快来看看代码!</h2>
<p>THREE.js是一个开源的库。别害怕深入库内部查看代码。
你可以在<a href="https://github.com/mrdoob/three.js">github</a>里查看它的源代码。
您还可以通过在调试器中单步执行函数来查看内部。</p>
<h2 id="put-requestanimationframe-at-bottom-of-your-render-function-">将 <code class="notranslate"
translate="no">requestAnimationFrame</code> 放着在渲染函数的底部。</h2>
<p>我经常看到如下的代码</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render() {
requestAnimationFrame(render);
// -- do stuff --
renderer.render(scene, camera);
}
requestAnimationFrame(render);
</pre>
<p>我建议将<code class="notranslate" translate="no">requestAnimationFrame</code> 的调用放在底部,如下所示</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render() {
// -- do stuff --
renderer.render(scene, camera);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
</pre>
<p>最大的原因是,在正常情况下,如果出现错误,代码执行将停止。但将<code class="notranslate" translate="no">requestAnimationFrame</code>
置于顶部意味着您的代码将继续运行,因为您已经请求了另一帧。在我看来,发现这些错误比忽略它们要好。它们很容易成为某些东西没有按预期出现的原因,但除非代码停止,否则您可能根本不会注意到。
</p>
<h2 id="check-your-units-">检查你使用的单位</h2>
<p>这基本上意味着知道何时使用度(degrees)和何时使用弧度(radians)。不幸的是THREE.js并不是在所有地方都使用相同的单位。
现在我能想到的情况就是摄像机的视野是以degrees为单位的。所有其他角度均以radians为单位。</p>
<p>
另一个值得注意的地方是你所使用的长度单位的大小。目前3D应用程序还可以选择他们想要的任何单位大小。
一个应用程序可能会选择1个单位=1厘米。另一个可能选择1个单位=1英尺。事实上,你可以为某些应用选择任何你想要的单位。
也就是说,THREE.js假设1个单位=1米。这对于基于物理的渲染(PBR)——使用米来计算照明效果之类的事情很重要。这对于AR和VR也很重要,因为它们需要处理真实世界中的长度,如手机所在的位置或VR控制器所在的位置。
</p>
<h2 id="making-a-minimal-complete-verifiable-example-for-stack-overflow">
在Stack Overflow上制作一个
<em>最小的、完整的、可验证的</em> 示例
</h2>
<p>如果你决定问一个关于THREE.js的问题,你需要提供一个MCVE,即最小的(Minimal)、完整的(Complete)、可验证的(Verifiable)示例。</p>
<p> <strong>最小的(Minimal)</strong> 是很重要的。 比如说在上一个例子<a href="load-gltf.html">loading a gLTF
article</a>中你有一个关于运动轨迹的例子。那个例子有很多部分,比如
</p>
<ol>
<li>一些HTML</li>
<li>一些 CSS</li>
<li>光源</li>
<li>阴影</li>
<li>操作阴影的lil-gui 代码</li>
<li>加载.GLTF文件的代码</li>
<li>调整canvas大小的代码</li>
<li>让汽车沿着路线移动的代码</li>
</ol>
<p>看起来真的很多。如果你的问题仅仅是关于路线跟随的部分,你可以将大部分HTML代码删除,因为你只需要一个<code class="notranslate"
translate="no"><canvas></code>
和一个<code class="notranslate"
translate="no"><script></code>标记就可以了。你可以删除CSS和调整大小的代码,也可以删除.GLTF代码,因为你只关心路径。
你可以使用<a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate"
translate="no">MeshBasicMaterial</code></a>删除灯光和阴影。你当然也可以删除lil gui代码。代码中纹理创建地平面,如果使用<a
href="/docs/#api/en/helpers/GridHelper"><code class="notranslate"
translate="no">GridHelper</code></a>.会更容易。
最后,如果我们的问题是关于在路径上移动物体,我们可以在路径上使用立方体,而不是装载的汽车模型。
</p>
<p>
这里有一个更简单的例子,考虑了以上所有因素。它从271行缩减到135行。我们甚至可以考虑通过简化路径来进一步缩小它。也许一条有3点或4点的路径和我们有21点的路径一样有效。
</p>
<p></p>
<div translate="no" class="threejs_example_container notranslate">
<div><iframe class="threejs_example notranslate" translate="no" style=" "
src="/manual/examples/resources/editor.html?url=/manual/examples/debugging-mcve.html"></iframe>
</div>
<a class="threejs_center" href="/manual/examples/debugging-mcve.html"
target="_blank">点击这里在新窗口中打开</a>
</div>
<p></p>
<p>我保留了 <code class="notranslate" translate="no">OrbitController</code> 因为它是很有用的——别人可以通过移动相机
搞清楚发生了什么。但是根据实际情况你也可以移除它。
</p>
<p>
认真写一个MCVE所带来的好处就是我们经常能自己解决问题。删除所有不需要的内容,并尽我们所能制作最小的示例来重现问题的过程往往会使得错误原因的展现。
</p>
<p>
最重要的是,它尊重了查看你问题代码的其他人的时间。通过做一个简单的例子,你就可以让他们更容易地帮助你。你也将在这个过程中学习。
</p>
<p>同样重要的是,当你去到Stack Overflow 提交问题的时候 <strong>将你的代码放在<a
href="https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/">代码片段</a>.</strong>里
你当然可以使用JSFiddle 或者 Codepen 或者相似的第三方网站去测试你的MCVE,但是一旦你在Stack
Overflow上面发布问题,你就需要把代码放在<strong>问题本身</strong>中重现你的问题。
</p>
<p>
还请注意,此站点上的所有实时示例都应作为片段运行。只需将HTML、CSS和JavaScript部分复制到
<a
href="https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/">代码段编辑器</a>
各自的部分。请记住,尽量删除与您的问题无关的部分,并尽量使您的代码达到所需的最小数量。
</p>
<p>遵循这些建议你就可能在解决问题上得到帮助</p>
<h2 id="use-a-meshbasicmaterial-">使用<a href="/docs/#api/en/materials/MeshBasicMaterial"><code
class="notranslate" translate="no">MeshBasicMaterial</code></a></h2>
<p>由于<a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate"
translate="no">MeshBasicMaterial</code></a>不使用光照,这样做就不会产生一些物体不显示的问题了。如果你的物体在使用<a
href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate"
translate="no">MeshBasicMaterial</code></a>
的时候显示,但是在使用其他任何材料时候不显示,那么你便知道问题出在材料或者光照上而不是其他地方的代码。
</p>
<h2 id="check-your-near-and-far-settings-for-your-camera">检查你相机的 <code class="notranslate"
translate="no">near</code> 和 <code class="notranslate" translate="no">far</code> 设置
</h2>
<p>一个 <a href="/docs/#api/en/cameras/PerspectiveCamera"><code class="notranslate"
translate="no">PerspectiveCamera</code></a> 有 <code class="notranslate"
translate="no">near</code> 和
<code class="notranslate" translate="no">far</code> 设置——在
<a href="cameras.html">article on cameras</a>这篇文章中讲过。保证它的值的设置所能观察到的空间范围能够包裹你渲染的物体
甚至可以<strong>暂时性的</strong>设置为 <code class="notranslate" translate="no">near</code> = 0.001 以及 <code
class="notranslate" translate="no">far</code> = 1000000。你可能遇到深度层级分辨率问题但是你至少可以看到你的物体——前提是它在你的相机前。
</p>
<h2 id="check-your-scene-is-in-front-of-the-camera">检查相机前的场景</h2>
<p>有时候一些东西不会出现因为它们不在相机前。 如果你的相机是不可控制的,尝试添加一个相机控制器比如
<code class="notranslate" translate="no">OrbitController</code>,这样你就可以看到周围的景象并找到你的场景。
或者,尝试使用<a href="load-obj.html">这篇文章</a>中介绍的代码来设置场景的帧。该代码查找场景部分的大小,然后移动摄影机并调整<code
class="notranslate" translate="no">near</code>和<code class="notranslate"
translate="no">far</code>设置以使其可见。然后可以查看调试器或添加一些 <code class="notranslate"
translate="no">console.log</code> 消息以打印场景的大小和中心。
</p>
<h2 id="put-something-in-front-of-the-camera">在相机前放一些东西</h2>
<p>
这只是另一种说法,如果所有其他方法都失败了,那么就从一些有效的方法开始,然后慢慢地再添加一些东西。
如果你得到一个屏幕上没有任何东西,那么试着把一些东西直接放在相机前面。制作一个球体或长方体,给它一个简单的材质,比如 <a
href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate"
translate="no">MeshBasicMaterial</code></a> ,
确保你可以在屏幕上看到它。然后可以一次添加一些东西,然后再测试它。最终,你要么复现出你的bug,要么在途中发现它。
</p>
<hr>
<p>以上这些就是调试JavaScript的一些建议。让我们开始浏览<a href="debugging-glsl.html">调试GLSL的一些建议</a>吧。</p>
</div>
</div>
</div>
<script src="../resources/prettify.js"></script>
<script src="../resources/lesson.js"></script>
</body>
</html>