Skip to content

Instantly share code, notes, and snippets.

@PixelEnvision
Last active February 8, 2023 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PixelEnvision/43105df70abf1e6bc89a17dfa24d0b4b to your computer and use it in GitHub Desktop.
Save PixelEnvision/43105df70abf1e6bc89a17dfa24d0b4b to your computer and use it in GitHub Desktop.
Cinemachine Camera Size Adjuster Extension (Aspect Ratio Based Width Scaling)
using Cinemachine;
using UnityEngine;
namespace Cinemachine
{
[AddComponentMenu("")] // Hide in menu
[SaveDuringPlay]
[ExecuteAlways]
[DisallowMultipleComponent]
public class CinemachineCameraSizeAdjuster : CinemachineExtension
{
[SerializeField] Vector2Int _baseAspectRatio = new Vector2Int(9, 16);
[SerializeField][Range(0, 179)] float _baseCameraFOV = 60;
[SerializeField] float _baseCameraSize = 5;
float baseAspectRatio => _baseAspectRatio.x / (float)_baseAspectRatio.y;
protected override void PostPipelineStageCallback(
CinemachineVirtualCameraBase vcam,
CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
{
if (stage == CinemachineCore.Stage.Body)
{
LensSettings lens = state.Lens;
if (state.Lens.Orthographic)
{
if (state.Lens.Aspect < baseAspectRatio)
{
// letterboxing
var baseHorizontalSize = _baseCameraSize * baseAspectRatio;
var verticalSize = baseHorizontalSize / state.Lens.Aspect;
lens.OrthographicSize = verticalSize;
}
else
{
// pillarboxing
lens.OrthographicSize = _baseCameraSize;
}
}
else
{
if (state.Lens.Aspect < baseAspectRatio)
{
// letterboxing
var baseVerticalSize = Mathf.Tan(_baseCameraFOV * 0.5f * Mathf.Deg2Rad);
var baseHorizontalSize = baseVerticalSize * baseAspectRatio;
var verticalSize = baseHorizontalSize / state.Lens.Aspect;
var verticalFov = Mathf.Atan(verticalSize) * Mathf.Rad2Deg * 2;
lens.FieldOfView = verticalFov;
}
else
{
// pillarboxing
lens.FieldOfView = _baseCameraFOV;
}
}
state.Lens = lens;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment