65 lines
1.9 KiB
C#
65 lines
1.9 KiB
C#
|
|
using System;
|
||
|
|
using System.Runtime.InteropServices;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace Hallucinate.GameSetup.Maze.Native
|
||
|
|
{
|
||
|
|
public class NativeNoiseProvider : IDisposable
|
||
|
|
{
|
||
|
|
private const string DLL_NAME = "BackroomsNoise";
|
||
|
|
|
||
|
|
[DllImport(DLL_NAME)]
|
||
|
|
private static extern IntPtr CreateNoiseGenerator(int seed, float frequency, int noiseType);
|
||
|
|
|
||
|
|
[DllImport(DLL_NAME)]
|
||
|
|
private static extern float GetNoiseValue(IntPtr handle, float x, float z);
|
||
|
|
|
||
|
|
[DllImport(DLL_NAME)]
|
||
|
|
private static extern void GetNoiseBuffer(IntPtr handle, float startX, float startZ, int width, int depth, float[] buffer);
|
||
|
|
|
||
|
|
[DllImport(DLL_NAME)]
|
||
|
|
private static extern void DestroyNoiseGenerator(IntPtr handle);
|
||
|
|
|
||
|
|
private IntPtr _handle;
|
||
|
|
public bool IsInitialized => _handle != IntPtr.Zero;
|
||
|
|
|
||
|
|
public NativeNoiseProvider(int seed, float frequency = 0.01f, int noiseType = 0)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
_handle = CreateNoiseGenerator(seed, frequency, noiseType);
|
||
|
|
}
|
||
|
|
catch (DllNotFoundException)
|
||
|
|
{
|
||
|
|
Debug.LogWarning($"Native library '{DLL_NAME}' not found. Ensure it is compiled and placed in Plugins folder.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public float GetNoise(float x, float z)
|
||
|
|
{
|
||
|
|
if (!IsInitialized) return 0f;
|
||
|
|
return GetNoiseValue(_handle, x, z);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void FillBuffer(float startX, float startZ, int width, int depth, float[] buffer)
|
||
|
|
{
|
||
|
|
if (!IsInitialized || buffer == null) return;
|
||
|
|
GetNoiseBuffer(_handle, startX, startZ, width, depth, buffer);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Dispose()
|
||
|
|
{
|
||
|
|
if (IsInitialized)
|
||
|
|
{
|
||
|
|
DestroyNoiseGenerator(_handle);
|
||
|
|
_handle = IntPtr.Zero;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
~NativeNoiseProvider()
|
||
|
|
{
|
||
|
|
Dispose();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|