Add horizontal camera scaling for devices

This commit is contained in:
Michal Pikulski
2025-12-18 15:07:27 +01:00
parent b2bc77674d
commit 61f6da7a7d
7 changed files with 326 additions and 32 deletions

View File

@@ -19,6 +19,9 @@ namespace AppleHillsCamera
[Tooltip("Whether to adjust the camera automatically when the screen size changes")]
public bool adjustOnScreenResize = true;
[Tooltip("Enable debug logging")]
public bool debugLogging;
// Event that other components can subscribe to when camera is adjusted
public event Action OnCameraAdjusted;
@@ -71,7 +74,7 @@ namespace AppleHillsCamera
}
/// <summary>
/// Manually trigger camera adjustment to match the reference width.
/// Manually trigger camera adjustment to match the reference width or height.
/// </summary>
public void AdjustCamera()
{
@@ -81,13 +84,34 @@ namespace AppleHillsCamera
return;
}
// Calculate the orthographic size based on the target width and screen aspect ratio
float targetWidth = referenceMarker.targetWidth;
float screenAspect = (float)Screen.height / Screen.width;
float orthoSize;
// Orthographic size is half the height, so we calculate:
// orthoSize = (targetWidth / 2) * (screenHeight / screenWidth)
float orthoSize = (targetWidth / 2f) * screenAspect;
if (referenceMarker.scalingMode == ScreenReferenceMarker.ScalingMode.Width)
{
// Calculate the orthographic size based on the target width and screen aspect ratio
float targetWidth = referenceMarker.targetWidth;
float screenAspect = (float)Screen.height / Screen.width;
// Orthographic size is half the height, so we calculate:
// orthoSize = (targetWidth / 2) * (screenHeight / screenWidth)
orthoSize = (targetWidth / 2f) * screenAspect;
if (debugLogging)
{
Logging.Debug($"CameraScreenAdapter: Scaling by Width - targetWidth={targetWidth:F2}, aspect={screenAspect:F2}, orthoSize={orthoSize:F2}");
}
}
else // ScalingMode.Height
{
// When scaling by height, the orthographic size directly matches half the target height
float targetHeight = referenceMarker.targetHeight;
orthoSize = targetHeight / 2f;
if (debugLogging)
{
Logging.Debug($"CameraScreenAdapter: Scaling by Height - targetHeight={targetHeight:F2}, orthoSize={orthoSize:F2}");
}
}
// Apply the calculated size to the camera
if (_usingCinemachine)
@@ -96,7 +120,11 @@ namespace AppleHillsCamera
var lens = _virtualCamera.Lens;
lens.OrthographicSize = orthoSize;
_virtualCamera.Lens = lens;
Logging.Debug($"Cinemachine Camera adapted: Width={targetWidth}, Aspect={screenAspect:F2}, OrthoSize={orthoSize:F2}");
if (debugLogging)
{
Logging.Debug($"Cinemachine Camera adapted: OrthoSize={orthoSize:F2}");
}
}
else
{
@@ -104,7 +132,11 @@ namespace AppleHillsCamera
if (_regularCamera.orthographic)
{
_regularCamera.orthographicSize = orthoSize;
Logging.Debug($"Camera adapted: Width={targetWidth}, Aspect={screenAspect:F2}, OrthoSize={orthoSize:F2}");
if (debugLogging)
{
Logging.Debug($"Camera adapted: OrthoSize={orthoSize:F2}");
}
}
else
{