コード例 #1
0
        private bool DetectOnGround()
        {
            var entityBoundingBox =
                Entity.BoundingBox;

            //	entityBoundingBox = new BoundingBox(entityBoundingBox.Min);

            //var boundingCylinder = new BoundingCylinder(e.KnownPosition.)

            var offset = 0f;

            //if (Math.Round(entityBoundingBox.Min.Y) <= (int) entityBoundingBox.Min.Y)
            if (entityBoundingBox.Min.Y % 1 < 0.05f)
            {
                offset = -1f;
            }

            bool foundGround = false;

            foreach (var corner in entityBoundingBox.GetCorners()
                     .Where(x => Math.Abs(x.Y - entityBoundingBox.Min.Y) < 0.001f))
            {
                var blockcoords = new BlockCoordinates(
                    new PlayerLocation(corner.X, Math.Floor(corner.Y + offset), corner.Z));

                var block = Entity.Level.GetBlockState(blockcoords.X, blockcoords.Y, blockcoords.Z);

                if (block?.Model == null || !block.Block.Solid)
                {
                    continue;
                }

                foreach (var box in block.Model.GetBoundingBoxes(blockcoords).OrderBy(x => x.Max.Y))
                {
                    var yDifference = MathF.Abs(entityBoundingBox.Min.Y - box.Max.Y);                     // <= 0.01f

                    if (yDifference > 0.015f)
                    {
                        continue;
                    }

                    if (box.Contains(corner) == ContainmentType.Contains)
                    {
                        foundGround = true;
                    }
                    //return true;
                }
            }

            return(foundGround);
        }
コード例 #2
0
        public void Velocity(Vector3 velocity)
        {
            var oldLength = (Entity.Velocity).Length();

            if (oldLength < velocity.Length())
            {
                Entity.Velocity += new Vector3(velocity.X - Entity.Velocity.X, velocity.Y - Entity.Velocity.Y, velocity.Z - Entity.Velocity.Z);
            }
            else
            {
                Entity.Velocity = new Vector3(
                    MathF.Abs(Entity.Velocity.X) < 0.0001f ? velocity.Y : Entity.Velocity.X,
                    MathF.Abs(Entity.Velocity.Y) < 0.0001f ? velocity.Y : Entity.Velocity.Y,
                    MathF.Abs(Entity.Velocity.Z) < 0.0001f ? velocity.Z : Entity.Velocity.Z);
            }
        }
コード例 #3
0
        protected void GetVertices(IBlockAccess world, ChunkData chunkBuilder, BlockCoordinates blockCoordinates,
                                   Vector3 position, Block baseBlock,
                                   BlockStateModel[] models)
        {
            {
                var biome = world == null?BiomeUtils.GetBiomeById(0) : world.GetBiome(position);

                if (UseRandomizer)
                {
                    BlockStateModel selectedModel = null;
                    var             rnd           = MathF.Abs(NoiseGenerator.GetValue(position.X * position.Y, position.Z * position.X)) * WeightSum;

                    for (var index = 0; index < models.Length; index++)
                    {
                        var model = models[index];
                        rnd -= model.Weight;

                        if (rnd < 0)
                        {
                            selectedModel = model;

                            break;
                        }
                    }

                    CalculateModel(
                        world, blockCoordinates, chunkBuilder, position, baseBlock, selectedModel, biome);
                }
                else
                {
                    for (var bsModelIndex = 0; bsModelIndex < models.Length; bsModelIndex++)
                    {
                        var bsModel = models[bsModelIndex];

                        if (bsModel.Model == null)
                        {
                            continue;
                        }

                        CalculateModel(
                            world, blockCoordinates, chunkBuilder, position, baseBlock, bsModel,
                            biome);
                    }
                }
            }
        }
コード例 #4
0
        public override void GetVertices(IBlockAccess world, ChunkData chunkBuilder, BlockCoordinates blockCoordinates, Vector3 position, Block baseBlock)
        {
            var biome = world == null?BiomeUtils.GetBiomeById(0) : world.GetBiome(position);

            if (UseRandomizer)
            {
                BlockStateModelWrapper selectedModel = null;

                var rnd = MathF.Abs(NoiseGenerator.GetValue(position.X * position.Y, position.Z * position.X))
                          * WeightSum;

                for (var index = 0; index < Models.Length; index++)
                {
                    var model = Models[index];
                    rnd -= model.BlockStateModel.Weight;

                    if (rnd < 0)
                    {
                        selectedModel = model;

                        break;
                    }
                }

                CalculateModel(world, blockCoordinates, chunkBuilder, position, baseBlock, selectedModel.BlockStateModel, selectedModel.BlockModel, biome);
            }
            else
            {
                for (var bsModelIndex = 0; bsModelIndex < Models.Length; bsModelIndex++)
                {
                    var bsModel = Models[bsModelIndex];

                    //	if (string.IsNullOrWhiteSpace(bsModel.ModelName)) continue;

                    CalculateModel(world, blockCoordinates, chunkBuilder, position, baseBlock, bsModel.BlockStateModel, bsModel.BlockModel, biome);
                }
            }
        }
コード例 #5
0
        public void MoveTo(PlayerLocation location, bool updateLook = true)
        {
            var distance = Microsoft.Xna.Framework.Vector3.Distance(
                Entity.KnownPosition.ToVector3() * new Vector3(1f, 0f, 1f), location.ToVector3() * new Vector3(1f, 0f, 1f));

            //var difference =
            Entity.KnownPosition = location;

            //Entity.KnownPosition.X = location.X;
            //Entity.KnownPosition.Y = location.Y;
            //Entity.KnownPosition.Z = location.Z;
            Entity.KnownPosition.OnGround = location.OnGround;

            if (updateLook)
            {
                Entity.KnownPosition.Yaw     = location.Yaw;
                Entity.KnownPosition.HeadYaw = location.HeadYaw;
                Entity.KnownPosition.Pitch   = location.Pitch;
            }

            UpdateTarget();

            Entity.DistanceMoved += MathF.Abs(distance);
        }
コード例 #6
0
ファイル: PlayerMob.cs プロジェクト: wqd1019dqw/Alex
        public override void Update(IUpdateArgs args)
        {
            base.Update(args);

            var dt = (float)args.GameTime.ElapsedGameTime.TotalSeconds;

            if (IsSneaking)
            {
                _body.Rotation = new Vector3(-35f, _body.Rotation.Y, _body.Rotation.Z);
                _body.Position = Vector3.Forward * 7.5f;
                //_head.Position = new Vector3(_body.Position.X, 0.25f, 0f);

                _leftArmModel.Rotation  = new Vector3(72f, 0f, 0f);
                _rightArmModel.Rotation = new Vector3(72f, 0f, 0f);
            }
            else
            {
                _body.Position = Vector3.Zero;
                _body.Rotation = new Vector3(0f);


                var moveSpeed = MovementSpeed;
                var tcos0     = (float)(Math.Cos(DistanceMoved * 38.17) * moveSpeed) * 57.3f;
                var tcos1     = -tcos0;

                var   pos    = KnownPosition.ToVector3();
                float deltaX = pos.X - _prevUpdatePosition.X;
                float deltaZ = pos.Z - _prevUpdatePosition.Z;
                float distSQ = deltaX * deltaX + deltaZ * deltaZ;

                //_armRotation = _armRotation;

                // Test arm rotations
                if (_leftArmModel != null && _rightArmModel != null)
                {
                    //var lArmRot = new Vector3((0.5f + MathF.Sin(_armRotation)) * 7.5f, 0f,
                    //	0.1f + (MathF.Cos(_armRotation) * 1.5f));
                    Vector3 rArmRot = Vector3.Zero;
                    var     lArmRot = new Vector3(tcos0, 0, 0);
                    if (distSQ > 0f)
                    {
                        _armRotation += (float)((new Vector3(Velocity.X, 0, Velocity.Z).Length()) + distSQ) * dt;

                        rArmRot = new Vector3((0.5f + MathF.Cos(_armRotation)) * 24.5f, 0, 0);
                    }
                    else
                    {
                        _armRotation += dt;
                        rArmRot       = new Vector3((0.5f + MathF.Cos(_armRotation)) * -7.5f, 0f,
                                                    0.1f + (MathF.Sin(_armRotation) * -1.5f));
                    }

                    _leftArmModel.Rotation     = rArmRot;
                    _rightArmModel.Rotation    = -rArmRot;
                    _rightSleeveModel.Rotation = -rArmRot;
                    _leftSleeveModel.Rotation  = rArmRot;
                }


                if (_leftLegModel != null && _rightLegModel != null)
                {
                    Vector3 lLegRot = Vector3.Zero;
                    Vector3 rLegRot = Vector3.Zero;

                    if (distSQ > 0f)
                    {
                        _legRotation += (float)((new Vector3(Velocity.X, 0, Velocity.Z).Length()) + distSQ) * dt;

                        lLegRot = new Vector3(MathF.Sin(_legRotation) * 34.5f, 0f, 0f);
                        rLegRot = new Vector3(-MathF.Sin(_legRotation) * 34.5f, 0f, 0f);

                        _prevUpdatePosition = pos;
                    }
                    else
                    {
                        _legRotation = 0f;
                    }

                    _leftLegModel.Rotation    = lLegRot;
                    _rightLegModel.Rotation   = rLegRot;
                    _leftPantsModel.Rotation  = lLegRot;
                    _rightPantsModel.Rotation = rLegRot;
                }
            }
        }
コード例 #7
0
ファイル: ResourcePackBlockModel.cs プロジェクト: K4mey/Alex
        private Vector3 FixRotation(Vector3 v, ModelElement element, int xRot, int yRot)
        {
            if (element.Rotation.Axis != Axis.Undefined)
            {
                var r     = element.Rotation;
                var angle = (float)(r.Angle * (Math.PI / 180f));

                var origin = r.Origin;

                var c = MathF.Cos(angle);
                var s = MathF.Sin(angle);

                switch (r.Axis)
                {
                case Axis.Y:
                {
                    var x = v.X - origin.X;
                    var z = v.Z - origin.Z;

                    v.X = origin.X + (x * c - z * s);
                    v.Z = origin.Z + (z * c + x * s);
                }
                break;

                case Axis.X:
                {
                    var x = v.Z - origin.Z;
                    var z = v.Y - origin.Y;

                    v.Z = origin.Z + (x * c - z * s);
                    v.Y = origin.Y + (z * c + x * s);
                }
                break;

                case Axis.Z:
                {
                    var x = v.X - origin.X;
                    var z = v.Y - origin.Y;

                    v.X = origin.X + (x * c - z * s);
                    v.Y = origin.Y + (z * c + x * s);
                }
                break;
                }
            }

            if (xRot > 0)
            {
                var rotX = (float)(xRot * (Math.PI / 180f));
                var c    = MathF.Cos(rotX);
                var s    = MathF.Sin(rotX);
                var z    = v.Z - 8f;
                var y    = v.Y - 8f;

                v.Z = 8f + (z * c - y * s);
                v.Y = 8f + (y * c + z * s);
            }

            if (yRot > 0)
            {
                var rotX = (float)(yRot * (Math.PI / 180f));
                var c    = MathF.Cos(rotX);
                var s    = MathF.Sin(rotX);
                var z    = v.X - 8f;
                var y    = v.Z - 8f;

                v.X = 8f + (z * c - y * s);
                v.Z = 8f + (y * c + z * s);
            }

            return(v);
        }
コード例 #8
0
        private Dictionary <string, FaceCache> ProcessModel(BlockStateModel raw, out Vector3 min, out Vector3 max)
        {
            float facesMinX = float.MaxValue, facesMinY = float.MaxValue, facesMinZ = float.MaxValue;
            float facesMaxX = float.MinValue, facesMaxY = float.MinValue, facesMaxZ = float.MinValue;

            Dictionary <string, FaceCache> faceCaches = new Dictionary <string, FaceCache>();

            var model = raw.Model;

            for (var index = 0; index < model.Elements.Length; index++)
            {
                var element = model.Elements[index];
                element.To   *= Scale;
                element.From *= Scale;

                FaceCache cache = new FaceCache();

                foreach (var face in element.Faces)
                {
                    var   uv = face.Value.UV;
                    float x1 = 0, x2 = 0, y1 = 0, y2 = 0;
                    if (uv == null)
                    {
                        switch (face.Key)
                        {
                        case BlockFace.North:
                        case BlockFace.South:
                            x1 = element.From.X;
                            x2 = element.To.X;
                            y1 = 16f - element.To.Y;
                            y2 = 16f - element.From.Y;
                            break;

                        case BlockFace.West:
                        case BlockFace.East:
                            x1 = element.From.Z;
                            x2 = element.To.Z;
                            y1 = 16f - element.To.Y;
                            y2 = 16f - element.From.Y;
                            break;

                        case BlockFace.Down:
                        case BlockFace.Up:
                            x1 = element.From.X;
                            x2 = element.To.X;
                            y1 = 16f - element.To.Z;
                            y2 = 16f - element.From.Z;
                            break;
                        }
                    }
                    else
                    {
                        x1 = uv.X1;
                        x2 = uv.X2;
                        y1 = uv.Y1;
                        y2 = uv.Y2;
                    }

                    var verts = GetFaceVertices(face.Key, element.From, element.To,
                                                GetTextureUVMap(Resources, ResolveTexture(raw, face.Value.Texture), x1, x2, y1, y2, face.Value.Rotation),
                                                out int[] indexes);

                    float minX = float.MaxValue, minY = float.MaxValue, minZ = float.MaxValue;
                    float maxX = float.MinValue, maxY = float.MinValue, maxZ = float.MinValue;

                    for (int i = 0; i < verts.Length; i++)
                    {
                        var v = verts[i];

                        if (element.Rotation.Axis != Axis.Undefined)
                        {
                            var r     = element.Rotation;
                            var angle = (float)(r.Angle * (Math.PI / 180f));

                            //angle = r.Axis == Axis.Z ? angle : -angle;
                            //angle = r.Axis == Axis.Y ? -angle : angle;

                            var origin = r.Origin;

                            var c = MathF.Cos(angle);
                            var s = MathF.Sin(angle);

                            switch (r.Axis)
                            {
                            case Axis.Y:
                            {
                                var x = v.Position.X - origin.X;
                                var z = v.Position.Z - origin.Z;

                                v.Position.X = origin.X + (x * c - z * s);
                                v.Position.Z = origin.Z + (z * c + x * s);
                            }
                            break;

                            case Axis.X:
                            {
                                var x = v.Position.Z - origin.Z;
                                var z = v.Position.Y - origin.Y;

                                v.Position.Z = origin.Z + (x * c - z * s);
                                v.Position.Y = origin.Y + (z * c + x * s);
                            }
                            break;

                            case Axis.Z:
                            {
                                var x = v.Position.X - origin.X;
                                var z = v.Position.Y - origin.Y;

                                v.Position.X = origin.X + (x * c - z * s);
                                v.Position.Y = origin.Y + (z * c + x * s);
                            }
                            break;
                            }
                        }

                        if (raw.X > 0)
                        {
                            var rotX = (float)(raw.X * (Math.PI / 180f));
                            var c    = MathF.Cos(rotX);
                            var s    = MathF.Sin(rotX);
                            var z    = v.Position.Z - 8f;
                            var y    = v.Position.Y - 8f;

                            v.Position.Z = 8f + (z * c - y * s);
                            v.Position.Y = 8f + (y * c + z * s);
                        }

                        if (raw.Y > 0)
                        {
                            var rotX = (float)(raw.Y * (Math.PI / 180f));
                            var c    = MathF.Cos(rotX);
                            var s    = MathF.Sin(rotX);
                            var z    = v.Position.X - 8f;
                            var y    = v.Position.Z - 8f;

                            v.Position.X = 8f + (z * c - y * s);
                            v.Position.Z = 8f + (y * c + z * s);
                        }

                        v.Position /= 16f;

                        if (v.Position.X < minX)
                        {
                            minX = v.Position.X;
                        }
                        else if (v.Position.X > maxX)
                        {
                            maxX = v.Position.X;
                        }

                        if (v.Position.Y < minY)
                        {
                            minY = v.Position.Y;
                        }
                        else if (v.Position.Y > maxY)
                        {
                            maxY = v.Position.Y;
                        }

                        if (v.Position.Z < minZ)
                        {
                            minZ = v.Position.Z;
                        }
                        else if (v.Position.Z > maxZ)
                        {
                            maxZ = v.Position.Z;
                        }

                        verts[i] = v;
                    }

                    if (element.Rotation.Axis != Axis.Undefined && element.Rotation.Rescale)
                    {
                        var diffX = maxX - minX;
                        var diffY = maxY - minY;
                        var diffZ = maxZ - minZ;

                        for (var i = 0; i < verts.Length; i++)
                        {
                            var v = verts[i];

                            v.Position.X = (v.Position.X - minX) / diffX;
                            v.Position.Y = (v.Position.Y - minY) / diffY;
                            v.Position.Z = (v.Position.Z - minZ) / diffZ;

                            verts[i] = v;

                            if (v.Position.X < facesMinX)
                            {
                                facesMinX = v.Position.X;
                            }
                            else if (v.Position.X > facesMaxX)
                            {
                                facesMaxX = v.Position.X;
                            }

                            if (v.Position.Y < facesMinY)
                            {
                                facesMinY = v.Position.Y;
                            }
                            else if (v.Position.Y > facesMaxY)
                            {
                                facesMaxY = v.Position.Y;
                            }

                            if (v.Position.Z < facesMinZ)
                            {
                                facesMinZ = v.Position.Z;
                            }
                            else if (v.Position.Z > facesMaxZ)
                            {
                                facesMaxZ = v.Position.Z;
                            }
                        }
                    }

                    if (minX < facesMinX)
                    {
                        facesMinX = minX;
                    }
                    else if (maxX > facesMaxX)
                    {
                        facesMaxX = maxX;
                    }

                    if (minY < facesMinY)
                    {
                        facesMinY = minY;
                    }
                    else if (maxY > facesMaxY)
                    {
                        facesMaxY = maxY;
                    }

                    if (minZ < facesMinZ)
                    {
                        facesMinZ = minZ;
                    }
                    else if (maxZ > facesMaxZ)
                    {
                        facesMaxZ = maxZ;
                    }

                    cache.Set(face.Key, new FaceData(verts, indexes, face.Value.Rotation, null));
                }
                faceCaches.Add(index.ToString(), cache);
            }

            min = new Vector3(facesMinX, facesMinY, facesMinZ);
            max = new Vector3(facesMaxX, facesMaxY, facesMaxZ);

            return(faceCaches);
        }
コード例 #9
0
        public Vector3 Move(Vector3 amount)
        {
            var   oldPosition = Entity.KnownPosition.ToVector3();
            float offset      = 0f;

            //TODO: Fix position offset

            var beforeAdjustment = new Vector3(amount.X, amount.Y, amount.Z);

            List <BoundingBox> boxes = new List <BoundingBox>();

            if (TestTerrainCollisionY(ref amount, out var yCollisionPoint, out var yBox, boxes))
            {
                if (beforeAdjustment.Y <= 0f)
                {
                    Entity.KnownPosition.OnGround = true;
                }

                Entity.Velocity *= new Vector3(1f, 0f, 1f);
                //Entity.Velocity += new Vector3(0f, direction.Y, 0f);

                Entity.CollidedWithWorld(
                    beforeAdjustment.Y < 0 ? Vector3.Down : Vector3.Up, yCollisionPoint,
                    beforeAdjustment.Y);
            }

            float collisionX = 0f;
            bool  collideX   = TestTerrainCollisionX(ref amount, out var xCollisionPoint, out var xBox, out collisionX, boxes);

            float collisionZ = 0f;
            bool  collideZ   = TestTerrainCollisionZ(ref amount, out var zCollisionPoint, out var zBox, out collisionZ, boxes);

            var canJump = true;

            if (Entity.KnownPosition.OnGround)
            {
                var adjusted     = Entity.GetBoundingBox(Entity.KnownPosition + amount);
                var intersecting = PhysicsManager.GetIntersecting(Entity.Level, adjusted);
                var targetY      = 0f;
                //if (!PhysicsManager.GetIntersecting(Entity.Level, adjusted).Any(bb => bb.Max.Y >= adjusted.Min.Y && bb.Min.Y <= adjusted.Max.Y))
                foreach (var box in intersecting)
                {
                    var yDifference = box.Max.Y - Entity.BoundingBox.Min.Y;

                    if (yDifference > MaxJumpHeight)
                    {
                        canJump = false;
                        break;
                    }

                    if (yDifference > targetY)
                    {
                        targetY = yDifference;
                    }
                }

                if (canJump && targetY > 0f)
                {
                    amount.Y = targetY;
                    //var a = intersecting.
                }
                else
                {
                    canJump = false;
                }
            }
            else
            {
                canJump = false;
            }

            if (!canJump)
            {
                if (collideX)
                {
                    //Entity.Velocity += new Vector3(direction.X, 0f, 0f);

                    Entity.CollidedWithWorld(
                        beforeAdjustment.X < 0 ? Vector3.Left : Vector3.Right, xCollisionPoint,
                        beforeAdjustment.X);

                    var dir = (xCollisionPoint - Entity.KnownPosition);
                    dir.Normalize();
                    var block = Entity.Level.GetBlockState(xCollisionPoint);
                    if (block != null && block.Block.CanClimb(dir.GetBlockFace()))
                    {
                        //amount.Y += MathF.Abs(beforeAdjustment.X);
                        amount.X        = 0;
                        amount.Y        = Math.Max(amount.Y, beforeAdjustment.X * 0.3f);
                        Entity.Velocity = new Vector3(0f, Entity.Velocity.Y, Entity.Velocity.Z);
                    }
                    else
                    {
                        amount.X = collisionX;

                        Entity.Velocity = new Vector3(0f, Entity.Velocity.Y, Entity.Velocity.Z);
                    }
                }

                if (collideZ)
                {
                    amount.Z        = collisionZ;
                    Entity.Velocity = new Vector3(Entity.Velocity.X, Entity.Velocity.Y, 0f);
                    //Entity.Velocity *= new Vector3(1f, 1f, 0f);
                    //Entity.Velocity += new Vector3(0f, 0f, direction.Z);

                    Entity.CollidedWithWorld(
                        beforeAdjustment.Z < 0 ? Vector3.Backward : Vector3.Forward, zCollisionPoint,
                        beforeAdjustment.Z);

                    var dir = (zCollisionPoint - Entity.KnownPosition);
                    dir.Normalize();

                    var block = Entity.Level.GetBlockState(zCollisionPoint);
                    if (block != null && block.Block.CanClimb(dir.GetBlockFace()))
                    {
                        amount.Z        = 0;
                        amount.Y        = Math.Max(amount.Y, beforeAdjustment.Z * 0.3f);
                        Entity.Velocity = new Vector3(Entity.Velocity.X, Entity.Velocity.Y, 0f);
                    }
                    else
                    {
                        amount.Z = collisionZ;

                        Entity.Velocity = new Vector3(Entity.Velocity.X, Entity.Velocity.Y, 0f);
                    }
                }
            }

            Entity.KnownPosition += amount;
            //Entity.KnownPosition.Y += (amount.Y - offset);
            //Entity.KnownPosition.Z += amount.Z;

            Entity.KnownPosition.OnGround = DetectOnGround();

            if (boxes.Count > 0)
            {
                LastCollision = boxes.ToArray();
            }
            //Entity.Velocity = direction;

            UpdateTarget();

            Entity.DistanceMoved +=
                MathF.Abs(Microsoft.Xna.Framework.Vector3.Distance(oldPosition * new Vector3(1f, 0f, 1f), Entity.KnownPosition.ToVector3() * new Vector3(1f, 0f, 1f)));

            return(amount);
        }
コード例 #10
0
        private BlockShaderVertex[] ProcessVertices(BlockShaderVertex[] vertices,
                                                    BlockStateModel bsModel,
                                                    ModelElement element,
                                                    BlockTextureData?uvMap,
                                                    BlockFace blockFace,
                                                    ModelElementFace face)
        {
            for (int i = 0; i < vertices.Length; i++)
            {
                var v = vertices[i];

                v.Position /= 16f;
                v.Position  = FixRotation(v.Position, element);

                if (bsModel.X > 0)
                {
                    var rotX = bsModel.X * (MathHelper.Pi / 180f);
                    var c    = MathF.Cos(rotX);
                    var s    = MathF.Sin(rotX);
                    var z    = v.Position.Z - 0.5f;
                    var y    = v.Position.Y - 0.5f;

                    v.Position.Z = 0.5f + (z * c - y * s);
                    v.Position.Y = 0.5f + (y * c + z * s);
                }

                if (bsModel.Y > 0)
                {
                    var rotY = bsModel.Y * (MathHelper.Pi / 180f);
                    var c    = MathF.Cos(rotY);
                    var s    = MathF.Sin(rotY);
                    var x    = v.Position.X - 0.5f;
                    var z    = v.Position.Z - 0.5f;

                    v.Position.X = 0.5f + (x * c - z * s);
                    v.Position.Z = 0.5f + (z * c + x * s);
                }

                if (uvMap.HasValue)
                {
                    var tw = uvMap.Value.TextureInfo.Width;
                    var th = uvMap.Value.TextureInfo.Height;

                    var rot = face.Rotation;

                    if (rot > 0)
                    {
                        var rotY = rot * (MathHelper.Pi / 180f);
                        var c    = MathF.Cos(rotY);
                        var s    = MathF.Sin(rotY);
                        var x    = v.TexCoords.X - 8f * tw;
                        var y    = v.TexCoords.Y - 8f * th;

                        v.TexCoords.X = 8f * tw + (x * c - y * s);
                        v.TexCoords.Y = 8f * th + (y * c + x * s);
                    }

                    if (bsModel.Uvlock)
                    {
                        if (bsModel.Y > 0 && (blockFace == BlockFace.Up || blockFace == BlockFace.Down))
                        {
                            var rotY = bsModel.Y * (MathHelper.Pi / 180f);
                            var c    = MathF.Cos(rotY);
                            var s    = MathF.Sin(rotY);
                            var x    = v.TexCoords.X - 8f * tw;
                            var y    = v.TexCoords.Y - 8f * th;

                            v.TexCoords.X = 8f * tw + (x * c - y * s);
                            v.TexCoords.Y = 8f * th + (y * c + x * s);
                        }

                        if (bsModel.X > 0 && (blockFace != BlockFace.Up && blockFace != BlockFace.Down))
                        {
                            var rotX = bsModel.X * (MathHelper.Pi / 180f);
                            var c    = MathF.Cos(rotX);
                            var s    = MathF.Sin(rotX);
                            var x    = v.TexCoords.X - 8f * tw;
                            var y    = v.TexCoords.Y - 8f * th;

                            v.TexCoords.X = 8f * tw + (x * c - y * s);
                            v.TexCoords.Y = 8f * th + (y * c + x * s);
                        }
                    }

                    v.TexCoords += uvMap.Value.TextureInfo.Position;
                    v.TexCoords *= (Vector2.One / uvMap.Value.TextureInfo.AtlasSize);
                }

                v.Face      = blockFace;
                vertices[i] = v;
            }

            return(vertices);
        }
コード例 #11
0
        private Vector3 FixRotation(
            Vector3 v,
            ModelElement element)
        {
            if (element.Rotation.Axis != Axis.Undefined)
            {
                var r     = element.Rotation;
                var angle = (float)(r.Angle * (Math.PI / 180f));
                angle = (element.Rotation.Axis == Axis.Z) ? angle : -angle;

                var ci = 1.0f / MathF.Cos(angle);

                var origin = r.Origin;

                var c = MathF.Cos(angle);
                var s = MathF.Sin(angle);

                v.X -= (origin.X / 16.0f);
                v.Y -= (origin.Y / 16.0f);
                v.Z -= (origin.Z / 16.0f);

                switch (r.Axis)
                {
                case Axis.Y:
                {
                    var x = v.X;
                    var z = v.Z;

                    v.X = (x * c - z * s);
                    v.Z = (z * c + x * s);

                    if (r.Rescale)
                    {
                        v.X *= ci;
                        v.Z *= ci;
                    }
                }
                break;

                case Axis.X:
                {
                    var x = v.Z;
                    var z = v.Y;

                    v.Z = (x * c - z * s);
                    v.Y = (z * c + x * s);

                    if (r.Rescale)
                    {
                        v.Z *= ci;
                        v.Y *= ci;
                    }
                }
                break;

                case Axis.Z:
                {
                    var x = v.X;
                    var z = v.Y;

                    v.X = (x * c - z * s);
                    v.Y = (z * c + x * s);

                    if (r.Rescale)
                    {
                        v.X *= ci;
                        v.Y *= ci;
                    }
                }
                break;
                }

                v.X += (origin.X / 16.0f);
                v.Y += (origin.Y / 16.0f);
                v.Z += (origin.Z / 16.0f);
            }

            return(v);
        }