LensSettingsPropertyDrawer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Reflection;
  4. using System.Collections.Generic;
  5. using Cinemachine.Utility;
  6. using System;
  7. #if CINEMACHINE_HDRP || CINEMACHINE_LWRP_7_0_0
  8. #if CINEMACHINE_HDRP_7_0_0
  9. using UnityEngine.Rendering.HighDefinition;
  10. #else
  11. #if CINEMACHINE_LWRP_7_0_0
  12. using UnityEngine.Rendering.Universal;
  13. #else
  14. using UnityEngine.Experimental.Rendering.HDPipeline;
  15. #endif
  16. #endif
  17. #endif
  18. namespace Cinemachine.Editor
  19. {
  20. [CustomPropertyDrawer(typeof(LensSettingsPropertyAttribute))]
  21. internal sealed class LensSettingsPropertyDrawer : PropertyDrawer
  22. {
  23. const int vSpace = 2;
  24. LensSettings def = new LensSettings(); // to access name strings
  25. GUIContent FocalLengthLabel = new GUIContent("Focal Length", "The length of the lens (in mm)");
  26. #if CINEMACHINE_HDRP
  27. GUIContent PhysicalPropertiesLabel = new GUIContent("Physical Properties", "Physical properties of the lens");
  28. static bool mPhysicalExapnded;
  29. #endif
  30. public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
  31. {
  32. float height = EditorGUIUtility.singleLineHeight;
  33. rect.height = height;
  34. property.isExpanded = EditorGUI.Foldout(
  35. new Rect(rect.x, rect.y, EditorGUIUtility.labelWidth, rect.height),
  36. property.isExpanded, label, true);
  37. if (property.isExpanded)
  38. {
  39. ++EditorGUI.indentLevel;
  40. rect.y += height + vSpace;
  41. if (IsOrtho)
  42. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.OrthographicSize));
  43. else
  44. {
  45. if (IsPhysical)
  46. DrawFocalLengthControl(rect, property);
  47. else
  48. DrawFOVControl(rect, property);
  49. }
  50. rect.y += height + vSpace;
  51. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.NearClipPlane));
  52. rect.y += height + vSpace;
  53. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.FarClipPlane));
  54. if (IsPhysical)
  55. {
  56. #if CINEMACHINE_HDRP
  57. rect.y += height + vSpace;
  58. mPhysicalExapnded = EditorGUI.Foldout(
  59. new Rect(rect.x, rect.y, EditorGUIUtility.labelWidth, rect.height),
  60. mPhysicalExapnded, PhysicalPropertiesLabel, true);
  61. if (mPhysicalExapnded)
  62. {
  63. ++EditorGUI.indentLevel;
  64. rect.y += height + vSpace;
  65. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.Aperture));
  66. rect.y += height + vSpace;
  67. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.Iso));
  68. rect.y += height + vSpace;
  69. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.ShutterSpeed));
  70. rect.y += height + vSpace;
  71. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.BladeCount));
  72. rect.y += height + vSpace;
  73. var curvature = property.FindPropertyRelative(() => def.Curvature);
  74. using (var propertyScope = new EditorGUI.PropertyScope(rect, new GUIContent("Curvature"), curvature))
  75. {
  76. var v = curvature.vector2Value;
  77. // The layout system breaks alignment when mixing inspector fields with custom layout'd
  78. // fields as soon as a scrollbar is needed in the inspector, so we'll do the layout
  79. // manually instead
  80. const int kFloatFieldWidth = 50;
  81. const int kSeparatorWidth = 5;
  82. float indentOffset = EditorGUI.indentLevel * 15f;
  83. var labelRect = new Rect(rect.x, rect.y, EditorGUIUtility.labelWidth - indentOffset, rect.height);
  84. var floatFieldLeft = new Rect(labelRect.xMax, rect.y, kFloatFieldWidth + indentOffset, rect.height);
  85. var sliderRect = new Rect(floatFieldLeft.xMax + kSeparatorWidth - indentOffset, rect.y, rect.width - labelRect.width - kFloatFieldWidth * 2 - kSeparatorWidth * 2, rect.height);
  86. var floatFieldRight = new Rect(sliderRect.xMax + kSeparatorWidth - indentOffset, rect.y, kFloatFieldWidth + indentOffset, rect.height);
  87. EditorGUI.PrefixLabel(labelRect, propertyScope.content);
  88. v.x = EditorGUI.FloatField(floatFieldLeft, v.x);
  89. EditorGUI.MinMaxSlider(sliderRect, ref v.x, ref v.y, HDPhysicalCamera.kMinAperture, HDPhysicalCamera.kMaxAperture);
  90. v.y = EditorGUI.FloatField(floatFieldRight, v.y);
  91. curvature.vector2Value = v;
  92. }
  93. rect.y += height + vSpace;
  94. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.BarrelClipping));
  95. rect.y += height + vSpace;
  96. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.Anamorphism));
  97. rect.y += height + vSpace;
  98. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.LensShift));
  99. --EditorGUI.indentLevel;
  100. }
  101. #else
  102. rect.y += height + vSpace;
  103. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.LensShift));
  104. #endif
  105. }
  106. rect.y += height + vSpace;
  107. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.Dutch));
  108. --EditorGUI.indentLevel;
  109. }
  110. }
  111. static float ExtraSpaceHackWTF() { return EditorGUIUtility.singleLineHeight - 2; }
  112. void DrawFOVControl(Rect rect, SerializedProperty property)
  113. {
  114. var FOVProperty = property.FindPropertyRelative(() => def.FieldOfView);
  115. float dropdownWidth = (rect.width - EditorGUIUtility.labelWidth) / 3;
  116. rect.width -= dropdownWidth;
  117. EditorGUI.PropertyField(rect, FOVProperty);
  118. rect.x += rect.width; rect.width = dropdownWidth;
  119. CinemachineLensPresets presets = CinemachineLensPresets.InstanceIfExists;
  120. int preset = (presets == null) ? -1 : presets.GetMatchingPreset(FOVProperty.floatValue);
  121. rect.x -= ExtraSpaceHackWTF(); rect.width += ExtraSpaceHackWTF();
  122. int selection = EditorGUI.Popup(rect, GUIContent.none, preset, m_PresetOptions);
  123. if (selection == m_PresetOptions.Length-1 && CinemachineLensPresets.Instance != null)
  124. Selection.activeObject = presets = CinemachineLensPresets.Instance;
  125. else if (selection >= 0 && selection < m_PresetOptions.Length-1)
  126. {
  127. FOVProperty.floatValue = presets.m_Presets[selection].m_FieldOfView;
  128. property.serializedObject.ApplyModifiedProperties();
  129. }
  130. }
  131. void DrawFocalLengthControl(Rect rect, SerializedProperty property)
  132. {
  133. var FOVProperty = property.FindPropertyRelative(() => def.FieldOfView);
  134. float dropdownWidth = (rect.width - EditorGUIUtility.labelWidth) / 3;
  135. rect.width -= dropdownWidth;
  136. float f = VerticalFOVToFocalLength(FOVProperty.floatValue);
  137. EditorGUI.BeginProperty(rect, FocalLengthLabel, FOVProperty);
  138. f = EditorGUI.FloatField(rect, FocalLengthLabel, f);
  139. f = FocalLengthToVerticalFOV(f);
  140. if (!Mathf.Approximately(FOVProperty.floatValue, f))
  141. FOVProperty.floatValue = Mathf.Clamp(f, 1, 179);
  142. EditorGUI.EndProperty();
  143. rect.x += rect.width; rect.width = dropdownWidth;
  144. #if CINEMACHINE_HDRP
  145. CinemachineLensPresets presets = CinemachineLensPresets.InstanceIfExists;
  146. int preset = -1;
  147. if (presets != null)
  148. {
  149. var focalLength = VerticalFOVToFocalLength(FOVProperty.floatValue);
  150. var aperture = property.FindPropertyRelative(() => def.Aperture).floatValue;
  151. var iso = property.FindPropertyRelative(() => def.Iso).intValue;
  152. var shutterSpeed = property.FindPropertyRelative(() => def.ShutterSpeed).floatValue;
  153. var bladeCount = property.FindPropertyRelative(() => def.BladeCount).intValue;
  154. var curvature = property.FindPropertyRelative(() => def.Curvature).vector2Value;
  155. var barrelClipping = property.FindPropertyRelative(() => def.BarrelClipping).floatValue;
  156. var anamprphism = property.FindPropertyRelative(() => def.Anamorphism).floatValue;
  157. var lensShift = property.FindPropertyRelative(() => def.LensShift).vector2Value;
  158. preset = presets.GetMatchingPhysicalPreset(
  159. focalLength, iso, shutterSpeed, aperture, bladeCount,
  160. curvature, barrelClipping, anamprphism, lensShift);
  161. }
  162. rect.x -= ExtraSpaceHackWTF(); rect.width += ExtraSpaceHackWTF();
  163. int selection = EditorGUI.Popup(rect, GUIContent.none, preset, m_PhysicalPresetOptions);
  164. if (selection == m_PhysicalPresetOptions.Length-1 && CinemachineLensPresets.Instance != null)
  165. Selection.activeObject = presets = CinemachineLensPresets.Instance;
  166. else if (selection >= 0 && selection < m_PhysicalPresetOptions.Length-1)
  167. {
  168. var v = presets.m_PhysicalPresets[selection];
  169. FOVProperty.floatValue = FocalLengthToVerticalFOV(v.m_FocalLength);
  170. property.FindPropertyRelative(() => def.Aperture).floatValue = v.Aperture;
  171. property.FindPropertyRelative(() => def.Iso).intValue = v.Iso;
  172. property.FindPropertyRelative(() => def.ShutterSpeed).floatValue = v.ShutterSpeed;
  173. property.FindPropertyRelative(() => def.BladeCount).intValue = v.BladeCount;
  174. property.FindPropertyRelative(() => def.Curvature).vector2Value = v.Curvature;
  175. property.FindPropertyRelative(() => def.BarrelClipping).floatValue = v.BarrelClipping;
  176. property.FindPropertyRelative(() => def.Anamorphism).floatValue = v.Anamorphism;
  177. property.FindPropertyRelative(() => def.LensShift).vector2Value = v.LensShift;
  178. property.serializedObject.ApplyModifiedProperties();
  179. }
  180. #else
  181. CinemachineLensPresets presets = CinemachineLensPresets.InstanceIfExists;
  182. int preset = (presets == null) ? -1 : presets.GetMatchingPhysicalPreset(VerticalFOVToFocalLength(FOVProperty.floatValue));
  183. rect.x -= ExtraSpaceHackWTF(); rect.width += ExtraSpaceHackWTF();
  184. int selection = EditorGUI.Popup(rect, GUIContent.none, preset, m_PhysicalPresetOptions);
  185. if (selection == m_PhysicalPresetOptions.Length-1 && CinemachineLensPresets.Instance != null)
  186. Selection.activeObject = presets = CinemachineLensPresets.Instance;
  187. else if (selection >= 0 && selection < m_PhysicalPresetOptions.Length-1)
  188. {
  189. FOVProperty.floatValue = FocalLengthToVerticalFOV(
  190. presets.m_PhysicalPresets[selection].m_FocalLength);
  191. property.serializedObject.ApplyModifiedProperties();
  192. }
  193. #endif
  194. }
  195. float VerticalFOVToFocalLength(float fov)
  196. {
  197. return SensorSize.y * 0.5f / Mathf.Tan(Mathf.Deg2Rad * fov * 0.5f);
  198. }
  199. float FocalLengthToVerticalFOV(float focalLength)
  200. {
  201. if (focalLength < UnityVectorExtensions.Epsilon)
  202. return 180f;
  203. return Mathf.Rad2Deg * 2.0f * Mathf.Atan(SensorSize.y * 0.5f / focalLength);
  204. }
  205. bool IsOrtho { get; set; }
  206. bool IsPhysical { get; set; }
  207. Vector2 SensorSize { get; set; }
  208. GUIContent[] m_PresetOptions = new GUIContent[0];
  209. GUIContent[] m_PhysicalPresetOptions = new GUIContent[0];
  210. void CacheABunchOfStuff(SerializedProperty property)
  211. {
  212. object lens = SerializedPropertyHelper.GetPropertyValue(property);
  213. IsOrtho = AccessProperty<bool>(typeof(LensSettings), lens, "Orthographic");
  214. IsPhysical = AccessProperty<bool>(typeof(LensSettings), lens, "IsPhysicalCamera");
  215. SensorSize = AccessProperty<Vector2>(typeof(LensSettings), lens, "SensorSize");
  216. List<GUIContent> options = new List<GUIContent>();
  217. CinemachineLensPresets presets = CinemachineLensPresets.InstanceIfExists;
  218. if (presets != null)
  219. for (int i = 0; i < presets.m_Presets.Length; ++i)
  220. options.Add(new GUIContent(presets.m_Presets[i].m_Name));
  221. options.Add(new GUIContent("Edit Presets..."));
  222. m_PresetOptions = options.ToArray();
  223. options.Clear();
  224. if (presets != null)
  225. for (int i = 0; i < presets.m_PhysicalPresets.Length; ++i)
  226. options.Add(new GUIContent(presets.m_PhysicalPresets[i].m_Name));
  227. options.Add(new GUIContent("Edit Presets..."));
  228. m_PhysicalPresetOptions = options.ToArray();
  229. }
  230. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  231. {
  232. // Cache it here because it's called less often - less garbage
  233. CacheABunchOfStuff(property);
  234. float height = EditorGUIUtility.singleLineHeight + vSpace;
  235. if (property.isExpanded)
  236. {
  237. if (!IsPhysical)
  238. height *= 5;
  239. else
  240. {
  241. #if CINEMACHINE_HDRP
  242. height *= 5 + (mPhysicalExapnded ? 9 : 1);
  243. #else
  244. height *= 5 + 1;
  245. #endif
  246. }
  247. }
  248. return height - vSpace;
  249. }
  250. static T AccessProperty<T>(Type type, object obj, string memberName)
  251. {
  252. if (string.IsNullOrEmpty(memberName) || (type == null))
  253. return default(T);
  254. System.Reflection.BindingFlags bindingFlags = System.Reflection.BindingFlags.Public;
  255. if (obj != null)
  256. bindingFlags |= System.Reflection.BindingFlags.Instance;
  257. else
  258. bindingFlags |= System.Reflection.BindingFlags.Static;
  259. PropertyInfo pi = type.GetProperty(memberName, bindingFlags);
  260. if ((pi != null) && (pi.PropertyType == typeof(T)))
  261. return (T)pi.GetValue(obj, null);
  262. else
  263. return default(T);
  264. }
  265. }
  266. }