using UnityEngine;
using System;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine.Serialization;
namespace Unity.Cinemachine.Editor
{
///
/// User-definable named presets for physical lenses. This is a Singleton asset, available in editor only
///
[Serializable]
public sealed class CinemachinePhysicalLensPalette : ScriptableObject
{
static CinemachinePhysicalLensPalette s_Instance = null;
static bool s_AlreadySearched = false;
/// Get the singleton instance of this object, or null if it doesn't exist
public static CinemachinePhysicalLensPalette InstanceIfExists
{
get
{
if (!s_AlreadySearched)
{
s_AlreadySearched = true;
var guids = AssetDatabase.FindAssets("t:CinemachinePhysicalLensPalette");
for (int i = 0; i < guids.Length && s_Instance == null; ++i)
s_Instance = AssetDatabase.LoadAssetAtPath(
AssetDatabase.GUIDToAssetPath(guids[i]));
}
return s_Instance;
}
}
/// Get the singleton instance of this object. Creates asset if nonexistent
public static CinemachinePhysicalLensPalette Instance
{
get
{
if (InstanceIfExists == null)
{
var newAssetPath = EditorUtility.SaveFilePanelInProject(
"Create Physical Lens Palette asset", "CinemachinePhysicalLensPalette", "asset",
"This editor-only file will contain the physical lens presets for this project");
if (!string.IsNullOrEmpty(newAssetPath))
{
s_Instance = CreateInstance();
// Create some sample presets
var defaultPhysical = LensSettings.Default.PhysicalProperties;
s_Instance.Presets = new()
{
new () { Name = "21mm", FocalLength = 21f, PhysicalProperties = defaultPhysical },
new () { Name = "35mm", FocalLength = 35f, PhysicalProperties = defaultPhysical },
new () { Name = "58mm", FocalLength = 58f, PhysicalProperties = defaultPhysical },
new () { Name = "80mm", FocalLength = 80f, PhysicalProperties = defaultPhysical },
new () { Name = "125mm", FocalLength = 125f, PhysicalProperties = defaultPhysical }
};
AssetDatabase.CreateAsset(s_Instance, newAssetPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
return s_Instance;
}
}
/// Physical Lens Preset
[Serializable]
public struct Preset
{
/// The name of the preset
[Tooltip("Lens Name")]
[FormerlySerializedAs("m_Name")]
public string Name;
/// This is the camera focal length in mm
[Tooltip("This is the camera focal length in mm")]
[FormerlySerializedAs("m_FocalLength")]
public float FocalLength;
/// Physical lens settings
[Tooltip("Physical lens settings")]
public LensSettings.PhysicalSettings PhysicalProperties;
}
/// The array containing Preset definitions, for physical cameras
[Tooltip("The array containing Preset definitions, for physical cameras")]
public List Presets = new();
/// Get the index of the first physical preset that matches the preset name
/// Name of the preset
/// the preset index, or -1 if no matching preset
public int GetPresetIndex(string presetName)
=> Presets.FindIndex(x => x.Name == presetName);
/// Get the index of the physical preset that matches the template
/// Template holding the preset tsettings. Name is ignored.
/// the preset index, or -1 if no matching preset
public int GetMatchingPreset(Preset p)
{
return Presets.FindIndex(x =>
Mathf.Approximately(x.FocalLength, p.FocalLength)
&& x.PhysicalProperties.GateFit == p.PhysicalProperties.GateFit
&& Mathf.Approximately(x.PhysicalProperties.LensShift.x, p.PhysicalProperties.LensShift.x)
&& Mathf.Approximately(x.PhysicalProperties.LensShift.y, p.PhysicalProperties.LensShift.y)
&& Mathf.Approximately(x.PhysicalProperties.SensorSize.x, p.PhysicalProperties.SensorSize.x)
&& Mathf.Approximately(x.PhysicalProperties.SensorSize.y, p.PhysicalProperties.SensorSize.y)
&& x.PhysicalProperties.Iso == p.PhysicalProperties.Iso
&& Mathf.Approximately(x.PhysicalProperties.ShutterSpeed, p.PhysicalProperties.ShutterSpeed)
&& Mathf.Approximately(x.PhysicalProperties.Aperture, p.PhysicalProperties.Aperture)
&&x.PhysicalProperties.BladeCount == p.PhysicalProperties.BladeCount
&& Mathf.Approximately(x.PhysicalProperties.Curvature.x, p.PhysicalProperties.Curvature.x)
&& Mathf.Approximately(x.PhysicalProperties.Curvature.y, p.PhysicalProperties.Curvature.y)
&& Mathf.Approximately(x.PhysicalProperties.BarrelClipping, p.PhysicalProperties.BarrelClipping)
&& Mathf.Approximately(x.PhysicalProperties.Anamorphism, p.PhysicalProperties.Anamorphism));
}
}
}