public static TMOFrame[] Slerp(TMOFrame frame0, TMOFrame frame1, TMOFrame frame2, TMOFrame frame3, int length) { TMOFrame[] frames = new TMOFrame[length]; for (int frame_index = 0; frame_index < length; frame_index++) { frames[frame_index] = new TMOFrame(); frames[frame_index].matrices = new TMOMat[frame1.matrices.Length]; } for (int i = 0; i < frame1.matrices.Length; i++) { TMOMat[] interpolated_matrices = TMOMat.Slerp( frame0.matrices[i], frame1.matrices[i], frame2.matrices[i], frame3.matrices[i], length); for (int frame_index = 0; frame_index < length; frame_index++) { frames[frame_index].matrices[i] = interpolated_matrices[frame_index]; } } return(frames); }
protected void UpdateBoneMatrices(TSONode tso_node, TMOFrame tmo_frame) { matrixStack.Push(); Matrix transform; if (tmo_frame != tmo_frame_one) { // TMO animation TMONode tmo_node; if (nodemap.TryGetValue(tso_node, out tmo_node)) { transform = tmo_frame.matrices[tmo_node.ID].m; } else { transform = tso_node.transformation_matrix; } } else { transform = tso_node.transformation_matrix; } matrixStack.MultiplyMatrixLocal(transform); combinedMatrices[tmo_frame][tso_node] = matrixStack.Top; foreach (TSONode child_node in tso_node.child_nodes) { UpdateBoneMatrices(child_node, tmo_frame); } matrixStack.Pop(); }
public TSOFigure() { tmo = new TMOFile(); nodemap = new Dictionary <TSONode, TMONode>(); tmo_frame_one = new TMOFrame(); combinedMatrices = new Dictionary <TMOFrame, Dictionary <TSONode, Matrix> >(); combinedMatrices[tmo_frame_one] = new Dictionary <TSONode, Matrix>(); matrixStack = new MatrixStack(); }
public static TMOFrame AddSub(TMOFrame frame0, TMOFrame frame1, TMOFrame frame2, int[] index_pair) { TMOFrame ret = new TMOFrame(); ret.matrices = new TMOMat[frame0.matrices.Length]; for (int i = 0; i < frame0.matrices.Length; i++) { ret.matrices[i] = TMOMat.AddSub(frame0.matrices[i], frame1.matrices[index_pair[i]], frame2.matrices[index_pair[i]]); } return(ret); }
public static TMOFrame Select(TMOFrame frame0, TMOFrame frame1, int[] index_pair) { TMOFrame ret = new TMOFrame(); ret.matrices = new TMOMat[frame0.matrices.Length]; for (int i = 0; i < frame0.matrices.Length; i++) { ret.matrices[i] = frame1.matrices[index_pair[i]]; } return(ret); }
//TSOFileをTSOListに追加します。 public void AddTSO(TSOFile tso) { if (tmo.frames != null) { AddNodeMap(tso); } TMOFrame tmo_frame = GetTMOFrame(); UpdateBoneMatrices(tso, tmo_frame); TSOList.Add(tso); }
public void SlerpFrameEndTo(TMOFile motion, int append_length) { int i0 = (frames.Length > 1) ? frames.Length - 1 - 1 : 0; int i1 = frames.Length - 1; int i2 = 0; int i3 = (motion.frames.Length > 1) ? 1 : 0; TMOFrame frame0 = frames[i0]; TMOFrame frame1 = frames[i1]; TMOFrame frame2 = motion.frames[i2]; TMOFrame frame3 = motion.frames[i3]; TMOFrame[] interp_frames = TMOFrame.Slerp(frame0, frame1, frame2, frame3, append_length); int old_length = frames.Length; Array.Resize(ref frames, frames.Length + append_length); Array.Copy(interp_frames, 0, frames, old_length, append_length); this.opt0 = frames.Length - 1; }
public void AppendFrameFrom(TMOFile motion) { int[] index_pair = CreateFrameIndexPair(motion); TMOFrame source_frame = frames[0]; int append_length = motion.frames.Length; TMOFrame[] append_frames = new TMOFrame[append_length]; for (int i = 0; i < motion.frames.Length; i++) { append_frames[i] = TMOFrame.Select(source_frame, motion.frames[i], index_pair); } int old_length = frames.Length; Array.Resize(ref frames, frames.Length + append_length); Array.Copy(append_frames, 0, frames, old_length, append_length); this.opt0 = frames.Length - 1; }
public static void Write(BinaryWriter bw, TMOFrame item) { Write(bw, item.matrices); }
public void Load(Stream source_stream) { this.reader = new BinaryReader(source_stream, System.Text.Encoding.Default); byte[] magic = reader.ReadBytes(4); if (magic[0] != (byte)'T' || magic[1] != (byte)'M' || magic[2] != (byte)'O' || magic[3] != (byte)'1') { throw new Exception("File is not TMO"); } this.header = reader.ReadBytes(8); this.opt0 = reader.ReadInt32(); this.opt1 = reader.ReadInt32(); int node_count = reader.ReadInt32(); nodes = new TMONode[node_count]; nodemap = new Dictionary <string, TMONode>(); for (int i = 0; i < node_count; i++) { nodes[i] = new TMONode(); nodes[i].id = i; nodes[i].name = ReadString(); nodes[i].sname = nodes[i].name.Substring(nodes[i].name.LastIndexOf('|') + 1); nodemap.Add(nodes[i].name, nodes[i]); //Console.WriteLine(i + ": " + nodes[i].name); } for (int i = 0; i < node_count; i++) { int index = nodes[i].name.LastIndexOf('|'); if (index <= 0) { continue; } string pname = nodes[i].name.Substring(0, index); nodes[i].parent = nodemap[pname]; nodes[i].parent.children.Add(nodes[i]); } int frame_count = reader.ReadInt32(); frames = new TMOFrame[frame_count]; for (int i = 0; i < frame_count; i++) { frames[i] = new TMOFrame(); frames[i].id = i; int matrix_count = reader.ReadInt32(); frames[i].matrices = new TMOMat[matrix_count]; for (int j = 0; j < matrix_count; j++) { frames[i].matrices[j] = new TMOMat(ReadMatrix()); nodes[j].frame_matrices.Add(frames[i].matrices[j]); //Console.WriteLine(frames[i].matrices[j].m); } } this.footer = reader.ReadBytes(4); reader.Close(); }
public Dictionary <TSONode, Matrix> GetCombinedMatrices() { TMOFrame tmo_frame = GetTMOFrame(); return(combinedMatrices[tmo_frame]); }
protected void UpdateBoneMatrices(TSOFile tso, TMOFrame tmo_frame) { matrixStack.LoadMatrix(Matrix.Identity); UpdateBoneMatrices(tso.nodes[0], tmo_frame); }
public void UpdateCenterPosition() { if (TSOList.Count == 0) { return; } TSOFile tso = TSOList[0]; switch (GetCenterBoneType()) { case 1://Hand { TSONode tso_nodeR; TSONode tso_nodeL; string boneR = "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_RightShoulder_Dummy|W_RightShoulder|W_RightArm_Dummy|W_RightArm|W_RightArmRoll|W_RightForeArm|W_RightForeArmRoll|W_RightHand"; string boneL = "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_LeftShoulder_Dummy|W_LeftShoulder|W_LeftArm_Dummy|W_LeftArm|W_LeftArmRoll|W_LeftForeArm|W_LeftForeArmRoll|W_LeftHand"; if (tso.nodemap.TryGetValue(boneR, out tso_nodeR) && tso.nodemap.TryGetValue(boneL, out tso_nodeL)) { TMOFrame tmo_frame = GetTMOFrame(); Matrix mR = combinedMatrices[tmo_frame][tso_nodeR]; Matrix mL = combinedMatrices[tmo_frame][tso_nodeL]; position = new Vector3((mR.M41 + mL.M41) / 2.0f, (mR.M42 + mL.M42) / 2.0f, -(mR.M43 + mL.M43) / 2.0f); } else { Console.WriteLine("bone not found. " + boneR); } } break; case 2://Leg { TSONode tso_nodeR; TSONode tso_nodeL; string boneR = "|W_Hips|W_RightHips_Dummy|W_RightUpLeg|W_RightUpLegRoll|W_RightLeg"; string boneL = "|W_Hips|W_LeftHips_Dummy|W_LeftUpLeg|W_LeftUpLegRoll|W_LeftLeg"; if (tso.nodemap.TryGetValue(boneR, out tso_nodeR) && tso.nodemap.TryGetValue(boneL, out tso_nodeL)) { TMOFrame tmo_frame = GetTMOFrame(); Matrix mR = combinedMatrices[tmo_frame][tso_nodeR]; Matrix mL = combinedMatrices[tmo_frame][tso_nodeL]; position = new Vector3((mR.M41 + mL.M41) / 2.0f, (mR.M42 + mL.M42) / 2.0f, -(mR.M43 + mL.M43) / 2.0f); } else { Console.WriteLine("bone not found. " + boneR); } } break; case 3://Foot { TSONode tso_nodeR; TSONode tso_nodeL; string boneR = "|W_Hips|W_RightHips_Dummy|W_RightUpLeg|W_RightUpLegRoll|W_RightLeg|W_RightLegRoll|W_RightFoot"; string boneL = "|W_Hips|W_LeftHips_Dummy|W_LeftUpLeg|W_LeftUpLegRoll|W_LeftLeg|W_LeftLegRoll|W_LeftFoot"; if (tso.nodemap.TryGetValue(boneR, out tso_nodeR) && tso.nodemap.TryGetValue(boneL, out tso_nodeL)) { TMOFrame tmo_frame = GetTMOFrame(); Matrix mR = combinedMatrices[tmo_frame][tso_nodeR]; Matrix mL = combinedMatrices[tmo_frame][tso_nodeL]; position = new Vector3((mR.M41 + mL.M41) / 2.0f, (mR.M42 + mL.M42) / 2.0f, -(mR.M43 + mL.M43) / 2.0f); } else { Console.WriteLine("bone not found. " + boneR); } } break; default: { TSONode tso_node; string bone = GetCenterBoneName(); if (tso.nodemap.TryGetValue(bone, out tso_node)) { TMOFrame tmo_frame = GetTMOFrame(); Matrix m = combinedMatrices[tmo_frame][tso_node]; position = new Vector3(m.M41, m.M42, -m.M43); } else { Console.WriteLine("bone not found. " + bone); } } break; } }