-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
413 lines (364 loc) · 13.5 KB
/
Copy pathindex.html
File metadata and controls
413 lines (364 loc) · 13.5 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=1024" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>WebGL Codelab</title>
<meta name="description" content="WebGL Codelab DevFest BCN" />
<meta name="author" content="Alberto Alonso Ruibal" />
<link href="css/impress.css" rel="stylesheet" />
<link href='http://fonts.googleapis.com/css?family=Finger+Paint' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Ubuntu:300,400' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Droid+Sans+Mono' rel='stylesheet' type='text/css'>
</head>
<body class="impress-not-supported">
<div class="fallback-message">
<p>
Your browser <b>doesn't support the features required</b> by
impress.js, so you are presented with a simplified version of this
presentation.
</p>
<p>
For the best experience please use the latest <b>Chrome</b>, <b>Safari</b>
or <b>Firefox</b> browser.
</p>
</div>
<div id="impress">
<div id="start" class="step slide" data-x="0" data-y="0" data-z="5000">
<h1>Game Development with WebGL and Three.js</h1>
<br /> <img src="img/intro.jpg" style="display: block; margin-left: auto; margin-right: auto;">
<br>
<span class="footnote">SobrosoParty, March 2013</span>
</div>
<div class="step slide">
<h1>Who am I?</h1>
<img src="img/albertoruibal.jpg" style="float:right">
<ul>
<li>Previously: J2EE dev, System manager</li>
<li>Now Android & HTML5/GWT dev</li>
<li>Co-Founder at <a href="http://www.mobialia.com">Mobialia</a></li>
<li>Twitter: @albertoruibal</li>
<li>GDG Vigo Co-Organizer</li>
</ul>
Apps:
<ul>
<li>Mobialia Chess</li>
<li>Gas-Stations Spain</li>
<li>Slot Racing</li>
</ul>
</div>
<div class="step slide" data-x="1000" data-y="0" data-z="4000" data-rotate="10" data-scale="1">
<h1>Why WebGL?</h1>
<ul>
<li>Advanced Graphics in the browser</li>
<li>Direct Access to the GPU: acceleration</li>
<li>Javascript API</li>
<li>Plugin-free</li>
<li>Based on OpenGL ES 2.0</li>
<li>Supported on almost all desktop browsers:</li>
</ul>
<center><img src="img/browsers.png"/></center>
</div>
<div class="step slide" data-x="1000" data-y="0" data-z="4000" data-rotate="10" data-scale="1">
<h1>Mobile Browsers</h1>
<ul>
<li>BlackBerry (Playbook, BB10)</li>
<li>Firefox Mobile/Firefox OS</li>
<li>Chrome Beta for Android (w/flags)</li>
</ul>
<h1>Also supported with...</h1>
<ul>
<li>Google Chrome Frame (Plugin for IE)</li>
<li>Chromium Embedded Framework (DLL used by Steam, iTunes...)</li>
</ul>
</div>
<div class="step slide" data-x="2000" data-y="0" data-z="3000"
data-rotate="-10" data-scale="1">
<h1>The WebGL Mess</h1>
WebGL development needs knowledge about:
<ul>
<li>Vertex Shaders</li>
<li>Fragment Shaders</li>
<li>Uniforms</li>
<li>Varyings</li>
<li>Matrices</li>
<li>...</li>
</ul>
WebGL doesn't know about cameras, models, materials...
</div>
<div class="step slide" data-x="3000" data-y="0" data-z="2000" data-rotate="10">
<h1>Three.js</h1>
<p>Lightweight library that provides a object model with:</p>
<ul>
<li>Scene</li>
<li>Geometry</li>
<li>Material</li>
<li>Mesh</li>
<li>Light</li>
<li>...</li>
</ul>
And a "Renderer" to draw the scene (also model load...)
</div>
<div class="step slide" data-x="4000" data-y="0" data-z="1000" data-rotate="-10">
<h1>About Three.js</h1>
<img src="img/cabello.png" style="float: right">
By Ricardo Cabello (Mr. Doob)
<ul>
<li><a href="https://twitter.com/mrdoob">Twitter (@mrdoob)</a></li>
<li><a href="https://plus.google.com/104300307601542851567/posts">Google Plus page</a></li>
<li>Three.js in GitHub: <a href="https://github.com/mrdoob/three.js/">github.com/mrdoob/three.js/</a></li>
</ul>
</div>
<div class="step slide" data-x="5000" data-y="0" data-z="0" data-rotate="0">
<h1>Three.js Demos</h1>
<ul>
<li>Official examples <a href="http://mrdoob.github.com/three.js/">mrdoob.github.com/three.js/</a></li>
<li>Mr Doob page: <a href="http://mrdoob.com/">mrdoob.com/</a></li>
<li>Hello Racer: <a href="http://helloracer.com/webgl/">helloracer.com/webgl/</a></li>
<li>Trigger Rally: <a href="http://triggerrally.com//">triggerrally.com/</a></li>
<li>HexGL: <a href="http://hexgl.bkcore.com/">hexgl.bkcore.com/</a></li>
<li>Nissan Juke: <a href="http://pierrelepers.com/lab/jthree/">pierrelepers.com/lab/jthree/</a></li>
</ul>
</div>
<div class="step slide" data-x="0" data-y="1000" data-z="5000" data-rotate="0" data-scale="1"">
<h1>Using Three.js</h1>
<ul>
<li>Download three.min.js from:</li>
</ul>
<a href="https://github.com/mrdoob/three.js/tree/master/build/">github.com/mrdoob/three.js/tree/master/build/</a>
<ul>
<li>Create an empty page including the js:</li>
<pre><code><script src="js/three.min.js"></script></code></pre>
<li>Avoid scrollbars:</li>
<pre><code><style type="text/css">
body { margin: 0px; overflow: hidden; }
</style>
</code></pre>
<li>There is an inline code editor<br>
</ul>
<a href="http://mrdoob.com/projects/code-editor/">mrdoob.com/projects/code-editor/</a></li>
</div>
<div class="step slide" data-x="1000" data-y="1000" data-z="4000" data-rotate="10" data-scale="1"">
<h1>Setup Scene and Renderer</h1>
<pre><code style="javascript"><script src="js/three.min.js"></script><script>
var scene, renderer;
init();
function init() {
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,
window.innerHeight);
document.body.appendChild(renderer.domElement);
}
</script>
</code></pre>
</div>
<div class="step slide" data-x="2000" data-y="1000" data-z="3000" data-rotate="-10" data-scale="1"">
<h1>Camera and Render</h1>
<pre><code>var camera;
//...
camera = new THREE.PerspectiveCamera(45,
window.innerWidth / window.innerHeight, 1, 10000);
camera.position.set(0, 0, 1000);
</code></pre>
To render each frame we call:
<pre><code>renderer.render(scene, camera);
</code></pre>
</div>
<div class="step slide" data-x="3000" data-y="1000" data-z="2000" data-rotate="10" data-scale="1"">
<h1>Adding a Mesh</h1>
<pre><code>// Create a Geometry
geometry = new THREE.CubeGeometry(200, 200, 200);
// Create a Material
material = new THREE.MeshLambertMaterial({
color : 0xffffff,
});
// Create a Mesh with the Geometry and the Material
mesh = new THREE.Mesh(geometry, material);
// Add it to the Scene
scene.add(mesh);</code></pre>
</div>
<div class="step slide" data-x="4000" data-y="1000" data-z="1000" data-rotate="0" data-scale="1"">
<h1>Adding a Light</h1>
<pre><code>// Create a Point Light
pointLight = new THREE.PointLight( 0xffffff );
// Set the point light position
pointLight.position =
new THREE.Vector3(1000,1000,0);
// Add the light to the scene
scene.add( pointLight );</code></pre>
More light types:
<ul>
<li>AmbientLight</li>
<li>DirectionalLight</li>
<li>SpotLight</li>
</div>
<div class="step slide" data-x="5000" data-y="1000" data-z="0" data-rotate="-10" data-scale="1"">
<h1>Animating</h1>
<pre><code>animate();
function animate() {
// Ask for the next frame
requestAnimationFrame(animate);
// Rotate Cube
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.02;
// Render Frame
renderer.render(scene, camera);
}</code></pre>
</div>
<div class="step slide" data-x="10000" data-y="1000" data-z="10000" data-scale="1"">
<h1>A Basic WebGL Scene</h1>
<iframe src="step1.html" width="800" height="500"></iframe>
</div>
<div class="step slide" data-x="0" data-y="2000" data-z="5000" data-rotate="0" data-scale="1"">
<h1>Loading models</h1>
<ul>
<li>Download from the Three.js GitHub:<br>
examples/js/loaders/ColladaLoader.js</li>
<li>Place it in js/ and load it:</li>
<pre><code><script src="js/ColladaLoader.js"></script></code></pre>
</ul>
<br>
To test model load in local you will need to start Chrome with:<br><b>chrome --allow-file-access-from-files</b>
</div>
<div class="step slide" data-x="1000" data-y="2000" data-z="4000" data-rotate="10" data-scale="1"">
<h1>Export a Model with SketchUp</h1>
<center><img src="img/sketchup.png"></center>
<ul>
<li>Download the skp model from...</li>
<li>We will use the collada format (.dae)</li>
<li>Be careful with groups</li>
</ul>
</div>
<div class="step slide" data-x="2000" data-y="2000" data-z="3000" data-rotate="-10" data-scale="1"">
<h1>Load the model</h1>
Using the ColladaLoader
<pre><code>var loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
loader.load('./car.dae', function(collada) {
// Onload function
car = collada.scene;
init();
animate();
});</code></pre>
</div>
<div class="step slide" data-x="3000" data-y="2000" data-z="2000" data-rotate="10" data-scale="1"">
<h1>Camera</h1>
Make the camera look at (0,0,0):
<pre><code>camera = new THREE.PerspectiveCamera(45,
window.innerWidth / window.innerHeight, 1, 10000);
camera.position = new THREE.Vector3(0, 1000, 1000);
camera.lookAt(new THREE.Vector3(0, 0, 0));</code></pre>
<img src="img/camera.jpg" style="float: right">
</div>
<div class="step slide" data-x="4000" data-y="2000" data-z="1000" data-rotate="-10" data-scale="1"">
<h1>Animation</h1>
We will rotate the car in each frame
<pre><code>function animate() {
requestAnimationFrame(animate);
car.rotation.y += 0.02;
renderer.render(scene, camera);
}</code></pre>
</div>
<div class="step slide" data-x="10000" data-y="2000" data-z="10000" data-scale="1"">
<h1>The Loaded Model</h1>
<iframe src="step2.html" width="800" height="500"></iframe>
</div>
<div class="step slide" data-x="0" data-y="3000" data-z="5000" data-rotate="0" data-scale="1"">
<h1>A Basic Game Scenery</h1>
Setup keyboard control:
<pre><code>keyDown = new Array();
for (i = 0; i < 300; i++) keyDown[i] = false;
document.onkeydown = function(event) {
keyDown[event.keyCode] = true;
}
document.onkeyup = function(event) {
keyDown[event.keyCode] = false;
}</code></pre>
</div>
<div class="step slide" data-x="1000" data-y="3000" data-z="4000" data-rotate="10" data-scale="1"">
<h1>Accelerate and brake</h1>
We will add this code in the animate() loop:
<pre><code>if (keyDown[38]) { // if Up is pressed
if (speed < 100) speed += 0.5;
} else {
speed -= 1;
if (speed < 0) speed = 0;
}</code></pre>
</div>
<div class="step slide" data-x="2000" data-y="3000" data-z="3000" data-rotate="0" data-scale="1"">
<h1>Rotate and move the car</h1>
Rotate on left/right press:
<pre><code>if (speed > 0) {
if (keyDown[37]) car.rotation.y += 0.02;
if (keyDown[39]) car.rotation.y -= 0.02;
}</code></pre>
And move the car:
<pre><code>car.position.x += speed * Math.sin(car.rotation.y);
car.position.z += speed * Math.cos(car.rotation.y);</code></pre>
</div>
<div class="step slide" data-x="3000" data-y="3000" data-z="2000" data-rotate="-10" data-scale="1"">
<h1>And more...</h1>
Camera looking at the car:
<pre><code>camera.lookAt(car.position);</code></pre>
</div>
<div class="step slide" data-x="4000" data-y="3000" data-z="1000" data-rotate="10" data-scale="1"">
<h1>Create a Plane</h1>
As a reference for the car movement
<pre><code>geometry = new THREE.PlaneGeometry(
50000, 50000, 100, 100);
material = new THREE.MeshBasicMaterial({
color : 0xcccccc,
wireframe : true});
mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = Math.PI / 2;
scene.add(mesh);</code></pre>
</div>
<div class="step slide" data-x="10000" data-y="3000" data-z="10000" data-scale="1"">
<h1>Play!</h1>
<iframe src="step3.html" width="800" height="500"></iframe>
</ul>
Accelerate with ↑, steer with ← and →
</div>
<div class="step slide" data-x="0" data-y="4000" data-z="5000" data-rotate="0">
<h1>And More Three.js Resources</h1>
<ul>
<li>Official Three.js doc <a href="http://mrdoob.github.com/three.js/docs/57/">http://mrdoob.github.com/three.js/docs/57/</a></li>
<li>Basics of Three.js (Ilmari Heikkinen) <a href="http://fhtr.org/BasicsOfThreeJS/">http://fhtr.org/BasicsOfThreeJS/</a></li>
<li>Intro at AeroTwist by Paul Lewis <a href="http://www.aerotwist.com/tutorials/getting-started-with-three-js/">http://www.aerotwist.com/tutorials/getting-started-with-three-js/</a></li>
</ul>
</div>
<div class="step slide" data-x="0" data-y="4000" data-z="5000" data-rotate="0">
<h1>Finally, for a game you will also need Sound</h1>
<ul>
<li>WebAudio (only for webkit/based browsers, soon on FF)</a></li>
</ul>
<h1>And Maybe Physics!</h1>
<ul>
<li>Physijs <a href="http://chandlerprall.github.com/Physijs/">http://chandlerprall.github.com/Physijs/</a></li>
<li>Ammo.js <a href="https://github.com/kripken/ammo.js/">https://github.com/kripken/ammo.js/</a></li>
</ul>
</div>
<div class="step slide" data-x="1000" data-y="4000" data-z="4000" data-rotate="10">
<h1>Thanks</h1>
<center>
<br/>Questions?<br/><br/>
<br/>
@albertoruibal<br/>
<br/>
<br/>
Code:
<a href='http://github.com/albertoruibal/codelab_three.js'>github.com/albertoruibal/codelab_three.js</a>
</center>
</div>
</div>
<link rel="stylesheet" href="css/tomorrow-night-bright.css">
<script src="js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script src="js/impress.js"></script>
<script>
impress().init();
</script>
</body>
</html>