123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- using UnityEngine.Rendering;
- using UnityEngine.Scripting.APIUpdating;
- namespace UnityEngine.Experimental.Rendering.Universal
- {
-
-
-
- [DisallowMultipleComponent]
- [AddComponentMenu("Rendering/2D/Pixel Perfect Camera (Experimental)")]
- [RequireComponent(typeof(Camera))]
- [MovedFrom("UnityEngine.Experimental.Rendering.LWRP")] public class PixelPerfectCamera : MonoBehaviour, IPixelPerfectCamera
- {
-
-
-
- public int assetsPPU { get { return m_AssetsPPU; } set { m_AssetsPPU = value > 0 ? value : 1; } }
-
-
-
- public int refResolutionX { get { return m_RefResolutionX; } set { m_RefResolutionX = value > 0 ? value : 1; } }
-
-
-
- public int refResolutionY { get { return m_RefResolutionY; } set { m_RefResolutionY = value > 0 ? value : 1; } }
-
-
-
-
- public bool upscaleRT { get { return m_UpscaleRT; } set { m_UpscaleRT = value; } }
-
-
-
-
- public bool pixelSnapping { get { return m_PixelSnapping; } set { m_PixelSnapping = value; } }
-
-
-
- public bool cropFrameX { get { return m_CropFrameX; } set { m_CropFrameX = value; } }
-
-
-
- public bool cropFrameY { get { return m_CropFrameY; } set { m_CropFrameY = value; } }
-
-
-
-
- public bool stretchFill { get { return m_StretchFill; } set { m_StretchFill = value; } }
-
-
-
- public int pixelRatio
- {
- get
- {
- if (m_CinemachineCompatibilityMode)
- {
- if (m_UpscaleRT)
- return m_Internal.zoom * m_Internal.cinemachineVCamZoom;
- else
- return m_Internal.cinemachineVCamZoom;
- }
- else
- {
- return m_Internal.zoom;
- }
- }
- }
-
-
-
-
-
-
-
-
- public Vector3 RoundToPixel(Vector3 position)
- {
- float unitsPerPixel = m_Internal.unitsPerPixel;
- if (unitsPerPixel == 0.0f)
- return position;
- Vector3 result;
- result.x = Mathf.Round(position.x / unitsPerPixel) * unitsPerPixel;
- result.y = Mathf.Round(position.y / unitsPerPixel) * unitsPerPixel;
- result.z = Mathf.Round(position.z / unitsPerPixel) * unitsPerPixel;
- return result;
- }
-
-
-
-
-
- public float CorrectCinemachineOrthoSize(float targetOrthoSize)
- {
- m_CinemachineCompatibilityMode = true;
- if (m_Internal == null)
- return targetOrthoSize;
- else
- return m_Internal.CorrectCinemachineOrthoSize(targetOrthoSize);
- }
- [SerializeField] int m_AssetsPPU = 100;
- [SerializeField] int m_RefResolutionX = 320;
- [SerializeField] int m_RefResolutionY = 180;
- [SerializeField] bool m_UpscaleRT;
- [SerializeField] bool m_PixelSnapping;
- [SerializeField] bool m_CropFrameX;
- [SerializeField] bool m_CropFrameY;
- [SerializeField] bool m_StretchFill;
- Camera m_Camera;
- PixelPerfectCameraInternal m_Internal;
- bool m_CinemachineCompatibilityMode;
- internal bool isRunning
- {
- get
- {
- #if UNITY_EDITOR
- return (Application.isPlaying || runInEditMode) && enabled;
- #else
- return enabled;
- #endif
- }
- }
- internal FilterMode finalBlitFilterMode
- {
- get
- {
- if (!isRunning)
- return FilterMode.Bilinear;
- else
- return m_Internal.useStretchFill ? FilterMode.Bilinear : FilterMode.Point;
- }
- }
- internal Vector2Int offscreenRTSize
- {
- get
- {
- if (!isRunning)
- return Vector2Int.zero;
- else
- return new Vector2Int(m_Internal.offscreenRTWidth, m_Internal.offscreenRTHeight);
- }
- }
-
- void PixelSnap()
- {
- Vector3 cameraPosition = m_Camera.transform.position;
- Vector3 roundedCameraPosition = RoundToPixel(cameraPosition);
- Vector3 offset = roundedCameraPosition - cameraPosition;
- offset.z = -offset.z;
- Matrix4x4 offsetMatrix = Matrix4x4.TRS(-offset, Quaternion.identity, new Vector3(1.0f, 1.0f, -1.0f));
- m_Camera.worldToCameraMatrix = offsetMatrix * m_Camera.transform.worldToLocalMatrix;
- }
- void Awake()
- {
- m_Camera = GetComponent<Camera>();
- m_Internal = new PixelPerfectCameraInternal(this);
- m_Internal.originalOrthoSize = m_Camera.orthographicSize;
- }
- void LateUpdate()
- {
- #if UNITY_EDITOR
- if (!UnityEditor.EditorApplication.isPaused)
- #endif
- {
-
-
-
-
- m_CinemachineCompatibilityMode = false;
- }
- }
- void OnBeginCameraRendering(ScriptableRenderContext context, Camera camera)
- {
- if (camera != m_Camera)
- return;
- var targetTexture = m_Camera.targetTexture;
- Vector2Int rtSize = targetTexture == null ? new Vector2Int(Screen.width, Screen.height) : new Vector2Int(targetTexture.width, targetTexture.height);
- m_Internal.CalculateCameraProperties(rtSize.x, rtSize.y);
- PixelSnap();
- if (m_Internal.useOffscreenRT)
- m_Camera.pixelRect = m_Internal.CalculateFinalBlitPixelRect(rtSize.x, rtSize.y);
- else
- m_Camera.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
-
-
-
-
- if (!m_CinemachineCompatibilityMode)
- {
- m_Camera.orthographicSize = m_Internal.orthoSize;
- }
- UnityEngine.U2D.PixelPerfectRendering.pixelSnapSpacing = m_Internal.unitsPerPixel;
- }
- void OnEndCameraRendering(ScriptableRenderContext context, Camera camera)
- {
- if (camera == m_Camera)
- UnityEngine.U2D.PixelPerfectRendering.pixelSnapSpacing = 0.0f;
- }
- void OnEnable()
- {
- RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
- RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
- #if UNITY_EDITOR
- if (!UnityEditor.EditorApplication.isPlaying)
- UnityEditor.EditorApplication.playModeStateChanged += OnPlayModeChanged;
- #endif
- }
- internal void OnDisable()
- {
- RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
- RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
- m_Camera.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
- m_Camera.orthographicSize = m_Internal.originalOrthoSize;
- m_Camera.ResetWorldToCameraMatrix();
- #if UNITY_EDITOR
- if (!UnityEditor.EditorApplication.isPlaying)
- UnityEditor.EditorApplication.playModeStateChanged -= OnPlayModeChanged;
- #endif
- }
- #if DEVELOPMENT_BUILD || UNITY_EDITOR
-
- void OnGUI()
- {
- #if UNITY_EDITOR
- if (!UnityEditor.EditorApplication.isPlaying && !runInEditMode)
- return;
- #endif
- Color oldColor = GUI.color;
- GUI.color = Color.red;
- Vector2Int renderResolution = Vector2Int.zero;
- renderResolution.x = m_Internal.useOffscreenRT ? m_Internal.offscreenRTWidth : m_Camera.pixelWidth;
- renderResolution.y = m_Internal.useOffscreenRT ? m_Internal.offscreenRTHeight : m_Camera.pixelHeight;
- if (renderResolution.x % 2 != 0 || renderResolution.y % 2 != 0)
- {
- string warning = string.Format("Rendering at an odd-numbered resolution ({0} * {1}). Pixel Perfect Camera may not work properly in this situation.", renderResolution.x, renderResolution.y);
- GUILayout.Box(warning);
- }
- var targetTexture = m_Camera.targetTexture;
- Vector2Int rtSize = targetTexture == null ? new Vector2Int(Screen.width, Screen.height) : new Vector2Int(targetTexture.width, targetTexture.height);
- if (rtSize.x < refResolutionX || rtSize.y < refResolutionY)
- {
- GUILayout.Box("Target resolution is smaller than the reference resolution. Image may appear stretched or cropped.");
- }
- GUI.color = oldColor;
- }
- #endif
- #if UNITY_EDITOR
- void OnPlayModeChanged(UnityEditor.PlayModeStateChange state)
- {
-
- if (state == UnityEditor.PlayModeStateChange.ExitingEditMode)
- {
- runInEditMode = false;
- OnDisable();
- }
- }
- #endif
- }
- }
|