LightningController.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Funly.SkyStudio
  5. {
  6. public class LightningController : MonoBehaviour, ISkyModule
  7. {
  8. SkyProfile m_SkyProfile;
  9. float m_TimeOfDay;
  10. List<LightningRenderer> m_LightningRenderers = new List<LightningRenderer>();
  11. private void Start()
  12. {
  13. // Verify GPU instancing is supported.
  14. if (SystemInfo.supportsInstancing == false) {
  15. Debug.LogWarning("Can't render lightning since GPU instancing is not supported on this platform.");
  16. enabled = false;
  17. return;
  18. }
  19. ClearLightningRenderers();
  20. }
  21. public void UpdateForTimeOfDay(SkyProfile skyProfile, float timeOfDay)
  22. {
  23. m_SkyProfile = skyProfile;
  24. m_TimeOfDay = timeOfDay;
  25. }
  26. public void Update()
  27. {
  28. if (m_SkyProfile == null || m_SkyProfile.IsFeatureEnabled(ProfileFeatureKeys.LightningFeature) == false) {
  29. ClearLightningRenderers();
  30. return;
  31. }
  32. if (m_SkyProfile.lightningArtSet == null || m_SkyProfile.lightningArtSet.lightingStyleItems == null ||
  33. m_SkyProfile.lightningArtSet.lightingStyleItems.Count == 0) {
  34. return;
  35. }
  36. if (m_SkyProfile.lightningArtSet.lightingStyleItems.Count != m_LightningRenderers.Count) {
  37. ClearLightningRenderers();
  38. CreateLightningRenderers();
  39. }
  40. // Assign a style to each renderer.
  41. for (int i = 0; i < m_SkyProfile.lightningArtSet.lightingStyleItems.Count; i++) {
  42. LightningArtItem style = m_SkyProfile.lightningArtSet.lightingStyleItems[i];
  43. LightningRenderer lr = m_LightningRenderers[i];
  44. lr.UpdateForTimeOfDay(m_SkyProfile, m_TimeOfDay, style);
  45. }
  46. }
  47. public void ClearLightningRenderers()
  48. {
  49. for (int i = 0; i < this.transform.childCount; i++) {
  50. Destroy(this.transform.GetChild(i).gameObject);
  51. }
  52. m_LightningRenderers.Clear();
  53. }
  54. public void CreateLightningRenderers()
  55. {
  56. for (int i = 0; i < m_SkyProfile.lightningArtSet.lightingStyleItems.Count; i++) {
  57. GameObject go = new GameObject("Lightning Renderer");
  58. LightningRenderer lr = go.AddComponent<LightningRenderer>();
  59. lr.transform.parent = this.transform;
  60. m_LightningRenderers.Add(lr);
  61. }
  62. }
  63. }
  64. }