| OLD | NEW |
| (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_playerPosition = [0, 0, 0]; |
| 93 var g_playerDirection = 0; |
| 94 var g_animParam; |
| 95 var g_playerMode; |
| 96 var g_eye = [15, 25, 50]; |
| 97 var g_target = [0, 10, 0]; |
| 98 var g_up = [0, 1, 0]; |
| 99 var g_viewMatrix; |
| 100 var g_moveMatrix; |
| 101 var g_canJump = true; |
| 102 var g_jumping = false; |
| 103 var g_playerVelocity = [0, 0, 0]; |
| 104 var g_targetDirection = 0; |
| 105 var g_particleSystem; |
| 106 var g_poofEmitter; |
| 107 var g_poof; |
| 108 |
| 109 var g_anims = { |
| 110 idle1: {startFrame: 0, endFrame: 30}, |
| 111 walk: {startFrame: 31, endFrame: 71}, |
| 112 jumpStart: {startFrame: 72, endFrame: 87}, |
| 113 jumpUp: {startFrame: 87, endFrame: 87}, |
| 114 jumpCrest: {startFrame: 87, endFrame: 91}, |
| 115 jumpFall: {startFrame: 91, endFrame: 91}, |
| 116 jumpLand: {startFrame: 91, endFrame: 110}, |
| 117 run: {startFrame: 111, endFrame: 127}, |
| 118 idle2: {startFrame: 128, endFrame: 173}, |
| 119 idle3: {startFrame: 174, endFrame: 246}, |
| 120 idle4: {startFrame: 247, endFrame: 573}}; |
| 121 |
| 122 var g_animation; |
| 123 var g_animTimer; |
| 124 |
| 125 /** |
| 126 * Updates the projection matrix. |
| 127 */ |
| 128 function updateProjection() { |
| 129 g_viewInfo.drawContext.projection = g_math.matrix4.perspective( |
| 130 g_math.degToRad(45), // field of view. |
| 131 g_o3dWidth / g_o3dHeight, // aspect ratio |
| 132 0.1, // Near plane. |
| 133 5000); // Far plane. |
| 134 } |
| 135 |
| 136 /** |
| 137 * Given a view matrix computes an movement matrix to make it easy |
| 138 * to move something relative to the camera view in the XZ plane. |
| 139 * @param {!o3djs.math.Matrix4} viewMatrix A view matrix. |
| 140 * @return {!o3djs.math.Matrix4} A movement matrix. |
| 141 */ |
| 142 function computeMoveMatrixFromViewMatrix(viewMatrix) { |
| 143 var cameraMatrix = g_math.matrix4.inverse(viewMatrix); |
| 144 var xAxis = g_math.cross([0, 1, 0], cameraMatrix[2].slice(0, 3)); |
| 145 var zAxis = g_math.cross(xAxis, [0, 1, 0]); |
| 146 return [ |
| 147 xAxis.concat(0), |
| 148 [0, 1, 0, 0], |
| 149 zAxis.concat(0), |
| 150 [0, 0, 0, 1]]; |
| 151 } |
| 152 |
| 153 /* |
| 154 * Updates the camera. |
| 155 */ |
| 156 function updateCamera() { |
| 157 g_viewMatrix = g_math.matrix4.lookAt(g_eye, g_target, g_up); |
| 158 g_viewInfo.drawContext.view = g_viewMatrix; |
| 159 g_moveMatrix = computeMoveMatrixFromViewMatrix(g_viewMatrix); |
| 160 }; |
| 161 |
| 162 /** |
| 163 * Updates global variables of the client's size if they have changed. |
| 164 */ |
| 165 function updateClientSize() { |
| 166 var newWidth = g_client.width; |
| 167 var newHeight = g_client.height; |
| 168 if (g_o3dWidth != newWidth || g_o3dHeight != newHeight) { |
| 169 g_o3dWidth = newWidth; |
| 170 g_o3dHeight = newHeight; |
| 171 updateProjection(); |
| 172 } |
| 173 } |
| 174 |
| 175 /** |
| 176 * Start an animation. |
| 177 * @param {!Object} animation to start. |
| 178 */ |
| 179 function startAnimation(animation) { |
| 180 g_animation = animation; |
| 181 g_animTimer = g_animation.startTime; |
| 182 } |
| 183 |
| 184 /** |
| 185 * Starts a new mode. |
| 186 * @param {number} mode Mode to start. |
| 187 */ |
| 188 function startMode(mode) { |
| 189 if (mode != g_playerMode) { |
| 190 g_playerMode = mode; |
| 191 mode.init(); |
| 192 } |
| 193 } |
| 194 |
| 195 /** |
| 196 * Creates the client area. |
| 197 */ |
| 198 function init() { |
| 199 o3djs.util.makeClients(initStep2); |
| 200 } |
| 201 |
| 202 /** |
| 203 * Initializes O3D and creates one shape. |
| 204 * @param {Array} clientElements Array of o3d object elements. |
| 205 */ |
| 206 function initStep2(clientElements) { |
| 207 // Initializes global variables and libraries. |
| 208 g_o3dElement = clientElements[0]; |
| 209 g_o3d = g_o3dElement.o3d; |
| 210 g_math = o3djs.math; |
| 211 g_client = g_o3dElement.client; |
| 212 |
| 213 // Convert anim frames to anim times. |
| 214 for (var animName in g_anims) { |
| 215 var anim = g_anims[animName]; |
| 216 anim.startTime = anim.startFrame / 30; |
| 217 anim.endTime = anim.endFrame / 30; |
| 218 anim.timeRange = anim.endTime - anim.startTime; |
| 219 } |
| 220 |
| 221 // Creates a pack to manage our resources/assets |
| 222 g_pack = g_client.createPack(); |
| 223 |
| 224 g_root = g_pack.createObject('Transform'); |
| 225 |
| 226 g_viewInfo = o3djs.rendergraph.createBasicView( |
| 227 g_pack, |
| 228 g_root, |
| 229 g_client.renderGraphRoot); |
| 230 |
| 231 updateCamera(); |
| 232 |
| 233 var checkerMaterial = o3djs.material.createMaterialFromFile( |
| 234 g_pack, 'shaders/checker.shader', g_viewInfo.performanceDrawList); |
| 235 |
| 236 g_globalParams = o3djs.material.createAndBindStandardParams(g_pack); |
| 237 g_globalParams.lightWorldPos.value = [30, 60, 40]; |
| 238 g_globalParams.lightColor.value = [1, 1, 1, 1]; |
| 239 |
| 240 // Create a ground plane. |
| 241 var shape = o3djs.primitives.createPlane( |
| 242 g_pack, checkerMaterial, 100, 100, 10, 10); |
| 243 var transform = g_pack.createObject('Transform'); |
| 244 transform.parent = g_root; |
| 245 transform.addShape(shape); |
| 246 |
| 247 // Load character. |
| 248 var transform = g_pack.createObject('Transform'); |
| 249 g_playerTransform = transform; |
| 250 var playerPack = g_client.createPack(); |
| 251 var paramObject = playerPack.createObject('ParamObject'); |
| 252 g_animParam = paramObject.createParam('animTime', 'ParamFloat'); |
| 253 o3djs.scene.loadScene(g_client, playerPack, g_playerTransform, |
| 254 'assets/character.o3dtgz', initStep3, |
| 255 {opt_animSource: g_animParam}); |
| 256 } |
| 257 |
| 258 /** |
| 259 * Continue setting up after the model has loaded. |
| 260 */ |
| 261 function initStep3(playerPack, parent, exception) { |
| 262 o3djs.pack.preparePack(playerPack, g_viewInfo); |
| 263 o3djs.material.bindParams(playerPack, g_globalParams); |
| 264 g_playerTransform.parent = g_root; |
| 265 |
| 266 g_particleSystem = o3djs.particles.createParticleSystem(g_pack, g_viewInfo); |
| 267 g_poofEmitter = g_particleSystem.createParticleEmitter(); |
| 268 g_poofEmitter.setState(o3djs.particles.ParticleStateIds.ADD); |
| 269 g_poofEmitter.setColorRamp( |
| 270 [1, 1, 1, 0.3, |
| 271 1, 1, 1, 0]); |
| 272 g_poofEmitter.setParameters({ |
| 273 numParticles: 30, |
| 274 lifeTime: 0.5, |
| 275 startTime: 0, |
| 276 startSize: 5, |
| 277 endSize: 10, |
| 278 spinSpeedRange: 10}, |
| 279 function(index, parameters) { |
| 280 var angle = Math.random() * 2 * Math.PI; |
| 281 parameters.velocity = g_math.matrix4.transformPoint( |
| 282 g_math.matrix4.rotationY(angle), [25, 2.5, 0]); |
| 283 parameters.acceleration = g_math.mulVectorVector( |
| 284 parameters.velocity, [-1.5, 1, -1.5]); |
| 285 }); |
| 286 g_poof = g_poofEmitter.createOneShot(g_root); |
| 287 |
| 288 // Setup a render callback for per frame processing. |
| 289 g_client.setRenderCallback(onRender); |
| 290 |
| 291 o3djs.event.addEventListener(g_o3dElement, 'keydown', onKeyDown); |
| 292 o3djs.event.addEventListener(g_o3dElement, 'keyup', onKeyUp); |
| 293 |
| 294 window.g_finished = true; // for selenium testing. |
| 295 window.o3d_prepForSelenium = function() { |
| 296 g_animParam.value = 0; |
| 297 g_animParam = {value: 0}; |
| 298 } |
| 299 } |
| 300 |
| 301 /** |
| 302 * Tracks key down events. |
| 303 * @param {Event} e keyboard event. |
| 304 */ |
| 305 function onKeyDown(e) { |
| 306 g_keyDown[e.keyCode] = true; |
| 307 } |
| 308 |
| 309 /** |
| 310 * Tracks key up events. |
| 311 * @param {Event} e keyboard event. |
| 312 */ |
| 313 function onKeyUp(e) { |
| 314 g_keyDown[e.keyCode] = false; |
| 315 } |
| 316 |
| 317 /** |
| 318 * Look at keys. |
| 319 */ |
| 320 function handleMoveKeys(elapsedTime) { |
| 321 var directionX = 0; |
| 322 var directionZ = 0; |
| 323 |
| 324 if (g_keyDown[37] || g_keyDown[65]) { directionX = -1; } |
| 325 if (g_keyDown[39] || g_keyDown[68]) { directionX = 1; } |
| 326 if (g_keyDown[38] || g_keyDown[87]) { directionZ = -1; } |
| 327 if (g_keyDown[40] || g_keyDown[83]) { directionZ = 1; } |
| 328 |
| 329 if (g_canJump) { |
| 330 if (g_keyDown[32]) { |
| 331 startMode(g_modes.JUMP); |
| 332 } |
| 333 } else { |
| 334 if (!g_jumping) { |
| 335 if (!g_keyDown[32]) { |
| 336 g_canJump = true; |
| 337 } |
| 338 } |
| 339 } |
| 340 |
| 341 if (directionX != 0 || directionZ != 0) { |
| 342 if (!g_jumping) { |
| 343 startMode(g_modes.WALK); |
| 344 } |
| 345 var moveTranslation = g_math.matrix4.transformPoint( |
| 346 g_moveMatrix, |
| 347 [MOVE_VELOCITY * directionX, 0, MOVE_VELOCITY * directionZ]); |
| 348 g_targetDirection = Math.atan2(moveTranslation[0], |
| 349 moveTranslation[2]); |
| 350 g_playerVelocity[0] = moveTranslation[0]; |
| 351 g_playerVelocity[2] = moveTranslation[2]; |
| 352 } else { |
| 353 g_playerVelocity[0] = 0; |
| 354 g_playerVelocity[2] = 0; |
| 355 if (!g_jumping) { |
| 356 startMode(g_modes.IDLE); |
| 357 } |
| 358 } |
| 359 } |
| 360 |
| 361 /** |
| 362 * Moves the camera. |
| 363 */ |
| 364 function moveCamera() { |
| 365 var newTarget = [g_playerPosition[0], 10, g_playerPosition[2]]; |
| 366 g_target = g_math.lerpVector(g_target, newTarget, 0.03); |
| 367 updateCamera(); |
| 368 } |
| 369 |
| 370 /** |
| 371 * Updates the direction. |
| 372 * @param {number} elapsedTime Time elasped since last frame. |
| 373 */ |
| 374 function updateDirection(elapsedTime) { |
| 375 g_playerDirection = g_math.lerpRadian(g_playerDirection, g_targetDirection, |
| 376 0.2); |
| 377 } |
| 378 |
| 379 /** |
| 380 * Adds gravity to velocity. |
| 381 * @param {number} elapsedTime Time elasped since last frame. |
| 382 */ |
| 383 function calculateGravity(elapsedTime) { |
| 384 g_playerVelocity[1] += GRAVITY * elapsedTime; |
| 385 } |
| 386 |
| 387 /** |
| 388 * Updates the player's position. |
| 389 * @param {number} elapsedTime Time elasped since last frame. |
| 390 */ |
| 391 function updateMovement(elapsedTime) { |
| 392 g_playerPosition = g_math.addVector(g_playerPosition, |
| 393 g_math.mulVectorScalar(g_playerVelocity, |
| 394 elapsedTime)); |
| 395 } |
| 396 |
| 397 var g_modes = {}; |
| 398 |
| 399 /** |
| 400 * Handle idle mode. |
| 401 */ |
| 402 g_modes.IDLE = { |
| 403 init: function() { |
| 404 startAnimation(g_anims.idle1); |
| 405 g_jumping = false; |
| 406 }, |
| 407 handle: function(elapsedTime) { |
| 408 updateDirection(elapsedTime); |
| 409 g_animTimer += elapsedTime; |
| 410 if (g_animTimer >= g_animation.endTime) { |
| 411 // Pick an idle at random. |
| 412 var idle = 0; |
| 413 if (Math.random() > 0.8) { |
| 414 // Choose another idle. |
| 415 idle = Math.floor(Math.random() * 10 / 3); |
| 416 if (idle > 3) { |
| 417 idle = 3; |
| 418 } |
| 419 } |
| 420 var idleName = 'idle' + (idle + 1); |
| 421 startAnimation(g_anims[idleName]); |
| 422 } |
| 423 } |
| 424 }; |
| 425 |
| 426 /** |
| 427 * Handle walk mode. |
| 428 */ |
| 429 g_modes.WALK = { |
| 430 init: function() { |
| 431 startAnimation(g_anims.run); |
| 432 g_jumping = false; |
| 433 }, |
| 434 handle: function(elapsedTime) { |
| 435 updateDirection(elapsedTime); |
| 436 updateMovement(elapsedTime); |
| 437 g_animTimer += elapsedTime; |
| 438 if (g_animTimer >= g_animation.endTime) { |
| 439 g_animTimer = g_math.modClamp(g_animTimer, |
| 440 g_animation.timeRange, |
| 441 g_animation.startTime); |
| 442 } |
| 443 } |
| 444 }; |
| 445 |
| 446 /** |
| 447 * Handle jump mode. |
| 448 */ |
| 449 g_modes.JUMP = { |
| 450 init: function() { |
| 451 startAnimation(g_anims.jumpStart); |
| 452 g_jumping = true; |
| 453 g_canJump = false; |
| 454 g_playerVelocity[1] = JUMP_VELOCITY; |
| 455 }, |
| 456 handle: function(elapsedTime) { |
| 457 g_animTimer += elapsedTime; |
| 458 if (g_animTimer >= g_animation.endTime) { |
| 459 startMode(g_modes.JUMP_UP); |
| 460 } |
| 461 } |
| 462 }; |
| 463 |
| 464 g_modes.JUMP_UP = { |
| 465 init: function() { |
| 466 startAnimation(g_anims.jumpUp); |
| 467 }, |
| 468 handle: function(elapsedTime) { |
| 469 updateDirection(elapsedTime); |
| 470 calculateGravity(elapsedTime); |
| 471 updateMovement(elapsedTime); |
| 472 if (g_playerVelocity[1] < 10) { |
| 473 startMode(g_modes.JUMP_CREST); |
| 474 } |
| 475 } |
| 476 }; |
| 477 |
| 478 g_modes.JUMP_CREST = { |
| 479 init: function() { |
| 480 startAnimation(g_anims.jumpCrest); |
| 481 }, |
| 482 handle: function(elapsedTime) { |
| 483 updateDirection(elapsedTime); |
| 484 calculateGravity(elapsedTime); |
| 485 updateMovement(elapsedTime); |
| 486 g_animTimer += elapsedTime; |
| 487 if (g_animTimer >= g_animation.endTime) { |
| 488 startMode(g_modes.JUMP_FALL); |
| 489 } |
| 490 } |
| 491 }; |
| 492 |
| 493 g_modes.JUMP_FALL = { |
| 494 init: function() { |
| 495 startAnimation(g_anims.jumpFall); |
| 496 }, |
| 497 handle: function(elapsedTime) { |
| 498 updateDirection(elapsedTime); |
| 499 calculateGravity(elapsedTime); |
| 500 updateMovement(elapsedTime); |
| 501 if (g_playerPosition[1] <= 0) { |
| 502 startMode(g_modes.JUMP_LAND); |
| 503 g_playerPosition[1] = 0; |
| 504 g_playerVelocity[1] = 0; |
| 505 g_poof.trigger(g_playerPosition); |
| 506 } |
| 507 } |
| 508 }; |
| 509 |
| 510 g_modes.JUMP_LAND = { |
| 511 init: function() { |
| 512 startAnimation(g_anims.jumpLand); |
| 513 }, |
| 514 handle: function(elapsedTime) { |
| 515 updateDirection(elapsedTime); |
| 516 g_animTimer += elapsedTime; |
| 517 if (g_animTimer >= g_animation.endTime) { |
| 518 g_jumping = false; |
| 519 startMode(g_modes.IDLE); |
| 520 } |
| 521 } |
| 522 }; |
| 523 |
| 524 function updatePlayer() { |
| 525 g_animParam.value = g_animTimer; |
| 526 g_playerTransform.identity(); |
| 527 g_playerTransform.translate(g_playerPosition); |
| 528 g_playerTransform.rotateY(g_playerDirection); |
| 529 } |
| 530 |
| 531 /** |
| 532 * Called every frame. |
| 533 * @param {!o3d.RenderEvent} renderEvent Rendering Information. |
| 534 */ |
| 535 function onRender(renderEvent) { |
| 536 var elapsedTime = renderEvent.elapsedTime; |
| 537 |
| 538 updateClientSize(); |
| 539 handleMoveKeys(elapsedTime); |
| 540 g_playerMode.handle(elapsedTime); |
| 541 updatePlayer(); |
| 542 moveCamera(); |
| 543 }; |
| 544 |
| 545 /** |
| 546 * Remove any callbacks so they don't get called after the page has unloaded. |
| 547 */ |
| 548 function unload() { |
| 549 if (g_client) { |
| 550 g_client.cleanup(); |
| 551 } |
| 552 } |
| 553 </script> |
| 554 </head> |
| 555 <body> |
| 556 <table style="width: 100%; height:100%;"> |
| 557 <tr style="height: 50px;"><td> |
| 558 <div style="width: 100%; height: 50px; font-size: large;"> |
| 559 <img src="assets/colorbar.png" width="100%" height="10px"/><br/> |
| 560 Google I/O 2009 O3D Sample |
| 561 </div> |
| 562 </td></tr> |
| 563 <tr style="height: 100%;"><td> |
| 564 <div style="width: 100%; height: 100%;"> |
| 565 <div id="o3d" style="width: 100%; height: 100%;"></div> |
| 566 </div> |
| 567 </td></tr> |
| 568 </table> |
| 569 </body> |
| 570 </html> |
| OLD | NEW |