Skip to content

Commit 29476c9

Browse files
committed
Terrace node, make smoothness a hybrid input
1 parent 008585b commit 29476c9

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

include/FastNoise/Generators/Modifiers.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,16 @@ namespace FastNoise
282282

283283
void SetSource( SmartNodeArg<> gen ) { this->SetSourceMemberVariable( mSource, gen ); }
284284
void SetStepCount( float stepCount ) { mStepCount = stepCount; mStepCountRecip = 1 / stepCount; }
285-
void SetSmoothness( float smoothness ) { mSmoothness = smoothness; if( mSmoothness != 0.0f ) mSmoothnessRecip = 1 + 1 / smoothness; }
285+
286+
void SetSmoothness( float smoothness ) { mSmoothness = smoothness; if( smoothness != 0.0f ) mSmoothnessRecip = 1 + 1 / smoothness; }
287+
void SetSmoothness( SmartNodeArg<> gen ) { this->SetSourceMemberVariable( mSmoothness, gen ); }
286288

287289
protected:
288290
GeneratorSource mSource;
291+
HybridSource mSmoothness = 0.0f;
292+
float mSmoothnessRecip = 0.0f;
289293
float mStepCount = 1.0f;
290294
float mStepCountRecip = 1.0f;
291-
float mSmoothness = 0.0f;
292-
float mSmoothnessRecip = 0.0f;
293295
};
294296

295297
#ifdef FASTNOISE_METADATA
@@ -303,7 +305,7 @@ namespace FastNoise
303305
groups.push_back( "Modifiers" );
304306
this->AddGeneratorSource( "Source", &Terrace::SetSource );
305307
this->AddVariable( { "Step Count", "Increasing the step count reduces the size of each step" }, 1.0f, &Terrace::SetStepCount );
306-
this->AddVariable( { "Smoothness", "How smooth the transitions between steps are" }, 0.0f, &Terrace::SetSmoothness );
308+
this->AddHybridSource( { "Smoothness", "How smooth the transitions between steps are" }, 0.0f, &Terrace::SetSmoothness, &Terrace::SetSmoothness );
307309

308310
description =
309311
"Cuts the input value into steps to give a terraced terrain effect";

include/FastNoise/Generators/Modifiers.inl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,31 @@ class FastSIMD::DispatchClass<FastNoise::Terrace, SIMD> final : public virtual F
131131
source *= float32v( mStepCount );
132132
float32v rounded = FS::Round( source );
133133

134-
if( mSmoothness != 0.0f )
134+
if( mSmoothness.simdGeneratorPtr )
135135
{
136+
// Dynamic smoothness from generator
137+
float32v smoothness = this->GetSourceValue( mSmoothness, seed, pos... );
138+
mask32v smoothnessMask = smoothness != float32v( 0.0f );
139+
140+
if( FS::AnyMask( smoothnessMask ) )
141+
{
142+
float32v diff = rounded - source;
143+
mask32v diffSign = diff < float32v( 0 );
144+
145+
diff = FS::Abs( diff );
146+
diff = float32v( 0.5f ) - diff;
147+
148+
float32v smoothnessRecip = float32v( 1.0f ) + FS::Reciprocal( smoothness );
149+
diff *= smoothnessRecip;
150+
diff = FS::Min( diff, float32v( 0.5f ) );
151+
diff = FS::Select( diffSign, float32v( 0.5f ) - diff, diff - float32v( 0.5f ) );
152+
153+
rounded = FS::Select( smoothnessMask, rounded + diff, rounded );
154+
}
155+
}
156+
else if( mSmoothness.constant != 0.0f )
157+
{
158+
// Constant smoothness, use precomputed reciprocal
136159
float32v diff = rounded - source;
137160
mask32v diffSign = diff < float32v( 0 );
138161

0 commit comments

Comments
 (0)