public SkelAnimation BakeToSkel(VBN skel) { SkelAnimation a = new SkelAnimation(); for (int i = 1; i < anim.frameCount + 1; i++) { a.frames.Add(new KeyFrame()); } for (int i = 0; i < anim.nodes.Count; i++) { Frame[] frames = Bake(i); int fr = 0; foreach (Frame f in frames) { if (fr == 0) { fr++; continue; } if (i >= skel.bones.Count) { continue; } KeyFrame frame = a.frames[fr - 1]; KeyNode node = new KeyNode(); frame.nodes.Add(node); node.hash = skel.bones[i].boneId; node.tType = 1; node.rType = 1; node.sType = 1; node.t.X = f.x != -99 ? f.x : skel.bones[i].position[0]; node.t.Y = f.y != -99 ? f.y : skel.bones[i].position[1]; node.t.Z = f.z != -99 ? f.z : skel.bones[i].position[2]; node.r.X = f.rx != -99 ? f.rx * (float)Math.PI / 180 : skel.bones[i].rotation[0]; node.r.Y = f.ry != -99 ? f.ry * (float)Math.PI / 180 : skel.bones[i].rotation[1]; node.r.Z = f.rz != -99 ? f.rz * (float)Math.PI / 180 : skel.bones[i].rotation[2]; node.s.X = f.sx != -99 ? f.sx : skel.bones[i].scale[0]; node.s.Y = f.sy != -99 ? f.sy : skel.bones[i].scale[1]; node.s.Z = f.sz != -99 ? f.sz : skel.bones[i].scale[2]; node.r = VBN.FromEulerAngles(node.r.Z, node.r.Y, node.r.X); fr++; } } return(a); }
public void Apply(VBN bone) { for (int i = 0; i < nodes.Count; i++) { List <AnimType> used = new List <AnimType>(); Bone b = bone.bones[i]; Vector3 erot = ANIM.quattoeul(b.rot); foreach (DATAnimTrack track in nodes[i]) { KeyNode node = track.keys[0]; if (Debug) { Console.WriteLine("Bone " + i + " " + track.type + " " + node.value); } switch (track.type) { case AnimType.XPOS: b.pos.X = node.value; break; case AnimType.YPOS: b.pos.Y = node.value; break; case AnimType.ZPOS: b.pos.Z = node.value; break; case AnimType.XROT: erot.X = node.value; break; case AnimType.YROT: erot.Y = node.value; break; case AnimType.ZROT: erot.Z = node.value; break; } } b.rot = VBN.FromEulerAngles(erot.Z, erot.Y, erot.X); } bone.update(); }
public VBN GetVBN(FileData d) { VBN v = new VBN(); d.endian = Endianness.Big; d.Seek(8); int ver = d.ReadInt(); d.Skip(4); //outer offset to brres int boneHeader = 0x40; // for version 9 only int dlist = d.ReadInt(); int boneSec = d.ReadInt(); int vertSec = d.ReadInt(); int normSec = d.ReadInt(); int colrSec = d.ReadInt(); int texcSec = d.ReadInt(); d.Skip(8); int polySec = d.ReadInt(); d.Seek(0x40); d.Skip(16); int vertCount = d.ReadInt(); int faceCount = d.ReadInt(); d.Skip(4); int boneCount = d.ReadInt(); v.totalBoneCount = (uint)boneCount; for (int i = 0; i < 3; i++) { v.boneCountPerType[i + 1] = 0; } d.Skip(4); int bonetableoff = d.ReadInt() + boneHeader; d.Seek(bonetableoff); int bcount = d.ReadInt(); int[] nodeIndex = new int[bcount]; for (int i = 0; i < bcount; i++) { nodeIndex[i] = d.ReadInt(); } Random rng = new Random(); uint boneID = (uint)rng.Next(1, 0xFFFFFF); // BONES----------------------------------------------- d.Seek(boneSec); d.Skip(4); // length int bseccount = d.ReadInt(); for (int i = 0; i < bseccount; i++) { Debug.Write(i); d.Skip(4); // entry id and unknown d.Skip(4); // left and right index int name = d.ReadInt() + boneSec; int data = d.ReadInt() + boneSec; int temp = d.Pos(); if (name != boneSec && data != boneSec) { // read bone data d.Seek(data); d.Skip(8); int nameOff = d.ReadInt() + data; int index = d.ReadInt(); // id d.Skip(4); // index d.Skip(8); // idk billboard settings and padding Bone n = new Bone(v); n.scale = new float[3]; n.position = new float[3]; n.rotation = new float[3]; d.Skip(4); // index n.scale[0] = d.ReadFloat(); n.scale[1] = d.ReadFloat(); n.scale[2] = d.ReadFloat(); n.rotation[0] = toRadians(d.ReadFloat()); n.rotation[1] = toRadians(d.ReadFloat()); n.rotation[2] = toRadians(d.ReadFloat()); n.position[0] = d.ReadFloat(); n.position[1] = d.ReadFloat(); n.position[2] = d.ReadFloat(); n.pos = new Vector3(n.position[0], n.position[1], n.position[2]); n.sca = new Vector3(n.scale[0], n.scale[1], n.scale[2]); n.rot = (VBN.FromEulerAngles(n.rotation [2], n.rotation [1], n.rotation [0])); d.Skip(24); d.Seek(data + 0x5C); d.Seek(d.ReadInt() + data + 12); int parentid = 0x0FFFFFFF; if (d.Pos() != data + 12) { parentid = d.ReadInt(); } n.parentIndex = (int)parentid; n.Text = d.ReadString(nameOff, -1); n.boneId = boneID; boneID++; v.bones.Add(n); } else { bseccount++; } d.Seek(temp); } v.update(); //v.updateChildren(); v.boneCountPerType[0] = (uint)v.bones.Count; return(v); }
public void Read(string fname) { StreamReader reader = File.OpenText(fname); string line; string current = ""; bones = new VBN(); triangles = new List <SmdTriangle>(); Dictionary <int, Bone> boneList = new Dictionary <int, Bone>(); int time = 0; while ((line = reader.ReadLine()) != null) { line = Regex.Replace(line, @"\s+", " "); string[] args = line.Replace(";", "").TrimStart().Split(' '); if (args[0].Equals("triangles") || args[0].Equals("end") || args[0].Equals("skeleton") || args[0].Equals("nodes")) { current = args[0]; continue; } if (current.Equals("nodes")) { int id = int.Parse(args[0]); Bone b = new Bone(bones); b.Text = args[1].Replace('"', ' ').Trim(); int s = 2; while (args[s].Contains("\"")) { b.Text += args[s++]; } b.parentIndex = int.Parse(args[s]); boneList.Add(id, b); } if (current.Equals("skeleton")) { if (args[0].Contains("time")) { time = int.Parse(args[1]); } else { if (time == 0) { Bone b = boneList[int.Parse(args[0])]; b.position = new float[3]; b.rotation = new float[3]; b.scale = new float[3]; b.position[0] = float.Parse(args[1]); b.position[1] = float.Parse(args[2]); b.position[2] = float.Parse(args[3]); b.rotation[0] = float.Parse(args[4]); b.rotation[1] = float.Parse(args[5]); b.rotation[2] = float.Parse(args[6]); b.scale[0] = 1f; b.scale[1] = 1f; b.scale[2] = 1f; b.pos = new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3])); b.rot = VBN.FromEulerAngles(float.Parse(args[6]), float.Parse(args[5]), float.Parse(args[4])); bones.bones.Add(b); if (b.parentIndex != -1) { b.parentIndex = bones.bones.IndexOf(boneList[b.parentIndex]); } } } } if (current.Equals("triangles")) { string meshName = args[0]; if (args[0].Equals("")) { continue; } SmdTriangle t = new SmdTriangle(); triangles.Add(t); t.material = meshName; for (int j = 0; j < 3; j++) { line = reader.ReadLine(); line = Regex.Replace(line, @"\s+", " "); args = line.Replace(";", "").TrimStart().Split(' '); int parent = int.Parse(args[0]); SmdVertex vert = new SmdVertex(); vert.p = new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3])); vert.n = new Vector3(float.Parse(args[4]), float.Parse(args[5]), float.Parse(args[6])); vert.uv = new Vector2(float.Parse(args[7]), float.Parse(args[8])); vert.bones = new int[0]; vert.weights = new float[0]; if (args.Length > 9) { int wCount = int.Parse(args[9]); int w = 10; vert.bones = new int[wCount]; vert.weights = new float[wCount]; for (int i = 0; i < wCount; i++) { vert.bones[i] = (int.Parse(args[w++])); vert.weights[i] = (float.Parse(args[w++])); } } switch (j) { case 0: t.v1 = vert; break; case 1: t.v2 = vert; break; case 2: t.v3 = vert; break; } } } } bones.reset(); }
public static void Read(string fname, Animation a, VBN v) { StreamReader reader = File.OpenText(fname); string line; string current = ""; bool readBones = false; int frame = 0, prevframe = 0; KeyFrame k = new KeyFrame(); VBN vbn = v; if (v != null && v.bones.Count == 0) { readBones = true; } else { vbn = new VBN(); } while ((line = reader.ReadLine()) != null) { line = Regex.Replace(line, @"\s+", " "); string[] args = line.Replace(";", "").TrimStart().Split(' '); if (args[0].Equals("nodes") || args[0].Equals("skeleton") || args[0].Equals("end") || args[0].Equals("time")) { current = args[0]; if (args.Length > 1) { prevframe = frame; frame = int.Parse(args[1]); /*if (frame != prevframe + 1) { * Console.WriteLine ("Needs interpolation " + frame); * }*/ k = new KeyFrame(); k.frame = frame; //a.addKeyframe(k); } continue; } if (current.Equals("nodes")) { Bone b = new Bone(vbn); b.Text = args[1].Replace("\"", ""); b.parentIndex = int.Parse(args[2]); //b.children = new System.Collections.Generic.List<int> (); vbn.totalBoneCount++; vbn.bones.Add(b); Animation.KeyNode node = new Animation.KeyNode(b.Text); a.bones.Add(node); } if (current.Equals("time")) { //Animation.KeyFrame n = new Animation.KeyFrame(); /*n.id = v.boneIndex(vbn.bones[int.Parse(args[0])].Text); * if (n.id == -1) * { * continue; * } * else * n.hash = v.bones[n.id].boneId;*/ // only if it finds the node //k.addNode(n); // reading the skeleton if this isn't an animation if (readBones && frame == 0) { Bone b = vbn.bones[int.Parse(args[0])]; b.position = new float[3]; b.rotation = new float[3]; b.scale = new float[3]; b.position[0] = float.Parse(args[1]); b.position[1] = float.Parse(args[2]); b.position[2] = float.Parse(args[3]); b.rotation[0] = float.Parse(args[4]); b.rotation[1] = float.Parse(args[5]); b.rotation[2] = float.Parse(args[6]); b.scale[0] = 1f; b.scale[1] = 1f; b.scale[2] = 1f; b.pos = new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3])); b.rot = VBN.FromEulerAngles(float.Parse(args[6]), float.Parse(args[5]), float.Parse(args[4])); if (b.parentIndex != -1) { vbn.bones [b.parentIndex].Nodes.Add(b); } } Animation.KeyNode bone = a.GetBone(vbn.bones[int.Parse(args[0])].Text); bone.rotType = Animation.RotationType.Euler; Animation.KeyFrame n = new Animation.KeyFrame(); n.Value = float.Parse(args[1]); n.Frame = frame; bone.xpos.keys.Add(n); n = new Animation.KeyFrame(); n.Value = float.Parse(args[2]); n.Frame = frame; bone.ypos.keys.Add(n); n = new Animation.KeyFrame(); n.Value = float.Parse(args[3]); n.Frame = frame; bone.zpos.keys.Add(n); n = new Animation.KeyFrame(); n.Value = float.Parse(args[4]); n.Frame = frame; bone.xrot.keys.Add(n); n = new Animation.KeyFrame(); n.Value = float.Parse(args[5]); n.Frame = frame; bone.yrot.keys.Add(n); n = new Animation.KeyFrame(); n.Value = float.Parse(args[6]); n.Frame = frame; bone.zrot.keys.Add(n); if (args.Length > 7) { n = new Animation.KeyFrame(); n.Value = float.Parse(args[7]); n.Frame = frame; bone.xsca.keys.Add(n); n = new Animation.KeyFrame(); n.Value = float.Parse(args[8]); n.Frame = frame; bone.ysca.keys.Add(n); n = new Animation.KeyFrame(); n.Value = float.Parse(args[9]); n.Frame = frame; bone.zsca.keys.Add(n); } } } a.frameCount = frame; vbn.boneCountPerType[0] = (uint)vbn.bones.Count; vbn.update(); }