Pārlūkot izejas kodu

velocity node update

sunag 8 gadi atpakaļ
vecāks
revīzija
aabcaba894

+ 40 - 16
examples/js/nodes/utils/VelocityNode.js

@@ -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 );
@@ -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;
@@ -53,7 +78,6 @@ THREE.VelocityNode.prototype.updateFrame = function( delta ) {
 
 			this.value.copy( this.velocity );
 
-			break;
 	}
 
 };

+ 6 - 6
examples/webgl_materials_nodes.html

@@ -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(
@@ -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 ) {