PageRenderTime 53ms CodeModel.GetById 20ms app.highlight 17ms RepoModel.GetById 13ms app.codeStats 0ms

/src/away3d/tools/serialize/Serialize.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 178 lines | 153 code | 24 blank | 1 comment | 18 complexity | b758706ac4faef13b71b741c0c44f7e5 MD5 | raw file
  1package away3d.tools.serialize
  2{
  3	import away3d.animators.IAnimator;
  4	import away3d.animators.data.JointPose;
  5	import away3d.animators.data.Skeleton;
  6	import away3d.animators.data.SkeletonJoint;
  7	import away3d.animators.data.SkeletonPose;
  8	import away3d.arcane;
  9	import away3d.containers.ObjectContainer3D;
 10	import away3d.containers.Scene3D;
 11	import away3d.core.base.ISubGeometry;
 12	import away3d.core.base.SkinnedSubGeometry;
 13	import away3d.core.base.SubMesh;
 14	import away3d.entities.Mesh;
 15	import away3d.materials.MaterialBase;
 16	import away3d.materials.lightpickers.StaticLightPicker;
 17	
 18	import flash.utils.getQualifiedClassName;
 19	
 20	use namespace arcane;
 21	
 22	public class Serialize
 23	{
 24		public static var tabSize:uint = 2;
 25		
 26		public function Serialize()
 27		{
 28		}
 29		
 30		public static function serializeScene(scene:Scene3D, serializer:SerializerBase):void
 31		{
 32			for (var i:uint = 0; i < scene.numChildren; i++)
 33				serializeObjectContainer(scene.getChildAt(i), serializer);
 34		}
 35		
 36		public static function serializeObjectContainer(objectContainer3D:ObjectContainer3D, serializer:SerializerBase):void
 37		{
 38			if (objectContainer3D is Mesh)
 39				serializeMesh(objectContainer3D as Mesh, serializer); // do not indent any extra for first level here
 40			else
 41				serializeObjectContainerInternal(objectContainer3D, serializer, true /* serializeChildrenAndEnd */);
 42		}
 43		
 44		public static function serializeMesh(mesh:Mesh, serializer:SerializerBase):void
 45		{
 46			serializeObjectContainerInternal(mesh as ObjectContainer3D, serializer, false /* serializeChildrenAndEnd */);
 47			serializer.writeBoolean("castsShadows", mesh.castsShadows);
 48			
 49			if (mesh.animator)
 50				serializeAnimationState(mesh.animator, serializer);
 51			
 52			if (mesh.material)
 53				serializeMaterial(mesh.material, serializer);
 54			
 55			if (mesh.subMeshes.length) {
 56				for each (var subMesh:SubMesh in mesh.subMeshes)
 57					serializeSubMesh(subMesh, serializer);
 58			}
 59			serializeChildren(mesh as ObjectContainer3D, serializer);
 60			serializer.endObject();
 61		}
 62		
 63		public static function serializeAnimationState(animator:IAnimator, serializer:SerializerBase):void
 64		{
 65			serializer.beginObject(classNameFromInstance(animator), null);
 66			serializeAnimator(animator, serializer);
 67			serializer.endObject();
 68		}
 69		
 70		public static function serializeAnimator(animator:IAnimator, serializer:SerializerBase):void
 71		{
 72			serializer.beginObject(classNameFromInstance(animator), null);
 73			serializer.endObject();
 74		}
 75		
 76		public static function serializeSubMesh(subMesh:SubMesh, serializer:SerializerBase):void
 77		{
 78			serializer.beginObject(classNameFromInstance(subMesh), null);
 79			if (subMesh.material)
 80				serializeMaterial(subMesh.material, serializer);
 81			if (subMesh.subGeometry)
 82				serializeSubGeometry(subMesh.subGeometry, serializer);
 83			serializer.endObject();
 84		}
 85		
 86		public static function serializeMaterial(material:MaterialBase, serializer:SerializerBase):void
 87		{
 88			serializer.beginObject(classNameFromInstance(material), material.name);
 89			
 90			if (material.lightPicker is StaticLightPicker)
 91				serializer.writeString("lights", String(StaticLightPicker(material.lightPicker).lights));
 92			serializer.writeBoolean("mipmap", material.mipmap);
 93			serializer.writeBoolean("smooth", material.smooth);
 94			serializer.writeBoolean("repeat", material.repeat);
 95			serializer.writeBoolean("bothSides", material.bothSides);
 96			serializer.writeString("blendMode", material.blendMode);
 97			serializer.writeBoolean("requiresBlending", material.requiresBlending);
 98			serializer.writeUint("uniqueId", material.uniqueId);
 99			serializer.writeUint("numPasses", material.numPasses);
100			serializer.endObject();
101		}
102		
103		public static function serializeSubGeometry(subGeometry:ISubGeometry, serializer:SerializerBase):void
104		{
105			serializer.beginObject(classNameFromInstance(subGeometry), null);
106			serializer.writeUint("numTriangles", subGeometry.numTriangles);
107			if (subGeometry.indexData)
108				serializer.writeUint("numIndices", subGeometry.indexData.length);
109			serializer.writeUint("numVertices", subGeometry.numVertices);
110			if (subGeometry.UVData)
111				serializer.writeUint("numUVs", subGeometry.UVData.length);
112			var skinnedSubGeometry:SkinnedSubGeometry = subGeometry as SkinnedSubGeometry;
113			if (skinnedSubGeometry) {
114				if (skinnedSubGeometry.jointWeightsData)
115					serializer.writeUint("numJointWeights", skinnedSubGeometry.jointWeightsData.length);
116				if (skinnedSubGeometry.jointIndexData)
117					serializer.writeUint("numJointIndexes", skinnedSubGeometry.jointIndexData.length);
118			}
119			serializer.endObject();
120		}
121		
122		public static function serializeSkeletonJoint(skeletonJoint:SkeletonJoint, serializer:SerializerBase):void
123		{
124			serializer.beginObject(classNameFromInstance(skeletonJoint), skeletonJoint.name);
125			serializer.writeInt("parentIndex", skeletonJoint.parentIndex);
126			serializer.writeTransform("inverseBindPose", skeletonJoint.inverseBindPose);
127			serializer.endObject();
128		}
129		
130		public static function serializeSkeleton(skeleton:Skeleton, serializer:SerializerBase):void
131		{
132			serializer.beginObject(classNameFromInstance(skeleton), skeleton.name);
133			for each (var skeletonJoint:SkeletonJoint in skeleton.joints)
134				serializeSkeletonJoint(skeletonJoint, serializer);
135			serializer.endObject();
136		}
137		
138		public static function serializeJointPose(jointPose:JointPose, serializer:SerializerBase):void
139		{
140			serializer.beginObject(classNameFromInstance(jointPose), jointPose.name);
141			serializer.writeVector3D("translation", jointPose.translation);
142			serializer.writeQuaternion("orientation", jointPose.orientation);
143			serializer.endObject();
144		}
145		
146		public static function serializeSkeletonPose(skeletonPose:SkeletonPose, serializer:SerializerBase):void
147		{
148			serializer.beginObject(classNameFromInstance(skeletonPose), "" /*skeletonPose.name*/);
149			serializer.writeUint("numJointPoses", skeletonPose.numJointPoses);
150			for each (var jointPose:JointPose in skeletonPose.jointPoses)
151				serializeJointPose(jointPose, serializer);
152			serializer.endObject();
153		}
154		
155		// private stuff - shouldn't ever need to call externally
156		
157		private static function serializeChildren(parent:ObjectContainer3D, serializer:SerializerBase):void
158		{
159			for (var i:uint = 0; i < parent.numChildren; i++)
160				serializeObjectContainer(parent.getChildAt(i), serializer);
161		}
162		
163		private static function classNameFromInstance(instance:*):String
164		{
165			return getQualifiedClassName(instance).split("::").pop();
166		}
167		
168		private static function serializeObjectContainerInternal(objectContainer:ObjectContainer3D, serializer:SerializerBase, serializeChildrenAndEnd:Boolean):void
169		{
170			serializer.beginObject(classNameFromInstance(objectContainer), objectContainer.name);
171			serializer.writeTransform("transform", objectContainer.transform.rawData);
172			if (serializeChildrenAndEnd) {
173				serializeChildren(objectContainer, serializer);
174				serializer.endObject();
175			}
176		}
177	}
178}