Exemplo n.º 1
0
        public static void ReadModel(FMDL model, Model mdl)
        {
            if (model == null)
            {
                model = new FMDL();
            }
            model.Text     = mdl.Name;
            model.Skeleton = new FSKL(mdl.Skeleton);

            model.Nodes.RemoveAt(2);
            model.Nodes.Add(model.Skeleton.node);

            model.ModelU = mdl;
            foreach (Material mat in mdl.Materials.Values)
            {
                FMAT FMAT = new FMAT();
                FMAT.Text = mat.Name;
                FMAT.ReadMaterial(mat);
                model.Nodes["FmatFolder"].Nodes.Add(FMAT);
                model.materials.Add(FMAT.Text, FMAT);
            }
            foreach (Shape shp in mdl.Shapes.Values)
            {
                VertexBuffer vertexBuffer = mdl.VertexBuffers[shp.VertexBufferIndex];
                Material     material     = mdl.Materials[shp.MaterialIndex];
                FSHP         mesh         = new FSHP();
                ReadShapesVertices(mesh, shp, vertexBuffer, model);
                mesh.MaterialIndex = shp.MaterialIndex;

                model.Nodes["FshpFolder"].Nodes.Add(mesh);
                model.shapes.Add(mesh);
            }
        }
Exemplo n.º 2
0
        private static void ApplyTransformFix(FMDL fmdl, FSHP m, SF.Shader shader)
        {
            Matrix4 idenity = Matrix4.Identity;

            shader.SetInt("NoSkinning", 0);
            shader.SetInt("RigidSkinning", 0);
            shader.SetInt("SingleBoneIndex", m.BoneIndex);

            shader.SetMatrix4x4("SingleBoneBindTransform", ref idenity);
            //Some objects will have no weights or indices. These will weigh to the bone index in the shape section.

            if (m.VertexSkinCount == 1)
            {
                shader.SetInt("RigidSkinning", 1);
            }
            if (m.VertexSkinCount == 0)
            {
                if (fmdl.Skeleton.bones.Count > 0)
                {
                    Matrix4 transform = fmdl.Skeleton.bones[m.BoneIndex].invert * fmdl.Skeleton.bones[m.BoneIndex].Transform;

                    shader.SetMatrix4x4("SingleBoneBindTransform", ref transform);
                    shader.SetInt("NoSkinning", 1);
                }
            }
        }
Exemplo n.º 3
0
 private static void SetBoneUniforms(Shader shader, FMDL fmdl)
 {
     for (int i = 0; i < fmdl.Skeleton.Node_Array.Length; i++)
     {
         Matrix4 transform = fmdl.Skeleton.bones[fmdl.Skeleton.Node_Array[i]].invert * fmdl.Skeleton.bones[fmdl.Skeleton.Node_Array[i]].transform;
         GL.UniformMatrix4(GL.GetUniformLocation(shader.Id, String.Format("bones[{0}]", i)), false, ref transform);
     }
 }
        public static Model SetModel(FMDL fmdl)
        {
            Model model = new Model();

            model.Name = fmdl.Text;
            model.Path = "";

            model.Shapes        = new List <Shape>();
            model.VertexBuffers = new List <VertexBuffer>();
            model.Materials     = new List <Material>();
            model.UserData      = new List <UserData>();
            model.Skeleton      = new Skeleton();
            model.Skeleton      = fmdl.Skeleton.node.Skeleton;
            model.ShapeDict     = new ResDict();
            model.MaterialDict  = new ResDict();
            model.UserDataDict  = new ResDict();
            model.UserData      = fmdl.Model.UserData;
            model.UserDataDict  = fmdl.Model.UserDataDict;

            if (model.Skeleton.InverseModelMatrices == null)
            {
                model.Skeleton.InverseModelMatrices = new List <Syroot.Maths.Matrix3x4>();
            }
            if (model.Skeleton.MatrixToBoneList == null)
            {
                model.Skeleton.MatrixToBoneList = new List <ushort>();
            }

            fmdl.Skeleton.CalculateIndices();

            int i          = 0;
            var duplicates = fmdl.shapes.GroupBy(c => c.Text).Where(g => g.Skip(1).Any()).SelectMany(c => c);

            foreach (var shape in duplicates)
            {
                shape.Text += i++;
            }

            foreach (FSHP shape in fmdl.shapes)
            {
                BFRES.CheckMissingTextures(shape);
                SetShape(shape, shape.Shape);

                model.Shapes.Add(shape.Shape);
                model.VertexBuffers.Add(shape.VertexBuffer);
                shape.Shape.VertexBufferIndex = (ushort)(model.VertexBuffers.Count - 1);

                //  BFRES.SetShaderAssignAttributes(shape.GetMaterial().shaderassign, shape);
            }
            foreach (FMAT mat in fmdl.materials.Values)
            {
                SetMaterial(mat, mat.Material);
                model.Materials.Add(mat.Material);
            }
            return(model);
        }
Exemplo n.º 5
0
        private void DrawModel(FSHP m, FMDL mdl, Shader shader, bool drawSelection = false)
        {
            if (m.lodMeshes[m.DisplayLODIndex].faces.Count <= 3)
            {
                return;
            }

            SetUniforms(m.GetMaterial(), shader, m, m.DisplayId);
            SetTextureUniforms(m.GetMaterial(), m, shader);
            SetBoneUniforms(shader, mdl);
            SetVertexAttributes(m, shader);
            ApplyTransformFix(mdl, m, shader);

            if (m.Checked && m.Parent.Parent.Checked)
            {
                if ((m.IsSelected))
                {
                    DrawModelSelection(m, shader);
                }
                else
                {
                    if (Runtime.RenderModelWireframe)
                    {
                        DrawModelWireframe(m, shader);
                    }
                    if (Runtime.RenderModels)
                    {
                        PrimitiveType primitiveType = PrimitiveType.Triangles;

                        switch (m.lodMeshes[m.DisplayLODIndex].PrimitiveType)
                        {
                        case STPolygonType.Line:
                            primitiveType = PrimitiveType.Lines;
                            break;

                        case STPolygonType.LineStrip:
                            primitiveType = PrimitiveType.LineStrip;
                            break;

                        case STPolygonType.Point:
                            primitiveType = PrimitiveType.Points;
                            break;

                        case STPolygonType.Triangle:
                            primitiveType = PrimitiveType.Triangles;
                            break;
                        }

                        GL.DrawElements(primitiveType, m.lodMeshes[m.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, m.Offset);
                    }
                }
            }
        }
Exemplo n.º 6
0
        private static void SetBoneUniforms(Shader shader, FMDL fmdl)
        {
            for (int i = 0; i < fmdl.Skeleton.Node_Array.Length; i++)
            {
                /*         Matrix3x4 transform3x4 = fmdl.Skeleton.matrices[fmdl.Skeleton.Node_Array[i]];
                 *              Matrix4 transform = new Matrix4(transform3x4.Row0, transform3x4.Row1,
                 *                                                  transform3x4.Row2, new Vector4(0, 0, 0, 1));*/

                Matrix4 transform = fmdl.Skeleton.bones[fmdl.Skeleton.Node_Array[i]].invert * fmdl.Skeleton.bones[fmdl.Skeleton.Node_Array[i]].transform;
                GL.UniformMatrix4(GL.GetUniformLocation(shader.Id, String.Format("bones[{0}]", i)), false, ref transform);
            }
        }
Exemplo n.º 7
0
        private static void SetBoneUniforms(ShaderProgram shader, FMDL fmdl, FSHP fshp)
        {
            for (int i = 0; i < fmdl.Skeleton.Node_Array.Length; i++)
            {
                //   if (!fmdl.Skeleton.bones[fmdl.Skeleton.Node_Array[i]].Visible)
                //    fshp.Checked = false;


                GL.Uniform1(GL.GetUniformLocation(shader.program, String.Format("boneIds[{0}]", i)), fmdl.Skeleton.Node_Array[i]);

                Matrix4 transform = fmdl.Skeleton.bones[fmdl.Skeleton.Node_Array[i]].invert * fmdl.Skeleton.bones[fmdl.Skeleton.Node_Array[i]].Transform;
                GL.UniformMatrix4(GL.GetUniformLocation(shader.program, String.Format("bones[{0}]", i)), false, ref transform);
            }
        }
Exemplo n.º 8
0
        public void LoadSubFile(FMDL fmdl)
        {
            activeModel = fmdl;

            if (fmdl.ModelU != null)
            {
                stPropertyGrid1.LoadProperty(fmdl.ModelU, OnPropertyChanged);
                userDataEditor1.LoadUserData(fmdl.ModelU.UserData);
            }
            else
            {
                userDataEditor1.LoadUserData(fmdl.Model.UserData);
                stPropertyGrid1.LoadProperty(fmdl.Model, OnPropertyChanged);
            }
        }
Exemplo n.º 9
0
        public void NewModel()
        {
            BFRESGroupNode group = new BFRESGroupNode();
            FMDL           anim  = new FMDL();

            if (IsWiiU)
            {
                BfresWiiU.ReadModel(anim, new ResU.Model());
            }
            else
            {
                BfresSwitch.ReadModel(anim, new ResNX.Model());
            }

            group.AddNode(anim, "NewModel");
        }
Exemplo n.º 10
0
        public static Model SetModel(FMDL fmdl)
        {
            Model model = new Model();

            model.Name = fmdl.Text;
            model.Path = "";

            model.Shapes        = new ResDict <Shape>();
            model.VertexBuffers = new List <VertexBuffer>();
            model.Materials     = new ResDict <Material>();
            model.UserData      = new ResDict <UserData>();
            model.Skeleton      = new Skeleton();
            model.Skeleton      = fmdl.Skeleton.node.SkeletonU;
            model.UserData      = fmdl.ModelU.UserData;

            fmdl.Skeleton.CalculateIndices();

            int i          = 0;
            var duplicates = fmdl.shapes.GroupBy(c => c.Text).Where(g => g.Skip(1).Any()).SelectMany(c => c);

            foreach (var shape in duplicates)
            {
                shape.Text += i++;
            }

            foreach (FSHP shape in fmdl.shapes)
            {
                BFRES.CheckMissingTextures(shape);
                SetShape(shape, shape.ShapeU);

                model.Shapes.Add(shape.Text, shape.ShapeU);
                model.VertexBuffers.Add(shape.VertexBufferU);
                shape.ShapeU.VertexBufferIndex = (ushort)(model.VertexBuffers.Count - 1);

                //   BFRES.SetShaderAssignAttributes(shape.GetMaterial().shaderassign, shape);
            }

            foreach (FMAT mat in fmdl.materials.Values)
            {
                SetMaterial(mat, mat.MaterialU, fmdl.GetResFileU());
                model.Materials.Add(mat.Text, mat.MaterialU);
            }
            return(model);
        }
Exemplo n.º 11
0
        static List <DisplayVertex> CreateDisplayVertices(FMDL model, FSHP mesh)
        {
            // rearrange faces
            mesh.display = mesh.lodMeshes[mesh.DisplayLODIndex].getDisplayFace().ToArray();

            List <DisplayVertex> displayVertList = new List <DisplayVertex>();

            if (mesh.lodMeshes[mesh.DisplayLODIndex].faces.Count <= 3)
            {
                return(displayVertList);
            }

            foreach (Vertex v in mesh.vertices)
            {
                model.MaxPosition = OpenGLUtils.GetMax(model.MaxPosition, v.pos);
                model.MinPosition = OpenGLUtils.GetMin(model.MinPosition, v.pos);

                DisplayVertex displayVert = new DisplayVertex()
                {
                    Position   = v.pos,
                    Normal     = v.nrm,
                    Color0     = v.col,
                    Color1     = v.col2,
                    TexCoord0  = v.uv0,
                    TexCoord1  = v.uv1,
                    TexCoord2  = v.uv2,
                    BlendIndex = new Vector4(
                        v.boneIds.Count > 0 ? v.boneIds[0] : -1,
                        v.boneIds.Count > 1 ? v.boneIds[1] : -1,
                        v.boneIds.Count > 2 ? v.boneIds[2] : -1,
                        v.boneIds.Count > 3 ? v.boneIds[3] : -1),
                    BlendWeight = new Vector4(
                        v.boneWeights.Count > 0 ? v.boneWeights[0] : 0,
                        v.boneWeights.Count > 1 ? v.boneWeights[1] : 0,
                        v.boneWeights.Count > 2 ? v.boneWeights[2] : 0,
                        v.boneWeights.Count > 3 ? v.boneWeights[3] : 0),
                };

                displayVertList.Add(displayVert);
            }

            return(displayVertList);
        }
        public void LoadObjects(FMDL fmdl)
        {
            ActiveFMDL = fmdl;

            objectList.Items.Clear();
            foreach (var shape in fmdl.shapes)
            {
                ListViewItem item = new ListViewItem();
                item.Text = shape.Text;
                foreach (var attribute in shape.vertexAttributes)
                {
                    item.SubItems.Add(attribute.Name);
                }

                objectList.Items.Add(shape.Text);
            }
            objectList.Items[0].Selected = true;
            objectList.Select();
        }
Exemplo n.º 13
0
        public static void Read(BFRESRender renderer, ResFile resFile, TreeNode ResFileNode)
        {
            int CurMdl = 0;

            foreach (Model mdl in resFile.Models.Values)
            {
                FMDL model = new FMDL();
                model.Text     = mdl.Name;
                model.Skeleton = new FSKL(mdl.Skeleton);
                model.Nodes.Add(model.Skeleton.node);
                model.Skeleton.reset();
                model.Skeleton.update();
                model.BFRESRender = renderer;
                model.Skeleton.node.BFRESRender = renderer;
                model.ModelU = mdl;
                foreach (Material mat in mdl.Materials.Values)
                {
                    FMAT FMAT = new FMAT();
                    FMAT.BFRESRender = renderer;
                    FMAT.Text        = mat.Name;
                    FMAT.ReadMaterial(mat);
                    model.Nodes[1].Nodes.Add(FMAT);
                    model.materials.Add(FMAT.Text, FMAT);
                }
                foreach (Shape shp in mdl.Shapes.Values)
                {
                    VertexBuffer vertexBuffer = mdl.VertexBuffers[shp.VertexBufferIndex];
                    Material     material     = mdl.Materials[shp.MaterialIndex];
                    FSHP         mesh         = new FSHP();
                    mesh.BFRESRender = renderer;
                    mesh.ModelIndex  = CurMdl;
                    ReadShapesVertices(mesh, shp, vertexBuffer, model);
                    mesh.MaterialIndex = shp.MaterialIndex;

                    model.Nodes[0].Nodes.Add(mesh);
                    model.shapes.Add(mesh);
                }
                ResFileNode.Nodes[0].Nodes.Add(model);
                renderer.models.Add(model);

                CurMdl++;
            }
        }
Exemplo n.º 14
0
        public static void ReadShapesVertices(FSHP fshp, Shape shp, VertexBuffer vertexBuffer, FMDL model)
        {
            fshp.boundingBoxes.Clear();
            fshp.boundingRadius.Clear();
            fshp.BoneIndices.Clear();

            foreach (Bounding bnd in shp.SubMeshBoundings)
            {
                FSHP.BoundingBox box = new FSHP.BoundingBox();
                box.Center = new Vector3(bnd.Center.X, bnd.Center.Y, bnd.Center.Z);
                box.Extend = new Vector3(bnd.Extent.X, bnd.Extent.Y, bnd.Extent.Z);
                fshp.boundingBoxes.Add(box);
            }
            foreach (float rad in shp.RadiusArray)
            {
                fshp.boundingRadius.Add(rad);
            }

            fshp.VertexBufferIndex = shp.VertexBufferIndex;
            fshp.ShapeU            = shp;
            fshp.VertexBufferU     = vertexBuffer;
            fshp.VertexSkinCount   = shp.VertexSkinCount;
            fshp.BoneIndex         = shp.BoneIndex;
            fshp.Text = shp.Name;
            fshp.TargetAttribCount = shp.TargetAttribCount;
            fshp.MaterialIndex     = shp.MaterialIndex;

            if (shp.SkinBoneIndices != null)
            {
                foreach (ushort bn in shp.SkinBoneIndices)
                {
                    fshp.BoneIndices.Add(bn);
                }
            }

            ReadMeshes(fshp, shp);
            ReadVertexBuffer(fshp, vertexBuffer, model);
        }
Exemplo n.º 15
0
        private void DrawModel(FSHP m, FMDL mdl, SF.Shader shader, bool ModelSelected)
        {
            if (m.lodMeshes[m.DisplayLODIndex].faces.Count <= 3)
            {
                return;
            }

            var mat = m.GetFMAT();

            if (shader != OpenTKSharedResources.shaders["BFRES_Normals"])
            {
                bool useVertexColors = true;
                if (mat.shaderassign.ShaderArchive == "Park_UBER")
                {
                    useVertexColors = false;
                }

                SetRenderSettings(shader, useVertexColors);
                SetRenderPass(mat);
                SetUniforms(mat, shader, m, m.DisplayId);
                SetTextureUniforms(mat, m, shader);
            }
            SetBoneUniforms(shader, mdl, m);
            ApplyTransformFix(mdl, m, shader);
            SetVertexAttributes(m, shader);

            //Check the binded bone if it's visible from bone visual anims
            //     if (!mdl.Skeleton.bones[m.boneIndx].Visible)
            //         m.Checked = false;

            if (m.Checked && mdl.Skeleton.bones.Count > 0 && mdl.Skeleton.bones[m.BoneIndex].Visible && mat.Enabled)
            {
                shader.SetVector3("materialSelectColor", new Vector3(0));
                if (m.GetMaterial().IsSelected)
                {
                    shader.SetVector3("materialSelectColor", ColorUtility.ToVector3(Color.FromArgb(0, 163, 204)));
                    DrawModelSelection(m, shader);
                }
                else if (m.IsSelected || ModelSelected)
                {
                    DrawModelSelection(m, shader);
                }
                else
                {
                    if (Runtime.RenderModelWireframe)
                    {
                        DrawModelWireframe(m, shader);
                    }
                    if (Runtime.RenderModels)
                    {
                        DrawMdoelHoverSelection(m, shader, IsSelected(), Hovered);

                        PrimitiveType primitiveType = PrimitiveType.Triangles;

                        switch (m.lodMeshes[m.DisplayLODIndex].PrimativeType)
                        {
                        case STPrimitiveType.Lines:
                            primitiveType = PrimitiveType.Lines;
                            break;

                        case STPrimitiveType.LineStrips:
                            primitiveType = PrimitiveType.LineStrip;
                            break;

                        case STPrimitiveType.Points:
                            primitiveType = PrimitiveType.Points;
                            break;

                        case STPrimitiveType.Triangles:
                            primitiveType = PrimitiveType.Triangles;
                            break;
                        }

                        GL.DrawElements(primitiveType, m.lodMeshes[m.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, m.Offset);
                    }
                }
            }
        }
Exemplo n.º 16
0
        private void DrawModel(GL_ControlBase control, FSHP m, FMDL mdl, bool ModelSelected)
        {
            if (m.lodMeshes[m.DisplayLODIndex].faces.Count <= 3)
            {
                return;
            }

            var mat = m.GetFMAT();

            SHShaderProgram shader = SearchProgram(
                mat.shaderassign.ShaderArchive,
                mat.shaderassign.ShaderModel);

            if (shader == null)
            {
                return;
            }

            shader.Enable();
            shader.EnableVertexAttributes();

            GL.Enable(EnableCap.AlphaTest);
            GL.AlphaFunc(AlphaFunction.Gequal, 0.1f);

            //   SetRenderPass(mat);
            //    SetUniforms(mat, shader, m, m.DisplayId);
            //   SetTextureUniforms(mat, m, shader);

            shader.LoadUniforms(mdl, m, mdl.Skeleton, control);

            SetVertexAttributes(m, shader);

            if (m.Checked && mdl.Skeleton.bones.Count > 0 && mdl.Skeleton.bones[m.BoneIndex].Visible && mat.Enabled)
            {
                PrimitiveType primitiveType = PrimitiveType.Triangles;

                switch (m.lodMeshes[m.DisplayLODIndex].PrimativeType)
                {
                case STPrimitiveType.Lines:
                    primitiveType = PrimitiveType.Lines;
                    break;

                case STPrimitiveType.LineStrips:
                    primitiveType = PrimitiveType.LineStrip;
                    break;

                case STPrimitiveType.Points:
                    primitiveType = PrimitiveType.Points;
                    break;

                case STPrimitiveType.Triangles:
                    primitiveType = PrimitiveType.Triangles;
                    break;
                }

                GL.DrawElements(primitiveType, m.lodMeshes[m.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, m.Offset);
            }

            shader.DisableVertexAttributes();
            shader.Disable();
        }
Exemplo n.º 17
0
        public void LoadShape(FSHP fshp)
        {
            InitializeControls();

            activeShape = fshp;

            shapeVertexSkinCountUD.Value = (decimal)fshp.VertexSkinCount;

            FMDL fmdl = fshp.GetParentModel();

            //Load info
            nameTB.Bind(fshp, "Text");
            shapeIndexUD.Value = fmdl.shapes.IndexOf(fshp);

            //Load meshes to toggle LOD display
            for (int i = 0; i < fshp.lodMeshes.Count; i++)
            {
                lodDisplayCB.Items.Add($"mesh {i}");
            }
            lodDisplayCB.SelectedIndex = activeShape.DisplayLODIndex;

            //Load material (all materials will load when activated)
            materialComboBox1.Items.Add(fshp.GetMaterial().Text);
            materialComboBox1.SelectedIndex = 0;

            //Load bone binded (all bones will load when activated)
            bonesCB.Items.Add(fmdl.Skeleton.bones[fshp.BoneIndex].Text);
            bonesCB.SelectedIndex = 0;

            //Load transform
            GetShapeTransform();

            if (fshp.VertexBufferU != null)
            {
                vertexBufferSkinCountUD.Maximum = (decimal)fshp.VertexBufferU.VertexSkinCount;
                vertexBufferSkinCountUD.Value   = (decimal)fshp.VertexBufferU.VertexSkinCount;
                vertexBufferList1.LoadVertexBuffers(fshp, fshp.VertexBufferU);
            }
            else
            {
                vertexBufferSkinCountUD.Maximum = (decimal)fshp.VertexBuffer.VertexSkinCount;
                vertexBufferSkinCountUD.Value   = (decimal)fshp.VertexBuffer.VertexSkinCount;
                vertexBufferList1.LoadVertexBuffers(fshp, fshp.VertexBuffer);
            }

            vtxCountUD.Maximum = fshp.vertices.Count;
            vtxCountUD.Value   = fshp.vertices.Count;


            if (fshp.ShapeU != null)
            {
                keyShapeList1.LoadKeyShapes(fshp.ShapeU.KeyShapes);
            }
            else
            {
                keyShapeList1.LoadKeyShapes(fshp.Shape.KeyShapes, fshp.Shape.KeyShapeDict);
            }

            int lodIndx = 0;

            foreach (var mesh in fshp.lodMeshes)
            {
                lodListView.Items.Add($"Detail Level {lodIndx++}");
            }

            IsLoaded = true;
        }
Exemplo n.º 18
0
 public void LoadUniforms(FMDL fmdl, FSHP shp, STSkeleton skeleton, GL_EditorFramework.GL_Core.GL_ControlBase control)
 {
 }
Exemplo n.º 19
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);
            }
        }
Exemplo n.º 20
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);
            }
        }
Exemplo n.º 21
0
        private void DrawModel(FSHP m, FMDL mdl, ShaderProgram shader, bool drawSelection)
        {
            if (m.lodMeshes[m.DisplayLODIndex].faces.Count <= 3)
            {
                return;
            }

            if (shader != normalsShaderProgram)
            {
                SetRenderPass(m.GetMaterial(), shader, m, m.DisplayId);
                SetUniforms(m.GetMaterial(), shader, m, m.DisplayId);
                SetTextureUniforms(m.GetMaterial(), m, shader);
            }
            SetBoneUniforms(shader, mdl, m);
            ApplyTransformFix(mdl, m, shader);
            SetVertexAttributes(m, shader);

            //Check the binded bone if it's visible from bone visual anims
            //     if (!mdl.Skeleton.bones[m.boneIndx].Visible)
            //         m.Checked = false;

            if (m.Checked && mdl.Skeleton.bones.Count > 0 && mdl.Skeleton.bones[m.BoneIndex].Visible)
            {
                shader.SetVector3("materialSelectColor", new Vector3(0));
                if (m.GetMaterial().IsSelected)
                {
                    shader.SetVector3("materialSelectColor", ColorUtility.ToVector3(Color.FromArgb(0, 163, 204)));
                    DrawModelSelection(m, shader);
                }
                else if ((m.IsSelected))
                {
                    DrawModelSelection(m, shader);
                }
                else
                {
                    if (Runtime.RenderModelWireframe)
                    {
                        DrawModelWireframe(m, shader);
                    }
                    if (Runtime.RenderModels)
                    {
                        DrawMdoelHoverSelection(m, shader, IsSelected(), Hovered);

                        PrimitiveType primitiveType = PrimitiveType.Triangles;

                        switch (m.lodMeshes[m.DisplayLODIndex].PrimitiveType)
                        {
                        case STPolygonType.Line:
                            primitiveType = PrimitiveType.Lines;
                            break;

                        case STPolygonType.LineStrip:
                            primitiveType = PrimitiveType.LineStrip;
                            break;

                        case STPolygonType.Point:
                            primitiveType = PrimitiveType.Points;
                            break;

                        case STPolygonType.Triangle:
                            primitiveType = PrimitiveType.Triangles;
                            break;
                        }

                        GL.DrawElements(primitiveType, m.lodMeshes[m.DisplayLODIndex].displayFaceSize, DrawElementsType.UnsignedInt, m.Offset);
                    }
                }
            }
        }
Exemplo n.º 22
0
        public static Model SetModel(FMDL fmdl)
        {
            Model model = new Model();

            model.Name = fmdl.Text;
            model.Path = "";

            model.Shapes        = new ResDict <Shape>();
            model.VertexBuffers = new List <VertexBuffer>();
            model.Materials     = new ResDict <Material>();
            model.UserData      = new ResDict <UserData>();
            model.Skeleton      = new Skeleton();
            model.Skeleton      = fmdl.Skeleton.node.SkeletonU;
            model.UserData      = fmdl.ModelU.UserData;

            if (model.Skeleton.InverseModelMatrices == null)
            {
                model.Skeleton.InverseModelMatrices = new List <Syroot.Maths.Matrix3x4>();
            }
            if (model.Skeleton.MatrixToBoneList == null)
            {
                model.Skeleton.MatrixToBoneList = new List <ushort>();
            }

            foreach (var bone in fmdl.Skeleton.bones)
            {
                if (model.Skeleton.InverseModelMatrices.Count <= 0)
                {
                    break;
                }

                int inde = 0;
                foreach (var bn in model.Skeleton.Bones.Values)
                {
                    if (bone.Text == bn.Name)
                    {
                        var mat = MatrixExenstion.GetMatrixInverted(bone);

                        if (bn.SmoothMatrixIndex > -1)
                        {
                            model.Skeleton.InverseModelMatrices[bn.SmoothMatrixIndex] = mat;
                        }
                    }
                    inde++;
                }
            }

            int i          = 0;
            var duplicates = fmdl.shapes.GroupBy(c => c.Text).Where(g => g.Skip(1).Any()).SelectMany(c => c);

            foreach (var shape in duplicates)
            {
                shape.Text += i++;
            }

            foreach (FSHP shape in fmdl.shapes)
            {
                BFRES.CheckMissingTextures(shape);
                SetShape(shape, shape.ShapeU);

                model.Shapes.Add(shape.Text, shape.ShapeU);
                model.VertexBuffers.Add(shape.VertexBufferU);
                shape.ShapeU.VertexBufferIndex = (ushort)(model.VertexBuffers.Count - 1);

                //   BFRES.SetShaderAssignAttributes(shape.GetMaterial().shaderassign, shape);
            }

            foreach (FMAT mat in fmdl.materials.Values)
            {
                SetMaterial(mat, mat.MaterialU, fmdl.GetResFileU());
                model.Materials.Add(mat.Text, mat.MaterialU);
            }
            return(model);
        }
Exemplo n.º 23
0
        public void LoadObject(FMDL mdl, FSHP shape)
        {
            InitializeControls();

            activeShape = shape;
            activeModel = mdl;



            //Load all the material names unless there's alot
            if (mdl.materials.Count < 1)
            {
                ReloadMaterialList();
            }
            else
            {
                materialComboBox1.Items.Add(shape.GetMaterial().Text);
                materialComboBox1.SelectedIndex = 0;
            }
            for (int i = 0; i < shape.lodMeshes.Count; i++)
            {
                LODMeshCB.Items.Add($"mesh {i}");
            }
            LODMeshCB.SelectedIndex = activeShape.DisplayLODIndex;

            textBoxName.Text      = shape.Text;
            textBoxBoneIndex.Text = shape.BoneIndex.ToString();
            textBoxMatIndex.Text  = shape.MaterialIndex.ToString();


            bonesCB.Items.Add(mdl.Skeleton.bones[shape.boneIndx].Text);
            bonesCB.SelectedIndex       = 0;
            textBoxVertexSkinCount.Text = shape.VertexSkinCount.ToString();

            if (BFRES.IsWiiU)
            {
            }
            else
            {
                if (shape.Shape.Flags == ShapeFlags.SubMeshBoundaryConsistent)
                {
                    checkBoxUseSubMeshBoundryConsistent.Checked = true;
                }
                if (shape.Shape.Flags == ShapeFlags.HasVertexBuffer)
                {
                    checkBoxUseVertexBuffer.Checked = true;
                }
            }


            shaderAttCB.Items.Add("NONE");
            foreach (FSHP.VertexAttribute att in shape.vertexAttributes)
            {
                vtxAttributesCB.Items.Add(att.Name);
                vtxFormatCB.Items.Add(att.Format);

                if (activeShape.GetMaterial().shaderassign.attributes.ContainsValue(att.Name))
                {
                    var VertexShaderAttributre = activeShape.GetMaterial().shaderassign.attributes.FirstOrDefault(x => x.Value == att.Name).Key;

                    shaderAttCB.Items.Add(VertexShaderAttributre);
                }
            }

            if (vtxAttributesCB.Items.Count > 0)
            {
                vtxAttributesCB.SelectedIndex = 0;
            }
            if (vtxFormatCB.Items.Count > 0)
            {
                vtxFormatCB.SelectedIndex = 0;
            }


            Vector3 translate = new Vector3(0);
            Vector3 scale     = new Vector3(1);
            Vector4 rotate    = new Vector4(0);

            translate = activeShape.boundingBoxes[0].Center;

            transXUD.Value = (decimal)translate.X;
            transYUD.Value = (decimal)translate.Y;
            transZUD.Value = (decimal)translate.Z;
            rotUDX.Value   = (decimal)rotate.X;
            rotUDY.Value   = (decimal)rotate.Y;
            rotUDZ.Value   = (decimal)rotate.Z;
            scaleUDX.Value = (decimal)scale.X;
            scaleUDY.Value = (decimal)scale.Y;
            scaleUDZ.Value = (decimal)scale.Z;

            RenderTools.DrawCube(translate, 2);
        }