My favorites | Sign in
Logo
                
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package org.papervision3d.core.controller
{
import org.papervision3d.core.geom.TriangleMesh3D;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.core.log.PaperLogger;

/**
* The MorphController class controls a mesh's vertices by applying a morph.
*
* Each possible mesh that can be blended (a morph target) must be specified.
* Each morph target is assigned a blend weight. The result is obtained via two methods :
*
* <ol>
* <li>NORMALIZED (Target1, Target2, ...)*(w1, w2, ...) = (1-w1-w2-...)*BaseMesh + w1*Target1 + w2*Target2 + ...</li>
* <li>RELATIVE (Target1, Target2, ...) + (w1, w2, ...) = BaseMesh + w1*Target1 + w2*Target2 + ...</li>
* </ol>
*
* @author Tim Knip / floorplanner.com
*/
public class MorphController implements IObjectController
{
/** */
public var active :Boolean;

/** */
public var target :TriangleMesh3D;

/** */
public var targets :Array;

/** */
public var weights :Array;

/** */
public var normalized :Boolean;

/** */
private var cached :Array;

/**
* Constructor.
*/
public function MorphController(target:TriangleMesh3D, normalized:Boolean=true)
{
this.target = target;
this.active = true;
this.targets = new Array();
this.normalized = normalized;
this.weights = new Array();
this.cached = new Array(target.geometry.vertices.length);

var v :Vertex3D;
for(var i:int = 0; i < cached.length; i++)
{
v = target.geometry.vertices[i];
cached[i] = v.clone();
}
}

/**
*
*/
public function addMorphTarget(mesh:TriangleMesh3D, weight:Number):void
{
if(mesh.geometry.vertices.length != this.target.geometry.vertices.length)
{
PaperLogger.warning("Invalid morph target! " +
"Number of specified vertices (" + mesh.geometry.vertices.length + ")" +
" not equal to number of base vertices (" +
+this.target.geometry.vertices.length + ").");
return;
}

this.targets.push(mesh);
this.weights.push(weight);
}

/**
*
*/
public function update():void
{
var orig :Array = this.target.geometry.vertices;
var cached :Array = this.cached;
var mesh :TriangleMesh3D;
var c :Vertex3D, v :Vertex3D, t :Vertex3D;
var num :int = orig.length;
var totalWeight :Number = 0;
var restWeight :Number, weight :Number;
var i :int, j :int;

if(!this.active)
{
return;
}

if(normalized)
{
for(i = 0; i < weights.length; i++)
{
totalWeight += weights[i];
}
restWeight = 1 - totalWeight;
}

for(i = 0; i < num; i++)
{
v = orig[i];
c = cached[i];

v.x = normalized ? restWeight * c.x : c.x;
v.y = normalized ? restWeight * c.y : c.y;
v.z = normalized ? restWeight * c.z : c.z;

for(j = 0; j < targets.length; j++)
{
mesh = targets[j];
t = mesh.geometry.vertices[i];
weight = weights[j];

v.x += weight * t.x;
v.y += weight * t.y;
v.z += weight * t.z;
}
}
}
}
}
Show details Hide details

Change log

r911 by tim.knip on May 26, 2009   Diff
Papervision3D 2.1 - initial commit.
- DAE : morphing, vertex-animation, lots
of fixes
- Animation : total revamp of the
animation system
- MD2 : adapted to match new animation
system
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 3053 bytes, 128 lines
Hosted by Google Code