瓦利沃利示例

发布于 2024-12-04 11:23:53 字数 89 浏览 0 评论 0原文

我正在寻找沃利算法的简单示例。据我了解,它与 Perlin Noise 类似,但是会进行一些计算以使“图像”看起来平铺。

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

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.

Any info/help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-11 11:23:53

不太确定“简单”,但有一个 全面gamedev.net 上的指南,包括(python)实现

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

2024-12-11 11:23:53

我不确定我是否以正确的方式做到了这一点,但我制作了该算法的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

我得到的结果是:

结果

希望这有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文