package away3dlite.core.base; import away3dlite.core.base.Mesh; import away3dlite.core.base.Object3D; import flash.Vector; /** * Keyframe animation morpher */ class Morpher extends Object3D { private var weight:Float; private var startingMesh:Mesh; private var _vertices:Vector; /** * Creates a new Morpher object. * * @param mesh A mesh object used to define the starting vertices. */ public function new(mesh:Mesh) { super(); startingMesh = mesh; _vertices = startingMesh.vertices; } /** * resets all vertex objects to 0,0,0 */ public function start():Void { weight = 0; for(i in 0..._vertices.length) { _vertices[i] = 0; } } /** * interpolates the vertex objects position values between the current vertex positions and the external vertex positions * * @param comp The external mesh used for interpolating values * @param k The increment used on the weighting value */ public function mix(comp:Mesh, k:Float):Void { weight += k; _vertices = startingMesh.vertices; var _verticesComp = comp.vertices; var i = 0; while(i < _vertices.length) { _vertices[i] += _verticesComp[i] * k; _vertices[i+1] += _verticesComp[i+1] * k; _vertices[i+2] += _verticesComp[i+2] * k; i += 3; } } /** * resets all vertex objects to the external mesh positions * * @param comp The external mesh used for vertex values */ public function finish(comp:Mesh):Void { mix(comp, 1 - weight); weight = 1; } }