Skip to content

Commit

Permalink
Rust-based noise generation API. (#715)
Browse files Browse the repository at this point in the history
Supports tileable 2D, 2D, 3D & 4D Ridged & FBM noise.

I still need to generate native versions for all the platforms, so we can't merge this yet.
  • Loading branch information
PJB3005 authored Nov 29, 2018
1 parent 50d6097 commit 4f48774
Show file tree
Hide file tree
Showing 15 changed files with 727 additions and 2 deletions.
4 changes: 4 additions & 0 deletions MSBuild/SS14.Engine.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<RemoveDir Directories="$(OutputPath)Resources" />
<Copy SourceFiles="@(_ResourceFiles)" DestinationFolder="$(OutputPath)Resources\%(RecursiveDir)" />
</Target>
<Target Name="CopySS14Noise">
<Exec Condition="'$(Platform)' == 'x64'" Command="$(Python) ../Tools/download_ss14_noise.py $(Platform) $(TargetOS) $(OutputPath)" CustomErrorRegularExpression="^Error" />
<Warning Condition="'$(Platform)' != 'x64'" Text="Did not download ss14_noise because the platform is not set to x64. Only use this build for unit testing!" />
</Target>
</Project>
3 changes: 2 additions & 1 deletion SS14.Client/SS14.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,5 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\MSBuild\SS14.Engine.targets" />
</Project>
<Target Name="AfterBuild" DependsOnTargets="CopySS14Noise" />
</Project>
3 changes: 2 additions & 1 deletion SS14.Server/SS14.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\MSBuild\SS14.Engine.targets" />
<Target Name="AfterBuild" DependsOnTargets="CopySS14Noise" />
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>
3 changes: 3 additions & 0 deletions SS14.Shared.Maths/SS14.Shared.Maths.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@
<Compile Include="Quaternion.cs" />
<Compile Include="Ray.cs" />
<Compile Include="Vector2.cs" />
<Compile Include="Vector2d.cs" />
<Compile Include="Vector2i.cs" />
<Compile Include="Vector2u.cs" />
<Compile Include="Vector3.cs" />
<Compile Include="Vector3d.cs" />
<Compile Include="Vector4.cs" />
<Compile Include="Vector4d.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
24 changes: 24 additions & 0 deletions SS14.Shared.Maths/Vector2d.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Runtime.InteropServices;

namespace SS14.Shared.Maths
{
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public readonly struct Vector2d
{
public readonly double X;
public readonly double Y;

public Vector2d(double x, double y)
{
X = x;
Y = y;
}

public static implicit operator Vector2d(Vector2 vector)
{
return new Vector2d(vector.X, vector.Y);
}
}
}
26 changes: 26 additions & 0 deletions SS14.Shared.Maths/Vector3d.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Runtime.InteropServices;

namespace SS14.Shared.Maths
{
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public readonly struct Vector3d
{
public readonly double X;
public readonly double Y;
public readonly double Z;

public Vector3d(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}

public static implicit operator Vector3d(Vector3 vector)
{
return new Vector3d(vector.X, vector.Y, vector.Z);
}
}
}
28 changes: 28 additions & 0 deletions SS14.Shared.Maths/Vector4d.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Runtime.InteropServices;

namespace SS14.Shared.Maths
{
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public readonly struct Vector4d
{
public readonly double X;
public readonly double Y;
public readonly double Z;
public readonly double W;

public Vector4d(double x, double y, double z, double w)
{
X = x;
Y = y;
Z = z;
W = w;
}

public static implicit operator Vector4d(Vector4 vector)
{
return new Vector4d(vector.X, vector.Y, vector.Z, vector.W);
}
}
}
180 changes: 180 additions & 0 deletions SS14.Shared/Noise/NoiseGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
using System;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
using SS14.Shared.Maths;

namespace SS14.Shared.Noise
{
[PublicAPI]
public sealed class NoiseGenerator : IDisposable
{
[PublicAPI]
public enum NoiseType
{
Fbm = 0,
Ridged = 1
}

private IntPtr _nativeGenerator;

public NoiseGenerator(NoiseType type)
{
_nativeGenerator = _GeneratorMake((byte) type);
}

public bool Disposed => _nativeGenerator == IntPtr.Zero;

public void Dispose()
{
if (Disposed) return;

_GeneratorDispose(_nativeGenerator);
_nativeGenerator = IntPtr.Zero;
}

public void SetFrequency(double frequency)
{
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
_GeneratorSetFrequency(_nativeGenerator, frequency);
}

public void SetLacunarity(double lacunarity)
{
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
_GeneratorSetLacunarity(_nativeGenerator, lacunarity);
}

public void SetPersistence(double persistence)
{
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
_GeneratorSetPersistence(_nativeGenerator, persistence);
}

public void SetPeriodX(double periodX)
{
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
_GeneratorSetPeriodX(_nativeGenerator, periodX);
}

public void SetPeriodY(double periodY)
{
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
_GeneratorSetPeriodY(_nativeGenerator, periodY);
}

public void SetOctaves(uint octaves)
{
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
if (octaves > 32)
throw new ArgumentOutOfRangeException(nameof(octaves), octaves,
"Octave count cannot be greater than 32.");
_GeneratorSetOctaves(_nativeGenerator, octaves);
}

public void SetSeed(uint seed)
{
_GeneratorSetSeed(_nativeGenerator, seed);
}

public double GetNoiseTiled(float x, float y)
{
return GetNoiseTiled(new Vector2(x, y));
}

public double GetNoiseTiled(Vector2d vec)
{
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));

return _GetNoiseTiled2D(_nativeGenerator, vec);
}

public double GetNoise(float x, float y)
{
return GetNoise(new Vector2(x, y));
}

public double GetNoise(Vector2d vector)
{
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));

return _GetNoise2D(_nativeGenerator, vector);
}

public double GetNoise(float x, float y, float z)
{
return GetNoise(new Vector3(x, y, z));
}

public double GetNoise(Vector3d vector)
{
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));

return _GetNoise3D(_nativeGenerator, vector);
}

public double GetNoise(float x, float y, float z, float w)
{
return GetNoise(new Vector4(x, y, z, w));
}

public double GetNoise(Vector4d vector)
{
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));

return _GetNoise4D(_nativeGenerator, vector);
}

~NoiseGenerator()
{
Dispose();
}

#region FFI

[DllImport("ss14_noise.dll", EntryPoint = "generator_new", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr _GeneratorMake(byte noiseType);

[DllImport("ss14_noise.dll", EntryPoint = "generator_set_octaves", CallingConvention = CallingConvention.Cdecl)]
private static extern void _GeneratorSetOctaves(IntPtr gen, uint octaves);

[DllImport("ss14_noise.dll", EntryPoint = "generator_set_persistence",
CallingConvention = CallingConvention.Cdecl)]
private static extern void _GeneratorSetPersistence(IntPtr gen, double persistence);

[DllImport("ss14_noise.dll", EntryPoint = "generator_set_lacunarity",
CallingConvention = CallingConvention.Cdecl)]
private static extern void _GeneratorSetLacunarity(IntPtr gen, double lacunarity);

[DllImport("ss14_noise.dll", EntryPoint = "generator_set_period_x",
CallingConvention = CallingConvention.Cdecl)]
private static extern void _GeneratorSetPeriodX(IntPtr gen, double periodX);

[DllImport("ss14_noise.dll", EntryPoint = "generator_set_period_y",
CallingConvention = CallingConvention.Cdecl)]
private static extern void _GeneratorSetPeriodY(IntPtr gen, double periodY);

[DllImport("ss14_noise.dll", EntryPoint = "generator_set_frequency",
CallingConvention = CallingConvention.Cdecl)]
private static extern void _GeneratorSetFrequency(IntPtr gen, double frequency);

[DllImport("ss14_noise.dll", EntryPoint = "generator_set_seed", CallingConvention = CallingConvention.Cdecl)]
private static extern void _GeneratorSetSeed(IntPtr gen, uint seed);

[DllImport("ss14_noise.dll", EntryPoint = "generator_dispose", CallingConvention = CallingConvention.Cdecl)]
private static extern void _GeneratorDispose(IntPtr gen);

[DllImport("ss14_noise.dll", EntryPoint = "get_noise_2d", CallingConvention = CallingConvention.Cdecl)]
private static extern double _GetNoise2D(IntPtr gen, Vector2d pos);

[DllImport("ss14_noise.dll", EntryPoint = "get_noise_3d", CallingConvention = CallingConvention.Cdecl)]
private static extern double _GetNoise3D(IntPtr gen, Vector3d pos);

[DllImport("ss14_noise.dll", EntryPoint = "get_noise_4d", CallingConvention = CallingConvention.Cdecl)]
private static extern double _GetNoise4D(IntPtr gen, Vector4d pos);

[DllImport("ss14_noise.dll", EntryPoint = "get_noise_tiled_2d", CallingConvention = CallingConvention.Cdecl)]
private static extern double _GetNoiseTiled2D(IntPtr gen, Vector2d pos);

#endregion
}
}
2 changes: 2 additions & 0 deletions SS14.Shared/Noise/ss14-noise/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
*.rs.bk
78 changes: 78 additions & 0 deletions SS14.Shared/Noise/ss14-noise/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4f48774

Please sign in to comment.