Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(686)

Side by Side Diff: samples/GoogleIO-2009/step10.html

Issue 149438: Add Google IO sample. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/o3d/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/GoogleIO-2009/step09.html ('k') | samples/GoogleIO-2009/step11.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 <!--
2 Copyright 2009, Google Inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following disclaimer
13 in the documentation and/or other materials provided with the
14 distribution.
15 * Neither the name of Google Inc. nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 -->
31 <!--
32 Google I/O O3D Sample.
33
34 This sample shows the steps to make a simple frame rate independent game.
35 -->
36 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
37 "http://www.w3.org/TR/html4/loose.dtd">
38 <html>
39 <head>
40 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
41 <title>
42 Google I/O O3D Sample
43 </title>
44 <style type="text/css">
45 html, body {
46 height: 100%;
47 margin: 0;
48 padding: 0;
49 border: none;
50 font-family: Arial, sans-serif;
51 }
52 </style>
53 <!-- Include sample javascript library functions-->
54 <script type="text/javascript" src="../o3djs/base.js"></script>
55
56 <!-- Our javascript code -->
57 <script type="text/javascript">
58 o3djs.require('o3djs.util');
59 o3djs.require('o3djs.math');
60 o3djs.require('o3djs.rendergraph');
61 o3djs.require('o3djs.primitives');
62 o3djs.require('o3djs.material');
63 o3djs.require('o3djs.particles');
64 o3djs.require('o3djs.scene');
65 o3djs.require('o3djs.pack');
66
67 // Events
68 // init() once the page has finished loading.
69 // unload() when the page is unloaded.
70 window.onload = init;
71 window.onunload= unload;
72
73 // constants
74 var MOVE_VELOCITY = 25; // in units per second.
75 var JUMP_VELOCITY = 100;
76 var GRAVITY = -500;
77
78 // global variables
79 var g_o3dElement;
80 var g_o3d;
81 var g_math;
82 var g_client;
83 var g_viewInfo;
84 var g_pack;
85 var g_root;
86 var g_globalParams;
87 var g_o3dWidth;
88 var g_o3dHeight;
89 var g_o3dElement;
90 var g_keyDown = []; // which keys are down by key code.
91 var g_playerTransform;
92 var g_playerXPosition = 0;
93 var g_playerYPosition = 0;
94 var g_playerZPosition = 0;
95 var g_eye = [15, 25, 50];
96 var g_target = [0, 10, 0];
97 var g_up = [0, 1, 0];
98 var g_viewMatrix;
99 var g_moveMatrix;
100 var g_canJump = true;
101 var g_jumping = false;
102 var g_playerYVelocity = 0;
103 var g_particleSystem;
104 var g_poofEmitter;
105 var g_poof;
106
107 /**
108 * Updates the projection matrix.
109 */
110 function updateProjection() {
111 g_viewInfo.drawContext.projection = g_math.matrix4.perspective(
112 g_math.degToRad(45), // field of view.
113 g_o3dWidth / g_o3dHeight, // aspect ratio
114 0.1, // Near plane.
115 5000); // Far plane.
116 }
117
118 /**
119 * Given a view matrix computes an movement matrix to make it easy
120 * to move something relative to the camera view in the XZ plane.
121 * @param {!o3djs.math.Matrix4} viewMatrix A view matrix.
122 * @return {!o3djs.math.Matrix4} A movement matrix.
123 */
124 function computeMoveMatrixFromViewMatrix(viewMatrix) {
125 var cameraMatrix = g_math.matrix4.inverse(viewMatrix);
126 var xAxis = g_math.cross([0, 1, 0], cameraMatrix[2].slice(0, 3));
127 var zAxis = g_math.cross(xAxis, [0, 1, 0]);
128 return [
129 xAxis.concat(0),
130 [0, 1, 0, 0],
131 zAxis.concat(0),
132 [0, 0, 0, 1]];
133 }
134
135 /*
136 * Updates the camera.
137 */
138 function updateCamera() {
139 g_viewMatrix = g_math.matrix4.lookAt(g_eye, g_target, g_up);
140 g_viewInfo.drawContext.view = g_viewMatrix;
141 g_moveMatrix = computeMoveMatrixFromViewMatrix(g_viewMatrix);
142 };
143
144 /**
145 * Updates global variables of the client's size if they have changed.
146 */
147 function updateClientSize() {
148 var newWidth = g_client.width;
149 var newHeight = g_client.height;
150 if (g_o3dWidth != newWidth || g_o3dHeight != newHeight) {
151 g_o3dWidth = newWidth;
152 g_o3dHeight = newHeight;
153 updateProjection();
154 }
155 }
156
157 /**
158 * Creates the client area.
159 */
160 function init() {
161 o3djs.util.makeClients(initStep2);
162 }
163
164 /**
165 * Initializes O3D and creates one shape.
166 * @param {Array} clientElements Array of o3d object elements.
167 */
168 function initStep2(clientElements) {
169 // Initializes global variables and libraries.
170 g_o3dElement = clientElements[0];
171 g_o3d = g_o3dElement.o3d;
172 g_math = o3djs.math;
173 g_client = g_o3dElement.client;
174
175 // Creates a pack to manage our resources/assets
176 g_pack = g_client.createPack();
177
178 g_root = g_pack.createObject('Transform');
179
180 g_viewInfo = o3djs.rendergraph.createBasicView(
181 g_pack,
182 g_root,
183 g_client.renderGraphRoot);
184
185 updateCamera();
186
187 var checkerMaterial = o3djs.material.createMaterialFromFile(
188 g_pack, 'shaders/checker.shader', g_viewInfo.performanceDrawList);
189
190 g_globalParams = o3djs.material.createAndBindStandardParams(g_pack);
191 g_globalParams.lightWorldPos.value = [30, 60, 40];
192 g_globalParams.lightColor.value = [1, 1, 1, 1];
193
194 // Create a ground plane.
195 var shape = o3djs.primitives.createPlane(
196 g_pack, checkerMaterial, 100, 100, 10, 10);
197 var transform = g_pack.createObject('Transform');
198 transform.parent = g_root;
199 transform.addShape(shape);
200
201 // Load character.
202 var transform = g_pack.createObject('Transform');
203 g_playerTransform = transform;
204 var playerPack = g_client.createPack();
205 o3djs.scene.loadScene(g_client, playerPack, g_playerTransform,
206 'assets/character.o3dtgz', initStep3);
207 }
208
209 /**
210 * Continue setting up after the model has loaded.
211 */
212 function initStep3(playerPack, parent, exception) {
213 o3djs.pack.preparePack(playerPack, g_viewInfo);
214 o3djs.material.bindParams(playerPack, g_globalParams);
215 g_playerTransform.parent = g_root;
216
217 g_particleSystem = o3djs.particles.createParticleSystem(g_pack, g_viewInfo);
218 g_poofEmitter = g_particleSystem.createParticleEmitter();
219 g_poofEmitter.setState(o3djs.particles.ParticleStateIds.ADD);
220 g_poofEmitter.setColorRamp(
221 [1, 1, 1, 0.3,
222 1, 1, 1, 0]);
223 g_poofEmitter.setParameters({
224 numParticles: 30,
225 lifeTime: 0.5,
226 startTime: 0,
227 startSize: 5,
228 endSize: 10,
229 spinSpeedRange: 10},
230 function(index, parameters) {
231 var angle = Math.random() * 2 * Math.PI;
232 parameters.velocity = g_math.matrix4.transformPoint(
233 g_math.matrix4.rotationY(angle), [25, 2.5, 0]);
234 parameters.acceleration = g_math.mulVectorVector(
235 parameters.velocity, [-1.5, 1, -1.5]);
236 });
237 g_poof = g_poofEmitter.createOneShot(g_root);
238
239 // Setup a render callback for per frame processing.
240 g_client.setRenderCallback(onRender);
241
242 o3djs.event.addEventListener(g_o3dElement, 'keydown', onKeyDown);
243 o3djs.event.addEventListener(g_o3dElement, 'keyup', onKeyUp);
244
245 window.g_finished = true; // for selenium testing.
246 }
247
248 /**
249 * Tracks key down events.
250 * @param {Event} e keyboard event.
251 */
252 function onKeyDown(e) {
253 g_keyDown[e.keyCode] = true;
254 }
255
256 /**
257 * Tracks key up events.
258 * @param {Event} e keyboard event.
259 */
260 function onKeyUp(e) {
261 g_keyDown[e.keyCode] = false;
262 }
263
264 /**
265 * Look at keys.
266 */
267 function handleMoveKeys(elapsedTime) {
268 var directionX = 0;
269 var directionZ = 0;
270
271 if (g_keyDown[37] || g_keyDown[65]) { directionX = -1; }
272 if (g_keyDown[39] || g_keyDown[68]) { directionX = 1; }
273 if (g_keyDown[38] || g_keyDown[87]) { directionZ = -1; }
274 if (g_keyDown[40] || g_keyDown[83]) { directionZ = 1; }
275
276 if (g_canJump) {
277 if (g_keyDown[32]) {
278 g_jumping = true;
279 g_canJump = false;
280 g_playerYVelocity = JUMP_VELOCITY;
281 }
282 } else {
283 if (g_jumping) {
284 g_playerYVelocity += GRAVITY * elapsedTime;
285 g_playerYPosition += g_playerYVelocity * elapsedTime;
286 if (g_playerYPosition <= 0) {
287 g_playerYPosition = 0;
288 g_poof.trigger(
289 [g_playerXPosition, g_playerYPosition, g_playerZPosition]);
290 g_jumping = false;
291 }
292 } else {
293 if (!g_keyDown[32]) {
294 g_canJump = true;
295 }
296 }
297 }
298
299 if (directionX != 0 || directionZ != 0) {
300 var moveTranslation = g_math.matrix4.transformPoint(
301 g_moveMatrix,
302 [MOVE_VELOCITY * directionX * elapsedTime,
303 0,
304 MOVE_VELOCITY * directionZ * elapsedTime]);
305
306 g_playerXPosition += moveTranslation[0];
307 g_playerZPosition += moveTranslation[2];
308 }
309
310 g_playerTransform.identity();
311 g_playerTransform.translate(
312 g_playerXPosition, g_playerYPosition, g_playerZPosition);
313 }
314
315 /**
316 * Move the camera.
317 */
318 function moveCamera() {
319 var newTarget = [g_playerXPosition, 10, g_playerZPosition];
320 g_target = g_math.lerpVector(g_target, newTarget, 0.03);
321 updateCamera();
322 }
323
324 /**
325 * Called every frame.
326 * @param {!o3d.RenderEvent} renderEvent Rendering Information.
327 */
328 function onRender(renderEvent) {
329 var elapsedTime = renderEvent.elapsedTime;
330
331 updateClientSize();
332 handleMoveKeys(elapsedTime);
333 moveCamera();
334 };
335
336 /**
337 * Remove any callbacks so they don't get called after the page has unloaded.
338 */
339 function unload() {
340 if (g_client) {
341 g_client.cleanup();
342 }
343 }
344 </script>
345 </head>
346 <body>
347 <table style="width: 100%; height:100%;">
348 <tr style="height: 50px;"><td>
349 <div style="width: 100%; height: 50px; font-size: large;">
350 <img src="assets/colorbar.png" width="100%" height="10px"/><br/>
351 Google I/O 2009 O3D Sample
352 </div>
353 </td></tr>
354 <tr style="height: 100%;"><td>
355 <div style="width: 100%; height: 100%;">
356 <div id="o3d" style="width: 100%; height: 100%;"></div>
357 </div>
358 </td></tr>
359 </table>
360 </body>
361 </html>
OLDNEW
« no previous file with comments | « samples/GoogleIO-2009/step09.html ('k') | samples/GoogleIO-2009/step11.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698