Skip to content

Commit 4f48774

Browse files
authored
Rust-based noise generation API. (#715)
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.
1 parent 50d6097 commit 4f48774

15 files changed

+727
-2
lines changed

MSBuild/SS14.Engine.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<RemoveDir Directories="$(OutputPath)Resources" />
77
<Copy SourceFiles="@(_ResourceFiles)" DestinationFolder="$(OutputPath)Resources\%(RecursiveDir)" />
88
</Target>
9+
<Target Name="CopySS14Noise">
10+
<Exec Condition="'$(Platform)' == 'x64'" Command="$(Python) ../Tools/download_ss14_noise.py $(Platform) $(TargetOS) $(OutputPath)" CustomErrorRegularExpression="^Error" />
11+
<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!" />
12+
</Target>
913
</Project>

SS14.Client/SS14.Client.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,5 @@
328328
</ItemGroup>
329329
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
330330
<Import Project="..\MSBuild\SS14.Engine.targets" />
331-
</Project>
331+
<Target Name="AfterBuild" DependsOnTargets="CopySS14Noise" />
332+
</Project>

SS14.Server/SS14.Server.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,11 @@
209209
</ItemGroup>
210210
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
211211
<Import Project="..\MSBuild\SS14.Engine.targets" />
212+
<Target Name="AfterBuild" DependsOnTargets="CopySS14Noise" />
212213
<PropertyGroup>
213214
<PreBuildEvent>
214215
</PreBuildEvent>
215216
<PostBuildEvent>
216217
</PostBuildEvent>
217218
</PropertyGroup>
218-
</Project>
219+
</Project>

SS14.Shared.Maths/SS14.Shared.Maths.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,13 @@
9595
<Compile Include="Quaternion.cs" />
9696
<Compile Include="Ray.cs" />
9797
<Compile Include="Vector2.cs" />
98+
<Compile Include="Vector2d.cs" />
9899
<Compile Include="Vector2i.cs" />
99100
<Compile Include="Vector2u.cs" />
100101
<Compile Include="Vector3.cs" />
102+
<Compile Include="Vector3d.cs" />
101103
<Compile Include="Vector4.cs" />
104+
<Compile Include="Vector4d.cs" />
102105
</ItemGroup>
103106
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
104107
</Project>

SS14.Shared.Maths/Vector2d.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace SS14.Shared.Maths
5+
{
6+
[Serializable]
7+
[StructLayout(LayoutKind.Sequential)]
8+
public readonly struct Vector2d
9+
{
10+
public readonly double X;
11+
public readonly double Y;
12+
13+
public Vector2d(double x, double y)
14+
{
15+
X = x;
16+
Y = y;
17+
}
18+
19+
public static implicit operator Vector2d(Vector2 vector)
20+
{
21+
return new Vector2d(vector.X, vector.Y);
22+
}
23+
}
24+
}

SS14.Shared.Maths/Vector3d.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace SS14.Shared.Maths
5+
{
6+
[Serializable]
7+
[StructLayout(LayoutKind.Sequential)]
8+
public readonly struct Vector3d
9+
{
10+
public readonly double X;
11+
public readonly double Y;
12+
public readonly double Z;
13+
14+
public Vector3d(double x, double y, double z)
15+
{
16+
X = x;
17+
Y = y;
18+
Z = z;
19+
}
20+
21+
public static implicit operator Vector3d(Vector3 vector)
22+
{
23+
return new Vector3d(vector.X, vector.Y, vector.Z);
24+
}
25+
}
26+
}

SS14.Shared.Maths/Vector4d.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace SS14.Shared.Maths
5+
{
6+
[Serializable]
7+
[StructLayout(LayoutKind.Sequential)]
8+
public readonly struct Vector4d
9+
{
10+
public readonly double X;
11+
public readonly double Y;
12+
public readonly double Z;
13+
public readonly double W;
14+
15+
public Vector4d(double x, double y, double z, double w)
16+
{
17+
X = x;
18+
Y = y;
19+
Z = z;
20+
W = w;
21+
}
22+
23+
public static implicit operator Vector4d(Vector4 vector)
24+
{
25+
return new Vector4d(vector.X, vector.Y, vector.Z, vector.W);
26+
}
27+
}
28+
}

SS14.Shared/Noise/NoiseGenerator.cs

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using JetBrains.Annotations;
4+
using SS14.Shared.Maths;
5+
6+
namespace SS14.Shared.Noise
7+
{
8+
[PublicAPI]
9+
public sealed class NoiseGenerator : IDisposable
10+
{
11+
[PublicAPI]
12+
public enum NoiseType
13+
{
14+
Fbm = 0,
15+
Ridged = 1
16+
}
17+
18+
private IntPtr _nativeGenerator;
19+
20+
public NoiseGenerator(NoiseType type)
21+
{
22+
_nativeGenerator = _GeneratorMake((byte) type);
23+
}
24+
25+
public bool Disposed => _nativeGenerator == IntPtr.Zero;
26+
27+
public void Dispose()
28+
{
29+
if (Disposed) return;
30+
31+
_GeneratorDispose(_nativeGenerator);
32+
_nativeGenerator = IntPtr.Zero;
33+
}
34+
35+
public void SetFrequency(double frequency)
36+
{
37+
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
38+
_GeneratorSetFrequency(_nativeGenerator, frequency);
39+
}
40+
41+
public void SetLacunarity(double lacunarity)
42+
{
43+
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
44+
_GeneratorSetLacunarity(_nativeGenerator, lacunarity);
45+
}
46+
47+
public void SetPersistence(double persistence)
48+
{
49+
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
50+
_GeneratorSetPersistence(_nativeGenerator, persistence);
51+
}
52+
53+
public void SetPeriodX(double periodX)
54+
{
55+
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
56+
_GeneratorSetPeriodX(_nativeGenerator, periodX);
57+
}
58+
59+
public void SetPeriodY(double periodY)
60+
{
61+
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
62+
_GeneratorSetPeriodY(_nativeGenerator, periodY);
63+
}
64+
65+
public void SetOctaves(uint octaves)
66+
{
67+
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
68+
if (octaves > 32)
69+
throw new ArgumentOutOfRangeException(nameof(octaves), octaves,
70+
"Octave count cannot be greater than 32.");
71+
_GeneratorSetOctaves(_nativeGenerator, octaves);
72+
}
73+
74+
public void SetSeed(uint seed)
75+
{
76+
_GeneratorSetSeed(_nativeGenerator, seed);
77+
}
78+
79+
public double GetNoiseTiled(float x, float y)
80+
{
81+
return GetNoiseTiled(new Vector2(x, y));
82+
}
83+
84+
public double GetNoiseTiled(Vector2d vec)
85+
{
86+
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
87+
88+
return _GetNoiseTiled2D(_nativeGenerator, vec);
89+
}
90+
91+
public double GetNoise(float x, float y)
92+
{
93+
return GetNoise(new Vector2(x, y));
94+
}
95+
96+
public double GetNoise(Vector2d vector)
97+
{
98+
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
99+
100+
return _GetNoise2D(_nativeGenerator, vector);
101+
}
102+
103+
public double GetNoise(float x, float y, float z)
104+
{
105+
return GetNoise(new Vector3(x, y, z));
106+
}
107+
108+
public double GetNoise(Vector3d vector)
109+
{
110+
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
111+
112+
return _GetNoise3D(_nativeGenerator, vector);
113+
}
114+
115+
public double GetNoise(float x, float y, float z, float w)
116+
{
117+
return GetNoise(new Vector4(x, y, z, w));
118+
}
119+
120+
public double GetNoise(Vector4d vector)
121+
{
122+
if (Disposed) throw new ObjectDisposedException(nameof(NoiseGenerator));
123+
124+
return _GetNoise4D(_nativeGenerator, vector);
125+
}
126+
127+
~NoiseGenerator()
128+
{
129+
Dispose();
130+
}
131+
132+
#region FFI
133+
134+
[DllImport("ss14_noise.dll", EntryPoint = "generator_new", CallingConvention = CallingConvention.Cdecl)]
135+
private static extern IntPtr _GeneratorMake(byte noiseType);
136+
137+
[DllImport("ss14_noise.dll", EntryPoint = "generator_set_octaves", CallingConvention = CallingConvention.Cdecl)]
138+
private static extern void _GeneratorSetOctaves(IntPtr gen, uint octaves);
139+
140+
[DllImport("ss14_noise.dll", EntryPoint = "generator_set_persistence",
141+
CallingConvention = CallingConvention.Cdecl)]
142+
private static extern void _GeneratorSetPersistence(IntPtr gen, double persistence);
143+
144+
[DllImport("ss14_noise.dll", EntryPoint = "generator_set_lacunarity",
145+
CallingConvention = CallingConvention.Cdecl)]
146+
private static extern void _GeneratorSetLacunarity(IntPtr gen, double lacunarity);
147+
148+
[DllImport("ss14_noise.dll", EntryPoint = "generator_set_period_x",
149+
CallingConvention = CallingConvention.Cdecl)]
150+
private static extern void _GeneratorSetPeriodX(IntPtr gen, double periodX);
151+
152+
[DllImport("ss14_noise.dll", EntryPoint = "generator_set_period_y",
153+
CallingConvention = CallingConvention.Cdecl)]
154+
private static extern void _GeneratorSetPeriodY(IntPtr gen, double periodY);
155+
156+
[DllImport("ss14_noise.dll", EntryPoint = "generator_set_frequency",
157+
CallingConvention = CallingConvention.Cdecl)]
158+
private static extern void _GeneratorSetFrequency(IntPtr gen, double frequency);
159+
160+
[DllImport("ss14_noise.dll", EntryPoint = "generator_set_seed", CallingConvention = CallingConvention.Cdecl)]
161+
private static extern void _GeneratorSetSeed(IntPtr gen, uint seed);
162+
163+
[DllImport("ss14_noise.dll", EntryPoint = "generator_dispose", CallingConvention = CallingConvention.Cdecl)]
164+
private static extern void _GeneratorDispose(IntPtr gen);
165+
166+
[DllImport("ss14_noise.dll", EntryPoint = "get_noise_2d", CallingConvention = CallingConvention.Cdecl)]
167+
private static extern double _GetNoise2D(IntPtr gen, Vector2d pos);
168+
169+
[DllImport("ss14_noise.dll", EntryPoint = "get_noise_3d", CallingConvention = CallingConvention.Cdecl)]
170+
private static extern double _GetNoise3D(IntPtr gen, Vector3d pos);
171+
172+
[DllImport("ss14_noise.dll", EntryPoint = "get_noise_4d", CallingConvention = CallingConvention.Cdecl)]
173+
private static extern double _GetNoise4D(IntPtr gen, Vector4d pos);
174+
175+
[DllImport("ss14_noise.dll", EntryPoint = "get_noise_tiled_2d", CallingConvention = CallingConvention.Cdecl)]
176+
private static extern double _GetNoiseTiled2D(IntPtr gen, Vector2d pos);
177+
178+
#endregion
179+
}
180+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target/
2+
*.rs.bk

SS14.Shared/Noise/ss14-noise/Cargo.lock

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)