Exemple #1
0
        //Generates a mesh from required parameters, as well as specifying a parent bone to be used, used to simplify and clean up code.
        public static ModelMesh makeMeshFromPolygonList(List <Polygon> polys, GraphicsDevice graphics, ModelBone parentBone)
        {
            List <ModelMeshPart> parts = new List <ModelMeshPart>();
            ModelBone            bone  = new ModelBone();

            bone.Transform      = Matrix.CreateTranslation(0, 0, 0);
            bone.ModelTransform = parentBone.ModelTransform;

            List <Vector3> points = new List <Vector3>();

            foreach (Polygon p in polys)
            {
                ModelMeshPart part = makePartFromPolygon(p, graphics);
                parts.Add(part);
                foreach (Vector3 point in p.points)
                {
                    points.Add(point);
                }
            }

            ModelMesh mesh = new ModelMesh(graphics, parts);

            bone.AddMesh(mesh);
            parentBone.AddChild(bone);
            bone.Parent         = parentBone;
            mesh.ParentBone     = bone;
            mesh.BoundingSphere = BoundingSphere.CreateFromPoints(points);

            return(mesh);
        }
Exemple #2
0
        //Generates a model from required parameters, used to simplify and clean up code.
        public static Model makeModel(List <ModelMesh> meshes, GraphicsDevice graphics, Effect effect)
        {
            List <ModelBone> boneList = new List <ModelBone>();
            ModelBone        boneObj  = new ModelBone();

            boneObj.Transform      = Matrix.CreateTranslation(0, 0, 0);
            boneObj.ModelTransform = Matrix.CreateTranslation(0, 0, 0);

            foreach (ModelMesh modelMeshObj in meshes)
            {
                boneObj.AddChild(modelMeshObj.ParentBone);
                modelMeshObj.ParentBone.Parent = boneObj;
                boneList.Add(modelMeshObj.ParentBone);
            }

            Model modelObj = new Model(graphics, boneList, meshes);

            modelObj.Root = boneObj;

            foreach (ModelMesh modelMesh in modelObj.Meshes)
            {
                foreach (ModelMeshPart meshPart in modelMesh.MeshParts)
                {
                    meshPart.Effect = effect;
                }
            }

            return(modelObj);
        }
Exemple #3
0
        protected internal override Model Read(ContentReader reader, Model existingInstance)
        {
            // Read the bone names and transforms.
            uint             boneCount = reader.ReadUInt32();
            List <ModelBone> bones     = new List <ModelBone>((int)boneCount);

            for (uint i = 0; i < boneCount; i += 1)
            {
                string    name   = reader.ReadObject <string>();
                Matrix    matrix = reader.ReadMatrix();
                ModelBone bone   = new ModelBone {
                    Transform = matrix,
                    Index     = (int)i,
                    Name      = name
                };
                bones.Add(bone);
            }
            // Read the bone hierarchy.
            for (int i = 0; i < boneCount; i += 1)
            {
                ModelBone bone = bones[i];
                // Read the parent bone reference.
                int parentIndex = ReadBoneReference(reader, boneCount);
                if (parentIndex != -1)
                {
                    bone.Parent = bones[parentIndex];
                }
                // Read the child bone references.
                uint childCount = reader.ReadUInt32();
                if (childCount != 0)
                {
                    for (uint j = 0; j < childCount; j += 1)
                    {
                        int childIndex = ReadBoneReference(reader, boneCount);
                        if (childIndex != -1)
                        {
                            bone.AddChild(bones[childIndex]);
                        }
                    }
                }
            }

            List <ModelMesh> meshes = new List <ModelMesh>();

            // Read the mesh data.
            int meshCount = reader.ReadInt32();

            for (int i = 0; i < meshCount; i += 1)
            {
                string         name            = reader.ReadObject <string>();
                int            parentBoneIndex = ReadBoneReference(reader, boneCount);
                BoundingSphere boundingSphere  = reader.ReadBoundingSphere();

                // Tag
                object meshTag = reader.ReadObject <object>();

                // Read the mesh part data.
                int partCount = reader.ReadInt32();

                List <ModelMeshPart> parts = new List <ModelMeshPart>(partCount);

                for (uint j = 0; j < partCount; j += 1)
                {
                    ModelMeshPart part;
                    if (existingInstance != null)
                    {
                        part = existingInstance.Meshes[i].MeshParts[(int)j];
                    }
                    else
                    {
                        part = new ModelMeshPart();
                    }

                    part.VertexOffset   = reader.ReadInt32();
                    part.NumVertices    = reader.ReadInt32();
                    part.StartIndex     = reader.ReadInt32();
                    part.PrimitiveCount = reader.ReadInt32();

                    // Tag
                    part.Tag = reader.ReadObject <object>();

                    parts.Add(part);

                    int jj = (int)j;
                    reader.ReadSharedResource <VertexBuffer>(
                        delegate(VertexBuffer v)
                    {
                        parts[jj].VertexBuffer = v;
                    }
                        );
                    reader.ReadSharedResource <IndexBuffer>(
                        delegate(IndexBuffer v)
                    {
                        parts[jj].IndexBuffer = v;
                    }
                        );
                    reader.ReadSharedResource <Effect>(
                        delegate(Effect v)
                    {
                        parts[jj].Effect = v;
                    }
                        );
                }
                if (existingInstance != null)
                {
                    continue;
                }
                ModelMesh mesh = new ModelMesh(reader.GraphicsDevice, parts);
                mesh.Tag        = meshTag;
                mesh.Name       = name;
                mesh.ParentBone = bones[parentBoneIndex];
                mesh.ParentBone.AddMesh(mesh);
                mesh.BoundingSphere = boundingSphere;
                meshes.Add(mesh);
            }
            if (existingInstance != null)
            {
                // Read past remaining data and return existing instance
                ReadBoneReference(reader, boneCount);
                reader.ReadObject <object>();
                return(existingInstance);
            }
            // Read the final pieces of model data.
            int   rootBoneIndex = ReadBoneReference(reader, boneCount);
            Model model         = new Model(reader.GraphicsDevice, bones, meshes);

            model.Root = bones[rootBoneIndex];
            model.BuildHierarchy();
            // Tag?
            model.Tag = reader.ReadObject <object>();
            return(model);
        }
        private ModelBone ProcessBone(PooledTexture2D texture, EntityModel source, EntityModelBone bone, List <VertexPositionColorTexture> vertices, Vector2 uvScale, Vector2 textureSize, Dictionary <string, ModelBone> modelBones)
        {
            ModelBone modelBone;

            List <short> indices = new List <short>();

            bone.Pivot *= new Vector3(-1f, 1f, 1f);

            if (bone.Cubes != null)
            {
                foreach (var cube in bone.Cubes)
                {
                    if (cube == null)
                    {
                        Log.Warn("Cube was null!");
                        continue;
                    }

                    Cube built = new Cube(cube.InflatedSize, textureSize, cube.Uv, uvScale, bone.Mirror);

                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Front, bone.Mirror);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Back, bone.Mirror);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Top, bone.Mirror);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Bottom, bone.Mirror);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Left, bone.Mirror);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Right, bone.Mirror);

                    indices.AddRange(built.Front.indexes.Concat(built.Back.indexes).Concat(built.Top.indexes)
                                     .Concat(built.Bottom.indexes).Concat(built.Left.indexes).Concat(built.Right.indexes)
                                     .ToArray());
                }
            }

            var bindPoseMatrix = Matrix.CreateTranslation(-bone.Pivot)
                                 * Matrix.CreateRotationY(MathUtils.ToRadians(bone.BindPoseRotation.Y))
                                 * Matrix.CreateRotationX(MathUtils.ToRadians(-bone.BindPoseRotation.X))
                                 * Matrix.CreateRotationZ(MathUtils.ToRadians(bone.BindPoseRotation.Z))
                                 * Matrix.CreateTranslation(bone.Pivot);

            /*var boneMatrix = Matrix.Identity * Matrix.CreateTranslation(-bone.Pivot)
             * Matrix.CreateFromAxisAngle(
             *                                       Vector3.Right, MathUtils.ToRadians(bone.Rotation.X))
             * Matrix.CreateFromAxisAngle(
             *                                       Vector3.Backward, MathUtils.ToRadians(bone.Rotation.Z))
             * Matrix.CreateFromAxisAngle(
             *                                       Vector3.Up, MathUtils.ToRadians(bone.Rotation.Y))
             * Matrix.CreateTranslation(bone.Pivot);*/
            var boneMatrix =
                Matrix.CreateTranslation(-bone.Pivot)
                * Matrix.CreateRotationY(MathUtils.ToRadians(bone.Rotation.Y))
                * Matrix.CreateRotationX(MathUtils.ToRadians(-bone.Rotation.X))
                * Matrix.CreateRotationZ(MathUtils.ToRadians(bone.Rotation.Z))
                * Matrix.CreateTranslation(bone.Pivot);

            modelBone = new ModelBone(texture, indices.ToArray(), bone, bindPoseMatrix * boneMatrix);
            modelBone.Setup(Alex.Instance.GraphicsDevice);

            foreach (var childBone in source.Bones.Where(
                         x => string.Equals(x.Parent, bone.Name, StringComparison.InvariantCultureIgnoreCase)))
            {
                var child = ProcessBone(texture, source, childBone, vertices, uvScale, textureSize, modelBones);
                child.Parent = modelBone;

                modelBone.AddChild(child);

                if (!modelBones.TryAdd(childBone.Name, child))
                {
                    Log.Warn($"Failed to add bone! {childBone.Name}");
                }
            }

            return(modelBone);
        }
Exemple #5
0
        protected internal override Model Read(ContentReader reader, Model existingInstance)
        {
            List <ModelBone> bones     = new List <ModelBone>();
            uint             boneCount = reader.ReadUInt32();

            for (uint index = 0U; index < boneCount; ++index)
            {
                string    str       = reader.ReadObject <string>();
                Matrix    matrix    = reader.ReadMatrix();
                ModelBone modelBone = new ModelBone()
                {
                    Transform = matrix,
                    Index     = (int)index,
                    Name      = str
                };
                bones.Add(modelBone);
            }
            for (int index1 = 0; (long)index1 < (long)boneCount; ++index1)
            {
                ModelBone modelBone = bones[index1];
                int       index2    = ModelReader.ReadBoneReference(reader, boneCount);
                if (index2 != -1)
                {
                    modelBone.Parent = bones[index2];
                }
                uint num = reader.ReadUInt32();
                if ((int)num != 0)
                {
                    for (uint index3 = 0U; index3 < num; ++index3)
                    {
                        int index4 = ModelReader.ReadBoneReference(reader, boneCount);
                        if (index4 != -1)
                        {
                            modelBone.AddChild(bones[index4]);
                        }
                    }
                }
            }
            List <ModelMesh> meshes = new List <ModelMesh>();
            int num1 = reader.ReadInt32();

            for (int index1 = 0; index1 < num1; ++index1)
            {
                string         str            = reader.ReadObject <string>();
                int            index2         = ModelReader.ReadBoneReference(reader, boneCount);
                BoundingSphere boundingSphere = reader.ReadBoundingSphere();
                reader.ReadObject <object>();
                int num2 = reader.ReadInt32();
                List <ModelMeshPart> parts = new List <ModelMeshPart>();
                for (uint index3 = 0U; (long)index3 < (long)num2; ++index3)
                {
                    ModelMeshPart modelMeshPart = existingInstance == null ? new ModelMeshPart() : ((ReadOnlyCollection <ModelMesh>)existingInstance.Meshes)[index1].MeshParts[(int)index3];
                    modelMeshPart.VertexOffset   = reader.ReadInt32();
                    modelMeshPart.NumVertices    = reader.ReadInt32();
                    modelMeshPart.StartIndex     = reader.ReadInt32();
                    modelMeshPart.PrimitiveCount = reader.ReadInt32();
                    modelMeshPart.Tag            = reader.ReadObject <object>();
                    parts.Add(modelMeshPart);
                    int jj = (int)index3;
                    reader.ReadSharedResource <VertexBuffer>((Action <VertexBuffer>)(v => parts[jj].VertexBuffer = v));
                    reader.ReadSharedResource <IndexBuffer>((Action <IndexBuffer>)(v => parts[jj].IndexBuffer    = v));
                    reader.ReadSharedResource <Effect>((Action <Effect>)(v => parts[jj].Effect = v));
                }
                if (existingInstance == null)
                {
                    ModelMesh mesh = new ModelMesh(reader.GraphicsDevice, parts);
                    mesh.Name       = str;
                    mesh.ParentBone = bones[index2];
                    mesh.ParentBone.AddMesh(mesh);
                    mesh.BoundingSphere = boundingSphere;
                    meshes.Add(mesh);
                }
            }
            if (existingInstance != null)
            {
                ModelReader.ReadBoneReference(reader, boneCount);
                reader.ReadObject <object>();
                return(existingInstance);
            }
            else
            {
                int   index = ModelReader.ReadBoneReference(reader, boneCount);
                Model model = new Model(reader.GraphicsDevice, bones, meshes);
                model.Root = bones[index];
                model.BuildHierarchy();
                model.Tag = reader.ReadObject <object>();
                return(model);
            }
        }
        private ModelBone ProcessBone(PooledTexture2D texture,
                                      EntityModel source,
                                      EntityModelBone bone,
                                      List <VertexPositionColorTexture> vertices,
                                      Vector2 textureSize,
                                      Dictionary <string, ModelBone> modelBones)
        {
            ModelBone modelBone;

            List <short> indices = new List <short>();

            //bone.Pivot *= new Vector3(-1f, 1f, 1f);

            if (bone.Cubes != null)
            {
                foreach (var cube in bone.Cubes)
                {
                    if (cube == null)
                    {
                        Log.Warn("Cube was null!");

                        continue;
                    }

                    //if (cube.Uv.IsOutOfBound(textureSize))
                    {
                        //	continue;
                    }

                    var  inflation = (float)(cube.Inflate ?? bone.Inflate);
                    var  mirror    = cube.Mirror ?? bone.Mirror;
                    Cube built     = new Cube(source, cube, textureSize, mirror, inflation);

                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Front, bone, inflation);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Back, bone, inflation);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Top, bone, inflation);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Bottom, bone, inflation);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Left, bone, inflation);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Right, bone, inflation);

                    indices.AddRange(
                        built.Front.indexes.Concat(built.Back.indexes).Concat(built.Top.indexes)
                        .Concat(built.Bottom.indexes).Concat(built.Left.indexes).Concat(built.Right.indexes)
                        .ToArray());
                }
            }

            var pivot      = bone.Pivot ?? Vector3.Zero;
            var boneMatrix = Matrix.Identity;

            if (bone.BindPoseRotation.HasValue)
            {
                var rotation = bone.BindPoseRotation.Value;

                boneMatrix = Matrix.CreateTranslation(-pivot)
                             * Matrix.CreateRotationX(MathUtils.ToRadians(-rotation.X))
                             * Matrix.CreateRotationY(MathUtils.ToRadians(rotation.Y))
                             * Matrix.CreateRotationZ(MathUtils.ToRadians(rotation.Z))
                             * Matrix.CreateTranslation(pivot);
            }

            modelBone = new ModelBone(texture, indices.ToArray(), bone, boneMatrix);

            if (bone.Rotation.HasValue)
            {
                var r = bone.Rotation.Value;
                modelBone.Rotation = new Vector3(r.X, r.Y, r.Z);
            }

            modelBone.Setup(Alex.Instance.GraphicsDevice);

            foreach (var childBone in source.Bones.Where(
                         x => x.Parent != null && string.Equals(x.Parent, bone.Name, StringComparison.OrdinalIgnoreCase)))
            {
                if (childBone.Parent != null && childBone.Parent.Equals(childBone.Name))
                {
                    continue;
                }

                if (string.IsNullOrWhiteSpace(childBone.Name))
                {
                    childBone.Name = Guid.NewGuid().ToString();
                }

                var child = ProcessBone(texture, source, childBone, vertices, textureSize, modelBones);
                child.Parent = modelBone;

                modelBone.AddChild(child);

                if (!modelBones.TryAdd(childBone.Name, child))
                {
                    Log.Warn($"Failed to add bone! {childBone.Name}");
                    break;
                }
            }

            return(modelBone);
        }
Exemple #7
0
        private ModelBone ProcessBone(
            EntityModel source,
            EntityModelBone bone,
            ref List <VertexPositionColorTexture> vertices,
            Vector2 textureSize,
            Dictionary <string, ModelBone> modelBones)
        {
            ModelBone modelBone;

            int startIndex   = vertices.Count;
            int elementCount = 0;

            if (bone.Cubes != null)
            {
                foreach (var cube in bone.Cubes)
                {
                    if (cube == null)
                    {
                        Log.Warn("Cube was null!");

                        continue;
                    }

                    var inflation = (float)(cube.Inflate ?? bone.Inflate);
                    var mirror    = cube.Mirror ?? bone.Mirror;

                    var origin = cube.InflatedOrigin(inflation);

                    MCMatrix matrix = MCMatrix.CreateTranslation(origin);
                    if (cube.Rotation.HasValue)
                    {
                        var rotation = cube.Rotation.Value;

                        Vector3 pivot = origin + (cube.InflatedSize(inflation) / 2f);

                        if (cube.Pivot.HasValue)
                        {
                            pivot = cube.InflatedPivot(inflation);                            // cube.Pivot.Value;
                        }

                        matrix =
                            MCMatrix.CreateTranslation(origin)
                            * MCMatrix.CreateTranslation((-pivot))
                            * MCMatrix.CreateRotationDegrees(rotation)
                            * MCMatrix.CreateTranslation(pivot);
                    }

                    Cube built = new Cube(cube, textureSize, mirror, inflation);
                    ModifyCubeIndexes(ref vertices, built.Front, origin, matrix);
                    ModifyCubeIndexes(ref vertices, built.Back, origin, matrix);
                    ModifyCubeIndexes(ref vertices, built.Top, origin, matrix);
                    ModifyCubeIndexes(ref vertices, built.Bottom, origin, matrix);
                    ModifyCubeIndexes(ref vertices, built.Left, origin, matrix);
                    ModifyCubeIndexes(ref vertices, built.Right, origin, matrix);
                }
            }

            elementCount = vertices.Count - startIndex;

            modelBone = new ModelBone(bone, startIndex, elementCount);

            if (bone.Rotation.HasValue)
            {
                var r = bone.Rotation.Value;
                modelBone.BindingRotation = new Vector3(r.X, r.Y, r.Z);
            }

            if (bone.BindPoseRotation.HasValue)
            {
                var r = bone.BindPoseRotation.Value;
                modelBone.BindingRotation += new Vector3(r.X, r.Y, r.Z);
            }

            foreach (var childBone in source.Bones.Where(
                         x => x.Parent != null && string.Equals(x.Parent, bone.Name, StringComparison.OrdinalIgnoreCase)))
            {
                if (childBone.Parent != null && childBone.Parent.Equals(childBone.Name))
                {
                    continue;
                }

                if (string.IsNullOrWhiteSpace(childBone.Name))
                {
                    childBone.Name = Guid.NewGuid().ToString();
                }

                var child = ProcessBone(source, childBone, ref vertices, textureSize, modelBones);
                //child.Parent = modelBone;

                modelBone.AddChild(child);

                if (!modelBones.TryAdd(childBone.Name, child))
                {
                    Log.Warn($"Failed to add bone! {childBone.Name}");
                    break;
                }
            }

            return(modelBone);
        }
Exemple #8
0
        private ModelBone ProcessBone(EntityModel source, EntityModelBone bone, List <VertexPositionNormalTexture> vertices, Vector2 uvScale, Vector2 textureSize, Dictionary <string, ModelBone> modelBones)
        {
            ModelBone modelBone;

            List <short> indices = new List <short>();

            if (bone.Cubes != null)
            {
                foreach (var cube in bone.Cubes)
                {
                    if (cube == null)
                    {
                        Log.Warn("Cube was null!");
                        continue;
                    }

                    Cube built = new Cube(cube.Size * (float)cube.Inflate, textureSize);
                    built.Mirrored = bone.Mirror;
                    built.BuildCube(cube.Uv * uvScale);

                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Front);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Back);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Top);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Bottom);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Left);
                    vertices = ModifyCubeIndexes(vertices, cube, ref built.Right);

                    indices.AddRange(built.Front.indexes.Concat(built.Back.indexes).Concat(built.Top.indexes)
                                     .Concat(built.Bottom.indexes).Concat(built.Left.indexes).Concat(built.Right.indexes)
                                     .ToArray());
                }
            }

            var bindPoseMatrix = Matrix.CreateTranslation(-bone.Pivot)
                                 * Matrix.CreateRotationX(MathUtils.ToRadians(-bone.BindPoseRotation.X))
                                 * Matrix.CreateRotationY(MathUtils.ToRadians(bone.BindPoseRotation.Y))
                                 * Matrix.CreateRotationZ(MathUtils.ToRadians(bone.BindPoseRotation.Z))
                                 * Matrix.CreateTranslation(bone.Pivot);

            /*var boneMatrix = Matrix.Identity * Matrix.CreateTranslation(-bone.Pivot)
             * Matrix.CreateFromAxisAngle(
             *                                       Vector3.Right, MathUtils.ToRadians(bone.Rotation.X))
             * Matrix.CreateFromAxisAngle(
             *                                       Vector3.Backward, MathUtils.ToRadians(bone.Rotation.Z))
             * Matrix.CreateFromAxisAngle(
             *                                       Vector3.Up, MathUtils.ToRadians(bone.Rotation.Y))
             * Matrix.CreateTranslation(bone.Pivot);*/
            var boneMatrix =
                Matrix.CreateTranslation(-bone.Pivot)
                * Matrix.CreateRotationX(MathUtils.ToRadians(-bone.Rotation.X))
                * Matrix.CreateRotationY(MathUtils.ToRadians(bone.Rotation.Y))
                * Matrix.CreateRotationZ(MathUtils.ToRadians(bone.Rotation.Z))
                * Matrix.CreateTranslation(bone.Pivot);

            modelBone = new ModelBone(Texture, indices.ToArray(), bone.Parent, bone, bindPoseMatrix * boneMatrix);

            if (!string.IsNullOrWhiteSpace(bone.Parent))
            {
                ModelBone parentBone = null;
                if (!modelBones.TryGetValue(bone.Parent, out parentBone))
                {
                    var result = source.Bones.FirstOrDefault(
                        x => x.Name.Equals(bone.Parent, StringComparison.InvariantCultureIgnoreCase));

                    if (result != null)
                    {
                        parentBone = ProcessBone(source, result, vertices, uvScale, textureSize, modelBones);
                    }
                }

                if (parentBone != null)
                {
                    modelBone.Parent = parentBone;
                    parentBone.AddChild(modelBone);
                }
                else
                {
                    Log.Warn($"Could not find parent-bone \"{bone.Parent}\" for bonw {bone.Name} on type: {source.Name}");
                }
            }

            //modelBone.UpdateRotationMatrix = !bone.NeverRender;
            if (!modelBones.TryAdd(bone.Name, modelBone))
            {
                Log.Debug($"Failed to add bone! {bone.Name}");
            }

            return(modelBone);
        }