2 using System.Collections.Generic;
8 public unsafe class TSOGeneratorRefBone : TSOGenerator
10 public TSOGeneratorRefBone(TSOGeneratorConfig config)
16 List<Vertex> refverts;
20 void CreateRefVerts(TSOFile tso)
22 refverts = new List<Vertex>();
24 foreach (TSOMesh i in tso.meshes)
25 foreach (TSOSubMesh j in i.sub_meshes)
26 refverts.AddRange(j.vertices);
29 void CreatePointCluster()
31 pc = new PointCluster(refverts.Count);
33 foreach (Vertex i in refverts)
39 protected override bool DoLoadRefTSO(string path)
41 tsoref = LoadTSO(path);
42 tsoref.SwitchBoneIndicesOnMesh();
43 CreateRefVerts(tsoref);
48 protected override bool DoGenerateMeshes()
50 meshes = new List<TSOMesh>();
52 foreach (MqoObject obj in mqo.Objects)
54 if (obj.name.ToLower() == "bone")
57 Console.WriteLine("object:" + obj.name);
60 List<int> vref = new List<int>(obj.vertices.Count);
62 foreach (UVertex i in obj.vertices)
63 vref.Add(pc.NearestIndex(i.Pos.x, i.Pos.y, i.Pos.z));
67 List<int> faces_1 = new List<int>();
68 List<int> faces_2 = new List<int>();
69 Heap<int> bh = new Heap<int>();
70 Heap<Vertex> vh = new Heap<Vertex>();
71 Vertex[] refvs = new Vertex[3];
72 List<ushort> vert_indices = new List<ushort>();
73 Dictionary<int, bool> adding_bone_indices = new Dictionary<int, bool>();
74 List<TSOSubMesh> subs = new List<TSOSubMesh>();
76 for (int i = 0, n = obj.faces.Count; i < n; ++i)
80 Console.WriteLine(" vertices bone_indices");
81 Console.WriteLine(" -------- ------------");
83 while (faces_1.Count != 0)
85 int spec = obj.faces[faces_1[0]].spec;
90 foreach (int f in faces_1)
92 MqoFace face = obj.faces[f];
94 if (face.spec != spec)
100 refvs[0] = refverts[vref[face.a]];
101 refvs[1] = refverts[vref[face.b]];
102 refvs[2] = refverts[vref[face.c]];
104 adding_bone_indices.Clear();
106 for (int k = 0; k < 3; ++k)
108 UInt32 idx0 = refvs[k].Idx;
109 Point4 wgt0 = refvs[k].Wgt;
110 byte* idx = (byte*)(&idx0);
111 float* wgt = (float*)(&wgt0);
113 for (int l = 0; l < 4; ++l)
115 if (wgt[l] <= float.Epsilon)
117 if (bh.map.ContainsKey(idx[l]))
120 adding_bone_indices[idx[l]] = true;
124 if (bh.Count + adding_bone_indices.Count > 16)
130 foreach (int i in adding_bone_indices.Keys)
135 for (int k = 0; k < 3; ++k)
137 UInt32 idx0 = refvs[k].Idx;
138 Point4 wgt0 = refvs[k].Wgt;
139 byte* idx = (byte*)(&idx0);
140 float* wgt = (float*)(&wgt0);
142 for (int l = 0; l < 4; ++l)
144 if (wgt[l] <= float.Epsilon)
147 idx[l] = (byte)bh[idx[l]];
150 //refvs[k]は値型なのでrefvertsに影響しない。
154 Vertex va = new Vertex(obj.vertices[face.a].Pos, refvs[0].Wgt, refvs[0].Idx, obj.vertices[face.a].Nrm, new Point2(face.ta.x, 1 - face.ta.y));
155 Vertex vb = new Vertex(obj.vertices[face.b].Pos, refvs[1].Wgt, refvs[1].Idx, obj.vertices[face.b].Nrm, new Point2(face.tb.x, 1 - face.tb.y));
156 Vertex vc = new Vertex(obj.vertices[face.c].Pos, refvs[2].Wgt, refvs[2].Idx, obj.vertices[face.c].Nrm, new Point2(face.tc.x, 1 - face.tc.y));
158 vert_indices.Add(vh.Add(va));
159 vert_indices.Add(vh.Add(vc));
160 vert_indices.Add(vh.Add(vb));
163 ushort[] optimized_indices = NvTriStrip.Optimize(vert_indices.ToArray());
165 TSOSubMesh sub = new TSOSubMesh();
167 sub.numbones = bh.Count;
168 sub.bones = bh.ary.ToArray();
170 sub.numvertices = optimized_indices.Length;
171 Vertex[] vertices = new Vertex[optimized_indices.Length];
172 for (int i = 0; i < optimized_indices.Length; ++i)
174 vertices[i] = vh.ary[optimized_indices[i]];
176 sub.vertices = vertices;
178 Console.WriteLine(" {0,8} {1,12}", sub.vertices.Length, sub.bones.Length);
182 List<int> faces_tmp = faces_1;
188 TSOMesh mesh = new TSOMesh();
189 mesh.name = obj.name;
190 mesh.numsubs = subs.Count;
191 mesh.sub_meshes = subs.ToArray();
192 mesh.matrix = Matrix44.Identity;
200 protected override bool DoCleanup()
204 return base.DoCleanup();