Tile Worley的例子

时间:2011-09-10 23:11:12

标签: java noise

我正在寻找一个Worley算法的简单示例。根据我的理解,它与Perlin Noise类似,但是在侧面进行一些计算以使“图像”看起来很平滑。

任何信息/帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

不确定“简单”,但gamedev.net上有comprehensive guide,包括(python)实现

答案 1 :(得分:0)

我不确定我的做法是否正确,但我制作了该算法的 Java 版本(或者我认为它是如何工作的)。但在我看来,结果相当令人满意。

源代码:

import java.util.Random;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.*;

public class Noise {

    private static int numOfPoints;
    private static int width;
    private static int height;
    private static int density;
    private static boolean inverted;

    public static void main(String[] args) {

        if (args.length < 5) return;

        numOfPoints = Integer.parseInt(args[0]);
        width = Integer.parseInt(args[1]);
        height = Integer.parseInt(args[2]);
        density = Integer.parseInt(args[3]);
        inverted = Boolean.parseBoolean(args[4]);

        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.createGraphics();
        Point[] p = new Point[numOfPoints];
        Random r = new Random();

        for (int i = 0; i < numOfPoints; i++) {
            p[i] = new Point(r.nextInt(width), r.nextInt(height));
        }

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {

                float minDistance = (float) Math.sqrt(width * width + height * height);

                for (Point point : p) {
                    Point distanceVector = new Point(x - point.x, y - point.y);
                    float dist = (float) Math.sqrt(distanceVector.x * distanceVector.x + distanceVector.y * distanceVector.y);
                    if (dist < minDistance) minDistance = dist;
                }

                int shade = (int) (clamp(minDistance, 0, density) * (255.0f / density));
                if (!inverted) shade = 255 - shade;
                g.setColor(new Color(shade, shade, shade));
                g.fillRect(x, y, 1, 1);
            }
        }
        g.dispose();
        try {
            ImageIO.write(img, "PNG", new File("out.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static float clamp(float var, float min, float max) {
        return var<=min?min:var>=max?max:var;
    }
}

启动时,我将它们作为命令行参数:java Noise 75 400 400 50 false

我得到了这个结果:

Result

希望这有帮助!