Unity记录5.7-地图-不同地形的过渡

文章首发见博客:https://mwhls.top/4856.html
无图/格式错误/后续更新请见首发页。
更多更新请到mwhls.top查看
欢迎留言提问或批评建议,私信不回。

汇总:Unity 记录

摘要:不同地形的过渡。

过渡地形-2023/9/15
  • 在之前很长一段时间中,我对过渡地形的设想是引入新地形,但是过于麻烦。

    • 当有n种地形时,就有n*(n-1)种过渡。
    • 尤其当上下左右都属于不同地形时,还需要使用非常多的处理。
  • 昨天突然想到,我对于过渡地形的需求,是因为纯方块的连接过于笔直,很丑。

    • 因此只用让连接处平滑起来就行了。
  • 因此在实现上,找到上下左右的表面tile,在已经生成好的地形块基础上,将上下左右的连接处,再进行一次地面生成。

    • 最终结果如下图,效果还可以,写起来也简单。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

实现代码-2023/9/15
  • 核心部分。
    public TilemapBlock _generate_2Dblock_transition(TilemapBlock block, TilemapAroundBlock around_blocks){
    
    
        // ---------- init ----------
        Vector3Int BSize = _game_configs.__block_size__;    // for convenience
        int perlin;
        _HeightGenerator height_limiter = new(block, new(BSize.x, BSize.y / 5));
        height_limiter.Add(new(0, BSize.y / 20));
        height_limiter.Add(new(BSize.x-1, BSize.y / 20));
        // around surface
        string self_surface = _terrain.ID2TerrainInfo[block.terrain_ID].tile_surface;
        string up_surface = around_blocks.up_terrainInfo.tile_surface;
        string down_surface = around_blocks.down_terrainInfo.tile_surface;
        string left_surface = around_blocks.left_terrainInfo.tile_surface;
        string right_surface = around_blocks.right_terrainInfo.tile_surface;
    
        for (int x = 0; x < BSize.x; x++){
    
    
            perlin = height_limiter.get_height(x);
            for (int y = 0; y < perlin; y++){
    
    
                if (up_surface != null && up_surface != self_surface) // up is different, need transition
                    if (block.map[x][BSize.y - y - 1] != "0") block.map[x][BSize.y - y - 1] = up_surface;
                if (down_surface != null && down_surface != self_surface) // down is different, need transition
                    if (block.map[x][y] != "0") block.map[x][y] = down_surface;
                if (left_surface != null && left_surface != self_surface) // left is different, need transition
                    if (block.map[y][x] != "0") block.map[y][x] = left_surface;
                if (right_surface != null && right_surface != self_surface) // right is different, need transition
                    if (block.map[BSize.y - y - 1][x] != "0") block.map[BSize.y - y - 1][x] = right_surface;
    
            }
        }
        return block;
    }

猜你喜欢

转载自blog.csdn.net/asd123pwj/article/details/133678332