2
* Copyright 2011, Blender Foundation.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
#include "node_texture.h"
24
* H: fractal increment parameter
25
* lacunarity: gap between successive frequencies
26
* octaves: number of frequencies in the fBm
28
* from "Texturing and Modelling: A procedural approach"
31
float noise_musgrave_fBm(point p, string basis, float H, float lacunarity, float octaves)
36
float pwHL = pow(lacunarity, -H);
39
for(i = 0; i < (int)octaves; i++) {
40
value += noise_basis(p, basis) * pwr;
45
rmd = octaves - floor(octaves);
47
value += rmd * noise_basis(p, basis) * pwr;
52
/* Musgrave Multifractal
54
* H: highest fractal dimension
55
* lacunarity: gap between successive frequencies
56
* octaves: number of frequencies in the fBm
59
float noise_musgrave_multi_fractal(point p, string basis, float H, float lacunarity, float octaves)
64
float pwHL = pow(lacunarity, -H);
67
for(i = 0; i < (int)octaves; i++) {
68
value *= (pwr * noise_basis(p, basis) + 1.0);
73
rmd = octaves - floor(octaves);
75
value *= (rmd * pwr * noise_basis(p, basis) + 1.0); /* correct? */
80
/* Musgrave Heterogeneous Terrain
82
* H: fractal dimension of the roughest area
83
* lacunarity: gap between successive frequencies
84
* octaves: number of frequencies in the fBm
85
* offset: raises the terrain from `sea level'
88
float noise_musgrave_hetero_terrain(point p, string basis, float H, float lacunarity, float octaves, float offset)
90
float value, increment, rmd;
91
float pwHL = pow(lacunarity, -H);
95
/* first unscaled octave of function; later octaves are scaled */
96
value = offset + noise_basis(p, basis);
99
for(i = 1; i < (int)octaves; i++) {
100
increment = (noise_basis(p, basis) + offset) * pwr * value;
106
rmd = octaves - floor(octaves);
108
increment = (noise_basis(p, basis) + offset) * pwr * value;
109
value += rmd * increment;
115
/* Hybrid Additive/Multiplicative Multifractal Terrain
117
* H: fractal dimension of the roughest area
118
* lacunarity: gap between successive frequencies
119
* octaves: number of frequencies in the fBm
120
* offset: raises the terrain from `sea level'
123
float noise_musgrave_hybrid_multi_fractal(point p, string basis, float H, float lacunarity, float octaves, float offset, float gain)
125
float result, signal, weight, rmd;
126
float pwHL = pow(lacunarity, -H);
130
result = noise_basis(p, basis) + offset;
131
weight = gain * result;
134
for(i = 1; (weight > 0.001) && (i < (int)octaves); i++) {
138
signal = (noise_basis(p, basis) + offset) * pwr;
140
result += weight * signal;
141
weight *= gain * signal;
145
rmd = octaves - floor(octaves);
147
result += rmd * ((noise_basis(p, basis) + offset) * pwr);
152
/* Ridged Multifractal Terrain
154
* H: fractal dimension of the roughest area
155
* lacunarity: gap between successive frequencies
156
* octaves: number of frequencies in the fBm
157
* offset: raises the terrain from `sea level'
160
float noise_musgrave_ridged_multi_fractal(point p, string basis, float H, float lacunarity, float octaves, float offset, float gain)
162
float result, signal, weight;
163
float pwHL = pow(lacunarity, -H);
167
signal = offset - fabs(noise_basis(p, basis));
172
for(i = 1; i < (int)octaves; i++) {
174
weight = clamp(signal * gain, 0.0, 1.0);
175
signal = offset - fabs(noise_basis(p, basis));
178
result += signal * pwr;
187
shader node_musgrave_texture(
189
string Basis = "Perlin",
190
float Dimension = 2.0,
191
float Lacunarity = 1.0,
194
float Intensity = 1.0,
198
output float Fac = 0.0)
200
float dimension = max(Dimension, 0.0);
201
float octaves = max(Octaves, 0.0);
202
float lacunarity = max(Lacunarity, 1e-5);
203
float size = nonzero(Size, 1e-5);
205
point p = Vector/size;
207
if(Type == "Multifractal")
208
Fac = Intensity*noise_musgrave_multi_fractal(p, Basis, dimension, lacunarity, octaves);
209
else if(Type == "fBM")
210
Fac = Intensity*noise_musgrave_fBm(p, Basis, dimension, lacunarity, octaves);
211
else if(Type == "Hybrid Multifractal")
212
Fac = Intensity*noise_musgrave_hybrid_multi_fractal(p, Basis, dimension, lacunarity, octaves, Offset, Gain);
213
else if(Type == "Ridged Multifractal")
214
Fac = Intensity*noise_musgrave_ridged_multi_fractal(p, Basis, dimension, lacunarity, octaves, Offset, Gain);
215
else if(Type == "Hetero Terrain")
216
Fac = Intensity*noise_musgrave_hetero_terrain(p, Basis, dimension, lacunarity, octaves, Offset);