public static List <FOBJKey> GetKeys(HSD_FOBJ FOBJ) { FOBJFrameDecoder e = new FOBJFrameDecoder(FOBJ); { return(e.GetKeys()); } }
public static List <FOBJKey> GetKeys(HSD_FOBJ FOBJ, float startframe) { FOBJFrameDecoder e = new FOBJFrameDecoder(FOBJ, startframe); { return(e.GetKeys(startframe)); } }
public static List <FOBJKey> DecodeTrack(HSD_FOBJ FOBJ) { using (FOBJFrameDecoder e = new FOBJFrameDecoder(FOBJ)) { return(e.GetKeys()); } }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <param name="type"></param> /// <param name="keys"></param> /// <param name="defaultValue"></param> private void CreateTrack(FigaTreeNode node, JointTrackType type, List <FOBJKey> keys, float defaultValue) { // empty track if (keys.Count == 0) { return; } // skip constant tracks if (keys.Count == 1 && Math.Abs(keys[0].Value - defaultValue) < 0.001f) { return; } // skip constant tracks if (keys.Count == 2 && Math.Abs(keys[0].Value - defaultValue) < 0.001f) { return; } HSD_FOBJ fobj = new HSD_FOBJ(); fobj.SetKeys(keys, type); HSD_Track track = new HSD_Track(); track.FromFOBJ(fobj); node.Tracks.Add(track); }
/// <summary> /// /// </summary> /// <param name="fobj"></param> /// <param name="lines"></param> public static HSD_FOBJ ImportKeys(HSD_FOBJ fobj, string[] lines) { List <FOBJKey> keys = new List <FOBJKey>(); var animationType = JointTrackType.HSD_A_J_ROTX; foreach (var v in lines) { var args = v.Trim().Split(','); if (args.Length == 1 && Enum.TryParse <JointTrackType>(args[0], out animationType)) { continue; } if (args.Length < 4) { continue; } keys.Add(new FOBJKey() { Frame = float.Parse(args[0]), Value = float.Parse(args[1]), Tan = float.Parse(args[2]), InterpolationType = (GXInterpolationType)Enum.Parse(typeof(GXInterpolationType), args[3]) }); } fobj.SetKeys(keys, animationType); return(fobj); }
public FOBJFrameDecoder(HSD_FOBJ FOBJ, float startframe) { if (FOBJ.Buffer == null) { FOBJ.SetKeys(new List <FOBJKey>() { new FOBJKey() }, JointTrackType.HSD_A_J_ROTX); } Stream = new MemoryStream(FOBJ.Buffer); Reader = new BinaryReaderExt(Stream); this.FOBJ = FOBJ; }
/// <summary> /// /// </summary> /// <param name="fobj"></param> /// <returns></returns> public static string ToString(HSD_FOBJ fobj) { StringBuilder sb = new StringBuilder(); sb.AppendLine(fobj.AnimationType.ToString()); foreach (var v in fobj.GetDecodedKeys()) { sb.AppendLine($"{v.Frame},{v.Value},{v.Tan},{v.InterpolationType}"); } return(sb.ToString()); }
/// <summary> /// /// </summary> /// <param name="nodes"></param> /// <param name="frameCount"></param> /// <returns></returns> public HSD_FigaTree ToFigaTree(float error = 0.0001f) { HSD_FigaTree tree = new HSD_FigaTree(); tree.FrameCount = FrameCount; tree.Type = 1; tree.Nodes = Nodes.Select(e => { var fn = new FigaTreeNode(); foreach (var t in e.Tracks) { HSD_Track track = new HSD_Track(); HSD_FOBJ fobj = t.ToFobj(error); track.FromFOBJ(fobj); fn.Tracks.Add(track); } return(fn); }).ToList(); return(tree); }
/// <summary> /// /// </summary> /// <param name="nodes"></param> /// <param name="frameCount"></param> /// <returns></returns> public HSD_FigaTree ToFigaTree() { HSD_FigaTree tree = new HSD_FigaTree(); tree.FrameCount = FrameCount; tree.Type = 1; tree.Nodes = Nodes.Select(e => { var fn = new FigaTreeNode(); foreach (var t in e.Tracks) { HSD_Track track = new HSD_Track(); HSD_FOBJ fobj = new HSD_FOBJ(); fobj.SetKeys(t.Keys, t.JointTrackType); track.FOBJ = fobj; fn.Tracks.Add(track); } return(fn); }).ToList(); return(tree); }
public static HSD_FOBJ EncodeFrames(List <FOBJKey> Keys, byte TrackType, float error = 0.0001f) { HSD_FOBJ fobj = new HSD_FOBJ(); fobj.JointTrackType = (JointTrackType)TrackType; if (fobj.JointTrackType == JointTrackType.HSD_A_J_PTCL) { return(fobj); } // automatically set single key interpolation type if (Keys.Count == 1) { Keys[0].InterpolationType = GXInterpolationType.HSD_A_OP_KEY; } // perform quantization FOBJQuantanizer valueQ = new FOBJQuantanizer(error); FOBJQuantanizer tangentQ = new FOBJQuantanizer(error); foreach (FOBJKey key in Keys) { valueQ.AddValue(key.Value); tangentQ.AddValue(key.Tan); } fobj.ValueScale = valueQ.GetValueScale(); fobj.ValueFormat = valueQ.GetDataFormat(); fobj.TanScale = tangentQ.GetValueScale(); fobj.TanFormat = tangentQ.GetDataFormat(); MemoryStream o = new MemoryStream(); using (BinaryWriterExt Writer = new BinaryWriterExt(o)) { Writer.BigEndian = false; for (int i = 0; i < Keys.Count;) { GXInterpolationType ip = Keys[i].InterpolationType; int j; for (j = 0; j < Keys.Count - i; j++) { if (Keys[i + j].InterpolationType != ip) { break; } } if (j > 0x7FF) { j = 0x7FF; } int flag = ((j - 1) << 4) | (int)ip; Writer.WritePacked(flag); for (int k = i; k < i + j; k++) { int DeltaTime = 0; if (k + 1 < Keys.Count) { var nextKey = Keys.Find(e => e.Frame > Keys[k].Frame && e.InterpolationType != GXInterpolationType.HSD_A_OP_SLP); if (nextKey != null) { DeltaTime = (int)(nextKey.Frame - Keys[k].Frame); } } if (k == Keys.Count) { DeltaTime = 1; } switch (ip) { case GXInterpolationType.HSD_A_OP_CON: valueQ.WriteValue(Writer, Keys[k].Value); Writer.WritePacked(DeltaTime); break; case GXInterpolationType.HSD_A_OP_LIN: valueQ.WriteValue(Writer, Keys[k].Value); Writer.WritePacked(DeltaTime); break; case GXInterpolationType.HSD_A_OP_SPL0: valueQ.WriteValue(Writer, Keys[k].Value); Writer.WritePacked(DeltaTime); break; case GXInterpolationType.HSD_A_OP_SPL: valueQ.WriteValue(Writer, Keys[k].Value); tangentQ.WriteValue(Writer, Keys[k].Tan); Writer.WritePacked(DeltaTime); break; case GXInterpolationType.HSD_A_OP_SLP: tangentQ.WriteValue(Writer, Keys[k].Tan); break; case GXInterpolationType.HSD_A_OP_KEY: valueQ.WriteValue(Writer, Keys[k].Value); break; } } i += j; } } fobj.Buffer = o.ToArray(); o.Dispose(); return(fobj); }
public static HSD_FOBJ EncodeFrames(List <FOBJKey> Keys, byte TrackType) { HSD_FOBJ fobj = new HSD_FOBJ(); fobj.AnimationType = TrackType; // perform quantization FOBJQuantanizer valueQ = new FOBJQuantanizer(); FOBJQuantanizer tangentQ = new FOBJQuantanizer(); foreach (FOBJKey key in Keys) { valueQ.AddValue(key.Value); tangentQ.AddValue(key.Tan); } fobj.ValueScale = valueQ.GetValueScale(); fobj.ValueFormat = valueQ.GetDataFormat(); fobj.TanScale = tangentQ.GetValueScale(); fobj.TanFormat = tangentQ.GetDataFormat(); MemoryStream o = new MemoryStream(); using (HSDWriter Writer = new HSDWriter(o)) { Writer.BigEndian = false; int time = 0; for (int i = 0; i < Keys.Count;) { InterpolationType ip = Keys[i].InterpolationType; int j; for (j = 0; j < Keys.Count - i; j++) { if (Keys[i + j].InterpolationType != ip) { break; } } int flag = ((j - 1) << 4) | (int)ip; Writer.ExtendedByte(flag); for (int k = i; k < i + j; k++) { int DeltaTime = 0; if (k + 1 < Keys.Count) { DeltaTime = (int)(Keys[k + 1].Frame - Keys[k].Frame); } if (k == Keys.Count) { DeltaTime = 1; } switch (ip) { case InterpolationType.Step: valueQ.WriteValue(Writer, Keys[k].Value); Writer.ExtendedByte(DeltaTime); break; case InterpolationType.Linear: valueQ.WriteValue(Writer, Keys[k].Value); Writer.ExtendedByte(DeltaTime); break; case InterpolationType.HermiteValue: valueQ.WriteValue(Writer, Keys[k].Value); Writer.ExtendedByte(DeltaTime); break; case InterpolationType.Hermite: valueQ.WriteValue(Writer, Keys[k].Value); tangentQ.WriteValue(Writer, Keys[k].Tan); Writer.ExtendedByte(DeltaTime); break; case InterpolationType.HermiteCurve: tangentQ.WriteValue(Writer, Keys[k].Tan); break; case InterpolationType.Constant: valueQ.WriteValue(Writer, Keys[k].Value); break; default: throw new Exception("end"); } if (ip != InterpolationType.HermiteCurve) { time = (int)Keys[k].Frame; } } i += j; } } fobj.Data = o.ToArray(); o.Close(); o.Dispose(); return(fobj); }
public FOBJFrameDecoder(HSD_FOBJ FOBJ) { Stream = new MemoryStream(FOBJ.Data); Reader = new HSDReader(Stream); this.FOBJ = FOBJ; }
/// <summary> /// /// </summary> /// <param name="fobj"></param> /// <returns></returns> private static SBTransformTrack DecodeFOBJ(HSD_FOBJ fobj) { SBTrackType trackType = SBTrackType.TranslateX; switch (fobj.JointTrackType) { case JointTrackType.HSD_A_J_ROTX: trackType = SBTrackType.RotateX; break; case JointTrackType.HSD_A_J_ROTY: trackType = SBTrackType.RotateY; break; case JointTrackType.HSD_A_J_ROTZ: trackType = SBTrackType.RotateZ; break; case JointTrackType.HSD_A_J_TRAX: trackType = SBTrackType.TranslateX; break; case JointTrackType.HSD_A_J_TRAY: trackType = SBTrackType.TranslateY; break; case JointTrackType.HSD_A_J_TRAZ: trackType = SBTrackType.TranslateZ; break; case JointTrackType.HSD_A_J_SCAX: trackType = SBTrackType.ScaleX; break; case JointTrackType.HSD_A_J_SCAY: trackType = SBTrackType.ScaleY; break; case JointTrackType.HSD_A_J_SCAZ: trackType = SBTrackType.ScaleZ; break; } SBTransformTrack track = new SBTransformTrack(trackType); var Keys = fobj.GetDecodedKeys(); // get current frame state AnimState prevState = null; for (int i = 0; i < Keys.Count; i++) { var state = GetState(Keys[i].Frame, Keys); bool nextSlope = i + 1 < Keys.Count && Keys[i + 1].InterpolationType == GXInterpolationType.HSD_A_OP_SLP; if (Keys[i].Frame == state.t1) { state.t0 = state.t1; state.p0 = state.p1; state.d0 = state.d1; } if (Keys[i].InterpolationType == GXInterpolationType.HSD_A_OP_SLP) { continue; } if (state.op_intrp == GXInterpolationType.HSD_A_OP_CON || state.op_intrp == GXInterpolationType.HSD_A_OP_KEY) { track.AddKey(state.t0, state.p0, InterpolationType.Step); } if (state.op_intrp == GXInterpolationType.HSD_A_OP_LIN) { track.AddKey(state.t0, state.p0, InterpolationType.Linear); } if (state.op_intrp == GXInterpolationType.HSD_A_OP_SPL || state.op_intrp == GXInterpolationType.HSD_A_OP_SPL0 || state.op_intrp == GXInterpolationType.HSD_A_OP_SLP) { track.AddKey(state.t0, state.p0, InterpolationType.Hermite, (nextSlope && prevState != null) ? prevState.d1 : state.d0, state.d0); } prevState = state; } return(track); }
public FOBJFrameDecoder(HSD_FOBJ FOBJ) { Stream = new MemoryStream(FOBJ.Buffer); Reader = new BinaryReaderExt(Stream); this.FOBJ = FOBJ; }
public static HSD_FOBJ EncodeFrames(List <FOBJKey> Keys, byte TrackType) { HSD_FOBJ fobj = new HSD_FOBJ(); fobj.AnimationType = (JointTrackType)TrackType; // perform quantization FOBJQuantanizer valueQ = new FOBJQuantanizer(); FOBJQuantanizer tangentQ = new FOBJQuantanizer(); foreach (FOBJKey key in Keys) { valueQ.AddValue(key.Value); tangentQ.AddValue(key.Tan); } fobj.ValueScale = valueQ.GetValueScale(); fobj.ValueFormat = valueQ.GetDataFormat(); fobj.TanScale = tangentQ.GetValueScale(); fobj.TanFormat = tangentQ.GetDataFormat(); MemoryStream o = new MemoryStream(); using (BinaryWriterExt Writer = new BinaryWriterExt(o)) { Writer.BigEndian = false; int time = 0; for (int i = 0; i < Keys.Count;) { GXInterpolationType ip = Keys[i].InterpolationType; int j; for (j = 0; j < Keys.Count - i; j++) { if (Keys[i + j].InterpolationType != ip) { break; } } int flag = ((j - 1) << 4) | (int)ip; Writer.ExtendedByte(flag); for (int k = i; k < i + j; k++) { int DeltaTime = 0; if (k + 1 < Keys.Count) { DeltaTime = (int)(Keys[k + 1].Frame - Keys[k].Frame); } if (k == Keys.Count) { DeltaTime = 1; } switch (ip) { case GXInterpolationType.HSD_A_OP_CON: valueQ.WriteValue(Writer, Keys[k].Value); Writer.ExtendedByte(DeltaTime); break; case GXInterpolationType.HSD_A_OP_LIN: valueQ.WriteValue(Writer, Keys[k].Value); Writer.ExtendedByte(DeltaTime); break; case GXInterpolationType.HSD_A_OP_SPL0: valueQ.WriteValue(Writer, Keys[k].Value); Writer.ExtendedByte(DeltaTime); break; case GXInterpolationType.HSD_A_OP_SPL: valueQ.WriteValue(Writer, Keys[k].Value); tangentQ.WriteValue(Writer, Keys[k].Tan); Writer.ExtendedByte(DeltaTime); break; case GXInterpolationType.HSD_A_OP_SLP: tangentQ.WriteValue(Writer, Keys[k].Tan); break; case GXInterpolationType.HSD_A_OP_KEY: valueQ.WriteValue(Writer, Keys[k].Value); break; } if (ip != GXInterpolationType.HSD_A_OP_SLP) { time = (int)Keys[k].Frame; } } i += j; } } fobj.Buffer = o.ToArray(); o.Dispose(); return(fobj); }
/// <summary> /// /// </summary> /// <param name="track"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="z"></param> private static HSD_FOBJ OrientTrack(string boneName, SkeletonPorter port, List <FOBJKey> keys, JointTrackType trackType, HSD_JOBJ sourceBone, HSD_JOBJ targetBone) { //var frames = keys.Select(e => e.Frame);//.Join(y.Keys.Select(e=>e.Frame).Join(z.Keys.Select(e=>e.Frame))).Distinct(); Vector3 unit = Vector3.Zero; switch (trackType) { case JointTrackType.HSD_A_J_ROTX: unit = Vector3.UnitX; break; case JointTrackType.HSD_A_J_ROTY: unit = Vector3.UnitY; break; case JointTrackType.HSD_A_J_ROTZ: unit = Vector3.UnitZ; break; } var newTrack = port.ReOrientParent(unit, boneName); if (Math.Abs(Math.Abs(newTrack.X) - 1) < 0.001f) { trackType = JointTrackType.HSD_A_J_ROTX; } if (Math.Abs(Math.Abs(newTrack.Y) - 1) < 0.001f) { trackType = JointTrackType.HSD_A_J_ROTY; } if (Math.Abs(Math.Abs(newTrack.Z) - 1) < 0.001f) { trackType = JointTrackType.HSD_A_J_ROTZ; } /*var k = keys[0]; * keys.Clear(); * k.InterpolationType = GXInterpolationType.HSD_A_OP_KEY; * keys.Add(k);*/ int index = 0; foreach (var f in keys) { var rot = unit * f.Value; rot -= new Vector3(sourceBone.RX, sourceBone.RY, sourceBone.RZ); rot = port.ReOrientParent(rot, boneName); rot += new Vector3(targetBone.RX, targetBone.RY, targetBone.RZ); if (boneName.Equals("LLegJA")) { System.Diagnostics.Debug.WriteLine($"{boneName} {trackType} {rot} {f.Value}"); } if (boneName.Equals("RShoulderN")) { System.Diagnostics.Debug.WriteLine($"{boneName} {trackType} {rot} {f.Value}"); } if (boneName.Equals("LKneeJ")) { System.Diagnostics.Debug.WriteLine($"{boneName} {trackType} {rot} {f.Value}"); } switch (trackType) { case JointTrackType.HSD_A_J_ROTX: f.Value = rot.X; break; case JointTrackType.HSD_A_J_ROTY: f.Value = rot.Y; break; case JointTrackType.HSD_A_J_ROTZ: f.Value = rot.Z; break; } index++; } var fobj = new HSD_FOBJ(); fobj.SetKeys(keys, trackType); return(fobj); }