topical media & game development

talk show tell print

mobile-query-three-vendor-threex-examples-threex.domevent-vendor-threex-THREEx.GeometryWobble.js / js



  var THREEx                = THREEx || {};
  
  THREEx.GeometryWobble        = {};
  
  // Geometry Wobble
  // based on paul lewis / areotwist - http://lab.aerotwist.com/webgl/undulating-monkey/
  
  
  THREEx.GeometryWobble.init	= function(geometry)
  {
          for(var i = 0; i < geometry.vertices.length; i++){
                  var vertex        = geometry.vertices[i];
                  vertex.originalPosition        = vertex.position.clone();
                  vertex.dirVector        = vertex.position.clone().normalize();
          }
          geometry.dynamic        = true;
          
          this.cpuAxis(geometry, 'y')
  }
  
  THREEx.GeometryWobble.cpuAxis        = function(geometry, type, factor)
  {
          if( type === undefined )        type        = 'x';
          if( factor === undefined )        factor        = 0.2;
          
          for(var i = 0; i < geometry.vertices.length; i++) {
                  var vertex        = geometry.vertices[i];
  // Note: may need more axis ?
                  if( type === 'x' )        vertex.axisValue        = vertex.originalPosition.x * factor;
                  else if( type === 'y' )        vertex.axisValue        = vertex.originalPosition.y * factor;
                  else if( type === 'z' )        vertex.axisValue        = vertex.originalPosition.z * factor;
                  else        console.assert(false);
          }
  }
  
  THREEx.GeometryWobble.Animate        = function(geometry, phase, magnitude)
  {
          if( phase === undefined )        phase                = 0;
          if( magnitude === undefined )        magnitude        = 0.2;
          
          if( typeof magnitude === "number" )        magnitude        = new THREE.Vector3(magnitude, magnitude, magnitude)
  
          for(var i = 0; i < geometry.vertices.length; i++) {
                  var vertex        = geometry.vertices[i];
                  var vertexPhase        = Math.cos(phase + vertex.axisValue);
                  
                  vertex.position.x = vertex.originalPosition.x + vertexPhase * vertex.dirVector.x * magnitude.x;
                  vertex.position.y = vertex.originalPosition.y + vertexPhase * vertex.dirVector.y * magnitude.y;
                  vertex.position.z = vertex.originalPosition.z + vertexPhase * vertex.dirVector.z * magnitude.z;
          }
          
          geometry.__dirtyVertices = true;
  }
  


(C) Æliens 04/09/2009

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.