-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastNoiseWrapper.h
437 lines (376 loc) · 15.8 KB
/
FastNoiseWrapper.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// FastNoiseWrapper.h
//
// MIT License
//
// Copyright(c) 2019 Víctor Hernández
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// The developer's email is [email protected]
//
// VERSION: 1.0.0
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "FastNoise.h"
#include "FastNoiseWrapper.generated.h"
// Fast Noise UE4 enum wrappers
UENUM(BlueprintType) enum class EFastNoise_NoiseType : uint8 { Value, ValueFractal, Perlin, PerlinFractal, Simplex, SimplexFractal, Cellular, WhiteNoise, Cubic, CubicFractal };
UENUM(BlueprintType) enum class EFastNoise_Interp : uint8 { Linear, Hermite, Quintic };
UENUM(BlueprintType) enum class EFastNoise_FractalType : uint8 { FBM, Billow, RigidMulti };
UENUM(BlueprintType) enum class EFastNoise_CellularDistanceFunction : uint8 { Euclidean, Manhattan, Natural };
UENUM(BlueprintType) enum class EFastNoise_CellularReturnType : uint8 { CellValue, /*NoiseLookup,*/ Distance, Distance2, Distance2Add, Distance2Sub, Distance2Mul, Distance2Div };
/**
* UE4 Wrapper for Auburns's FastNoise library, also available for blueprints usage
*/
UCLASS(BlueprintType)
class PROJECT_API UFastNoiseWrapper : public UObject
{
GENERATED_BODY()
public:
/**
* Set all the properties needed to generate the noise
*
* @param noiseType - noise return type of GetNoise(...). Default value: Simplex
* @param seed - seed used for all noise types. Using different seeds will cause the noise output to change. Default value: 1337
* @param frequency - frequency for all noise types, except White Noise. Affects how coarse the noise output is. Default value: 0.01
* @param interp - interpolation method used to smooth between noise values in Value and Perlin Noise. Possible interpolation methods (lowest to highest quality): Linear; Hermite; Quintic. Default value: Quintic
* @param fractaltype - method for combining octaves in all fractal noise types. Default value: FBM
* @param octaves - octave count for all fractal noise types. The amount of noise layers used to create the fractal. Default value: 3
* @param lacunarity - octave lacunarity for all fractal noise types. The frequency multiplier between each octave. Default value: 2.0
* @param gain - octave gain for all fractal noise types. The relative strength of noise from each layer when compared to the last. Default value: 0.5
* @param cellularJitter - maximum distance a cellular point can move from its grid position. Setting this high will make artifacts more common. Default value: 0.45
* @param cellularDistanceFunction - distance function used in cellular noise calculations. The distance function used to calculate the cell for a given point. Natural is a blend of Euclidean and Manhattan to give curved cell boundaries. Default value: Euclidean
* @param cellularReturnType - return type from cellular noise calculations. Default value: CellValue
*/
UFUNCTION(BlueprintCallable, Category = "Fast Noise")
void SetupFastNoise
(
const EFastNoise_NoiseType noiseType = EFastNoise_NoiseType::Simplex,
const int32 seed = 1337,
const float frequency = 0.01f,
const EFastNoise_Interp interp = EFastNoise_Interp::Quintic,
const EFastNoise_FractalType fractaltype = EFastNoise_FractalType::FBM,
const int32 octaves = 3,
const float lacunarity = 2.0f,
const float gain = 0.5f,
const float cellularJitter = 0.45f,
const EFastNoise_CellularDistanceFunction cellularDistanceFunction = EFastNoise_CellularDistanceFunction::Euclidean,
const EFastNoise_CellularReturnType cellularReturnType = EFastNoise_CellularReturnType::CellValue
)
{
SetNoiseType(noiseType);
SetSeed(seed);
SetFrequency(frequency);
SetInterpolation(interp);
SetFractalType(fractaltype);
SetOctaves(octaves);
SetLacunarity(lacunarity);
SetGain(gain);
SetCellularJitter(cellularJitter);
SetDistanceFunction(cellularDistanceFunction);
SetReturnType(cellularReturnType);
bInitialized = true;
}
/** Returns if Fast Noise properties are initialized or not */
UFUNCTION(BlueprintPure, Category = "Fast Noise")
bool IsInitialized() { return bInitialized; }
/**
* Returns the noise calculation given x and y values
*
* @param x - the x value
* @param y - the y value
*/
UFUNCTION(BlueprintCallable, Category = "Fast Noise")
float GetNoise2D(const float x, const float y) { return IsInitialized() ? fastNoise.GetNoise(x, y) : 0.0f; }
/**
* Returns the noise calculation given x, y and z values
*
* @param x - the x value
* @param y - the y value
* @param z - the z value
*/
UFUNCTION(BlueprintCallable, Category = "Fast Noise")
float GetNoise3D(const float x, const float y, const float z = 0.0f) { return IsInitialized() ? fastNoise.GetNoise(x, y, z) : 0.0f; }
//***********************************************************
//********************* GETTERS *********************
//***********************************************************
/** Gets noise type */
UFUNCTION(BlueprintPure, Category = "Fast Noise|General settings")
EFastNoise_NoiseType GetNoiseType()
{
switch (fastNoise.GetNoiseType())
{
case FastNoise::NoiseType::Value:
return EFastNoise_NoiseType::Value;
case FastNoise::NoiseType::ValueFractal:
return EFastNoise_NoiseType::ValueFractal;
case FastNoise::NoiseType::Perlin:
return EFastNoise_NoiseType::Perlin;
case FastNoise::NoiseType::PerlinFractal:
return EFastNoise_NoiseType::PerlinFractal;
case FastNoise::NoiseType::SimplexFractal:
return EFastNoise_NoiseType::SimplexFractal;
case FastNoise::NoiseType::Cellular:
return EFastNoise_NoiseType::Cellular;
case FastNoise::NoiseType::WhiteNoise:
return EFastNoise_NoiseType::WhiteNoise;
case FastNoise::NoiseType::Cubic:
return EFastNoise_NoiseType::Cubic;
case FastNoise::NoiseType::CubicFractal:
return EFastNoise_NoiseType::CubicFractal;
case FastNoise::NoiseType::Simplex:
default:
return EFastNoise_NoiseType::Simplex;
}
}
/** Gets seed. */
UFUNCTION(BlueprintPure, Category = "Fast Noise|General settings")
int32 GetSeed() { return fastNoise.GetSeed(); }
/** Gets frequency. */
UFUNCTION(BlueprintPure, Category = "Fast Noise|General settings")
float GetFrequency() { return fastNoise.GetFrequency(); }
/** Gets interpolation type. */
UFUNCTION(BlueprintPure, Category = "Fast Noise|General settings")
EFastNoise_Interp GetInterpolation()
{
switch (fastNoise.GetInterp())
{
case FastNoise::Interp::Linear:
return EFastNoise_Interp::Linear;
case FastNoise::Interp::Hermite:
return EFastNoise_Interp::Hermite;
case FastNoise::Interp::Quintic:
default:
return EFastNoise_Interp::Quintic;
}
}
/** Gets fractal type. */
UFUNCTION(BlueprintPure, Category = "Fast Noise|Fractal settings")
EFastNoise_FractalType GetFractalType()
{
switch (fastNoise.GetFractalType())
{
case FastNoise::FractalType::Billow:
return EFastNoise_FractalType::Billow;
case FastNoise::FractalType::RigidMulti:
return EFastNoise_FractalType::RigidMulti;
case FastNoise::FractalType::FBM:
default:
return EFastNoise_FractalType::FBM;
}
}
/** Gets fractal octaves. */
UFUNCTION(BlueprintPure, Category = "Fast Noise|Fractal settings")
int32 GetOctaves() { return fastNoise.GetFractalOctaves(); }
/** Gets fractal lacunarity. */
UFUNCTION(BlueprintPure, Category = "Fast Noise|Fractal settings")
float GetLacunarity() { return fastNoise.GetFractalLacunarity(); }
/** Gets fractal gain. */
UFUNCTION(BlueprintPure, Category = "Fast Noise|Fractal settings")
float GetGain() { return fastNoise.GetFractalGain(); }
/** Gets cellular jitter. */
UFUNCTION(BlueprintPure, Category = "Fast Noise|Cellular settings")
float GetCellularJitter() { return fastNoise.GetCellularJitter(); }
/** Gets cellular distance function. */
UFUNCTION(BlueprintPure, Category = "Fast Noise|Cellular settings")
EFastNoise_CellularDistanceFunction GetDistanceFunction()
{
switch (fastNoise.GetCellularDistanceFunction())
{
case FastNoise::CellularDistanceFunction::Manhattan:
return EFastNoise_CellularDistanceFunction::Manhattan;
case FastNoise::CellularDistanceFunction::Natural:
return EFastNoise_CellularDistanceFunction::Natural;
case FastNoise::CellularDistanceFunction::Euclidean:
default:
return EFastNoise_CellularDistanceFunction::Euclidean;
}
}
/** Gets cellular return type. */
UFUNCTION(BlueprintPure, Category = "Fast Noise|Cellular settings")
EFastNoise_CellularReturnType GetReturnType()
{
switch (fastNoise.GetCellularReturnType())
{
/*case FastNoise::CellularReturnType::NoiseLookup:
return EFastNoise_CellularReturnType::NoiseLookup;*/
case FastNoise::CellularReturnType::Distance:
return EFastNoise_CellularReturnType::Distance;
case FastNoise::CellularReturnType::Distance2:
return EFastNoise_CellularReturnType::Distance2;
case FastNoise::CellularReturnType::Distance2Add:
return EFastNoise_CellularReturnType::Distance2Add;
case FastNoise::CellularReturnType::Distance2Sub:
return EFastNoise_CellularReturnType::Distance2Sub;
case FastNoise::CellularReturnType::Distance2Mul:
return EFastNoise_CellularReturnType::Distance2Mul;
case FastNoise::CellularReturnType::Distance2Div:
return EFastNoise_CellularReturnType::Distance2Div;
case FastNoise::CellularReturnType::CellValue:
default:
return EFastNoise_CellularReturnType::CellValue;
}
}
//***********************************************************
//********************* SETTERS *********************
//***********************************************************
/** Set noise type. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|General settings")
void SetNoiseType(const EFastNoise_NoiseType noiseType)
{
switch (noiseType)
{
case EFastNoise_NoiseType::Value:
fastNoise.SetNoiseType(FastNoise::NoiseType::Value);
break;
case EFastNoise_NoiseType::ValueFractal:
fastNoise.SetNoiseType(FastNoise::NoiseType::ValueFractal);
break;
case EFastNoise_NoiseType::Perlin:
fastNoise.SetNoiseType(FastNoise::NoiseType::Perlin);
break;
case EFastNoise_NoiseType::PerlinFractal:
fastNoise.SetNoiseType(FastNoise::NoiseType::PerlinFractal);
break;
case EFastNoise_NoiseType::SimplexFractal:
fastNoise.SetNoiseType(FastNoise::NoiseType::SimplexFractal);
break;
case EFastNoise_NoiseType::Cellular:
fastNoise.SetNoiseType(FastNoise::NoiseType::Cellular);
break;
case EFastNoise_NoiseType::WhiteNoise:
fastNoise.SetNoiseType(FastNoise::NoiseType::WhiteNoise);
break;
case EFastNoise_NoiseType::Cubic:
fastNoise.SetNoiseType(FastNoise::NoiseType::Cubic);
break;
case EFastNoise_NoiseType::CubicFractal:
fastNoise.SetNoiseType(FastNoise::NoiseType::CubicFractal);
break;
case EFastNoise_NoiseType::Simplex:
default:
fastNoise.SetNoiseType(FastNoise::NoiseType::Simplex);
}
}
/** Set seed. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|General settings")
void SetSeed(const int32 seed) { fastNoise.SetSeed(seed); }
/** Set frequency. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|General settings")
void SetFrequency(const float frequency) { fastNoise.SetFrequency(frequency); }
/** Set interpolation type. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|General settings")
void SetInterpolation(const EFastNoise_Interp interp)
{
switch (interp)
{
case EFastNoise_Interp::Linear:
fastNoise.SetInterp(FastNoise::Interp::Linear);
break;
case EFastNoise_Interp::Hermite:
fastNoise.SetInterp(FastNoise::Interp::Hermite);
break;
case EFastNoise_Interp::Quintic:
default:
fastNoise.SetInterp(FastNoise::Interp::Quintic);
}
}
/** Set fractal type. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|Fractal settings")
void SetFractalType(const EFastNoise_FractalType fractalType)
{
switch (fractalType)
{
case EFastNoise_FractalType::Billow:
fastNoise.SetFractalType(FastNoise::FractalType::Billow);
break;
case EFastNoise_FractalType::RigidMulti:
fastNoise.SetFractalType(FastNoise::FractalType::RigidMulti);
break;
case EFastNoise_FractalType::FBM:
default:
fastNoise.SetFractalType(FastNoise::FractalType::FBM);
}
}
/** Set fractal octaves. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|Fractal settings")
void SetOctaves(const int32 octaves) { fastNoise.SetFractalOctaves(octaves); }
/** Set fractal lacunarity. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|Fractal settings")
void SetLacunarity(const float lacunarity) { fastNoise.SetFractalLacunarity(lacunarity); }
/** Set fractal gain. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|Fractal settings")
void SetGain(const float gain) { fastNoise.SetFractalGain(gain); }
/** Set cellular jitter. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|Cellular settings")
void SetCellularJitter(const float cellularJitter) { fastNoise.SetCellularJitter(cellularJitter); }
/** Set cellular distance function. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|Cellular settings")
void SetDistanceFunction(const EFastNoise_CellularDistanceFunction distanceFunction)
{
switch (distanceFunction)
{
case EFastNoise_CellularDistanceFunction::Manhattan:
fastNoise.SetCellularDistanceFunction(FastNoise::CellularDistanceFunction::Manhattan);
break;
case EFastNoise_CellularDistanceFunction::Natural:
fastNoise.SetCellularDistanceFunction(FastNoise::CellularDistanceFunction::Natural);
break;
case EFastNoise_CellularDistanceFunction::Euclidean:
default:
fastNoise.SetCellularDistanceFunction(FastNoise::CellularDistanceFunction::Euclidean);
}
}
/** Set cellular return type. */
UFUNCTION(BlueprintCallable, Category = "Fast Noise|Cellular settings")
void SetReturnType(const EFastNoise_CellularReturnType cellularReturnType)
{
switch (cellularReturnType)
{
/*case EFastNoise_CellularReturnType::NoiseLookup:
fastNoise.SetCellularReturnType(FastNoise::CellularReturnType::NoiseLookup);
break;*/
case EFastNoise_CellularReturnType::Distance:
fastNoise.SetCellularReturnType(FastNoise::CellularReturnType::Distance);
break;
case EFastNoise_CellularReturnType::Distance2:
fastNoise.SetCellularReturnType(FastNoise::CellularReturnType::Distance2);
break;
case EFastNoise_CellularReturnType::Distance2Add:
fastNoise.SetCellularReturnType(FastNoise::CellularReturnType::Distance2Add);
break;
case EFastNoise_CellularReturnType::Distance2Sub:
fastNoise.SetCellularReturnType(FastNoise::CellularReturnType::Distance2Sub);
break;
case EFastNoise_CellularReturnType::Distance2Mul:
fastNoise.SetCellularReturnType(FastNoise::CellularReturnType::Distance2Mul);
break;
case EFastNoise_CellularReturnType::Distance2Div:
fastNoise.SetCellularReturnType(FastNoise::CellularReturnType::Distance2Div);
break;
case EFastNoise_CellularReturnType::CellValue:
default:
fastNoise.SetCellularReturnType(FastNoise::CellularReturnType::CellValue);
}
}
private:
FastNoise fastNoise;
bool bInitialized = false;
};