0

我正在使用 Kurt Spencer 在此处找到的 OpenSimplexNoise 实现。

我试图尽可能快地在 512x52 JavaFX Canvas 上绘制产生的噪声。

注意:为简单起见,以下代码中未显示,绘图函数采用缩放级别(JavaFX Slider 元素的值)。从该滑块上的更改侦听器调用绘图函数。

我试过的

GraphicsContext setFill() 和 fillRect()

使用噪声的值设置填充,然后在适当的位置为 1x1 矩形调用 fillRect():

public void drawWithRect() {
    // width and height of the canvas
    int width = (int)getCanvas().getWidth();
    int height = (int)getCanvas().getHeight();

    // Get the graphics context of the canvas
    GraphicsContext gc = getCanvas().getGraphicsContext2D();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            double val = (noise((double)x/40, (double)y/40));
            gc.setFill(Color.color(val,val,val));
            gc.fillRect(x,y,1,1);
        }
    }
}

结果:尽管显然大约需要 40 毫秒,但这严重滞后于我的计算机,一次需要 5 多秒才能显示结果。这很糟糕。我不确定这里的幕后发生了什么导致程序难以渲染所有内容......

使用 PixelWriter

我的下一个改进来自使用 PixelWriter:

public void drawWithPixelWriter() {
    // width and height of the canvas
    int width = (int)getCanvas().getWidth();
    int height = (int)getCanvas().getHeight();

    // Get the graphics context of the canvas
    GraphicsContext gc = getCanvas().getGraphicsContext2D();

    // Create the PixelWriter
    PixelWriter pixelWriter = gc.getPixelWriter();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            double val = (noise((double)x/40, (double)y/40));
            pixelWriter.setColor(x,y, Color.color(val,val,val));
        }
    }
}

结果:平均 25 毫秒。这要好得多。没有真正的延迟,应用程序感觉流畅且响应迅速。

使用 PixelFormat 写入

我们使用PixelFormat.getByteRgbInstance();并将噪声值写入一个大字节数组,然后将其与 PixelFormat 一起传递给 pixelWriter。

public void drawWithPixelFormat() {
    // width and height of the canvas
    int width = (int)getCanvas().getWidth();
    int height = (int)getCanvas().getHeight();

    // array to hold rgb value for every pixel
    byte[] pixels = new byte[height * width * 3];

    // Get the graphics context of the canvas
    GraphicsContext gc = getCanvas().getGraphicsContext2D();

    // Create the PixelWriter
    PixelWriter pixelWriter = gc.getPixelWriter();

    // Define the PixelFormat
    PixelFormat<ByteBuffer> pixelFormat = PixelFormat.getByteRgbInstance();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // Get the index
            int i = y * width * 3 + x * 3;
            //Get the noise value
            byte val = (byte)(noise((double)x/40, (double)y/40)*255);
            // set the rgb colors of the pixel;
            pixels[i] = val;
            pixels[i + 1] = val;
            pixels[i + 2] = val;
        }
    }
    // draw the noise
    pixelWriter.setPixels(0, 0, width, height, pixelFormat, pixels, 0, width * 3);
}

结果:每次抽签平均 16 毫秒。这要好得多,对于 512x512,它非常平滑。在这里查看 gif

但是如果我想生成更大的地图呢?对于 1024x1024 的画布,绘制时间约为 65 毫秒,并且明显不稳定。如果我想添加多个八度音阶或根据某些条件更改颜色怎么办?所有这些都会增加抽签时间,所以我必须尽可能地减少它。

有什么改进建议吗?

4

0 回答 0