Renamed repo
This commit is contained in:
1 parent
6bee186952
commit
df7c9e1235
16 files changed
+7
-7
No files matched your search
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CubicNoise.Contracts
|
||||
{
|
||||
/// <summary>
|
||||
/// An interface which each noise generator must implement.
|
||||
/// </summary>
|
||||
public interface INoiseEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// Producing a one-dimensional based noise value.
|
||||
/// </summary>
|
||||
/// <param name="x">The x coordinate.</param>
|
||||
/// <returns>The noise value.</returns>
|
||||
float Get(float x);
|
||||
|
||||
/// <summary>
|
||||
/// Produces a two-dimensional based noise value.
|
||||
/// </summary>
|
||||
/// <param name="x">The x coordinate.</param>
|
||||
/// <param name="y">The y coordinate.</param>
|
||||
/// <returns>The noise value.</returns>
|
||||
float Get(float x, float y);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace CubicNoise.Contracts
|
||||
{
|
||||
/// <summary>
|
||||
/// An interface which gets used to provide arbitrary parameter names
|
||||
/// to the general noise engine class. It is a way to abstract the
|
||||
/// concrete (but to the general noise engine class unknown) parameters
|
||||
/// for different noise generators.
|
||||
/// </summary>
|
||||
public interface IParameterName
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using CubicNoise.Contracts;
|
||||
|
||||
namespace CubicNoise
|
||||
{
|
||||
/// <summary>
|
||||
/// This class stores all parameters needed to create a noise engine instance.
|
||||
/// </summary>
|
||||
public sealed class EngineParameters
|
||||
{
|
||||
/// <summary>
|
||||
/// The engine's seed value. To use a string value as seed, use the GetDeterministicHashCode() method
|
||||
/// which gets provided by this library. A different seed value results in a complete different result.
|
||||
/// When you generate e.g. a landscape, two different seeds will produce different landscapes.
|
||||
/// </summary>
|
||||
public int Seed { get; set; } = new Random().Next();
|
||||
|
||||
/// <summary>
|
||||
/// The desired kind of noise generator.
|
||||
/// </summary>
|
||||
public NoiseTypes Type { get; set; } = NoiseTypes.UNKNOWN;
|
||||
|
||||
/// <summary>
|
||||
/// A dictionary of additional parameters needed by the chosen noise generator.
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<IParameterName, int> IntParameters { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using CubicNoise.Noisers;
|
||||
|
||||
namespace CubicNoise
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for this library.
|
||||
/// </summary>
|
||||
public static class Extensions
|
||||
{
|
||||
//
|
||||
/// <summary>
|
||||
/// This is a deterministic hash function for strings. The official hash methods in .NET Core are no longer
|
||||
/// deterministic due to possible hashing attacks, cf. https://youtu.be/R2Cq3CLI6H8.
|
||||
///
|
||||
/// The source for this implementation: https://andrewlock.net/why-is-string-gethashcode-different-each-time-i-run-my-program-in-net-core/.
|
||||
/// Thanks Andrew for providing such a hash function.
|
||||
///
|
||||
/// Aware: Never use this hash function for any security-related task or for any hashtable storage, etc.
|
||||
///
|
||||
/// Please use it only in the case, that you really need a deterministic value. In the context of this
|
||||
/// library, the hash values are usually used for media, games, art, or simulations in order to share
|
||||
/// results with others. This is the only valid use case!
|
||||
/// </summary>
|
||||
/// <param name="str">The string for which a hash value is to be computed.</param>
|
||||
/// <returns>A deterministic 32 bit / 4 byte hash value of the given string.</returns>
|
||||
public static int GetDeterministicHashCode(this string str)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var hash1 = (5381 << 16) + 5381;
|
||||
var hash2 = hash1;
|
||||
|
||||
for (var i = 0; i < str.Length; i += 2)
|
||||
{
|
||||
hash1 = ((hash1 << 5) + hash1) ^ str[i];
|
||||
if (i == str.Length - 1)
|
||||
break;
|
||||
|
||||
hash2 = ((hash2 << 5) + hash2) ^ str[i + 1];
|
||||
}
|
||||
|
||||
return hash1 + (hash2 * 1566083941);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using CubicNoise.Contracts;
|
||||
using CubicNoise.Noisers;
|
||||
|
||||
namespace CubicNoise
|
||||
{
|
||||
/// <summary>
|
||||
/// The main class of the library. You should use it to generate a noise engine. This class is thread-safe.
|
||||
/// You can use all methods from as many threads, as you want. There a no async methods, because the
|
||||
/// performance is very high. Even on a 2019 mid-size business laptop with Intel i5 CPU, each core
|
||||
/// is able to produce > 6 million values per second (2 dimensions).
|
||||
///
|
||||
/// Not only the factory method is thread-safe. Each instance of the class is thread-safe as well.
|
||||
/// </summary>
|
||||
public sealed class NoiseEngine : INoiseEngine
|
||||
{
|
||||
private readonly INoiseEngine engine;
|
||||
|
||||
private NoiseEngine(NoiseTypes type, int seed, IReadOnlyDictionary<IParameterName, int> intParameters)
|
||||
{
|
||||
this.engine = type switch
|
||||
{
|
||||
NoiseTypes.UNKNOWN => new RandomNumberEngine(seed),
|
||||
|
||||
NoiseTypes.CUBIC_NOISE => new CubicNoiseEngine(seed, intParameters),
|
||||
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(type), type, "The provided noise type is unknown.")
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The factory method to use for generating a noise engine. This method is thread-safe. Call it from as many threads
|
||||
/// as you want.
|
||||
/// </summary>
|
||||
/// <param name="parameters">The parameters for the desired noise engine.</param>
|
||||
/// <returns>The desired noise engine instance.</returns>
|
||||
public static NoiseEngine Create(EngineParameters parameters) => new NoiseEngine(parameters.Type, parameters.Seed, parameters?.IntParameters);
|
||||
|
||||
/// <summary>
|
||||
/// Yields a one-dimensional based noise value. This method is thread-safe as well. Call it from as
|
||||
/// many threads as you want. You can expect about 16 million calls per CPU core per second (year 2019).
|
||||
/// </summary>
|
||||
/// <param name="x">The x coordinate.</param>
|
||||
/// <returns>The corresponding noise value for the given coordinate.</returns>
|
||||
public float Get(float x) => this.engine.Get(x);
|
||||
|
||||
/// <summary>
|
||||
/// Yields a two-dimensional based noise value. This method is thread-safe as well. Call it from as
|
||||
/// many threads as you want. You can expect about 6 million calls per CPU core per second (year 2019).
|
||||
/// </summary>
|
||||
/// <param name="x">The x coordinate.</param>
|
||||
/// <param name="y">The y coordinate.</param>
|
||||
/// <returns>The corresponding noise value for the given 2d coordinate.</returns>
|
||||
public float Get(float x, float y) => this.engine.Get(x, y);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<Authors>Thorsten Sommer</Authors>
|
||||
<Copyright>Thorsten Sommer</Copyright>
|
||||
<PackageProjectUrl>https://github.com/SommerEngineering/CubicNoise</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/SommerEngineering/CubicNoise</RepositoryUrl>
|
||||
<PackageTags>Noise, Procedural Generation, Cubic Noise</PackageTags>
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DocumentationFile>$(MSBuildThisFileDirectory)\NoiseEngine.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<DocumentationFile>$(MSBuildThisFileDirectory)\NoiseEngine.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="..\LICENSE">
|
||||
<Pack>True</Pack>
|
||||
<PackagePath></PackagePath>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,168 @@
|
||||
<?xml version="1.0"?>
|
||||
<doc>
|
||||
<assembly>
|
||||
<name>CubicNoise</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="T:CubicNoise.Contracts.INoiseEngine">
|
||||
<summary>
|
||||
An interface which each noise generator must implement.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:CubicNoise.Contracts.INoiseEngine.Get(System.Single)">
|
||||
<summary>
|
||||
Producing a one-dimensional based noise value.
|
||||
</summary>
|
||||
<param name="x">The x coordinate.</param>
|
||||
<returns>The noise value.</returns>
|
||||
</member>
|
||||
<member name="M:CubicNoise.Contracts.INoiseEngine.Get(System.Single,System.Single)">
|
||||
<summary>
|
||||
Produces a two-dimensional based noise value.
|
||||
</summary>
|
||||
<param name="x">The x coordinate.</param>
|
||||
<param name="y">The y coordinate.</param>
|
||||
<returns>The noise value.</returns>
|
||||
</member>
|
||||
<member name="T:CubicNoise.Contracts.IParameterName">
|
||||
<summary>
|
||||
An interface which gets used to provide arbitrary parameter names
|
||||
to the general noise engine class. It is a way to abstract the
|
||||
concrete (but to the general noise engine class unknown) parameters
|
||||
for different noise generators.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:CubicNoise.EngineParameters">
|
||||
<summary>
|
||||
This class stores all parameters needed to create a noise engine instance.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:CubicNoise.EngineParameters.Seed">
|
||||
<summary>
|
||||
The engine's seed value. To use a string value as seed, use the GetDeterministicHashCode() method
|
||||
which gets provided by this library. A different seed value results in a complete different result.
|
||||
When you generate e.g. a landscape, two different seeds will produce different landscapes.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:CubicNoise.EngineParameters.Type">
|
||||
<summary>
|
||||
The desired kind of noise generator.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:CubicNoise.EngineParameters.IntParameters">
|
||||
<summary>
|
||||
A dictionary of additional parameters needed by the chosen noise generator.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:CubicNoise.Extensions">
|
||||
<summary>
|
||||
Extension methods for this library.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:CubicNoise.Extensions.GetDeterministicHashCode(System.String)">
|
||||
<summary>
|
||||
This is a deterministic hash function for strings. The official hash methods in .NET Core are no longer
|
||||
deterministic due to possible hashing attacks, cf. https://youtu.be/R2Cq3CLI6H8.
|
||||
|
||||
The source for this implementation: https://andrewlock.net/why-is-string-gethashcode-different-each-time-i-run-my-program-in-net-core/.
|
||||
Thanks Andrew for providing such a hash function.
|
||||
|
||||
Aware: Never use this hash function for any security-related task or for any hashtable storage, etc.
|
||||
|
||||
Please use it only in the case, that you really need a deterministic value. In the context of this
|
||||
library, the hash values are usually used for media, games, art, or simulations in order to share
|
||||
results with others. This is the only valid use case!
|
||||
</summary>
|
||||
<param name="str">The string for which a hash value is to be computed.</param>
|
||||
<returns>A deterministic 32 bit / 4 byte hash value of the given string.</returns>
|
||||
</member>
|
||||
<member name="T:CubicNoise.NoiseEngine">
|
||||
<summary>
|
||||
The main class of the library. You should use it to generate a noise engine. This class is thread-safe.
|
||||
You can use all methods from as many threads, as you want. There a no async methods, because the
|
||||
performance is very high. Even on a 2019 mid-size business laptop with Intel i5 CPU, each core
|
||||
is able to produce > 6 million values per second (2 dimensions).
|
||||
|
||||
Not only the factory method is thread-safe. Each instance of the class is thread-safe as well.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:CubicNoise.NoiseEngine.Create(CubicNoise.EngineParameters)">
|
||||
<summary>
|
||||
The factory method to use for generating a noise engine. This method is thread-safe. Call it from as many threads
|
||||
as you want.
|
||||
</summary>
|
||||
<param name="parameters">The parameters for the desired noise engine.</param>
|
||||
<returns>The desired noise engine instance.</returns>
|
||||
</member>
|
||||
<member name="M:CubicNoise.NoiseEngine.Get(System.Single)">
|
||||
<summary>
|
||||
Yields a one-dimensional based noise value. This method is thread-safe as well. Call it from as
|
||||
many threads as you want. You can expect about 16 million calls per CPU core per second (year 2019).
|
||||
</summary>
|
||||
<param name="x">The x coordinate.</param>
|
||||
<returns>The corresponding noise value for the given coordinate.</returns>
|
||||
</member>
|
||||
<member name="M:CubicNoise.NoiseEngine.Get(System.Single,System.Single)">
|
||||
<summary>
|
||||
Yields a two-dimensional based noise value. This method is thread-safe as well. Call it from as
|
||||
many threads as you want. You can expect about 6 million calls per CPU core per second (year 2019).
|
||||
</summary>
|
||||
<param name="x">The x coordinate.</param>
|
||||
<param name="y">The y coordinate.</param>
|
||||
<returns>The corresponding noise value for the given 2d coordinate.</returns>
|
||||
</member>
|
||||
<member name="T:CubicNoise.Noisers.CubicNoiseEngine">
|
||||
<summary>
|
||||
This is the cubic noise engine by Job Talle, cf. https://jobtalle.com/cubic_noise.html. It based on the
|
||||
C# version which was provided at Github: https://github.com/jobtalle/CubicNoise/blob/master/c%23/CubicNoise.cs.
|
||||
|
||||
Thanks Job for your implementation.
|
||||
|
||||
Compared to Job's version, this implementation is a bit optimized and
|
||||
used useful C# 8.0 / .NET Core 3.1 LTS features.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:CubicNoise.Noisers.CubicNoiseIntParameters">
|
||||
<summary>
|
||||
This class contains all known cubic noise's parameters.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:CubicNoise.Noisers.CubicNoiseIntParameters.OCTAVE">
|
||||
<summary>
|
||||
Cubic noise's octave parameter.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:CubicNoise.Noisers.CubicNoiseIntParameters.PERIOD_X">
|
||||
<summary>
|
||||
Cubic noise's period x parameter.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:CubicNoise.Noisers.CubicNoiseIntParameters.PERIOD_Y">
|
||||
<summary>
|
||||
Cubic noise's period y parameter.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:CubicNoise.Noisers.RandomNumberEngine">
|
||||
<summary>
|
||||
This is the random number engine which gets used in case that the UNKNOWN type was used.
|
||||
This engine is not meant for production. It is a placeholder for empty values, where a type
|
||||
is needed. The engine will generate a random value each time.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:CubicNoise.NoiseTypes">
|
||||
<summary>
|
||||
All implemented noise generators.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:CubicNoise.NoiseTypes.UNKNOWN">
|
||||
<summary>
|
||||
The UNKNOWN generator is a placeholder for empty values. You should not use it in production. It generates random numbers on every call.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:CubicNoise.NoiseTypes.CUBIC_NOISE">
|
||||
<summary>
|
||||
This is the cubic noise generator by Job Talle, cf. https://jobtalle.com/cubic_noise.html and https://github.com/jobtalle.
|
||||
</summary>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CubicNoise
|
||||
{
|
||||
/// <summary>
|
||||
/// All implemented noise generators.
|
||||
/// </summary>
|
||||
public enum NoiseTypes
|
||||
{
|
||||
/// <summary>
|
||||
/// The UNKNOWN generator is a placeholder for empty values. You should not use it in production. It generates random numbers on every call.
|
||||
/// </summary>
|
||||
UNKNOWN = 0,
|
||||
|
||||
/// <summary>
|
||||
/// This is the cubic noise generator by Job Talle, cf. https://jobtalle.com/cubic_noise.html and https://github.com/jobtalle.
|
||||
/// </summary>
|
||||
CUBIC_NOISE = 1_000,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using CubicNoise.Contracts;
|
||||
|
||||
namespace CubicNoise.Noisers
|
||||
{
|
||||
/// <summary>
|
||||
/// This is the cubic noise engine by Job Talle, cf. https://jobtalle.com/cubic_noise.html. It based on the
|
||||
/// C# version which was provided at Github: https://github.com/jobtalle/CubicNoise/blob/master/c%23/CubicNoise.cs.
|
||||
///
|
||||
/// Thanks Job for your implementation.
|
||||
///
|
||||
/// Compared to Job's version, this implementation is a bit optimized and
|
||||
/// used useful C# 8.0 / .NET Core 3.1 LTS features.
|
||||
/// </summary>
|
||||
internal sealed class CubicNoiseEngine : INoiseEngine
|
||||
{
|
||||
private const int RANDOM_NUMBER_A = 134_775_813;
|
||||
private const int RANDOM_NUMBER_B = 1_103_515_245;
|
||||
|
||||
private readonly int octave;
|
||||
private readonly int periodX;
|
||||
private readonly int periodY;
|
||||
private readonly int seed;
|
||||
|
||||
internal CubicNoiseEngine(int seed, IReadOnlyDictionary<IParameterName, int> intParameters)
|
||||
{
|
||||
this.seed = seed;
|
||||
this.octave = intParameters?.ContainsKey(CubicNoiseIntParameters.OCTAVE) == true ? intParameters[CubicNoiseIntParameters.OCTAVE] : 16;
|
||||
this.periodX = intParameters?.ContainsKey(CubicNoiseIntParameters.PERIOD_X) == true ? intParameters[CubicNoiseIntParameters.PERIOD_X] : 1_000;
|
||||
this.periodY = intParameters?.ContainsKey(CubicNoiseIntParameters.PERIOD_Y) == true ? intParameters[CubicNoiseIntParameters.PERIOD_Y] : 512;
|
||||
}
|
||||
|
||||
public float Get(float x)
|
||||
{
|
||||
var xi = (int)Math.Floor(x / this.octave);
|
||||
var octaveXFactor = x / this.octave - xi;
|
||||
|
||||
return Interpolation(
|
||||
a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: 0),
|
||||
b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: 0),
|
||||
c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: 0),
|
||||
d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: 0),
|
||||
x: octaveXFactor) * 0.5f + 0.25f;
|
||||
}
|
||||
|
||||
public float Get(float x, float y)
|
||||
{
|
||||
var xi = (int)Math.Floor(x / octave);
|
||||
var yi = (int)Math.Floor(y / octave);
|
||||
|
||||
var octaveXFactor = x / octave - xi;
|
||||
var octaveYFactor = y / octave - yi;
|
||||
|
||||
return Interpolation(
|
||||
a: Interpolation(
|
||||
a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)),
|
||||
b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)),
|
||||
c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)),
|
||||
d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)),
|
||||
x: octaveXFactor),
|
||||
|
||||
b: Interpolation(
|
||||
a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)),
|
||||
b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)),
|
||||
c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)),
|
||||
d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)),
|
||||
x: octaveXFactor),
|
||||
|
||||
c: Interpolation(
|
||||
a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)),
|
||||
b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)),
|
||||
c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)),
|
||||
d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)),
|
||||
x: octaveXFactor),
|
||||
|
||||
d: Interpolation(
|
||||
a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)),
|
||||
b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)),
|
||||
c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)),
|
||||
d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)),
|
||||
x: octaveXFactor),
|
||||
|
||||
x: octaveYFactor) * 0.5f + 0.25f;
|
||||
}
|
||||
|
||||
private float RandomNumberGenerator(int x, int y)
|
||||
{
|
||||
return (float)((((x ^ y) * RANDOM_NUMBER_A) ^ (this.seed + x)) * (((RANDOM_NUMBER_B * x) << 16) ^ (RANDOM_NUMBER_B * y) - RANDOM_NUMBER_A)) / int.MaxValue;
|
||||
}
|
||||
|
||||
private static int DeterminePatch(int coordinate, int period)
|
||||
{
|
||||
return coordinate % period;
|
||||
}
|
||||
|
||||
private static float Interpolation(float a, float b, float c, float d, float x)
|
||||
{
|
||||
var p = (d - c) - (a - b);
|
||||
return x * (x * (x * p + ((a - b) - p)) + (c - a)) + b;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using CubicNoise.Contracts;
|
||||
|
||||
namespace CubicNoise.Noisers
|
||||
{
|
||||
/// <summary>
|
||||
/// This class contains all known cubic noise's parameters.
|
||||
/// </summary>
|
||||
public class CubicNoiseIntParameters : IParameterName
|
||||
{
|
||||
/// <summary>
|
||||
/// Cubic noise's octave parameter.
|
||||
/// </summary>
|
||||
public static readonly IParameterName OCTAVE = new CubicNoiseIntParameters();
|
||||
|
||||
/// <summary>
|
||||
/// Cubic noise's period x parameter.
|
||||
/// </summary>
|
||||
public static readonly IParameterName PERIOD_X = new CubicNoiseIntParameters();
|
||||
|
||||
/// <summary>
|
||||
/// Cubic noise's period y parameter.
|
||||
/// </summary>
|
||||
public static readonly IParameterName PERIOD_Y = new CubicNoiseIntParameters();
|
||||
|
||||
private CubicNoiseIntParameters()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using CubicNoise.Contracts;
|
||||
|
||||
namespace CubicNoise.Noisers
|
||||
{
|
||||
/// <summary>
|
||||
/// This is the random number engine which gets used in case that the UNKNOWN type was used.
|
||||
/// This engine is not meant for production. It is a placeholder for empty values, where a type
|
||||
/// is needed. The engine will generate a random value each time.
|
||||
/// </summary>
|
||||
internal sealed class RandomNumberEngine : INoiseEngine
|
||||
{
|
||||
private readonly Random rng;
|
||||
|
||||
internal RandomNumberEngine(int seed)
|
||||
{
|
||||
this.rng = new Random(seed);
|
||||
}
|
||||
|
||||
public float Get(float x)
|
||||
{
|
||||
return this.rng.Next();
|
||||
}
|
||||
|
||||
public float Get(float x, float y)
|
||||
{
|
||||
return this.rng.Next();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user