// Stylized Water 3 by Staggart Creations (http://staggart.xyz) // COPYRIGHT PROTECTED UNDER THE UNITY ASSET STORE EULA (https://unity.com/legal/as-terms) // • Copying or referencing source code for the production of new asset store, or public, content is strictly prohibited! // • Uploading this file to a public repository will subject it to an automated DMCA takedown request. using System; using Unity.Collections; using Unity.Mathematics; using UnityEngine; using Object = UnityEngine.Object; namespace StylizedWater3 { public static partial class HeightQuerySystem { /// /// Holds a list of sampling positions, and the returned water height values relative to them /// public class Sampler { /// /// Input sample positions in world-space /// public NativeArray positions; /// /// Output height values at each sampling /// public NativeArray heightValues; private int currentSampleCount = 0; /// /// Checks if the sampler has been initialized /// /// public bool IsCreated() { return currentSampleCount > 0; } public int SampleCount => currentSampleCount; /// /// Initialize the sampler with a number of sampling points. This allocates the memory required. /// /// Number of positions to sample at. For best performance, this number should be conservative! /// Specify if this sampler is used with the CPU-height query method. If so, no limit is imposed on the sample count /// public void SetSampleCount(int sampleCount, bool cpu = false) { if (cpu == false && sampleCount > Query.MAX_SIZE) { Dispose(); throw new Exception($"The number of sample positions ({sampleCount}) exceeds the maximum capacity ({Query.MAX_SIZE}) of a single sampler." + $" Decrease the number of input positions, or issue multiple smaller requests"); } //Changed if (currentSampleCount != sampleCount) { #if SWS_DEV if(currentSampleCount > 0) Debug.Log($"Sampler count changed from {currentSampleCount} to {sampleCount}"); #endif if (positions.IsCreated) positions.Dispose(); //Input data positions = new NativeArray(sampleCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); if (heightValues.IsCreated) heightValues.Dispose(); //Output data heightValues = new NativeArray(sampleCount, Allocator.Persistent); } currentSampleCount = sampleCount; } [Obsolete("Use SetSampleCount() instead. Method was renamed for clarity")] public void Initialize(int sampleCount, bool cpu = false) { SetSampleCount(sampleCount, cpu); } public void SetSamplePosition(int index, float3 value) { if (index > currentSampleCount) { throw new Exception($"Index out of range. This sampler was initialized with {currentSampleCount} number of samples. Dispose() and SetSampleCount() the sampler to increase the number of samples!"); } positions[index] = value; } public void Dispose() { if (positions.IsCreated) positions.Dispose(); if (heightValues.IsCreated) heightValues.Dispose(); currentSampleCount = 0; } } } }