跳到主要内容

noise

简介

噪声生成

c++ libnoise 介绍

源项目是一个可移植的开源相干噪声生成库:

该 C++ 库用于生成相干噪声:一种平滑变化的噪声,包括柏林噪声、脊状多重分形噪声等

相干噪声常被用于生成看起来自然的纹理、行星地形和其他类似的图形(如上述网站主页的山景图即为libnoise生成的地形文件渲染的)。

该 pkg 在 cmm_noise_export 中导出了库中大部分常用的接口并封装为 gs 接口

组件接口

noise.gs

函数原型函数作用
object create_noise(enum NoiseType type)生成噪声
string type_to_name(enum NoiseType type)根据type获取噪声类型名称

noise_instance.gs

相干噪声实例的接口

函数原型函数作用
bool try_close_manually()是否能够手动关闭,即判断该实例是否被引用
bool is_noise_instance()该object是否为噪声实例
void set_refered_as_source(bool flag)设置该实例是否为别的噪声实例的引用
void set_source_module(int idx, object n)设置源模块,将源模块连接到此噪声模块
float get_value(float x, float y, float z)根据指定输入的坐标值,生成输出值
int get_seed()获取种子值
void set_seed(int seed)设置噪声生成种子
float get_displacement()获取位移
float get_frequency()获取频率
float get_lacunarity()获取噪声的空隙度
int get_noise_quality()获取噪声的 quality
int get_octave_count()获取噪声的八度音程数/粗糙度
float get_persistence()获取Perlin噪声的持久性
float get_power()获取Turbulence噪声的强度
int get_source_module_count()获取所需源噪声模块的数量
void set_displacement(float val)设置位移
void set_frequency(float val)设置频率
void set_lacunarity(float val)设置噪声的空隙度
void set_noise_quality(int val)设置噪声的qulity
void set_octave_count(int val)设置噪声的八度音程数/粗糙度
void set_persistence(float val)设置Perlin噪声的持久性
void set_power(float val)设置Turbulence噪声的强度
map detail()获取噪声的详细信息
void set_param(string name, mixed val)设置参数

枚举

NoiseType

UNKNOWN,
PERLINE,
TURBULENCE,
VORONOI,

样例

perline.gs
// Perline 
void create_perline()
{
printf(HIG "EXAMPLE: Create %s noise" NOR "\n", noise.type_to_name(NoiseType.PERLINE));
auto n = noise.create_noise(NoiseType.PERLINE);
n.set_seed(1980);
n.set_param("frequency", 998);
n.set_param("lacunarity", 234.567);
n.set_param("noise_quality", 2);
n.set_param("octave_count", 10);
n.set_param("persistence", 5588.4433);

for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
for (int z = 0; z < 3; z++)
{
string key = sprintf("x=%d:y=%d:z=%d", x, y, z);
printf(" %O : %d,\n", key, (int)n.get_value(x, y, z));
///---test_equal((int)n.get_value(x, y, z), perline_vals[key]);
}
}
turbulence.gs
// Turbulence 
void create_turbulence()
{
printf(HIG "EXAMPLE: Create %s noise" NOR "\n", noise.type_to_name(NoiseType.TURBULENCE));
auto s0 = noise.create_noise(NoiseType.PERLINE);
s0.set_seed(1980);
s0.set_param("frequency", 990);
s0.set_param("lacunarity", 234.567);
s0.set_param("noise_quality", 2);
s0.set_param("octave_count", 10);
s0.set_param("persistence", 5588.4433);

auto n = noise.create_noise(NoiseType.TURBULENCE);
n.set_seed(1977);
n.set_param("frequency", 995);
n.set_param("octave_count", 8);
n.set_param("power", 134.9988);
n.set_source_module(0, s0);

for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
for (int z = 0; z < 3; z++)
{
string key = sprintf("x=%d:y=%d:z=%d", x, y, z);
printf(" %O : %d,\n", key, (int)n.get_value(x, y, z));
///---test_equal((int)n.get_value(x, y, z), turbulence_vals[key]);
}
}
voronoi.gs
// Voronoi 
void create_voronoi()
{
printf(HIG "EXAMPLE: Create %s noise" NOR "\n", noise.type_to_name(NoiseType.VORONOI));
auto n = noise.create_noise(NoiseType.VORONOI);
n.set_seed(1975);
n.set_param("frequency", 991);
n.set_param("displacement", 555.666);

for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
for (int z = 0; z < 3; z++)
{
string key = sprintf("x=%d:y=%d:z=%d", x, y, z);
printf(" %O : %d,\n", key, (int)n.get_value(x, y, z));
///---test_equal((int)n.get_value(x, y, z), voronoi_vals[key]);
}
}