399 lines
11 KiB
C#
399 lines
11 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.Rendering.PostProcessing;
|
|
|
|
namespace Gaia
|
|
{
|
|
[RequireComponent(typeof(Light))]
|
|
public class GaiaUnderWaterEffects : MonoBehaviour
|
|
{
|
|
[Header("Global")]
|
|
public GaiaConstants.EnvironmentRenderer m_currentRenderer;
|
|
|
|
[Header("Caustic Settings")]
|
|
[Tooltip("Sets if the light object follows the player")]
|
|
public bool m_followPlayer;
|
|
|
|
[Tooltip("Creates gameobject walls around the player to fix the horizon color issues with the fog rendering")]
|
|
public bool m_useHorizonFix = true;
|
|
|
|
[Tooltip("Creates simple underwater particles effect")]
|
|
public bool m_useUnderwaterparticles = true;
|
|
|
|
[Tooltip("Sets the caustics size, not this only works on directonial lights")]
|
|
[Range(0f, 2000f)]
|
|
public int m_causticsSize = 5;
|
|
|
|
[Tooltip("Caustic textures used to generate the effect")]
|
|
public Texture[] m_cookies = new Texture[16];
|
|
|
|
[Tooltip("How many frame renders are made, higher the number the faster the animation. Recommend between 15-30 for optimial performance and visuals")]
|
|
public float m_framesPerSecond = 25f;
|
|
|
|
[Tooltip("What the current sea level is. Gaias default is 50")]
|
|
public float m_sealevel = 50f;
|
|
|
|
[Header("Underwater Settings")]
|
|
[Range(0f, 1f)]
|
|
[Tooltip("Sets the underwater ambiance audio volume")]
|
|
public float m_underwaterSoundFXVolume = 0.4f;
|
|
|
|
[Range(0f, 1f)]
|
|
[Tooltip("Sets the water submerge audio volume")]
|
|
public float m_waterSubmergeSounfFXVolume = 0.4f;
|
|
|
|
[Tooltip("Sets the submerge down sound fx")]
|
|
public AudioClip m_submergeSoundFXDown;
|
|
|
|
[Tooltip("Sets the submerge up sound fx")]
|
|
public AudioClip m_submergeSoundFXUp;
|
|
|
|
[Tooltip("Sets the underwater fog color")]
|
|
public Color32 m_underWaterFogColor = new Color32(76, 112, 142, byte.MaxValue);
|
|
|
|
[Tooltip("Sets the underwater fog distance")]
|
|
public float m_underWaterFogDistance = 70f;
|
|
|
|
private Light mainlight;
|
|
|
|
private Transform causticsObject;
|
|
|
|
public Transform player;
|
|
|
|
private int indexNumber;
|
|
|
|
private bool coroutineStatus;
|
|
|
|
[HideInInspector]
|
|
public Color32 storedFogColor;
|
|
|
|
[HideInInspector]
|
|
public float storedFogDistance;
|
|
|
|
private GameObject ambientAudio;
|
|
|
|
private GameObject underwaterAudio;
|
|
|
|
private GameObject horizonObject;
|
|
|
|
[HideInInspector]
|
|
public GameObject horizonObjectStored;
|
|
|
|
private AudioSource objectAudioSource;
|
|
|
|
private GameObject underwaterParticles;
|
|
|
|
[HideInInspector]
|
|
public GameObject underwaterParticlesStored;
|
|
|
|
private Transform partentObject;
|
|
|
|
private GaiaSettings m_gaiaSettings;
|
|
|
|
private GaiaSceneInfo m_gaiaSceneInfo;
|
|
|
|
public PostProcessVolume transitionPostFX;
|
|
|
|
public PostProcessVolume underwaterPostFX;
|
|
|
|
private void Start()
|
|
{
|
|
transitionPostFX = GameObject.Find("Underwater Transition PostFX").GetComponent<PostProcessVolume>();
|
|
underwaterPostFX = GameObject.Find("Underwater PostFX").GetComponent<PostProcessVolume>();
|
|
if (Application.isPlaying)
|
|
{
|
|
if (transitionPostFX != null)
|
|
{
|
|
transitionPostFX.enabled = true;
|
|
}
|
|
if (underwaterPostFX != null)
|
|
{
|
|
underwaterPostFX.enabled = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (transitionPostFX != null)
|
|
{
|
|
transitionPostFX.enabled = false;
|
|
}
|
|
if (underwaterPostFX != null)
|
|
{
|
|
underwaterPostFX.enabled = false;
|
|
}
|
|
}
|
|
if (m_gaiaSettings == null)
|
|
{
|
|
m_gaiaSettings = GaiaUtils.GetGaiaSettings();
|
|
}
|
|
if (m_gaiaSceneInfo == null)
|
|
{
|
|
m_gaiaSceneInfo = GaiaSceneInfo.GetSceneInfo();
|
|
}
|
|
if (m_gaiaSettings != null)
|
|
{
|
|
m_currentRenderer = m_gaiaSettings.m_currentRenderer;
|
|
}
|
|
if (GameObject.Find("Water Planar Reflections") != null)
|
|
{
|
|
ReflectionProbe component = GameObject.Find("Water Planar Reflections").GetComponent<ReflectionProbe>();
|
|
if (component != null)
|
|
{
|
|
component.mode = ReflectionProbeMode.Custom;
|
|
}
|
|
}
|
|
mainlight = base.gameObject.GetComponent<Light>();
|
|
mainlight.cookie = null;
|
|
causticsObject = base.gameObject.transform;
|
|
partentObject = GetOrCreateEnvironmentParent().transform;
|
|
if (m_currentRenderer == GaiaConstants.EnvironmentRenderer.HighDefinition2018x)
|
|
{
|
|
if (!(GameObject.Find("High Definition Environment Volume") != null))
|
|
{
|
|
Debug.LogWarning("Unabled to find a HDRP environment volume in the scene. Please insure one is set in this scene.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
storedFogColor = RenderSettings.fogColor;
|
|
storedFogDistance = RenderSettings.fogEndDistance;
|
|
}
|
|
ambientAudio = null;
|
|
if (ambientAudio == null)
|
|
{
|
|
ambientAudio = GameObject.Find("Ambient Audio");
|
|
}
|
|
underwaterAudio = null;
|
|
if (underwaterAudio == null)
|
|
{
|
|
underwaterAudio = GameObject.Find("Underwater SoundFX");
|
|
}
|
|
if (m_gaiaSceneInfo != null)
|
|
{
|
|
m_sealevel = m_gaiaSceneInfo.m_seaLevel;
|
|
}
|
|
if (m_useHorizonFix)
|
|
{
|
|
horizonObject = GameObject.Find("Ambient Underwater Horizon");
|
|
if (horizonObjectStored != null)
|
|
{
|
|
horizonObject = Object.Instantiate(horizonObjectStored);
|
|
horizonObject.name = "Ambient Underwater Horizon";
|
|
if (partentObject != null)
|
|
{
|
|
horizonObject.transform.parent = partentObject;
|
|
}
|
|
MeshRenderer[] componentsInChildren = horizonObject.GetComponentsInChildren<MeshRenderer>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].enabled = false;
|
|
}
|
|
}
|
|
}
|
|
if (m_useUnderwaterparticles)
|
|
{
|
|
underwaterParticles = GameObject.Find("Underwater Particles Effects");
|
|
if (underwaterParticlesStored != null)
|
|
{
|
|
underwaterParticles = Object.Instantiate(underwaterParticlesStored);
|
|
underwaterParticles.name = "Underwater Particles Effects";
|
|
underwaterParticles.SetActive(value: false);
|
|
if (partentObject != null)
|
|
{
|
|
underwaterParticles.transform.parent = partentObject;
|
|
}
|
|
}
|
|
}
|
|
if (base.gameObject.GetComponent<AudioSource>() == null)
|
|
{
|
|
base.gameObject.AddComponent<AudioSource>();
|
|
objectAudioSource = base.gameObject.GetComponent<AudioSource>();
|
|
objectAudioSource.volume = m_waterSubmergeSounfFXVolume;
|
|
}
|
|
else
|
|
{
|
|
objectAudioSource = base.gameObject.GetComponent<AudioSource>();
|
|
objectAudioSource.volume = m_waterSubmergeSounfFXVolume;
|
|
}
|
|
if (mainlight.type == LightType.Directional)
|
|
{
|
|
mainlight.cookieSize = m_causticsSize;
|
|
}
|
|
StopAllCoroutines();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (m_currentRenderer == GaiaConstants.EnvironmentRenderer.HighDefinition2018x && !(GameObject.Find("High Definition Environment Volume") != null))
|
|
{
|
|
Debug.LogWarning("Unabled to find a HDRP environment volume in the scene. Please insure one is set in this scene.");
|
|
}
|
|
}
|
|
|
|
public Transform GetThePlayer()
|
|
{
|
|
return null;
|
|
}
|
|
|
|
private static GameObject GetOrCreateEnvironmentParent()
|
|
{
|
|
GameObject gameObject = GameObject.Find("Gaia Environment");
|
|
if (gameObject == null)
|
|
{
|
|
gameObject = new GameObject("Gaia Environment");
|
|
}
|
|
return gameObject;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (m_currentRenderer != GaiaConstants.EnvironmentRenderer.HighDefinition2018x)
|
|
{
|
|
RenderSettings.fogColor = storedFogColor;
|
|
RenderSettings.fogEndDistance = storedFogDistance;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!(player != null))
|
|
{
|
|
return;
|
|
}
|
|
if (m_useHorizonFix && horizonObject != null)
|
|
{
|
|
horizonObject.transform.position = new Vector3(player.position.x + 1000f, m_sealevel - 300f, player.position.z);
|
|
}
|
|
if (m_useUnderwaterparticles && underwaterParticles != null)
|
|
{
|
|
underwaterParticles.transform.position = player.position;
|
|
}
|
|
if (m_followPlayer)
|
|
{
|
|
causticsObject.position = new Vector3(player.position.x, m_sealevel, player.position.z);
|
|
}
|
|
if (player.position.y >= m_sealevel)
|
|
{
|
|
if (!coroutineStatus)
|
|
{
|
|
return;
|
|
}
|
|
StopAllCoroutines();
|
|
StopCoroutine(CausticsAnimation(systemOn: false));
|
|
if (m_currentRenderer != GaiaConstants.EnvironmentRenderer.HighDefinition2018x)
|
|
{
|
|
RenderSettings.fogColor = storedFogColor;
|
|
RenderSettings.fogEndDistance = storedFogDistance;
|
|
}
|
|
indexNumber = 0;
|
|
if (mainlight != null)
|
|
{
|
|
mainlight.cookie = null;
|
|
}
|
|
if (m_submergeSoundFXUp != null && objectAudioSource != null)
|
|
{
|
|
objectAudioSource.PlayOneShot(m_submergeSoundFXUp, m_waterSubmergeSounfFXVolume);
|
|
}
|
|
if (m_useHorizonFix && horizonObject != null)
|
|
{
|
|
MeshRenderer[] componentsInChildren = horizonObject.GetComponentsInChildren<MeshRenderer>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].enabled = false;
|
|
}
|
|
}
|
|
if (m_useUnderwaterparticles)
|
|
{
|
|
underwaterParticles.SetActive(value: false);
|
|
}
|
|
if (ambientAudio != null)
|
|
{
|
|
AudioSource component = ambientAudio.GetComponent<AudioSource>();
|
|
if (component != null)
|
|
{
|
|
component.volume = 0.5f;
|
|
}
|
|
}
|
|
if (underwaterAudio != null)
|
|
{
|
|
AudioSource component2 = underwaterAudio.GetComponent<AudioSource>();
|
|
if (component2 != null)
|
|
{
|
|
component2.volume = 0f;
|
|
}
|
|
}
|
|
coroutineStatus = false;
|
|
}
|
|
else
|
|
{
|
|
if (!(player.position.y < m_sealevel) || coroutineStatus)
|
|
{
|
|
return;
|
|
}
|
|
StartCoroutine(CausticsAnimation(systemOn: true));
|
|
if (m_currentRenderer != GaiaConstants.EnvironmentRenderer.HighDefinition2018x)
|
|
{
|
|
RenderSettings.fogColor = m_underWaterFogColor;
|
|
RenderSettings.fogEndDistance = m_underWaterFogDistance;
|
|
}
|
|
if (objectAudioSource != null && m_submergeSoundFXDown != null)
|
|
{
|
|
objectAudioSource.PlayOneShot(m_submergeSoundFXDown, m_waterSubmergeSounfFXVolume);
|
|
}
|
|
if (m_useHorizonFix && horizonObject != null)
|
|
{
|
|
MeshRenderer[] componentsInChildren = horizonObject.GetComponentsInChildren<MeshRenderer>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].enabled = true;
|
|
}
|
|
}
|
|
if (m_useUnderwaterparticles)
|
|
{
|
|
underwaterParticles.SetActive(value: true);
|
|
underwaterParticles.GetComponent<ParticleSystem>().Play();
|
|
}
|
|
if (ambientAudio != null)
|
|
{
|
|
AudioSource component3 = ambientAudio.GetComponent<AudioSource>();
|
|
if (component3 != null)
|
|
{
|
|
component3.volume = 0f;
|
|
}
|
|
}
|
|
if (underwaterAudio != null)
|
|
{
|
|
AudioSource component4 = underwaterAudio.GetComponent<AudioSource>();
|
|
if (component4 != null)
|
|
{
|
|
component4.volume = m_underwaterSoundFXVolume;
|
|
}
|
|
}
|
|
coroutineStatus = true;
|
|
}
|
|
}
|
|
|
|
private IEnumerator CausticsAnimation(bool systemOn)
|
|
{
|
|
while (systemOn)
|
|
{
|
|
if (mainlight != null)
|
|
{
|
|
mainlight.cookie = m_cookies[indexNumber];
|
|
indexNumber++;
|
|
}
|
|
if (indexNumber == m_cookies.Length)
|
|
{
|
|
indexNumber = 0;
|
|
}
|
|
yield return new WaitForSeconds(1f / m_framesPerSecond);
|
|
}
|
|
}
|
|
|
|
public void LoadCaustics()
|
|
{
|
|
}
|
|
}
|
|
}
|