-
Perlin Noise
柏林噪声
Many people have used random number
generators in their programs to create
unpredictability,
make
the
motion
and
behavior
of
objects
appear
more
natural,
or
generate
textures. Random
number
generators
certainly
have
their
uses,
but
at
times
their
output
can
be
too
harsh
to
appear
natural.
This
article will
present
a
function
which
has
a
very wide
range
of
uses,
more
than I can think of,
but basically anywhere where you need something to
look natural in origin.
What's more
it's output can easily be tailored to suit your
needs.
很多人在他们的程序中使用随机数生成器去创造不可预测,
使物体的行为和运动表现的更加
自然,
或者生成纹理
。随机数生成器当然是有他们的用途的,
但是它们似乎过于苛刻。
这篇
文章将会展示一个用途十分广泛的功能,
甚至其用途比我
想到的还要广泛,
其结果可以轻易
的适合你的需求。
If you look at many
things in nature, you will notice that they are
fractal. They have various levels
of
detail. A common example is the outline of a
mountain range. It contains large variations in
height
(the
mountains),
medium
variations
(hills),
small
variations
(boulders),
tiny
variations
(stones) . . .
you could go on. Look at almost anything: the
distribution of patchy grass on a field,
waves
in
the
sea,
the
movements
of
an
ant,
the
movement
of
branches
of
a
tree,
patterns
in
marble, winds. All these
phenomena exhibit the same pattern of large and
small variations. The
Perlin Noise
function recreates this by simply adding up noisy
functions at a range of different
scales.
如果你观察自然界中很多事物,
你会注意到它们是分形的。
它们有着很多层次细节。
最平常
的例子是山峰轮廓。它包含着高度上的很大变化(山峰)
,中等变化(丘陵)
,小的变化(砾
石)
,微小变化(石头)
...
你可以继续想象。观察几乎所有事
物:片状分布于田间草,海中
的波浪,蚂蚁的运动方式,树枝的运动,大理石的花纹,风
。所有这些现象表现出了同一种
的大小的变化形式。
柏林噪声函
数通过直接添加一定范围内,
不同比例的噪声函数来重现这
种现
象。
To create a
Perlin noise function, you will need two things, a
Noise Function, and an Interpolation
Function.
为了创建一个柏林噪声函数,我们需要两
个东西,一个噪声函数和一个插值函数。
Introduction To Noise Functions
噪声函数介绍
A
noise
function
is
essentially
a
seeded
random
number
generator.
It
takes
an
integer
as
a
parameter,
and
returns
a
random
number
based
on
that
parameter.
If
you
pass
it
the
same
parameter twice, it produces the same
number twice. It is very important that it behaves
in this
way, otherwise the Perlin
function will simply produce nonsense.
一个噪声函数基本上是一个种子随机发生器。
它需要一个整数作为参数,
然后返回根据这个
参数返回一个随机数。
如果你两次都
传同一个参数进来,
它就会产生两次相同的数。
这条规
律非常重要,否则柏林函数只是生成一堆垃圾。
Here is a graph showing an example
noise function. A random value between 0 and1 is
assigned
to every
point on
the X axis.
这里的一张图展现了噪声函数的一
个例子。
X
轴上每个点被赋予一个
0<
/p>
到
1
之间的随机数。
By smoothly interpolating between the
values, we can define a continuous function that
takes a
non-integer
as
a
parameter.
I
will
discuss
various
ways
of
interpolating
the
values
later
in
this
article.
通过在
值之间平滑的插值,
我们定义了一个带有一个非整参数的连续函数。
我们将会在后面
的内容中讨论多种插值方式
Definitions
定义
Before
I
go
any
further,
let
me
define
what
I
mean
by
amplitude
and
frequency.
If
you
have
studied physics, you
may well have come across the concept of amplitude
and frequency applied
to a sin wave.
p>
当我们准备深入之前,让我定义下什么是振幅(
amplitude
)
和频率(
frequency)
。如
果你学过
物理,你可能遇到过在正玄波中振幅
(amlitud
e)
和频率
(frequency)
的
概念。
Sin Wave
The
wavelength
of
a
sin
wave
is
the
distance
from
one
peak
to
another.
The
amplitude
is
the
height of the wave. The frequency is
defined to be 1/wavelength.
正玄波
正玄波的波长
(wavelength)
是两个波峰只间的距离。振幅是此波的高度。频率我
们定义为
1/
波长
< br>(wavelength)
。
Noise Wave
In
the
graph
of
this example
noise function,
the
red
spots
indicate
the
random values
defined
along the dimension of the function. In
this case, the amplitude is the difference between
the
minimum and maximum values the
function could have. The wavelength is the
distance from one
red spot to the next.
Again frequency is defined to be 1/wavelength.
噪声波
图
中这个噪声波的例子中,
红点表示定义沿着在函数维上的随机值。
在这种情况下,
振幅是
这个函数的最大值与最小值的差值。波
长
(wavelength)
是两个红点之间的距离。同样的频
率
(frequency)
定义为
1/
波长
(wavelength).
Creating the Perlin Noise Function
创建柏林噪声函数
Now, if you take lots of such smooth
functions, with various frequencies and
amplitudes, you can
add them all
together to create a nice noisy function. This is
the Perlin Noise Function.
现在,
如果你使用很多平滑函数,
分别拥有各种各样的频率和振幅,
你可以把他们叠加在一
起来创建一个漂亮的噪声函数。这个就是柏林噪声函数。
Take the following
Noise Functions
使用以下的噪声函数
Add
them together, and this is what you get.
将他们叠加起来,你将会得到
:-)
You can see
that this function has large, medium and small
variations. You may even imagine that
it
looks
a
little
like
a
mountain
range.
In
fact
many
computer
generated
landscapes
are
made
using this method. Of
course they use 2D noise, which I shall get onto
in a moment.
你能发现这个函数拥有大的,<
/p>
中的和小的变化。
你甚至可以它已经有点像山的轮廓了。
事实
上很多电脑生成地形景观也是使用了这种方法,当然那使用的是
p>
2D
的噪声,我们将过一下
来研究这个。<
/p>
You can, of
course, do the same in 2 dimensions.
你当然同样的可以在二维下也这么做。
Some noise functions are
created in 2D
一些
2D
的噪声函数
Adding all these functions together
produces a noisy pattern.
把这些函数叠加起来产生的噪声样式。
Persistence
持续度
When you're adding together
these noise functions, you may wonder exactly what
amplitude and
frequency to use for each
one. The one dimensional example above used twice
the frequency
and
half
the
amplitude
for
each
successive
noise
function
added.
This
is
quite
common.
So
common
in
fact,
that
many
people
don't
even
consider
using
anything
else.
However, you
can
create
Perlin
Noise
functions
with
different
characteristics
by
using
other
frequencies
and
amplitudes at each step. For example,
to create smooth rolling hills, you could use
Perlin noise
function with large
amplitudes for the low frequencies , and very
small amplitudes for the higher
frequencies.
Or
you
could
make
a
flat,
but
very
rocky
plane
choosing
low
amplitudes
for
low
frequencies.
当你把噪声函数叠加的时候,
你可能想了解每次具体使用了什么振幅和频率。
上面一维的例
子对于每个连续叠加的噪声函数使用了两倍的频率和二分之一
倍的振幅。
这个太普通了,
事
实上太普
通,
以至于很多人甚至从来都没有考虑过使用其他什么。
尽管如
此,
你可以通过在
每步使用其他的频率和振幅来创建不同特征的
柏林噪声函数。
例如,
为了创建一个平滑滚动
< br>的丘陵,
你可以使用大的振幅和小的频率的柏林噪声函数,
同时小的振幅和高的频率,
你可
以创建一个平地,另外要创建
非常颠簸的平面,应该选择小的振幅和低的频率。
To make it simpler, and to avoid
repeating the words Amplitude and Frequency all
the time, a
single
number
is
used
to
specify
the
amplitude
of
each
frequency.
This
value
is
known
as
Persistence. There is
some ambiguity as to it's exact meaning. The term
was originally coined by
Mandelbrot,
one of the people behind the discovery of
fractals. He defined noise with a lot of
high frequency as having a low
persistence. My friend Matt also came up with
the concept of
persistence,
but defined it the other way round. To be honest,
I prefer Matt's definition. Sorry
Mandelbrot. So our definition of
persistence is this:
为了让这些更简单易懂,
同时为了避免重复振幅和频率这两个词,
我们用一个数来表示每个
频率下的振幅,这个数就是持续度
(Persistence)
。这里的词和它的真实意义有些歧异。这个
术语原本是
Mandelbrot
提出的,他是发现分形现象的人中的一个。他定义噪声拥有大量
的
高频率将体现出低的持续度。我的朋友
Matt
也想出了持续度的概念,但是是通过另外一种
方式定义它的。诚然,我更喜欢
Matt
的定义方式。对不起了,
Ma
ndelbrot.
所以我们这样
定义持续度
(persistence):
引用
frequency = 2i
amplitude =
persistencei
Where i is the ith noise
function being added. To illustrate the effect of
persistence on the output
of the Perlin
Noise, take a look at the diagrams below. They
show the component noise functions
that
are added, the effect of the persistence value,
and the resultant Perlin noise function.
i
是表示第
i
个被叠加的噪声函数。为了展示柏林函数在输出上持续度的表现效果,请看下
下面的图
表。他们展示了叠加的每个组成部分,持续度的效果和最终的柏林函数。
Octaves
倍频
Each successive noise
function you add is known as an octave. The reason
for this is that each
noise
function
is
twice
the
frequency
of
the
previous
one.
In
music,
octaves
also
have
this
property.
每个你所叠加
的噪声函数就是一个倍频。
因为每一个噪声函数是上一个的两倍频率。
< br>在音乐
上,倍频也有着这项属性。
Exactly how many octaves
you add together is entirely up to you. You may
add as many or as few
as
you
want.
However,
let
me
give
you
some
suggestions.
If
you
are
using
the
perlin
noise
function to render an image to the
screen, there will come a point when an octave has
too high a
frequency to be displayable.
There simply may not be enough pixels on the
screen to reproduce
all the little
details of a very high frequency noise function.
Some implementations of Perlin Noise
automatically add up as many noise
functions they can until the limits of the screen
(or other
medium) are reached.