Skip to content

Commit

Permalink
Add native output bounding for various generator noise types
Browse files Browse the repository at this point in the history
  • Loading branch information
Auburn committed Apr 21, 2024
1 parent 28f27cf commit d2aecea
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 147 deletions.
61 changes: 40 additions & 21 deletions include/FastNoise/Generators/BasicGenerators.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ namespace FastNoise
};
#endif

template<typename PARENT>
class VariableRange : public virtual PARENT
{
public:
void SetOutputMin( float value )
{
mRangeScale += mRangeMin - value;
mRangeMin = value;
}

void SetOutputMax( float value )
{
mRangeScale = ( value - mRangeMin );
}

protected:
float mRangeMin = -1;
float mRangeScale = 2;
};

#ifdef FASTNOISE_METADATA
template<typename PARENT>
struct MetadataT<VariableRange<PARENT>> : MetadataT<PARENT>
{
MetadataT()
{
this->AddVariable( { "Output Min", "Minimum bound of output range" }, -1.0f, &VariableRange<PARENT>::SetOutputMin );
this->AddVariable( { "Output Max", "Maximum bound of output range" }, 1.0f, &VariableRange<PARENT>::SetOutputMax );
}
};
#endif

class Constant : public virtual Generator
{
public:
Expand All @@ -53,15 +85,15 @@ namespace FastNoise
};
#endif

class White : public virtual Generator
class White : public virtual VariableRange<Generator>
{
public:
const Metadata& GetMetadata() const override;
};

#ifdef FASTNOISE_METADATA
template<>
struct MetadataT<White> : MetadataT<Generator>
struct MetadataT<White> : MetadataT<VariableRange<Generator>>
{
SmartNode<> CreateNode( FastSIMD::FeatureSet ) const override;

Expand All @@ -70,54 +102,41 @@ namespace FastNoise
groups.push_back( "Basic Generators" );

description =
"White noise generator\n"
"Outputs between -1.0 and 1.0";
"White noise generator";
}
};
#endif

class Checkerboard : public virtual ScalableGenerator
class Checkerboard : public virtual VariableRange<ScalableGenerator>
{
public:
const Metadata& GetMetadata() const override;

void SetHigh( SmartNodeArg<> gen ) { this->SetSourceMemberVariable( mHigh, gen ); }
void SetHigh( float value ) { mHigh = value; }
void SetLow( SmartNodeArg<> gen ) { this->SetSourceMemberVariable( mLow, gen ); }
void SetLow( float value ) { mLow = value; }

protected:
HybridSource mHigh = 1.0f;
HybridSource mLow = -1.0f;
};

#ifdef FASTNOISE_METADATA
template<>
struct MetadataT<Checkerboard> : MetadataT<ScalableGenerator>
struct MetadataT<Checkerboard> : MetadataT<VariableRange<ScalableGenerator>>
{
SmartNode<> CreateNode( FastSIMD::FeatureSet ) const override;

MetadataT()
{
groups.push_back( "Basic Generators" );
this->AddHybridSource( { "High", "Output for \"White\"" }, 1.0f, &Checkerboard::SetHigh, &Checkerboard::SetHigh );
this->AddHybridSource( { "Low", "Output for \"Black\"" }, -1.0f, &Checkerboard::SetLow, &Checkerboard::SetLow );

description =
"Outputs checkerboard pattern";
}
};
#endif

class SineWave : public virtual ScalableGenerator
class SineWave : public virtual VariableRange<ScalableGenerator>
{
public:
const Metadata& GetMetadata() const override;
};

#ifdef FASTNOISE_METADATA
template<>
struct MetadataT<SineWave> : MetadataT<ScalableGenerator>
struct MetadataT<SineWave> : MetadataT<VariableRange<ScalableGenerator>>
{
SmartNode<> CreateNode( FastSIMD::FeatureSet ) const override;

Expand All @@ -126,7 +145,7 @@ namespace FastNoise
groups.push_back( "Basic Generators" );

description =
"Outputs between -1.0 and 1.0";
"Outputs sine wave";
}
};
#endif
Expand Down
22 changes: 16 additions & 6 deletions include/FastNoise/Generators/BasicGenerators.inl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ protected:
}
};

template<FastSIMD::FeatureSet SIMD, typename PARENT>
class FastSIMD::DispatchClass<FastNoise::VariableRange<PARENT>, SIMD> : public virtual FastNoise::VariableRange<PARENT>, public FastSIMD::DispatchClass<PARENT, SIMD>
{
protected:
FS_FORCEINLINE float32v ScaleOutput( float32v value, float nativeMin, float nativeMax ) const
{
return FS::FMulAdd( float32v( 1.0f / ( nativeMax - nativeMin ) ) * float32v( this->mRangeScale ), value - float32v( nativeMin ), float32v( this->mRangeMin ) );
}
};

template<FastSIMD::FeatureSet SIMD>
class FastSIMD::DispatchClass<FastNoise::Constant, SIMD> final : public virtual FastNoise::Constant, public FastSIMD::DispatchClass<FastNoise::Generator, SIMD>
{
Expand All @@ -26,7 +36,7 @@ class FastSIMD::DispatchClass<FastNoise::Constant, SIMD> final : public virtual
};

template<FastSIMD::FeatureSet SIMD>
class FastSIMD::DispatchClass<FastNoise::White, SIMD> final : public virtual FastNoise::White, public FastSIMD::DispatchClass<FastNoise::Generator, SIMD>
class FastSIMD::DispatchClass<FastNoise::White, SIMD> final : public virtual FastNoise::White, public FastSIMD::DispatchClass<FastNoise::VariableRange<Generator>, SIMD>
{
FASTNOISE_IMPL_GEN_T;

Expand All @@ -36,12 +46,12 @@ class FastSIMD::DispatchClass<FastNoise::White, SIMD> final : public virtual Fas
size_t idx = 0;
((pos = FS::Cast<float>( (FS::Cast<int32_t>( pos ) ^ (FS::Cast<int32_t>( pos ) >> 16)) * int32v( Primes::Lookup[idx++] ) )), ...);

return GetValueCoord( seed, FS::Cast<int32_t>( pos )... );
return this->ScaleOutput( GetValueCoord( seed, FS::Cast<int32_t>( pos )... ), -kValueBounds, kValueBounds );
}
};

template<FastSIMD::FeatureSet SIMD>
class FastSIMD::DispatchClass<FastNoise::Checkerboard, SIMD> final : public virtual FastNoise::Checkerboard, public FastSIMD::DispatchClass<FastNoise::ScalableGenerator, SIMD>
class FastSIMD::DispatchClass<FastNoise::Checkerboard, SIMD> final : public virtual FastNoise::Checkerboard, public FastSIMD::DispatchClass<FastNoise::VariableRange<ScalableGenerator>, SIMD>
{
FASTNOISE_IMPL_GEN_T;

Expand All @@ -52,12 +62,12 @@ class FastSIMD::DispatchClass<FastNoise::Checkerboard, SIMD> final : public virt

int32v value = (FS::Convert<int32_t>( pos ) ^ ...);

return float32v( 1.0f ) ^ FS::Cast<float>( value << 31 );
return this->ScaleOutput( FS::Cast<float>( (value & int32v( 1 )) << 30 ), 0, 2 );
}
};

template<FastSIMD::FeatureSet SIMD>
class FastSIMD::DispatchClass<FastNoise::SineWave, SIMD> final : public virtual FastNoise::SineWave, public FastSIMD::DispatchClass<FastNoise::ScalableGenerator, SIMD>
class FastSIMD::DispatchClass<FastNoise::SineWave, SIMD> final : public virtual FastNoise::SineWave, public FastSIMD::DispatchClass<FastNoise::VariableRange<ScalableGenerator>, SIMD>
{
FASTNOISE_IMPL_GEN_T;

Expand All @@ -66,7 +76,7 @@ class FastSIMD::DispatchClass<FastNoise::SineWave, SIMD> final : public virtual
{
this->ScalePositions( pos... );

return (FS::Sin( pos ) * ...);
return this->ScaleOutput( (FS::Sin( pos ) * ...), -1, 1 );
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/FastNoise/Generators/Blends.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ namespace FastNoise
{
groups.push_back( "Blends" );
this->AddGeneratorSource( "Value", &PowInt::SetValue );
this->AddVariable( "Pow", 2, &PowInt::SetPow, 2, INT_MAX );
this->AddVariable( "Pow", 2, &PowInt::SetPow, 2 );

description = "Faster than PowFloat node but only for int powers";
}
Expand Down
28 changes: 14 additions & 14 deletions include/FastNoise/Generators/Cellular.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

namespace FastNoise
{
class Cellular : public virtual ScalableGenerator
template<typename PARENT = VariableRange<ScalableGenerator>>
class Cellular : public virtual PARENT
{
public:
void SetJitterModifier( SmartNodeArg<> gen ) { this->SetSourceMemberVariable( mJitterModifier, gen ); }
Expand All @@ -18,20 +19,20 @@ namespace FastNoise
};

#ifdef FASTNOISE_METADATA
template<>
struct MetadataT<Cellular> : MetadataT<ScalableGenerator>
template<typename PARENT>
struct MetadataT<Cellular<PARENT>> : MetadataT<PARENT>
{
MetadataT()
{
groups.push_back( "Coherent Noise" );
this->AddHybridSource( { "Jitter Modifier", "Above 1.0 will cause grid artifacts" }, 1.0f, &Cellular::SetJitterModifier, &Cellular::SetJitterModifier );
this->groups.push_back( "Coherent Noise" );
this->AddHybridSource( { "Jitter Modifier", "Above 1.0 will cause grid artifacts" }, 1.0f, &Cellular<PARENT>::SetJitterModifier, &Cellular<PARENT>::SetJitterModifier );
this->AddVariableEnum( { "Distance Function", "How distance to closest cells is calculated\nHybrid is EuclideanSquared + Manhattan" },
DistanceFunction::EuclideanSquared, &Cellular::SetDistanceFunction, kDistanceFunction_Strings );
DistanceFunction::EuclideanSquared, &Cellular<PARENT>::SetDistanceFunction, kDistanceFunction_Strings );
}
};
#endif

class CellularValue : public virtual Cellular
class CellularValue : public virtual Cellular<>
{
public:
const Metadata& GetMetadata() const override;
Expand All @@ -46,7 +47,7 @@ namespace FastNoise

#ifdef FASTNOISE_METADATA
template<>
struct MetadataT<CellularValue> : MetadataT<Cellular>
struct MetadataT<CellularValue> : MetadataT<Cellular<>>
{
SmartNode<> CreateNode( FastSIMD::FeatureSet ) const override;

Expand All @@ -56,13 +57,12 @@ namespace FastNoise

description =
"Returns value of Nth closest cell\n"
"Value is generated using white noise\n"
"Output is bounded -1 : 1";
"Value is generated using white noise";
}
};
#endif

class CellularDistance : public virtual Cellular
class CellularDistance : public virtual Cellular<>
{
public:
const Metadata& GetMetadata() const override;
Expand Down Expand Up @@ -90,7 +90,7 @@ namespace FastNoise

#ifdef FASTNOISE_METADATA
template<>
struct MetadataT<CellularDistance> : MetadataT<Cellular>
struct MetadataT<CellularDistance> : MetadataT<Cellular<>>
{
SmartNode<> CreateNode( FastSIMD::FeatureSet ) const override;

Expand All @@ -108,7 +108,7 @@ namespace FastNoise
};
#endif

class CellularLookup : public virtual Cellular
class CellularLookup : public virtual Cellular<ScalableGenerator>
{
public:
const Metadata& GetMetadata() const override;
Expand All @@ -121,7 +121,7 @@ namespace FastNoise

#ifdef FASTNOISE_METADATA
template<>
struct MetadataT<CellularLookup> : MetadataT<Cellular>
struct MetadataT<CellularLookup> : MetadataT<Cellular<ScalableGenerator>>
{
SmartNode<> CreateNode( FastSIMD::FeatureSet ) const override;

Expand Down
Loading

0 comments on commit d2aecea

Please sign in to comment.