forked from jeromeetienne/threex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
THREEx.GeometryWobble.js
54 lines (41 loc) · 1.73 KB
/
THREEx.GeometryWobble.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
}