RiverVFX.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // River Modeler
  2. // Staggart Creations (http://staggart.xyz)
  3. // Copyright protected under Unity Asset Store EULA
  4. // Copying or referencing source code for the production of new asset store content is strictly prohibited.
  5. using System;
  6. using System.Collections.Generic;
  7. using sc.modeling.water.common.runtime;
  8. using UnityEngine;
  9. #if VFX_GRAPH
  10. using UnityEngine.VFX;
  11. #endif
  12. namespace sc.modeling.river.runtime
  13. {
  14. [ExecuteAlways]
  15. [AddComponentMenu("")] // Hide
  16. public class RiverVFX : MonoBehaviour
  17. {
  18. public RiverModeler river;
  19. public enum Confines
  20. {
  21. EntireMesh,
  22. Bounds
  23. }
  24. public Confines confines;
  25. public Vector3 boundsSize = Vector3.one * 10f;
  26. #if VFX_GRAPH
  27. public VisualEffect targetEffect;
  28. #endif
  29. #if MATHEMATICS
  30. public VFX.PointCache pointCache = new VFX.PointCache();
  31. #endif
  32. protected Bounds bounds;
  33. #if MATHEMATICS
  34. protected List<VFX.Emitter> emitters = new List<VFX.Emitter>();
  35. #endif
  36. protected Bounds particleBounds;
  37. private void Reset()
  38. {
  39. river = GetComponentInParent<RiverModeler>();
  40. #if VFX_GRAPH
  41. targetEffect = GetComponent<VisualEffect>();
  42. if (!targetEffect)
  43. {
  44. targetEffect = gameObject.AddComponent<VisualEffect>();
  45. #if UNITY_EDITOR
  46. string effectPath = UnityEditor.AssetDatabase.GUIDToAssetPath(VFX.DEFAULT_EFFECT_GUID);
  47. if (effectPath != string.Empty)
  48. {
  49. targetEffect.visualEffectAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<VisualEffectAsset>(effectPath);
  50. }
  51. else
  52. {
  53. throw new Exception("Failed to find the effect required for this component, was it not imported from the asset store?");
  54. }
  55. #endif
  56. }
  57. #if MATHEMATICS
  58. pointCache = new VFX.PointCache();
  59. #endif
  60. #endif
  61. }
  62. private void OnEnable()
  63. {
  64. RiverModeler.onPostRebuildRiver += OnRiverRebuild;
  65. }
  66. private void OnDisable()
  67. {
  68. RiverModeler.onPostRebuildRiver -= OnRiverRebuild;
  69. }
  70. private void OnRiverRebuild(RiverModeler instance, RiverModeler.ChangeFlags updateFlags)
  71. {
  72. if (
  73. updateFlags.HasFlag(RiverModeler.ChangeFlags.Spline)
  74. || updateFlags.HasFlag(RiverModeler.ChangeFlags.VertexAttribute)
  75. || updateFlags.HasFlag(RiverModeler.ChangeFlags.Data)
  76. || updateFlags.HasFlag(RiverModeler.ChangeFlags.Foam)
  77. && river != null && instance.GetHashCode() == river.GetHashCode())
  78. {
  79. //Debug.Log("Update VFX emitters: " + updateFlags);
  80. GenerateEmitters();
  81. }
  82. }
  83. //Function to override in derived classes
  84. public virtual void GenerateEmitters()
  85. {
  86. }
  87. protected void ApplyEmitters()
  88. {
  89. #if VFX_GRAPH && MATHEMATICS
  90. pointCache.SetData(emitters, particleBounds);
  91. pointCache.ApplyToEffect(targetEffect);
  92. VFX.SetSortingOrder(targetEffect, river.orderInLayer+1);
  93. #endif
  94. }
  95. private void OnDrawGizmosSelected()
  96. {
  97. Color color = Color.yellow;
  98. if (confines == Confines.Bounds)
  99. {
  100. color.a = 1f;
  101. Gizmos.color = color;
  102. Gizmos.DrawWireCube(this.transform.position, boundsSize);
  103. }
  104. #if VFX_GRAPH
  105. else if (pointCache != null && targetEffect)
  106. {
  107. Gizmos.matrix = targetEffect.transform.localToWorldMatrix;
  108. color.a = 0.2f;
  109. Gizmos.color = color;
  110. Gizmos.DrawWireCube(pointCache.volumeBounds.center, pointCache.volumeBounds.size);
  111. }
  112. #endif
  113. }
  114. public bool IsValidEffect()
  115. {
  116. #if VFX_GRAPH && MATHEMATICS
  117. return VFX.PointCache.IsValidTargetEffect(targetEffect);
  118. #else
  119. return false;
  120. #endif
  121. }
  122. }
  123. }