| 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_playerXPosition = 0; |
| 93 var g_playerYPosition = 0; |
| 94 var g_playerZPosition = 0; |
| 95 var g_playerDirection = 0; |
| 96 var g_animParam; |
| 97 var g_playerMode; |
| 98 var g_eye = [15, 25, 50]; |
| 99 var g_target = [0, 10, 0]; |
| 100 var g_up = [0, 1, 0]; |
| 101 var g_viewMatrix; |
| 102 var g_moveMatrix; |
| 103 var g_canJump = true; |
| 104 var g_jumping = false; |
| 105 var g_playerYVelocity = 0; |
| 106 var g_particleSystem; |
| 107 var g_poofEmitter; |
| 108 var g_poof; |
| 109 |
| 110 var IDLE_START_TIME = 247 / 30; |
| 111 var IDLE_END_TIME = 573 / 30; |
| 112 var IDLE_TIME_RANGE = IDLE_END_TIME - IDLE_START_TIME; |
| 113 |
| 114 var g_animTimer = IDLE_START_TIME; |
| 115 |
| 116 /** |
| 117 * Updates the projection matrix. |
| 118 */ |
| 119 function updateProjection() { |
| 120 g_viewInfo.drawContext.projection = g_math.matrix4.perspective( |
| 121 g_math.degToRad(45), // field of view. |
| 122 g_o3dWidth / g_o3dHeight, // aspect ratio |
| 123 0.1, // Near plane. |
| 124 5000); // Far plane. |
| 125 } |
| 126 |
| 127 /** |
| 128 * Given a view matrix computes an movement matrix to make it easy |
| 129 * to move something relative to the camera view in the XZ plane. |
| 130 * @param {!o3djs.math.Matrix4} viewMatrix A view matrix. |
| 131 * @return {!o3djs.math.Matrix4} A movement matrix. |
| 132 */ |
| 133 function computeMoveMatrixFromViewMatrix(viewMatrix) { |
| 134 var cameraMatrix = g_math.matrix4.inverse(viewMatrix); |
| 135 var xAxis = g_math.cross([0, 1, 0], cameraMatrix[2].slice(0, 3)); |
| 136 var zAxis = g_math.cross(xAxis, [0, 1, 0]); |
| 137 return [ |
| 138 xAxis.concat(0), |
| 139 [0, 1, 0, 0], |
| 140 zAxis.concat(0), |
| 141 [0, 0, 0, 1]]; |
| 142 } |
| 143 |
| 144 /* |
| 145 * Updates the camera. |
| 146 */ |
| 147 function updateCamera() { |
| 148 g_viewMatrix = g_math.matrix4.lookAt(g_eye, g_target, g_up); |
| 149 g_viewInfo.drawContext.view = g_viewMatrix; |
| 150 g_moveMatrix = computeMoveMatrixFromViewMatrix(g_viewMatrix); |
| 151 }; |
| 152 |
| 153 /** |
| 154 * Updates global variables of the client's size if they have changed. |
| 155 */ |
| 156 function updateClientSize() { |
| 157 var newWidth = g_client.width; |
| 158 var newHeight = g_client.height; |
| 159 if (g_o3dWidth != newWidth || g_o3dHeight != newHeight) { |
| 160 g_o3dWidth = newWidth; |
| 161 g_o3dHeight = newHeight; |
| 162 updateProjection(); |
| 163 } |
| 164 } |
| 165 |
| 166 /** |
| 167 * Creates the client area. |
| 168 */ |
| 169 function init() { |
| 170 o3djs.util.makeClients(initStep2); |
| 171 } |
| 172 |
| 173 /** |
| 174 * Initializes O3D and creates one shape. |
| 175 * @param {Array} clientElements Array of o3d object elements. |
| 176 */ |
| 177 function initStep2(clientElements) { |
| 178 // Initializes global variables and libraries. |
| 179 g_o3dElement = clientElements[0]; |
| 180 g_o3d = g_o3dElement.o3d; |
| 181 g_math = o3djs.math; |
| 182 g_client = g_o3dElement.client; |
| 183 |
| 184 // Creates a pack to manage our resources/assets |
| 185 g_pack = g_client.createPack(); |
| 186 |
| 187 g_root = g_pack.createObject('Transform'); |
| 188 |
| 189 g_viewInfo = o3djs.rendergraph.createBasicView( |
| 190 g_pack, |
| 191 g_root, |
| 192 g_client.renderGraphRoot); |
| 193 |
| 194 updateCamera(); |
| 195 |
| 196 var checkerMaterial = o3djs.material.createMaterialFromFile( |
| 197 g_pack, 'shaders/checker.shader', g_viewInfo.performanceDrawList); |
| 198 |
| 199 g_globalParams = o3djs.material.createAndBindStandardParams(g_pack); |
| 200 g_globalParams.lightWorldPos.value = [30, 60, 40]; |
| 201 g_globalParams.lightColor.value = [1, 1, 1, 1]; |
| 202 |
| 203 // Create a ground plane. |
| 204 var shape = o3djs.primitives.createPlane( |
| 205 g_pack, checkerMaterial, 100, 100, 10, 10); |
| 206 var transform = g_pack.createObject('Transform'); |
| 207 transform.parent = g_root; |
| 208 transform.addShape(shape); |
| 209 |
| 210 // Load character. |
| 211 var transform = g_pack.createObject('Transform'); |
| 212 g_playerTransform = transform; |
| 213 var playerPack = g_client.createPack(); |
| 214 var paramObject = playerPack.createObject('ParamObject'); |
| 215 g_animParam = paramObject.createParam('animTime', 'ParamFloat'); |
| 216 o3djs.scene.loadScene(g_client, playerPack, g_playerTransform, |
| 217 'assets/character.o3dtgz', initStep3, |
| 218 {opt_animSource: g_animParam}); |
| 219 } |
| 220 |
| 221 /** |
| 222 * Continue setting up after the model has loaded. |
| 223 */ |
| 224 function initStep3(playerPack, parent, exception) { |
| 225 o3djs.pack.preparePack(playerPack, g_viewInfo); |
| 226 o3djs.material.bindParams(playerPack, g_globalParams); |
| 227 g_playerTransform.parent = g_root; |
| 228 |
| 229 g_particleSystem = o3djs.particles.createParticleSystem(g_pack, g_viewInfo); |
| 230 g_poofEmitter = g_particleSystem.createParticleEmitter(); |
| 231 g_poofEmitter.setState(o3djs.particles.ParticleStateIds.ADD); |
| 232 g_poofEmitter.setColorRamp( |
| 233 [1, 1, 1, 0.3, |
| 234 1, 1, 1, 0]); |
| 235 g_poofEmitter.setParameters({ |
| 236 numParticles: 30, |
| 237 lifeTime: 0.5, |
| 238 startTime: 0, |
| 239 startSize: 5, |
| 240 endSize: 10, |
| 241 spinSpeedRange: 10}, |
| 242 function(index, parameters) { |
| 243 var angle = Math.random() * 2 * Math.PI; |
| 244 parameters.velocity = g_math.matrix4.transformPoint( |
| 245 g_math.matrix4.rotationY(angle), [25, 2.5, 0]); |
| 246 parameters.acceleration = g_math.mulVectorVector( |
| 247 parameters.velocity, [-1.5, 1, -1.5]); |
| 248 }); |
| 249 g_poof = g_poofEmitter.createOneShot(g_root); |
| 250 |
| 251 // Setup a render callback for per frame processing. |
| 252 g_client.setRenderCallback(onRender); |
| 253 |
| 254 o3djs.event.addEventListener(g_o3dElement, 'keydown', onKeyDown); |
| 255 o3djs.event.addEventListener(g_o3dElement, 'keyup', onKeyUp); |
| 256 |
| 257 window.g_finished = true; // for selenium testing. |
| 258 window.o3d_prepForSelenium = function() { |
| 259 g_animParam.value = 0; |
| 260 g_animParam = {value: 0}; |
| 261 } |
| 262 } |
| 263 |
| 264 /** |
| 265 * Tracks key down events. |
| 266 * @param {Event} e keyboard event. |
| 267 */ |
| 268 function onKeyDown(e) { |
| 269 g_keyDown[e.keyCode] = true; |
| 270 } |
| 271 |
| 272 /** |
| 273 * Tracks key up events. |
| 274 * @param {Event} e keyboard event. |
| 275 */ |
| 276 function onKeyUp(e) { |
| 277 g_keyDown[e.keyCode] = false; |
| 278 } |
| 279 |
| 280 /** |
| 281 * Look at keys. |
| 282 */ |
| 283 function handleMoveKeys(elapsedTime) { |
| 284 var directionX = 0; |
| 285 var directionZ = 0; |
| 286 |
| 287 if (g_keyDown[37] || g_keyDown[65]) { directionX = -1; } |
| 288 if (g_keyDown[39] || g_keyDown[68]) { directionX = 1; } |
| 289 if (g_keyDown[38] || g_keyDown[87]) { directionZ = -1; } |
| 290 if (g_keyDown[40] || g_keyDown[83]) { directionZ = 1; } |
| 291 |
| 292 if (g_canJump) { |
| 293 if (g_keyDown[32]) { |
| 294 g_jumping = true; |
| 295 g_canJump = false; |
| 296 g_playerYVelocity = JUMP_VELOCITY; |
| 297 } |
| 298 } else { |
| 299 if (g_jumping) { |
| 300 g_playerYVelocity += GRAVITY * elapsedTime; |
| 301 g_playerYPosition += g_playerYVelocity * elapsedTime; |
| 302 if (g_playerYPosition <= 0) { |
| 303 g_playerYPosition = 0; |
| 304 g_poof.trigger( |
| 305 [g_playerXPosition, g_playerYPosition, g_playerZPosition]); |
| 306 g_jumping = false; |
| 307 } |
| 308 } else { |
| 309 if (!g_keyDown[32]) { |
| 310 g_canJump = true; |
| 311 } |
| 312 } |
| 313 } |
| 314 |
| 315 if (directionX != 0 || directionZ != 0) { |
| 316 var moveTranslation = g_math.matrix4.transformPoint( |
| 317 g_moveMatrix, |
| 318 [MOVE_VELOCITY * directionX * elapsedTime, |
| 319 0, |
| 320 MOVE_VELOCITY * directionZ * elapsedTime]); |
| 321 var targetDirection = Math.atan2(moveTranslation[0], moveTranslation[2]); |
| 322 g_playerDirection = g_math.lerpRadian(g_playerDirection, targetDirection, |
| 323 0.2); |
| 324 g_playerXPosition += moveTranslation[0]; |
| 325 g_playerZPosition += moveTranslation[2]; |
| 326 } |
| 327 |
| 328 g_playerTransform.identity(); |
| 329 g_playerTransform.translate( |
| 330 g_playerXPosition, g_playerYPosition, g_playerZPosition); |
| 331 g_playerTransform.rotateY(g_playerDirection); |
| 332 } |
| 333 |
| 334 /** |
| 335 * Move the camera. |
| 336 */ |
| 337 function moveCamera() { |
| 338 var newTarget = [g_playerXPosition, 10, g_playerZPosition]; |
| 339 g_target = g_math.lerpVector(g_target, newTarget, 0.03); |
| 340 updateCamera(); |
| 341 } |
| 342 |
| 343 /** |
| 344 * Deal with player animation. |
| 345 */ |
| 346 function handleAnimation(elapsedTime) { |
| 347 g_animTimer += elapsedTime; |
| 348 if (g_animTimer >= IDLE_END_TIME) { |
| 349 g_animTimer = g_math.modClamp(g_animTimer, |
| 350 IDLE_TIME_RANGE, |
| 351 IDLE_START_TIME); |
| 352 } |
| 353 g_animParam.value = g_animTimer; |
| 354 } |
| 355 |
| 356 /** |
| 357 * Called every frame. |
| 358 * @param {!o3d.RenderEvent} renderEvent Rendering Information. |
| 359 */ |
| 360 function onRender(renderEvent) { |
| 361 var elapsedTime = renderEvent.elapsedTime; |
| 362 |
| 363 updateClientSize(); |
| 364 handleMoveKeys(elapsedTime); |
| 365 handleAnimation(elapsedTime); |
| 366 moveCamera(); |
| 367 }; |
| 368 |
| 369 /** |
| 370 * Remove any callbacks so they don't get called after the page has unloaded. |
| 371 */ |
| 372 function unload() { |
| 373 if (g_client) { |
| 374 g_client.cleanup(); |
| 375 } |
| 376 } |
| 377 </script> |
| 378 </head> |
| 379 <body> |
| 380 <table style="width: 100%; height:100%;"> |
| 381 <tr style="height: 50px;"><td> |
| 382 <div style="width: 100%; height: 50px; font-size: large;"> |
| 383 <img src="assets/colorbar.png" width="100%" height="10px"/><br/> |
| 384 Google I/O 2009 O3D Sample |
| 385 </div> |
| 386 </td></tr> |
| 387 <tr style="height: 100%;"><td> |
| 388 <div style="width: 100%; height: 100%;"> |
| 389 <div id="o3d" style="width: 100%; height: 100%;"></div> |
| 390 </div> |
| 391 </td></tr> |
| 392 </table> |
| 393 </body> |
| 394 </html> |
| OLD | NEW |