开发者

Tile Worley example

I'm looking for a simple example of the Worley algorithm. From what I understand it is similar to Perlin Noise, however does some calculations on the side to make the "image" look tiley开发者_StackOverflow中文版.

Any info/help would be appreciated.


No so sure about "simple", but there is a comprehensive guide on gamedev.net, including a (python) implementation


I'm not sure if I did it the right way, but I made a java version of the algorithm (or how I think it works). But the results are pretty satisfying in my opinion.

Source code:

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;
    }
}

When starting it, I put those as command line arguments: java Noise 75 400 400 50 false

I get this as a result:

Result

Hope this helped!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜