Beispiel #1
0
        public Model3DGroup Render(double time)
        {
            var meshs = BaseModel.Clone();

            time = time % (float)animation.DurationInTicks;
            TransformNode(time, aiScene.RootNode, globalInverseTransform);

            for (int meshId = 0; meshId < aiScene.MeshCount; meshId++)
            {
                if (!aiMeshVertexBoneInfos.ContainsKey(meshId))
                {
                    continue;
                }

                var mesh               = aiScene.Meshes[meshId];
                var mesh3D             = (meshs.Children[meshId] as GeometryModel3D).Geometry as MeshGeometry3D;
                var vertextBoneWeights = aiMeshVertexBoneInfos[meshId];

                for (int vertexId = 0; vertexId < mesh.VertexCount; vertexId++)
                {
                    var ver = mesh.Vertices[vertexId];
                    if (vertextBoneWeights.ContainsKey(vertexId))
                    {
                        var vertexWeights = vertextBoneWeights[vertexId];

                        var boneTransform = mat4.Identity;

                        var sum = vertexWeights.Sum(m => m.Weight);

                        vertexWeights.ForEach(info =>
                        {
                            boneTransform += allBones[info.BoneName].finalTransformation * (float)(info.Weight / sum);
                        });

                        var animPosition = new vec4(ver.X, ver.Y, ver.Z, 1.0f);
                        animPosition = boneTransform * animPosition;

                        mesh3D.Positions[vertexId] = new Point3D(animPosition.x, animPosition.y, animPosition.z);
                    }
                    else
                    {
                        mesh3D.Positions[vertexId] = new Point3D(ver.X, ver.Y, ver.Z);
                    }
                }
            }

            return(meshs);
        }