| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 Copyright 2009, Google Inc. | 2 Copyright 2009, Google Inc. |
| 3 All rights reserved. | 3 All rights reserved. |
| 4 | 4 |
| 5 Redistribution and use in source and binary forms, with or without | 5 Redistribution and use in source and binary forms, with or without |
| 6 modification, are permitted provided that the following conditions are | 6 modification, are permitted provided that the following conditions are |
| 7 met: | 7 met: |
| 8 | 8 |
| 9 * Redistributions of source code must retain the above copyright | 9 * Redistributions of source code must retain the above copyright |
| 10 notice, this list of conditions and the following disclaimer. | 10 notice, this list of conditions and the following disclaimer. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 // global variables | 57 // global variables |
| 58 var g_o3d; | 58 var g_o3d; |
| 59 var g_math; | 59 var g_math; |
| 60 var g_client; | 60 var g_client; |
| 61 var g_viewInfo; | 61 var g_viewInfo; |
| 62 var g_pack; | 62 var g_pack; |
| 63 var g_particleSystem; | 63 var g_particleSystem; |
| 64 var g_clockParam; | 64 var g_clockParam; |
| 65 var g_textures = []; | 65 var g_textures = []; |
| 66 var g_emitters = []; // so we can find in the debugger to edit in real time. | 66 var g_emitters = []; // so we can find in the debugger to edit in real time. |
| 67 var g_poofs = []; |
| 68 var g_keyDown = []; |
| 69 var g_poofIndex = 0; |
| 70 var g_trail; |
| 71 var g_trailParameters; |
| 72 |
| 73 var MAX_POOFS = 3; |
| 67 | 74 |
| 68 /** | 75 /** |
| 69 * Loads a texture. | 76 * Loads a texture. |
| 70 * @param {!o3djs.loader.Loader} loader Loader to use to load texture. | 77 * @param {!o3djs.loader.Loader} loader Loader to use to load texture. |
| 71 * @param {string} url relativel url of texture. | 78 * @param {string} url relativel url of texture. |
| 72 * @param {number} index Index at which to record texture. | 79 * @param {number} index Index at which to record texture. |
| 73 */ | 80 */ |
| 74 function loadTexture(loader, url, index) { | 81 function loadTexture(loader, url, index) { |
| 75 loader.loadTexture( | 82 loader.loadTexture( |
| 76 g_pack, | 83 g_pack, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 g_math.degToRad(30), // 30 degree fov. | 141 g_math.degToRad(30), // 30 degree fov. |
| 135 g_client.width / g_client.height, | 142 g_client.width / g_client.height, |
| 136 0.1, // Near plane. | 143 0.1, // Near plane. |
| 137 5000); // Far plane. | 144 5000); // Far plane. |
| 138 | 145 |
| 139 g_viewInfo.drawContext.view = g_math.matrix4.lookAt( | 146 g_viewInfo.drawContext.view = g_math.matrix4.lookAt( |
| 140 [500, 1000, 1000], // eye | 147 [500, 1000, 1000], // eye |
| 141 [0, 200, 0], // target | 148 [0, 200, 0], // target |
| 142 [0, 1, 0]); // up | 149 [0, 1, 0]); // up |
| 143 | 150 |
| 144 | |
| 145 | |
| 146 // Load textures. This happens asynchronously. | 151 // Load textures. This happens asynchronously. |
| 147 var loader = o3djs.loader.createLoader(initStep3); | 152 var loader = o3djs.loader.createLoader(initStep3); |
| 148 loadTexture(loader, 'assets/particle-anim.png', 0); | 153 loadTexture(loader, 'assets/particle-anim.png', 0); |
| 149 loadTexture(loader, 'assets/ripple.png', 1); | 154 loadTexture(loader, 'assets/ripple.png', 1); |
| 150 loader.finish(); | 155 loader.finish(); |
| 151 } | 156 } |
| 152 | 157 |
| 153 function initStep3() { | 158 function initStep3() { |
| 154 // Normally we wouldn't pass in a clock and the particle system would handle | 159 // Normally we wouldn't pass in a clock and the particle system would handle |
| 155 // this for me but for selenium testing we need to be able to control the | 160 // this for me but for selenium testing we need to be able to control the |
| (...skipping 11 matching lines...) Expand all Loading... |
| 167 setupFlame(); | 172 setupFlame(); |
| 168 setupNaturalGasFlame(); | 173 setupNaturalGasFlame(); |
| 169 setupSmoke(); | 174 setupSmoke(); |
| 170 setupWhiteEnergy(); | 175 setupWhiteEnergy(); |
| 171 setupGoogle(); | 176 setupGoogle(); |
| 172 setupRain(); | 177 setupRain(); |
| 173 setupRipples(); | 178 setupRipples(); |
| 174 setupAnim(); | 179 setupAnim(); |
| 175 setupBall(); | 180 setupBall(); |
| 176 setupCube(); | 181 setupCube(); |
| 182 setupPoof(); |
| 183 setupTrail(); |
| 184 |
| 185 window.addEventListener('keypress', onKeyPress, true); |
| 186 window.addEventListener('keydown', onKeyDown, true); |
| 187 window.addEventListener('keyup', onKeyUp, true); |
| 177 | 188 |
| 178 // Setup an onrender callback for animation. | 189 // Setup an onrender callback for animation. |
| 179 g_client.setRenderCallback(onrender); | 190 g_client.setRenderCallback(onrender); |
| 180 | 191 |
| 181 window.g_finished = true; // for selenium testing. | 192 window.g_finished = true; // for selenium testing. |
| 182 } | 193 } |
| 183 | 194 |
| 195 function onKeyPress(event) { |
| 196 event = event || window.event; |
| 197 |
| 198 var keyChar = String.fromCharCode(o3djs.event.getEventKeyChar(event)); |
| 199 // Just in case they have capslock on. |
| 200 keyChar = keyChar.toLowerCase(); |
| 201 |
| 202 switch (keyChar) { |
| 203 case 'p': |
| 204 triggerPoof(); |
| 205 break; |
| 206 } |
| 207 } |
| 208 |
| 209 function onKeyDown(event) { |
| 210 event = event || window.event; |
| 211 g_keyDown[event.keyCode] = true; |
| 212 } |
| 213 |
| 214 function onKeyUp(event) { |
| 215 event = event || window.event; |
| 216 g_keyDown[event.keyCode] = false; |
| 217 } |
| 218 |
| 184 function setupFlame() { | 219 function setupFlame() { |
| 185 var transform = g_pack.createObject('Transform'); | 220 var transform = g_pack.createObject('Transform'); |
| 186 transform.parent = g_client.root; | 221 transform.parent = g_client.root; |
| 187 transform.translate(-300, 0, 0); | 222 transform.translate(-300, 0, 0); |
| 188 | 223 |
| 189 var emitter = g_particleSystem.createParticleEmitter(); | 224 var emitter = g_particleSystem.createParticleEmitter(); |
| 190 g_emitters.push(emitter); | 225 g_emitters.push(emitter); |
| 191 emitter.setState(o3djs.particles.ParticleStateIds.ADD); | 226 emitter.setState(o3djs.particles.ParticleStateIds.ADD); |
| 192 emitter.setColorRamp( | 227 emitter.setColorRamp( |
| 193 [1, 1, 0, 1, | 228 [1, 1, 0, 1, |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 Math.floor(pseudoRandom() * 3) * Math.PI * 0.5); | 488 Math.floor(pseudoRandom() * 3) * Math.PI * 0.5); |
| 454 parameters.orientation = o3djs.quaternions.rotationToQuaternion(matrix); | 489 parameters.orientation = o3djs.quaternions.rotationToQuaternion(matrix); |
| 455 var position = g_math.matrix4.transformDirection( | 490 var position = g_math.matrix4.transformDirection( |
| 456 matrix, | 491 matrix, |
| 457 [pseudoRandom() * 200 - 100, 100, pseudoRandom() * 200 - 100]); | 492 [pseudoRandom() * 200 - 100, 100, pseudoRandom() * 200 - 100]); |
| 458 parameters.position = position; | 493 parameters.position = position; |
| 459 }); | 494 }); |
| 460 transform.addShape(emitter.shape); | 495 transform.addShape(emitter.shape); |
| 461 } | 496 } |
| 462 | 497 |
| 498 function setupPoof() { |
| 499 var emitter = g_particleSystem.createParticleEmitter(); |
| 500 emitter.setState(o3djs.particles.ParticleStateIds.ADD); |
| 501 emitter.setColorRamp( |
| 502 [1, 1, 1, 0.3, |
| 503 1, 1, 1, 0]); |
| 504 emitter.setParameters({ |
| 505 numParticles: 30, |
| 506 lifeTime: 1.5, |
| 507 startTime: 0, |
| 508 startSize: 50, |
| 509 endSize: 200, |
| 510 spinSpeedRange: 10}, |
| 511 function(index, parameters) { |
| 512 var angle = Math.random() * 2 * Math.PI; |
| 513 parameters.velocity = g_math.matrix4.transformPoint( |
| 514 g_math.matrix4.rotationY(angle), [300, 0, 0]); |
| 515 parameters.acceleration = g_math.mulVectorVector( |
| 516 parameters.velocity, [-0.3, 0, -0.3]); |
| 517 }); |
| 518 // make 3 poofs one shots |
| 519 for (var ii = 0; ii < MAX_POOFS; ++ii) { |
| 520 g_poofs[ii] = emitter.createOneShot(g_client.root); |
| 521 } |
| 522 } |
| 523 |
| 524 function triggerPoof() { |
| 525 // We have multiple poofs because if you only have one and it is still going |
| 526 // when you trigger it a second time it will immediately start over. |
| 527 g_poofs[g_poofIndex].trigger([100 + 100 * g_poofIndex, 0, 300]); |
| 528 g_poofIndex++; |
| 529 if (g_poofIndex == MAX_POOFS) { |
| 530 g_poofIndex = 0; |
| 531 } |
| 532 } |
| 533 |
| 534 function setupTrail() { |
| 535 g_trailParameters = { |
| 536 numParticles: 2, |
| 537 lifeTime: 2, |
| 538 startSize: 10, |
| 539 endSize: 90, |
| 540 velocityRange: [20, 20, 20], |
| 541 spinSpeedRange: 4}; |
| 542 g_trail = g_particleSystem.createTrail( |
| 543 g_client.root, |
| 544 1000, |
| 545 g_trailParameters); |
| 546 g_trail.setState(o3djs.particles.ParticleStateIds.ADD); |
| 547 g_trail.setColorRamp( |
| 548 [1, 0, 0, 1, |
| 549 1, 1, 0, 1, |
| 550 1, 1, 1, 0]); |
| 551 } |
| 552 |
| 553 function leaveTrail() { |
| 554 var trailClock = window.g_clock * -0.8; |
| 555 g_trail.birthParticles( |
| 556 [Math.sin(trailClock) * 400, 200, Math.cos(trailClock) * 400]); |
| 557 } |
| 558 |
| 463 /** | 559 /** |
| 464 * Called every frame. | 560 * Called every frame. |
| 465 * @param {!o3d.RenderEvent} renderEvent Rendering Information. | 561 * @param {!o3d.RenderEvent} renderEvent Rendering Information. |
| 466 */ | 562 */ |
| 467 function onrender(renderEvent) { | 563 function onrender(renderEvent) { |
| 468 var elapsedTime = renderEvent.elapsedTime; | 564 var elapsedTime = renderEvent.elapsedTime; |
| 469 window.g_clock += elapsedTime * window.g_timeMult; | 565 window.g_clock += elapsedTime * window.g_timeMult; |
| 470 | 566 |
| 567 if (g_keyDown[84]) { // 'T' key. |
| 568 leaveTrail(); |
| 569 } |
| 570 |
| 471 var cameraClock = window.g_clock * 0.3; | 571 var cameraClock = window.g_clock * 0.3; |
| 472 g_viewInfo.drawContext.view = g_math.matrix4.lookAt( | 572 g_viewInfo.drawContext.view = g_math.matrix4.lookAt( |
| 473 [Math.sin(cameraClock) * 1500, 500, Math.cos(cameraClock) * 1500], // eye | 573 [Math.sin(cameraClock) * 1500, 500, Math.cos(cameraClock) * 1500], // eye |
| 474 [0, 100, 0], // target | 574 [0, 100, 0], // target |
| 475 [0, 1, 0]); // up | 575 [0, 1, 0]); // up |
| 476 | 576 |
| 477 g_clockParam.value = window.g_clock; | 577 g_clockParam.value = window.g_clock; |
| 478 } | 578 } |
| 479 | 579 |
| 480 /** | 580 /** |
| 481 * Remove any callbacks so they don't get called after the page has unloaded. | 581 * Remove any callbacks so they don't get called after the page has unloaded. |
| 482 */ | 582 */ |
| 483 function unload() { | 583 function unload() { |
| 484 if (g_client) { | 584 if (g_client) { |
| 485 g_client.cleanup(); | 585 g_client.cleanup(); |
| 486 } | 586 } |
| 487 } | 587 } |
| 488 </script> | 588 </script> |
| 489 </head> | 589 </head> |
| 490 <body onload="init();" onunload="unload();"> | 590 <body onload="init();" onunload="unload();"> |
| 491 <h1>Particles</h1> | 591 <h1>Particles</h1> |
| 492 <br/> | 592 <br/> |
| 493 <!-- Start of O3D plugin --> | 593 <!-- Start of O3D plugin --> |
| 494 <div id="o3d" style="width: 800px; height: 600px;"></div> | 594 <div id="o3d" style="width: 800px; height: 600px;"></div> |
| 495 <!-- End of O3D plugin --> | 595 <!-- End of O3D plugin --> |
| 596 Press 'P' to make a poof.<br/> |
| 597 Press 'T' to make a trail. |
| 496 </body> | 598 </body> |
| 497 </html> | 599 </html> |
| OLD | NEW |