示例#1
0
        static void UpdateModel(FoamModel Model, int FrameIndex)
        {
            if (Model.Animations == null)
            {
                return;
            }

            //Matrix4x4 ParentRotMat = Matrix4x4.CreateFromYawPitchRoll(0, -Pi / 2, 0);

            foreach (var Msh in Model.Meshes)
            {
                List <Vertex3> Verts = new List <Vertex3>();
                if (Msh.BoneInformation == null)
                {
                    continue;
                }

                foreach (var Index in Msh.Indices)
                {
                    FoamVertex3  Vert  = Msh.Vertices[Index];
                    FoamBoneInfo Info  = Msh.BoneInformation[Index];
                    FoamBone     Bone1 = Model.Bones[Info.Bone1];

                    // TODO: Weights
                    Matrix4x4 BindWorld  = Bone1.BindMatrix;
                    Matrix4x4 WorldTrans = Model.CalcWorldTransform(0, FrameIndex, Info.Bone1);
                    Vector3   Pos        = Vector3.Transform(Vert.Position, BindWorld * WorldTrans);

                    // TODO: Flip?
                    Verts.Add(new Vertex3(Pos, Vert.UV));
                }

                Mesh *RayMesh = ((Model)Msh.Userdata).meshes;
                Raylib.UnloadMesh(*RayMesh);
                *RayMesh = Raylib.GenMeshRaw(Verts.ToArray());
            }
        }
示例#2
0
        static FoamModel Load(string FileName, int MD3Frame = 0)
        {
            string Ext = Path.GetExtension(FileName).ToLower();

            using (AssimpContext Importer = new AssimpContext()) {
                Importer.SetConfig(new MD3HandleMultiPartConfig(false));
                //Importer.SetConfig(new MD5NoAnimationAutoLoadConfig(true));
                Importer.SetConfig(new VertexBoneWeightLimitConfig(4));
                Importer.SetConfig(new MD3KeyFrameImportConfig(MD3Frame));

                PostProcessSteps ProcessSteps = PostProcessSteps.Triangulate;
                ProcessSteps |= PostProcessSteps.SplitLargeMeshes;
                ProcessSteps |= PostProcessSteps.OptimizeMeshes;
                ProcessSteps |= PostProcessSteps.LimitBoneWeights;
                ProcessSteps |= PostProcessSteps.JoinIdenticalVertices;
                ProcessSteps |= PostProcessSteps.ImproveCacheLocality;
                ProcessSteps |= PostProcessSteps.GenerateNormals;
                ProcessSteps |= PostProcessSteps.GenerateUVCoords;

                //if (Ext != ".md3")
                ProcessSteps |= PostProcessPreset.ConvertToLeftHanded;

                Scene Sc = Importer.ImportFile(FileName, ProcessSteps);

                List <FoamMaterial> MaterialList = new List <FoamMaterial>();
                foreach (var Mat in Sc.Materials)
                {
                    FoamMaterial FoamMat = new FoamMaterial(Mat.Name);

                    AddTextureIfExists(Mat.HasTextureDiffuse, ref FoamMat, Mat.TextureDiffuse, FoamTextureType.Diffuse);
                    AddTextureIfExists(Mat.HasTextureEmissive, ref FoamMat, Mat.TextureEmissive, FoamTextureType.Glow);
                    AddTextureIfExists(Mat.HasTextureNormal, ref FoamMat, Mat.TextureNormal, FoamTextureType.Normal);
                    AddTextureIfExists(Mat.HasTextureSpecular, ref FoamMat, Mat.TextureSpecular, FoamTextureType.Specular);
                    AddTextureIfExists(Mat.HasTextureReflection, ref FoamMat, Mat.TextureReflection, FoamTextureType.Reflection);
                    AddTextureIfExists(Mat.HasTextureHeight, ref FoamMat, Mat.TextureHeight, FoamTextureType.Height);
                    AddTextureIfExists(Mat.HasTextureLightMap, ref FoamMat, Mat.TextureLightMap, FoamTextureType.LightMap);
                    AddTextureIfExists(Mat.HasTextureDisplacement, ref FoamMat, Mat.TextureDisplacement, FoamTextureType.Displacement);
                    AddTextureIfExists(Mat.HasTextureAmbient, ref FoamMat, Mat.TextureAmbient, FoamTextureType.Ambient);
                    AddTextureIfExists(Mat.HasTextureOpacity, ref FoamMat, Mat.TextureOpacity, FoamTextureType.Opacity);

                    MaterialList.Add(FoamMat);
                }

                FoamBone[]      Bones    = new FoamBone[0];
                List <FoamMesh> MeshList = new List <FoamMesh>();

                foreach (var Msh in Sc.Meshes)
                {
                    Vector3D[] Verts    = Msh.Vertices.ToArray();
                    Vector3D[] UVs1     = Msh.TextureCoordinateChannels[0].ToArray();
                    Vector3D[] UVs2     = Msh.TextureCoordinateChannels[1].ToArray();
                    Vector3D[] Normals  = Msh.Normals.ToArray();
                    Vector3D[] Tangents = Msh.Tangents.ToArray();
                    Color4D[]  Colors   = Msh.VertexColorChannels[0].ToArray();

                    string MeshName      = Msh.Name;
                    int    MaterialIndex = Msh.MaterialIndex;

                    FoamVertex3[] FoamVertices = new FoamVertex3[Verts.Length];
                    for (int i = 0; i < FoamVertices.Length; i++)
                    {
                        Vector2   UV1     = UVs1.Length != 0 ? new Vector2(UVs1[i].X, UVs1[i].Y) : Vector2.Zero;
                        Vector2   UV2     = UVs2.Length != 0 ? new Vector2(UVs2[i].X, UVs2[i].Y) : Vector2.Zero;
                        Vector3   Normal  = Normals.Length != 0 ? new Vector3(Normals[i].X, Normals[i].Y, Normals[i].Z) : Vector3.Zero;
                        Vector3   Tangent = Tangents.Length != 0 ? new Vector3(Tangents[i].X, Tangents[i].Y, Tangents[i].Z) : Vector3.Zero;
                        FoamColor Color   = Colors.Length != 0 ? new FoamColor(Colors[i].R, Colors[i].G, Colors[i].B, Colors[i].A) : FoamColor.White;

                        FoamVertex3 V = new FoamVertex3(new Vector3(Verts[i].X, Verts[i].Y, Verts[i].Z), UV1, UV2, Normal, Tangent, Color);
                        FoamVertices[i] = V;
                    }

                    bool CalculateTangents = Tangents.Length == 0 && UVs1.Length != 0;
                    //bool CalculateNormals = Normals.Length == 0;

                    List <ushort> FoamIndices = new List <ushort>();
                    foreach (var F in Msh.Faces)
                    {
                        ushort IndexA = (ushort)F.Indices[0];
                        ushort IndexB = (ushort)F.Indices[1];
                        ushort IndexC = (ushort)F.Indices[2];

                        if (CalculateTangents)
                        {
                            FoamVertex3 V0 = FoamVertices[IndexA];
                            FoamVertex3 V1 = FoamVertices[IndexB];
                            FoamVertex3 V2 = FoamVertices[IndexC];

                            Vector3 DeltaPos1 = V1.Position - V0.Position;
                            Vector3 DeltaPos2 = V2.Position - V0.Position;

                            //if (CalculateTangents) {
                            Vector2 DeltaUV1 = V1.UV - V0.UV;
                            Vector2 DeltaUV2 = V2.UV - V0.UV;

                            Vector3 Tangent = (DeltaPos1 * DeltaUV2.Y - DeltaPos2 * DeltaUV1.Y) * (1.0f / (DeltaUV1.X * DeltaUV2.Y - DeltaUV1.Y * DeltaUV2.X));
                            FoamVertices[IndexA].Tangent = FoamVertices[IndexB].Tangent = FoamVertices[IndexC].Tangent = Tangent;

                            /*}
                             *
                             * if (CalculateNormals)
                             *      FoamVertices[IndexA].Normal = FoamVertices[IndexB].Normal = FoamVertices[IndexC].Normal = Vector3.Normalize(Vector3.Cross(DeltaPos1, DeltaPos2));*/
                        }

                        FoamIndices.AddRange(F.Indices.Select(I => (ushort)I));
                    }

                    FoamBoneInfo[] BoneInfo = null;

                    if (Msh.BoneCount != 0)
                    {
                        BoneInfo = new FoamBoneInfo[FoamVertices.Length];
                        Bone[] OrigBones = Msh.Bones.ToArray();

                        // Convert bones
                        for (int i = 0; i < OrigBones.Length; i++)
                        {
                            if (!ContainsBoneNamed(Bones, OrigBones[i].Name))
                            {
                                Utils.Append(ref Bones, new FoamBone(OrigBones[i].Name, -1, ConvertMatrix(OrigBones[i].OffsetMatrix)));
                            }
                        }

                        // Convert vertex bone information
                        for (int i = 0; i < FoamVertices.Length; i++)
                        {
                            BoneInfo[i] = FindWeightsFor(OrigBones, Bones, i);
                        }
                    }

                    MeshList.Add(new FoamMesh(FoamVertices, FoamIndices?.ToArray() ?? null, BoneInfo, MeshName, MaterialIndex));
                }

                if (Bones.Length > 0)
                {
                    Node[] NodeHierarchy = Flatten(Sc.RootNode);
                    //Node SceneRootNode = Sc.RootNode;
                    Node RootNode = FindRoot(FindNode(NodeHierarchy, Bones[0].Name), Bones);
                    Utils.Prepend(ref Bones, new FoamBone(RootNode.Name, -1, ConvertMatrix(RootNode.Transform)));

                    /*Node RootNodeTest = RootNode;
                     * while (RootNodeTest.Parent != null) {
                     *      Bones[0].BindMatrix = Bones[0].BindMatrix * ConvertMatrix(RootNodeTest.Transform);
                     *      RootNodeTest = RootNodeTest.Parent;
                     * }*/


                    /*while (RootNode.Parent != null) {
                     *      Utils.Prepend(ref Bones, new FoamBone(RootNode.Name, -1, NumMatrix4x4.Identity));
                     *      RootNode = RootNode.Parent;
                     * }*/


                    for (int i = 0; i < Bones.Length; i++)
                    {
                        Node BoneNode        = FindNode(NodeHierarchy, Bones[i].Name);
                        int  BoneParentIndex = FindBoneIndex(Bones, BoneNode.Parent.Name);

                        if (BoneNode != RootNode)
                        {
                            if (BoneParentIndex == -1)
                            {
                                throw new Exception("Could not find a bone");
                            }
                        }

                        Bones[i].ParentBoneIndex = BoneParentIndex;
                    }
                }
                else
                {
                    Bones = null;
                }

                // Animations
                FoamAnimation[] Animations = null;

                foreach (var Anim in Sc.Animations)
                {
                    string[]             BoneNames  = Anim.NodeAnimationChannels.Select(C => C.NodeName).ToArray();
                    int                  FrameCount = Anim.NodeAnimationChannels[0].PositionKeyCount;
                    FoamAnimationFrame[] Frames     = new FoamAnimationFrame[FrameCount];

                    for (int i = 0; i < FrameCount; i++)
                    {
                        Frames[i] = ReadFrame(Anim.NodeAnimationChannels, BoneNames, i);
                    }

                    FoamAnimation Animation = new FoamAnimation(Anim.Name, Frames, BoneNames, (float)Anim.DurationInTicks, (float)Anim.TicksPerSecond);
                    Utils.Append(ref Animations, Animation);
                }

                return(new FoamModel(Path.GetFileNameWithoutExtension(FileName), FoamFlags.Model, MeshList.ToArray(), Bones, Animations, MaterialList.ToArray()));
            }
        }
示例#3
0
        static void DrawBones(FoamModel Mdl)
        {
            if (Mdl.Bones == null)
            {
                return;
            }

            if (WorldTexts == null || WorldTexts.Length != Mdl.Bones.Length)
            {
                WorldTexts = new KeyValuePair <Vector2, string> [Mdl.Bones.Length];
                for (int i = 0; i < WorldTexts.Length; i++)
                {
                    WorldTexts[i] = new KeyValuePair <Vector2, string>(new Vector2(0, 0), "null");
                }
            }

            //Matrix4x4 ParentRotMat = Matrix4x4.CreateFromYawPitchRoll(0, -Pi / 2, 0);

            for (int i = 0; i < Mdl.Bones.Length; i++)
            {
                FoamBone Bone = Mdl.Bones[i];


                //*
                if (Mdl.Animations != null)
                {
                    // Actual bones
                    Matrix4x4 ParentWorld = Matrix4x4.Identity;
                    if (Bone.ParentBoneIndex != -1)
                    {
                        ParentWorld = Mdl.CalcWorldTransform(0, FrameIndex, Bone.ParentBoneIndex);
                    }
                    Matrix4x4.Decompose(ParentWorld, out Vector3 ParentScale, out Quaternion ParentRot, out Vector3 ParentPos);

                    Matrix4x4 World = Mdl.CalcWorldTransform(0, FrameIndex, i);
                    Matrix4x4.Decompose(World, out Vector3 Scale, out Quaternion Rot, out Vector3 Pos);

                    DrawLine3D(ParentPos, Pos, Color.Red);

                    // Text
                    Vector3 Center = (Pos + ParentPos) / 2;
                    WorldTexts[i] = new KeyValuePair <Vector2, string>(Raylib.GetWorldToScreen(RotateVec3(Center), Cam3D), Bone.Name);
                }
                //*/


                // Bind pose bones
                Matrix4x4 BindParentWorld = Matrix4x4.Identity;
                if (Bone.ParentBoneIndex != -1)
                {
                    BindParentWorld = Mdl.CalcBindTransform(Bone.ParentBoneIndex);
                }
                Matrix4x4.Decompose(BindParentWorld, out Vector3 BindParentScale, out Quaternion BindParentRot, out Vector3 BindParentPos);

                Matrix4x4 BindWorld = Mdl.CalcBindTransform(i);
                Matrix4x4.Decompose(BindWorld, out Vector3 BindScale, out Quaternion BindRot, out Vector3 BindPos);

                DrawLine3D(BindParentPos, BindPos, Color.Green);
                //*/
            }
        }