Beispiel #1
0
        private static void ReadVertexBuffer(FSHP fshp, VertexBuffer vtx, FMDL model)
        {
            fshp.vertices.Clear();
            fshp.vertexAttributes.Clear();

            //Create a buffer instance which stores all the buffer data
            VertexBufferHelper helper = new VertexBufferHelper(vtx, Syroot.BinaryData.ByteOrder.BigEndian);

            //Set each array first from the lib if exist. Then add the data all in one loop
            Syroot.Maths.Vector4F[] vec4Positions = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4Normals   = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4uv0       = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4uv1       = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4uv2       = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4c0        = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4t0        = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4b0        = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4w0        = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4i0        = new Syroot.Maths.Vector4F[0];

            //For shape morphing
            Syroot.Maths.Vector4F[] vec4Positions1 = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4Positions2 = new Syroot.Maths.Vector4F[0];

            foreach (VertexAttrib att in vtx.Attributes.Values)
            {
                FSHP.VertexAttribute attr = new FSHP.VertexAttribute();
                attr.Name   = att.Name;
                attr.Format = attr.GetTypeWiiU(att.Format);

                if (att.Name == "_p0")
                {
                    vec4Positions = AttributeData(att, helper, "_p0");
                }
                if (att.Name == "_n0")
                {
                    vec4Normals = AttributeData(att, helper, "_n0");
                }
                if (att.Name == "_u0")
                {
                    vec4uv0 = AttributeData(att, helper, "_u0");
                }
                if (att.Name == "_u1")
                {
                    vec4uv1 = AttributeData(att, helper, "_u1");
                }
                if (att.Name == "_u2")
                {
                    vec4uv2 = AttributeData(att, helper, "_u2");
                }
                if (att.Name == "_c0")
                {
                    vec4c0 = AttributeData(att, helper, "_c0");
                }
                if (att.Name == "_t0")
                {
                    vec4t0 = AttributeData(att, helper, "_t0");
                }
                if (att.Name == "_b0")
                {
                    vec4b0 = AttributeData(att, helper, "_b0");
                }
                if (att.Name == "_w0")
                {
                    vec4w0 = AttributeData(att, helper, "_w0");
                }
                if (att.Name == "_i0")
                {
                    vec4i0 = AttributeData(att, helper, "_i0");
                }

                if (att.Name == "_p1")
                {
                    vec4Positions1 = AttributeData(att, helper, "_p1");
                }
                if (att.Name == "_p2")
                {
                    vec4Positions2 = AttributeData(att, helper, "_p2");
                }

                fshp.vertexAttributes.Add(attr);
            }
            for (int i = 0; i < vec4Positions.Length; i++)
            {
                Vertex v = new Vertex();
                if (vec4Positions.Length > 0)
                {
                    v.pos = new Vector3(vec4Positions[i].X, vec4Positions[i].Y, vec4Positions[i].Z);
                }
                if (vec4Positions1.Length > 0)
                {
                    v.pos1 = new Vector3(vec4Positions1[i].X, vec4Positions1[i].Y, vec4Positions1[i].Z);
                }
                if (vec4Positions2.Length > 0)
                {
                    v.pos2 = new Vector3(vec4Positions2[i].X, vec4Positions2[i].Y, vec4Positions2[i].Z);
                }
                if (vec4Normals.Length > 0)
                {
                    v.nrm = new Vector3(vec4Normals[i].X, vec4Normals[i].Y, vec4Normals[i].Z);
                }
                if (vec4uv0.Length > 0)
                {
                    v.uv0 = new Vector2(vec4uv0[i].X, vec4uv0[i].Y);
                }
                if (vec4uv1.Length > 0)
                {
                    v.uv1 = new Vector2(vec4uv1[i].X, vec4uv1[i].Y);
                }
                if (vec4uv2.Length > 0)
                {
                    v.uv2 = new Vector2(vec4uv2[i].X, vec4uv2[i].Y);
                }
                if (vec4w0.Length > 0)
                {
                    v.boneWeights.Add(vec4w0[i].X);
                    v.boneWeights.Add(vec4w0[i].Y);
                    v.boneWeights.Add(vec4w0[i].Z);
                    v.boneWeights.Add(vec4w0[i].W);
                }
                if (vec4i0.Length > 0)
                {
                    v.boneIds.Add((int)vec4i0[i].X);
                    v.boneIds.Add((int)vec4i0[i].Y);
                    v.boneIds.Add((int)vec4i0[i].Z);
                    v.boneIds.Add((int)vec4i0[i].W);
                }

                if (vec4t0.Length > 0)
                {
                    v.tan = new Vector4(vec4t0[i].X, vec4t0[i].Y, vec4t0[i].Z, vec4t0[i].W);
                }
                if (vec4b0.Length > 0)
                {
                    v.bitan = new Vector4(vec4b0[i].X, vec4b0[i].Y, vec4b0[i].Z, vec4b0[i].W);
                }
                if (vec4c0.Length > 0)
                {
                    v.col = new Vector4(vec4c0[i].X, vec4c0[i].Y, vec4c0[i].Z, vec4c0[i].W);
                }

                if (fshp.VertexSkinCount == 1)
                {
                    Matrix4 sb = model.Skeleton.bones[model.Skeleton.Node_Array[v.boneIds[0]]].Transform;
                    v.pos = Vector3.TransformPosition(v.pos, sb);
                    v.nrm = Vector3.TransformNormal(v.nrm, sb);
                }
                if (fshp.VertexSkinCount == 0)
                {
                    Matrix4 NoBindFix = model.Skeleton.bones[fshp.BoneIndex].Transform;
                    v.pos = Vector3.TransformPosition(v.pos, NoBindFix);
                    v.nrm = Vector3.TransformNormal(v.nrm, NoBindFix);
                }
                fshp.vertices.Add(v);
            }
        }
        private static void ReadVertexBuffer(FSHP fshp, VertexBuffer vtx, FMDL model)
        {
            fshp.vertices.Clear();
            fshp.vertexAttributes.Clear();

            //Create a buffer instance which stores all the buffer data
            VertexBufferHelper helper = new VertexBufferHelper(vtx, Syroot.BinaryData.ByteOrder.BigEndian);

            //Set each array first from the lib if exist. Then add the data all in one loop
            Syroot.Maths.Vector4F[] vec4Positions = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4Normals   = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4uv0       = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4uv1       = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4uv2       = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4c0        = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4t0        = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4b0        = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4w0        = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4i0        = new Syroot.Maths.Vector4F[0];

            //For shape morphing
            Syroot.Maths.Vector4F[] vec4Positions1 = new Syroot.Maths.Vector4F[0];
            Syroot.Maths.Vector4F[] vec4Positions2 = new Syroot.Maths.Vector4F[0];

            List <VertexAttrib> SortedList = vtx.Attributes.Values.OrderBy(o => o.BufferIndex).ToList();

            foreach (VertexAttrib att in SortedList)
            {
                Console.WriteLine($"{att.Name} {att.BufferIndex} {att.Offset} {att.Format} ");
            }

            foreach (VertexAttrib att in vtx.Attributes.Values)
            {
                FSHP.VertexAttribute attr = new FSHP.VertexAttribute();
                attr.Name        = att.Name;
                attr.Format      = attr.GetTypeWiiU(att.Format);
                attr.BufferIndex = att.BufferIndex;

                if (att.Name == "_p0")
                {
                    vec4Positions = AttributeData(att, helper, "_p0");
                }
                if (att.Name == "_n0")
                {
                    vec4Normals = AttributeData(att, helper, "_n0");
                }
                if (att.Name == "_u0")
                {
                    vec4uv0 = AttributeData(att, helper, "_u0");
                }
                if (att.Name == "_u1")
                {
                    vec4uv1 = AttributeData(att, helper, "_u1");
                }
                if (att.Name == "_u2")
                {
                    vec4uv2 = AttributeData(att, helper, "_u2");
                }
                if (att.Name == "_c0")
                {
                    vec4c0 = AttributeData(att, helper, "_c0");
                }
                if (att.Name == "_t0")
                {
                    vec4t0 = AttributeData(att, helper, "_t0");
                }
                if (att.Name == "_b0")
                {
                    vec4b0 = AttributeData(att, helper, "_b0");
                }
                if (att.Name == "_w0")
                {
                    vec4w0 = AttributeData(att, helper, "_w0");
                }
                if (att.Name == "_i0")
                {
                    vec4i0 = AttributeData(att, helper, "_i0");
                }

                if (att.Name == "_p1")
                {
                    vec4Positions1 = AttributeData(att, helper, "_p1");
                }
                if (att.Name == "_p2")
                {
                    vec4Positions2 = AttributeData(att, helper, "_p2");
                }

                fshp.vertexAttributes.Add(attr);
            }
            for (int i = 0; i < vec4Positions.Length; i++)
            {
                Vertex v = new Vertex();
                if (vec4Positions.Length > 0)
                {
                    v.pos = new Vector3(vec4Positions[i].X, vec4Positions[i].Y, vec4Positions[i].Z);
                }
                if (vec4Positions1.Length > 0)
                {
                    v.pos1 = new Vector3(vec4Positions1[i].X, vec4Positions1[i].Y, vec4Positions1[i].Z);
                }
                if (vec4Positions2.Length > 0)
                {
                    v.pos2 = new Vector3(vec4Positions2[i].X, vec4Positions2[i].Y, vec4Positions2[i].Z);
                }
                if (vec4Normals.Length > 0)
                {
                    v.nrm = new Vector3(vec4Normals[i].X, vec4Normals[i].Y, vec4Normals[i].Z);
                }
                if (vec4uv0.Length > 0)
                {
                    v.uv0 = new Vector2(vec4uv0[i].X, vec4uv0[i].Y);
                }
                if (vec4uv1.Length > 0)
                {
                    v.uv1 = new Vector2(vec4uv1[i].X, vec4uv1[i].Y);
                }
                if (vec4uv2.Length > 0)
                {
                    v.uv2 = new Vector2(vec4uv2[i].X, vec4uv2[i].Y);
                }
                if (vec4w0.Length > 0)
                {
                    v.boneWeights.Add(vec4w0[i].X);
                    v.boneWeights.Add(vec4w0[i].Y);
                    v.boneWeights.Add(vec4w0[i].Z);
                    v.boneWeights.Add(vec4w0[i].W);
                }
                if (vec4i0.Length > 0)
                {
                    v.boneIds.Add((int)vec4i0[i].X);
                    v.boneIds.Add((int)vec4i0[i].Y);
                    v.boneIds.Add((int)vec4i0[i].Z);
                    v.boneIds.Add((int)vec4i0[i].W);
                }

                if (vec4t0.Length > 0)
                {
                    v.tan = new Vector4(vec4t0[i].X, vec4t0[i].Y, vec4t0[i].Z, vec4t0[i].W);
                }
                if (vec4b0.Length > 0)
                {
                    v.bitan = new Vector4(vec4b0[i].X, vec4b0[i].Y, vec4b0[i].Z, vec4b0[i].W);
                }
                if (vec4c0.Length > 0)
                {
                    v.col = new Vector4(vec4c0[i].X, vec4c0[i].Y, vec4c0[i].Z, vec4c0[i].W);
                }

                if (fshp.VertexSkinCount == 1)
                {
                    int boneIndex = fshp.BoneIndex;
                    if (v.boneIds.Count > 0)
                    {
                        boneIndex = model.Skeleton.Node_Array[v.boneIds[0]];
                    }

                    //Check if the bones are a rigid type
                    //In game it seems to not transform if they are not rigid
                    if (model.Skeleton.bones[boneIndex].RigidMatrixIndex != -1)
                    {
                        Matrix4 sb = model.Skeleton.bones[boneIndex].Transform;
                        v.pos = Vector3.TransformPosition(v.pos, sb);
                        v.nrm = Vector3.TransformNormal(v.nrm, sb);
                    }
                }
                if (fshp.VertexSkinCount == 0)
                {
                    try
                    {
                        if (model.Skeleton.bones.Count > 0)
                        {
                            int boneIndex = fshp.BoneIndex;

                            Matrix4 NoBindFix = model.Skeleton.bones[boneIndex].Transform;
                            v.pos = Vector3.TransformPosition(v.pos, NoBindFix);
                            v.nrm = Vector3.TransformNormal(v.nrm, NoBindFix);
                        }
                    }
                    catch //Matrix failed. Print the coordinate data of the bone
                    {
                        Console.WriteLine(model.Skeleton.bones[fshp.BoneIndex].Text);
                        Console.WriteLine(model.Skeleton.bones[fshp.BoneIndex].GetPosition());
                        Console.WriteLine(model.Skeleton.bones[fshp.BoneIndex].GetRotation());
                        Console.WriteLine(model.Skeleton.bones[fshp.BoneIndex].GetScale());
                    }
                }
                fshp.vertices.Add(v);
            }
        }