VelocityNode.js 3.2 KB
Newer Older
S
SUNAG 已提交
1 2 3 4
/**
 * @author sunag / http://www.sunag.com.br/
 */

S
sunag 已提交
5
import { Vector3Node } from '../inputs/Vector3Node.js';
S
sunag 已提交
6

S
sunag 已提交
7
function VelocityNode( target, params ) {
S
SUNAG 已提交
8

S
sunag 已提交
9
	Vector3Node.call( this );
S
SUNAG 已提交
10

S
sunag 已提交
11
	this.params = {};
S
SUNAG 已提交
12

S
SUNAG 已提交
13
	this.velocity = new THREE.Vector3();
S
SUNAG 已提交
14

S
sunag 已提交
15 16 17
	this.setTarget( target );
	this.setParams( params );

S
sunag 已提交
18
}
S
sunag 已提交
19

S
sunag 已提交
20 21 22
VelocityNode.prototype = Object.create( Vector3Node.prototype );
VelocityNode.prototype.constructor = VelocityNode;
VelocityNode.prototype.nodeType = "Velocity";
S
sunag 已提交
23

S
sunag 已提交
24
VelocityNode.prototype.isReadonly = function ( builder ) {
S
sunag 已提交
25 26 27 28 29

	return false;

};

S
sunag 已提交
30
VelocityNode.prototype.setParams = function ( params ) {
S
sunag 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

	switch ( this.params.type ) {

		case "elastic":

			delete this.moment;

			delete this.speed;
			delete this.springVelocity;

			delete this.lastVelocity;

			break;

	}

	this.params = params || {};

S
sunag 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
	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;

	}
S
SUNAG 已提交
63

S
SUNAG 已提交
64 65
};

S
sunag 已提交
66
VelocityNode.prototype.setTarget = function ( target ) {
S
sunag 已提交
67 68 69 70 71 72 73 74 75 76 77 78

	if ( this.target ) {

		delete this.position;
		delete this.oldPosition;

	}

	this.target = target;

	if ( target ) {

S
sunag 已提交
79
		this.position = target.getWorldPosition( this.position || new THREE.Vector3() );
S
sunag 已提交
80 81 82 83 84 85
		this.oldPosition = this.position.clone();

	}

};

S
sunag 已提交
86
VelocityNode.prototype.updateFrameVelocity = function ( frame ) {
S
sunag 已提交
87 88 89

	if ( this.target ) {

S
sunag 已提交
90
		this.position = this.target.getWorldPosition( this.position || new THREE.Vector3() );
S
sunag 已提交
91 92 93 94 95 96
		this.velocity.subVectors( this.position, this.oldPosition );
		this.oldPosition.copy( this.position );

	}

};
S
SUNAG 已提交
97

S
sunag 已提交
98
VelocityNode.prototype.updateFrame = function ( frame ) {
S
SUNAG 已提交
99

S
sunag 已提交
100
	this.updateFrameVelocity( frame );
S
SUNAG 已提交
101 102 103

	switch ( this.params.type ) {

S
SUNAG 已提交
104
		case "elastic":
S
SUNAG 已提交
105

S
sunag 已提交
106
			// convert to real scale: 0 at 1 values
S
sunag 已提交
107
			var deltaFps = frame.delta * ( this.params.fps || 60 );
S
sunag 已提交
108 109 110

			var spring = Math.pow( this.params.spring, deltaFps ),
				damping = Math.pow( this.params.damping, deltaFps );
S
SUNAG 已提交
111

S
sunag 已提交
112
			// fix relative frame-rate
S
sunag 已提交
113
			this.velocity.multiplyScalar( Math.exp( - this.params.damping * deltaFps ) );
S
SUNAG 已提交
114

S
sunag 已提交
115 116 117
			// elastic
			this.velocity.add( this.springVelocity );
			this.velocity.add( this.speed.multiplyScalar( damping ).multiplyScalar( 1 - spring ) );
S
SUNAG 已提交
118

S
sunag 已提交
119 120
			// speed
			this.speed.subVectors( this.velocity, this.lastVelocity );
S
SUNAG 已提交
121

S
sunag 已提交
122 123 124 125 126 127 128 129 130 131 132
			// 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 );
S
SUNAG 已提交
133
			this.value.copy( this.moment );
S
SUNAG 已提交
134

S
SUNAG 已提交
135
			break;
S
SUNAG 已提交
136

S
SUNAG 已提交
137
		default:
S
SUNAG 已提交
138

S
SUNAG 已提交
139
			this.value.copy( this.velocity );
S
SUNAG 已提交
140

S
SUNAG 已提交
141
	}
S
SUNAG 已提交
142 143

};
S
sunag 已提交
144

S
sunag 已提交
145
VelocityNode.prototype.copy = function ( source ) {
S
sunag 已提交
146

S
sunag 已提交
147
	Vector3Node.prototype.copy.call( this, source );
S
sunag 已提交
148

S
sunag 已提交
149
	if ( source.target ) object.setTarget( source.target );
S
sunag 已提交
150

S
sunag 已提交
151
	object.setParams( source.params );
S
sunag 已提交
152

S
sunag 已提交
153 154
};

S
sunag 已提交
155
VelocityNode.prototype.toJSON = function ( meta ) {
S
sunag 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

	var data = this.getJSONNode( meta );

	if ( ! data ) {

		data = this.createJSONNode( meta );

		if ( this.target ) data.target = this.target.uuid;

		// clone params
		data.params = JSON.parse( JSON.stringify( this.params ) );

	}

	return data;

};
S
sunag 已提交
173

S
sunag 已提交
174
export { VelocityNode };