using UnityEngine; using System; using UnityEditor; using System.Collections.Generic; #if CINEMACHINE_HDRP || CINEMACHINE_URP #if CINEMACHINE_HDRP_7_3_1 using UnityEngine.Rendering.HighDefinition; #else #if CINEMACHINE_URP using UnityEngine.Rendering.Universal; #else using UnityEngine.Experimental.Rendering.HDPipeline; #endif #endif #endif namespace Cinemachine.Editor { /// /// User-definable named presets for lenses. This is a Singleton asset, available in editor only /// [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)] [Serializable] public sealed class CinemachineLensPresets : ScriptableObject { private static CinemachineLensPresets sInstance = null; private static bool alreadySearched = false; /// Get the singleton instance of this object, or null if it doesn't exist public static CinemachineLensPresets InstanceIfExists { get { if (!alreadySearched) { alreadySearched = true; var guids = AssetDatabase.FindAssets("t:CinemachineLensPresets"); for (int i = 0; i < guids.Length && sInstance == null; ++i) sInstance = AssetDatabase.LoadAssetAtPath( AssetDatabase.GUIDToAssetPath(guids[i])); } return sInstance; } } /// Get the singleton instance of this object. Creates asset if nonexistant public static CinemachineLensPresets Instance { get { if (InstanceIfExists == null) { string newAssetPath = EditorUtility.SaveFilePanelInProject( "Create Lens Presets asset", "CinemachineLensPresets", "asset", "This editor-only file will contain the lens presets for this project"); if (!string.IsNullOrEmpty(newAssetPath)) { sInstance = CreateInstance(); // Create some sample presets List defaults = new List(); defaults.Add(new Preset() { m_Name = "21mm", m_FieldOfView = 60f } ); defaults.Add(new Preset() { m_Name = "35mm", m_FieldOfView = 38f } ); defaults.Add(new Preset() { m_Name = "58mm", m_FieldOfView = 23f } ); defaults.Add(new Preset() { m_Name = "80mm", m_FieldOfView = 17f } ); defaults.Add(new Preset() { m_Name = "125mm", m_FieldOfView = 10f } ); sInstance.m_Presets = defaults.ToArray(); AssetDatabase.CreateAsset(sInstance, newAssetPath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } return sInstance; } } /// Lens Preset [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)] [Serializable] public struct Preset { /// /// Name of the preset /// [Tooltip("Lens Name")] public string m_Name; /// /// This is the camera view in vertical degrees. For cinematic people, a 50mm lens /// on a super-35mm sensor would equal a 19.6 degree FOV /// [Range(1f, 179f)] [Tooltip("This is the camera view in vertical degrees. For cinematic people, " + " a 50mm lens on a super-35mm sensor would equal a 19.6 degree FOV")] public float m_FieldOfView; } /// The array containing Preset definitions for nonphysical cameras [Tooltip("The array containing Preset definitions, for nonphysical cameras")] public Preset[] m_Presets = new Preset[0]; /// Physical Lens Preset [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)] [Serializable] public struct PhysicalPreset { /// /// Name of the preset /// [Tooltip("Lens Name")] public string m_Name; /// /// This is the camera focal length in mm /// [Tooltip("This is the camera focal length in mm")] public float m_FocalLength; /// Position of the gate relative to the film back public Vector2 LensShift; #if CINEMACHINE_HDRP public int Iso; public float ShutterSpeed; [Range(HDPhysicalCamera.kMinAperture, HDPhysicalCamera.kMaxAperture)] public float Aperture; [Range(HDPhysicalCamera.kMinBladeCount, HDPhysicalCamera.kMaxBladeCount)] public int BladeCount; public Vector2 Curvature; [Range(0, 1)] public float BarrelClipping; [Range(-1, 1)] public float Anamorphism; #endif } /// The array containing Preset definitions, for physical cameras [Tooltip("The array containing Preset definitions, for physical cameras")] public PhysicalPreset[] m_PhysicalPresets = new PhysicalPreset[0]; /// Get the index of the preset that matches the lens settings /// Vertical field of view /// the preset index, or -1 if no matching preset public int GetMatchingPreset(float verticalFOV) { for (int i = 0; i < m_Presets.Length; ++i) if (Mathf.Approximately(m_Presets[i].m_FieldOfView, verticalFOV)) return i; return -1; } /// Get the index of the physical preset that matches the lens settings /// Focal length to match /// the preset index, or -1 if no matching preset public int GetMatchingPhysicalPreset(float focalLength) { for (int i = 0; i < m_PhysicalPresets.Length; ++i) if (Mathf.Approximately(m_PhysicalPresets[i].m_FocalLength, focalLength)) return i; return -1; } #if CINEMACHINE_HDRP /// Get the index of the physical preset that matches the lens settings /// the preset index, or -1 if no matching preset public int GetMatchingPhysicalPreset( float FocalLength, int Iso, float ShutterSpeed, float Aperture, int BladeCount, Vector2 Curvature, float BarrelClipping, float Anamorphism, Vector2 LensShift) { for (int i = 0; i < m_PhysicalPresets.Length; ++i) { if (Mathf.Approximately(m_PhysicalPresets[i].m_FocalLength, FocalLength) && m_PhysicalPresets[i].Iso == Iso && Mathf.Approximately(m_PhysicalPresets[i].ShutterSpeed, ShutterSpeed) && Mathf.Approximately(m_PhysicalPresets[i].Aperture, Aperture) && m_PhysicalPresets[i].BladeCount == BladeCount && Mathf.Approximately(m_PhysicalPresets[i].Curvature.x, Curvature.x) && Mathf.Approximately(m_PhysicalPresets[i].Curvature.y, Curvature.y) && Mathf.Approximately(m_PhysicalPresets[i].BarrelClipping, BarrelClipping) && Mathf.Approximately(m_PhysicalPresets[i].Anamorphism, Anamorphism) && Mathf.Approximately(m_PhysicalPresets[i].LensShift.x, LensShift.x) && Mathf.Approximately(m_PhysicalPresets[i].LensShift.y, LensShift.y)) { return i; } } return -1; } #endif } }