public void Write(Syroot.Maths.Vector4F v)
 {
     Write(v.X);
     Write(v.Y);
     Write(v.Z);
     Write(v.W);
 }
示例#2
0
 private Syroot.Maths.Vector4F ConvertIndices(List <int> vector)
 {
     Syroot.Maths.Vector4F buffer = new Syroot.Maths.Vector4F();
     for (int i = 0; i < Math.Min(VertexSkinCount, vector.Count); i++)
     {
         buffer[i] = vector[i];
     }
     return(buffer);
 }
示例#3
0
        private Syroot.Maths.Vector4F ConvertWeights(List <float> weights)
        {
            //The total should be 1.0
            float total = weights.Sum(x => x);

            //Find the difference of the total and 1.0
            float dif = 1.0f - total;

            if (dif != 0)
            {
                //Divide the difference to force values as 1.0
                for (int i = 0; i < weights.Count; i++)
                {
                    weights[i] = weights[i] / dif;
                }
            }

            Syroot.Maths.Vector4F buffer = new Syroot.Maths.Vector4F();
            for (int i = 0; i < Math.Min(VertexSkinCount, 4); i++)
            {
                buffer[i] = weights.Count < i ? weights[i] : 0;
            }
            return(buffer);
        }
示例#4
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);
            }
        }
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="shp"></param>
        /// <param name="vtx"></param>
        /// <param name="model"></param>
        private static void WriteVertexBuffer(XmlWriter writer, Shape shp, VertexBuffer vtx, ResU.Model model, JPSkeleton jPSkeleton)
        {
            //Create a buffer instance which stores all the buffer data
            VertexBufferHelper helper = new VertexBufferHelper(vtx, Syroot.BinaryData.ByteOrder.BigEndian); // TODO if this supports Switch files, you will need to re-look at this

            //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[] vec4c1        = 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 vtx.Attributes.Values)
            {
                if (att.Name == "_p0")
                {
                    vec4Positions = AttributeData(att, helper, "_p0");
                }
                else if (att.Name == "_n0")
                {
                    vec4Normals = AttributeData(att, helper, "_n0");
                }
                else if (att.Name == "_u0")
                {
                    vec4uv0 = AttributeData(att, helper, "_u0");
                }
                else if (att.Name == "_u1")
                {
                    vec4uv1 = AttributeData(att, helper, "_u1");
                }
                else if (att.Name == "_u2")
                {
                    vec4uv2 = AttributeData(att, helper, "_u2");
                }
                else if (att.Name == "_c0")
                {
                    vec4c0 = AttributeData(att, helper, "_c0");
                }
                else if (att.Name == "_c1")
                {
                    vec4c1 = AttributeData(att, helper, "_c1");
                }
                else if (att.Name == "_t0")
                {
                    vec4t0 = AttributeData(att, helper, "_t0");
                }
                else if (att.Name == "_b0")
                {
                    vec4b0 = AttributeData(att, helper, "_b0");
                }
                else if (att.Name == "_w0")
                {
                    vec4w0 = AttributeData(att, helper, "_w0");
                }
                else if (att.Name == "_i0")
                {
                    vec4i0 = AttributeData(att, helper, "_i0");
                }

                else if (att.Name == "_p1")
                {
                    vec4Positions1 = AttributeData(att, helper, "_p1");
                }
                else if (att.Name == "_p2")
                {
                    vec4Positions2 = AttributeData(att, helper, "_p2");
                }
                else
                {
                    Program.AssertAndLog(Program.ErrorType.eUnhandledVertexAttrType, false, $"Vertex Attribute type {att.Name} not handled.");
                }
            }
            List <Vertex> vertices = new List <Vertex>();

            for (int i = 0; i < vec4Positions.Length; i++)
            {
                Vertex v = new Vertex();
                if (vec4Positions.Length > 0)
                {
                    v.pos = new OpenTK.Vector3(vec4Positions[i].X, vec4Positions[i].Y, vec4Positions[i].Z);
                }
                if (vec4Positions1.Length > 0)
                {
                    v.pos1 = new OpenTK.Vector3(vec4Positions1[i].X, vec4Positions1[i].Y, vec4Positions1[i].Z);
                }
                if (vec4Positions2.Length > 0)
                {
                    v.pos2 = new OpenTK.Vector3(vec4Positions2[i].X, vec4Positions2[i].Y, vec4Positions2[i].Z);
                }
                if (vec4Normals.Length > 0)
                {
                    v.nrm = new OpenTK.Vector3(vec4Normals[i].X, vec4Normals[i].Y, vec4Normals[i].Z);
                }
                if (vec4uv0.Length > 0)
                {
                    v.uv0 = new OpenTK.Vector2(vec4uv0[i].X, vec4uv0[i].Y);
                }
                if (vec4uv1.Length > 0)
                {
                    v.uv1 = new OpenTK.Vector2(vec4uv1[i].X, vec4uv1[i].Y);
                }
                if (vec4uv2.Length > 0)
                {
                    v.uv2 = new OpenTK.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 OpenTK.Vector4(vec4t0[i].X, vec4t0[i].Y, vec4t0[i].Z, vec4t0[i].W);
                }
                if (vec4b0.Length > 0)
                {
                    v.bitan = new OpenTK.Vector4(vec4b0[i].X, vec4b0[i].Y, vec4b0[i].Z, vec4b0[i].W);
                }
                if (vec4c0.Length > 0)
                {
                    v.col = new OpenTK.Vector4(vec4c0[i].X, vec4c0[i].Y, vec4c0[i].Z, vec4c0[i].W);
                }
                if (vec4c1.Length > 0)
                {
                    v.col2 = new OpenTK.Vector4(vec4c1[i].X, vec4c1[i].Y, vec4c1[i].Z, vec4c1[i].W);
                }


                int[] boneList = new int[model.Skeleton.MatrixToBoneList.Count];
                int   uiBone   = 0;
                foreach (ushort node in model.Skeleton.MatrixToBoneList)
                {
                    boneList[uiBone] = node;
                    uiBone++;
                }
                if (shp.VertexSkinCount == 1)
                {
                    int boneIndex = shp.BoneIndex;
                    if (v.boneIds.Count > 0)
                    {
                        boneIndex = boneList[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)
                    {
                        OpenTK.Matrix4 absTransform = jPSkeleton.bones[boneIndex].Transform;

                        v.pos = OpenTK.Vector3.TransformPosition(v.pos, absTransform);
                        v.nrm = OpenTK.Vector3.TransformNormal(v.nrm, absTransform);
                    }
                }
                if (shp.VertexSkinCount == 0)
                {
                    try
                    {
                        if (model.Skeleton.Bones.Count > 0)
                        {
                            int boneIndex = shp.BoneIndex;


                            // Create the Transform Matrix4
                            OpenTK.Vector3 mPos = new OpenTK.Vector3(
                                model.Skeleton.Bones[boneIndex].Position.X,
                                model.Skeleton.Bones[boneIndex].Position.Y,
                                model.Skeleton.Bones[boneIndex].Position.Z);

                            OpenTK.Quaternion mRot;
                            OpenTK.Vector3    v3 = new OpenTK.Vector3(
                                model.Skeleton.Bones[boneIndex].Rotation.X,
                                model.Skeleton.Bones[boneIndex].Rotation.Y,
                                model.Skeleton.Bones[boneIndex].Rotation.Z
                                );
                            if (model.Skeleton.Bones[boneIndex].FlagsRotation == BoneFlagsRotation.Quaternion)
                            {
                                mRot = new OpenTK.Quaternion(v3, model.Skeleton.Bones[boneIndex].Rotation.W);
                            }
                            else if (model.Skeleton.Bones[boneIndex].FlagsRotation == BoneFlagsRotation.EulerXYZ)
                            {
                                mRot = new OpenTK.Quaternion(v3);
                            }
                            else
                            {
                                mRot = new OpenTK.Quaternion();
                            }
                            OpenTK.Vector3 mSca = new OpenTK.Vector3(
                                model.Skeleton.Bones[boneIndex].Scale.X,
                                model.Skeleton.Bones[boneIndex].Scale.Y,
                                model.Skeleton.Bones[boneIndex].Scale.Z);

                            OpenTK.Matrix4 NoBindFix = OpenTK.Matrix4.CreateScale(mSca) * OpenTK.Matrix4.CreateFromQuaternion(mRot) * OpenTK.Matrix4.CreateTranslation(mPos);
                            v.pos = OpenTK.Vector3.TransformPosition(v.pos, NoBindFix);
                            v.nrm = OpenTK.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());
                    }
                }

                vertices.Add(v);
            }

            // Write Vertex Data
            writer.WriteStartElement("Vertices");
            writer.WriteAttributeString("VertexCount", vtx.VertexCount.ToString());
            for (int i = 0; i < vertices.Count; i++)
            {
                writer.WriteStartElement("Vertex");

                writer.WriteAttributeString("Index", i.ToString());
                writer.WriteAttributeString("Position0", Program.Vector3ToString(vertices[i].pos));
                writer.WriteAttributeString("Position1", Program.Vector3ToString(vertices[i].pos1)); Program.AssertAndLog(Program.ErrorType.eVertexPosSet, vertices[i].pos1 == OpenTK.Vector3.Zero, $"Vertex index {i} pos1 is set to {vertices[i].pos1} and not 0");     // Add C++ support
                writer.WriteAttributeString("Position2", Program.Vector3ToString(vertices[i].pos2)); Program.AssertAndLog(Program.ErrorType.eVertexPosSet, vertices[i].pos2 == OpenTK.Vector3.Zero, $"Vertex index {i} pos2 is set to {vertices[i].pos2} and not 0");     // Add C++ support
                writer.WriteAttributeString("Normal", Program.Vector3ToString(vertices[i].nrm));
                writer.WriteAttributeString("UV0", Program.Vector2ToString(vertices[i].uv0));
                writer.WriteAttributeString("UV1", Program.Vector2ToString(vertices[i].uv1));
                writer.WriteAttributeString("UV2", Program.Vector2ToString(vertices[i].uv2));
                writer.WriteAttributeString("Color0", Program.Vector4ToString(vertices[i].col));
                writer.WriteAttributeString("Color1", Program.Vector4ToString(vertices[i].col2)); Program.AssertAndLog(Program.ErrorType.eVertexPosSet, vertices[i].col2 == OpenTK.Vector4.One, $"Vertex index {i} col2 is set to {vertices[i].col2} and not One");        // Add C++ support. Unknown use of col2
                writer.WriteAttributeString("Tangent", Program.Vector4ToString(vertices[i].tan));
                writer.WriteAttributeString("Binormal", Program.Vector4ToString(vertices[i].bitan));

                string tempBoneWeights = "";
                foreach (var w in vertices[i].boneWeights)
                {
                    tempBoneWeights += (w.ToString() + ',');
                }
                tempBoneWeights = tempBoneWeights.Trim(',');
                writer.WriteAttributeString("BlendWeights", tempBoneWeights);

                string tempBoneIds = "";
                foreach (var w in vertices[i].boneIds)
                {
                    tempBoneIds += (w.ToString() + ',');
                }
                tempBoneIds = tempBoneIds.Trim(',');
                writer.WriteAttributeString("BlendIndex", tempBoneIds);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }
示例#6
0
 public static string DataToString(Syroot.Maths.Vector4F v)
 {
     return($"{v.X},{v.Y},{v.Z} {v.W}");
 }
示例#7
0
 public static Vector4 ToVec4(Syroot.Maths.Vector4F v)
 {
     return(new Vector4(v.X, v.Y, v.Z, v.W));
 }
示例#8
0
        public void Load(Mesh mesh)
        {
            Mesh = mesh;

            Text = mesh.Name;

            lodMeshes = new List <LOD_Mesh>();
            foreach (var group in Shape.FaceGroups)
            {
                LOD_Mesh msh = new LOD_Mesh();

                foreach (var faceDescriptors in group.FaceDescriptors)
                {
                    foreach (var buffer in faceDescriptors.Buffers)
                    {
                        msh.PrimitiveType = STPolygonType.Triangle;
                        msh.FirstVertex   = 0;

                        uint[] indicesArray = buffer.GetIndices().ToArray();
                        for (int face = 0; face < indicesArray.Length; face++)
                        {
                            msh.faces.Add((int)indicesArray[face] + (int)msh.FirstVertex);
                        }
                    }
                }

                lodMeshes.Add(msh);
            }

            for (int vtxGrp = 0; vtxGrp < Shape.VertexGroups.Count; vtxGrp++)
            {
                if (Shape.VertexGroups[vtxGrp].VertexBufferInterleaved != null)
                {
                    var FaceGroup = Shape.FaceGroups[vtxGrp];

                    int BoneIndex = 0;
                    if (FaceGroup.BoneIndexList != null && FaceGroup.BoneIndexList.Length > 0)
                    {
                        BoneIndex = (int)FaceGroup.BoneIndexList[0];
                    }

                    STSkeleton skeleton = new STSkeleton();
                    if (ParentModelWrapper.Skeleton != null && ParentModelWrapper.Skeleton.Renderable != null)
                    {
                        skeleton = ParentModelWrapper.Skeleton.Renderable;
                    }

                    var buffer = Shape.VertexGroups[vtxGrp].VertexBufferInterleaved;

                    //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[] vec4w0        = new Syroot.Maths.Vector4F[0];
                    Syroot.Maths.Vector4F[] vec4i0        = new Syroot.Maths.Vector4F[0];

                    foreach (var attribute in buffer.Attributes)
                    {
                        switch (attribute.AttributeType)
                        {
                        case GfxAttributeType.Position: vec4Positions = attribute.Data; break;

                        case GfxAttributeType.Normal: vec4Normals = attribute.Data; break;

                        case GfxAttributeType.TextureCoordinate0: vec4uv0 = attribute.Data; break;

                        case GfxAttributeType.TextureCoordinate1: vec4uv1 = attribute.Data; break;

                        case GfxAttributeType.TextureCoordinate2: vec4uv2 = attribute.Data; break;

                        case GfxAttributeType.Tangent: vec4t0 = attribute.Data; break;

                        case GfxAttributeType.Color: vec4c0 = attribute.Data; break;

                        case GfxAttributeType.BoneWeight: vec4w0 = attribute.Data; break;

                        case GfxAttributeType.BoneIndex: vec4i0 = attribute.Data; break;
                        }
                    }

                    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 (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 (vec4c0.Length > 0)
                        {
                            v.col = new Vector4(vec4c0[i].X, vec4c0[i].Y, vec4c0[i].Z, vec4c0[i].W);
                        }

                        if (FaceGroup.SkinnningMode == SkinnningMode.Rigid)
                        {
                            if (v.boneIds.Count > 0)
                            {
                                Matrix4 sb = skeleton.bones[(int)FaceGroup.BoneIndexList[v.boneIds[0]]].Transform;
                                v.pos = Vector3.TransformPosition(v.pos, sb);
                                v.nrm = Vector3.TransformNormal(v.nrm, sb);
                            }
                        }
                        if (FaceGroup.SkinnningMode == SkinnningMode.None)
                        {
                            if (skeleton.bones.Count > 0)
                            {
                                Matrix4 NoBindFix = ParentModelWrapper.Skeleton.Renderable.bones[BoneIndex].Transform;
                                v.pos = Vector3.TransformPosition(v.pos, NoBindFix);
                                v.nrm = Vector3.TransformNormal(v.nrm, NoBindFix);
                            }
                        }
                        vertices.Add(v);
                    }
                }
            }
        }
示例#9
0
 public static OpenTK.Vector4 ToTKVector4(this Syroot.Maths.Vector4F vec4)
 {
     return(new OpenTK.Vector4(vec4.X, vec4.Y, vec4.Z, vec4.W));
 }
示例#10
0
 /// <summary>
 /// Returns string in a format of "X,Y,Z,W" without parentheses.
 /// </summary>
 /// <param name="vec4"></param>
 /// <returns></returns>
 public static string Vector4FToString(Syroot.Maths.Vector4F vec4)
 {
     return(vec4.X.ToString() + "," + vec4.Y.ToString() + "," + vec4.Z.ToString() + "," + vec4.W.ToString());
 }
示例#11
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];

            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);
            }
        }
示例#12
0
        private void ReadVertexBuffer(Model mdl, Shape shp, Mesh poly, FMDL_Model model)
        {
            //Create a buffer instance which stores all the buffer data
            VertexBufferHelper helper = new VertexBufferHelper(mdl.VertexBuffers[shp.VertexBufferIndex], TargetSwitchBFRES.ByteOrder);

            //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 mdl.VertexBuffers[shp.VertexBufferIndex].Attributes)
            {
                Mesh.VertexAttribute attr = new Mesh.VertexAttribute();
                attr.Name   = att.Name;
                attr.Format = 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");
                }

                poly.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 (poly.VertexSkinCount == 1)
                {
                    Matrix4 sb = model.skeleton.bones[model.Node_Array[v.boneIds[0]]].transform;
                    //  Console.WriteLine(model.skeleton.bones[model.Node_Array[v.boneIds[0]]].Text);
                    v.pos = Vector3.TransformPosition(v.pos, sb);
                    v.nrm = Vector3.TransformNormal(v.nrm, sb);
                }
                if (poly.VertexSkinCount == 0)
                {
                    Matrix4 NoBindFix = model.skeleton.bones[poly.boneIndx].transform;
                    v.pos = Vector3.TransformPosition(v.pos, NoBindFix);
                    v.nrm = Vector3.TransformNormal(v.nrm, NoBindFix);
                }

                poly.vertices.Add(v);
            }
        }
示例#13
0
            public void ReplaceMatchingFile( )
            {
                OpenFileDialog ofd = new OpenFileDialog();

                ofd.DefaultExt = ReplaceFilter;

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string extension = System.IO.Path.GetExtension(ofd.FileName);

                    if (extension == ".bfskl")
                    {
                        if (SkeletonU != null)
                        {
                            ResU.Skeleton SkeltonTemp = new ResU.Skeleton();
                            SkeltonTemp.Import(ofd.FileName, GetResFileU());

                            foreach (BfresBone bone in fskl.bones)
                            {
                                if (SkeltonTemp.Bones.ContainsKey(bone.Text))
                                {
                                    bone.CopyData(SkeltonTemp.Bones[bone.Text]);
                                }
                            }

                            BfresWiiU.ReadSkeleton(this, SkeletonU, fskl);
                        }
                        else
                        {
                            Skeleton SkeltonTemp = new Skeleton();
                            SkeltonTemp.Import(ofd.FileName);

                            foreach (BfresBone bone in fskl.bones)
                            {
                                if (SkeltonTemp.BoneDict.ContainsKey(bone.Text))
                                {
                                    int index = SkeltonTemp.BoneDict.GetIndex(bone.Text);
                                    bone.CopyData(SkeltonTemp.Bones[index]);
                                }
                            }

                            BfresSwitch.ReadSkeleton(this, Skeleton, fskl);
                        }
                    }
                    if (extension == ".csv")
                    {
                        using (var reader = new System.IO.StringReader(System.IO.File.ReadAllText(ofd.FileName)))
                        {
                            string value = reader.ReadLine();
                            if (value != "Bones Geometry")
                            {
                                return;
                            }

                            float X = 0;
                            float Y = 0;
                            float Z = 0;
                            float W = 0;
                            while (true)
                            {
                                string line = reader.ReadLine();
                                if (line != null)
                                {
                                    foreach (BfresBone bone in fskl.bones)
                                    {
                                        if (bone.Text == line)
                                        {
                                            string name           = line;
                                            string scaleStr       = reader.ReadLine();
                                            string rotationStr    = reader.ReadLine();
                                            string translationStr = reader.ReadLine();

                                            string[] valuesS = scaleStr.Replace("\n", "").Replace("\r", "").Split(',');
                                            string[] valuesR = rotationStr.Replace("\n", "").Replace("\r", "").Split(',');
                                            string[] valuesT = translationStr.Replace("\n", "").Replace("\r", "").Split(',');

                                            Syroot.Maths.Vector3F translate;
                                            Syroot.Maths.Vector3F scale;
                                            Syroot.Maths.Vector4F rotate;

                                            float.TryParse(valuesT[0], out X);
                                            float.TryParse(valuesT[1], out Y);
                                            float.TryParse(valuesT[2], out Z);
                                            translate = new Syroot.Maths.Vector3F(X, Y, Z);

                                            float.TryParse(valuesR[0], out X);
                                            float.TryParse(valuesR[1], out Y);
                                            float.TryParse(valuesR[2], out Z);
                                            float.TryParse(valuesR[3], out W);
                                            rotate = new Syroot.Maths.Vector4F(X, Y, Z, W);

                                            float.TryParse(valuesS[0], out X);
                                            float.TryParse(valuesS[1], out Y);
                                            float.TryParse(valuesS[2], out Z);
                                            scale = new Syroot.Maths.Vector3F(X, Y, Z);

                                            if (bone.BoneU != null)
                                            {
                                                bone.BoneU.Position = translate;
                                                bone.BoneU.Scale    = scale;
                                                bone.BoneU.Rotation = rotate;
                                            }
                                            else
                                            {
                                                bone.Bone.Position = translate;
                                                bone.Bone.Scale    = scale;
                                                bone.Bone.Rotation = rotate;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }

                            if (SkeletonU != null)
                            {
                                BfresWiiU.ReadSkeleton(this, SkeletonU, fskl);
                            }
                            else
                            {
                                BfresSwitch.ReadSkeleton(this, Skeleton, fskl);
                            }

                            LibraryGUI.UpdateViewport();
                        }
                    }
                }
            }
示例#14
0
        public static void WiiU2Switch(string FileName, int CurModel, BFRES b)
        {
            ResFile TargetWiiUBFRES = new ResFile(FileName);



            int CurMdl = 0;

            foreach (Model mdl in TargetWiiUBFRES.Models.Values)
            {
                int CurBn = 0;
                foreach (Syroot.NintenTools.Bfres.Bone bn in mdl.Skeleton.Bones.Values)
                {
                    Bone bone = b.models[CurMdl].skeleton.bones[CurBn];

                    bone.scale[0]    = bn.Scale.X;
                    bone.scale[1]    = bn.Scale.Y;
                    bone.scale[2]    = bn.Scale.Z;
                    bone.rotation[0] = bn.Rotation.X;
                    bone.rotation[1] = bn.Rotation.Y;
                    bone.rotation[2] = bn.Rotation.Z;
                    bone.rotation[3] = bn.Rotation.W;
                    bone.position[0] = bn.Position.X;
                    bone.position[1] = bn.Position.Y;
                    bone.position[2] = bn.Position.Z;
                    CurBn++;
                }
                b.models[CurMdl].skeleton.reset();

                int CurShape = 0;
                foreach (Shape shp in mdl.Shapes.Values)
                {
                    Mesh poly = b.models[CurMdl].poly[CurShape];

                    //Create a buffer instance which stores all the buffer data
                    // VertexBufferHelperAttrib uv1 = helper["_u1"];

                    int TotalCount = poly.vertices.Count;

                    int LODCount = 0;

                    uint   FaceCount    = FaceCount = shp.Meshes[LODCount].IndexCount;
                    uint[] indicesArray = shp.Meshes[LODCount].GetIndices().ToArray();


                    int TotalFaceCount = poly.lodMeshes[poly.DisplayLODIndex].faces.Count;

                    poly.lodMeshes[poly.DisplayLODIndex].faces.Clear();

                    for (int face = 0; face < FaceCount; face++)
                    {
                        poly.lodMeshes[poly.DisplayLODIndex].faces.Add((int)indicesArray[face] + (int)shp.Meshes[LODCount].FirstVertex);
                    }

                    if (TotalFaceCount != poly.lodMeshes[poly.DisplayLODIndex].faces.Count)
                    {
                        MessageBox.Show("Error F");
                    }

                    poly.vertices.Clear();


                    //Create a buffer instance which stores all the buffer data
                    VertexBufferHelper helper = new VertexBufferHelper(mdl.VertexBuffers[shp.VertexBufferIndex], TargetWiiUBFRES.ByteOrder);

                    //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];


                    foreach (VertexAttrib att in mdl.VertexBuffers[shp.VertexBufferIndex].Attributes.Values)
                    {
                        Mesh.VertexAttribute attr = new Mesh.VertexAttribute();
                        attr.Name = att.Name;
                        // attr.Format = att.Format;

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

                        poly.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 (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);
                        }

                        poly.vertices.Add(v);
                    }

                    CurShape++;
                }
                CurMdl++;
            }
            b.UpdateVertexData();
        }
示例#15
0
        public void Read(ResFile TargetWiiUBFRES)
        {
            Nodes.Add(TModels);
            Nodes.Add(TTextures);
            Nodes.Add(TShaderparam);
            Nodes.Add(TColoranim);
            Nodes.Add(TTextureSRT);
            Nodes.Add(TTexturePat);
            Nodes.Add(TBonevisabilty);
            Nodes.Add(TVisualAnim);
            Nodes.Add(TShapeAnim);
            Nodes.Add(TSceneAnim);
            Nodes.Add(TEmbedded);
            ImageKey         = "bfres";
            SelectedImageKey = "bfres";

            FSKACount = TargetWiiUBFRES.SkeletalAnims.Count;
            FTXPCount = TargetWiiUBFRES.TexPatternAnims.Count;
            FSHUCount = TargetWiiUBFRES.ColorAnims.Count + TargetWiiUBFRES.TexSrtAnims.Count + TargetWiiUBFRES.ShaderParamAnims.Count;

            AnimationCountTotal = FSKACount + FTXPCount + FSHUCount;

            FTEXContainer = new FTEXContainer();
            foreach (Texture tex in TargetWiiUBFRES.Textures.Values)
            {
                string TextureName = tex.Name;
                FTEX   texture     = new FTEX();
                texture.ReadFTEX(tex);

                TTextures.Nodes.Add(texture);

                FTEXContainer.FTEXtextures.Add(texture.Text, texture);
                Runtime.FTEXContainerList.Add(FTEXContainer);
            }

            int ModelCur = 0;

            //FMDLs -Models-
            foreach (Model mdl in TargetWiiUBFRES.Models.Values)
            {
                FMDL_Model model = new FMDL_Model(); //This will store VBN data and stuff
                model.Text = mdl.Name;

                TModels.Nodes.Add(model);

                model.Node_Array = new int[mdl.Skeleton.MatrixToBoneList.Count];
                int nodes = 0;
                foreach (ushort node in mdl.Skeleton.MatrixToBoneList)
                {
                    model.Node_Array[nodes] = node;
                    nodes++;
                }

                foreach (Syroot.NintenTools.Bfres.Bone bn in mdl.Skeleton.Bones.Values)
                {
                    Bone bone = new Bone(model.skeleton);
                    bone.Text        = bn.Name;
                    bone.boneId      = bn.BillboardIndex;
                    bone.parentIndex = bn.ParentIndex;
                    bone.scale       = new float[3];
                    bone.rotation    = new float[4];
                    bone.position    = new float[3];

                    if (bn.FlagsRotation == BoneFlagsRotation.Quaternion)
                    {
                        bone.boneRotationType = 1;
                    }
                    else
                    {
                        bone.boneRotationType = 0;
                    }

                    bone.scale[0]    = bn.Scale.X;
                    bone.scale[1]    = bn.Scale.Y;
                    bone.scale[2]    = bn.Scale.Z;
                    bone.rotation[0] = bn.Rotation.X;
                    bone.rotation[1] = bn.Rotation.Y;
                    bone.rotation[2] = bn.Rotation.Z;
                    bone.rotation[3] = bn.Rotation.W;
                    bone.position[0] = bn.Position.X;
                    bone.position[1] = bn.Position.Y;
                    bone.position[2] = bn.Position.Z;

                    model.skeleton.bones.Add(bone);
                }
                model.skeleton.reset();
                model.skeleton.update();

                //MeshTime!!
                int ShapeCur = 0;
                foreach (Shape shp in mdl.Shapes.Values)
                {
                    Mesh poly = new Mesh();
                    poly.Text            = shp.Name;
                    poly.MaterialIndex   = shp.MaterialIndex;
                    poly.VertexSkinCount = shp.VertexSkinCount;
                    poly.boneIndx        = shp.BoneIndex;
                    poly.fmdlIndx        = ModelCur;

                    foreach (int bn in shp.SkinBoneIndices)
                    {
                        if (!poly.BoneIndexList.ContainsKey(model.skeleton.bones[bn].Text))
                        {
                            poly.BoneIndexList.Add(model.skeleton.bones[bn].Text, bn);
                        }
                    }

                    TModels.Nodes[ModelCur].Nodes.Add(poly);

                    //Create a buffer instance which stores all the buffer data
                    VertexBufferHelper helper = new VertexBufferHelper(mdl.VertexBuffers[shp.VertexBufferIndex], TargetWiiUBFRES.ByteOrder);

                    //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];


                    foreach (VertexAttrib att in mdl.VertexBuffers[shp.VertexBufferIndex].Attributes.Values)
                    {
                        Mesh.VertexAttribute attr = new Mesh.VertexAttribute();
                        attr.Name = att.Name;
                        // attr.Format = att.Format;

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

                        poly.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 (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 (poly.VertexSkinCount == 1)
                        {
                            Matrix4 sb = model.skeleton.bones[model.Node_Array[v.boneIds[0]]].transform;
                            //  Console.WriteLine(model.skeleton.bones[model.Node_Array[v.boneIds[0]]].Text);
                            v.pos = Vector3.TransformPosition(v.pos, sb);
                            v.nrm = Vector3.TransformNormal(v.nrm, sb);
                        }
                        if (poly.VertexSkinCount == 0)
                        {
                            Matrix4 NoBindFix = model.skeleton.bones[poly.boneIndx].transform;
                            v.pos = Vector3.TransformPosition(v.pos, NoBindFix);
                            v.nrm = Vector3.TransformNormal(v.nrm, NoBindFix);
                        }

                        poly.vertices.Add(v);
                    }

                    //shp.Meshes.Count - 1 //For going to the lowest poly LOD mesh


                    poly.BoundingCount = shp.SubMeshBoundings.Count;

                    int CurLOD = 0;
                    foreach (var lod in shp.Meshes)
                    {
                        Mesh.LOD_Mesh lodmsh = new Mesh.LOD_Mesh();
                        lodmsh.index = CurLOD++;

                        uint   FaceCount    = lod.IndexCount;
                        uint[] indicesArray = lod.GetIndices().ToArray();

                        for (int face = 0; face < FaceCount; face++)
                        {
                            lodmsh.faces.Add((int)indicesArray[face] + (int)lod.FirstVertex);
                        }

                        poly.lodMeshes.Add(lodmsh);
                    }

                    foreach (Bounding bnd in shp.SubMeshBoundings)
                    {
                        Mesh.BoundingBox box = new Mesh.BoundingBox();
                        box.Center = new Vector3(bnd.Center.X, bnd.Center.Y, bnd.Center.Z);
                        box.Extent = new Vector3(bnd.Extent.X, bnd.Extent.Y, bnd.Extent.Z);

                        poly.boundingBoxes.Add(box); //Each box is by LOD mesh. This will be in a seperate class later so only one will be added
                    }
                    foreach (float r in shp.RadiusArray)
                    {
                        poly.radius.Add(r);
                    }

                    // Read materials
                    Material mat = mdl.Materials[shp.MaterialIndex];

                    int SampIndex = 0;
                    foreach (var smp in mat.Samplers)
                    {
                        poly.material.Samplers.Add(smp.Key, SampIndex);
                        SampIndex++;
                    }

                    int AlbedoCount = 0;

                    string TextureName = "";

                    MaterialData.ShaderAssign shaderassign = new MaterialData.ShaderAssign();

                    if (mat.ShaderAssign != null) //Some special cases (env models) have none
                    {
                        shaderassign.ShaderModel   = mat.ShaderAssign.ShadingModelName;
                        shaderassign.ShaderArchive = mat.ShaderAssign.ShaderArchiveName;


                        int o = 0;
                        foreach (var op in mat.ShaderAssign.ShaderOptions)
                        {
                            shaderassign.options.Add(op.Key, mat.ShaderAssign.ShaderOptions[o]);
                            o++;
                        }

                        int sa = 0;
                        foreach (var smp in mat.ShaderAssign.SamplerAssigns)
                        {
                            shaderassign.samplers.Add(smp.Key, mat.ShaderAssign.SamplerAssigns[sa]);
                            sa++;
                        }

                        int va = 0;
                        foreach (var att in mat.ShaderAssign.AttribAssigns)
                        {
                            shaderassign.attributes.Add(att.Key, mat.ShaderAssign.AttribAssigns[va]);
                            va++;
                        }
                    }

                    poly.material.shaderassign = shaderassign;

                    int id = 0;
                    foreach (TextureRef tex in mdl.Materials[shp.MaterialIndex].TextureRefs)
                    {
                        TextureName = tex.Name;

                        MatTexture texture = new MatTexture();

                        texture.wrapModeS = (int)mdl.Materials[shp.MaterialIndex].Samplers[id].TexSampler.ClampX;
                        texture.wrapModeT = (int)mdl.Materials[shp.MaterialIndex].Samplers[id].TexSampler.ClampY;


                        bool IsAlbedo = HackyTextureList.Any(TextureName.Contains);

                        if (mdl.Materials[shp.MaterialIndex].Samplers[id].Name == "_a0")
                        {
                            poly.material.HasDiffuseMap = true;
                            texture.hash = 0;
                            texture.Type = MatTexture.TextureType.Diffuse;
                        }
                        if (mdl.Materials[shp.MaterialIndex].Samplers[id].Name == "_a1")
                        {
                            poly.material.HasDiffuseLayer = true;
                            texture.hash = 19;
                            texture.Type = MatTexture.TextureType.DiffuseLayer2;
                        }
                        if (mdl.Materials[shp.MaterialIndex].Samplers[id].Name == "_n0")
                        {
                            texture.hash = 1;
                            poly.material.HasNormalMap = true;
                            texture.Type = MatTexture.TextureType.Normal;
                        }
                        if (mdl.Materials[shp.MaterialIndex].Samplers[id].Name == "_s0")
                        {
                            texture.hash = 4;
                            poly.material.HasSpecularMap = true;
                            texture.Type = MatTexture.TextureType.Specular;
                        }
                        if (mdl.Materials[shp.MaterialIndex].Samplers[id].Name == "_b0")
                        {
                            texture.hash = 2;
                            poly.material.HasShadowMap = true;
                            texture.Type = MatTexture.TextureType.Shadow;
                        }
                        if (mdl.Materials[shp.MaterialIndex].Samplers[id].Name == "_b1")
                        {
                            texture.hash = 3;
                            poly.material.HasLightMap = true;
                            texture.Type = MatTexture.TextureType.Light;
                        }
                        if (mdl.Materials[shp.MaterialIndex].Samplers[id].Name == "_e0")
                        {
                            texture.hash = 8;
                            poly.material.HasEmissionMap = true;
                            texture.Type = MatTexture.TextureType.Emission;
                        }

                        texture.Name = TextureName;
                        poly.material.textures.Add(texture);
                        id++;
                    }

                    foreach (Sampler smp in mat.Samplers.Values)
                    {
                        SamplerInfo s = new SamplerInfo();
                        s.WrapModeU = (int)smp.TexSampler.ClampX;
                        s.WrapModeV = (int)smp.TexSampler.ClampY;
                        s.WrapModeW = (int)smp.TexSampler.ClampZ;
                        poly.material.samplerinfo.Add(s);
                    }

                    poly.material.Name = mdl.Materials[shp.MaterialIndex].Name;
                    if (mdl.Materials[shp.MaterialIndex].ShaderParamData != null) //Some special cases (env models) have none
                    {
                        using (Syroot.BinaryData.BinaryDataReader reader = new Syroot.BinaryData.BinaryDataReader(new MemoryStream(mdl.Materials[shp.MaterialIndex].ShaderParamData)))
                        {
                            reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian;
                            foreach (Syroot.NintenTools.Bfres.ShaderParam param in mdl.Materials[shp.MaterialIndex].ShaderParams.Values)
                            {
                                ShaderParam prm = new ShaderParam();

                                prm.Type = param.Type;
                                prm.Name = param.Name;

                                switch (param.Type)
                                {
                                case ShaderParamType.Float:
                                    reader.Seek(param.DataOffset, SeekOrigin.Begin);
                                    prm.Value_float = reader.ReadSingle();
                                    break;

                                case ShaderParamType.Float2:
                                    reader.Seek(param.DataOffset, SeekOrigin.Begin);
                                    prm.Value_float2 = new Vector2(
                                        reader.ReadSingle(),
                                        reader.ReadSingle());
                                    break;

                                case ShaderParamType.Float3:
                                    reader.Seek(param.DataOffset, SeekOrigin.Begin);
                                    prm.Value_float3 = new Vector3(
                                        reader.ReadSingle(),
                                        reader.ReadSingle(),
                                        reader.ReadSingle()); break;

                                case ShaderParamType.Float4:
                                    reader.Seek(param.DataOffset, SeekOrigin.Begin);
                                    prm.Value_float4 = new Vector4(
                                        reader.ReadSingle(),
                                        reader.ReadSingle(),
                                        reader.ReadSingle(),
                                        reader.ReadSingle()); break;

                                case ShaderParamType.TexSrt:
                                    reader.Seek(param.DataOffset, SeekOrigin.Begin);
                                    ShaderParam.TextureSRT texSRT = new ShaderParam.TextureSRT();
                                    texSRT.Mode      = reader.ReadSingle(); //Scale mode, Maya, max ect
                                    texSRT.scale     = new Vector2(reader.ReadSingle(), reader.ReadSingle());
                                    texSRT.rotate    = reader.ReadSingle();
                                    texSRT.translate = new Vector2(reader.ReadSingle(), reader.ReadSingle());

                                    prm.Value_TexSrt = texSRT; break;
                                }
                                poly.material.matparam.Add(param.Name, prm);
                            }
                            reader.Close();
                        }
                    }


                    model.poly.Add(poly);
                    ShapeCur++;
                }
                models.Add(model);
                ModelCur++;
            }
        }