有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将不同高度的扁平瓷砖缝合在一起

问题

我有一个3D世界,我在其中创建了高度不同的完全平坦的瓷砖。我想做的是把瓦片的顶点缝合在一起,这样它们就在中间的某个地方相遇,并创造出无缝的地形。(例如,从“侧视图”到“侧视图”)

Here's a picture of what the tiles currently look like (the floating tile has an arrow texture on it)

目前,我将地图数据存储在包含x位置、z位置、高度和纹理的文本文件中。我在代码中创建高度贴图,如下所示:

    public static BufferedImage createHeightmapImage(int[][] vertexHeights) {
        BufferedImage img = new BufferedImage(Heightmap.HEIGHTMAP_RESOLUTION, Heightmap.HEIGHTMAP_RESOLUTION, BufferedImage.TYPE_INT_ARGB);
        int a = (255);
        for (int y = 0; y < img.getHeight(); y++) {
            for (int x = 0; x < img.getWidth(); x++) {
                int r = (vertexHeights[x][y]);
                int g = (vertexHeights[x][y]);
                int b = (vertexHeights[x][y]);
                int p = (a << 24) | (r << 16) | (g << 8) | b;
                img.setRGB(x, y, p);
            }
        }
        AffineTransform transform = new AffineTransform();
        transform.rotate(Math.toRadians(180), img.getWidth() / 2, img.getHeight() / 2);
        AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
        return img = op.filter(img, null);
    }

它采用(当前为4)顶点高度,这对于平面平铺来说都是相同的。我将图像旋转180度,使其北边与我指定的北边方向相同

我的地图加载代码如下:

    public GameMap loadMap(String name) {
        ArrayList<MapData> mapData = getSortedMap(name);

        List<Tile> tileList = new ArrayList<Tile>();

        for (int i = 0; i < mapData.size(); i++) {
            int worldX = mapData.get(i).getX();
            int worldZ = mapData.get(i).getZ();
            String texture = mapData.get(i).getTexture();
            int heightmapValue = mapData.get(i).getHeight();
            TileTexture tileTexture = new TileTexture(loadTexture(texture));
            Heightmap heightmap = Tile.generateFlatHeightmap(heightmapValue);
            Tile tile = new Tile(worldX, worldZ, this, tileTexture, heightmap);
            tileList.add(tile);
        }
        GameMap map = new GameMap(name, tileList);
        return map;
    }

    public ArrayList<MapData> getSortedMap(String name) {
        ArrayList<MapData> data = FileUtils.getMapData(FileUtils.loadAsStringList("res/map/" + name + ".map"));
        return data;
    }

getSortedMap函数只需将地图数据加载到ArrayList中,并根据给定的x和z对其进行排序,这样我就可以从左上角到右下角读取和加载地形

我曾尝试创建一个更大的heightmap图像,并从中为每个平铺获取子图像,但最终创建的地形是锯齿状的,在边缘或拐角处没有连接

我还试着在创建世界后改变高度图,以在每个边和角中间达到顶点的高度,但是我找不到一种方法,而不需要创建一个只适用于一个高度图分辨率的代码。我目前使用的分辨率是2x2,不过如果它不适合我的需要,我会考虑增加分辨率


共 (0) 个答案