CinemachineNoiseSettingsEditor.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEditorInternal;
  4. using System.Collections.Generic;
  5. namespace Cinemachine.Editor
  6. {
  7. [CustomEditor(typeof(NoiseSettings))]
  8. internal sealed class NoiseSettingsEditor : BaseEditor<NoiseSettings>
  9. {
  10. private const float vSpace = 2;
  11. private const float hSpace = 3;
  12. // Needed for accessing string names of fields
  13. #pragma warning disable 0649 // assigned but never used
  14. private NoiseSettings.TransformNoiseParams tpDef;
  15. private NoiseSettings.NoiseParams npDef;
  16. private static float mPreviewTime = 2;
  17. private static float mPreviewHeight = 5;
  18. private float mNoiseOffsetBase = 0;
  19. private float mNoiseOffset = 0;
  20. private bool mAnimatedPreview = false;
  21. GUIContent mAnimatedLabel = new GUIContent("Animated", "Animate the noise signal preview");
  22. private ReorderableList[] mPosChannels;
  23. private ReorderableList[] mRotChannels;
  24. private static GUIContent[] mPoslabels = new GUIContent[]
  25. {
  26. new GUIContent("Position X"),
  27. new GUIContent("Position Y"),
  28. new GUIContent("Position Z")
  29. };
  30. private static GUIContent[] mRotlabels = new GUIContent[]
  31. {
  32. new GUIContent("Rotation X"),
  33. new GUIContent("Rotation Y"),
  34. new GUIContent("Rotation Z")
  35. };
  36. private static bool[] mPosExpanded = new bool[3];
  37. private static bool[] mRotExpanded = new bool[3];
  38. private void OnEnable()
  39. {
  40. mNoiseOffsetBase = Time.realtimeSinceStartup;
  41. mNoiseOffset = 0;
  42. }
  43. /// <summary>Get the property names to exclude in the inspector.</summary>
  44. /// <param name="excluded">Add the names to this list</param>
  45. protected override void GetExcludedPropertiesInInspector(List<string> excluded)
  46. {
  47. base.GetExcludedPropertiesInInspector(excluded);
  48. excluded.Add(FieldPath(x => Target.PositionNoise));
  49. excluded.Add(FieldPath(x => Target.OrientationNoise));
  50. }
  51. public override void OnInspectorGUI()
  52. {
  53. if (mPosChannels == null)
  54. mPosChannels = SetupReorderableLists(
  55. serializedObject.FindProperty(() => Target.PositionNoise), mPoslabels);
  56. if (mRotChannels == null)
  57. mRotChannels = SetupReorderableLists(
  58. serializedObject.FindProperty(() => Target.OrientationNoise), mRotlabels);
  59. BeginInspector();
  60. Rect r = EditorGUILayout.GetControlRect();
  61. mPreviewTime = EditorGUI.Slider(r, "Preview Time", mPreviewTime, 0.01f, 10f);
  62. r = EditorGUILayout.GetControlRect();
  63. float labelWidth = GUI.skin.label.CalcSize(mAnimatedLabel).x + EditorGUIUtility.singleLineHeight;
  64. r.width -= labelWidth + hSpace;
  65. mPreviewHeight = EditorGUI.Slider(r, "Preview Height", mPreviewHeight, 1f, 10f);
  66. r.x += r.width + hSpace; r.width = labelWidth;
  67. mAnimatedPreview = EditorGUI.ToggleLeft(r, mAnimatedLabel, mAnimatedPreview);
  68. EditorGUILayout.Separator();
  69. r = EditorGUILayout.GetControlRect();
  70. EditorGUI.LabelField(r, "Position Noise - amplitudes are in Distance units", EditorStyles.boldLabel);
  71. r = EditorGUILayout.GetControlRect(true, mPreviewHeight * EditorGUIUtility.singleLineHeight);
  72. if (Event.current.type == EventType.Repaint)
  73. {
  74. mSampleCachePos.SnapshotSample(r.size, Target.PositionNoise, mNoiseOffset, mAnimatedPreview);
  75. mSampleCachePos.DrawSamplePreview(r, 7);
  76. }
  77. for (int i = 0; i < mPosChannels.Length; ++i)
  78. {
  79. r = EditorGUILayout.GetControlRect();
  80. mPosExpanded[i] = EditorGUI.Foldout(r, mPosExpanded[i], mPoslabels[i], true);
  81. if (mPosExpanded[i])
  82. {
  83. r = EditorGUILayout.GetControlRect(true, mPreviewHeight * EditorGUIUtility.singleLineHeight);
  84. if (Event.current.type == EventType.Repaint)
  85. mSampleCachePos.DrawSamplePreview(r, 1 << i);
  86. mPosChannels[i].DoLayoutList();
  87. }
  88. }
  89. EditorGUILayout.Separator();
  90. r = EditorGUILayout.GetControlRect();
  91. EditorGUI.LabelField(r, "Rotation Noise - amplitude units are degrees", EditorStyles.boldLabel);
  92. r = EditorGUILayout.GetControlRect(true, mPreviewHeight * EditorGUIUtility.singleLineHeight);
  93. if (Event.current.type == EventType.Repaint)
  94. {
  95. mSampleCacheRot.SnapshotSample(r.size, Target.OrientationNoise, mNoiseOffset, mAnimatedPreview);
  96. mSampleCacheRot.DrawSamplePreview(r, 7);
  97. }
  98. for (int i = 0; i < mPosChannels.Length; ++i)
  99. {
  100. r = EditorGUILayout.GetControlRect();
  101. mRotExpanded[i] = EditorGUI.Foldout(r, mRotExpanded[i], mRotlabels[i], true);
  102. if (mRotExpanded[i])
  103. {
  104. r = EditorGUILayout.GetControlRect(true, mPreviewHeight * EditorGUIUtility.singleLineHeight);
  105. if (Event.current.type == EventType.Repaint)
  106. mSampleCacheRot.DrawSamplePreview(r, 1 << i);
  107. mRotChannels[i].DoLayoutList();
  108. }
  109. }
  110. serializedObject.ApplyModifiedProperties();
  111. // Make it live!
  112. if (mAnimatedPreview && Event.current.type == EventType.Repaint)
  113. {
  114. mNoiseOffset += Time.realtimeSinceStartup - mNoiseOffsetBase;
  115. Repaint();
  116. }
  117. mNoiseOffsetBase = Time.realtimeSinceStartup;
  118. }
  119. class SampleCache
  120. {
  121. private List<Vector3> mSampleCurveX = new List<Vector3>();
  122. private List<Vector3> mSampleCurveY = new List<Vector3>();
  123. private List<Vector3> mSampleCurveZ = new List<Vector3>();
  124. private List<Vector3> mSampleNoise = new List<Vector3>();
  125. public void SnapshotSample(
  126. Vector2 areaSize, NoiseSettings.TransformNoiseParams[] signal, float noiseOffset, bool animated)
  127. {
  128. // These values give a smoother curve, more-or-less fitting in the window
  129. int numSamples = Mathf.RoundToInt(areaSize.x);
  130. if (animated)
  131. numSamples *= 2;
  132. const float signalScale = 0.75f;
  133. float maxVal = 0;
  134. for (int i = 0; i < signal.Length; ++i)
  135. {
  136. maxVal = Mathf.Max(maxVal, Mathf.Abs(signal[i].X.Amplitude * signalScale));
  137. maxVal = Mathf.Max(maxVal, Mathf.Abs(signal[i].Y.Amplitude * signalScale));
  138. maxVal = Mathf.Max(maxVal, Mathf.Abs(signal[i].Z.Amplitude * signalScale));
  139. }
  140. mSampleNoise.Clear();
  141. for (int i = 0; i < numSamples; ++i)
  142. {
  143. float t = (float)i / (numSamples - 1) * mPreviewTime + noiseOffset;
  144. Vector3 p = NoiseSettings.GetCombinedFilterResults(signal, t, Vector3.zero);
  145. mSampleNoise.Add(p);
  146. }
  147. mSampleCurveX.Clear();
  148. mSampleCurveY.Clear();
  149. mSampleCurveZ.Clear();
  150. float halfHeight = areaSize.y / 2;
  151. float yOffset = halfHeight;
  152. for (int i = 0; i < numSamples; ++i)
  153. {
  154. float t = (float)i / (numSamples - 1);
  155. Vector3 p = mSampleNoise[i];
  156. mSampleCurveX.Add(new Vector3(areaSize.x * t, halfHeight * Mathf.Clamp(-p.x / maxVal, -1, 1) + yOffset, 0));
  157. mSampleCurveY.Add(new Vector3(areaSize.x * t, halfHeight * Mathf.Clamp(-p.y / maxVal, -1, 1) + yOffset, 0));
  158. mSampleCurveZ.Add(new Vector3(areaSize.x * t, halfHeight * Mathf.Clamp(-p.z / maxVal, -1, 1) + yOffset, 0));
  159. }
  160. }
  161. public void DrawSamplePreview(Rect r, int channelMask)
  162. {
  163. EditorGUI.DrawRect(r, Color.black);
  164. var oldMatrix = Handles.matrix;
  165. Handles.matrix = Handles.matrix * Matrix4x4.Translate(r.position);
  166. if ((channelMask & 1) != 0)
  167. {
  168. Handles.color = new Color(1, 0.5f, 0, 0.8f);
  169. Handles.DrawPolyLine(mSampleCurveX.ToArray());
  170. }
  171. if ((channelMask & 2) != 0)
  172. {
  173. Handles.color = new Color(0, 1, 0, 0.8f);
  174. Handles.DrawPolyLine(mSampleCurveY.ToArray());
  175. }
  176. if ((channelMask & 4) != 0)
  177. {
  178. Handles.color = new Color(0, 0.5f, 1, 0.8f);
  179. Handles.DrawPolyLine(mSampleCurveZ.ToArray());
  180. }
  181. Handles.color = Color.black;
  182. Handles.DrawLine(new Vector3(1, 0, 0), new Vector3(r.width, 0, 0));
  183. Handles.DrawLine(new Vector3(0, r.height, 0), new Vector3(r.width, r.height, 0));
  184. Handles.matrix = oldMatrix;
  185. }
  186. }
  187. SampleCache mSampleCachePos = new SampleCache();
  188. SampleCache mSampleCacheRot = new SampleCache();
  189. private ReorderableList[] SetupReorderableLists(
  190. SerializedProperty property, GUIContent[] titles)
  191. {
  192. ReorderableList[] lists = new ReorderableList[3];
  193. for (int i = 0; i < 3; ++i)
  194. lists[i] = SetupReorderableList(property, i, new GUIContent("Components"));
  195. return lists;
  196. }
  197. private ReorderableList SetupReorderableList(
  198. SerializedProperty property, int channel, GUIContent title)
  199. {
  200. ChannelList list = new ChannelList(
  201. property.serializedObject, property, channel, title);
  202. list.drawHeaderCallback = (Rect rect) =>
  203. {
  204. GUIContent steadyLabel = new GUIContent("(non-random wave if checked)");
  205. float steadyLabelWidth = GUI.skin.label.CalcSize(steadyLabel).x;
  206. Rect r = rect;
  207. EditorGUI.LabelField(r, list.mTitle);
  208. r.x = rect.x + rect.width - steadyLabelWidth; r.width = steadyLabelWidth;
  209. EditorGUI.LabelField(r, steadyLabel);
  210. };
  211. list.drawElementCallback
  212. = (Rect rect, int index, bool isActive, bool isFocused) =>
  213. {
  214. SerializedProperty element = list.serializedProperty.GetArrayElementAtIndex(index);
  215. switch (list.mChannel)
  216. {
  217. case 0: DrawNoiseChannel(rect, element.FindPropertyRelative(() => tpDef.X)); break;
  218. case 1: DrawNoiseChannel(rect, element.FindPropertyRelative(() => tpDef.Y)); break;
  219. case 2: DrawNoiseChannel(rect, element.FindPropertyRelative(() => tpDef.Z)); break;
  220. default: break;
  221. }
  222. };
  223. list.onAddCallback = (ReorderableList l) =>
  224. {
  225. var index = l.serializedProperty.arraySize;
  226. ++l.serializedProperty.arraySize;
  227. SerializedProperty p = l.serializedProperty.GetArrayElementAtIndex(index);
  228. ClearComponent(p.FindPropertyRelative(() => tpDef.X));
  229. ClearComponent(p.FindPropertyRelative(() => tpDef.Y));
  230. ClearComponent(p.FindPropertyRelative(() => tpDef.Z));
  231. };
  232. list.onRemoveCallback = (ReorderableList l) =>
  233. {
  234. // Can't just delete because the component arrays are connected
  235. SerializedProperty p = l.serializedProperty.GetArrayElementAtIndex(l.index);
  236. bool IsClear
  237. = (list.mChannel == 0 || IsClearComponent(p.FindPropertyRelative(() => tpDef.X)))
  238. && (list.mChannel == 1 || IsClearComponent(p.FindPropertyRelative(() => tpDef.Y)))
  239. && (list.mChannel == 2 || IsClearComponent(p.FindPropertyRelative(() => tpDef.Z)));
  240. if (IsClear)
  241. l.serializedProperty.DeleteArrayElementAtIndex(l.index);
  242. else switch (list.mChannel)
  243. {
  244. case 0: ClearComponent(p.FindPropertyRelative(() => tpDef.X)); break;
  245. case 1: ClearComponent(p.FindPropertyRelative(() => tpDef.Y)); break;
  246. case 2: ClearComponent(p.FindPropertyRelative(() => tpDef.Z)); break;
  247. default: break;
  248. }
  249. };
  250. return list;
  251. }
  252. class ChannelList : ReorderableList
  253. {
  254. public int mChannel;
  255. public GUIContent mTitle;
  256. public ChannelList(
  257. SerializedObject serializedObject,
  258. SerializedProperty elements,
  259. int channel, GUIContent title)
  260. : base(serializedObject, elements, true, true, true, true)
  261. {
  262. mChannel = channel;
  263. mTitle = title;
  264. }
  265. };
  266. private GUIContent steadyLabel;
  267. private GUIContent freqLabel;
  268. private float freqLabelWidth;
  269. private GUIContent ampLabel;
  270. private float ampLabelWidth;
  271. private void InitializeLabels(SerializedProperty property)
  272. {
  273. if (steadyLabel == null)
  274. {
  275. SerializedProperty p = property.FindPropertyRelative(() => npDef.Constant);
  276. steadyLabel = new GUIContent(p.displayName, p.tooltip) { text = " " };
  277. }
  278. if (freqLabel == null)
  279. {
  280. SerializedProperty p = property.FindPropertyRelative(() => npDef.Frequency);
  281. freqLabel = new GUIContent(p.displayName, p.tooltip);
  282. freqLabelWidth = GUI.skin.label.CalcSize(freqLabel).x;
  283. }
  284. if (ampLabel == null)
  285. {
  286. SerializedProperty p = property.FindPropertyRelative(() => npDef.Amplitude);
  287. ampLabel = new GUIContent(p.displayName, p.tooltip);
  288. ampLabelWidth = GUI.skin.label.CalcSize(ampLabel).x;
  289. }
  290. }
  291. private void DrawNoiseChannel(Rect rect, SerializedProperty property)
  292. {
  293. InitializeLabels(property);
  294. Rect r = rect;
  295. r.height -= vSpace;
  296. r.width -= EditorGUIUtility.singleLineHeight + hSpace;
  297. r.width /= 2;
  298. float oldLabelWidth = EditorGUIUtility.labelWidth;
  299. EditorGUIUtility.labelWidth = freqLabelWidth;
  300. EditorGUI.PropertyField(r, property.FindPropertyRelative(() => npDef.Frequency), freqLabel);
  301. r.x += r.width + hSpace;
  302. EditorGUIUtility.labelWidth = ampLabelWidth;
  303. EditorGUI.PropertyField(r, property.FindPropertyRelative(() => npDef.Amplitude), ampLabel);
  304. r.y -= 1;
  305. r.x += r.width + hSpace; r.width = EditorGUIUtility.singleLineHeight + hSpace;
  306. EditorGUIUtility.labelWidth = hSpace;
  307. EditorGUI.PropertyField(r, property.FindPropertyRelative(() => npDef.Constant), steadyLabel);
  308. EditorGUIUtility.labelWidth = oldLabelWidth;
  309. }
  310. // SerializedProperty is a NoiseSettings.NoiseParam
  311. void ClearComponent(SerializedProperty p)
  312. {
  313. p.FindPropertyRelative(() => npDef.Amplitude).floatValue = 0;
  314. p.FindPropertyRelative(() => npDef.Frequency).floatValue = 0;
  315. p.FindPropertyRelative(() => npDef.Constant).boolValue = false;
  316. }
  317. // SerializedProperty is a NoiseSettings.NoiseParam
  318. bool IsClearComponent(SerializedProperty p)
  319. {
  320. return p.FindPropertyRelative(() => npDef.Amplitude).floatValue == 0
  321. && p.FindPropertyRelative(() => npDef.Frequency).floatValue == 0;
  322. }
  323. }
  324. }