protected override bool ReadInternal() { RootMesh = null; TexturesPaths = new List <string>(); TexturesCache = new Dictionary <int, Texture2D>(); Meshes = new List <ModelMesh>(); if (base.ReadInternal() == false) { return(false); } AnimationLength = Reader.ReadInt32(); ShadeType = (ModelShadeType)Reader.ReadInt32(); if (FileHeader.Version.Version >= 0x0104) { Alpha = Reader.ReadByte(); } else { Alpha = 0; } var unknown = Reader.ReadBytes(16); var textureCount = Reader.ReadInt32(); for (var i = 0; i < textureCount; i++) { var textureName = Reader.ReadStringIso(40); TexturesPaths.Add(string.Format("data/texture/{0}", textureName)); } var mainMeshName = Reader.ReadStringIso(40); var meshCount = Reader.ReadInt32(); for (var i = 0; i < meshCount; i++) { var mesh = new ModelMesh(this); Meshes.Add(mesh); if (mesh.Name == mainMeshName) { RootMesh = mesh; } } if (RootMesh == null) { throw new Exception("Failed to find main-mesh node"); } RootMesh.Parent = null; RootMesh.UpdateChildren(Meshes); var bbmin = new Vector3(999999, 999999, 999999); var bbmax = new Vector3(-999999, -999999, -9999999); RootMesh.SetBoundingBox(ref bbmin, ref bbmax); BbMin = bbmin; BbMax = bbmax; BbRange = (BbMin + BbMax) / 2.0f; RootMesh.CalcMatrix1(); RootMesh.CalcMatrix2(); _realBbMin = new Vector3(999999, 999999, 999999); _realBbMax = new Vector3(-999999, -999999, -999999); //glm::mat4 mat = glm::scale(glm::vec3(1,-1,1)); var mat = Matrix.CreateScale(1, -1, 1); RootMesh.SetBoundingBox2(ref mat, ref _realBbMin, ref _realBbMax); _realBbRange = (_realBbMax + _realBbMin) / 2.0f; MaxRange = Math.Max( Math.Max(_realBbMax.X, -_realBbMin.X), Math.Max( Math.Max(_realBbMax.Y, -_realBbMin.Y), Math.Max(_realBbMax.Z, -_realBbMin.Z) ) ); return(true); }
public void Save(string Path) { // Override file structure using (BinaryWriter bw = new BinaryWriter(new FileStream(Path, FileMode.Create, FileAccess.Write))) { bw.Write(Header.ToCharArray()); // Pointers are calculated always before saving for safety UpdatePointers(); bw.Write(PointerMaterial); bw.Write(PointerMesh); bw.Write(PointerSkeleton); bw.Write(PointerAnimation); bw.Write(PointerMeshGroup); bw.Write(PointerAnimationGroup); bw.Write(PointerSoundEffect); bw.Write(PointerBoundingBox); // Flags bw.Write(FlagUInt01); bw.Write(FlagUInt02); bw.Write(FlagUInt03); bw.Write(FlagUInt04); bw.Write(FlagUInt05); // Details bw.Write((uint)ResourceType); bw.Write(Name.Length); bw.Write(Name.ToCharArray()); bw.Write(UnkByteArray01); // Pointer.BoundingBox bw.Write(RootMesh.Length); bw.Write(RootMesh.ToCharArray()); bw.Write(BoundingBox01); bw.Write(BoundingBox02); bw.Write(ExtraBoundingData.Length > 0 ? 1 : 0); bw.Write(ExtraBoundingData); // Pointer.Material bw.Write(Materials.Count); for (int i = 0; i < Materials.Count; i++) { bw.Write(Materials[i].Index); bw.Write(Materials[i].Path.Length); bw.Write(Materials[i].Path.ToCharArray()); } // Pointer.Mesh bw.Write(Meshes.Count); for (int i = 0; i < Meshes.Count; i++) { bw.Write(Meshes[i].Path.Length); bw.Write(Meshes[i].Path.ToCharArray()); if (FlagUInt01 != 0) { bw.Write(Meshes[i].UnkUInt01); } } // Pointer.Animation bw.Write(UnkUInt01); bw.Write(UnkUInt02); bw.Write(Animations.Count); for (int i = 0; i < Animations.Count; i++) { bw.Write(Animations[i].Path.Length); bw.Write(Animations[i].Path.ToCharArray()); } // Pointer.Skeleton bw.Write(Skeletons.Count); for (int i = 0; i < Skeletons.Count; i++) { bw.Write(Skeletons[i].Path.Length); bw.Write(Skeletons[i].Path.ToCharArray()); bw.Write(Skeletons[i].ExtraBone.Length); bw.Write(Skeletons[i].ExtraBone.ToCharArray()); } // Pointer.MeshGroup bw.Write(MeshGroups.Count); for (int i = 0; i < MeshGroups.Count; i++) { bw.Write(MeshGroups[i].Name.Length); bw.Write(MeshGroups[i].Name.ToCharArray()); bw.Write(MeshGroups[i].FileIndexes.Length); bw.Write(MeshGroups[i].FileIndexes); } // Pointer.AnimationGroup bw.Write(AnimationGroups.Count); for (int i = 0; i < AnimationGroups.Count; i++) { bw.Write(AnimationGroups[i].Name.Length); bw.Write(AnimationGroups[i].Name.ToCharArray()); bw.Write(AnimationGroups[i].Entries.Count); for (int j = 0; j < AnimationGroups[i].Entries.Count; j++) { bw.Write((uint)AnimationGroups[i].Entries[j].Type); bw.Write(AnimationGroups[i].Entries[j].FileIndex); bw.Write(AnimationGroups[i].Entries[j].Events.Count); for (int k = 0; k < AnimationGroups[i].Entries[j].Events.Count; k++) { bw.Write(AnimationGroups[i].Entries[j].Events[k].KeyTime); bw.Write(AnimationGroups[i].Entries[j].Events[k].Type); bw.Write(AnimationGroups[i].Entries[j].Events[k].UnkUInt01); bw.Write(AnimationGroups[i].Entries[j].Events[k].UnkUInt02); } bw.Write(AnimationGroups[i].Entries[j].WalkPoints.Count); bw.Write(AnimationGroups[i].Entries[j].WalkingLength); for (int k = 0; k < AnimationGroups[i].Entries[j].WalkPoints.Count; k++) { bw.Write(AnimationGroups[i].Entries[j].WalkPoints[k].X); bw.Write(AnimationGroups[i].Entries[j].WalkPoints[k].Y); } } } // Pointer.SoundEffect bw.Write(SoundEffectUndecodedBytes); } }