コード例 #1
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;
                }
            }
        }
コード例 #2
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);
        }
コード例 #3
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);
        }
コード例 #4
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);
        }
コード例 #5
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);
        }