Beispiel #1
0
		public Model(IDisposableResource parent, SoftwareModel softwareModel, bool loadColors, bool loadUVs, bool loadNormals, string contentDirectory, Dictionary<string,Type> materialTypes, List<MaterialFieldBinder> value1BinderTypes, List<MaterialFieldBinder> value2BinderTypes, List<MaterialFieldBinder> value3BinderTypes, List<MaterialFieldBinder> value4BinderTypes, List<MaterialFieldBinder> textureBinderTypes, Dictionary<string,string> fileExtOverrides, int classicInstanceCount, Loader.LoadedCallbackMethod loadedCallback)
		: base(parent)
		{
			Loader.AddLoadable(this);
			new LoadWaiter(new ILoadable[]{softwareModel},
			delegate(object sender, bool succeeeded)
			{
				if (succeeeded)
				{
					using (var stream = new MemoryStream())
					{
						try
						{
							Model.Save(stream, false, softwareModel, loadColors, loadUVs, loadNormals);
							stream.Position = 0;
						}
						catch (Exception e)
						{
							FailedToLoad = true;
							Dispose();
							if (loadedCallback != null) loadedCallback(this, false);
						}

						if (!FailedToLoad) init(null, stream, contentDirectory, materialTypes, value1BinderTypes, value2BinderTypes, value3BinderTypes, value4BinderTypes, textureBinderTypes, fileExtOverrides, classicInstanceCount, loadedCallback);
					}
				}
				else
				{
					FailedToLoad = true;
					Dispose();
					if (loadedCallback != null) loadedCallback(this, false);
				}
			});
		}
Beispiel #2
0
 public static void Save(string filename, bool compress, SoftwareModel softwareModel, bool loadColors, bool loadUVs, bool loadNormals)
 {
     using (var file = Streams.SaveFile(filename, FolderLocations.Unknown))
         #endif
     {
         Save(file, compress, softwareModel, loadColors, loadUVs, loadNormals);
     }
 }
Beispiel #3
0
        public static void Write(BinaryWriter writer, SoftwareModel softwareModel, SoftwareMesh softwareMesh, bool loadColors, bool loadUVs, bool loadNormals)
        {
            List <BufferLayoutElement> elements;

            float[] vertices;
            int[]   indices;
            initData(softwareMesh, loadColors, loadUVs, loadNormals, out elements, out vertices, out indices);

            // name
            writer.Write(softwareMesh.Name);

            // material
            int materialIndex = -1;

            if (softwareMesh.Material != null)
            {
                for (int i = 0; i != softwareModel.Materials.Count; ++i)
                {
                    if (softwareMesh.Material == softwareModel.Materials[i])
                    {
                        materialIndex = i;
                        break;
                    }
                }
            }
            writer.Write(materialIndex);

            // elements
            writer.Write(elements.Count);
            foreach (var element in elements)
            {
                writer.Write((int)element.Type);
                writer.Write((int)element.Usage);
                writer.Write(element.StreamIndex);
                writer.Write(element.UsageIndex);
                writer.Write(element.FloatOffset);
            }

            // vertices
            writer.Write(vertices.Length);
            foreach (var vertex in vertices)
            {
                writer.Write(vertex);
            }

            // indices
            writer.Write(indices.Length);
            foreach (var index in indices)
            {
                writer.Write(index);
            }
        }
 public SoftwareObjectArmature(SoftwareModel model, RMX_Object o)
     : base(model, o)
 {
     // find armature
     foreach (var armature in model.Armatures)
     {
         if (o.Armature.Name == armature.Name)
         {
             Armature = armature;
             break;
         }
     }
     if (Armature == null) Debug.ThrowError("SoftwareObjectArmature", "Failed to find Armature: " + o.Armature.Name);
 }
Beispiel #5
0
        public SoftwareObject(SoftwareModel model, RMX_Object o)
        {
            Model = model;
            Name  = o.Name;

            // transform
            foreach (var input in o.Transform.Inputs)
            {
                switch (input.Type)
                {
                case "EulerRotation": Rotation = new Vector3(input.Values[0], input.Values[1], input.Values[2]); break;

                case "Scale": Scale = new Vector3(input.Values[0], input.Values[1], input.Values[2]); break;

                case "Position": Position = new Vector3(input.Values[0], input.Values[1], input.Values[2]); break;

                default: Debug.ThrowError("SoftwareMesh", "Unsuported Transform Type: " + input.Type); break;
                }
            }

            // find action
            if (o.DefaultAction != null)
            {
                foreach (var action in model.Actions)
                {
                    if (o.DefaultAction.Name == action.Name)
                    {
                        DefaultAction = action;
                        break;
                    }
                }
                if (DefaultAction == null)
                {
                    Debug.ThrowError("SoftwareObjectArmature", "Failed to find Action: " + o.DefaultAction.Name);
                }
            }

            // bone groups
            BoneGroups = new List <SoftwareObjectBoneGroup>();
            if (o.BoneGroups != null)
            {
                foreach (var bonegroup in o.BoneGroups.BoneGroups)
                {
                    BoneGroups.Add(new SoftwareObjectBoneGroup(bonegroup));
                }
            }
        }
 public SoftwareObjectArmature(SoftwareModel model, RMX_Object o)
     : base(model, o)
 {
     // find armature
     foreach (var armature in model.Armatures)
     {
         if (o.Armature.Name == armature.Name)
         {
             Armature = armature;
             break;
         }
     }
     if (Armature == null)
     {
         Debug.ThrowError("SoftwareObjectArmature", "Failed to find Armature: " + o.Armature.Name);
     }
 }
Beispiel #7
0
        public Model(IDisposableResource parent, SoftwareModel softwareModel, bool loadColors, bool loadUVs, bool loadNormals, string contentDirectory, Dictionary <string, Type> materialTypes, List <MaterialFieldBinder> value1BinderTypes, List <MaterialFieldBinder> value2BinderTypes, List <MaterialFieldBinder> value3BinderTypes, List <MaterialFieldBinder> value4BinderTypes, List <MaterialFieldBinder> textureBinderTypes, Dictionary <string, string> fileExtOverrides, int classicInstanceCount, Loader.LoadedCallbackMethod loadedCallback)
            : base(parent)
        {
            Loader.AddLoadable(this);
            new LoadWaiter(new ILoadable[] { softwareModel },
                           delegate(object sender, bool succeeeded)
            {
                if (succeeeded)
                {
                    using (var stream = new MemoryStream())
                    {
                        try
                        {
                            Model.Save(stream, false, softwareModel, loadColors, loadUVs, loadNormals);
                            stream.Position = 0;
                        }
                        catch (Exception e)
                        {
                            FailedToLoad = true;
                            Dispose();
                            if (loadedCallback != null)
                            {
                                loadedCallback(this, false);
                            }
                        }

                        if (!FailedToLoad)
                        {
                            init(null, stream, contentDirectory, materialTypes, value1BinderTypes, value2BinderTypes, value3BinderTypes, value4BinderTypes, textureBinderTypes, fileExtOverrides, classicInstanceCount, loadedCallback);
                        }
                    }
                }
                else
                {
                    FailedToLoad = true;
                    Dispose();
                    if (loadedCallback != null)
                    {
                        loadedCallback(this, false);
                    }
                }
            });
        }
        public SoftwareObject(SoftwareModel model, RMX_Object o)
        {
            Model = model;
            Name = o.Name;

            // transform
            foreach (var input in o.Transform.Inputs)
            {
                switch (input.Type)
                {
                    case "EulerRotation": Rotation = new Vector3(input.Values[0], input.Values[1], input.Values[2]); break;
                    case "Scale": Scale = new Vector3(input.Values[0], input.Values[1], input.Values[2]); break;
                    case "Position": Position = new Vector3(input.Values[0], input.Values[1], input.Values[2]); break;
                    default: Debug.ThrowError("SoftwareMesh", "Unsuported Transform Type: " + input.Type); break;
                }
            }

            // find action
            if (o.DefaultAction != null)
            {
                foreach (var action in model.Actions)
                {
                    if (o.DefaultAction.Name == action.Name)
                    {
                        DefaultAction = action;
                        break;
                    }
                }
                if (DefaultAction == null) Debug.ThrowError("SoftwareObjectArmature", "Failed to find Action: " + o.DefaultAction.Name);
            }

            // bone groups
            BoneGroups = new List<SoftwareObjectBoneGroup>();
            if (o.BoneGroups != null)
            {
                foreach (var bonegroup in o.BoneGroups.BoneGroups)
                {
                    BoneGroups.Add(new SoftwareObjectBoneGroup(bonegroup));
                }
            }
        }
        public SoftwareObjectMesh(SoftwareModel model, RMX_Object o)
            : base(model, o)
        {
            if (o.Mesh != null)
            {
                foreach (var mesh in model.Meshes)
                {
                    if (o.Mesh.Name == mesh.Name)
                    {
                        Mesh = mesh;
                        break;
                    }
                }

                if (Mesh == null)
                {
                    Debug.ThrowError("SoftwareObjectMesh", "Failed to find mesh");
                }
            }
            else
            {
                Debug.ThrowError("SoftwareObjectMesh", "RMX_Object be linked to a Mesh object");
            }
        }
Beispiel #10
0
        public SoftwareMesh(SoftwareModel model, RMX_Mesh mesh)
        {
            Model = model;
            Name  = mesh.Name;

            Verticies = new List <SoftwareVertex>();
            Triangles = new List <SoftwareTriangle>();
            Edges     = new List <SoftwareEdge>();

            VetexComponents       = new List <object>();
            TriangleComponents    = new List <object>();
            EdgeComponents        = new List <object>();
            VertexComponentKeys   = new Dictionary <VertexComponentKeyTypes, int>();
            TriangleComponentKeys = new Dictionary <TriangleComponentKeyTypes, int>();
            EdgeComponentKeys     = new Dictionary <EdgeComponentKeyTypes, int>();

            // material
            if (!string.IsNullOrEmpty(mesh.Material))
            {
                foreach (var material in model.Materials)
                {
                    if (material.Name == mesh.Material)
                    {
                        this.Material = material;
                    }
                }

                if (Material == null)
                {
                    Debug.ThrowError("SoftwareMesh", "Failed to find material: " + mesh.Material);
                }
            }

            // verticies
            var channels     = mesh.Verticies.Channels;
            int positionStep = 0;

            for (int i = 0; i != channels.Length; ++i)
            {
                var channel = channels[i];
                if (channel.ID != "Position")
                {
                    continue;
                }

                Dimensions   = channel.Step;
                positionStep = channel.Step;
                if (channel.Step == 3)
                {
                    var vertexPostions = new Vector3[channel.Values.Length / 3];
                    var positions      = channel.Values;
                    int i2             = 0;
                    for (int i3 = 0; i3 != vertexPostions.Length; ++i3)
                    {
                        Verticies.Add(new SoftwareVertex(i3));
                        vertexPostions[i3] = new Vector3(positions[i2], positions[i2 + 1], positions[i2 + 2]);
                        i2 += 3;
                    }

                    VetexComponents.Add(vertexPostions);
                }
                else if (channel.Step == 2)
                {
                    var vertexPostions = new Vector2[channel.Values.Length / 2];
                    var positions      = channel.Values;
                    int i2             = 0;
                    for (int i3 = 0; i3 != vertexPostions.Length; ++i3)
                    {
                        Verticies.Add(new SoftwareVertex(i3));
                        vertexPostions[i3] = new Vector2(positions[i2], positions[i2 + 1]);
                        i2 += 2;
                    }

                    VetexComponents.Add(vertexPostions);
                }
                else
                {
                    Debug.ThrowError("SoftwareMesh", "Unsupotred position step count");
                }

                VertexComponentKeys.Add(VertexComponentKeyTypes.Positions, i);
            }

            // vertex bone groups
            var counts  = mesh.Verticies.BoneGroups.Counts.Values;
            var indices = mesh.Verticies.BoneGroups.Indices.Values;
            var weights = mesh.Verticies.BoneGroups.Weights.Values;

            if (counts.Length != Verticies.Count)
            {
                Debug.ThrowError("SoftwareMesh", "BoneGroup counts do not match vertex count");
            }
            int bg = 0;

            for (int i = 0; i != counts.Length; ++i)
            {
                for (int i2 = 0; i2 != counts[i]; ++i2)
                {
                    int i3 = bg + i2;
                    Verticies[i].BoneGroups.Add(new SoftwareVertexBoneGroup(indices[i3], weights[i3]));
                }

                bg += counts[i];
            }

            // triangles
            bool hasPositionData = false;

            float[] colors = null, normals = null, uvs = null;
            foreach (var channel in channels)
            {
                switch (channel.ID)
                {
                case "Position": hasPositionData = true; break;

                case "Color": colors = channel.Values; break;

                case "Normal": normals = channel.Values; break;

                case "UV": uvs = channel.Values; break;
                }
            }
            if (!hasPositionData)
            {
                Debug.ThrowError("SoftwareMesh", "Vertices missing position data");
            }

            int[] positionIndices = null, colorIndices = null, normalIndices = null, uvIndices = null;
            foreach (var index in mesh.Faces.Indices)
            {
                switch (index.ID)
                {
                case "Position": positionIndices = index.Values; break;

                case "Color": colorIndices = index.Values; break;

                case "Normal": normalIndices = index.Values; break;

                case "UV": uvIndices = index.Values; break;
                }
            }
            if (positionIndices == null)
            {
                Debug.ThrowError("SoftwareMesh", "Faces missing position data");
            }

            var triangleColors = new List <TriangleColorComponent>();
            var triangleNormals = new List <TriangleNormalComponent>();
            var triangleUVs = new List <TriangleUVComponent>();
            int ti = 0, vi = 0;

            if (positionStep == 3)
            {
                foreach (int step in mesh.Faces.Steps.Values)
                {
                    int loop = step - 2;
                    int vi2 = vi + 1, vi3 = vi + 2;
                    for (int i2 = 0; i2 != loop; ++i2)
                    {
                        var triangle = new SoftwareTriangle(ti, Verticies[positionIndices[vi]], Verticies[positionIndices[vi2]], Verticies[positionIndices[vi3]]);
                        Triangles.Add(triangle);

                        if (colorIndices != null)
                        {
                            int cii = colorIndices[vi] * 3, cii2 = colorIndices[vi2] * 3, cii3 = colorIndices[vi3] * 3;
                            triangleColors.Add(new TriangleColorComponent
                                               (
                                                   new Vector4(colors[cii], colors[cii + 1], colors[cii + 2], colors[cii + 3]),
                                                   new Vector4(colors[cii2], colors[cii2 + 1], colors[cii2 + 2], colors[cii2 + 3]),
                                                   new Vector4(colors[cii3], colors[cii3 + 1], colors[cii3 + 2], colors[cii3 + 3]))
                                               );
                        }

                        if (normalIndices != null)
                        {
                            int nii = normalIndices[vi] * 3, nii2 = normalIndices[vi2] * 3, nii3 = normalIndices[vi3] * 3;
                            triangleNormals.Add(new TriangleNormalComponent
                                                (
                                                    new Vector3(normals[nii], normals[nii + 1], normals[nii + 2]),
                                                    new Vector3(normals[nii2], normals[nii2 + 1], normals[nii2 + 2]),
                                                    new Vector3(normals[nii3], normals[nii3 + 1], normals[nii3 + 2]))
                                                );
                        }

                        if (uvIndices != null)
                        {
                            int uii = uvIndices[vi] * 2, uii2 = uvIndices[vi2] * 2, uii3 = uvIndices[vi3] * 2;
                            triangleUVs.Add(new TriangleUVComponent
                                            (
                                                new Vector2(uvs[uii], uvs[uii + 1]),
                                                new Vector2(uvs[uii2], uvs[uii2 + 1]),
                                                new Vector2(uvs[uii3], uvs[uii3 + 1]))
                                            );
                        }

                        ++ti;
                        ++vi2;
                        ++vi3;
                    }

                    vi += step;
                }
            }
            else
            {
                Debug.ThrowError("SoftwareMesh", "Position step not implemented yet: Step value = " + positionStep);
            }

            int componentIndex = 0;

            if (colors != null)
            {
                TriangleComponents.Add(triangleColors.ToArray());
                TriangleComponentKeys.Add(TriangleComponentKeyTypes.ColorComponents, componentIndex);
                ++componentIndex;
            }

            if (normals != null)
            {
                TriangleComponents.Add(triangleNormals.ToArray());
                TriangleComponentKeys.Add(TriangleComponentKeyTypes.NormalComponents, componentIndex);
                ++componentIndex;
            }

            if (uvs != null)
            {
                TriangleComponents.Add(triangleUVs.ToArray());
                TriangleComponentKeys.Add(TriangleComponentKeyTypes.UVComponents, componentIndex);
                ++componentIndex;
            }
        }
Beispiel #11
0
		public static void Save(Stream stream, bool compress, SoftwareModel softwareModel, bool loadColors, bool loadUVs, bool loadNormals)
		{
			var writer = new BinaryWriter(stream);
			
			// meta data
			writer.Write(Streams.MakeFourCC('R', 'M', 'F', 'T'));// tag
			writer.Write(1.0f);// version
			writer.Write(false);//compress);// TODO: add zip compression

			// frames
			writer.Write(softwareModel.FrameStart);
			writer.Write(softwareModel.FrameEnd);
			writer.Write(softwareModel.FPS);

			// materials
			writer.Write(softwareModel.Materials.Count);
			foreach (var material in softwareModel.Materials)
			{
				writer.Write(material.Name);

				// values1
				writer.Write(material.Values1.Count);
				foreach (var value in material.Values1)
				{
					writer.Write(value.Key);
					writer.Write(value.Value);
				}

				// values2
				writer.Write(material.Values2.Count);
				foreach (var value in material.Values2)
				{
					writer.Write(value.Key);
					writer.WriteVector(value.Value);
				}

				// values3
				writer.Write(material.Values3.Count);
				foreach (var value in material.Values3)
				{
					writer.Write(value.Key);
					writer.WriteVector(value.Value);
				}

				// values4
				writer.Write(material.Values4.Count);
				foreach (var value in material.Values4)
				{
					writer.Write(value.Key);
					writer.WriteVector(value.Value);
				}

				// textures
				writer.Write(material.Textures.Count);
				foreach (var texture in material.Textures)
				{
					writer.Write(texture.Key);
					writer.Write(texture.Value);
				}
			}

			// meshes
			writer.Write(softwareModel.Meshes.Count);
			foreach (var mesh in softwareModel.Meshes)
			{
				Mesh.Write(writer, softwareModel, mesh, loadColors, loadUVs, loadNormals);
			}

			// actions
			writer.Write(softwareModel.Actions.Count);
			foreach (var action in softwareModel.Actions)
			{
				Action.Write(writer, action);
			}

			// armatures
			writer.Write(softwareModel.Armatures.Count);
			foreach (var armature in softwareModel.Armatures)
			{
				Armature.Write(writer, armature);
			}

			// objects
			writer.Write(softwareModel.Objects.Count);
			foreach (var o in softwareModel.Objects)
			{
				Object.Write(writer, o);
			}
		}
Beispiel #12
0
		public static void Save(string filename, bool compress, SoftwareModel softwareModel, bool loadColors, bool loadUVs, bool loadNormals)
		{
			using (var file = Streams.SaveFile(filename, FolderLocations.Unknown))
		#endif
			{
				Save(file, compress, softwareModel, loadColors, loadUVs, loadNormals);
			}
		}
Beispiel #13
0
		public static async void Save(string filename, bool compress, SoftwareModel softwareModel, bool loadColors, bool loadUVs, bool loadNormals)
		{
			using (var file = await Streams.SaveFile(filename, FolderLocations.Unknown))
Beispiel #14
0
        public static void Write(BinaryWriter writer, SoftwareModel softwareModel, SoftwareMesh softwareMesh, bool loadColors, bool loadUVs, bool loadNormals)
        {
            List<BufferLayoutElement> elements;
            float[] vertices;
            int[] indices;
            initData(softwareMesh, loadColors, loadUVs, loadNormals, out elements, out vertices, out indices);

            // name
            writer.Write(softwareMesh.Name);

            // material
            int materialIndex = -1;
            if (softwareMesh.Material != null)
            {
                for (int i = 0; i != softwareModel.Materials.Count; ++i)
                {
                    if (softwareMesh.Material == softwareModel.Materials[i])
                    {
                        materialIndex = i;
                        break;
                    }
                }
            }
            writer.Write(materialIndex);

            // elements
            writer.Write(elements.Count);
            foreach (var element in elements)
            {
                writer.Write((int)element.Type);
                writer.Write((int)element.Usage);
                writer.Write(element.StreamIndex);
                writer.Write(element.UsageIndex);
                writer.Write(element.FloatOffset);
            }

            // vertices
            writer.Write(vertices.Length);
            foreach (var vertex in vertices)
            {
                writer.Write(vertex);
            }

            // indices
            writer.Write(indices.Length);
            foreach (var index in indices)
            {
                writer.Write(index);
            }
        }
Beispiel #15
0
        public static void Save(Stream stream, bool compress, SoftwareModel softwareModel, bool loadColors, bool loadUVs, bool loadNormals)
        {
            var writer = new BinaryWriter(stream);

            // meta data
            writer.Write(Streams.MakeFourCC('R', 'M', 'F', 'T')); // tag
            writer.Write(1.0f);                                   // version
            writer.Write(false);                                  //compress);// TODO: add zip compression

            // frames
            writer.Write(softwareModel.FrameStart);
            writer.Write(softwareModel.FrameEnd);
            writer.Write(softwareModel.FPS);

            // materials
            writer.Write(softwareModel.Materials.Count);
            foreach (var material in softwareModel.Materials)
            {
                writer.Write(material.Name);

                // values1
                writer.Write(material.Values1.Count);
                foreach (var value in material.Values1)
                {
                    writer.Write(value.Key);
                    writer.Write(value.Value);
                }

                // values2
                writer.Write(material.Values2.Count);
                foreach (var value in material.Values2)
                {
                    writer.Write(value.Key);
                    writer.WriteVector(value.Value);
                }

                // values3
                writer.Write(material.Values3.Count);
                foreach (var value in material.Values3)
                {
                    writer.Write(value.Key);
                    writer.WriteVector(value.Value);
                }

                // values4
                writer.Write(material.Values4.Count);
                foreach (var value in material.Values4)
                {
                    writer.Write(value.Key);
                    writer.WriteVector(value.Value);
                }

                // textures
                writer.Write(material.Textures.Count);
                foreach (var texture in material.Textures)
                {
                    writer.Write(texture.Key);
                    writer.Write(texture.Value);
                }
            }

            // meshes
            writer.Write(softwareModel.Meshes.Count);
            foreach (var mesh in softwareModel.Meshes)
            {
                Mesh.Write(writer, softwareModel, mesh, loadColors, loadUVs, loadNormals);
            }

            // actions
            writer.Write(softwareModel.Actions.Count);
            foreach (var action in softwareModel.Actions)
            {
                Action.Write(writer, action);
            }

            // armatures
            writer.Write(softwareModel.Armatures.Count);
            foreach (var armature in softwareModel.Armatures)
            {
                Armature.Write(writer, armature);
            }

            // objects
            writer.Write(softwareModel.Objects.Count);
            foreach (var o in softwareModel.Objects)
            {
                Object.Write(writer, o);
            }
        }
Beispiel #16
0
 public static async void Save(string filename, bool compress, SoftwareModel softwareModel, bool loadColors, bool loadUVs, bool loadNormals)
 {
     using (var file = await Streams.SaveFile(filename, FolderLocations.Unknown))