94 lines
2.0 KiB
C#
94 lines
2.0 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace UltimateWater
|
|
{
|
|
[AddComponentMenu("Ultimate Water/Water Profile Blend")]
|
|
public class WaterProfileBlend : MonoBehaviour
|
|
{
|
|
public Water Water;
|
|
|
|
[SerializeField]
|
|
private List<WaterProfile> _Profiles = new List<WaterProfile> { null };
|
|
|
|
[SerializeField]
|
|
private List<float> _Weights = new List<float> { 1f };
|
|
|
|
private void Awake()
|
|
{
|
|
if (Water == null)
|
|
{
|
|
Water = Utilities.GetWaterReference();
|
|
if (Water.IsNullReference(this))
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
_Profiles.RemoveAll((WaterProfile x) => x == null);
|
|
if (_Profiles.Count == 0)
|
|
{
|
|
Debug.LogError("[WaterProfileBlend] : no valid profiles found");
|
|
base.enabled = false;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Water.ProfilesManager.SetProfiles(CreateProfiles(_Profiles, _Weights));
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
if (Application.isPlaying && Water != null && Water.WindWaves != null)
|
|
{
|
|
Water.ProfilesManager.SetProfiles(CreateProfiles(_Profiles, _Weights));
|
|
}
|
|
if (WeightSum(_Weights) == 0f)
|
|
{
|
|
_Weights[0] = 1f;
|
|
}
|
|
}
|
|
|
|
private void Reset()
|
|
{
|
|
if (Water == null)
|
|
{
|
|
Water = Utilities.GetWaterReference();
|
|
}
|
|
}
|
|
|
|
private static Water.WeightedProfile[] CreateProfiles(List<WaterProfile> profiles, List<float> weights)
|
|
{
|
|
List<float> list = NormalizeWeights(weights);
|
|
int count = profiles.Count;
|
|
Water.WeightedProfile[] array = new Water.WeightedProfile[count];
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
array[i] = new Water.WeightedProfile(profiles[i], list[i]);
|
|
}
|
|
return array;
|
|
}
|
|
|
|
private static float WeightSum(List<float> weights)
|
|
{
|
|
float num = 0f;
|
|
for (int i = 0; i < weights.Count; i++)
|
|
{
|
|
num += weights[i];
|
|
}
|
|
return num;
|
|
}
|
|
|
|
private static List<float> NormalizeWeights(List<float> weights)
|
|
{
|
|
List<float> list = new List<float>(weights.Count);
|
|
float num = WeightSum(weights);
|
|
for (int i = 0; i < weights.Count; i++)
|
|
{
|
|
list.Add(weights[i] / num);
|
|
}
|
|
return list;
|
|
}
|
|
}
|
|
}
|