📄 dae.as.svn-base
字号:
return stack;
}
/**
*
* @param instance_controller
* @param instance
* @return
*/
private function buildMorph( instance_controller:DaeInstanceController, instance:AnimatedMesh3D ):void
{
var controller:DaeController = document.controllers[instance_controller.url];
var morph:DaeMorph = controller.morph;
var success:Boolean = buildGeometry(morph.source, instance);
if( !success )
{
Logger.error("[ERROR] could not find geometry for morph!");
throw new Error("could not find geometry for morph!");
}
var ctl:MorphController = new MorphController(instance.geometry);
var target0:DisplayObject3D = new DisplayObject3D();
buildGeometry(morph.source, target0);
var frame:uint = 0;
var duration:uint = AnimationEngine.NUM_FRAMES / morph.targets.length;
// use a copy of the original vertices!
ctl.addFrame(new AnimationFrame(frame, duration, target0.geometry.vertices, "start"));
frame += duration;
for( var i:int = 0; i < morph.targets.length; i++ )
{
var obj:DisplayObject3D = new DisplayObject3D();
var target:String = morph.targets[i];
var weight:Number = morph.weights[i];
buildGeometry(target, obj);
ctl.addFrame(new AnimationFrame(frame, duration, obj.geometry.vertices, target));
frame += duration;
}
instance.addController(ctl);
_morphs[ instance ] = true;
}
/**
*
* @param node
* @return
*/
private function buildNode( node:DaeNode, parent:DisplayObject3D ):void
{
var instance_controller :DaeInstanceController = findSkinController(node);
var instance_ctl_morph :DaeInstanceController = findMorphController(node);
var newNode:DisplayObject3D;
var instance:DisplayObject3D;
var material:MaterialObject3D;
if( instance_controller )
{
buildMaterialInstances(instance_controller.materials);
newNode = buildSkin(instance_controller, material);
if( newNode )
{
instance = parent.addChild(newNode);
}
}
else if( instance_ctl_morph )
{
buildMaterialInstances(instance_ctl_morph.materials);
newNode = new AnimatedMesh3D(material, new Array(), new Array(), node.id);
buildMorph(instance_ctl_morph, newNode as AnimatedMesh3D);
instance = parent.addChild(newNode);
}
else if( node.geometries.length )
{
newNode = new Node3D(node.name, node.id, node.sid);
for each( var geomInst:DaeInstanceGeometry in node.geometries )
{
material = buildMaterialInstances(geomInst.materials);
var inst:TriangleMesh3D = new TriangleMesh3D(material, new Array(), new Array());
buildGeometry(geomInst.url, inst, material);
newNode.addChild(inst);
}
instance = parent.addChild(newNode);
Node3D(instance).matrixStack = buildMatrixStack(node);
Node3D(instance).transforms = node.transforms;
}
else
{
instance = parent.addChild(new Node3D(node.name, node.id, node.sid));
Node3D(instance).matrixStack = buildMatrixStack(node);
Node3D(instance).transforms = node.transforms;
}
for( var j:int = 0; j < node.instance_nodes.length; j++ )
{
var instance_node:DaeInstanceNode = node.instance_nodes[j];
var dae_node:DaeNode = document.getDaeNodeById(instance_node.url);
buildNode(dae_node, instance);
}
for( var i:int = 0; i < node.nodes.length; i++ )
buildNode(node.nodes[i], instance);
var matrix:Matrix3D = buildMatrix(node);
instance.copyTransform( matrix );
}
/**
*
* @param event
* @return
*/
private function buildScene( event:Event ):void
{
if( _reader.hasEventListener(Event.COMPLETE) )
_reader.removeEventListener(Event.COMPLETE, buildScene);
this.document = _reader.document;
_yUp = (this.document.asset.yUp == ASCollada.DAE_Y_UP);
_materialInstances = new Object();
_materialTextureSets = new Object();
_skins = new Dictionary();
_morphs = new Dictionary();
buildMaterials();
buildVisualScene();
linkSkins(this._rootNode);
readySkins(this);
readyMorphs(this);
if( !_loadScaleSet )
this.scale = DEFAULT_SCALE;
// there may be animations left to parse...
if( document.numQueuedAnimations )
{
hasAnimations = true;
_reader.addEventListener( Event.COMPLETE, animationCompleteHandler );
_reader.addEventListener( ProgressEvent.PROGRESS, animationProgressHandler );
_reader.readAnimations();
}
else
hasAnimations = false;
// done with geometry
dispatchEvent(new Event(Event.COMPLETE));
}
/**
*
* @param instance_controller
* @return
*/
private function buildSkin( instance_controller:DaeInstanceController, material:MaterialObject3D = null ):TriangleMesh3D
{
var controller:DaeController = document.controllers[ instance_controller.url ];
if( !controller || !controller.skin )
{
Logger.trace( "[WARNING] no skin controller!" );
return null;
}
var skin:DaeSkin = controller.skin;
var obj:Skin3D = new Skin3D(material, new Array(), new Array(), skin.source, (document.yUp == DaeDocument.Y_UP));
obj.bindPose = new Matrix3D(skin.bind_shape_matrix);
obj.joints = new Array();
var success:Boolean = buildGeometry(skin.source, obj);
// geometry could reside in a morph controller
if( !success && document.controllers[skin.source] )
{
var morph_controller:DaeController = document.controllers[skin.source];
if( morph_controller.morph )
{
success = buildGeometry(morph_controller.morph.source, obj);
if( success )
{
var ctl:MorphController = new MorphController(obj.geometry);
var method:String = morph_controller.morph.method;
var duration:int = AnimationEngine.NUM_FRAMES / morph_controller.morph.targets.length;
var frame:uint = 0;
for( var i:int = 0; i < morph_controller.morph.targets.length; i++ )
{
var morph:DisplayObject3D = new DisplayObject3D();
var target:String = morph_controller.morph.targets[i];
var weight:Number = morph_controller.morph.weights[i];
var morph_succes:Boolean = buildGeometry(target, morph);
if( morph_succes )
{
ctl.addFrame(new AnimationFrame(frame, duration, morph.geometry.vertices, target));
frame += duration;
//obj.morph_targets.push( morph.geometry );
//obj.morph_weights.push( weight );
}
}
obj.addController(ctl);
}
}
}
if( !success )
{
Logger.error( "[ERROR] could not find geometry for skin!" );
throw new Error( "could not find geometry for skin!" );
}
obj.geometry.ready = true;
_skins[ obj ] = instance_controller;
if( !this.skin )
this.skin = obj;
return obj;
}
/**
*
* @param spline
* @return
*/
private function buildSpline( spline:DaeSpline ):DisplayObject3D
{
var lines:Lines3D = new Lines3D(new LineMaterial(0xffff00, 0.5));
for( var i:int = 0; i < spline.vertices.length; i++ )
{
var v0:Array = spline.vertices[i];
var v1:Array = spline.vertices[(i+1) % spline.vertices.length];
lines.addNewLine(0, v0[0], v0[1], v0[2], v1[0], v1[1], v1[2]);
}
return lines;
}
/**
*
* @param mesh
* @return
*/
private function buildVertices( mesh:DaeMesh ):Array
{
var vertices:Array = new Array();
for( var i:int = 0; i < mesh.vertices.length; i++ )
{
var v:Array = mesh.vertices[i];
vertices.push(new Vertex3D(v[0], v[1], v[2]));
}
return vertices;
}
/**
* Builds the visual scene (scenegraph).
*/
private function buildVisualScene():void
{
this._rootNode = addChild(new DisplayObject3D("COLLADA_root"));
for( var i:int = 0; i < document.vscene.nodes.length; i++ )
buildNode(document.vscene.nodes[i], this._rootNode);
}
/**
* Clones the source and append the clone to target. Then recurse...
*
* @param target
* @param source
*/
private function cloneObj( target:DisplayObject3D, source:DisplayObject3D ):void
{
var o:DisplayObject3D;
if( source === _rootNode )
{
o = new DisplayObject3D( source.name + "-" + _numClones );
o.copyTransform( source.transform );
target = target.addChild(o);
}
else if( source is TriangleMesh3D )
{
o = new TriangleMesh3D(source.material, new Array(), new Array(), source.name + "-" + _numClones);
o.geometry = cloneGeometry(o, source.geometry);
o.geometry.ready = true;
target = target.addChild(o);
}
else if( source is Node3D )
{
var n:Node3D = source as Node3D;
o = new Node3D(n.name + "-" + _numClones, n.daeID, n.daeSID);
o.copyTransform(n.transform);
target = target.addChild(o);
}
else if( source is DisplayObject3D )
{
o = new DisplayObject3D(source.name + "-" + _numClones);
o.copyTransform(source.transform);
target = target.addChild(o);
}
for each( var child:DisplayObject3D in source.children )
{
cloneObj(target, child);
}
}
/**
* Clones a GeometryObject3D an sets up the faces for target.
*
* @param target The target for the cloned faces and vertices.
* @param source The source GeometryObject3D.
*
* @return GeometryObject3D
*/
private function cloneGeometry( target:DisplayObject3D, source:GeometryObject3D ):GeometryObject3D
{
var geom:GeometryObject3D = new GeometryObject3D();
var vertices:Array = source.vertices;
var faces:Array = source.faces;
var i:int;
var newVerts:Dictionary = new Dictionary();
geom.vertices = new Array();
geom.faces = new Array();
for( i = 0; i < vertices.length; i++ )
{
var v:Vertex3D = vertices[i];
newVerts[ v ] = v.clone();
geom.vertices.push( newVerts[ v ] );
}
for( i = 0; i < faces.length; i++ )
{
var f:Triangle3D = faces[i];
var v0:Vertex3D = newVerts[ f.v0 ];
var v1:Vertex3D = newVerts[ f.v1 ];
var v2:Vertex3D = newVerts[ f.v2 ];
var uv0:NumberUV = f.uv[0].clone();
var uv1:NumberUV = f.uv[1].clone();
var uv2:NumberUV = f.uv[2].clone();
var newTri:Triangle3D = new Triangle3D(target, [v0, v1, v2], f.material, [uv0, uv1, uv2]);
geom.faces.push( newTri );
}
return geom;
}
/**
*
* @param id
* @return
*/
private function findAnimationChannelsByID( id:String ):Array
{
var channels:Array = new Array();
try
{
for each( var animation:DaeAnimation in document.animations )
{
for each( var channel:DaeChannel in animation.channels )
{
var target:String = channel.target.split("/").shift() as String;
if( target == id )
channels.push(channel);
}
}
}
catch( e:Error )
{
}
return channels;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -