|
| 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 | +} |
0 commit comments