public void SetSubmeshMaterial(int meshIdx, int submeshIdx, string matId) { ObjectID materialId = new ObjectID(matId); odfSubmesh submesh = Parser.MeshSection[meshIdx][submeshIdx]; submesh.MaterialId = materialId; }
public static void CopyUnknowns(odfSubmesh srcSubmesh, odfSubmesh destSubmesh, int format) { destSubmesh.Unknown1 = srcSubmesh.Unknown1; destSubmesh.AlwaysZero1 = (byte[])srcSubmesh.AlwaysZero1.Clone(); destSubmesh.AlwaysZero2 = (byte[])srcSubmesh.AlwaysZero2.Clone(); destSubmesh.Unknown4 = srcSubmesh.Unknown4; destSubmesh.Unknown5 = srcSubmesh.Unknown5; destSubmesh.Unknown6 = srcSubmesh.Unknown6; destSubmesh.Unknown7 = srcSubmesh.Unknown7; destSubmesh.Unknown8 = (byte[])srcSubmesh.Unknown8.Clone(); if (format < 10) { if (srcSubmesh.AlwaysZero3 != null) { destSubmesh.AlwaysZero3 = (byte[])srcSubmesh.AlwaysZero3.Clone(); } else if (destSubmesh.AlwaysZero3 == null) { destSubmesh.AlwaysZero3 = new byte[448]; } } else { destSubmesh.AlwaysZero3 = null; } destSubmesh.AlwaysZero4 = (byte[])srcSubmesh.AlwaysZero4.Clone(); }
public void SetSubmeshId(int meshIdx, int submeshIdx, string id) { ObjectID newsubmeshID = new ObjectID(id); if (Parser.IsUsedID(newsubmeshID)) { throw new FormatException("ID is already in use"); } odfSubmesh submesh = Parser.MeshSection[meshIdx][submeshIdx]; ObjectID oldsubmeshId = submesh.Id; foreach (odfBoneList boneList in Parser.EnvelopeSection) { if (boneList.SubmeshId == oldsubmeshId) { boneList.SubmeshId = newsubmeshID; break; } } foreach (odfMorphObject morphObj in Parser.MorphSection) { if (morphObj.SubmeshId == oldsubmeshId) { morphObj.SubmeshId = newsubmeshID; } } submesh.Id = newsubmeshID; Parser.UsedIDs.Add((int)newsubmeshID, typeof(odfSubmesh)); Parser.UsedIDs.Remove((int)oldsubmeshId); }
public static void RemoveSubmesh(odfParser parser, odfSubmesh submesh, bool deleteMorphs) { odfBoneList boneList = odf.FindBoneList(submesh.Id, parser.EnvelopeSection); if (boneList != null) { parser.EnvelopeSection.RemoveChild(boneList); parser.UsedIDs.Remove((int)boneList.Id); } if (parser.MorphSection != null && deleteMorphs) { for (int i = 0; i < parser.MorphSection.Count; i++) { odfMorphObject morphObj = parser.MorphSection[i]; if (morphObj.SubmeshId == submesh.Id) { parser.MorphSection.RemoveChild(i); --i; } } } ((odfMesh)submesh.Parent).RemoveChild(submesh); }
public odfSubmesh Clone() { odfSubmesh newSubmesh = new odfSubmesh(Name, Id, _FormatType); newSubmesh.Unknown1 = Unknown1; newSubmesh.AlwaysZero1 = (byte[])AlwaysZero1.Clone(); newSubmesh.MaterialId = new ObjectID(MaterialId); newSubmesh.TextureIds = (ObjectID[])TextureIds.Clone(); newSubmesh.AlwaysZero2 = (byte[])AlwaysZero2.Clone(); newSubmesh.Unknown4 = Unknown4; newSubmesh.Unknown5 = Unknown5; newSubmesh.Unknown6 = Unknown6; if (AlwaysZero3 != null) { newSubmesh.AlwaysZero3 = (byte[])AlwaysZero3.Clone(); } newSubmesh.Unknown7 = Unknown7; newSubmesh.Unknown8 = (byte[])Unknown8.Clone(); newSubmesh.VertexList = new List <odfVertex>(NumVertices); foreach (odfVertex vert in VertexList) { newSubmesh.VertexList.Add(vert.Clone()); } newSubmesh.FaceList = new List <odfFace>(NumVertexIndices / 3); foreach (odfFace face in FaceList) { newSubmesh.FaceList.Add(face.Clone()); } newSubmesh.AlwaysZero4 = (byte[])AlwaysZero4.Clone(); return(newSubmesh); }
public static void RemoveMesh(odfParser parser, odfMesh mesh, odfFrame meshFrame, bool deleteMorphs) { while (mesh.Count > 0) { odfSubmesh submesh = mesh[0]; RemoveSubmesh(parser, submesh, deleteMorphs); } meshFrame.MeshId = ObjectID.INVALID; parser.MeshSection.RemoveChild(mesh); parser.UsedIDs.Remove((int)mesh.Id); }
public void HighlightBone(odfParser parser, int meshIdx, int submeshIdx, int boneIdx) { const int boneObjSize = 16; odfSubmesh submesh = parser.MeshSection[meshIdx][submeshIdx]; odfBoneList boneList = odf.FindBoneList(submesh.Id, parser.EnvelopeSection); string boneFrameName = null; if (boneIdx >= 0) { odfBone bone = boneList[boneIdx]; boneFrameName = odf.FindFrame(bone.FrameId, parser.FrameSection.RootFrame).Name; } AnimationMeshContainer mesh = (AnimationMeshContainer)meshFrames[0].MeshContainer; for (int i = 0; mesh != null; i++) { if (i == submeshIdx && (mesh.MeshData != null) && (mesh.MeshData.Mesh != null)) { List <odfVertex> vertexList = submesh.VertexList; float[][] vertexWeights = ConvertVertexWeights(vertexList, boneList); FillVertexBuffer(mesh.MeshData.Mesh, vertexList, vertexWeights, boneIdx); // break; } if (boneIdx >= 0) { for (int idx = 0; idx < mesh.BoneLines.Length / boneObjSize; idx++) { if (mesh.BoneNames[idx] == boneFrameName) { for (int j = 0; j < boneObjSize; j++) { mesh.BoneLines[idx * boneObjSize + j].Color = Color.Crimson.ToArgb(); } break; } } } else { for (int idx = 0; idx < mesh.BoneLines.Length / boneObjSize; idx++) { for (int j = 0; j < boneObjSize; j++) { mesh.BoneLines[idx * boneObjSize + j].Color = Color.CornflowerBlue.ToArgb(); } } } mesh = (AnimationMeshContainer)mesh.NextMeshContainer; } }
public void SetSubmeshUnknowns(int meshIdx, int submeshIdx, int Unknown1, int Unknown31, int Unknown4, int Unknown5, int Unknown6, int Unknown7, double Unknown8) { odfSubmesh submesh = Parser.MeshSection[meshIdx][submeshIdx]; submesh.Unknown1 = Unknown1; submesh.Unknown31 = (UInt32)Unknown31; submesh.Unknown4 = Unknown4; submesh.Unknown5 = Unknown5; submesh.Unknown6 = Unknown6; submesh.Unknown7 = Unknown7; submesh.Unknown8 = BitConverter.GetBytes((float)Unknown8); }
public static void odfSubmesh(odfSubmesh submesh, bool emptyFieldsOnly) { if (!emptyFieldsOnly) { submesh.Unknown1 = 1; } if (!emptyFieldsOnly || submesh.AlwaysZero1 == null) { submesh.AlwaysZero1 = new byte[4]; } if (!emptyFieldsOnly) { submesh.Unknown31 = 0; } if (!emptyFieldsOnly || submesh.AlwaysZero2 == null) { submesh.AlwaysZero2 = new byte[16]; } if (!emptyFieldsOnly) { submesh.Unknown4 = 0x0F; } if (!emptyFieldsOnly) { submesh.Unknown5 = 2; } if (!emptyFieldsOnly) { submesh.Unknown6 = 0; } if (!emptyFieldsOnly) { submesh.Unknown7 = 0x0600; } if (!emptyFieldsOnly) { submesh.Unknown8 = BitConverter.GetBytes(0.4f); } if ((!emptyFieldsOnly || submesh.AlwaysZero3 == null) && submesh._FormatType < 10) { submesh.AlwaysZero3 = new byte[448]; } if (!emptyFieldsOnly || submesh.AlwaysZero4 == null) { submesh.AlwaysZero4 = new byte[24]; } }
public static odfSubmesh FindMeshObject(ObjectID id, odfMeshSection meshSection) { for (int meshIdx = 0; meshIdx < meshSection.Count; meshIdx++) { odfMesh mesh = meshSection[meshIdx]; for (int submeshIdx = 0; submeshIdx < mesh.Count; submeshIdx++) { odfSubmesh meshObj = mesh[submeshIdx]; if (meshObj.Id == id) { return(meshObj); } } } return(null); }
private new void listViewAnimationTrack_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { List <DockContent> formODFList; if (!Gui.Docking.DockContents.TryGetValue(typeof(FormMeshView), out formODFList)) { return; } foreach (FormMeshView formMesh in formODFList) { odfFrame boneFrame = odf.FindFrame(e.Item.Text, formMesh.Editor.Parser.FrameSection.RootFrame); if (boneFrame == null) { continue; } for (int i = 0; i < formMesh.renderObjectMeshes.Count; i++) { RenderObjectODF mesh = formMesh.renderObjectMeshes[i]; if (mesh != null && formMesh.renderObjectIds[i] > -1) { odfMesh odfMesh = formMesh.Editor.Parser.MeshSection[i]; for (int j = 0; j < odfMesh.Count; j++) { odfSubmesh submesh = odfMesh[j]; odfBoneList bones = odf.FindBoneList(submesh.Id, formMesh.Editor.Parser.EnvelopeSection); if (bones == null) { continue; } for (int k = 0; k < bones.Count; k++) { if (bones[k].FrameId == boneFrame.Id) { mesh.HighlightBone(formMesh.Editor.Parser, i, j, e.IsSelected ? k : -1); Gui.Renderer.Render(); break; } } } } } } }
public static HashSet <int> SearchHierarchy(odfParser parser, HashSet <int> meshIDs) { HashSet <int> exportFrames = new HashSet <int>(); SearchHierarchy(parser.FrameSection.RootFrame, parser.FrameSection.RootFrame, meshIDs, exportFrames); if (parser.EnvelopeSection != null) { for (int meshIdx = 0; meshIdx < parser.MeshSection.Count; meshIdx++) { odfMesh mesh = parser.MeshSection[meshIdx]; if (!meshIDs.Contains((int)mesh.Id)) { continue; } for (int meshObjIdx = 0; meshObjIdx < mesh.Count; meshObjIdx++) { odfSubmesh meshObj = mesh[meshObjIdx]; for (int envIdx = 0; envIdx < parser.EnvelopeSection.Count; envIdx++) { odfBoneList boneList = parser.EnvelopeSection[envIdx]; if (meshObj.Id != boneList.SubmeshId) { continue; } for (int i = 0; i < boneList.Count; i++) { ObjectID boneID = boneList[i].FrameId; if (!exportFrames.Contains((int)boneID)) { odfFrame boneParent = FindFrame(boneID, parser.FrameSection.RootFrame); while (boneParent != null && exportFrames.Add((int)boneParent.Id)) { boneParent = boneParent.Parent as odfFrame; } } } } } } } return(exportFrames); }
private static List <odfTexture> Export(string dest, odfParser parser, List <odfMesh> meshes, bool worldCoords) { List <odfTexture> usedTextures = new List <odfTexture>(parser.TextureSection.Count); DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(dest)); if (!dir.Exists) { dir.Create(); } List <odfMaterial> materialList = new List <odfMaterial>(parser.MaterialSection.Count); Dictionary <ObjectName, ObjectName> matTexDic = new Dictionary <ObjectName, ObjectName>(); using (StreamWriter writer = new StreamWriter(dest, false)) { for (int i = 0; i < meshes.Count; i++) { odfMesh meshListSome = meshes[i]; for (int j = 0; j < meshListSome.Count; j++) { odfSubmesh meshObj = meshListSome[j]; odfMaterial mat = odf.FindMaterialInfo(meshObj.MaterialId, parser.MaterialSection); if (mat != null) { if (!materialList.Contains(mat)) { materialList.Add(mat); } odfTexture tex = odf.FindTextureInfo(meshObj.TextureIds[0], parser.TextureSection); if (tex != null && !usedTextures.Contains(tex)) { usedTextures.Add(tex); } if (tex != null && !matTexDic.ContainsKey(mat.Name)) { matTexDic.Add(mat.Name, tex.Name); } } else { Report.ReportLog("Warning: Mesh " + meshes[i].Name + " Object " + meshObj.Name + " has an invalid material"); } } } writer.WriteLine("Metasequoia Document"); writer.WriteLine("Format Text Ver 1.0"); writer.WriteLine(); writer.WriteLine("Material " + materialList.Count + " {"); foreach (odfMaterial mat in materialList) { string s = "\t\"" + mat.Name + "\" col(0.800 0.800 0.800 1.000) dif(0.500) amb(0.100) emi(0.500) spc(0.100) power(30.00)"; ObjectName matTexName; if (matTexDic.TryGetValue(mat.Name, out matTexName)) { s += " tex(\"" + matTexName + "\")"; } writer.WriteLine(s); } writer.WriteLine("}"); Random rand = new Random(); for (int i = 0; i < meshes.Count; i++) { Matrix transform = Matrix.Identity; if (worldCoords) { odfFrame parent = odf.FindMeshFrame(meshes[i].Id, parser.FrameSection.RootFrame); while (parent != null) { transform = parent.Matrix * transform; parent = parent.Parent as odfFrame; } } odfMesh meshListSome = meshes[i]; string meshName = meshes[i].Name; if (meshName == String.Empty) { meshName = meshes[i].Id.ToString(); } for (int j = 0; j < meshListSome.Count; j++) { odfSubmesh meshObj = meshListSome[j]; odfMaterial mat = odf.FindMaterialInfo(meshObj.MaterialId, parser.MaterialSection); int mqoMatIdx = -1; if (mat != null) { mqoMatIdx = materialList.IndexOf(mat); } float[] color = new float[3]; for (int k = 0; k < color.Length; k++) { color[k] = (float)((rand.NextDouble() / 2) + 0.5); } string mqoName = meshName + "[" + j + "]"; if (worldCoords) { mqoName += "[W]"; } string meshObjName = meshObj.Name; if (meshObjName != String.Empty) { mqoName += meshObjName; } writer.WriteLine("Object \"" + mqoName + "\" {"); writer.WriteLine("\tshading 1"); writer.WriteLine("\tcolor " + color[0].ToFloatString() + " " + color[1].ToFloatString() + " " + color[2].ToFloatString()); writer.WriteLine("\tcolor_type 1"); List <ImportedVertex> vertList = odf.ImportedVertexListUnskinned(meshObj.VertexList); List <ImportedFace> faceList = odf.ImportedFaceList(meshObj.FaceList); if (worldCoords) { for (int k = 0; k < vertList.Count; k++) { Vector4 v4 = Vector3.Transform(vertList[k].Position, transform); vertList[k].Position = new Vector3(v4.X, v4.Y, v4.Z); } } SB3Utility.Mqo.ExporterCommon.WriteMeshObject(writer, vertList, faceList, mqoMatIdx, null); writer.WriteLine("}"); } } writer.WriteLine("Eof"); } return(usedTextures); }
public static void CreateUnknowns(odfSubmesh submesh, int format) { UnknownDefaults.odfSubmesh(submesh, false); }
private AnimationFrame CreateFrame(odfFrame frame, odfParser parser, HashSet <int> extractFrames, HashSet <int> meshIDs, Device device, Matrix combinedParent, List <AnimationFrame> meshFrames) { AnimationFrame animationFrame = new AnimationFrame(); animationFrame.Name = frame.Name; animationFrame.TransformationMatrix = frame.Matrix; animationFrame.OriginalTransform = animationFrame.TransformationMatrix; animationFrame.CombinedTransform = combinedParent * animationFrame.TransformationMatrix; if ((int)frame.MeshId != 0 && meshIDs.Contains((int)frame.MeshId)) { odfMesh mesh = odf.FindMeshListSome(frame.MeshId, parser.MeshSection); ExtendedMaterial[] materials = new ExtendedMaterial[mesh.Count]; AnimationMeshContainer[] meshContainers = new AnimationMeshContainer[mesh.Count]; Vector3 min = new Vector3(Single.MaxValue); Vector3 max = new Vector3(Single.MinValue); for (int i = 0; i < mesh.Count; i++) { odfSubmesh submesh = mesh[i]; List <odfFace> faceList = submesh.FaceList; List <odfVertex> vertexList = submesh.VertexList; odfBoneList boneList = odf.FindBoneList(submesh.Id, parser.EnvelopeSection); bool skinned = boneList != null; int numBones = skinned ? boneList.Count : 0; string[] boneNames = new string[numBones]; Matrix[] boneOffsets = new Matrix[numBones]; for (int boneIdx = 0; boneIdx < numBones; boneIdx++) { odfBone bone = boneList[boneIdx]; boneNames[boneIdx] = odf.FindFrame(bone.FrameId, parser.FrameSection.RootFrame).Name; Matrix mirrored; if (!BoneMatrixDic.TryGetValue(boneNames[boneIdx], out mirrored)) { #if !DONT_MIRROR Vector3 translate, scale; Quaternion rotate; bone.Matrix.Decompose(out scale, out rotate, out translate); mirrored = Matrix.Scaling(scale.X, scale.Y, -scale.Z) * Matrix.RotationQuaternion(rotate) * Matrix.Translation(translate); #else mirrored = bone.Matrix; #endif BoneMatrixDic.Add(boneNames[boneIdx], mirrored); } boneOffsets[boneIdx] = mirrored; } Mesh animationMesh = new Mesh(device, faceList.Count, vertexList.Count, MeshFlags.Managed, PositionBlendWeightsIndexedNormalTexturedColoured.Format); using (DataStream indexStream = animationMesh.LockIndexBuffer(LockFlags.None)) { for (int j = 0; j < faceList.Count; j++) { ushort[] indices = faceList[j].VertexIndices; indexStream.Write(indices[0]); indexStream.Write(indices[1]); indexStream.Write(indices[2]); } animationMesh.UnlockIndexBuffer(); } float[][] vertexWeights = ConvertVertexWeights(vertexList, boneList); FillVertexBuffer(animationMesh, vertexList, vertexWeights, -1); var normalLines = new PositionBlendWeightsIndexedColored[vertexList.Count * 2]; for (int j = 0; j < vertexList.Count; j++) { odfVertex vertex = vertexList[j]; #if !DONT_MIRROR Vector3 position = new Vector3(vertex.Position.X, vertex.Position.Y, -vertex.Position.Z); Vector3 normal = new Vector3(vertex.Normal.X, vertex.Normal.Y, -vertex.Normal.Z); #else Vector3 position = vertex.Position; Vector3 normal = vertex.Normal; #endif float[] boneWeights = vertexWeights[j]; normalLines[j * 2] = new PositionBlendWeightsIndexedColored(position, boneWeights, vertex.BoneIndices, Color.Yellow.ToArgb()); normalLines[(j * 2) + 1] = new PositionBlendWeightsIndexedColored(position + (normal / 11), boneWeights, vertex.BoneIndices, Color.Blue.ToArgb()); min = Vector3.Minimize(min, position); max = Vector3.Maximize(max, position); } AnimationMeshContainer meshContainer = new AnimationMeshContainer(); meshContainer.Name = animationFrame.Name; meshContainer.MeshData = new MeshData(animationMesh); meshContainer.NormalLines = normalLines; meshContainer.BoneNames = boneNames; meshContainer.BoneOffsets = boneOffsets; meshContainers[i] = meshContainer; odfMaterial mat = odf.FindMaterialInfo(submesh.MaterialId, parser.MaterialSection); if (mat != null) { Material material3D = new Material(); material3D.Ambient = mat.Ambient; material3D.Diffuse = mat.Diffuse; material3D.Emissive = mat.Emissive; material3D.Specular = mat.Specular; material3D.Power = mat.SpecularPower; int matIdx = parser.MaterialSection.IndexOf(mat); Materials[matIdx] = material3D; meshContainer.MaterialIndex = matIdx; int texIdx = -1; if ((int)submesh.TextureIds[0] != 0 && !TextureDic.TryGetValue((int)submesh.TextureIds[0], out texIdx)) { odfTexture tex = odf.FindTextureInfo(submesh.TextureIds[0], parser.TextureSection); if (tex != null) { try { odfTextureFile texFile = new odfTextureFile(null, Path.GetDirectoryName(parser.ODFPath) + Path.DirectorySeparatorChar + tex.TextureFile); int fileSize = 0; ImportedTexture impTex = new ImportedTexture(texFile.DecryptFile(ref fileSize).BaseStream, tex.TextureFile); Texture memTex = impTex.ToTexture(device); texIdx = TextureDic.Count; TextureDic.Add((int)submesh.TextureIds[0], texIdx); Textures[texIdx] = memTex; } catch (SlimDXException ex) { Utility.ReportException(ex); Report.ReportLog("Please check " + tex.TextureFile + ". It may have an unsupported format."); } catch (Exception ex) { Utility.ReportException(ex); } } } meshContainer.TextureIndex = texIdx; } } for (int i = 0; i < (meshContainers.Length - 1); i++) { meshContainers[i].NextMeshContainer = meshContainers[i + 1]; } animationFrame.Bounds = new BoundingBox(min, max); animationFrame.MeshContainer = meshContainers[0]; meshFrames.Add(animationFrame); } for (int i = 0; i < frame.Count; i++) { odfFrame child = frame[i]; if (extractFrames.Contains((int)child.Id)) { AnimationFrame childAnimationFrame = CreateFrame(child, parser, extractFrames, meshIDs, device, animationFrame.CombinedTransform, meshFrames); childAnimationFrame.Parent = animationFrame; animationFrame.AppendChild(childAnimationFrame); } } numFrames++; return(animationFrame); }
public static odfBoneList CreateBoneList(ObjectID id, ObjectID meshFrameId, odfSubmesh submesh, List <ImportedBone> boneList, Matrix lMeshMatrixInv, odfFrame rootFrame) { if (boneList == null || boneList.Count == 0) { return(null); } Dictionary <byte, Tuple <byte, odfBone> > boneDic = new Dictionary <byte, Tuple <byte, odfBone> >(boneList.Count); Tuple <List <int>, List <float> >[] newBoneListComponents = new Tuple <List <int>, List <float> > [boneList.Count]; int boneframeNotFound = 0; for (int i = 0; i < submesh.NumVertices; i++) { odfVertex vert = submesh.VertexList[i]; for (int j = 0; j < vert.BoneIndices.Length; j++) { byte boneIdx = vert.BoneIndices[j]; if (boneIdx == 0xFF) { continue; } Tuple <byte, odfBone> boneDesc; odfBone newBone; if (!boneDic.TryGetValue(boneIdx, out boneDesc)) { odfFrame boneFrame = odf.FindFrame(boneList[boneIdx].Name, rootFrame); if (boneFrame == null) { boneframeNotFound++; continue; } newBone = new odfBone(boneFrame.Id); newBone.Matrix = boneList[boneIdx].Matrix; boneDesc = new Tuple <byte, odfBone>((byte)boneDic.Count, newBone); boneDic.Add(boneIdx, boneDesc); newBoneListComponents[boneDesc.Item1] = new Tuple <List <int>, List <float> >(new List <int>(200), new List <float>(200)); } else { newBone = boneDesc.Item2; } byte newBoneIdx = boneDesc.Item1; List <int> newBoneIdxList = newBoneListComponents[newBoneIdx].Item1; newBoneIdxList.Add(i); List <float> newBoneWeightList = newBoneListComponents[newBoneIdx].Item2; newBoneWeightList.Add(vert.Weights[j]); } } if (boneDic.Count == 0) { Report.ReportLog(submesh.ToString() + ": all bones dropped because of missing skeleton."); return(null); } odfBoneList newBoneList = new odfBoneList(new ObjectName(String.Empty, null), id, boneDic.Count); newBoneList.MeshFrameId = meshFrameId; newBoneList.SubmeshId = submesh.Id; newBoneList.AlwaysZero4 = new byte[4]; foreach (Tuple <byte, odfBone> boneDesc in boneDic.Values) { byte newBoneIdx = boneDesc.Item1; List <int> newBoneIdxList = newBoneListComponents[newBoneIdx].Item1; List <float> newBoneWeightList = newBoneListComponents[newBoneIdx].Item2; odfBone newBone = boneDesc.Item2; newBone.AlwaysZero24perIndex = new byte[24 * newBoneIdxList.Count]; newBone.VertexIndexArray = newBoneIdxList.ToArray(); newBone.WeightArray = newBoneWeightList.ToArray(); Matrix lMatrix = Matrix.Invert(newBone.Matrix); newBone.Matrix = Matrix.Invert(lMatrix * lMeshMatrixInv); newBoneList.AddChild(newBone); } if (boneframeNotFound > 0) { Report.ReportLog(submesh.ToString() + ": " + boneframeNotFound + " bone(s) because of missing boneframe(s) dropped."); } return(newBoneList); }
private void CollectObjectIDs(IObjInfo obj) { ObjectID id = null; if (obj is odfMaterial) { id = ((odfMaterial)obj).Id; } else if (obj is odfTexture) { id = ((odfTexture)obj).Id; } else if (obj is odfMesh) { odfMesh mesh = (odfMesh)obj; for (int i = 0; i < mesh.Count; i++) { odfSubmesh submesh = mesh[i]; CollectObjectIDs(submesh); } id = ((odfMesh)obj).Id; } else if (obj is odfSubmesh) { id = ((odfSubmesh)obj).Id; } else if (obj is odfFrame) { odfFrame frame = (odfFrame)obj; for (int i = 0; i < frame.Count; i++) { odfFrame childFrame = frame[i]; CollectObjectIDs(childFrame); } id = frame.Id; } else if (obj is odfMaterialSection) { odfMaterialSection matSec = (odfMaterialSection)obj; foreach (odfMaterial mat in matSec) { CollectObjectIDs(mat); } } else if (obj is odfTextureSection) { odfTextureSection texSec = (odfTextureSection)obj; foreach (odfTexture tex in texSec) { CollectObjectIDs(tex); } } else if (obj is odfMeshSection) { odfMeshSection meshSec = (odfMeshSection)obj; foreach (odfMesh mesh in meshSec) { CollectObjectIDs(mesh); } } else if (obj is odfFrameSection) { odfFrameSection frameSec = (odfFrameSection)obj; foreach (odfFrame frame in frameSec) { CollectObjectIDs(frame); } } else if (obj is odfEnvelopeSection) { odfEnvelopeSection envSec = (odfEnvelopeSection)obj; foreach (odfBoneList boneList in envSec.ChildList) { CollectObjectIDs(boneList); } id = envSec.Id; } else if (obj is odfBoneList) { odfBoneList boneList = (odfBoneList)obj; foreach (odfBone bone in boneList) { CollectObjectIDs(bone); } id = boneList.Id; } else if (obj is odfMorphSection) { id = ((odfMorphSection)obj).Id; } else if (obj is odfTXPTSection) { id = ((odfTXPTSection)obj).Id; } else if (obj is odfMATASection) { id = ((odfMATASection)obj).Id; } else if (obj is odfANIMSection) { id = ((odfANIMSection)obj).Id; } else if (obj is odfBANMSection) { id = ((odfBANMSection)obj).Id; } if (id != null) { int idVal = (int)id; if (idVal != 0) { try { this.UsedIDs.Add(idVal, obj.GetType()); } catch (ArgumentException argEx) { Type typeInDic; this.UsedIDs.TryGetValue(idVal, out typeInDic); Report.ReportLog(obj.GetType() + " ID: " + id + " - " + argEx.Message + " - " + typeInDic); } catch (Exception ex) { Report.ReportLog(obj.GetType() + " ID: " + id + " - " + ex.Message); } } else if (!(obj is odfBoneList)) { Report.ReportLog("Invalid ID used by " + obj.GetType().Name); } } }
private bool loadMESH(BinaryReader reader, odfFileSection fileSec) { odfMeshSection meshSection = new odfMeshSection(0); meshSection._FormatType = 10; if (!reader.BaseStream.CanSeek) { byte[] buffer = reader.ReadBytes(fileSec.Size); reader = new BinaryReader(new MemoryStream(buffer)); } for (int endPosition = (int)reader.BaseStream.Position + fileSec.Size; reader.BaseStream.Position < endPosition;) { ObjectName name = new ObjectName(reader.ReadBytes(64)); ObjectID id = new ObjectID(reader.ReadBytes(4)); int numSubmeshes = reader.ReadInt32(); odfMesh mesh = new odfMesh(name, id, numSubmeshes); for (int submeshIdx = 0; submeshIdx < numSubmeshes; submeshIdx++) { name = new ObjectName(reader.ReadBytes(64)); id = new ObjectID(reader.ReadBytes(4)); int unknown1 = reader.ReadInt32(); byte[] alwaysZero1 = reader.ReadBytes(4); ObjectID materialId = new ObjectID(reader.ReadBytes(4)); ObjectID[] texID = new ObjectID[4]; for (int texIdx = 0; texIdx < 4; texIdx++) { texID[texIdx] = new ObjectID(reader.ReadBytes(4)); } UInt32 unknown31 = reader.ReadUInt32(); byte[] alwaysZero2 = reader.ReadBytes(16); int unknown3 = reader.ReadInt32(); byte[] numVertsOrUnknown = reader.ReadBytes(4); byte[] numVertIndicesOrUnknown = reader.ReadBytes(4); int unknown4 = reader.ReadInt32(); int unknown5 = reader.ReadInt32(); byte[] unknownOrNumVerts = reader.ReadBytes(4); byte[] unknownOrNumVertIndices = reader.ReadBytes(4); if (meshSection.Count == 0) { int numVerts = BitConverter.ToInt32(numVertsOrUnknown, 0); int numVertexIdxs = BitConverter.ToInt32(numVertIndicesOrUnknown, 0); if (numVerts * numVertexIdxs == 0) { meshSection._FormatType = 9; } } odfSubmesh submesh = new odfSubmesh(name, id, meshSection._FormatType); submesh.Unknown1 = unknown1; submesh.AlwaysZero1 = alwaysZero1; submesh.MaterialId = materialId; submesh.TextureIds = texID; submesh.Unknown31 = unknown31; submesh.AlwaysZero2 = alwaysZero2; submesh.Unknown4 = unknown3; submesh.Unknown5 = unknown4; submesh.Unknown6 = unknown5; int numVertices, numVertexIndices; if (meshSection._FormatType < 10) { submesh.Unknown7 = BitConverter.ToInt32(numVertsOrUnknown, 0); submesh.Unknown8 = numVertIndicesOrUnknown; numVertices = BitConverter.ToInt32(unknownOrNumVerts, 0); numVertexIndices = BitConverter.ToInt32(unknownOrNumVertIndices, 0); submesh.AlwaysZero3 = reader.ReadBytes(448); } else { numVertices = BitConverter.ToInt32(numVertsOrUnknown, 0); numVertexIndices = BitConverter.ToInt32(numVertIndicesOrUnknown, 0); submesh.Unknown7 = BitConverter.ToInt32(unknownOrNumVerts, 0); submesh.Unknown8 = unknownOrNumVertIndices; } submesh.VertexList = ParseVertexList(reader, numVertices); submesh.FaceList = ParseFaceList(reader, numVertexIndices / 3); submesh.AlwaysZero4 = reader.ReadBytes(24); mesh.AddChild(submesh); } meshSection.AddChild(mesh); } fileSec.Section = meshSection; MeshSection = meshSection; return(true); }
public static void ReplaceMesh(odfFrame frame, odfParser parser, WorkspaceMesh mesh, List <ImportedMaterial> materials, List <ImportedTexture> textures, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod) { Matrix transform = Matrix.Identity; odfFrame transformFrame = frame; while (transformFrame != null) { transform *= transformFrame.Matrix; transformFrame = transformFrame.Parent as odfFrame; } transform.Invert(); string[] materialNames; int[] indices; bool[] worldCoords; bool[] replaceSubmeshesOption; odfMesh newMesh = CreateMesh(mesh, parser.MeshSection._FormatType, out materialNames, out indices, out worldCoords, out replaceSubmeshesOption); odfMesh frameMesh = odf.FindMeshListSome(frame.MeshId, parser.MeshSection); if (frameMesh != null) { if (parser.UsedIDs == null) // prevent misleading error message { parser.CollectObjectIDs(); } newMesh.Id = frameMesh.Id; newMesh.Name = frameMesh.Name; parser.MeshSection.InsertChild(parser.MeshSection.IndexOf(frameMesh), newMesh); } else { newMesh.Id = parser.GetNewID(typeof(odfMesh)); frame.MeshId = newMesh.Id; parser.MeshSection.AddChild(newMesh); } Dictionary <ObjectID, ObjectID> submeshIDtranslation = new Dictionary <ObjectID, ObjectID>(newMesh.Count); odfSubmesh[] replaceSubmeshes = frameMesh != null ? new odfSubmesh[frameMesh.Count] : null; List <odfSubmesh> addSubmeshes = new List <odfSubmesh>(newMesh.Count); for (int i = 0; i < newMesh.Count; i++) { ObjectID[] texIDs = new ObjectID[4] { ObjectID.INVALID, ObjectID.INVALID, ObjectID.INVALID, ObjectID.INVALID }; odfMaterial mat = odf.FindMaterialInfo(materialNames[i], parser.MaterialSection); if (materials != null && mat == null) { ImportedMaterial impMat = ImportedHelpers.FindMaterial(materialNames[i], materials); if (impMat != null) { mat = CreateMaterial(impMat, parser.GetNewID(typeof(odfMaterial))); parser.MaterialSection.AddChild(mat); for (int j = 0; j < impMat.Textures.Length; j++) { string texName = impMat.Textures[j]; odfTexture tex = odf.FindTextureInfo(texName, parser.TextureSection); if (tex == null) { ImportedTexture impTex = ImportedHelpers.FindTexture(texName, textures); if (impTex != null) { tex = CreateTexture(impTex, parser.GetNewID(typeof(odfTexture)), parser.TextureSection._FormatType, Path.GetDirectoryName(parser.ODFPath)); parser.TextureSection.AddChild(tex); texIDs[j] = tex.Id; } } else { texIDs[j] = tex.Id; } } } } odfSubmesh newSubmesh = newMesh[i]; newSubmesh.Id = parser.GetNewID(typeof(odfSubmesh)); newSubmesh.MaterialId = mat != null ? mat.Id : ObjectID.INVALID; newSubmesh.TextureIds = texIDs; List <odfVertex> newVertexList = newSubmesh.VertexList; if (worldCoords[i]) { for (int j = 0; j < newVertexList.Count; j++) { newVertexList[j].Position = Vector3.TransformCoordinate(newVertexList[j].Position, transform); } } odfSubmesh baseSubmesh = null; odfBoneList newBones = null; int newBonesIdx = -1; int idx = indices[i]; if ((frameMesh != null) && (idx >= 0) && (idx < frameMesh.Count)) { baseSubmesh = frameMesh[idx]; submeshIDtranslation.Add(newSubmesh.Id, baseSubmesh.Id); for (int j = 0; j < baseSubmesh.TextureIds.Length; j++) { ObjectID texID = baseSubmesh.TextureIds[j]; newSubmesh.TextureIds[j] = texID; } newSubmesh.Name = new ObjectName(baseSubmesh.Name.Name, baseSubmesh.Name.Info); CopyUnknowns(baseSubmesh, newSubmesh, parser.MeshSection._FormatType); if ((bonesMethod == CopyMeshMethod.CopyOrder) || (bonesMethod == CopyMeshMethod.CopyNear)) { odfBoneList baseBones = odf.FindBoneList(baseSubmesh.Id, parser.EnvelopeSection); if (baseBones != null) { newBones = baseBones.Clone(); newBones.Id = ObjectID.INVALID; // parser.GetNewID(typeof(odfBoneList)); newBones.SubmeshId = newSubmesh.Id; newBonesIdx = parser.EnvelopeSection.IndexOf(baseBones); } } else if (bonesMethod == CopyMeshMethod.Replace) { newBones = CreateBoneList(ObjectID.INVALID /*parser.GetNewID(typeof(odfBoneList))*/, frame.Id, newSubmesh, mesh.BoneList, transform, parser.FrameSection.RootFrame); newBonesIdx = parser.EnvelopeSection.Count; } } else { CreateUnknowns(newSubmesh, parser.MeshSection._FormatType); newBones = CreateBoneList(ObjectID.INVALID /*parser.GetNewID(typeof(odfBoneList))*/, frame.Id, newSubmesh, mesh.BoneList, transform, parser.FrameSection.RootFrame); newBonesIdx = parser.EnvelopeSection.Count; } if (newBones != null) { parser.EnvelopeSection.InsertChild(newBonesIdx, newBones); } if (baseSubmesh != null) { if (normalsMethod == CopyMeshMethod.CopyOrder) { odf.CopyNormalsOrder(baseSubmesh.VertexList, newSubmesh.VertexList); } else if (normalsMethod == CopyMeshMethod.CopyNear) { odf.CopyNormalsNear(baseSubmesh.VertexList, newSubmesh.VertexList); } if (bonesMethod == CopyMeshMethod.CopyOrder) { odf.CopyBonesOrder(baseSubmesh.VertexList, newSubmesh.VertexList, newBones); } else if (bonesMethod == CopyMeshMethod.CopyNear) { odf.CopyBonesNear(baseSubmesh.VertexList, newSubmesh.VertexList, newBones); } } if ((baseSubmesh != null) && merge && replaceSubmeshesOption[i]) { replaceSubmeshes[idx] = newSubmesh; } else { addSubmeshes.Add(newSubmesh); } } if ((frameMesh != null) && merge) { newMesh.Clear(); newMesh.Capacity = replaceSubmeshes.Length + addSubmeshes.Count; for (int i = 0, submeshesRemoved = 0; i < replaceSubmeshes.Length; i++) { if (replaceSubmeshes[i] == null) { odfSubmesh newSubmesh = frameMesh[i - submeshesRemoved++]; frameMesh.RemoveChild(newSubmesh); // save the bone list from being deleted in RemoveMesh newMesh.AddChild(newSubmesh); } else { newMesh.AddChild(replaceSubmeshes[i]); } } newMesh.AddRange(addSubmeshes); } if (frameMesh != null) { RemoveMesh(parser, frameMesh, frame, false); parser.UsedIDs.Add((int)newMesh.Id, typeof(odfMesh)); frame.MeshId = newMesh.Id; List <ObjectID> removeKeyList = new List <ObjectID>(); foreach (odfSubmesh submesh in newMesh) { ObjectID newSubmeshID = submesh.Id; ObjectID baseSubmeshID; if (submeshIDtranslation.TryGetValue(newSubmeshID, out baseSubmeshID)) { if (odf.FindBoneList(baseSubmeshID, parser.EnvelopeSection) == null) { odfBoneList boneList = odf.FindBoneList(newSubmeshID, parser.EnvelopeSection); if (boneList != null) { boneList.SubmeshId = baseSubmeshID; } submesh.Id = baseSubmeshID; parser.UsedIDs.Remove((int)newSubmeshID); } foreach (KeyValuePair <ObjectID, ObjectID> pair in submeshIDtranslation) { if (pair.Value == baseSubmeshID) { removeKeyList.Add(pair.Key); } } foreach (ObjectID removeId in removeKeyList) { submeshIDtranslation.Remove(removeId); } removeKeyList.Clear(); } } } }
public static odfMesh CreateMesh(WorkspaceMesh mesh, int subMeshFormat, out string[] materialNames, out int[] indices, out bool[] worldCoords, out bool[] replaceSubmeshesOption) { int numUncheckedSubmeshes = 0; foreach (ImportedSubmesh submesh in mesh.SubmeshList) { if (!mesh.isSubmeshEnabled(submesh)) { numUncheckedSubmeshes++; } } int numSubmeshes = mesh.SubmeshList.Count - numUncheckedSubmeshes; materialNames = new string[numSubmeshes]; indices = new int[numSubmeshes]; worldCoords = new bool[numSubmeshes]; replaceSubmeshesOption = new bool[numSubmeshes]; odfMesh newMesh = new odfMesh(new ObjectName(String.Empty, null), null, numSubmeshes); for (int i = 0, submeshIdx = 0; i < numSubmeshes; i++, submeshIdx++) { while (!mesh.isSubmeshEnabled(mesh.SubmeshList[submeshIdx])) { submeshIdx++; } ImportedSubmesh submesh = mesh.SubmeshList[submeshIdx]; odfSubmesh newSubmesh = new odfSubmesh(new ObjectName(String.Empty, null), null, subMeshFormat); newMesh.AddChild(newSubmesh); newSubmesh.MaterialId = ObjectID.INVALID; materialNames[i] = submesh.Material; indices[i] = submesh.Index; worldCoords[i] = submesh.WorldCoords; replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(mesh.SubmeshList[submeshIdx]); List <ImportedVertex> vertexList = submesh.VertexList; List <odfVertex> newVertexList = new List <odfVertex>(vertexList.Count); for (int j = 0; j < vertexList.Count; j++) { ImportedVertex vert = vertexList[j]; odfVertex newVertex = new odfVertex(); newVertex.Normal = vert.Normal; newVertex.UV = new Vector2(vert.UV[0], vert.UV[1]); newVertex.Weights = (float[])vert.Weights.Clone(); newVertex.BoneIndices = (byte[])vert.BoneIndices.Clone(); newVertex.Position = vert.Position; newVertexList.Add(newVertex); } newSubmesh.VertexList = newVertexList; List <ImportedFace> faceList = submesh.FaceList; List <odfFace> newFaceList = new List <odfFace>(faceList.Count); for (int j = 0; j < faceList.Count; j++) { int[] vertexIndices = faceList[j].VertexIndices; odfFace newFace = new odfFace(); newFace.VertexIndices = new ushort[3] { (ushort)vertexIndices[0], (ushort)vertexIndices[1], (ushort)vertexIndices[2] }; newFaceList.Add(newFace); } newSubmesh.FaceList = newFaceList; } return(newMesh); }
private void Export(DirectoryInfo dir) { try { odfMorphSection morphSection = parser.MorphSection; ushort[] meshIndices = morphObj.MeshIndices; odfSubmesh meshObjBase = odf.FindMeshObject(morphObj.SubmeshId, parser.MeshSection); colorVertex = new bool[meshObjBase.VertexList.Count]; for (int i = 0; i < meshIndices.Length; i++) { colorVertex[meshIndices[i]] = true; } vertLists = new List <List <ImportedVertex> >(morphObj.Count); for (int i = 0; i < morphObj.Count; i++) { if (skipUnusedProfiles) { bool skip = true; for (int j = 0; j < morphObj.SelectorList.Count; j++) { if (morphObj.SelectorList[j].ProfileIndex == i) { skip = false; break; } } if (skip) { continue; } } List <ImportedVertex> vertList = odf.ImportedVertexListUnskinned(meshObjBase.VertexList); vertLists.Add(vertList); for (int j = 0; j < meshIndices.Length; j++) { ImportedVertex vert = vertList[meshIndices[j]]; vert.Position = morphObj[i].VertexList[j].Position; } } faceList = odf.ImportedFaceList(meshObjBase.FaceList); string dest = Utility.GetDestFile(dir, meshObjBase.Parent.Name + "-" + morphObj.Name + "-", ".morph.mqo"); odfMaterial mat = odf.FindMaterialInfo(meshObjBase.MaterialId, parser.MaterialSection); Export(dest, mat, odf.FindTextureInfo(meshObjBase.TextureIds[0], parser.TextureSection)); foreach (odfTexture tex in usedTextures) { String texFilePath = Path.GetDirectoryName(parser.ODFPath) + @"\" + tex.TextureFile; try { odfTextureFile odfTex = new odfTextureFile(tex.Name, texFilePath); odf.ExportTexture(odfTex, dir.FullName + @"\" + tex.TextureFile); } catch (Exception ex) { Utility.ReportException(ex); } } Report.ReportLog("Finished exporting morph to " + dest); } catch (Exception ex) { Report.ReportLog("Error exporting morph: " + ex.Message); } }