Пример #1
0
        /*-----------------------------------------------------------------------------------------
        *
        *	Save/Load stuff :
        *
        *  -----------------------------------------------------------------------------------------*/

        /// <summary>
        /// Loads scene
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Scene Load(Stream stream)
        {
            var scene = new Scene();

            using (var reader = new BinaryReader(stream)) {
                if (!reader.CheckMagic("SCN1"))
                {
                    throw new Exception("Bad scene format. SCN1 expected.");
                }

                //---------------------------------------------
                scene.StartTime  = new TimeSpan(reader.ReadInt64());
                scene.EndTime    = new TimeSpan(reader.ReadInt64());
                scene.firstFrame = reader.ReadInt32();
                scene.lastFrame  = reader.ReadInt32();
                scene.trackCount = reader.ReadInt32();

                if (!reader.CheckMagic("ANIM"))
                {
                    throw new Exception("Bad scene format. ANIM expected.");
                }

                if (scene.trackCount != 0)
                {
                    scene.CreateAnimation(scene.FirstFrame, scene.LastFrame, scene.trackCount);
                }

                for (int fi = scene.firstFrame; fi <= scene.lastFrame; fi++)
                {
                    for (int ni = 0; ni < scene.trackCount; ni++)
                    {
                        scene.animData[fi - scene.firstFrame, ni] = reader.Read <Matrix>();
                    }
                }

                //---------------------------------------------
                if (!reader.CheckMagic("MTRL"))
                {
                    throw new Exception("Bad scene format. MTRL expected.");
                }

                var mtrlCount = reader.ReadInt32();

                scene.materials.Clear();

                for (int i = 0; i < mtrlCount; i++)
                {
                    var mtrl = new MeshMaterial();
                    mtrl.Name = reader.ReadString();

                    if (reader.ReadBoolean() == true)
                    {
                        mtrl.TexturePath = reader.ReadString();
                    }
                    else
                    {
                        mtrl.TexturePath = null;
                    }
                    scene.Materials.Add(mtrl);
                }

                //---------------------------------------------
                if (!reader.CheckMagic("NODE"))
                {
                    throw new Exception("Bad scene format. NODE expected.");
                }

                var nodeCount = reader.ReadInt32();

                for (int i = 0; i < nodeCount; ++i)
                {
                    var node = new Node();
                    node.Name        = reader.ReadString();
                    node.ParentIndex = reader.ReadInt32();
                    node.MeshIndex   = reader.ReadInt32();
                    node.TrackIndex  = reader.ReadInt32();
                    node.Transform   = reader.Read <Matrix>();
                    node.BindPose    = reader.Read <Matrix>();
                    scene.nodes.Add(node);
                }

                //---------------------------------------------
                if (!reader.CheckMagic("MESH"))
                {
                    throw new Exception("Bad scene format. MESH expected.");
                }

                var meshCount = reader.ReadInt32();

                for (int i = 0; i < meshCount; i++)
                {
                    var mesh = new Mesh();
                    mesh.Deserialize(reader);
                    scene.Meshes.Add(mesh);
                }
            }

            return(scene);
        }
Пример #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="material"></param>
 /// <returns></returns>
 public abstract TMaterial Convert(MeshMaterial material);