1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/*
* Copyright 2011, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "stdosl.h"
#include "node_texture.h"
/* Musgrave fBm
*
* H: fractal increment parameter
* lacunarity: gap between successive frequencies
* octaves: number of frequencies in the fBm
*
* from "Texturing and Modelling: A procedural approach"
*/
float noise_musgrave_fBm(point p, string basis, float H, float lacunarity, float octaves)
{
float rmd;
float value = 0.0;
float pwr = 1.0;
float pwHL = pow(lacunarity, -H);
int i;
for(i = 0; i < (int)octaves; i++) {
value += noise_basis(p, basis) * pwr;
pwr *= pwHL;
p *= lacunarity;
}
rmd = octaves - floor(octaves);
if(rmd != 0.0)
value += rmd * noise_basis(p, basis) * pwr;
return value;
}
/* Musgrave Multifractal
*
* H: highest fractal dimension
* lacunarity: gap between successive frequencies
* octaves: number of frequencies in the fBm
*/
float noise_musgrave_multi_fractal(point p, string basis, float H, float lacunarity, float octaves)
{
float rmd;
float value = 1.0;
float pwr = 1.0;
float pwHL = pow(lacunarity, -H);
int i;
for(i = 0; i < (int)octaves; i++) {
value *= (pwr * noise_basis(p, basis) + 1.0);
pwr *= pwHL;
p *= lacunarity;
}
rmd = octaves - floor(octaves);
if(rmd != 0.0)
value *= (rmd * pwr * noise_basis(p, basis) + 1.0); /* correct? */
return value;
}
/* Musgrave Heterogeneous Terrain
*
* H: fractal dimension of the roughest area
* lacunarity: gap between successive frequencies
* octaves: number of frequencies in the fBm
* offset: raises the terrain from `sea level'
*/
float noise_musgrave_hetero_terrain(point p, string basis, float H, float lacunarity, float octaves, float offset)
{
float value, increment, rmd;
float pwHL = pow(lacunarity, -H);
float pwr = pwHL;
int i;
/* first unscaled octave of function; later octaves are scaled */
value = offset + noise_basis(p, basis);
p *= lacunarity;
for(i = 1; i < (int)octaves; i++) {
increment = (noise_basis(p, basis) + offset) * pwr * value;
value += increment;
pwr *= pwHL;
p *= lacunarity;
}
rmd = octaves - floor(octaves);
if(rmd != 0.0) {
increment = (noise_basis(p, basis) + offset) * pwr * value;
value += rmd * increment;
}
return value;
}
/* Hybrid Additive/Multiplicative Multifractal Terrain
*
* H: fractal dimension of the roughest area
* lacunarity: gap between successive frequencies
* octaves: number of frequencies in the fBm
* offset: raises the terrain from `sea level'
*/
float noise_musgrave_hybrid_multi_fractal(point p, string basis, float H, float lacunarity, float octaves, float offset, float gain)
{
float result, signal, weight, rmd;
float pwHL = pow(lacunarity, -H);
float pwr = pwHL;
int i;
result = noise_basis(p, basis) + offset;
weight = gain * result;
p *= lacunarity;
for(i = 1; (weight > 0.001) && (i < (int)octaves); i++) {
if(weight > 1.0)
weight = 1.0;
signal = (noise_basis(p, basis) + offset) * pwr;
pwr *= pwHL;
result += weight * signal;
weight *= gain * signal;
p *= lacunarity;
}
rmd = octaves - floor(octaves);
if(rmd != 0.0)
result += rmd * ((noise_basis(p, basis) + offset) * pwr);
return result;
}
/* Ridged Multifractal Terrain
*
* H: fractal dimension of the roughest area
* lacunarity: gap between successive frequencies
* octaves: number of frequencies in the fBm
* offset: raises the terrain from `sea level'
*/
float noise_musgrave_ridged_multi_fractal(point p, string basis, float H, float lacunarity, float octaves, float offset, float gain)
{
float result, signal, weight;
float pwHL = pow(lacunarity, -H);
float pwr = pwHL;
int i;
signal = offset - fabs(noise_basis(p, basis));
signal *= signal;
result = signal;
weight = 1.0;
for(i = 1; i < (int)octaves; i++) {
p *= lacunarity;
weight = clamp(signal * gain, 0.0, 1.0);
signal = offset - fabs(noise_basis(p, basis));
signal *= signal;
signal *= weight;
result += signal * pwr;
pwr *= pwHL;
}
return result;
}
/* Shader */
shader node_musgrave_texture(
string Type = "fBM",
string Basis = "Perlin",
float Dimension = 2.0,
float Lacunarity = 1.0,
float Octaves = 2.0,
float Offset = 0.0,
float Intensity = 1.0,
float Gain = 1.0,
float Scale = 5.0,
point Vector = P,
output float Fac = 0.0)
{
float dimension = max(Dimension, 0.0);
float octaves = max(Octaves, 0.0);
float lacunarity = max(Lacunarity, 1e-5);
point p = Vector*Scale;
if(Type == "Multifractal")
Fac = Intensity*noise_musgrave_multi_fractal(p, Basis, dimension, lacunarity, octaves);
else if(Type == "fBM")
Fac = Intensity*noise_musgrave_fBm(p, Basis, dimension, lacunarity, octaves);
else if(Type == "Hybrid Multifractal")
Fac = Intensity*noise_musgrave_hybrid_multi_fractal(p, Basis, dimension, lacunarity, octaves, Offset, Gain);
else if(Type == "Ridged Multifractal")
Fac = Intensity*noise_musgrave_ridged_multi_fractal(p, Basis, dimension, lacunarity, octaves, Offset, Gain);
else if(Type == "Hetero Terrain")
Fac = Intensity*noise_musgrave_hetero_terrain(p, Basis, dimension, lacunarity, octaves, Offset);
}
|