Skip to content

Commit

Permalink
Merge pull request #11504 from sunag/86dev
Browse files Browse the repository at this point in the history
VelocityNode update (Flaccidity Map)
  • Loading branch information
mrdoob authored Jun 13, 2017
2 parents 516307a + aabcaba commit 558e964
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
56 changes: 40 additions & 16 deletions examples/js/nodes/utils/VelocityNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@
* @author sunag / http://www.sunag.com.br/
*/

THREE.VelocityNode = function( target, params ) {
THREE.VelocityNode = function ( target, params ) {

THREE.Vector3Node.call( this );

this.requestUpdate = true;

this.target = target;
this.params = params || {};

this.position = this.target.position.clone();
this.velocity = new THREE.Vector3();
this.moment = new THREE.Vector3();

this.params = params || {};
switch ( this.params.type ) {

case "elastic":

this.moment = new THREE.Vector3();

this.speed = new THREE.Vector3();
this.springVelocity = new THREE.Vector3();

this.lastVelocity = new THREE.Vector3();

break;

}

};

THREE.VelocityNode.prototype = Object.create( THREE.Vector3Node.prototype );
THREE.VelocityNode.prototype.constructor = THREE.VelocityNode;

THREE.VelocityNode.prototype.updateFrame = function( delta ) {
THREE.VelocityNode.prototype.updateFrame = function ( delta ) {

this.velocity.subVectors( this.target.position, this.position );
this.position.copy( this.target.position );
Expand All @@ -30,21 +43,33 @@ THREE.VelocityNode.prototype.updateFrame = function( delta ) {

case "elastic":

delta *= this.params.fps || 60;
// convert to real scale: 0 at 1 values
var deltaFps = delta * (this.params.fps || 60);

var spring = Math.pow( this.params.spring, deltaFps ),
damping = Math.pow( this.params.damping, deltaFps );

var spring = Math.pow( this.params.spring, delta );
var friction = Math.pow( this.params.friction, delta );
// fix relative frame-rate
this.velocity.multiplyScalar( Math.exp( -this.params.damping * deltaFps ) );

// spring
this.moment.x += this.velocity.x * spring;
this.moment.y += this.velocity.y * spring;
this.moment.z += this.velocity.z * spring;
// elastic
this.velocity.add( this.springVelocity );
this.velocity.add( this.speed.multiplyScalar( damping ).multiplyScalar( 1 - spring ) );

// friction
this.moment.x *= friction;
this.moment.y *= friction;
this.moment.z *= friction;
// speed
this.speed.subVectors( this.velocity, this.lastVelocity );

// spring velocity
this.springVelocity.add( this.speed );
this.springVelocity.multiplyScalar( spring );

// moment
this.moment.add( this.springVelocity );

// damping
this.moment.multiplyScalar( damping );

this.lastVelocity.copy( this.velocity );
this.value.copy( this.moment );

break;
Expand All @@ -53,7 +78,6 @@ THREE.VelocityNode.prototype.updateFrame = function( delta ) {

this.value.copy( this.velocity );

break;
}

};
12 changes: 6 additions & 6 deletions examples/webgl_materials_nodes.html
Original file line number Diff line number Diff line change
Expand Up @@ -1463,8 +1463,8 @@

var velocity = new THREE.VelocityNode( mesh, {
type: 'elastic',
spring: .8,
friction: .9
spring: .95,
damping: .95
} );

var velocityArea = new THREE.OperatorNode(
Expand Down Expand Up @@ -1501,13 +1501,13 @@

velocity.params.spring = val;

}, false, 0, .9 );
}, false, 0, .95 );

addGui( 'friction', velocity.params.friction, function( val ) {
addGui( 'damping', velocity.params.damping, function( val ) {

velocity.params.friction = val;
velocity.params.damping = val;

}, false, 0, .9 );
}, false, 0, .95 );

addGui( 'scale', scale.number, function( val ) {

Expand Down

0 comments on commit 558e964

Please sign in to comment.