CinemachineLensPresets.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using UnityEngine;
  2. using System;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. #if CINEMACHINE_HDRP || CINEMACHINE_LWRP_7_0_0
  6. #if CINEMACHINE_HDRP_7_0_0
  7. using UnityEngine.Rendering.HighDefinition;
  8. #else
  9. #if CINEMACHINE_LWRP_7_0_0
  10. using UnityEngine.Rendering.Universal;
  11. #else
  12. using UnityEngine.Experimental.Rendering.HDPipeline;
  13. #endif
  14. #endif
  15. #endif
  16. namespace Cinemachine.Editor
  17. {
  18. /// <summary>
  19. /// User-definable named presets for lenses. This is a Singleton asset, available in editor only
  20. /// </summary>
  21. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  22. [Serializable]
  23. public sealed class CinemachineLensPresets : ScriptableObject
  24. {
  25. private static CinemachineLensPresets sInstance = null;
  26. private static bool alreadySearched = false;
  27. /// <summary>Get the singleton instance of this object, or null if it doesn't exist</summary>
  28. public static CinemachineLensPresets InstanceIfExists
  29. {
  30. get
  31. {
  32. if (!alreadySearched)
  33. {
  34. alreadySearched = true;
  35. var guids = AssetDatabase.FindAssets("t:CinemachineLensPresets");
  36. for (int i = 0; i < guids.Length && sInstance == null; ++i)
  37. sInstance = AssetDatabase.LoadAssetAtPath<CinemachineLensPresets>(
  38. AssetDatabase.GUIDToAssetPath(guids[i]));
  39. }
  40. return sInstance;
  41. }
  42. }
  43. /// <summary>Get the singleton instance of this object. Creates asset if nonexistant</summary>
  44. public static CinemachineLensPresets Instance
  45. {
  46. get
  47. {
  48. if (InstanceIfExists == null)
  49. {
  50. string newAssetPath = EditorUtility.SaveFilePanelInProject(
  51. "Create Lens Presets asset", "CinemachineLensPresets", "asset",
  52. "This editor-only file will contain the lens presets for this project");
  53. if (!string.IsNullOrEmpty(newAssetPath))
  54. {
  55. sInstance = CreateInstance<CinemachineLensPresets>();
  56. // Create some sample presets
  57. List<Preset> defaults = new List<Preset>();
  58. defaults.Add(new Preset() { m_Name = "21mm", m_FieldOfView = 60f } );
  59. defaults.Add(new Preset() { m_Name = "35mm", m_FieldOfView = 38f } );
  60. defaults.Add(new Preset() { m_Name = "58mm", m_FieldOfView = 23f } );
  61. defaults.Add(new Preset() { m_Name = "80mm", m_FieldOfView = 17f } );
  62. defaults.Add(new Preset() { m_Name = "125mm", m_FieldOfView = 10f } );
  63. sInstance.m_Presets = defaults.ToArray();
  64. AssetDatabase.CreateAsset(sInstance, newAssetPath);
  65. AssetDatabase.SaveAssets();
  66. AssetDatabase.Refresh();
  67. }
  68. }
  69. return sInstance;
  70. }
  71. }
  72. /// <summary>Lens Preset</summary>
  73. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  74. [Serializable]
  75. public struct Preset
  76. {
  77. /// <summary>
  78. /// Name of the preset
  79. /// </summary>
  80. [Tooltip("Lens Name")]
  81. public string m_Name;
  82. /// <summary>
  83. /// This is the camera view in vertical degrees. For cinematic people, a 50mm lens
  84. /// on a super-35mm sensor would equal a 19.6 degree FOV
  85. /// </summary>
  86. [Range(1f, 179f)]
  87. [Tooltip("This is the camera view in vertical degrees. For cinematic people, "
  88. + " a 50mm lens on a super-35mm sensor would equal a 19.6 degree FOV")]
  89. public float m_FieldOfView;
  90. }
  91. /// <summary>The array containing Preset definitions for nonphysical cameras</summary>
  92. [Tooltip("The array containing Preset definitions, for nonphysical cameras")]
  93. public Preset[] m_Presets = new Preset[0];
  94. /// <summary>Physical Lens Preset</summary>
  95. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  96. [Serializable]
  97. public struct PhysicalPreset
  98. {
  99. /// <summary>
  100. /// Name of the preset
  101. /// </summary>
  102. [Tooltip("Lens Name")]
  103. public string m_Name;
  104. /// <summary>
  105. /// This is the camera focal length in mm
  106. /// </summary>
  107. [Tooltip("This is the camera focal length in mm")]
  108. public float m_FocalLength;
  109. /// <summary>Position of the gate relative to the film back</summary>
  110. public Vector2 LensShift;
  111. #if CINEMACHINE_HDRP
  112. public int Iso;
  113. public float ShutterSpeed;
  114. [Range(HDPhysicalCamera.kMinAperture, HDPhysicalCamera.kMaxAperture)]
  115. public float Aperture;
  116. [Range(HDPhysicalCamera.kMinBladeCount, HDPhysicalCamera.kMaxBladeCount)]
  117. public int BladeCount;
  118. public Vector2 Curvature;
  119. [Range(0, 1)]
  120. public float BarrelClipping;
  121. [Range(-1, 1)]
  122. public float Anamorphism;
  123. #endif
  124. }
  125. /// <summary>The array containing Preset definitions, for physical cameras</summary>
  126. [Tooltip("The array containing Preset definitions, for physical cameras")]
  127. public PhysicalPreset[] m_PhysicalPresets = new PhysicalPreset[0];
  128. /// <summary>Get the index of the preset that matches the lens settings</summary>
  129. /// <param name="fov">Vertical field of view</param>
  130. /// <returns>the preset index, or -1 if no matching preset</returns>
  131. public int GetMatchingPreset(float fov)
  132. {
  133. for (int i = 0; i < m_Presets.Length; ++i)
  134. if (Mathf.Approximately(m_Presets[i].m_FieldOfView, fov))
  135. return i;
  136. return -1;
  137. }
  138. /// <summary>Get the index of the physical preset that matches the lens settings</summary>
  139. /// <param name="focalLength">Focal length to match</param>
  140. /// <returns>the preset index, or -1 if no matching preset</returns>
  141. public int GetMatchingPhysicalPreset(float focalLength)
  142. {
  143. for (int i = 0; i < m_PhysicalPresets.Length; ++i)
  144. if (Mathf.Approximately(m_PhysicalPresets[i].m_FocalLength, focalLength))
  145. return i;
  146. return -1;
  147. }
  148. #if CINEMACHINE_HDRP
  149. /// <summary>Get the index of the physical preset that matches the lens settings</summary>
  150. /// <returns>the preset index, or -1 if no matching preset</returns>
  151. public int GetMatchingPhysicalPreset(
  152. float FocalLength,
  153. int Iso,
  154. float ShutterSpeed,
  155. float Aperture,
  156. int BladeCount,
  157. Vector2 Curvature,
  158. float BarrelClipping,
  159. float Anamorphism,
  160. Vector2 LensShift)
  161. {
  162. for (int i = 0; i < m_PhysicalPresets.Length; ++i)
  163. {
  164. if (Mathf.Approximately(m_PhysicalPresets[i].m_FocalLength, FocalLength)
  165. && m_PhysicalPresets[i].Iso == Iso
  166. && Mathf.Approximately(m_PhysicalPresets[i].ShutterSpeed, ShutterSpeed)
  167. && Mathf.Approximately(m_PhysicalPresets[i].Aperture, Aperture)
  168. && m_PhysicalPresets[i].BladeCount == BladeCount
  169. && Mathf.Approximately(m_PhysicalPresets[i].Curvature.x, Curvature.x)
  170. && Mathf.Approximately(m_PhysicalPresets[i].Curvature.y, Curvature.y)
  171. && Mathf.Approximately(m_PhysicalPresets[i].BarrelClipping, BarrelClipping)
  172. && Mathf.Approximately(m_PhysicalPresets[i].Anamorphism, Anamorphism)
  173. && Mathf.Approximately(m_PhysicalPresets[i].LensShift.x, LensShift.x)
  174. && Mathf.Approximately(m_PhysicalPresets[i].LensShift.y, LensShift.y))
  175. {
  176. return i;
  177. }
  178. }
  179. return -1;
  180. }
  181. #endif
  182. }
  183. }