Ejemplo n.º 1
0
 public void UpdateTransforms(ModelNode pNode, Matrix4 transf)
 {
     string nodename = pNode.Name;
     Matrix4 nodeTransf = Matrix4.Identity;
     double time;
     SingleAnimationNode pNodeAnim = FindNodeAnim(nodename, pNode.Mode, out time);
     if (pNodeAnim != null)
     {
         BEPUutilities.Vector3 vec = pNodeAnim.lerpPos(time);
         BEPUutilities.Quaternion quat = pNodeAnim.lerpRotate(time);
         Quaternion oquat = new Quaternion((float)quat.X, (float)quat.Y, (float)quat.Z, (float)quat.W);
         Matrix4 trans;
         Matrix4.CreateTranslation((float)vec.X, (float)vec.Y, (float)vec.Z, out trans);
         trans.Transpose();
         Matrix4 rot;
         Matrix4.CreateFromQuaternion(ref oquat, out rot);
         Matrix4 r2;
         if (CustomAnimationAdjustments.TryGetValue(nodename, out r2))
         {
             rot *= r2;
         }
         rot.Transpose();
         Matrix4.Mult(ref trans, ref rot, out nodeTransf);
     }
     else
     {
         Matrix4 temp;
         if (CustomAnimationAdjustments.TryGetValue(nodename, out temp))
         {
             temp.Transpose();
             nodeTransf = temp;
         }
     }
     Matrix4 global;
     Matrix4.Mult(ref transf, ref nodeTransf, out global);
     for (int i = 0; i < pNode.Bones.Count; i++)
     {
         if (ForceBoneNoOffset)
         {
             pNode.Bones[i].Transform = global;
         }
         else
         {
             Matrix4.Mult(ref global, ref pNode.Bones[i].Offset, out pNode.Bones[i].Transform);
         }
     }
     for (int i = 0; i < pNode.Children.Count; i++)
     {
         UpdateTransforms(pNode.Children[i], global);
     }
 }
Ejemplo n.º 2
0
 void PopulateChildren(ModelNode node, Model3DNode orin, Model model, AnimationEngine engine, List<ModelNode> allNodes)
 {
     allNodes.Add(node);
     if (engine.HeadBones.Contains(node.Name))
     {
         node.Mode = 0;
     }
     else if (engine.LegBones.Contains(node.Name))
     {
         node.Mode = 2;
     }
     else
     {
         node.Mode = 1;
     }
     for (int i = 0; i < orin.Children.Count; i++)
     {
         ModelNode child = new ModelNode() { Parent = node, Name = orin.Children[i].Name.ToLowerFast() };
         PopulateChildren(child, orin.Children[i], model, engine, allNodes);
         node.Children.Add(child);
     }
 }
Ejemplo n.º 3
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)
            {
                Engine   = this,
                Original = scene,
                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
                    {
                        modmesh.vbo.TexCoords.Add(new Vector3((float)mesh.TexCoords[i].X, 1 - (float)mesh.TexCoords[i].Y, 0));
                    }
                    if (!hasn)
                    {
                        modmesh.vbo.Normals.Add(new Vector3(0f, 0f, 1f));
                    }
                    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);
        }