public static void SaveXA(xaParser parser, string destPath, bool keepBackup) { DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(destPath)); string backup = null; if (keepBackup && File.Exists(destPath)) { backup = Utility.GetDestFile(dir, Path.GetFileNameWithoutExtension(destPath) + ".bak", Path.GetExtension(destPath)); File.Move(destPath, backup); } try { using (BufferedStream bufStr = new BufferedStream(File.OpenWrite(destPath))) { parser.WriteTo(bufStr); } } catch { if (File.Exists(backup)) { if (File.Exists(destPath)) { File.Delete(destPath); } File.Move(backup, destPath); } } }
public static xaAnimationTrack FindTrack(string name, xaParser parser) { foreach (xaAnimationTrack track in parser.AnimationSection.TrackList) { if (track.Name == name) { return(track); } } return(null); }
public static void CalculateNormals(xaParser parser, xxFrame meshFrame, string morphClip, string keyframe, float threshold) { HashSet <Tuple <xaMorphClip, xaMorphKeyframe> > keyframes = new HashSet <Tuple <xaMorphClip, xaMorphKeyframe> >(); foreach (xaMorphClip clip in parser.MorphSection.ClipList) { if (morphClip != null && clip.Name != morphClip) { continue; } if (keyframe != null) { xaMorphKeyframe xaKeyframe = FindMorphKeyFrame(keyframe, parser.MorphSection); if (xaKeyframe == null) { throw new Exception("keyframe " + keyframe + " not found in morph clip " + morphClip); } keyframes.Add(new Tuple <xaMorphClip, xaMorphKeyframe>(clip, xaKeyframe)); break; } else { foreach (xaMorphKeyframeRef morphRef in clip.KeyframeRefList) { xaMorphKeyframe xaKeyframe = FindMorphKeyFrame(morphRef.Name, parser.MorphSection); keyframes.Add(new Tuple <xaMorphClip, xaMorphKeyframe>(clip, xaKeyframe)); } } } if (keyframes.Count == 0) { Report.ReportLog("No keyframe for mesh " + meshFrame.Name + " to calculate normals for found."); return; } foreach (var tup in keyframes) { xaMorphIndexSet set = FindMorphIndexSet(tup.Item1.Name, parser.MorphSection); CalculateNormals(parser, meshFrame, tup.Item2, set, threshold); } }
public static void CalculateNormals(xaParser parser, xxFrame meshFrame, string morphClip, string keyframe, float threshold) { HashSet<Tuple<xaMorphClip, xaMorphKeyframe>> keyframes = new HashSet<Tuple<xaMorphClip, xaMorphKeyframe>>(); foreach (xaMorphClip clip in parser.MorphSection.ClipList) { if (morphClip != null && clip.Name != morphClip) continue; if (keyframe != null) { xaMorphKeyframe xaKeyframe = FindMorphKeyFrame(keyframe, parser.MorphSection); if (xaKeyframe == null) { throw new Exception("keyframe " + keyframe + " not found in morph clip " + morphClip); } keyframes.Add(new Tuple<xaMorphClip, xaMorphKeyframe>(clip, xaKeyframe)); break; } else { foreach (xaMorphKeyframeRef morphRef in clip.KeyframeRefList) { xaMorphKeyframe xaKeyframe = FindMorphKeyFrame(morphRef.Name, parser.MorphSection); keyframes.Add(new Tuple<xaMorphClip, xaMorphKeyframe>(clip, xaKeyframe)); } } } if (keyframes.Count == 0) { Report.ReportLog("No keyframe for mesh " + meshFrame.Name + " to calculate normals for found."); return; } foreach (var tup in keyframes) { xaMorphIndexSet set = FindMorphIndexSet(tup.Item1.Name, parser.MorphSection); CalculateNormals(parser, meshFrame, tup.Item2, set, threshold); } }
private static void CalculateNormals(xaParser parser, xxFrame meshFrame, xaMorphKeyframe keyframe, xaMorphIndexSet set, float threshold) { xxMesh mesh = meshFrame.Mesh; ushort[] meshIndices = set.MeshIndices; ushort[] morphIndices = set.MorphIndices; int morphSubmeshIdx = MorphMeshObjIdx(meshIndices, mesh); if (morphSubmeshIdx < 0) { throw new Exception("no valid mesh object was found for the morph " + set.Name); } xxSubmesh submesh = mesh.SubmeshList[morphSubmeshIdx]; List <xxVertex> morphedVertices = new List <xxVertex>(submesh.VertexList.Count); for (ushort i = 0; i < submesh.VertexList.Count; i++) { xxVertex vert = new xxVertexUShort(); vert.Index = i; vert.Position = submesh.VertexList[i].Position; vert.Normal = submesh.VertexList[i].Normal; morphedVertices.Add(vert); } for (int i = 0; i < meshIndices.Length; i++) { morphedVertices[meshIndices[i]].Position = keyframe.PositionList[morphIndices[i]]; } var pairList = new List <Tuple <List <xxFace>, List <xxVertex> > >(1); pairList.Add(new Tuple <List <xxFace>, List <xxVertex> >(submesh.FaceList, morphedVertices)); xx.CalculateNormals(pairList, threshold); for (int i = 0; i < meshIndices.Length; i++) { keyframe.NormalList[morphIndices[i]] = morphedVertices[meshIndices[i]].Normal; } }
public xaEditor(xaParser parser) { Parser = parser; }
private static void CalculateNormals(xaParser parser, xxFrame meshFrame, xaMorphKeyframe keyframe, xaMorphIndexSet set, float threshold) { xxMesh mesh = meshFrame.Mesh; ushort[] meshIndices = set.MeshIndices; ushort[] morphIndices = set.MorphIndices; int morphSubmeshIdx = MorphMeshObjIdx(meshIndices, mesh); if (morphSubmeshIdx < 0) { throw new Exception("no valid mesh object was found for the morph " + set.Name); } xxSubmesh submesh = mesh.SubmeshList[morphSubmeshIdx]; List<xxVertex> morphedVertices = new List<xxVertex>(submesh.VertexList.Count); for (ushort i = 0; i < submesh.VertexList.Count; i++) { xxVertex vert = new xxVertexUShort(); vert.Index = i; vert.Position = submesh.VertexList[i].Position; vert.Normal = submesh.VertexList[i].Normal; morphedVertices.Add(vert); } for (int i = 0; i < meshIndices.Length; i++) { morphedVertices[meshIndices[i]].Position = keyframe.PositionList[morphIndices[i]]; } var pairList = new List<Tuple<List<xxFace>, List<xxVertex>>>(1); pairList.Add(new Tuple<List<xxFace>, List<xxVertex>>(submesh.FaceList, morphedVertices)); xx.CalculateNormals(pairList, threshold); for (int i = 0; i < meshIndices.Length; i++) { keyframe.NormalList[morphIndices[i]] = morphedVertices[meshIndices[i]].Normal; } }
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); } }
public static void ExportMorphFbx([DefaultVar] xxParser xxparser, string path, xxFrame meshFrame, xaParser xaparser, xaMorphClip morphClip, string exportFormat, bool oneBlendShape) { Fbx.Exporter.ExportMorph(path, xxparser, meshFrame, morphClip, xaparser, exportFormat, oneBlendShape); }
public static void ExportMorphFbx([DefaultVar]xxParser xxparser, string path, xxFrame meshFrame, xaParser xaparser, xaMorphClip morphClip, string exportFormat, bool oneBlendShape) { Fbx.Exporter.ExportMorph(path, xxparser, meshFrame, morphClip, xaparser, exportFormat, oneBlendShape); }
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 Export(string dirPath, xxParser xxParser, xxFrame meshFrame, xaParser xaParser, xaMorphClip clip) { DirectoryInfo dir = new DirectoryInfo(dirPath); ExporterMorph exporter = new ExporterMorph(dir, xxParser, xaParser, clip); exporter.Export(dir, meshFrame); }
public static void ReplaceAnimation(WorkspaceAnimation wsAnimation, xaParser parser, int resampleCount, ReplaceAnimationMethod replaceMethod, int insertPos) { if (parser.AnimationSection == null) { Report.ReportLog("The .xa file doesn't have an animation section. Skipping this animation"); return; } if (!(wsAnimation.importedAnimation is ImportedKeyframedAnimation)) { Report.ReportLog("The animation has incompatible keyframes."); return; } Report.ReportLog("Replacing animation ..."); List<ImportedAnimationKeyframedTrack> trackList = ((ImportedKeyframedAnimation)wsAnimation.importedAnimation).TrackList; List<KeyValuePair<string, xaAnimationKeyframe[]>> newTrackList = new List<KeyValuePair<string, xaAnimationKeyframe[]>>(trackList.Count); List<Tuple<ImportedAnimationTrack, xaAnimationKeyframe[]>> interpolateTracks = new List<Tuple<ImportedAnimationTrack,xaAnimationKeyframe[]>>(); foreach (var wsTrack in trackList) { if (!wsAnimation.isTrackEnabled(wsTrack)) continue; ImportedAnimationKeyframe[] keyframes = ((ImportedAnimationKeyframedTrack)wsTrack).Keyframes; xaAnimationKeyframe[] newKeyframes = null; int wsTrackKeyframesLength = 0; for (int i = 0; i < keyframes.Length; i++) { if (keyframes[i] != null) wsTrackKeyframesLength++; } if (resampleCount < 0 || wsTrackKeyframesLength == resampleCount) { newKeyframes = new xaAnimationKeyframe[wsTrackKeyframesLength]; int keyframeIdx = 0; for (int i = 0; i < keyframes.Length; i++) { ImportedAnimationKeyframe keyframe = keyframes[i]; if (keyframe == null) continue; newKeyframes[keyframeIdx] = new xaAnimationKeyframe(); newKeyframes[keyframeIdx].Index = i; newKeyframes[keyframeIdx].Rotation = keyframe.Rotation; xa.CreateUnknowns(newKeyframes[keyframeIdx]); newKeyframes[keyframeIdx].Translation = keyframe.Translation; newKeyframes[keyframeIdx].Scaling = keyframe.Scaling; keyframeIdx++; } } else { newKeyframes = new xaAnimationKeyframe[resampleCount]; if (wsTrackKeyframesLength < 1) { xaAnimationKeyframe keyframe = new xaAnimationKeyframe(); keyframe.Rotation = Quaternion.Identity; keyframe.Scaling = new Vector3(1, 1, 1); keyframe.Translation = new Vector3(0, 0, 0); xa.CreateUnknowns(keyframe); for (int i = 0; i < newKeyframes.Length; i++) { keyframe.Index = i; newKeyframes[i] = keyframe; } } else { interpolateTracks.Add(new Tuple<ImportedAnimationTrack, xaAnimationKeyframe[]>(wsTrack, newKeyframes)); } } newTrackList.Add(new KeyValuePair<string, xaAnimationKeyframe[]>(wsTrack.Name, newKeyframes)); } if (interpolateTracks.Count > 0) { Fbx.InterpolateKeyframes(interpolateTracks, resampleCount); } List<xaAnimationTrack> animationNodeList = parser.AnimationSection.TrackList; Dictionary<string, xaAnimationTrack> animationNodeDic = null; if (replaceMethod != ReplaceAnimationMethod.Replace) { animationNodeDic = new Dictionary<string, xaAnimationTrack>(); foreach (xaAnimationTrack animationNode in animationNodeList) { animationNodeDic.Add(animationNode.Name, animationNode); } } if (replaceMethod == ReplaceAnimationMethod.Replace) { animationNodeList.Clear(); foreach (var newTrack in newTrackList) { xaAnimationTrack animationNode = new xaAnimationTrack(); animationNodeList.Add(animationNode); animationNode.KeyframeList = new List<xaAnimationKeyframe>(newTrack.Value); animationNode.Name = newTrack.Key; xa.CreateUnknowns(animationNode); } } else if (replaceMethod == ReplaceAnimationMethod.ReplacePresent) { foreach (var newTrack in newTrackList) { xaAnimationTrack animationNode = xa.animationGetOriginalKeyframes(animationNodeDic, newTrack.Key, animationNodeList); animationNode.KeyframeList = new List<xaAnimationKeyframe>(newTrack.Value); } } else if (replaceMethod == ReplaceAnimationMethod.Merge) { foreach (var newTrack in newTrackList) { xaAnimationTrack animationNode; xaAnimationKeyframe[] origKeyframes = xa.animationGetOriginalKeyframes(animationNodeDic, newTrack.Key, animationNodeList, out animationNode); xaAnimationKeyframe[] destKeyframes; int newEnd = insertPos + newTrack.Value.Length; if (origKeyframes.Length < insertPos) { destKeyframes = new xaAnimationKeyframe[newEnd]; xa.animationCopyKeyframeTransformArray(origKeyframes, 0, destKeyframes, 0, origKeyframes.Length); xa.animationNormalizeTrack(origKeyframes, destKeyframes, insertPos); } else { if (origKeyframes.Length < newEnd) { destKeyframes = new xaAnimationKeyframe[newEnd]; } else { destKeyframes = new xaAnimationKeyframe[origKeyframes.Length]; xa.animationCopyKeyframeTransformArray(origKeyframes, newEnd, destKeyframes, newEnd, origKeyframes.Length - newEnd); } xa.animationCopyKeyframeTransformArray(origKeyframes, 0, destKeyframes, 0, insertPos); } xa.animationCopyKeyframeTransformArray(newTrack.Value, 0, destKeyframes, insertPos, newTrack.Value.Length); animationNode.KeyframeList = new List<xaAnimationKeyframe>(destKeyframes); } } else if (replaceMethod == ReplaceAnimationMethod.Insert) { foreach (var newTrack in newTrackList) { xaAnimationTrack animationNode; xaAnimationKeyframe[] origKeyframes = xa.animationGetOriginalKeyframes(animationNodeDic, newTrack.Key, animationNodeList, out animationNode); ; xaAnimationKeyframe[] destKeyframes; int newEnd = insertPos + newTrack.Value.Length; if (origKeyframes.Length < insertPos) { destKeyframes = new xaAnimationKeyframe[newEnd]; xa.animationCopyKeyframeTransformArray(origKeyframes, 0, destKeyframes, 0, origKeyframes.Length); xa.animationNormalizeTrack(origKeyframes, destKeyframes, insertPos); } else { destKeyframes = new xaAnimationKeyframe[origKeyframes.Length + newTrack.Value.Length]; xa.animationCopyKeyframeTransformArray(origKeyframes, 0, destKeyframes, 0, insertPos); xa.animationCopyKeyframeTransformArray(origKeyframes, insertPos, destKeyframes, newEnd, origKeyframes.Length - insertPos); } xa.animationCopyKeyframeTransformArray(newTrack.Value, 0, destKeyframes, insertPos, newTrack.Value.Length); animationNode.KeyframeList = new List<xaAnimationKeyframe>(destKeyframes); } } else if (replaceMethod == ReplaceAnimationMethod.Append) { int maxKeyframes = 0; foreach (xaAnimationTrack animationNode in animationNodeList) { int numKeyframes = animationNode.KeyframeList[animationNode.KeyframeList.Count - 1].Index; if (numKeyframes > maxKeyframes) { maxKeyframes = numKeyframes; } } foreach (var newTrack in newTrackList) { xaAnimationTrack animationNode; xaAnimationKeyframe[] origKeyframes = xa.animationGetOriginalKeyframes(animationNodeDic, newTrack.Key, animationNodeList, out animationNode); xaAnimationKeyframe[] destKeyframes = new xaAnimationKeyframe[maxKeyframes + insertPos + newTrack.Value[newTrack.Value.Length - 1].Index + 1]; xa.animationCopyKeyframeTransformArray(origKeyframes, destKeyframes, 0); if (origKeyframes.Length > 0 && origKeyframes.Length == origKeyframes[origKeyframes.Length - 1].Index + 1) { xa.animationNormalizeTrack(origKeyframes, destKeyframes, origKeyframes.Length + insertPos); } xa.animationCopyKeyframeTransformArray(newTrack.Value, destKeyframes, maxKeyframes + insertPos); animationNode.KeyframeList = new List<xaAnimationKeyframe>(origKeyframes.Length + insertPos + newTrack.Value.Length); for (int i = 0; i < destKeyframes.Length; i++) { if (destKeyframes[i] == null) continue; animationNode.KeyframeList.Add(destKeyframes[i]); } } } else { Report.ReportLog("Error: Unexpected animation replace method " + replaceMethod + ". Skipping this animation"); return; } }
public static void SaveXA(xaParser parser, string destPath, bool keepBackup) { DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(destPath)); string backup = null; if (keepBackup && File.Exists(destPath)) { backup = Utility.GetDestFile(dir, Path.GetFileNameWithoutExtension(destPath) + ".bak", Path.GetExtension(destPath)); File.Move(destPath, backup); } try { using (BufferedStream bufStr = new BufferedStream(File.OpenWrite(destPath))) { parser.WriteTo(bufStr); } } catch { if (File.Exists(backup)) { if (File.Exists(destPath)) File.Delete(destPath); File.Move(backup, destPath); } } }
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); } }
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 void Dispose() { Parser = null; }
public static void ReplaceAnimation(WorkspaceAnimation wsAnimation, xaParser parser, int resampleCount, ReplaceAnimationMethod replaceMethod, int insertPos) { if (parser.AnimationSection == null) { Report.ReportLog("The .xa file doesn't have an animation section. Skipping this animation"); return; } if (!(wsAnimation.importedAnimation is ImportedKeyframedAnimation)) { Report.ReportLog("The animation has incompatible keyframes."); return; } Report.ReportLog("Replacing animation ..."); List <ImportedAnimationKeyframedTrack> trackList = ((ImportedKeyframedAnimation)wsAnimation.importedAnimation).TrackList; List <KeyValuePair <string, xaAnimationKeyframe[]> > newTrackList = new List <KeyValuePair <string, xaAnimationKeyframe[]> >(trackList.Count); List <Tuple <ImportedAnimationTrack, xaAnimationKeyframe[]> > interpolateTracks = new List <Tuple <ImportedAnimationTrack, xaAnimationKeyframe[]> >(); foreach (var wsTrack in trackList) { if (!wsAnimation.isTrackEnabled(wsTrack)) { continue; } ImportedAnimationKeyframe[] keyframes = ((ImportedAnimationKeyframedTrack)wsTrack).Keyframes; xaAnimationKeyframe[] newKeyframes = null; int wsTrackKeyframesLength = 0; for (int i = 0; i < keyframes.Length; i++) { if (keyframes[i] != null) { wsTrackKeyframesLength++; } } if (resampleCount < 0 || wsTrackKeyframesLength == resampleCount) { newKeyframes = new xaAnimationKeyframe[wsTrackKeyframesLength]; int keyframeIdx = 0; for (int i = 0; i < keyframes.Length; i++) { ImportedAnimationKeyframe keyframe = keyframes[i]; if (keyframe == null) { continue; } newKeyframes[keyframeIdx] = new xaAnimationKeyframe(); newKeyframes[keyframeIdx].Index = i; newKeyframes[keyframeIdx].Rotation = keyframe.Rotation; xa.CreateUnknowns(newKeyframes[keyframeIdx]); newKeyframes[keyframeIdx].Translation = keyframe.Translation; newKeyframes[keyframeIdx].Scaling = keyframe.Scaling; keyframeIdx++; } } else { newKeyframes = new xaAnimationKeyframe[resampleCount]; if (wsTrackKeyframesLength < 1) { xaAnimationKeyframe keyframe = new xaAnimationKeyframe(); keyframe.Rotation = Quaternion.Identity; keyframe.Scaling = new Vector3(1, 1, 1); keyframe.Translation = new Vector3(0, 0, 0); xa.CreateUnknowns(keyframe); for (int i = 0; i < newKeyframes.Length; i++) { keyframe.Index = i; newKeyframes[i] = keyframe; } } else { interpolateTracks.Add(new Tuple <ImportedAnimationTrack, xaAnimationKeyframe[]>(wsTrack, newKeyframes)); } } newTrackList.Add(new KeyValuePair <string, xaAnimationKeyframe[]>(wsTrack.Name, newKeyframes)); } if (interpolateTracks.Count > 0) { Fbx.InterpolateKeyframes(interpolateTracks, resampleCount); } List <xaAnimationTrack> animationNodeList = parser.AnimationSection.TrackList; Dictionary <string, xaAnimationTrack> animationNodeDic = null; if (replaceMethod != ReplaceAnimationMethod.Replace) { animationNodeDic = new Dictionary <string, xaAnimationTrack>(); foreach (xaAnimationTrack animationNode in animationNodeList) { animationNodeDic.Add(animationNode.Name, animationNode); } } if (replaceMethod == ReplaceAnimationMethod.Replace) { animationNodeList.Clear(); foreach (var newTrack in newTrackList) { xaAnimationTrack animationNode = new xaAnimationTrack(); animationNodeList.Add(animationNode); animationNode.KeyframeList = new List <xaAnimationKeyframe>(newTrack.Value); animationNode.Name = newTrack.Key; xa.CreateUnknowns(animationNode); } } else if (replaceMethod == ReplaceAnimationMethod.ReplacePresent) { foreach (var newTrack in newTrackList) { xaAnimationTrack animationNode = xa.animationGetOriginalKeyframes(animationNodeDic, newTrack.Key, animationNodeList); animationNode.KeyframeList = new List <xaAnimationKeyframe>(newTrack.Value); } } else if (replaceMethod == ReplaceAnimationMethod.Merge) { foreach (var newTrack in newTrackList) { xaAnimationTrack animationNode; xaAnimationKeyframe[] origKeyframes = xa.animationGetOriginalKeyframes(animationNodeDic, newTrack.Key, animationNodeList, out animationNode); xaAnimationKeyframe[] destKeyframes; int newEnd = insertPos + newTrack.Value.Length; if (origKeyframes.Length < insertPos) { destKeyframes = new xaAnimationKeyframe[newEnd]; xa.animationCopyKeyframeTransformArray(origKeyframes, 0, destKeyframes, 0, origKeyframes.Length); xa.animationNormalizeTrack(origKeyframes, destKeyframes, insertPos); } else { if (origKeyframes.Length < newEnd) { destKeyframes = new xaAnimationKeyframe[newEnd]; } else { destKeyframes = new xaAnimationKeyframe[origKeyframes.Length]; xa.animationCopyKeyframeTransformArray(origKeyframes, newEnd, destKeyframes, newEnd, origKeyframes.Length - newEnd); } xa.animationCopyKeyframeTransformArray(origKeyframes, 0, destKeyframes, 0, insertPos); } xa.animationCopyKeyframeTransformArray(newTrack.Value, 0, destKeyframes, insertPos, newTrack.Value.Length); animationNode.KeyframeList = new List <xaAnimationKeyframe>(destKeyframes); } } else if (replaceMethod == ReplaceAnimationMethod.Insert) { foreach (var newTrack in newTrackList) { xaAnimationTrack animationNode; xaAnimationKeyframe[] origKeyframes = xa.animationGetOriginalKeyframes(animationNodeDic, newTrack.Key, animationNodeList, out animationNode);; xaAnimationKeyframe[] destKeyframes; int newEnd = insertPos + newTrack.Value.Length; if (origKeyframes.Length < insertPos) { destKeyframes = new xaAnimationKeyframe[newEnd]; xa.animationCopyKeyframeTransformArray(origKeyframes, 0, destKeyframes, 0, origKeyframes.Length); xa.animationNormalizeTrack(origKeyframes, destKeyframes, insertPos); } else { destKeyframes = new xaAnimationKeyframe[origKeyframes.Length + newTrack.Value.Length]; xa.animationCopyKeyframeTransformArray(origKeyframes, 0, destKeyframes, 0, insertPos); xa.animationCopyKeyframeTransformArray(origKeyframes, insertPos, destKeyframes, newEnd, origKeyframes.Length - insertPos); } xa.animationCopyKeyframeTransformArray(newTrack.Value, 0, destKeyframes, insertPos, newTrack.Value.Length); animationNode.KeyframeList = new List <xaAnimationKeyframe>(destKeyframes); } } else if (replaceMethod == ReplaceAnimationMethod.Append) { int maxKeyframes = 0; foreach (xaAnimationTrack animationNode in animationNodeList) { int numKeyframes = animationNode.KeyframeList[animationNode.KeyframeList.Count - 1].Index; if (numKeyframes > maxKeyframes) { maxKeyframes = numKeyframes; } } foreach (var newTrack in newTrackList) { xaAnimationTrack animationNode; xaAnimationKeyframe[] origKeyframes = xa.animationGetOriginalKeyframes(animationNodeDic, newTrack.Key, animationNodeList, out animationNode); xaAnimationKeyframe[] destKeyframes = new xaAnimationKeyframe[maxKeyframes + insertPos + newTrack.Value[newTrack.Value.Length - 1].Index + 1]; xa.animationCopyKeyframeTransformArray(origKeyframes, destKeyframes, 0); if (origKeyframes.Length > 0 && origKeyframes.Length == origKeyframes[origKeyframes.Length - 1].Index + 1) { xa.animationNormalizeTrack(origKeyframes, destKeyframes, origKeyframes.Length + insertPos); } xa.animationCopyKeyframeTransformArray(newTrack.Value, destKeyframes, maxKeyframes + insertPos); animationNode.KeyframeList = new List <xaAnimationKeyframe>(origKeyframes.Length + insertPos + newTrack.Value.Length); for (int i = 0; i < destKeyframes.Length; i++) { if (destKeyframes[i] == null) { continue; } animationNode.KeyframeList.Add(destKeyframes[i]); } } } else { Report.ReportLog("Error: Unexpected animation replace method " + replaceMethod + ". Skipping this animation"); return; } }
public static void ExportMorphMqo([DefaultVar] string dirPath, xxParser xxparser, xxFrame meshFrame, xaParser xaparser, xaMorphClip clip) { Mqo.ExporterMorph.Export(dirPath, xxparser, meshFrame, xaparser, clip); }
private ExporterMorph(DirectoryInfo dir, xxParser xxParser, xaParser xaParser, xaMorphClip clip) { this.xxParser = xxParser; this.xaParser = xaParser; this.clip = clip; }
public static xaAnimationTrack FindTrack(string name, xaParser parser) { foreach (xaAnimationTrack track in parser.AnimationSection.TrackList) { if (track.Name == name) { return track; } } return null; }