Exemple #1
0
        private ModelBone ProcessBone(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);

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

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

            return(modelBone);
        }
        private ModelBone ProcessBone(EntityModelBone bone, List <VertexPositionNormalTexture> vertices, Vector2 uvScale, Vector2 textureSize, Dictionary <string, ModelBone> modelBones)
        {
            List <ModelBoneCube> cubes = new List <ModelBoneCube>();
            ModelBone            modelBone;

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

                    var size     = cube.Size;
                    var origin   = cube.Origin;
                    var pivot    = cube.Pivot;
                    var rotation = cube.Rotation;

                    origin = new Vector3(-(origin.X + size.X), origin.Y, origin.Z);

                    //VertexPositionNormalTexture[] vertices;
                    Cube built = new Cube(size * (float)cube.Inflate, textureSize);
                    built.Mirrored = bone.Mirror;
                    built.BuildCube(cube.Uv * uvScale);
                    vertices = ModifyCubeIndexes(vertices, ref built.Front);
                    vertices = ModifyCubeIndexes(vertices, ref built.Back);
                    vertices = ModifyCubeIndexes(vertices, ref built.Top);
                    vertices = ModifyCubeIndexes(vertices, ref built.Bottom);
                    vertices = ModifyCubeIndexes(vertices, ref built.Left);
                    vertices = ModifyCubeIndexes(vertices, ref built.Right);

                    var part = new ModelBoneCube(built.Front.indexes
                                                 .Concat(built.Back.indexes)
                                                 .Concat(built.Top.indexes)
                                                 .Concat(built.Bottom.indexes)
                                                 .Concat(built.Left.indexes)
                                                 .Concat(built.Right.indexes)
                                                 .ToArray(), Texture, rotation, pivot, origin);

                    part.Mirror = bone.Mirror;
                    cubes.Add(part);
                }
            }

            modelBone = new ModelBone(cubes.ToArray(), bone.Parent, bone);

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

            return(modelBone);
        }
Exemple #3
0
        private void Cache(Dictionary <string, ModelBone> modelBones)
        {
            List <EntityModelBone> headBones = new List <EntityModelBone>();

            var headBone =
                Model.Bones.FirstOrDefault(x => x.Name.Contains("head", StringComparison.InvariantCultureIgnoreCase));

            if (headBone != null)
            {
                headBones.Add(headBone);
                foreach (var bone in Model.Bones)
                {
                    if (bone == headBone)
                    {
                        continue;
                    }
                    if (bone.Parent.Equals(headBone.Name))
                    {
                        headBones.Add(bone);
                    }
                }

                foreach (var bone in Model.Bones.Where(x =>
                                                       !headBones.Any(hb => hb.Name.Equals(x.Name, StringComparison.InvariantCultureIgnoreCase))))
                {
                    if (headBones.Any(x => x.Name.Equals(bone.Name, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        headBones.Add(bone);
                    }
                }
            }

            List <VertexPositionNormalTexture> vertices = new List <VertexPositionNormalTexture>();

            foreach (var bone in Model.Bones)
            {
                if (bone == null)
                {
                    continue;
                }
                //	if (bone.NeverRender) continue;
                bool partOfHead = headBones.Contains(bone);

                //bone.Pivot = new Vector3(-bone.Pivot.X, bone.Pivot.Y, bone.Pivot.Z);
                List <ModelBoneCube> c = new List <ModelBoneCube>();
                ModelBone            modelBone;

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

                        var size     = cube.Size;
                        var origin   = cube.Origin;
                        var pivot    = bone.Pivot;
                        var rotation = bone.Rotation;

                        //VertexPositionNormalTexture[] vertices;
                        Cube built = new Cube(size, new Vector2(Texture.Width, Texture.Height));
                        built.Mirrored = bone.Mirror;
                        built.BuildCube(cube.Uv);

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

                        var part = new ModelBoneCube(built.Front.indexes
                                                     .Concat(built.Back.indexes)
                                                     .Concat(built.Top.indexes)
                                                     .Concat(built.Bottom.indexes)
                                                     .Concat(built.Left.indexes)
                                                     .Concat(built.Right.indexes)
                                                     .ToArray(), Texture, rotation, pivot, origin);

                        part.Mirror = bone.Mirror;
                        if (partOfHead)
                        {
                            part.ApplyHeadYaw = true;
                            part.ApplyYaw     = true;
                        }
                        else
                        {
                            part.ApplyPitch   = false;
                            part.ApplyYaw     = true;
                            part.ApplyHeadYaw = false;
                        }

                        c.Add(part);
                    }
                }

                modelBone = new ModelBone(c.ToArray(), bone);
                modelBone.UpdateRotationMatrix = !bone.NeverRender;
                if (!modelBones.TryAdd(bone.Name, modelBone))
                {
                    Log.Warn($"Failed to add bone! {Model.Name}:{bone.Name}");
                }
            }

            VertexBuffer = GpuResourceManager.GetBuffer(this, Alex.Instance.GraphicsDevice,
                                                        VertexPositionNormalTexture.VertexDeclaration, vertices.Count, BufferUsage.None);
            VertexBuffer.SetData(vertices.ToArray());
        }
Exemple #4
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);
        }