升级水插件

This commit is contained in:
2026-01-08 22:30:55 +08:00
parent febff82d24
commit ca68084264
415 changed files with 18138 additions and 7134 deletions

View File

@@ -14,14 +14,21 @@ namespace WaveHarmonic.Crest
{
// Waves
[Tooltip("How turbulent/chaotic the waves are.")]
[@Range(0, 1, order = -3)]
[Tooltip("Whether to use the wind turbulence on this component rather than the global wind turbulence.\n\nGlobal wind turbulence comes from the Water Renderer component.")]
[@GenerateAPI]
[@InlineToggle(order = -3), SerializeField]
bool _OverrideGlobalWindTurbulence;
[Tooltip("How turbulent/chaotic the waves are.")]
[@Predicated(nameof(_OverrideGlobalWindTurbulence), hide: true)]
[@ShowComputedProperty(nameof(WindTurbulence))]
[@Range(0, 1, order = -4)]
[@GenerateAPI(Getter.Custom)]
[SerializeField]
float _WindTurbulence = 0.145f;
[Tooltip("How aligned the waves are with wind.")]
[@Range(0, 1, order = -4)]
[@Range(0, 1, order = -5)]
[@GenerateAPI]
[SerializeField]
float _WindAlignment;
@@ -51,23 +58,39 @@ namespace WaveHarmonic.Crest
[@Heading("Collision Data Baking")]
#if !d_WaveHarmonic_Crest_CPUQueries
[HideInInspector]
#endif
[Tooltip("Enable running this FFT with baked data.\n\nThis makes the FFT periodic (repeating in time).")]
[@Predicated(nameof(_Mode), inverted: true, nameof(LodInputMode.Global), hide: true)]
[@DecoratedField, SerializeField]
internal bool _EnableBakedCollision = false;
#if !d_WaveHarmonic_Crest_CPUQueries
[HideInInspector]
#endif
[Tooltip("Frames per second of baked data.\n\nLarger values may help the collision track the surface closely at the cost of more frames and increase baked data size.")]
[@Predicated(nameof(_EnableBakedCollision))]
[@Predicated(nameof(_Mode), inverted: true, nameof(LodInputMode.Global), hide: true)]
[@DecoratedField, SerializeField]
internal int _TimeResolution = 4;
#if !d_WaveHarmonic_Crest_CPUQueries
[HideInInspector]
#endif
[Tooltip("Smallest wavelength required in collision.\n\nTo preview the effect of this, disable power sliders in spectrum for smaller values than this number. Smaller values require more resolution and increase baked data size.")]
[@Predicated(nameof(_EnableBakedCollision))]
[@Predicated(nameof(_Mode), inverted: true, nameof(LodInputMode.Global), hide: true)]
[@DecoratedField, SerializeField]
internal float _SmallestWavelengthRequired = 2f;
#if !d_WaveHarmonic_Crest_CPUQueries
[HideInInspector]
#endif
[Tooltip("FFT waves will loop with a period of this many seconds.\n\nSmaller values decrease data size but can make waves visibly repetitive.")]
[@Predicated(nameof(_EnableBakedCollision))]
[@Predicated(nameof(_Mode), inverted: true, nameof(LodInputMode.Global), hide: true)]
@@ -75,21 +98,28 @@ namespace WaveHarmonic.Crest
[SerializeField]
internal float _BakedTimeLoopLength = 32f;
internal float LoopPeriod => _EnableBakedCollision ? _BakedTimeLoopLength : _TimeLoopLength;
internal float LoopPeriod =>
#if d_WaveHarmonic_Crest_CPUQueries
_EnableBakedCollision ? _BakedTimeLoopLength :
#endif
_TimeLoopLength;
private protected override int MinimumResolution => 16;
private protected override int MaximumResolution => int.MaxValue;
FFTCompute _FFTCompute;
FFTCompute.Parameters _OldFFTParameters;
internal FFTCompute.Parameters FFTParameters => new
internal FFTCompute.Parameters GetFFTParameters(float gravity) => new
(
_ActiveSpectrum,
Resolution,
_TimeLoopLength,
WindSpeedMPS,
WindDirRadForFFT,
_WindTurbulence,
_WindAlignment
WindTurbulence,
_WindAlignment,
gravity
);
private protected override void OnUpdate(WaterRenderer water)
@@ -103,7 +133,7 @@ namespace WaveHarmonic.Crest
ReportMaxDisplacement(water);
// If geometry is being used, the water input shader will rotate the waves to align to geo
var parameters = FFTParameters;
var parameters = GetFFTParameters(water.Gravity);
// Don't create tons of generators when values are varying. Notify so that existing generators may be adapted.
if (parameters.GetHashCode() != _OldFFTParameters.GetHashCode())
@@ -111,6 +141,10 @@ namespace WaveHarmonic.Crest
FFTCompute.OnGenerationDataUpdated(_OldFFTParameters, parameters);
}
#if UNITY_EDITOR
_FFTCompute = FFTCompute.GetInstance(parameters);
#endif
_OldFFTParameters = parameters;
}
@@ -118,14 +152,22 @@ namespace WaveHarmonic.Crest
{
if (_LastGenerateFrameCount != Time.frameCount)
{
// Parameters will unlikely change as our Update is called in LateUpdate with Draw
// not too far after.
var parameters = GetFFTParameters(lod.Water.Gravity);
_WaveBuffers = FFTCompute.GenerateDisplacements
(
buffer,
lod.Water.CurrentTime,
FFTParameters,
parameters,
UpdateDataEachFrame
);
#if UNITY_EDITOR
_FFTCompute = FFTCompute.GetInstance(parameters);
#endif
_LastGenerateFrameCount = Time.frameCount;
}
@@ -157,11 +199,6 @@ namespace WaveHarmonic.Crest
}
}
private protected override void DestroySharedResources()
{
FFTCompute.CleanUpAll();
}
float WindDirRadForFFT
{
get
@@ -172,36 +209,64 @@ namespace WaveHarmonic.Crest
return 0f;
}
return _WaveDirectionHeadingAngle * Mathf.Deg2Rad;
return WaveDirectionHeadingAngle * Mathf.Deg2Rad;
}
}
float GetWindTurbulence()
{
return _OverrideGlobalWindTurbulence || WaterRenderer.Instance == null ? _WindTurbulence : WaterRenderer.Instance.WindTurbulence;
}
#if UNITY_EDITOR
void OnGUI()
{
if (_DrawSlicesInEditor)
{
FFTCompute.GetInstance(FFTParameters)?.OnGUI();
_FFTCompute?.OnGUI();
}
}
internal FFTCompute GetFFTComputeInstance()
{
return FFTCompute.GetInstance(FFTParameters);
}
#endif
}
partial class ShapeFFT
{
static int s_InstanceCount;
private protected override void Awake()
{
base.Awake();
s_InstanceCount++;
}
private protected override void OnDestroy()
{
base.OnDestroy();
if (--s_InstanceCount <= 0)
{
FFTCompute.CleanUpAll();
}
}
}
partial class ShapeFFT : ISerializationCallbackReceiver
{
[SerializeField, HideInInspector]
#pragma warning disable 414
int _Version = 1;
int _Version = 2;
#pragma warning restore 414
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
_Version = MigrateV1(_Version);
if (_Version < 2)
{
_OverrideGlobalWindTurbulence = true;
}
_Version = MigrateV2(_Version);
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
@@ -209,4 +274,19 @@ namespace WaveHarmonic.Crest
// Empty.
}
}
#if UNITY_EDITOR
partial class ShapeFFT
{
private protected override void Reset()
{
base.Reset();
if (_Mode != LodInputMode.Global)
{
_OverrideGlobalWindTurbulence = true;
}
}
}
#endif
}