RainDownfallController.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Funly.SkyStudio
  5. {
  6. // Controls the rain on the foreground mesh.
  7. [RequireComponent(typeof(AudioSource))]
  8. public class RainDownfallController : MonoBehaviour, ISkyModule
  9. {
  10. public MeshRenderer rainMeshRenderer;
  11. public Material rainMaterial;
  12. MaterialPropertyBlock m_PropertyBlock;
  13. AudioSource m_RainAudioSource;
  14. float m_TimeOfDay;
  15. SkyProfile m_SkyProfile;
  16. //WeatherEnclosure m_Enclosure;
  17. public void SetWeatherEnclosure(WeatherEnclosure enclosure)
  18. {
  19. //m_Enclosure = enclosure;
  20. if (rainMeshRenderer != null) {
  21. rainMeshRenderer.enabled = false;
  22. rainMeshRenderer = null;
  23. }
  24. if (!enclosure) {
  25. return;
  26. }
  27. rainMeshRenderer = enclosure.GetComponentInChildren<MeshRenderer>();
  28. if (!rainMeshRenderer) {
  29. Debug.LogError("Can't render rain since there's no MeshRenderer on the WeatherEnclosure");
  30. return;
  31. }
  32. m_PropertyBlock = new MaterialPropertyBlock();
  33. if (!rainMaterial) {
  34. return;
  35. }
  36. rainMeshRenderer.material = rainMaterial;
  37. rainMeshRenderer.enabled = true;
  38. UpdateForTimeOfDay(m_SkyProfile, m_TimeOfDay);
  39. }
  40. private void Update()
  41. {
  42. if (m_SkyProfile == null) {
  43. return;
  44. }
  45. UpdateForTimeOfDay(m_SkyProfile, m_TimeOfDay);
  46. }
  47. public void UpdateForTimeOfDay(SkyProfile skyProfile, float timeOfDay)
  48. {
  49. m_SkyProfile = skyProfile;
  50. m_TimeOfDay = timeOfDay;
  51. if (!skyProfile) {
  52. return;
  53. }
  54. // Update the volume.
  55. if (m_RainAudioSource == null) {
  56. m_RainAudioSource = GetComponent<AudioSource>();
  57. }
  58. // Suppress rain sounds if rain isn't enabled.
  59. if (skyProfile == null || m_SkyProfile.IsFeatureEnabled(ProfileFeatureKeys.RainFeature) == false) {
  60. if (m_RainAudioSource != null) {
  61. m_RainAudioSource.enabled = false;
  62. }
  63. return;
  64. }
  65. if (!rainMaterial) {
  66. Debug.LogError("Can't render rain without a rain material");
  67. return;
  68. }
  69. if (!rainMeshRenderer) {
  70. Debug.LogError("Can't show rain without an enclosure mesh renderer.");
  71. return;
  72. }
  73. if (m_PropertyBlock == null) {
  74. m_PropertyBlock = new MaterialPropertyBlock();
  75. }
  76. rainMeshRenderer.enabled = true;
  77. rainMeshRenderer.material = rainMaterial;
  78. rainMeshRenderer.GetPropertyBlock(m_PropertyBlock);
  79. float rainNearIntensity = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.RainNearIntensityKey, timeOfDay);
  80. float rainFarIntensity = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.RainFarIntensityKey, timeOfDay);
  81. Texture rainNearTexture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.RainNearTextureKey, timeOfDay);
  82. Texture rainFarTexture = skyProfile.GetTexturePropertyValue(ProfilePropertyKeys.RainFarTextureKey, timeOfDay);
  83. float rainNearSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.RainNearSpeedKey, timeOfDay);
  84. float rainFarSpeed = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.RainFarSpeedKey, timeOfDay);
  85. Color tintColor = m_SkyProfile.GetColorPropertyValue(ProfilePropertyKeys.RainTintColorKey, m_TimeOfDay);
  86. float turbulence = m_SkyProfile.GetNumberPropertyValue(ProfilePropertyKeys.RainWindTurbulence, m_TimeOfDay);
  87. float turbulenceSpeed = m_SkyProfile.GetNumberPropertyValue(ProfilePropertyKeys.RainWindTurbulenceSpeed, m_TimeOfDay);
  88. float nearTiling = m_SkyProfile.GetNumberPropertyValue(ProfilePropertyKeys.RainNearTextureTiling, m_TimeOfDay);
  89. float farTiling = m_SkyProfile.GetNumberPropertyValue(ProfilePropertyKeys.RainFarTextureTiling, m_TimeOfDay);
  90. if (rainNearTexture != null) {
  91. m_PropertyBlock.SetTexture("_NearTex", rainNearTexture);
  92. m_PropertyBlock.SetVector("_NearTex_ST", new Vector4(nearTiling, nearTiling, nearTiling, 1));
  93. }
  94. m_PropertyBlock.SetFloat("_NearDensity", rainNearIntensity);
  95. m_PropertyBlock.SetFloat("_NearRainSpeed", rainNearSpeed);
  96. if (rainFarTexture != null) {
  97. m_PropertyBlock.SetTexture("_FarTex", rainFarTexture);
  98. m_PropertyBlock.SetVector("_FarTex_ST", new Vector4(farTiling, farTiling, farTiling, 1));
  99. }
  100. m_PropertyBlock.SetFloat("_FarDensity", rainFarIntensity);
  101. m_PropertyBlock.SetFloat("_FarRainSpeed", rainFarSpeed);
  102. m_PropertyBlock.SetColor("_TintColor", tintColor);
  103. m_PropertyBlock.SetFloat("_Turbulence", turbulence);
  104. m_PropertyBlock.SetFloat("_TurbulenceSpeed", turbulenceSpeed);
  105. rainMeshRenderer.SetPropertyBlock(m_PropertyBlock);
  106. if (skyProfile.IsFeatureEnabled(ProfileFeatureKeys.RainSoundFeature)) {
  107. m_RainAudioSource.enabled = true;
  108. m_RainAudioSource.volume = skyProfile.GetNumberPropertyValue(ProfilePropertyKeys.RainSoundVolumeKey, timeOfDay);
  109. } else {
  110. m_RainAudioSource.enabled = false;
  111. }
  112. }
  113. }
  114. }