private void Export(DirectoryInfo dir, xxFrame meshFrame) { try { xaMorphSection morphSection = xaParser.MorphSection; xaMorphIndexSet indexSet = xa.FindMorphIndexSet(clip.Name, morphSection); ushort[] meshIndices = indexSet.MeshIndices; ushort[] morphIndices = indexSet.MorphIndices; xxMesh meshList = meshFrame.Mesh; int meshObjIdx = xa.MorphMeshObjIdx(meshIndices, meshList); if (meshObjIdx < 0) { throw new Exception("no valid mesh object was found for the morph"); } xxSubmesh meshObjBase = meshList.SubmeshList[meshObjIdx]; colorVertex = new bool[meshObjBase.VertexList.Count]; for (int i = 0; i < meshIndices.Length; i++) { colorVertex[meshIndices[i]] = true; } string dest = Utility.GetDestFile(dir, meshFrame.Name + "-" + clip.Name + "-", ".morph.mqo"); List <xaMorphKeyframeRef> refList = clip.KeyframeRefList; morphNames = new List <string>(refList.Count); vertLists = new List <List <ImportedVertex> >(refList.Count); for (int i = 0; i < refList.Count; i++) { if (!morphNames.Contains(refList[i].Name)) { List <ImportedVertex> vertList = xx.ImportedVertexList(meshObjBase.VertexList, xx.IsSkinned(meshList)); vertLists.Add(vertList); xaMorphKeyframe keyframe = xa.FindMorphKeyFrame(refList[i].Name, morphSection); for (int j = 0; j < meshIndices.Length; j++) { ImportedVertex vert = vertList[meshIndices[j]]; vert.Position = keyframe.PositionList[morphIndices[j]]; } morphNames.Add(keyframe.Name); } } faceList = xx.ImportedFaceList(meshObjBase.FaceList); Export(dest, meshObjBase.MaterialIndex); foreach (xxTexture tex in usedTextures) { xx.ExportTexture(tex, dir.FullName + @"\" + Path.GetFileName(tex.Name)); } Report.ReportLog("Finished exporting morph to " + dest); } catch (Exception ex) { Report.ReportLog("Error exporting morph: " + ex.Message); } }
public static xaMorphKeyframe FindMorphKeyFrame(string name, xaMorphSection section) { for (int i = 0; i < section.KeyframeList.Count; i++) { if (section.KeyframeList[i].Name == name) { return(section.KeyframeList[i]); } } return(null); }
public static xaMorphIndexSet FindMorphIndexSet(string name, xaMorphSection section) { for (int i = 0; i < section.IndexSetList.Count; i++) { if (section.IndexSetList[i].Name == name) { return(section.IndexSetList[i]); } } return(null); }
protected xaMorphSection ParseMorphSection() { if (reader.ReadByte() == 0) { return(null); } xaMorphSection section = new xaMorphSection(); int numIndexSets = reader.ReadInt32(); section.IndexSetList = new List <xaMorphIndexSet>(numIndexSets); for (int i = 0; i < numIndexSets; i++) { xaMorphIndexSet indexSet = new xaMorphIndexSet(); section.IndexSetList.Add(indexSet); indexSet.Unknown1 = reader.ReadBytes(1); int numVertices = reader.ReadInt32(); indexSet.MeshIndices = reader.ReadUInt16Array(numVertices); indexSet.MorphIndices = reader.ReadUInt16Array(numVertices); indexSet.Name = reader.ReadName(); } int numKeyframes = reader.ReadInt32(); section.KeyframeList = new List <xaMorphKeyframe>(numKeyframes); for (int i = 0; i < numKeyframes; i++) { xaMorphKeyframe keyframe = new xaMorphKeyframe(); section.KeyframeList.Add(keyframe); int numVertices = reader.ReadInt32(); keyframe.PositionList = new List <Vector3>(numVertices); keyframe.NormalList = new List <Vector3>(numVertices); for (int j = 0; j < numVertices; j++) { keyframe.PositionList.Add(reader.ReadVector3()); } for (int j = 0; j < numVertices; j++) { keyframe.NormalList.Add(reader.ReadVector3()); } keyframe.Name = reader.ReadName(); } int numClips = reader.ReadInt32(); section.ClipList = new List <xaMorphClip>(numClips); for (int i = 0; i < numClips; i++) { xaMorphClip clip = new xaMorphClip(); section.ClipList.Add(clip); clip.MeshName = reader.ReadName(); clip.Name = reader.ReadName(); int numKeyframeRefs = reader.ReadInt32(); clip.KeyframeRefList = new List <xaMorphKeyframeRef>(numKeyframeRefs); for (int j = 0; j < numKeyframeRefs; j++) { xaMorphKeyframeRef keyframeRef = new xaMorphKeyframeRef(); clip.KeyframeRefList.Add(keyframeRef); keyframeRef.Unknown1 = reader.ReadBytes(1); keyframeRef.Index = reader.ReadInt32(); keyframeRef.Unknown2 = reader.ReadBytes(1); keyframeRef.Name = reader.ReadName(); } clip.Unknown1 = reader.ReadBytes(4); } return(section); }
public static void ReplaceMorph(string destMorphName, xaParser parser, WorkspaceMorph wsMorphList, string newMorphName, bool replaceMorphMask, bool replaceNormals, float minSquaredDistance, bool minKeyframes) { if (parser.MorphSection == null) { Report.ReportLog("The .xa file doesn't have a morph section. Skipping these morphs"); return; } xaMorphSection morphSection = parser.MorphSection; xaMorphIndexSet indices = FindMorphIndexSet(destMorphName, morphSection); if (indices == null) { Report.ReportLog("Couldn't find morph clip " + destMorphName + ". Skipping these morphs"); return; } if (replaceMorphMask && wsMorphList.MorphedVertexIndices != null) { int index = morphSection.IndexSetList.IndexOf(indices); xaMorphIndexSet newIndices = new xaMorphIndexSet(); newIndices.Name = indices.Name; int numMorphedVertices = wsMorphList.MorphedVertexIndices.Count; newIndices.MeshIndices = new ushort[numMorphedVertices]; wsMorphList.MorphedVertexIndices.CopyTo(newIndices.MeshIndices); newIndices.MorphIndices = new ushort[numMorphedVertices]; if (minKeyframes) { for (ushort i = 0; i < numMorphedVertices; i++) { newIndices.MorphIndices[i] = i; } } else { wsMorphList.MorphedVertexIndices.CopyTo(newIndices.MorphIndices); } newIndices.Unknown1 = indices.Unknown1; morphSection.IndexSetList.RemoveAt(index); morphSection.IndexSetList.Insert(index, newIndices); indices = newIndices; } Report.ReportLog("Replacing morphs ..."); try { ushort[] meshIndices = indices.MeshIndices; ushort[] morphIndices = indices.MorphIndices; foreach (ImportedMorphKeyframe wsMorph in wsMorphList.KeyframeList) { if (!wsMorphList.isMorphKeyframeEnabled(wsMorph)) { continue; } List <ImportedVertex> vertList = wsMorph.VertexList; xaMorphKeyframe keyframe = FindMorphKeyFrame(wsMorph.Name, morphSection); if (keyframe == null) { Report.ReportLog("Adding new Keyframe " + wsMorph.Name); keyframe = new xaMorphKeyframe(); keyframe.Name = wsMorph.Name; int numVertices = minKeyframes ? meshIndices.Length : wsMorph.VertexList.Count; keyframe.PositionList = new List <Vector3>(new Vector3[numVertices]); keyframe.NormalList = new List <Vector3>(new Vector3[numVertices]); for (int i = 0; i < meshIndices.Length; i++) { keyframe.PositionList[morphIndices[i]] = vertList[meshIndices[i]].Position; keyframe.NormalList[morphIndices[i]] = vertList[meshIndices[i]].Normal; } morphSection.KeyframeList.Add(keyframe); } else { if (!minKeyframes && keyframe.PositionList.Count != vertList.Count || minKeyframes && keyframe.PositionList.Count != meshIndices.Length) { Report.ReportLog("Adapting Keyframe " + wsMorph.Name + " to new length."); int length = minKeyframes ? meshIndices.Length : vertList.Count; Vector3[] newPositions = new Vector3[length]; Vector3[] newNormals = new Vector3[length]; if (!minKeyframes) { for (int i = 0; i < vertList.Count; i++) { newPositions[i] = vertList[i].Position; newNormals[i] = vertList[i].Normal; } } for (int i = 0; i < meshIndices.Length; i++) { newPositions[morphIndices[i]] = vertList[meshIndices[i]].Position; newNormals[morphIndices[i]] = vertList[meshIndices[i]].Normal; } keyframe.PositionList.Clear(); keyframe.NormalList.Clear(); keyframe.PositionList.AddRange(newPositions); keyframe.NormalList.AddRange(newNormals); } else { Report.ReportLog("Replacing Keyframe " + wsMorph.Name); for (int i = 0; i < meshIndices.Length; i++) { Vector3 orgPos = new Vector3(keyframe.PositionList[morphIndices[i]].X, keyframe.PositionList[morphIndices[i]].Y, keyframe.PositionList[morphIndices[i]].Z), newPos = new Vector3(vertList[meshIndices[i]].Position.X, vertList[meshIndices[i]].Position.Y, vertList[meshIndices[i]].Position.Z); if ((orgPos - newPos).LengthSquared() >= minSquaredDistance) { keyframe.PositionList[morphIndices[i]] = vertList[meshIndices[i]].Position; if (replaceNormals) { keyframe.NormalList[morphIndices[i]] = vertList[meshIndices[i]].Normal; } } } } } string morphNewName = wsMorphList.getMorphKeyframeNewName(wsMorph); if (morphNewName != String.Empty) { for (int i = 0; i < morphSection.ClipList.Count; i++) { xaMorphClip clip = morphSection.ClipList[i]; for (int j = 0; j < clip.KeyframeRefList.Count; j++) { xaMorphKeyframeRef keyframeRef = clip.KeyframeRefList[j]; if (keyframeRef.Name == wsMorph.Name) { keyframeRef.Name = morphNewName; } } } keyframe.Name = morphNewName; } } if (newMorphName != String.Empty) { for (int i = 0; i < morphSection.ClipList.Count; i++) { xaMorphClip clip = morphSection.ClipList[i]; if (clip.Name == destMorphName) { clip.Name = newMorphName; break; } } indices.Name = newMorphName; } } catch (Exception ex) { Report.ReportLog("Error replacing morphs: " + ex.Message); } }
public static void ReplaceMorph(string destMorphName, xaParser parser, WorkspaceMorph wsMorphList, string newMorphName, bool replaceNormals, float minSquaredDistance) { if (parser.MorphSection == null) { Report.ReportLog("The .xa file doesn't have a morph section. Skipping these morphs"); return; } xaMorphSection morphSection = parser.MorphSection; xaMorphIndexSet indices = FindMorphIndexSet(destMorphName, morphSection); if (indices == null) { Report.ReportLog("Couldn't find morph clip " + destMorphName + ". Skipping these morphs"); return; } Report.ReportLog("Replacing morphs ..."); try { ushort[] meshIndices = indices.MeshIndices; ushort[] morphIndices = indices.MorphIndices; foreach (ImportedMorphKeyframe wsMorph in wsMorphList.KeyframeList) { if (!wsMorphList.isMorphKeyframeEnabled(wsMorph)) { continue; } List <ImportedVertex> vertList = wsMorph.VertexList; xaMorphKeyframe keyframe = FindMorphKeyFrame(wsMorph.Name, morphSection); if (keyframe == null) { keyframe = new xaMorphKeyframe(); keyframe.Name = wsMorph.Name; keyframe.PositionList = new List <Vector3>(new Vector3[wsMorph.VertexList.Count]); keyframe.NormalList = new List <Vector3>(new Vector3[wsMorph.VertexList.Count]); for (int i = 0; i < meshIndices.Length; i++) { keyframe.PositionList[morphIndices[i]] = vertList[meshIndices[i]].Position; keyframe.NormalList[morphIndices[i]] = vertList[meshIndices[i]].Normal; } morphSection.KeyframeList.Add(keyframe); } else { for (int i = 0; i < meshIndices.Length; i++) { Vector3 orgPos = new Vector3(keyframe.PositionList[morphIndices[i]].X, keyframe.PositionList[morphIndices[i]].Y, keyframe.PositionList[morphIndices[i]].Z), newPos = new Vector3(vertList[meshIndices[i]].Position.X, vertList[meshIndices[i]].Position.Y, vertList[meshIndices[i]].Position.Z); if ((orgPos - newPos).LengthSquared() >= minSquaredDistance) { keyframe.PositionList[morphIndices[i]] = vertList[meshIndices[i]].Position; if (replaceNormals) { keyframe.NormalList[morphIndices[i]] = vertList[meshIndices[i]].Normal; } } } } string morphNewName = wsMorphList.getMorphKeyframeNewName(wsMorph); if (morphNewName != String.Empty) { for (int i = 0; i < morphSection.ClipList.Count; i++) { xaMorphClip clip = morphSection.ClipList[i]; for (int j = 0; j < clip.KeyframeRefList.Count; j++) { xaMorphKeyframeRef keyframeRef = clip.KeyframeRefList[j]; if (keyframeRef.Name == wsMorph.Name) { keyframeRef.Name = morphNewName; } } } keyframe.Name = morphNewName; } } if (newMorphName != String.Empty) { for (int i = 0; i < morphSection.ClipList.Count; i++) { xaMorphClip clip = morphSection.ClipList[i]; if (clip.Name == destMorphName) { clip.Name = newMorphName; break; } } indices.Name = newMorphName; } } catch (Exception ex) { Report.ReportLog("Error replacing morphs: " + ex.Message); } }