Skip to content

Commit c2a5dd8

Browse files
author
Chris Ashworth
committed
Added a radial noise module, and added linear interpolation option to select module
1 parent af9097f commit c2a5dd8

File tree

6 files changed

+185
-1
lines changed

6 files changed

+185
-1
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
2+
#include "UnrealFastNoisePlugin.h"
3+
#include "UFNNoiseGenerator.h"
4+
#include "UFNRadialModule.h"
5+
6+
UUFNRadialModule::UUFNRadialModule(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
7+
{
8+
9+
}
10+
11+
float UUFNRadialModule::GetNoise3D(float aX, float aY, float aZ)
12+
{
13+
if (!(inputModule1 && inputModule2)) {
14+
return 0.0f;
15+
}
16+
17+
float dist = (origin - FVector(aX, aY, aZ)).Size();
18+
19+
if (interpType != ESelectInterpType::None)
20+
{
21+
// outside falloff
22+
if (dist > radius + falloff) {
23+
return inputModule2->GetNoise3D(aX, aY, aZ);
24+
}
25+
// inside radius
26+
else if (dist < radius) {
27+
return inputModule1->GetNoise3D(aX, aY, aZ);
28+
}
29+
// otherwise in the falloff range, so smooth
30+
else {
31+
32+
switch (interpType)
33+
{
34+
case ESelectInterpType::CircularIn:
35+
return FMath::InterpCircularIn(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff);
36+
case ESelectInterpType::CircularInOut:
37+
return FMath::InterpCircularInOut(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff);
38+
case ESelectInterpType::CircularOut:
39+
return FMath::InterpCircularOut(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff);
40+
case ESelectInterpType::ExponentialIn:
41+
return FMath::InterpExpoIn(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff);
42+
case ESelectInterpType::ExponentialInOut:
43+
return FMath::InterpExpoInOut(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff);
44+
case ESelectInterpType::ExponentialOut:
45+
return FMath::InterpExpoOut(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff);
46+
case ESelectInterpType::SineIn:
47+
return FMath::InterpSinIn(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff);
48+
case ESelectInterpType::SineInOut:
49+
return FMath::InterpSinInOut(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff);
50+
case ESelectInterpType::SineOut:
51+
return FMath::InterpSinInOut(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff);
52+
case ESelectInterpType::Step:
53+
return FMath::InterpStep(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff, numSteps);
54+
case ESelectInterpType::Linear:
55+
return FMath::Lerp(inputModule1->GetNoise3D(aX, aY, aZ), inputModule2->GetNoise3D(aX, aY, aZ), (dist - radius) / falloff);
56+
}
57+
}
58+
}
59+
60+
// If there's no interpolation, easy mode
61+
if (dist > radius) {
62+
return inputModule1->GetNoise3D(aX, aY, aZ);
63+
}
64+
else {
65+
return inputModule2->GetNoise3D(aX, aY, aZ);
66+
}
67+
68+
69+
}
70+
71+
float UUFNRadialModule::GetNoise2D(float aX, float aY)
72+
{
73+
if (!(inputModule1 && inputModule2)) {
74+
return 0.0f;
75+
}
76+
77+
float dist = (FVector2D(aX, aY) - FVector2D(origin.X, origin.Y)).Size();
78+
79+
if (interpType != ESelectInterpType::None)
80+
{
81+
// outside falloff
82+
if (dist > radius + falloff) {
83+
return inputModule2->GetNoise2D(aX, aY);
84+
}
85+
// inside radius
86+
else if (dist < radius) {
87+
return inputModule1->GetNoise2D(aX, aY);
88+
}
89+
// otherwise in the falloff range, so smooth
90+
else {
91+
92+
switch (interpType)
93+
{
94+
case ESelectInterpType::CircularIn:
95+
return FMath::InterpCircularIn(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff);
96+
case ESelectInterpType::CircularInOut:
97+
return FMath::InterpCircularInOut(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff);
98+
case ESelectInterpType::CircularOut:
99+
return FMath::InterpCircularOut(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff);
100+
case ESelectInterpType::ExponentialIn:
101+
return FMath::InterpExpoIn(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff);
102+
case ESelectInterpType::ExponentialInOut:
103+
return FMath::InterpExpoInOut(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff);
104+
case ESelectInterpType::ExponentialOut:
105+
return FMath::InterpExpoOut(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff);
106+
case ESelectInterpType::SineIn:
107+
return FMath::InterpSinIn(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff);
108+
case ESelectInterpType::SineInOut:
109+
return FMath::InterpSinInOut(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff);
110+
case ESelectInterpType::SineOut:
111+
return FMath::InterpSinInOut(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff);
112+
case ESelectInterpType::Step:
113+
return FMath::InterpStep(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff, numSteps);
114+
case ESelectInterpType::Linear:
115+
return FMath::Lerp(inputModule1->GetNoise2D(aX, aY), inputModule2->GetNoise2D(aX, aY), (dist - radius) / falloff);
116+
}
117+
}
118+
}
119+
120+
// If there's no interpolation, easy mode
121+
if (dist > radius) {
122+
return inputModule1->GetNoise2D(aX, aY);
123+
}
124+
else {
125+
return inputModule2->GetNoise2D(aX, aY);
126+
}
127+
128+
}
129+

‎Source/UnrealFastNoisePlugin/Private/UFNBlueprintFunctionLibrary.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "UFNSplineGenerator.h"
1010
#include "UFNUberNoiseModule.h"
1111
#include "UFNWarpModule.h"
12+
#include "UFNRadialModule.h"
1213
#include "Classes/Components/SplineComponent.h"
1314
#include "UFNBlueprintFunctionLibrary.h"
1415

@@ -230,6 +231,25 @@ UUFNNoiseGenerator* UUFNBlueprintFunctionLibrary::CreateSplineGenerator(UObject*
230231
return newSplineGenerator;
231232
}
232233

234+
UUFNNoiseGenerator* UUFNBlueprintFunctionLibrary::CreateRadialModule(UObject* outer, UUFNNoiseGenerator* inputModule1, UUFNNoiseGenerator* inputModule2, ESelectInterpType interpolationType, FVector origin, float radius, float falloff, int32 numSteps)
235+
{
236+
if (!(inputModule1 && inputModule2 && outer)) {
237+
return nullptr;
238+
}
239+
240+
UUFNRadialModule* newRadialModule = NewObject<UUFNRadialModule>(outer);
241+
242+
newRadialModule->inputModule1 = inputModule1;
243+
newRadialModule->inputModule2 = inputModule2;
244+
newRadialModule->falloff = falloff;
245+
newRadialModule->radius = radius;
246+
newRadialModule->origin = origin;
247+
newRadialModule->interpType = interpolationType;
248+
newRadialModule->numSteps = numSteps;
249+
250+
return newRadialModule;
251+
}
252+
233253
UUFNBlueprintFunctionLibrary::UUFNBlueprintFunctionLibrary(const class FObjectInitializer& obj)
234254
: Super(obj)
235255
{

‎Source/UnrealFastNoisePlugin/Private/UFNSelectModule.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ float UUFNSelectModule::GetNoise3D(float aX, float aY, float aZ)
5353
return FMath::InterpSinInOut(inputModule2->GetNoise3D(aX, aY, aZ), inputModule1->GetNoise3D(aX, aY, aZ), (control - (threshold - falloff) / (2.0f * falloff)));
5454
case ESelectInterpType::Step:
5555
return FMath::InterpStep(inputModule2->GetNoise3D(aX, aY, aZ), inputModule1->GetNoise3D(aX, aY, aZ), (control - (threshold - falloff) / (2.0f * falloff)), numSteps);
56+
case ESelectInterpType::Linear:
57+
return FMath::Lerp(inputModule2->GetNoise3D(aX, aY, aZ), inputModule1->GetNoise3D(aX, aY, aZ), (control - (threshold - falloff) / (2.0f * falloff)));
5658
}
5759
}
5860
}
@@ -111,6 +113,8 @@ float UUFNSelectModule::GetNoise2D(float aX, float aY)
111113
return FMath::InterpSinInOut(inputModule2->GetNoise2D(aX, aY), inputModule1->GetNoise2D(aX, aY), (control - (threshold - falloff) / (2.0f * falloff)));
112114
case ESelectInterpType::Step:
113115
return FMath::InterpStep(inputModule2->GetNoise2D(aX, aY), inputModule1->GetNoise2D(aX, aY), (control - (threshold - falloff) / (2.0f * falloff)), numSteps);
116+
case ESelectInterpType::Linear:
117+
return FMath::Lerp(inputModule2->GetNoise2D(aX, aY), inputModule1->GetNoise2D(aX, aY), (control - (threshold - falloff) / (2.0f * falloff)));
114118
}
115119
}
116120
}

‎Source/UnrealFastNoisePlugin/Public/FastNoise/FastNoise.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ enum class ESelectInterpType : uint8
8989
SineIn,
9090
SineOut,
9191
SineInOut,
92-
Step
92+
Step,
93+
Linear
9394
};
9495

9596

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "UFNNoiseGenerator.h"
4+
#include "UFNRadialModule.generated.h"
5+
6+
UCLASS()
7+
class UNREALFASTNOISEPLUGIN_API UUFNRadialModule : public UUFNNoiseGenerator
8+
{
9+
GENERATED_UCLASS_BODY()
10+
public:
11+
12+
float GetNoise3D(float aX, float aY, float aZ) override;
13+
float GetNoise2D(float aX, float aY) override;
14+
15+
UPROPERTY()
16+
UUFNNoiseGenerator* inputModule1;
17+
18+
UPROPERTY()
19+
UUFNNoiseGenerator* inputModule2;
20+
21+
FVector origin;
22+
float radius;
23+
float falloff;
24+
25+
ESelectInterpType interpType;
26+
int32 numSteps;
27+
};

‎Source/UnrealFastNoisePlugin/Public/UFNBlueprintFunctionLibrary.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@ class UNREALFASTNOISEPLUGIN_API UUFNBlueprintFunctionLibrary : public UBlueprint
4242
// Creates a Select module. Returns a value either from input1 or input 2 or input 3, depending on the value returned from the select module.
4343
UFUNCTION(BlueprintPure, Category = "UnrealFastNoise")
4444
static UUFNNoiseGenerator* Create3SelectModule(UObject* outer, UUFNNoiseGenerator* inputModule1, UUFNNoiseGenerator* inputModule2, UUFNNoiseGenerator* inputModule3, UUFNNoiseGenerator* selectModule, float lowerThreshold = 0.0f, float upperThreshold = 0.0f, ESelectInterpType interpolationType = ESelectInterpType::None, float falloff = 0.0f, int32 steps = 4);
45+
// Creates a Radial module. Returns a value either from input1 or input 2, depending on the distance from the origin. Has smooth falloff option (may be wonky)
46+
UFUNCTION(BlueprintPure, Category = "UnrealFastNoise")
47+
static UUFNNoiseGenerator* CreateRadialModule(UObject* outer, UUFNNoiseGenerator* inputModule1, UUFNNoiseGenerator* inputModule2, ESelectInterpType interpolationType = ESelectInterpType::None, FVector origin = FVector(0.0f), float radius = 500.f, float falloff = 0.0f, int32 steps = 4);
4548

4649
};

0 commit comments

Comments
 (0)