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
|
using System.Diagnostics;
namespace LibNoise.Operator
{
/// <summary>
/// Provides a noise module that outputs the value selected from one of two source
/// modules chosen by the output value from a control module. [OPERATOR]
/// </summary>
public class Select : ModuleBase
{
#region Fields
private double _fallOff;
private double _raw;
private double _min = -1.0;
private double _max = 1.0;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of Select.
/// </summary>
public Select()
: base(3)
{
}
/// <summary>
/// Initializes a new instance of Select.
/// </summary>
/// <param name="inputA">The first input module.</param>
/// <param name="inputB">The second input module.</param>
/// <param name="controller">The controller module.</param>
public Select(ModuleBase inputA, ModuleBase inputB, ModuleBase controller)
: base(3)
{
Modules[0] = inputA;
Modules[1] = inputB;
Modules[2] = controller;
}
/// <summary>
/// Initializes a new instance of Select.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="fallOff">The falloff value at the edge transition.</param>
/// <param name="inputA">The first input module.</param>
/// <param name="inputB">The second input module.</param>
public Select(double min, double max, double fallOff, ModuleBase inputA, ModuleBase inputB)
: this(inputA, inputB, null)
{
_min = min;
_max = max;
FallOff = fallOff;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the controlling module.
/// </summary>
public ModuleBase Controller
{
get { return Modules[2]; }
set
{
Debug.Assert(value != null);
Modules[2] = value;
}
}
/// <summary>
/// Gets or sets the falloff value at the edge transition.
/// </summary>
/// <remarks>
/// Called SetEdgeFallOff() on the original LibNoise.
/// </remarks>
public double FallOff
{
get { return _fallOff; }
set
{
var bs = _max - _min;
_raw = value;
_fallOff = (value > bs / 2) ? bs / 2 : value;
}
}
/// <summary>
/// Gets or sets the maximum, and re-calculated the fall-off accordingly.
/// </summary>
public double Maximum
{
get { return _max; }
set
{
_max = value;
FallOff = _raw;
}
}
/// <summary>
/// Gets or sets the minimum, and re-calculated the fall-off accordingly.
/// </summary>
public double Minimum
{
get { return _min; }
set
{
_min = value;
FallOff = _raw;
}
}
#endregion
#region Methods
/// <summary>
/// Sets the bounds.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
public void SetBounds(double min, double max)
{
Debug.Assert(min < max);
_min = min;
_max = max;
FallOff = _fallOff;
}
#endregion
#region ModuleBase Members
/// <summary>
/// Returns the output value for the given input coordinates.
/// </summary>
/// <param name="x">The input coordinate on the x-axis.</param>
/// <param name="y">The input coordinate on the y-axis.</param>
/// <param name="z">The input coordinate on the z-axis.</param>
/// <returns>The resulting output value.</returns>
public override double GetValue(double x, double y, double z)
{
Debug.Assert(Modules[0] != null);
Debug.Assert(Modules[1] != null);
Debug.Assert(Modules[2] != null);
var cv = Modules[2].GetValue(x, y, z);
if (_fallOff > 0.0)
{
double a;
if (cv < (_min - _fallOff))
{
return Modules[0].GetValue(x, y, z);
}
if (cv < (_min + _fallOff))
{
var lc = (_min - _fallOff);
var uc = (_min + _fallOff);
a = Utils.MapCubicSCurve((cv - lc) / (uc - lc));
return Utils.InterpolateLinear(Modules[0].GetValue(x, y, z),
Modules[1].GetValue(x, y, z), a);
}
if (cv < (_max - _fallOff))
{
return Modules[1].GetValue(x, y, z);
}
if (cv < (_max + _fallOff))
{
var lc = (_max - _fallOff);
var uc = (_max + _fallOff);
a = Utils.MapCubicSCurve((cv - lc) / (uc - lc));
return Utils.InterpolateLinear(Modules[1].GetValue(x, y, z),
Modules[0].GetValue(x, y, z), a);
}
return Modules[0].GetValue(x, y, z);
}
if (cv < _min || cv > _max)
{
return Modules[0].GetValue(x, y, z);
}
return Modules[1].GetValue(x, y, z);
}
#endregion
}
}
|