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