And here's an example with some more information: http://blog.touchmypixel.com/2010/02/away3dlite-haxe-morphing/
On Feb 22, 6:25 pm, tarwin <[email protected]> wrote: > Ok. So it's all working - thanks for the help! And here's the source > code (this one's in haXe but it's super simple to change it to work in > AS3 - Away3D Lite): > > away3dlite.core.base.Morpher.hx > --------------------------------------------------------------------------- > -------------- > 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<Float>; > > /** > * Creates a new <code>Morpher</code> 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; > }} > > --------------------------------------------------------------------------- > -------------- > > And the class I was using as a test. The main things to know is that > you have to: > * Load your Collada (XML) > * Parse your collada which gives you an Object3D (or is it > ObjectContainer3D) > * Add the Object3D (Mesh) you want to affect to the scene (here it's > m1, which is just a geo-sphere) > * Wait for your Collada ParserEvent.PARSE_SUCCESS to be complete (on > your Collada objects, not your Object3Ds) > * Make a new Morpher, giving it an initial Mesh to work with, here > done by grabbing it as a child of the parsedGeometry (I think this is > because a Collada represents a Scene not just an object and thus > doesn't have vertices, you need to get the child vertices). This both > sets the initial vertices but also, because the vertices are all > accessed as references, sets which Mesh your going to be affecting > (the one you've added to the scene). > * When you want to do the Morph just run .start(), then .mix(target, > amount) - here I'm doing it to a Mesh that is the same as the geo- > sphere, just pulled out here and there randomly, and doing it twice to > get some crazy effects - then run .finish(originalMesh) - the > "originalMesh" here is actually a copy of the original mesh, not the > starting mesh. I think this is so you know where it's starting point > was - though I'd like this confirmed by some others ... I was a little > confused at this point. > > --------------------------------------------------------------------------- > -------------- > > /** > * ... > * @author Tarwin Stroh-Spijer > */ > > package cc.av3d.test; > > import away3dlite.containers.Scene3D; > import away3dlite.core.base.Morpher; > import away3dlite.containers.ObjectContainer3D; > import away3dlite.core.base.Object3D; > import away3dlite.events.ParserEvent; > import away3dlite.loaders.Collada; > import away3dlite.materials.ColorMaterial; > import away3dlite.primitives.Sphere; > import away3dlite.templates.BasicTemplate; > import away3dlite.templates.FastTemplate; > import flash.events.Event; > import flash.Lib; > import haxe.Resource; > > class Application2 extends FastTemplate > { > private var m0:ObjectContainer3D; > private var m1:ObjectContainer3D; > private var m2:ObjectContainer3D; > private var mp:Morpher; > private var mv0:Object3D; > private var mv1:Object3D; > private var mv2:Object3D; > > private var ps:Int; > > public static function main() > { > Lib.current.addChild(new Application2()); > } > > public function new() > { > super(); > ps = 0; > } > > override private function init():Void > { > RedirectTrace.toFD(); > > super.init(); > > var daeData1 = Resource.getString("dae1"); > var daeData2 = Resource.getString("dae2"); > > var c0 = new Collada(); > c0.scaling = 3; > var c1 = new Collada(); > c1.scaling = 3; > var c2 = new Collada(); > c2.scaling = 3; > > c0.addEventListener(ParserEvent.PARSE_SUCCESS, pe); > c1.addEventListener(ParserEvent.PARSE_SUCCESS, pe); > c2.addEventListener(ParserEvent.PARSE_SUCCESS, pe); > > m0 = cast c0.parseGeometry(daeData1); > m1 = cast c1.parseGeometry(daeData1); > m2 = cast c2.parseGeometry(daeData2); > > scene.addChild(m1); > } > > private function pe(e:ParserEvent):Void > { > ps++; > if (ps == 3) { > parseComplete(); > } > } > > private function parseComplete():Void > { > mp = new Morpher(cast m1.getChildAt(0)); > addEventListener(Event.ENTER_FRAME, ef); > } > > private function ef(event:Event):Void > { > super.onEnterFrame(event); > > m0.rotationY = -180 + 360 * (mouseX / stage.stageWidth); > m1.rotationY = -180 + 360 * (mouseX / stage.stageWidth); > m2.rotationY = -180 + 360 * (mouseX / stage.stageWidth); > > mp.start(); > mp.mix(cast m2.getChildAt(0), (1 + Math.sin(Lib.getTimer() / > 150)) / > 2); > mp.mix(cast m2.getChildAt(0), (1 + Math.cos(Lib.getTimer() / > 250)) / > 2); > mp.finish(cast m0.getChildAt(0)); > > view.render(); > } > > } > > --------------------------------------------------------------------------- > -------------- > > Collada #1 > --------------------------------------------------------------------------- > -------------- > <?xml version="1.0" encoding="utf-8"?> > <COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" > version="1.4.1"> > <asset> > <contributor> > <author>tarwin</author> > <authoring_tool>OpenCOLLADA for 3ds Max; Version: 1.2.5; > Revision: 673; Platform: x64; Configuration: Release Max2010</ > authoring_tool> > </contributor> > <created>2010-02-18T15:37:51</created> > <modified>2010-02-18T15:37:51</modified> > <unit name="inch" meter="0.0254"/> > <up_axis>Z_UP</up_axis> > </asset> > <library_effects> > <effect id="ColorEffectR225G198B87"> > <profile_COMMON> > <technique sid="common"> > <phong> > <ambient> > <color>0.8823529 0.7764706 0.3411765 1</color> > </ambient> > <diffuse> > <color>0.8823529 0.7764706 0.3411765 1</color> > </diffuse> > <specular> > <color>1 1 1 1</color> > </specular> > <shininess> > <float>10</float> > </shininess> > <reflective> > <color>0 0 0 1</color> > </reflective> > <transparent> > <color>1 1 1 1</color> > </transparent> > <transparency> > <float>1</float> > </transparency> > </phong> > </technique> > </profile_COMMON> > </effect> > </library_effects> > <library_materials> > <material id="ColorEffectR225G198B87-material" > name="ColorEffectR225G198B87-material"> > <instance_effect url="#ColorEffectR225G198B87"/> > </material> > </library_materials> > <library_geometries> > <geometry id="geom-GeoSphere01" name="GeoSphere01"> > <mesh> > <source id="geom-GeoSphere01-positions"> > <float_array id="geom-GeoSphere01-positions-array" > count="756">0 0 31.42415 28.10661 0 14.05331 8.68542 26.73098 14.05331 > -22.73873 16.52065 14.05331 -22.73873 -16.52065 14.05331 8.685424 > -26.73097 14.05331 22.73873 16.52065 -14.05331 -8.685422 26.73097 > -14.05331 -28.10661 -2.45716e-6 -14.05331 -8.685423 -26.73097 > -14.05331 22.73873 -16.52064 -14.05331 0 0 -31.42415 6.901517 0 > 30.65691 13.46603 0 28.39266 19.37298 0 24.74196 24.33392 0 19.88309 > 2.132686 6.563734 30.65691 4.161231 12.80695 28.39266 5.986578 18.4248 > 24.74196 7.519594 23.14293 19.88309 -5.583445 4.05661 30.65691 > -10.89425 7.915131 28.39266 -15.67307 11.38715 24.74196 -19.68656 > 14.30312 19.88309 -5.583445 -4.056611 30.65691 -10.89424 -7.915133 > 28.39266 -15.67307 -11.38715 24.74196 -19.68655 -14.30312 19.88309 > 2.132687 -6.563733 30.65691 4.161232 -12.80695 28.39266 5.986581 > -18.4248 24.74196 7.519597 -23.14293 19.88309 26.4666 6.563734 > 15.61772 23.53421 12.80695 16.4195 19.45261 18.4248 16.4195 14.42111 > 23.14293 15.61772 1.936148 27.19954 15.61772 -4.907668 26.33993 > 16.4195 -11.51184 24.1941 16.4195 -17.55387 20.86685 15.61772 -25.27 > 10.24651 15.61772 -26.56731 3.472014 16.4195 -26.56731 -3.472021 > 16.4195 -25.27 -10.24651 15.61772 -17.55387 -20.86685 15.61772 > -11.51183 -24.1941 16.4195 -4.907663 -26.33993 16.4195 1.936152 > -27.19954 15.61772 14.42112 -23.14293 15.61772 19.45261 -18.42479 > 16.4195 23.53421 -12.80695 16.4195 26.46661 -6.563733 15.61772 > 29.91736 4.05661 8.716201 30.26722 7.915132 2.953474 29.13909 11.38715 > -2.953474 26.58807 14.30312 -8.716201 5.386907 29.70666 8.716201 > 1.825347 31.23174 2.953475 -1.825349 31.23175 -2.953475 -5.386909 > 29.70667 -8.716201 -26.58807 14.30312 8.716201 -29.13909 11.38715 > 2.953474 -30.26722 7.91513 -2.953474 -29.91737 4.056608 -8.716201 > -21.81924 -20.86685 8.716201 -19.8343 -24.1941 2.953474 -16.88083 > -26.33993 -2.953474 -13.10304 -27.19954 -8.716201 13.10304 -27.19954 > 8.716201 16.88083 -26.33992 2.953475 19.8343 -24.1941 -2.953475 > 21.81925 -20.86685 -8.716201 29.91737 -4.056608 8.716201 30.26722 > -7.915127 2.953474 29.1391 -11.38714 -2.953474 26.58808 -14.30311 > -8.716201 13.10304 27.19954 8.716201 16.88082 26.33993 2.953475 > 19.8343 24.1941 -2.953475 21.81924 20.86685 -8.716201 -21.81924 > 20.86685 8.716201 -19.8343 24.1941 2.953475 -16.88083 26.33992 > -2.953475 -13.10304 27.19954 -8.716201 -26.58807 -14.30312 8.716201 > -29.13909 -11.38715 2.953475 -30.26722 -7.915135 -2.953475 -29.91737 > -4.056613 -8.716201 5.38691 -29.70667 8.7162 1.825349 ... > > read more »
