RiverEditor.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.river.runtime;
  8. using sc.modeling.water.common.editor;
  9. using UnityEditor;
  10. using UnityEngine;
  11. using Object = UnityEngine.Object;
  12. #if SPLINES
  13. using UnityEngine.Splines;
  14. #endif
  15. namespace sc.modeling.river.editor
  16. {
  17. public class RiverEditor
  18. {
  19. private static readonly string[] materialGUIDS = new[]
  20. {
  21. "37de31e0cff7a6143936661d85112042", //SW3
  22. "befd35ed3bbfbef418af6a4754907fab", //SW2
  23. "46c16e84bcd61ab42b61bd6b597c17fb", //Demo
  24. "31321ba15b8f8eb4c954353edc038b1d" //URP Lit
  25. };
  26. private const string CASCADE_VFX_PREFAB_GUID = "c914fe647c90fa647bec5c4f7b119275";
  27. private const string SPLASH_VFX_PREFAB_GUID = "f3d93d93f1aaa0b40b1bf183eb50539a";
  28. private static Material GetDefaultMaterial()
  29. {
  30. for (int i = 0; i < materialGUIDS.Length; i++)
  31. {
  32. string defaultMatPath = AssetDatabase.GUIDToAssetPath(materialGUIDS[i]);
  33. //If installed
  34. if (defaultMatPath != string.Empty)
  35. {
  36. return AssetDatabase.LoadAssetAtPath(defaultMatPath, typeof(Material)) as Material;
  37. }
  38. }
  39. return null;
  40. }
  41. [MenuItem("GameObject/3D Object/Water/River", false, 1000)]
  42. public static GameObject CreateRiverObject()
  43. {
  44. GameObject gameObject = new GameObject(GameObjectUtility.GetUniqueNameForSibling(null, "River"));
  45. bool addSpline = EditorUtility.DisplayDialog("Create river object", "Create with a new spline?", "Yes", "No");
  46. if (addSpline)
  47. {
  48. #if SPLINES
  49. SplineContainer splineContainer = gameObject.AddComponent<SplineContainer>();
  50. splineContainer.Splines = null;
  51. int knots = 5;
  52. float amplitude = 2f;
  53. float length = 50f;
  54. Spline spline = new Spline(knots, false);
  55. for (int i = 0; i <= knots; i++)
  56. {
  57. float t = (float)i / (float)knots;
  58. BezierKnot knot = new BezierKnot();
  59. knot.Position = new Vector3(Mathf.Sin(t * knots * 2f) * amplitude, 0f, (t * length) - (length * 0.5f));
  60. spline.Add(knot, TangentMode.Linear);
  61. }
  62. //Automatically recalculate tangents
  63. spline.SetTangentMode(new SplineRange(0, spline.Count), TangentMode.AutoSmooth);
  64. splineContainer.AddSpline(spline);
  65. #else
  66. throw new Exception("The Splines package isn't installed.");
  67. #endif
  68. }
  69. gameObject.layer = LayerMask.NameToLayer("Water");
  70. #if UNITY_EDITOR
  71. Undo.RegisterCreatedObjectUndo(gameObject, "Created River Object");
  72. #endif
  73. RiverModeler component = gameObject.AddComponent<RiverModeler>();
  74. MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
  75. component.meshFilter = meshFilter;
  76. MeshRenderer r = gameObject.AddComponent<MeshRenderer>();
  77. r.sharedMaterial = GetDefaultMaterial();
  78. component.Rebuild();
  79. EditorUtilities.OnPostGameObjectCreation(gameObject);
  80. return gameObject;
  81. }
  82. #if SPLINES
  83. [MenuItem("CONTEXT/SplineContainer/Add River")]
  84. private static void AddRiverToSpline(MenuCommand cmd)
  85. {
  86. SplineContainer t = (SplineContainer)cmd.context;
  87. bool asChild = EditorUtility.DisplayDialog("Add river modeler to spline", "Add as a child object?", "Yes", "No");
  88. RiverModeler component = null;
  89. if (asChild)
  90. {
  91. GameObject obj = CreateRiverObject();
  92. obj.transform.parent = t.transform;
  93. component = obj.GetComponent<RiverModeler>();
  94. }
  95. else
  96. {
  97. if (!t.gameObject.GetComponent<RiverModeler>())
  98. {
  99. component = t.gameObject.AddComponent<RiverModeler>();
  100. }
  101. }
  102. component.splineContainer = t;
  103. MeshFilter meshFilter = component.meshFilter;
  104. if (!meshFilter)
  105. {
  106. component.meshFilter = component.gameObject.AddComponent<MeshFilter>();
  107. MeshRenderer r =component.gameObject.AddComponent<MeshRenderer>();
  108. r.sharedMaterial = GetDefaultMaterial();
  109. }
  110. EditorUtility.SetDirty(t);
  111. }
  112. #endif
  113. [MenuItem("GameObject/Audio/River Audio", false, 1000)]
  114. public static GameObject CreateAudioZone()
  115. {
  116. GameObject gameObject = new GameObject(GameObjectUtility.GetUniqueNameForSibling(null, "River Audio"));
  117. #if UNITY_EDITOR
  118. Undo.RegisterCreatedObjectUndo(gameObject, "Created River Audio object");
  119. #endif
  120. AudioZone component = gameObject.AddComponent<AudioZone>();
  121. #if UNITY_2022_1_OR_NEWER
  122. component.river = Object.FindFirstObjectByType<RiverModeler>();
  123. #else
  124. component.river = Object.FindObjectOfType<RiverModeler>();
  125. #endif
  126. EditorUtilities.OnPostGameObjectCreation(gameObject);
  127. return gameObject;
  128. }
  129. [MenuItem("GameObject/Effects/River Cascade VFX", false, 1000)]
  130. public static void CreateCascadeVFX()
  131. {
  132. GameObject gameObject = EditorUtilities.InstantiatePrefab(CASCADE_VFX_PREFAB_GUID, "Cascade VFX");
  133. if (gameObject == null) return;
  134. #if UNITY_EDITOR
  135. Undo.RegisterCreatedObjectUndo(gameObject, "River Cascade VFX");
  136. #endif
  137. EditorUtilities.OnPostGameObjectCreation(gameObject);
  138. }
  139. [MenuItem("GameObject/Effects/River Splash VFX", false, 1000)]
  140. public static void CreateSplashVFX()
  141. {
  142. GameObject gameObject = EditorUtilities.InstantiatePrefab(SPLASH_VFX_PREFAB_GUID, "Splash VFX");
  143. if (gameObject == null) return;
  144. #if UNITY_EDITOR
  145. Undo.RegisterCreatedObjectUndo(gameObject, "River Splash VFX");
  146. #endif
  147. EditorUtilities.OnPostGameObjectCreation(gameObject);
  148. }
  149. [MenuItem("CONTEXT/RiverModeler/Generate Lightmap UV")]
  150. public static void GenerateLightmapUV(MenuCommand cmd)
  151. {
  152. RiverModeler instance = (RiverModeler)cmd.context;
  153. MeshFilter mf = instance.meshFilter;
  154. if (!mf) return;
  155. Mesh mesh = mf.sharedMesh;
  156. if(mesh == null) return;
  157. UnwrapParam.SetDefaults(out var unwrapSettings);
  158. #if UNITY_2022_1_OR_NEWER
  159. if (Unwrapping.GenerateSecondaryUVSet(mesh, unwrapSettings) == false)
  160. {
  161. throw new Exception($"Lightmap UV generation for mesh \"{mesh.name}\" failed.");
  162. }
  163. #else
  164. Unwrapping.GenerateSecondaryUVSet(mesh, unwrapSettings);
  165. #endif
  166. }
  167. public static int RebuildAllInstances()
  168. {
  169. #if UNITY_6000_0_OR_NEWER
  170. RiverModeler[] rivers = Object.FindObjectsByType<RiverModeler>(FindObjectsSortMode.None);
  171. #else
  172. RiverModeler[] rivers = Object.FindObjectsOfType<RiverModeler>();
  173. #endif
  174. int count = 0;
  175. for (int i = 0; i < rivers.Length; i++)
  176. {
  177. count++;
  178. rivers[i].Rebuild();
  179. }
  180. return count;
  181. }
  182. }
  183. }