-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.html
642 lines (569 loc) · 19 KB
/
index.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<!DOCTYPE html>
<!--
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Eric Bidelman ([email protected])
-->
<html>
<head>
<title>Introduction to Three.js</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<script src='slides.js'></script>
<link href="http://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:regular,semibold,italic,italicsemibold|Droid+Sans+Mono&v2">
</head>
<body style="display: none">
<section class='slides layout-regular'>
<article class="biglogo">
<script>
(function() {
var c = document.createElement('canvas');
var ctx = null;
try {
ctx = c.getContext('webgl');
} catch(e) { }
try {
ctx = ctx || c.getContext('experimental-webgl');
} catch(e) { }
if (!ctx) alert("No WebGL detected, live demos disabled!");
})();
</script>
</article>
<article id="title" class="title">
<div>
<h1>Three.js basics</h1>
<h2 class="subtitle">Ilmari Heikkinen</h2>
<h3 class="subtitle"><img style="height:60px; padding-left:70px;" src="images/google_logo.png"></h3>
</div>
</article>
<article>
<h3>Who am I?</h3>
<p>Ilmari Heikkinen</p>
<p><a href="http://twitter.com/ilmarihei">@ilmarihei</a>
| <a href="http://fhtr.org/plus">fhtr.org/plus</a>
| <a href="http://fhtr.org">fhtr.org</a></p>
<p>Google Chrome Developer Programs Engineer</p>
<br><br>
<p>I write demos, do presentations, write articles</p>
<br><br>
<p>Slides available at <a href="http://fhtr.org/BasicsOfThreeJS">fhtr.org/BasicsOfThreeJS</a></p>
<p>Repo at <a href="https://github.com/kig/BasicsOfThreeJS">github.com/kig/BasicsOfThreeJS</a></p>
</article>
<article class="title">
<h2>Three.js?</h2>
</article>
<article>
<h3>Three.js is a JS 3D Engine</h3>
<ul>
<li><a href="https://github.com/mrdoob/three.js/">github.com/mrdoob/three.js</a></li>
<li>Lightweight</li>
<li>Easy to use</li>
<li>Batteries included</li>
<li>WebGL renderer (our focus today)</li>
<li>Also has Canvas and SVG renderers</li>
</ul>
</article>
<article class="title">
<h2>Basic setup</h2>
</article>
<article>
<h3>Renderer</h3>
<p>Create a WebGLRenderer</p>
<pre>
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(document.body.clientWidth,
document.body.clientHeight);</pre>
<p>Plug it in</p>
<pre>
document.body.appendChild(renderer.domElement);</pre>
<p>And make it pretty</p>
<pre>
renderer.setClearColorHex(0xEEEEEE, 1.0);
renderer.clear();</pre>
</article>
<article>
<h3>Wow!</h3>
<iframe src="three/three_1.html"></iframe>
</article>
<article>
<h3>It gets better, I promise!</h3>
<div>
<p>Create a Camera</p>
<pre>
// new THREE.PerspectiveCamera( FOV, viewAspectRatio, zNear, zFar );
var camera = new THREE.PerspectiveCamera(45, width/height, 1, 10000);
camera.position.z = 300;</pre>
<p>Make a Scene with a Cube</p>
<pre>
var scene = new THREE.Scene();
var cube = new THREE.Mesh(new THREE.CubeGeometry(50,50,50),
new THREE.MeshBasicMaterial({color: 0x000000}));
scene.add(cube);</pre>
<p>And render the Scene from the Camera</p>
<pre>
renderer.render(scene, camera);</pre>
</div>
</article>
<article>
<h3>At least it isn't empty.</h3>
<iframe src="three/three_2.html"></iframe>
</article>
<article>
<h3>Recap</h3>
<p>Create a renderer <code>new THREE.WebGLRenderer()</code></p>
<p>Create a camera <code>new THREE.PerspectiveCamera(fov, aR, n, f)</code></p>
<p>Create a scene <code>new THREE.Scene()</code></p>
<p>Create a mesh with a geometry and a material<code><br>new THREE.Mesh(new THREE.CubeGeometry(w,h,d),<br> new THREE.MeshBasicMaterial({color: 0x000000}))</code>
</p>
<p>Add the mesh to the scene <code>scene.add(mesh)</code></p>
<p>Render the scene <code>renderer.render(scene, camera)</code></p>
<p>Add the renderer canvas to the page <code>document.body.appendChild(renderer.domElement)</code></p>
</article>
<article class="title">
<h2>Animation</h2>
</article>
<article>
<h3>Can we make it move?</h3>
<p>Yes we can!</p>
<p class="small">(But it's a manual process.)</p>
<pre>
function animate(t) {
// spin the camera in a circle
camera.position.x = Math.sin(t/1000)*300;
camera.position.y = 150;
camera.position.z = Math.cos(t/1000)*300;
// you need to update lookAt every frame
camera.lookAt(scene.position);
// renderer automatically clears unless autoClear = false
renderer.render(scene, camera);
window.requestAnimationFrame(animate, renderer.domElement);
};
animate(new Date().getTime());
</pre>
</article>
<article>
<h3>Isn't it nice?</h3>
<iframe src="three/three_3.html"></iframe>
</article>
<article>
<h3>requestAnimationFrame?</h3>
<pre>requestAnimationFrame(function(time){}, element)</pre>
<p>It's like <code>setTimeout(f, timeUntilNextFrame)</code></p>
<p>Except that:</p>
<ul class="build">
<li>The browser knows when the next frame is drawn</li>
<div> → calls callback at screen refresh rate or lower<br><br></div>
<li>The browser knows when an element is on the screen</li>
<div> → only calls callback when the element is visible</div>
</ul>
</article>
<article class="title">
<h2>Lighting</h2>
</article>
<article>
<h3>Lights</h3>
<p>Without light, our scene is very forlorn.</p>
<p>Let's create a Light</p>
<pre>
var light = new THREE.SpotLight();
light.position.set( 170, 330, -160 );
scene.add(light);</pre>
<p>And a lit cube</p>
<pre>
var litCube = new THREE.Mesh(
new THREE.CubeGeometry(50, 50, 50),
new THREE.MeshLambertMaterial({color: 0xFFFFFF}));
litCube.position.y = 50;
scene.add(litCube);</pre>
</article>
<article>
<h3>There we go!</h3>
<iframe src="three/three_4.html"></iframe>
</article>
<article>
<h3>How about shadows?</h3>
<p>Three.js has shadow maps.</p>
<p>You need to enable them per-light and per-object.</p>
<p>The shadows only work on SpotLights.</p>
<pre>
// enable shadows on the renderer
renderer.shadowMapEnabled = true;
// enable shadows for a light
light.castShadow = true;
// enable shadows for an object
litCube.castShadow = true;
litCube.receiveShadow = true;</pre>
</article>
<article>
<h3>Added interest</h3>
<p>Let's add a ground plane</p>
<pre>
var planeGeo = new THREE.PlaneGeometry(400, 200, 10, 10);
var planeMat = new THREE.MeshLambertMaterial({color: 0xFFFFFF});
var plane = new THREE.Mesh(planeGeo, planeMat);
plane.rotation.x = -Math.PI/2;
plane.position.y = -25;
plane.receiveShadow = true;
scene.add(plane);</pre>
<p>And make the cube spin</p>
<pre>
litCube.position.x = Math.cos(t/600)*85;
litCube.position.y = 60-Math.sin(t/900)*25;
litCube.position.z = Math.sin(t/600)*85;
litCube.rotation.x = t/500;
litCube.rotation.y = t/800;</pre>
</article>
<article>
<h3>Plus a couple of tweaks</h3>
<iframe src="three/three_5.html"></iframe>
</article>
<article>
<h3>Lighting recap</h3>
<p>Set mesh material to <code>new THREE.MeshLambertMaterial</code> or <code>new THREE.MeshPhongMaterial</code></p>
<p>Create a light <code>new THREE.SpotLight(color)</code></p>
<p>Add the light to the scene <code>scene.add(light)</code></p>
<p>Turn on shadows if you need them</p>
<pre>
renderer.shadowMapEnabled = true;
light.castShadow = true;
object.castShadow = true;
object.receiveShadow = true;</pre>
</article>
<article class="title">
<h2>Shaders</h2>
</article>
<article>
<h3>What are shaders</h3>
<p>Shaders are small programs that tell WebGL</p>
<br>
<center class="build">
<p>where to draw</p>
<p class="small">and</p>
<p>what to draw<br><br></p>
<p style="text-align:left;">Shaders are written in GLSL, the GL Shading Language.</p>
<p style="text-align:left;">It's kinda like C for graphics.</p>
</center>
</article>
<article>
<h3>Vertex shader</h3>
<p>Where to draw.</p>
<p>Projects geometry to screen coordinates.</p>
<pre>
<script id="vertex" type="text/x-glsl-vert">
varying float vZ;
uniform float time;
void main() {
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
mvPosition.y += 20.0*sin(time*0.5+mvPosition.x/25.0);
mvPosition.x += 30.0*cos(time*0.5+mvPosition.y/25.0);
vec4 p = projectionMatrix * mvPosition;
vZ = p.z;
gl_Position = p;
}
</script></pre>
</article>
<article>
<h3>Fragment shader</h3>
<p>What to draw.</p>
<p>Computes the color of a pixel.</p>
<pre>
<script id="fragment" type="text/x-glsl-frag">
varying float vZ;
uniform float time;
uniform vec2 size;
void main() {
vec2 d = gl_FragCoord.xy - (0.5+0.02*sin(time))*size;
float a = sin(time*0.3)*2.0*3.14159;
d = vec2( d.x*cos(a) + d.y*sin(a),
-d.x*sin(a) + d.y*cos(a));
vec2 rg = vec2(1.0)-abs(d)/(0.5*size)
float b = abs(vZ) / 160.0;
gl_FragColor = vec4(rg,b,1.0);
}
</script></pre>
</article>
<article>
<h3>It's very colourful</h3>
<iframe src="three/three_6.html"></iframe>
</article>
<article>
<h3>Shaders and Three.js</h3>
<p>Use a ShaderMaterial</p>
<pre>
var uniforms = {
time : { type: "f", value: 1.0 },
size : { type: "v2", value: new THREE.Vector2(width,height) }
};
var shaderMaterial = new THREE.ShaderMaterial({
uniforms : uniforms,
vertexShader : $('#vertex').text(),
fragmentShader : $('#fragment').text()
});
var meshCube = new THREE.Mesh(
new THREE.CubeGeometry(50,50,50, 20,20,20), // 20 segments
shaderMaterial
);</pre>
</article>
<article>
<h3>Animating shader uniforms</h3>
<p>Change the uniform value</p>
<pre>
uniforms.time.value += 0.05;</pre>
<p>And for the size, add a resize listener</p>
<pre>
window.addEventListener('resize', function() {
uniforms.size.value.x = window.innerWidth;
uniforms.size.value.y = window.innerHeight;
}, false);</pre>
</article>
<article class="title">
<h2>Useful uses</h2>
</article>
<article>
<h3>Bar graph</h3>
<pre>
var grid = /* 2D Array */
var barGraph = new THREE.Object3D();
scene.add(barGraph);
var max = /* Grid max value */
var mat = new THREE.MeshLambertMaterial({color: 0xFFAA55});
for (var j=0; j<grid.length; j++) {
for (var i=0; i<grid[j].length; i++) {
var barHeight = grid[j][i]/max * 80;
var geo = new THREE.CubeGeometry(8, barHeight, 8);
var mesh = new THREE.Mesh(geo, mat);
mesh.position.x = (i-grid[j].length/2) * 16;
mesh.position.y = barHeight/2;
mesh.position.z = -(j-grid.length/2) * 16;
mesh.castShadow = mesh.receiveShadow = true;
barGraph.add(mesh);
}
}</pre>
</article>
<article>
<h3>Bar graph in action</h3>
<iframe src="three/three_7.html"></iframe>
</article>
<article>
<h3>Scatter plot</h3>
<pre>
var scatterPlot = new THREE.Object3D();
var mat = new THREE.ParticleBasicMaterial(
{vertexColors: true, size: 1.5});
var pointCount = 10000;
var pointGeo = new THREE.Geometry();
for (var i=0; i<pointCount; i++) {
var x = Math.random() * 100 - 50;
var y = x*0.8+Math.random() * 20 - 10;
var z = x*0.7+Math.random() * 30 - 15;
pointGeo.vertices.push(new THREE.Vertex(new THREE.Vector3(x,y,z)));
pointGeo.colors.push(new THREE.Color().setHSV(
(x+50)/100, (z+50)/100, (y+50)/100));
}
var points = new THREE.ParticleSystem(pointGeo, mat);
scatterPlot.add(points);
scene.fog = new THREE.FogExp2(0xFFFFFF, 0.0035);</pre>
</article>
<article>
<h3>Scatter plot in action</h3>
<iframe src="three/three_8.html"></iframe>
<p class="small">Double-click to animate</p>
</article>
<article>
<h3>How to do text</h3>
<p>Create a canvas, draw text on it, use as texture.</p>
<pre>
var c = document.createElement('canvas');
c.getContext('2d').font = '50px Arial';
c.getContext('2d').fillText('Hello, world!', 2, 50);
var tex = new THREE.Texture(c);
tex.needsUpdate = true;
var mat = new THREE.MeshBasicMaterial({map: tex});
mat.transparent = true;
var titleQuad = new THREE.Mesh(
new THREE.PlaneGeometry(c.width, c.height),
mat
);
titleQuad.doubleSided = true;</pre>
</article>
<article>
<h3>Hello, world!</h3>
<iframe src="three/three_9.html"></iframe>
</article>
<article>
<h3>And the lines?</h3>
<p>Create a geometry, add vertices, use to make a THREE.Line.</p>
<pre>
function v(x,y,z){
return new THREE.Vertex(new THREE.Vector3(x,y,z));
}
var lineGeo = new THREE.Geometry();
lineGeo.vertices.push(
v(-50, 0, 0), v(50, 0, 0),
v(0, -50, 0), v(0, 50, 0),
v(0, 0, -50), v(0, 0, 50)
);
var lineMat = new THREE.LineBasicMaterial({
color: 0x000000, lineWidth: 1});
var line = new THREE.Line(lineGeo, lineMat);
line.type = THREE.Lines;
scene.add(line);</pre>
</article>
<article>
<h3>Coordinate axes</h3>
<iframe src="three/three_10.html"></iframe>
</article>
<article class="title">
<h2>GUI controls</h2>
</article>
<article>
<h3>GUI controls</h3>
<p>To me, GUI controls have been a pain.</p>
<p>Write your own widgets, write your own onchange handlers, write your own interval change polling logic, style it all, hope it scales, gaaaaaah!</p>
<p>Let's just use DAT.GUI instead.</p>
<pre>
var gui = new DAT.GUI();
gui.add(cube.scale, 'x').min(0.1).max(10).step(0.1);
gui.add(cube.scale, 'y', 0.1, 10, 0.1);
gui.add(cube.scale, 'z', 0.1, 10, 0.1);</pre>
<p>Done!</p>
<p><a href="http://code.google.com/p/dat-gui">code.google.com/p/dat-gui</a></p>
</article>
<article>
<h3>Scalable cube</h3>
<iframe src="three/three_11.html"></iframe>
</article>
<article>
<h3>But but, where is the <i>pain</i>?</h3>
<p>Well, we could do a proxy to control only the currently selected object.</p>
<pre>
var controller = new THREE.Object3D();
var gui = new DAT.GUI({width: 160});
controller.setCurrent = function(current) {
this.current = current;
this.x.setValue(current.position.x);
this.y.setValue(current.position.y);
this.z.setValue(current.position.z);
};
controller.x = gui.add(controller.position, 'x').onChange(function(v){
controller.current.position.x = v;
});
// etc.</pre>
</article>
<article>
<h3>How to select?</h3>
<p>Project a ray into the scene and find intersecting objects.</p>
<pre>
var projector = new THREE.Projector();
window.addEventListener('mousedown', function (ev){
if (ev.target == renderer.domElement) {
var x = ev.clientX;
var y = ev.clientY;
var v = new THREE.Vector3((x/width)*2-1, -(y/height)*2+1, 0.5);
projector.unprojectVector(v, camera);
var ray = new THREE.Ray(camera.position,
v.subSelf(camera.position).normalize());
var intersects = ray.intersectObjects(controller.objects);
if (intersects.length > 0) {
controller.setCurrent(intersects[0].object);
}
}
}, false);</pre>
</article>
<article>
<h3>Great cubistan</h3>
<iframe src="three/three_12.html"></iframe>
</article>
<article class="title">
<h2>Loading models</h2>
</article>
<article>
<h3>Model exporters</h3>
<p>Look into utils/exporters/</p>
<ul>
<li>blender/</li>
<li>fbx/</li>
<li>max/</li>
<li>utf8/</li>
<li>convert_obj_three.py</li>
</ul>
</article>
<article>
<h3>Model loaders</h3>
<p>Look into src/extras/loaders/</p>
<ul>
<li>BinaryLoader.js</li>
<li>ColladaLoader.js</li>
<li>JSONLoader.js</li>
<li>SceneLoader.js</li>
<li>UTF8Loader.js</li>
</ul>
<p>Huh? So what should I use?</p>
</article>
<article>
<h3>COLLADA loader</h3>
<pre>
new THREE.ColladaLoader().load('models/monster.dae',
function(collada) {
var model = collada.scene;
model.scale.set(0.1, 0.1, 0.1);
model.rotation.x = -Math.PI/2;
scene.add(model);
});</pre>
<p>Doesn't look too complicated.</p>
<p>Copy-pasted from examples/webgl_collada.html</p>
<p>Along with the code to make it animate.</p>
</article>
<article>
<h3>Monster</h3>
<iframe src="three/three_13.html"></iframe>
</article>
<article class="title">
<h2>Conclusion</h2>
</article>
<article>
<h3>Three.js</h3>
<p><a href="https://github.com/mrdoob/three.js/">github.com/mrdoob/three.js</a></p>
<p>JavaScript library for 3D graphics</p>
<p>Easy to use</p>
<p>Efficient</p>
<p>Nice feature set</p>
<p>DAT.GUI for simple GUIs <a href="http://code.google.com/p/dat-gui">code.google.com/p/dat-gui</a></p>
</article>
<article>
<h3>The End</h3>
<p>Ilmari Heikkinen</p>
<p><a href="http://twitter.com/ilmarihei">@ilmarihei</a>
| <a href="http://fhtr.org/plus">fhtr.org/plus</a>
| <a href="http://fhtr.org">fhtr.org</a></p>
<br><br>
<p>Slides available at <a href="http://fhtr.org/BasicsOfThreeJS">fhtr.org/BasicsOfThreeJS</a></p>
<p>Repo at <a href="https://github.com/kig/BasicsOfThreeJS">github.com/kig/BasicsOfThreeJS</a></p>
</article>
</section>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1485935-6']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!--[if IE]>
<script src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js"></script>
<script>CFInstall.check({mode: 'overlay'});</script>
<![endif]-->
</body>
</html>