用于随机地形生成的 Java 2D Perlin 噪声

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25816351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:44:25  来源:igfitidea点击:

Java 2D Perlin Noise for Random Terrain Generation

java

提问by Kultid_Games

My name is Chris and I'm working on my first Java game. Thus far, I have created a tile based 2D game, however my level is done in such a way so that if I create an image and its all green, then that green would stand for a grass tile. If I put a pixel of blue, the game would assign that as a water tile.

我的名字是 Chris,我正在开发我的第一个 Java 游戏。到目前为止,我已经创建了一个基于图块的 2D 游戏,但是我的关卡是以这样的方式完成的,如果我创建一个图像并且它全是绿色,那么该绿色将代表草图块。如果我放置一个蓝色像素,游戏会将其指定为水图块。

However, that limits the game to how I design the level, I'd much rather have an infinite terrain of tiles.

但是,这将游戏限制在我设计关卡的方式上,我宁愿拥有无限的瓷砖地形。

Being a beginner, I looked up different ways to do so. A particularly poignant method was something called a Perlin Noise.

作为初学者,我寻找了不同的方法来做到这一点。一种特别尖锐的方法是一种叫做柏林噪声的东西。

I looked into it but it seemed very complex.

我研究了一下,但它看起来很复杂。

Would somebody mind defining it in simpler terms?

有人会介意用更简单的术语定义它吗?

Also, if you have any tutorials that 'dumb' it down a bit and give a brief overview, that'd be fantastic!

此外,如果您有任何教程可以稍微“简化”它并提供简要概述,那就太棒了!

Sorry I haven't been too specific, I'm actually avoiding from doing so.

抱歉,我没有说得太具体,我实际上是在避免这样做。

回答by karlphillip

Perlin noise was brilliantly covered by Daniel Shiffmanon The Nature Of Code. It's an online book that has awesome Javascript/ProcessingJS sample code to demonstrate some of the important concepts:

Daniel ShiffmanThe Nature Of Code 中出色地涵盖了 Perlin 噪音。这是一本在线书籍,其中包含很棒的 Javascript/ProcessingJS 示例代码来演示一些重要概念:

A good random number generator produces numbers that have no relationship and show no discernible pattern. As we are beginning to see, a little bit of randomness can be a good thing when programming organic, lifelike behaviors. However, randomness as the single guiding principle is not necessarily natural. An algorithm known as “Perlin noise”, named for its inventor Ken Perlin, takes this concept into account. Perlin developed the noise function while working on the original Tron moviein the early 1980s; it was designed to create procedural textures for computer-generated effects. In 1997 Perlin won an Academy Award in technical achievement for this work. Perlin noise can be used to generate various effects with natural qualities, such as clouds, landscapes, and patterned textures like marble.

Perlin noise has a more organic appearancebecause it produces a naturally ordered (“smooth”) sequence of pseudo-random numbers. The graph on the left below shows Perlin noise over time, with the x-axis representing time; note the smoothness of the curve. The graph on the right shows pure random numbers over time.

一个好的随机数生成器产生的数字没有任何关系,也没有明显的模式。正如我们开始看到的那样,在编写有机的、逼真的行为时,一点点随机性可能是一件好事。然而,作为单一指导原则的随机性并不一定是自然的。一种被称为“Perlin 噪声”的算法,以其发明者Ken Perlin 的名字命名,将这一概念考虑在内。Perlin 在制作原始Tron 电影时开发了噪声功能1980 年代初;它旨在为计算机生成的效果创建程序纹理。1997 年,Perlin 因这项工作获得了奥斯卡技术成就奖。Perlin 噪波可用于生成具有自然品质的各种效果,例如云、风景和大理石等图案纹理。

Perlin 噪声具有更有机的外观,因为它产生自然有序(“平滑”)的伪随机数序列。下面左图显示了 Perlin 噪声随时间的变化,x 轴代表时间;注意曲线的平滑度。右图显示了随时间变化的纯随机数。

enter image description here(The code for generating these graphs is available in the accompanying book downloads.)

在此处输入图片说明(生成这些图形的代码可在随附的书籍下载中找到。)

Khan Academydedicated the entire advanced Javascriptlessons to dissect some of the stuff shown by Shiffman on his book. They have great lessons on randomness, and of course, one just for the Perlin noise.

可汗学院专门用了整个高级 Javascript课程来剖析 Shiffman 在他的书中展示的一些内容。他们在随机性方面有很好的经验教训,当然,还有一个专门针对柏林噪音的课程。

回答by KdotJPG

I'd suggest skipping Perlin Noiseand taking a look at something called OpenSimplex Noise.

我建议跳过 Perlin Noise并查看一些名为OpenSimplex Noise 的东西。

It's useful for basically all of the same things as Perlin Noise, but it has significantly fewer visible directional artifacts. Basically, the noise takes an input coordinate (in 2D, 3D, or 4D) and returns a value between -1 and 1. The output values vary continuously with the input coordinate changes.

它对基本上所有与柏林噪声相同的东西都很有用,但它的可见方向性伪影要少得多。基本上,噪声采用输入坐标(2D、3D 或 4D)并返回一个介于 -1 和 1 之间的值。输出值随着输入坐标的变化而不断变化。

Here are three 256x256 images generated using noise (x / 24.0, y / 24.0):

以下是使用噪声 (x / 24.0, y / 24.0) 生成的三张 256x256 图像:

  • The first one is the raw noise
  • The second one is green where the values are greater than zero, and blue otherwise
  • The third one is blue where the values are greater than -0.2 and less than 0.2, and green otherwise.
  • 第一个是原始噪音
  • 第二个值大于零时为绿色,否则为蓝色
  • 第三个是蓝色,其中值大于 -0.2 且小于 0.2,否则为绿色。

Note that there's also Simplex Noise (different algorithm from OpenSimplex) that has reduced directional artifacts compared to Perlin Noise, but the 3D and higher implementations of Simplex Noise (if you happen to want to use 3D noise to vary anything in 2D over time) are saddled with a patent.

请注意,与 Perlin Noise 相比,还有 Simplex Noise(与 OpenSimplex 不同的算法)减少了方向性伪影,但 Simplex Noise 的 3D 和更高版本的实现(如果您碰巧想使用 3D 噪声随时间改变 2D 中的任何内容)是背负着专利。

OpenSimplex Noise is actually an algorithm I've developed for a game of my own, so shameless plug I know, but I think it's the best for you out of what's available.

OpenSimplex Noise 实际上是我为自己的游戏开发的算法,我知道这种无耻的插件,但我认为它是最适合您的。

回答by KdotJPG

You are not obliged to get the full understanding of the perlin or simplex implementation immediately. You can slowly learn while playing with parameters of the various methods you will find. Just use it by feeding x,y, possibly z or more dimension arguments with the coordinates of a grid for example. To keep it simple, you basically mix/superimpose several layers(octaves) of interpolated random images at different scales .

您没有义务立即完全了解 perlin 或 simplex 实现。你可以一边玩一边慢慢学习,你会发现各种方法的参数。例如,只需通过使用网格坐标提供 x、y、可能 z 或更多维度参数来使用它。为简单起见,您基本上可以混合/叠加不同比例的内插随机图像的多层(八度音程)。

You may also want to evaluate and store your noise offline because of the processing charge it may imply if used at run-time (although depending on the resolution / octaves and your processing budget or testing purposes, you can achieve quite decent real time frame rates too).

您可能还想离线评估和存储您的噪音,因为如果在运行时使用它可能意味着处理费用(尽管取决于分辨率/倍频程和您的处理预算或测试目的,您可以获得相当不错的实时帧速率也)。