コード例 #1
0
        public Model FromScene(Model3D scene, string name, AnimationEngine engine)
        {
            if (scene.Meshes.Count == 0)
            {
                throw new Exception("Scene has no meshes! (" + name + ")");
            }
            Model model = new Model(name);

            model.Engine   = this;
            model.Original = scene;
            model.Root     = convert(scene.MatrixA);
            foreach (Model3DMesh mesh in scene.Meshes)
            {
                if (mesh.Name.ToLowerFast().Contains("collision") || mesh.Name.ToLowerFast().Contains("norender"))
                {
                    continue;
                }
                ModelMesh modmesh = new ModelMesh(mesh.Name);
                modmesh.vbo.Prepare();
                bool hastc = mesh.TexCoords.Count == mesh.Vertices.Count;
                bool hasn  = mesh.Normals.Count == mesh.Vertices.Count;
                if (!hasn)
                {
                    SysConsole.Output(OutputType.WARNING, "Mesh has no normals! (" + name + ")");
                }
                if (!hastc)
                {
                    SysConsole.Output(OutputType.WARNING, "Mesh has no texcoords! (" + name + ")");
                }
                for (int i = 0; i < mesh.Vertices.Count; i++)
                {
                    BEPUutilities.Vector3 vertex = mesh.Vertices[i];
                    modmesh.vbo.Vertices.Add(new Vector3((float)vertex.X, (float)vertex.Y, (float)vertex.Z));
                    if (!hastc)
                    {
                        modmesh.vbo.TexCoords.Add(new Vector3(0, 0, 0));
                    }
                    else
                    {
                        BEPUutilities.Vector2 texCoord = mesh.TexCoords[i];
                        modmesh.vbo.TexCoords.Add(new Vector3((float)texCoord.X, 1 - (float)texCoord.Y, 0));
                    }
                    if (!hasn)
                    {
                        modmesh.vbo.Normals.Add(new Vector3(0, 0, 1));
                    }
                    else
                    {
                        modmesh.vbo.Normals.Add(new Vector3((float)mesh.Normals[i].X, (float)mesh.Normals[i].Y, (float)mesh.Normals[i].Z));
                    }
                    modmesh.vbo.Colors.Add(new Vector4(1, 1, 1, 1)); // TODO: From the mesh?
                }
                for (int i = 0; i < mesh.Indices.Count; i++)
                {
                    modmesh.vbo.Indices.Add((uint)mesh.Indices[i]);
                }
                int bc = mesh.Bones.Count;
                if (bc > 200)
                {
                    SysConsole.Output(OutputType.WARNING, "Mesh has " + bc + " bones! (" + name + ")");
                    bc = 200;
                }
                modmesh.vbo.BoneIDs      = new Vector4[modmesh.vbo.Vertices.Count].ToList();
                modmesh.vbo.BoneWeights  = new Vector4[modmesh.vbo.Vertices.Count].ToList();
                modmesh.vbo.BoneIDs2     = new Vector4[modmesh.vbo.Vertices.Count].ToList();
                modmesh.vbo.BoneWeights2 = new Vector4[modmesh.vbo.Vertices.Count].ToList();
                int[] pos = new int[modmesh.vbo.Vertices.Count];
                for (int i = 0; i < bc; i++)
                {
                    for (int x = 0; x < mesh.Bones[i].Weights.Count; x++)
                    {
                        int   IDa     = mesh.Bones[i].IDs[x];
                        float Weighta = (float)mesh.Bones[i].Weights[x];
                        int   spot    = pos[IDa]++;
                        if (spot > 7)
                        {
                            //SysConsole.Output(OutputType.WARNING, "Too many bones influencing " + vw.VertexID + "!");
                            ForceSet(modmesh.vbo.BoneWeights, IDa, 3, modmesh.vbo.BoneWeights[IDa][3] + Weighta);
                        }
                        else if (spot > 3)
                        {
                            ForceSet(modmesh.vbo.BoneIDs2, IDa, spot - 4, i);
                            ForceSet(modmesh.vbo.BoneWeights2, IDa, spot - 4, Weighta);
                        }
                        else
                        {
                            ForceSet(modmesh.vbo.BoneIDs, IDa, spot, i);
                            ForceSet(modmesh.vbo.BoneWeights, IDa, spot, Weighta);
                        }
                    }
                }
                model.Meshes.Add(modmesh);
            }
            model.RootNode = new ModelNode()
            {
                Parent = null, Name = scene.RootNode.Name.ToLowerFast()
            };
            List <ModelNode> allNodes = new List <ModelNode>();

            PopulateChildren(model.RootNode, scene.RootNode, model, engine, allNodes);
            for (int i = 0; i < model.Meshes.Count; i++)
            {
                for (int x = 0; x < scene.Meshes[i].Bones.Count; x++)
                {
                    ModelNode nodet = null;
                    string    nl    = scene.Meshes[i].Bones[x].Name.ToLowerFast();
                    for (int n = 0; n < allNodes.Count; n++)
                    {
                        if (allNodes[n].Name == nl)
                        {
                            nodet = allNodes[n];
                            break;
                        }
                    }
                    ModelBone mb = new ModelBone()
                    {
                        Offset = convert(scene.Meshes[i].Bones[x].MatrixA)
                    };
                    nodet.Bones.Add(mb);
                    model.Meshes[i].Bones.Add(mb);
                }
            }
            return(model);
        }
コード例 #2
0
ファイル: ModelEngine.cs プロジェクト: Morphan1/Voxalia
 public Model FromScene(Model3D scene, string name, AnimationEngine engine)
 {
     if (scene.Meshes.Count == 0)
     {
         throw new Exception("Scene has no meshes! (" + name + ")");
     }
     Model model = new Model(name);
     model.Engine = this;
     model.Original = scene;
     model.Root = convert(scene.MatrixA);
     foreach (Model3DMesh mesh in scene.Meshes)
     {
         if (mesh.Name.ToLowerFast().Contains("collision") || mesh.Name.ToLowerFast().Contains("norender"))
         {
             continue;
         }
         ModelMesh modmesh = new ModelMesh(mesh.Name);
         modmesh.vbo.Prepare();
         bool hastc = mesh.TexCoords.Count == mesh.Vertices.Count;
         bool hasn = mesh.Normals.Count == mesh.Vertices.Count;
         if (!hasn)
         {
             SysConsole.Output(OutputType.WARNING, "Mesh has no normals! (" + name + ")");
         }
         if (!hastc)
         {
             SysConsole.Output(OutputType.WARNING, "Mesh has no texcoords! (" + name + ")");
         }
         for (int i = 0; i < mesh.Vertices.Count; i++)
         {
             BEPUutilities.Vector3 vertex = mesh.Vertices[i];
             modmesh.vbo.Vertices.Add(new Vector3((float)vertex.X, (float)vertex.Y, (float)vertex.Z));
             if (!hastc)
             {
                 modmesh.vbo.TexCoords.Add(new Vector3(0, 0, 0));
             }
             else
             {
                 BEPUutilities.Vector2 texCoord = mesh.TexCoords[i];
                 modmesh.vbo.TexCoords.Add(new Vector3((float)texCoord.X, 1 - (float)texCoord.Y, 0));
             }
             if (!hasn)
             {
                 modmesh.vbo.Normals.Add(new Vector3(0, 0, 1));
             }
             else
             {
                 modmesh.vbo.Normals.Add(new Vector3((float)mesh.Normals[i].X, (float)mesh.Normals[i].Y, (float)mesh.Normals[i].Z));
             }
             modmesh.vbo.Colors.Add(new Vector4(1, 1, 1, 1)); // TODO: From the mesh?
         }
         for (int i = 0; i < mesh.Indices.Count; i++)
         {
             modmesh.vbo.Indices.Add((uint)mesh.Indices[i]);
         }
         int bc = mesh.Bones.Count;
         if (bc > 200)
         {
             SysConsole.Output(OutputType.WARNING, "Mesh has " + bc + " bones! (" + name + ")");
             bc = 200;
         }
         modmesh.vbo.BoneIDs = new Vector4[modmesh.vbo.Vertices.Count].ToList();
         modmesh.vbo.BoneWeights = new Vector4[modmesh.vbo.Vertices.Count].ToList();
         modmesh.vbo.BoneIDs2 = new Vector4[modmesh.vbo.Vertices.Count].ToList();
         modmesh.vbo.BoneWeights2 = new Vector4[modmesh.vbo.Vertices.Count].ToList();
         int[] pos = new int[modmesh.vbo.Vertices.Count];
         for (int i = 0; i < bc; i++)
         {
             for (int x = 0; x < mesh.Bones[i].Weights.Count; x++)
             {
                 int IDa = mesh.Bones[i].IDs[x];
                 float Weighta = (float)mesh.Bones[i].Weights[x];
                 int spot = pos[IDa]++;
                 if (spot > 7)
                 {
                     //SysConsole.Output(OutputType.WARNING, "Too many bones influencing " + vw.VertexID + "!");
                     ForceSet(modmesh.vbo.BoneWeights, IDa, 3, modmesh.vbo.BoneWeights[IDa][3] + Weighta);
                 }
                 else if (spot > 3)
                 {
                     ForceSet(modmesh.vbo.BoneIDs2, IDa, spot - 4, i);
                     ForceSet(modmesh.vbo.BoneWeights2, IDa, spot - 4, Weighta);
                 }
                 else
                 {
                     ForceSet(modmesh.vbo.BoneIDs, IDa, spot, i);
                     ForceSet(modmesh.vbo.BoneWeights, IDa, spot, Weighta);
                 }
             }
         }
         model.Meshes.Add(modmesh);
     }
     model.RootNode = new ModelNode() { Parent = null, Name = scene.RootNode.Name.ToLowerFast() };
     List<ModelNode> allNodes = new List<ModelNode>();
     PopulateChildren(model.RootNode, scene.RootNode, model, engine, allNodes);
     for (int i = 0; i < model.Meshes.Count; i++)
     {
         for (int x = 0; x < scene.Meshes[i].Bones.Count; x++)
         {
             ModelNode nodet = null;
             string nl = scene.Meshes[i].Bones[x].Name.ToLowerFast();
             for (int n = 0; n < allNodes.Count; n++)
             {
                 if (allNodes[n].Name == nl)
                 {
                     nodet = allNodes[n];
                     break;
                 }
             }
             ModelBone mb = new ModelBone() { Offset = convert(scene.Meshes[i].Bones[x].MatrixA) };
             nodet.Bones.Add(mb);
             model.Meshes[i].Bones.Add(mb);
         }
     }
     return model;
 }