topical media & game development

talk show tell print

graphic-o3d-samples-particles.htm / htm



  <!--
  Copyright 2009, Google Inc.
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
  
      * Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following disclaimer
  in the documentation and/or other materials provided with the
  distribution.
      * Neither the name of Google Inc. nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  -->
  
  <!--
  Particles.
  
  This example shows using the javascript particle library.
  -->
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
  <html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>
  Particles.
  </title>
  <!-- Include sample javascript library functions-->
  <script type="text/javascript" src="o3djs/base.js"></script>
  
  <!-- Our javascript code -->
  <script type="text/javascript">
  o3djs.require('o3djs.util');
  o3djs.require('o3djs.math');
  o3djs.require('o3djs.rendergraph');
  o3djs.require('o3djs.particles');
  
  // Events
  // init() once the page has finished loading.
  // unload() when the page is unloaded.
  window.onload = init;
  window.onunload= unload;
  
  // global variables
  var g_o3d;
  var g_math;
  var g_client;
  var g_viewInfo;
  var g_pack;
  var g_clock = 0;
  var g_timeMult = 1;
  var g_finished = false;  // for selenium testing
  var g_particleSystem;
  var g_clockParam;
  
  
Creates the client area.

  
  function init() {
    o3djs.util.makeClients(initStep2);
  }
  
  
Initializes O3D and creates one shape.
parameter: {Array} clientElements Array of o3d object elements.

  
  function initStep2(clientElements) {
    // Initializes global variables and libraries.
    var o3dElement = clientElements[0];
    g_o3d = o3dElement.o3d;
    g_math = o3djs.math;
    g_client = o3dElement.client;
  
    // Creates a pack to manage our resources/assets
    g_pack = g_client.createPack();
  
    g_viewInfo = o3djs.rendergraph.createBasicView(
        g_pack,
        g_client.root,
        g_client.renderGraphRoot);
  
    g_viewInfo.drawContext.projection = g_math.matrix4.perspective(
        g_math.degToRad(30), // 30 degree fov.
        g_client.width / g_client.height,
        0.1,                // Near plane.
        5000);              // Far plane.
  
    g_viewInfo.drawContext.view = g_math.matrix4.lookAt(
        [500, 1000, 1000], // eye
        [0, 200, 0],   // target
        [0, 1, 0]);  // up
  
    // Load our texture. This happens asynchronously.
    var url = o3djs.util.getAbsoluteURI('assets/particle-anim.png');
    o3djs.io.loadTexture(g_pack, url, initStep3);
  }
  
  function initStep3(texture, exception) {
    if (exception) {
      alert(exception);
      return;
    }
    var paramObject = g_pack.createObject('ParamObject');
    g_clockParam = paramObject.createParam('clock', 'ParamFloat');
    g_particleSystem = o3djs.particles.createParticleSystem(g_pack,
                                                            g_viewInfo,
                                                            g_clockParam);
    setupFlame();
    setupNaturalGasFlame();
    setupSmoke();
    setupWhiteEnergy();
    setupGoogle();
    setupRain();
    setupAnim(texture);
  
    // Setup an onrender callback for animation.
    g_client.setRenderCallback(onrender);
  
    g_finished = true;  // for selenium testing.
  }
  
  function setupFlame() {
    var transform = g_pack.createObject('Transform');
    transform.parent = g_client.root;
    transform.translate(-300, 0, 0);
  
    var emitter = g_particleSystem.createParticleEmitter();
    emitter.setState(o3djs.particles.ParticleStateIds.ADD);
    emitter.setColorRamp(
        [1, 1, 0, 1,
         1, 0, 0, 1,
         0, 0, 0, 1,
         0, 0, 0, 0.5,
         0, 0, 0, 0]);
    emitter.setParameters({
      numParticles: 20,
      lifeTime: 2,
      timeRange: 2,
      startSize: 50,
      endSize: 90,
      velocity:[0, 60, 0], velocityRange: [15, 15, 15],
      worldAcceleration: [0, -20, 0],
      spinSpeedRange: 4});
    transform.addShape(emitter.shape);
  }
  
  function setupNaturalGasFlame() {
    var transform = g_pack.createObject('Transform');
    transform.parent = g_client.root;
    transform.translate(-200, 0, 0);
  
    var emitter = g_particleSystem.createParticleEmitter();
    emitter.setState(o3djs.particles.ParticleStateIds.ADD);
    emitter.setColorRamp(
        [0.2, 0.2, 1, 1,
         0, 0, 1, 1,
         0, 0, 1, 0.5,
         0, 0, 1, 0]);
    emitter.setParameters({
      numParticles: 20,
      lifeTime: 2,
      timeRange: 2,
      startSize: 50,
      endSize: 20,
      velocity:[0, 60, 0],
      worldAcceleration: [0, -20, 0],
      spinSpeedRange: 4});
    transform.addShape(emitter.shape);
  }
  
  function setupSmoke() {
    var transform = g_pack.createObject('Transform');
    transform.parent = g_client.root;
    transform.translate(-100, 0, 0);
  
    var emitter = g_particleSystem.createParticleEmitter();
    emitter.setState(o3djs.particles.ParticleStateIds.BLEND);
    emitter.setColorRamp(
        [0, 0, 0, 1,
         0, 0, 0, 0]);
    emitter.setParameters({
      numParticles: 20,
      lifeTime: 2,
      timeRange: 2,
      startSize: 100,
      endSize: 150,
      velocity: [0, 200, 0], velocityRange: [20, 0, 20],
      worldAcceleration: [0, -25, 0],
      spinSpeedRange: 4});
    transform.addShape(emitter.shape);
  }
  
  function setupWhiteEnergy() {
    var transform = g_pack.createObject('Transform');
    transform.parent = g_client.root;
    transform.translate(0, 0, 0);
  
    var emitter = g_particleSystem.createParticleEmitter();
    emitter.setState(o3djs.particles.ParticleStateIds.ADD);
    emitter.setColorRamp(
        [1, 1, 1, 1,
         1, 1, 1, 0]);
    emitter.setParameters({
      numParticles: 80,
      lifeTime: 2,
      timeRange: 2,
      startSize: 100,
      endSize: 100,
      positionRange: [100, 0, 100],
      velocityRange: [20, 0, 20]});
    transform.addShape(emitter.shape);
  }
  
  function setupGoogle() {
    var image = [
      '.XXXX...XXXXX...XXXXX.',
      'X....X.......X..X....X',
      'X....X...XXXXX..X....X',
      'X....X.......X..X....X',
      '.XXXX...XXXXX...XXXXX.'];
    var height = image.length;
    var width = image[0].length;
  
    // Make an array of positions based on the text image.
    var positions = [];
      for (var yy = 0; yy < height; ++yy) {
          for (var xx = 0; xx < width; ++xx) {
        if (image[yy].substring(xx, xx + 1) == 'X') {
          positions.push([(xx - width * 0.5) * 10,
                         -(yy - height * 0.5) * 10]);
        }
      }
    }
    var transform = g_pack.createObject('Transform');
    transform.parent = g_client.root;
    transform.translate(100, 400, 0);
  
    var emitter = g_particleSystem.createParticleEmitter();
    emitter.setState(o3djs.particles.ParticleStateIds.ADD);
    emitter.setColorRamp(
        [1, 0, 0, 1,
         0, 1, 0, 1,
         0, 0, 1, 1,
         1, 1, 0, 0]);
    emitter.setParameters({
      numParticles: positions.length * 4,
      lifeTime: 2,
      timeRange: 2,
      startSize: 25,
      endSize: 50,
      positionRange: [2, 0, 2],
      velocity: [1, 0, 1]},
      function(particleIndex, parameters) {
        //var index = particleIndex;
        var index = Math.floor(Math.random() * positions.length);
        index = Math.min(index, positions.length - 1);
        parameters.position[0] = positions[index][0];
        parameters.position[1] = positions[index][1];
      });
    transform.addShape(emitter.shape);
  }
  
  function setupRain() {
    var transform = g_pack.createObject('Transform');
    transform.parent = g_client.root;
    transform.translate(200, 200, 0);
  
    var emitter = g_particleSystem.createParticleEmitter();
    emitter.setState(o3djs.particles.ParticleStateIds.BLEND);
    emitter.setColorRamp(
        [0.2, 0.2, 1, 1]);
    emitter.setParameters({
      numParticles: 80,
      lifeTime: 2,
      timeRange: 2,
      startSize: 5,
      endSize: 5,
      positionRange: [100, 0, 100],
      velocity: [0,-150,0]});
    transform.addShape(emitter.shape);
  }
  
  function setupAnim(texture) {
    var transform = g_pack.createObject('Transform');
    transform.parent = g_client.root;
    transform.translate(300, 0, 0);
  
    var emitter = g_particleSystem.createParticleEmitter(texture);
    emitter.setColorRamp(
        [1, 1, 1, 1,
         1, 1, 1, 1,
         1, 1, 1, 0]);
    emitter.setParameters({
      numParticles: 20,
      numFrames: 8,
      frameDuration: 0.25,
      frameStartRange: 8,
      lifeTime: 2,
      timeRange: 2,
      startSize: 50,
      endSize: 90,
      positionRange: [10, 10, 10],
      velocity:[0, 200, 0], velocityRange: [75, 15, 75],
      acceleration: [0, -150, 0],
      spinSpeedRange: 1});
    transform.addShape(emitter.shape);
  }
  
  
Called every frame.
parameter: {!o3d.RenderEvent} renderEvent Rendering Information.

  
  function onrender(renderEvent) {
    var elapsedTime = renderEvent.elapsedTime;
    g_clock += elapsedTime * g_timeMult;
  
      g_clockParam.value = g_clock;
  }
  
  
Remove any callbacks so they don't get called after the page has unloaded.

  
  function unload() {
    if (g_client) {
      g_client.cleanup();
    }
  }
  </script>
  </head>
  <body>
  <h1>Particles</h1>
  <br/>
  <!-- Start of O3D plugin -->
  <div id="o3d" style="width: 800px; height: 600px;"></div>
  <!-- End of O3D plugin -->
  </body>
  </html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.