コード例 #1
0
 public ObjectArmature(BinaryReader reader, Model model)
     : base(reader, model)
 {
     string armatureName = reader.ReadString();
     foreach (var armature in model.Armatures)
     {
         if (armatureName == armature.Name)
         {
             Armature = armature;
             break;
         }
     }
 }
コード例 #2
0
        public ObjectArmature(BinaryReader reader, Model model)
            : base(reader, model)
        {
            string armatureName = reader.ReadString();

            foreach (var armature in model.Armatures)
            {
                if (armatureName == armature.Name)
                {
                    Armature = armature;
                    break;
                }
            }
        }
コード例 #3
0
ファイル: Model.cs プロジェクト: reignstudios/ReignSDK
		private void init(string filename, Stream stream, 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)
		{
			try
			{
				var reader = new BinaryReader(stream);

				// meta data
				if (reader.ReadInt32() != Streams.MakeFourCC('R', 'M', 'F', 'T')) Debug.ThrowError("Error", "Not a ReignModel file: " + filename);
				float version = reader.ReadSingle();
				if (version != 1.0f) Debug.ThrowError("Error", "Unsuported model version: " + version.ToString());
				bool compressed = reader.ReadBoolean();

				// frames
				FrameStart = reader.ReadSingle();
				FrameEnd = reader.ReadSingle();
				FrameCount = FrameEnd - FrameStart;
				FPS = reader.ReadSingle();

				// materials
				int materialCount = reader.ReadInt32();
				Materials = new MaterialI[materialCount];
				Textures = new List<ITexture2D>();
				for (int i = 0; i != materialCount; ++i)
				{
					string name = reader.ReadString();

					// create material
					bool pass = false;
					foreach (var materialType in (Dictionary<string,Type>)materialTypes)
					{
						if (materialType.Key == name)
						{
							Materials[i] = (MaterialI)Activator.CreateInstance(materialType.Value);
							Materials[i].Name = name;
							pass = true;
							break;
						}
					}
					if (!pass) Debug.ThrowError("Model", "Failed to find a valid material type for: " + name);
					var material = Materials[i];

					// values1
					var values1 = new Dictionary<string,float>();
					int valueCount = reader.ReadInt32();
					for (int i2 = 0; i2 != valueCount; ++i2) values1.Add(reader.ReadString(), reader.ReadSingle());
					bindTypes(material, values1, value1BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

					// values2
					var values2 = new Dictionary<string,Vector2>();
					valueCount = reader.ReadInt32();
					for (int i2 = 0; i2 != valueCount; ++i2) values2.Add(reader.ReadString(), reader.ReadVector2());
					bindTypes(material, values2, value2BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

					// values3
					var values3 = new Dictionary<string,Vector3>();
					valueCount = reader.ReadInt32();
					for (int i2 = 0; i2 != valueCount; ++i2) values3.Add(reader.ReadString(), reader.ReadVector3());
					bindTypes(material, values3, value3BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

					// values4
					var values4 = new Dictionary<string,Vector4>();
					valueCount = reader.ReadInt32();
					for (int i2 = 0; i2 != valueCount; ++i2) values4.Add(reader.ReadString(), reader.ReadVector4());
					bindTypes(material, values4, value4BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

					// textures
					var textures = new Dictionary<string,string>();
					int textureCount = reader.ReadInt32();
					for (int i2 = 0; i2 != textureCount; ++i2) textures.Add(reader.ReadString(), reader.ReadString());
					bindTypes(material, textures, textureBinderTypes, contentDirectory, fileExtOverrides, handleFoundTextureBinder);
				}

				// meshes
				int meshCount = reader.ReadInt32();
				Meshes = new Mesh[meshCount];
				for (int i = 0; i != meshCount; ++i)
				{
					Meshes[i] = new Mesh(reader, this, classicInstanceCount);
				}

				// actions
				int actionCount = reader.ReadInt32();
				Actions = new Action[actionCount];
				for (int i = 0; i != actionCount; ++i)
				{
					Actions[i] = new Action(reader);
				}

				// armatures
				int armatureCount = reader.ReadInt32();
				Armatures = new Armature[armatureCount];
				for (int i = 0; i != armatureCount; ++i)
				{
					Armatures[i] = new Armature(reader);
				}

				// objects
				int objectCount = reader.ReadInt32();
				Objects = new Object[objectCount];
				for (int i = 0; i != objectCount; ++i)
				{
					string type = reader.ReadString();
					if (type == "MESH") Objects[i] = new ObjectMesh(reader, this);
					else if (type == "ARMATURE") Objects[i] = new ObjectArmature(reader, this);
					else Debug.ThrowError("Mesh", "Unsuported Object type: " + type);
				}

				// link objects
				foreach (var o in Objects)
				{
					o.linkObjects(Objects);
				}
			}
			catch (Exception e)
			{
				FailedToLoad = true;
				Loader.AddLoadableException(e);
				Dispose();
				if (loadedCallback != null) loadedCallback(this, false);
				return;
			}

			if (Textures.Count == 0)
			{
				Loaded = true;
				if (loadedCallback != null) loadedCallback(this, true);
			}
			else
			{
				new LoadWaiter(Textures.ToArray(),
				delegate(object sender, bool succeeded)
				{
					if (succeeded)
					{
						Loaded = true;
						if (loadedCallback != null) loadedCallback(this, true);
					}
					else
					{
						FailedToLoad = true;
						Dispose();
						if (loadedCallback != null) loadedCallback(this, false);
					}
				});
			}
		}
コード例 #4
0
ファイル: Model.cs プロジェクト: damian-666/ReignSDK
        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);
            }
        }
コード例 #5
0
ファイル: Model.cs プロジェクト: damian-666/ReignSDK
        private void init(string filename, Stream stream, 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)
        {
            try
            {
                var reader = new BinaryReader(stream);

                // meta data
                if (reader.ReadInt32() != Streams.MakeFourCC('R', 'M', 'F', 'T'))
                {
                    Debug.ThrowError("Error", "Not a ReignModel file: " + filename);
                }
                float version = reader.ReadSingle();
                if (version != 1.0f)
                {
                    Debug.ThrowError("Error", "Unsuported model version: " + version.ToString());
                }
                bool compressed = reader.ReadBoolean();

                // frames
                FrameStart = reader.ReadSingle();
                FrameEnd   = reader.ReadSingle();
                FrameCount = FrameEnd - FrameStart;
                FPS        = reader.ReadSingle();

                // materials
                int materialCount = reader.ReadInt32();
                Materials = new MaterialI[materialCount];
                Textures  = new List <ITexture2D>();
                for (int i = 0; i != materialCount; ++i)
                {
                    string name = reader.ReadString();

                    // create material
                    bool pass = false;
                    foreach (var materialType in (Dictionary <string, Type>)materialTypes)
                    {
                        if (materialType.Key == name)
                        {
                            Materials[i]      = (MaterialI)Activator.CreateInstance(materialType.Value);
                            Materials[i].Name = name;
                            pass = true;
                            break;
                        }
                    }
                    if (!pass)
                    {
                        Debug.ThrowError("Model", "Failed to find a valid material type for: " + name);
                    }
                    var material = Materials[i];

                    // values1
                    var values1    = new Dictionary <string, float>();
                    int valueCount = reader.ReadInt32();
                    for (int i2 = 0; i2 != valueCount; ++i2)
                    {
                        values1.Add(reader.ReadString(), reader.ReadSingle());
                    }
                    bindTypes(material, values1, value1BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

                    // values2
                    var values2 = new Dictionary <string, Vector2>();
                    valueCount = reader.ReadInt32();
                    for (int i2 = 0; i2 != valueCount; ++i2)
                    {
                        values2.Add(reader.ReadString(), reader.ReadVector2());
                    }
                    bindTypes(material, values2, value2BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

                    // values3
                    var values3 = new Dictionary <string, Vector3>();
                    valueCount = reader.ReadInt32();
                    for (int i2 = 0; i2 != valueCount; ++i2)
                    {
                        values3.Add(reader.ReadString(), reader.ReadVector3());
                    }
                    bindTypes(material, values3, value3BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

                    // values4
                    var values4 = new Dictionary <string, Vector4>();
                    valueCount = reader.ReadInt32();
                    for (int i2 = 0; i2 != valueCount; ++i2)
                    {
                        values4.Add(reader.ReadString(), reader.ReadVector4());
                    }
                    bindTypes(material, values4, value4BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

                    // textures
                    var textures     = new Dictionary <string, string>();
                    int textureCount = reader.ReadInt32();
                    for (int i2 = 0; i2 != textureCount; ++i2)
                    {
                        textures.Add(reader.ReadString(), reader.ReadString());
                    }
                    bindTypes(material, textures, textureBinderTypes, contentDirectory, fileExtOverrides, handleFoundTextureBinder);
                }

                // meshes
                int meshCount = reader.ReadInt32();
                Meshes = new Mesh[meshCount];
                for (int i = 0; i != meshCount; ++i)
                {
                    Meshes[i] = new Mesh(reader, this, classicInstanceCount);
                }

                // actions
                int actionCount = reader.ReadInt32();
                Actions = new Action[actionCount];
                for (int i = 0; i != actionCount; ++i)
                {
                    Actions[i] = new Action(reader);
                }

                // armatures
                int armatureCount = reader.ReadInt32();
                Armatures = new Armature[armatureCount];
                for (int i = 0; i != armatureCount; ++i)
                {
                    Armatures[i] = new Armature(reader);
                }

                // objects
                int objectCount = reader.ReadInt32();
                Objects = new Object[objectCount];
                for (int i = 0; i != objectCount; ++i)
                {
                    string type = reader.ReadString();
                    if (type == "MESH")
                    {
                        Objects[i] = new ObjectMesh(reader, this);
                    }
                    else if (type == "ARMATURE")
                    {
                        Objects[i] = new ObjectArmature(reader, this);
                    }
                    else
                    {
                        Debug.ThrowError("Mesh", "Unsuported Object type: " + type);
                    }
                }

                // link objects
                foreach (var o in Objects)
                {
                    o.linkObjects(Objects);
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return;
            }

            if (Textures.Count == 0)
            {
                Loaded = true;
                if (loadedCallback != null)
                {
                    loadedCallback(this, true);
                }
            }
            else
            {
                new LoadWaiter(Textures.ToArray(),
                               delegate(object sender, bool succeeded)
                {
                    if (succeeded)
                    {
                        Loaded = true;
                        if (loadedCallback != null)
                        {
                            loadedCallback(this, true);
                        }
                    }
                    else
                    {
                        FailedToLoad = true;
                        Dispose();
                        if (loadedCallback != null)
                        {
                            loadedCallback(this, false);
                        }
                    }
                });
            }
        }