Пример #1
0
        protected UVMap GetTextureUVMap(ResourceManager resources,
                                        string texture,
                                        float x1,
                                        float x2,
                                        float y1,
                                        float y2,
                                        int rot,
                                        Color color)
        {
            if (resources == null)
            {
                x1 = 0;
                x2 = 1 / 32f;
                y1 = 0;
                y2 = 1 / 32f;

                return(new UVMap(
                           new Microsoft.Xna.Framework.Vector2(x1, y1), new Microsoft.Xna.Framework.Vector2(x2, y1),
                           new Microsoft.Xna.Framework.Vector2(x1, y2), new Microsoft.Xna.Framework.Vector2(x2, y2), color,
                           color, color));
            }

            var textureInfo     = resources.Atlas.GetAtlasLocation(texture, out var uvSize);
            var textureLocation = textureInfo.Position;

            var xw = (textureInfo.Width / 16f) / uvSize.X;
            var yh = (textureInfo.Height / 16f) / uvSize.Y;

            textureLocation.X /= uvSize.X;
            textureLocation.Y /= uvSize.Y;

            x1 = textureLocation.X + (x1 * xw);
            x2 = textureLocation.X + (x2 * xw);
            y1 = textureLocation.Y + (y1 * yh);
            y2 = textureLocation.Y + (y2 * yh);

            var map = new UVMap(
                new Microsoft.Xna.Framework.Vector2(x1, y1), new Microsoft.Xna.Framework.Vector2(x2, y1),
                new Microsoft.Xna.Framework.Vector2(x1, y2), new Microsoft.Xna.Framework.Vector2(x2, y2), color, color,
                color, textureInfo.Animated);

            if (rot > 0)
            {
                map.Rotate(rot);
            }

            return(map);
        }
Пример #2
0
        public override (VertexPositionNormalTextureColor[] vertices, int[] indexes) GetVertices(IBlockAccess world, Vector3 vectorPos, Block baseBlock)
        {
            //Level = GetLevel(baseBlock.BlockState);

            var position = new BlockCoordinates(vectorPos);
            List <VertexPositionNormalTextureColor> result = new List <VertexPositionNormalTextureColor>(36);
            var indexResult = new List <int>();

            List <BlockFace> renderedFaces = new List <BlockFace>();

            foreach (var face in Enum.GetValues(typeof(BlockFace)).Cast <BlockFace>())
            {
                var pos = position + face.GetBlockCoordinates();

                bool shouldRenderFace = true;
                foreach (var blockState in world.GetBlockStates(pos.X, pos.Y, pos.Z))
                {
                    if (blockState.Storage != 0 && (blockState.State == null || (blockState.State.Block is Air)))
                    {
                        continue;
                    }

                    shouldRenderFace = baseBlock.ShouldRenderFace(face, blockState.State.Block);
                }

                if (shouldRenderFace)
                {
                    renderedFaces.Add(face);
                }
            }

            if (renderedFaces.Count == 0)
            {
                return(new VertexPositionNormalTextureColor[0], new int[0]);
            }

            int tl, tr, bl, br;

            //	var myLevel = GetLevel(baseBlock.BlockState);

            int lowestFound = 999;
            BlockCoordinates lowestBlock = BlockCoordinates.Up;

            bool isFlowing          = CheckFlowing(world, position);;
            int  rot                = 0;
            bool calculateDirection = false;

            var check    = position + BlockCoordinates.Up;
            var blocksUp = world.GetBlockStates(check.X, check.Y, check.Z).ToArray();            //.GetType();

            if ((IsWater && blocksUp.Any(
                     x => x.State.Block.Renderable && x.State.Block.BlockMaterial == Material.Water)) ||
                (IsLava && blocksUp.Any(
                     x => x.State.Block.Renderable && x.State.Block.BlockMaterial == Material.Lava))
                )
            {
                tl = 8;
                tr = 8;
                bl = 8;
                br = 8;

                rot       = 180;
                isFlowing = true;
            }
            else
            {
                if (isFlowing)
                {
                    calculateDirection = true;

                    tl = GetAverageLiquidLevels(world, position, out lowestBlock, out lowestFound);

                    tr = GetAverageLiquidLevels(world, position + BlockCoordinates.Right, out var trl, out var trv);
                    if (trv > lowestFound)
                    {
                        lowestBlock = trl;
                        lowestFound = trv;
                    }

                    bl = GetAverageLiquidLevels(world, position + BlockCoordinates.Forwards, out var bll, out var blv);
                    if (blv > lowestFound)
                    {
                        lowestBlock = bll;
                        lowestFound = blv;
                    }

                    br = GetAverageLiquidLevels(world, position + new BlockCoordinates(1, 0, 1), out var brl,
                                                out var brv);
                }
                else
                {
                    if (blocksUp.Any(x => x.State.Block.Solid && x.State.Block.Renderable))
                    {
                        tl = 8;
                        tr = 8;
                        bl = 8;
                        br = 8;
                    }
                    else
                    {
                        tl = 7;
                        tr = 7;
                        bl = 7;
                        br = 7;
                    }
                }

                //if (brv < lowestFound)
                //	lowestBlock = brl;
            }

            double lowestDistance = 9999;

            if (lowestBlock.Y <= position.Y && calculateDirection)
            {
                for (int i = 0; i < 4; i++)
                {
                    var rotation            = 0;
                    BlockCoordinates offset = BlockCoordinates.Zero;
                    switch (i)
                    {
                    case 0:
                        offset   = BlockCoordinates.North;
                        rotation = 180;
                        break;

                    case 1:
                        offset   = BlockCoordinates.South;
                        rotation = 0;
                        break;

                    case 2:
                        offset   = BlockCoordinates.East;
                        rotation = 270;
                        break;

                    case 3:
                        offset   = BlockCoordinates.West;
                        rotation = 90;
                        break;
                    }

                    var distance = Math.Abs(
                        (position + offset).DistanceTo(lowestBlock));
                    if (distance < lowestDistance)
                    {
                        lowestDistance = distance;
                        rot            = rotation;
                    }
                }
            }

            string texture = "";

            if (IsLava)
            {
                texture = "block/lava";
            }
            else
            {
                texture = "block/water";
            }
            //texture = texture + "_flow";
            if (isFlowing)
            {
                texture += "_flow";
            }
            else
            {
                texture += "_still";
            }

            //float frameX
            UVMap map = GetTextureUVMap(Alex.Instance.Resources, texture, 0, 16, 0, 16, 0, Color.White);

            var originalMap = new UVMap(map.TopLeft, map.TopRight, map.BottomLeft, map.BottomRight, map.ColorLeft, map.ColorTop, map.ColorBottom);

            //originalMap.Rotate(180);

            map.Rotate(rot);

            foreach (var face in renderedFaces)
            {
                float s     = 1f - Scale;
                var   start = Vector3.One * s;
                var   end   = Vector3.One * Scale;


                //if (b.BlockModel is LiquidBlockModel m && m.Level > Level && f != BlockFace.Up) continue;

                var faceMap = map;
                if (face != BlockFace.Up)
                {
                    faceMap = originalMap;
                }

                var vertices = GetFaceVertices(face, start, end, faceMap, out int[] indexes);

                float height       = 0;
                var   initialIndex = result.Count;
                for (var index = 0; index < vertices.Length; index++)
                {
                    var vert = vertices[index];
                    if (vert.Position.Y > start.Y)
                    {
                        const float modifier = 2f;
                        if (vert.Position.X == start.X && vert.Position.Z == start.Z)
                        {
                            height = (modifier * (tl));
                            rot    = 0;
                        }
                        else if (vert.Position.X != start.X && vert.Position.Z == start.Z)
                        {
                            height = (modifier * (tr));
                            rot    = 270;
                        }
                        else if (vert.Position.X == start.X && vert.Position.Z != start.Z)
                        {
                            height = (modifier * (bl));
                            rot    = 90;
                        }
                        else
                        {
                            height = (modifier * (br));
                            rot    = 270;
                        }

                        vert.Position.Y = height / 16.0f;                         //; + (position.Y);
                    }

                    vert.Position.Y += position.Y - s;
                    vert.Position.X += position.X;
                    vert.Position.Z += position.Z;

                    if (IsWater)
                    {
                        vert.Color = new Color(68, 175, 245);
                    }
                    else
                    {
                        vert.BlockLight = baseBlock.LightValue;
                    }
                    //vert.Color = AdjustColor(new Color(68, 175, 245), f,
                    //	GetLight(world, position + d), false);

                    result.Add(vert);
                }

                for (var index = 0; index < indexes.Length; index++)
                {
                    //var vert = vertices[index];
                    //var vert = vertices[indexes[index]];
                    indexResult.Add(initialIndex + indexes[index]);
                }
            }

            return(result.ToArray(), indexResult.ToArray());
        }
Пример #3
0
        public override (VertexPositionNormalTextureColor[] vertices, int[] indexes) GetVertices(IWorld world, Vector3 vectorPos, IBlock baseBlock)
        {
            var position = new BlockCoordinates(vectorPos);
            List <VertexPositionNormalTextureColor> result = new List <VertexPositionNormalTextureColor>(36);
            var indexResult = new List <int>();

            int tl = 0, tr = 0, bl = 0, br = 0;

            Level = baseBlock.BlockState.GetTypedValue(LEVEL);

            string b1, b2;

            if (IsLava)
            {
                b1 = "minecraft:lava";
                b2 = "minecraft:lava";
            }
            else
            {
                b1 = "minecraft:water";
                b2 = "minecraft:water";
            }

            int lowestFound = 999;
            BlockCoordinates lowestBlock = BlockCoordinates.Up;

            bool isFlowing          = isFlowing = Check(world, position);;
            int  rot                = 0;
            bool calculateDirection = false;
            var  bc = world.GetBlock(position + BlockCoordinates.Up);               //.GetType();

            if ((!IsLava && bc.IsWater) || (IsLava && bc.Name == "minecraft:lava")) //.Name == b1 || bc.Name == b2)
            {
                tl = 8;
                tr = 8;
                bl = 8;
                br = 8;

                rot = 180;
            }
            else
            {
                if (isFlowing)
                {
                    calculateDirection = true;

                    tl = GetAverageLiquidLevels(world, position, out lowestBlock, out lowestFound);

                    tr = GetAverageLiquidLevels(world, position + BlockCoordinates.Right, out var trl, out var trv);
                    if (trv < lowestFound)
                    {
                        lowestBlock = trl;
                        lowestFound = trv;
                    }

                    bl = GetAverageLiquidLevels(world, position + BlockCoordinates.Forwards, out var bll, out var blv);
                    if (blv < lowestFound)
                    {
                        lowestBlock = bll;
                        lowestFound = blv;
                    }

                    br = GetAverageLiquidLevels(world, position + new BlockCoordinates(1, 0, 1), out var brl,
                                                out var brv);
                }
                else
                {
                    tl = 8;
                    tr = 8;
                    bl = 8;
                    br = 8;
                }

                //if (brv < lowestFound)
                //	lowestBlock = brl;
            }

            double lowestDistance = 9999;

            if (lowestBlock.Y <= position.Y && calculateDirection)
            {
                for (int i = 0; i < 4; i++)
                {
                    var rotation            = 0;
                    BlockCoordinates offset = BlockCoordinates.Zero;
                    switch (i)
                    {
                    case 0:
                        offset   = BlockCoordinates.North;
                        rotation = 180;
                        break;

                    case 1:
                        offset   = BlockCoordinates.South;
                        rotation = 0;
                        break;

                    case 2:
                        offset   = BlockCoordinates.East;
                        rotation = 90;
                        break;

                    case 3:
                        offset   = BlockCoordinates.West;
                        rotation = 270;
                        break;

                    case 4:
                        //NorthWest
                        offset   = new BlockCoordinates(-1, 0, -1);
                        rotation = 0;
                        break;

                    case 5:
                        //SouthWest
                        offset   = new BlockCoordinates(-1, 0, 1);
                        rotation = 180;
                        break;

                    case 6:
                        //SouthEast
                        offset   = new BlockCoordinates(1, 0, 1);
                        rotation = 270;
                        break;

                    case 7:                             //NorthEast
                        offset   = new BlockCoordinates(1, 0, -1);
                        rotation = 0;
                        break;
                    }

                    if (i == 8)
                    {
                        rot = 0;
                    }

                    var distance = Math.Abs(
                        (position + offset).DistanceTo(lowestBlock));
                    if (distance < lowestDistance)
                    {
                        lowestDistance = distance;
                        rot            = rotation;
                    }
                }
            }

            /*if (difference == BlockCoordinates.North)
             * {
             *
             * }
             * else if (difference == BlockCoordinates.South)
             * {
             *      rot = 180;
             * }
             * else if (difference == BlockCoordinates.East)
             * {
             *      rot = 270;
             * }
             * else if (difference == BlockCoordinates.West)
             * {
             *      rot = 90;
             * }*/

            string texture = "";

            if (IsLava)
            {
                texture = "block/lava";
            }
            else
            {
                texture = "block/water";
            }
            //texture = texture + "_flow";
            if (isFlowing || (tl < 8 || tr < 8 || bl < 8 || br < 8))
            {
                texture += "_flow";
            }
            else
            {
                texture += "_still";
            }

            //float frameX
            UVMap map = GetTextureUVMap(Alex.Instance.Resources, texture, 0, 16, 0, 16, 0);

            var originalMap = new UVMap(map.TopLeft, map.TopRight, map.BottomLeft, map.BottomRight, map.ColorLeft, map.ColorTop, map.ColorBottom);

            originalMap.Rotate(180);

            map.Rotate(rot);

            foreach (var f in Enum.GetValues(typeof(BlockFace)).Cast <BlockFace>())
            {
                BlockCoordinates d = BlockCoordinates.Zero;
                d = f.GetBlockCoordinates();

                float height  = 0;
                bool  special = f == BlockFace.Up && (tl < 8 || tr < 8 || bl < 8 || br < 8);

                var modPos         = position + d;
                var b              = (BlockState)world.GetBlockState(modPos.X, modPos.Y, modPos.Z);
                LiquidBlockModel m = b.Model as LiquidBlockModel;
                var secondSpecial  = m != null && (m.Level > Level);

                float s     = 1f - Scale;
                var   start = Vector3.One * s;
                var   end   = Vector3.One * Scale;

                if (special || (secondSpecial) || (!string.IsNullOrWhiteSpace(b.Name) && (!b.Name.Equals(b1) && !b.Name.Equals(b2))))
                {
                    //if (b.BlockModel is LiquidBlockModel m && m.Level > Level && f != BlockFace.Up) continue;

                    var faceMap = map;
                    if (f != BlockFace.Up)
                    {
                        faceMap = originalMap;
                    }

                    var vertices = GetFaceVertices(f, start, end, faceMap, out int[] indexes);

                    var initialIndex = result.Count;
                    for (var index = 0; index < vertices.Length; index++)
                    {
                        var vert = vertices[index];
                        if (vert.Position.Y > start.Y)
                        {
                            const float modifier = 2f;
                            if (vert.Position.X == start.X && vert.Position.Z == start.Z)
                            {
                                height = (modifier * (tl));
                                rot    = 0;
                            }
                            else if (vert.Position.X != start.X && vert.Position.Z == start.Z)
                            {
                                height = (modifier * (tr));
                                rot    = 270;
                            }
                            else if (vert.Position.X == start.X && vert.Position.Z != start.Z)
                            {
                                height = (modifier * (bl));
                                rot    = 90;
                            }
                            else
                            {
                                height = (modifier * (br));
                                rot    = 270;
                            }

                            vert.Position.Y = height / 16.0f;                             //; + (position.Y);
                        }

                        vert.Position.Y += position.Y - s;
                        vert.Position.X += position.X;
                        vert.Position.Z += position.Z;

                        if (IsWater)
                        {
                            vert.Color = new Color(68, 175, 245);
                        }
                        //vert.Color = AdjustColor(new Color(68, 175, 245), f,
                        //	GetLight(world, position + d), false);

                        result.Add(vert);
                    }

                    for (var index = 0; index < indexes.Length; index++)
                    {
                        //var vert = vertices[index];
                        //var vert = vertices[indexes[index]];
                        indexResult.Add(initialIndex + indexes[index]);
                    }
                }
            }

            return(result.ToArray(), indexResult.ToArray());
        }
Пример #4
0
        public override VerticesResult GetVertices(IBlockAccess world, Vector3 vectorPos, Block baseBlock)
        {
            //int myLevel =
            //var chunk = world.GetChunk(vectorPos);
            //Level = GetLevel(baseBlock.BlockState);

            var position = new BlockCoordinates(vectorPos);
            //var                      biome   = world.GetBiome(position);
            List <BlockShaderVertex> result = new List <BlockShaderVertex>(36);
            var indexResult = new List <int>();

            var blocksUp = world.GetBlockStates(position.X, position.Y + 1, position.Z).ToArray();            //.GetType();

            bool aboveIsLiquid =
                (IsWater && blocksUp.Any(
                     x => x.State.Block.Renderable && (x.State.Block.BlockMaterial == Material.Water || x.State.Block.BlockMaterial == Material.WaterPlant))) || (IsLava &&
                                                                                                                                                                  blocksUp.Any(x => x.State.Block.Renderable && x.State.Block.BlockMaterial == Material.Lava));

            List <BlockFace> renderedFaces = new List <BlockFace>();

            //List<BlockFace> liquidFaces = new List<BlockFace>();
            foreach (var face in Faces)
            {
                var pos = position + face.GetBlockCoordinates();

                bool shouldRenderFace = true;
                foreach (var blockState in world.GetBlockStates(pos.X, pos.Y, pos.Z))
                {
                    if (blockState.Storage != 0 && (blockState.State == null || (blockState.State.Block is Air)))
                    {
                        continue;
                    }

                    var neighbor = blockState.State.Block;

                    shouldRenderFace = baseBlock.ShouldRenderFace(face, neighbor);

                    //	if ((neighbor.BlockMaterial == Material.Water || neighbor.BlockMaterial == Material.WaterPlant) && IsWater)
                    //{
                    //		if (face != BlockFace.Up && face != BlockFace.Down)
                    //			liquidFaces.Add(face);
                    //	}
                }

                if (shouldRenderFace)
                {
                    renderedFaces.Add(face);
                }
            }

            if (renderedFaces.Count == 0 && !aboveIsLiquid)
            {
                return(new VerticesResult(new BlockShaderVertex[0], new int[0]));
            }

            int tl, tr, bl, br;

            //	var myLevel = GetLevel(baseBlock.BlockState);

            int lowestFound = 999;
            BlockCoordinates lowestBlock = BlockCoordinates.Up;

            bool isFlowing          = CheckFlowing(world, position);;
            int  rot                = 0;
            bool calculateDirection = false;

            //var avgLiquidLevel = GetAverageLiquidLevels(world, position, out lowestBlock, out lowestFound);

            if (aboveIsLiquid)
            {
                tl = 8;
                tr = 8;
                bl = 8;
                br = 8;

                rot       = 180;
                isFlowing = true;
            }
            else
            {
                if (isFlowing)
                {
                    calculateDirection = true;
                }

                tl = GetAverageLiquidLevels(world, position, out lowestBlock, out lowestFound);;
                //tl = GetAverageLiquidLevels(world, position, out lowestBlock, out lowestFound);

                tr = GetAverageLiquidLevels(world, position + BlockCoordinates.Right, out var trl, out var trv);
                if (trv > lowestFound)
                {
                    lowestBlock = trl;
                    lowestFound = trv;
                }

                bl = GetAverageLiquidLevels(world, position + BlockCoordinates.Forwards, out var bll, out var blv);
                if (blv > lowestFound)
                {
                    lowestBlock = bll;
                    lowestFound = blv;
                }

                br = GetAverageLiquidLevels(world, position + new BlockCoordinates(1, 0, 1), out var brl,
                                            out var brv);

                //if (brv < lowestFound)
                //	lowestBlock = brl;
            }

            double lowestDistance = 9999;

            if (lowestBlock.Y <= position.Y && calculateDirection)
            {
                for (int i = 0; i < 4; i++)
                {
                    var rotation            = 0;
                    BlockCoordinates offset = BlockCoordinates.Zero;
                    switch (i)
                    {
                    case 0:
                        offset   = BlockCoordinates.North;
                        rotation = 180;
                        break;

                    case 1:
                        offset   = BlockCoordinates.South;
                        rotation = 0;
                        break;

                    case 2:
                        offset   = BlockCoordinates.East;
                        rotation = 270;
                        break;

                    case 3:
                        offset   = BlockCoordinates.West;
                        rotation = 90;
                        break;
                    }

                    var distance = Math.Abs(
                        (position + offset).DistanceTo(lowestBlock));
                    if (distance < lowestDistance)
                    {
                        lowestDistance = distance;
                        rot            = rotation;
                    }
                }
            }

            string texture = "";

            if (IsLava)
            {
                texture = "block/lava";
            }
            else
            {
                texture = "block/water";
            }
            //texture = texture + "_flow";
            if (isFlowing)
            {
                texture += "_flow";
            }
            else
            {
                texture += "_still";
            }

            UVMap map = GetTextureUVMap(Alex.Instance.Resources, texture, 0, 16, 0, 16, 0, Color.White);

            var originalMap = new UVMap(map.TopLeft, map.TopRight, map.BottomLeft, map.BottomRight, map.ColorLeft, map.ColorTop, map.ColorBottom);

            map.Rotate(rot);

            foreach (var face in renderedFaces)
            {
                /*if (!renderedFaces.Contains(face))
                 * {
                 *
                 *      continue;
                 * }*/

                float s     = 1f - Scale;
                var   start = Vector3.One * s;
                var   end   = Vector3.One * Scale;

                var faceMap = map;
                if (face != BlockFace.Up)
                {
                    faceMap = originalMap;
                }

                var vertices = GetFaceVertices(face, start, end, faceMap, out int[] indexes);

                float height       = 0;
                var   initialIndex = result.Count;
                for (var index = 0; index < vertices.Length; index++)
                {
                    var vert = vertices[index];
                    if (vert.Position.Y > start.Y)
                    {
                        const float modifier = 2f;
                        if (vert.Position.X == start.X && vert.Position.Z == start.Z)
                        {
                            height = (modifier * (tl));
                            rot    = 0;
                        }
                        else if (vert.Position.X != start.X && vert.Position.Z == start.Z)
                        {
                            height = (modifier * (tr));
                            rot    = 270;
                        }
                        else if (vert.Position.X == start.X && vert.Position.Z != start.Z)
                        {
                            height = (modifier * (bl));
                            rot    = 90;
                        }
                        else
                        {
                            height = (modifier * (br));
                            rot    = 270;
                        }

                        vert.Position.Y = height / 16.0f;                         //; + (position.Y);
                    }

                    vert.Position.Y += position.Y - s;
                    vert.Position.X += position.X;
                    vert.Position.Z += position.Z;

                    if (IsWater)
                    {
                        var bx = position.X;
                        var y  = position.Y;
                        var bz = position.Z;

                        if (ResourcePackBlockModel.SmoothLighting)
                        {
                            vert.Color = CombineColors(
                                GetBiomeColor(world, bx, y, bz), GetBiomeColor(world, bx - 1, y, bz - 1), GetBiomeColor(world, bx - 1, y, bz),
                                GetBiomeColor(world, bx, y, bz - 1), GetBiomeColor(world, bx + 1, y, bz + 1), GetBiomeColor(world, bx + 1, y, bz),
                                GetBiomeColor(world, bx, y, bz + 1), GetBiomeColor(world, bx - 1, y, bz + 1), GetBiomeColor(world, bx + 1, y, bz - 1));
                        }
                        else
                        {
                            vert.Color = GetBiomeColor(world, bx, y, bz);
                        }
                    }
                    else
                    {
                        vert.BlockLight = baseBlock.LightValue;
                    }

                    result.Add(vert);
                }

                for (var index = 0; index < indexes.Length; index++)
                {
                    //var vert = vertices[index];
                    //var vert = vertices[indexes[index]];
                    indexResult.Add(initialIndex + indexes[index]);
                }
            }

            return(new VerticesResult(result.ToArray(), indexResult.ToArray()));
        }