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

@@ -8,11 +8,24 @@ namespace AppleHillsCamera
/// </summary>
public class ScreenReferenceMarker : MonoBehaviour
{
[Header("Horizontal Reference")]
[Tooltip("The target width that should match the screen width")]
public float targetWidth = 10f;
public enum ScalingMode
{
Width,
Height
}
[Header("Vertical References")]
[Header("Scaling Mode")]
[Tooltip("Whether to scale the camera based on width or height")]
public ScalingMode scalingMode = ScalingMode.Width;
[Header("Reference Dimensions")]
[Tooltip("The target width that should match the screen width (used when scaling by width)")]
public float targetWidth = 10f;
[Tooltip("The target height that should match the screen height (used when scaling by height)")]
public float targetHeight = 5f;
[Header("Margins")]
[Tooltip("Distance from top of screen to use for anchoring")]
public float topMargin = 1f;
@@ -49,20 +62,8 @@ namespace AppleHillsCamera
Vector3 position = transform.position;
// Draw the width reference
Vector3 left = position + Vector3.left * (targetWidth / 2f);
Vector3 right = position + Vector3.right * (targetWidth / 2f);
Gizmos.DrawLine(left, right);
// Draw vertical endpoints
float endCapSize = 0.5f;
Gizmos.DrawLine(left, left + Vector3.up * endCapSize);
Gizmos.DrawLine(left, left + Vector3.down * endCapSize);
Gizmos.DrawLine(right, right + Vector3.up * endCapSize);
Gizmos.DrawLine(right, right + Vector3.down * endCapSize);
// Calculate visual screen edges based on actual camera viewport
float halfWidth = targetWidth / 2f;
// Calculate visual screen edges based on scaling mode and actual camera viewport
float halfWidth;
float halfHeight;
// Try to get camera references in the preferred order
@@ -71,14 +72,37 @@ namespace AppleHillsCamera
Camera mainCamera = Camera.main;
if (mainCamera != null && mainCamera.orthographic)
{
// Use the main camera's actual orthographic size for the height
halfHeight = mainCamera.orthographicSize;
if (scalingMode == ScalingMode.Width)
{
// When scaling by width, the width is fixed and height varies
halfWidth = targetWidth / 2f;
// Use the main camera's actual orthographic size for the height
halfHeight = mainCamera.orthographicSize;
}
else // ScalingMode.Height
{
// When scaling by height, the height is fixed and width varies
halfHeight = targetHeight / 2f;
// Calculate width based on camera's aspect ratio
float screenAspect = (float)Screen.width / Screen.height;
halfWidth = halfHeight * screenAspect;
}
}
else
{
// 2. Use Game/Simulator window resolution
float gameViewAspect = (float)Screen.height / Screen.width;
halfHeight = halfWidth * gameViewAspect;
if (scalingMode == ScalingMode.Width)
{
halfWidth = targetWidth / 2f;
halfHeight = halfWidth * gameViewAspect;
}
else // ScalingMode.Height
{
halfHeight = targetHeight / 2f;
halfWidth = halfHeight / gameViewAspect;
}
// 3. Fallback to the scene view camera if needed
UnityEditor.SceneView sceneView = UnityEditor.SceneView.lastActiveSceneView;
@@ -86,10 +110,49 @@ namespace AppleHillsCamera
{
// Use the scene view camera's aspect ratio instead
float sceneAspect = sceneView.camera.pixelHeight / (float)sceneView.camera.pixelWidth;
halfHeight = halfWidth * sceneAspect;
if (scalingMode == ScalingMode.Width)
{
halfHeight = halfWidth * sceneAspect;
}
else // ScalingMode.Height
{
float sceneAspectInv = sceneView.camera.pixelWidth / (float)sceneView.camera.pixelHeight;
halfWidth = halfHeight * sceneAspectInv;
}
}
}
// Draw the reference dimension indicator based on scaling mode
if (scalingMode == ScalingMode.Width)
{
// Draw the width reference (horizontal line)
Vector3 left = position + Vector3.left * halfWidth;
Vector3 right = position + Vector3.right * halfWidth;
Gizmos.DrawLine(left, right);
// Draw vertical endpoints
float endCapSize = 0.5f;
Gizmos.DrawLine(left, left + Vector3.up * endCapSize);
Gizmos.DrawLine(left, left + Vector3.down * endCapSize);
Gizmos.DrawLine(right, right + Vector3.up * endCapSize);
Gizmos.DrawLine(right, right + Vector3.down * endCapSize);
}
else // ScalingMode.Height
{
// Draw the height reference (vertical line)
Vector3 top = position + Vector3.up * halfHeight;
Vector3 bottom = position + Vector3.down * halfHeight;
Gizmos.DrawLine(top, bottom);
// Draw horizontal endpoints
float endCapSize = 0.5f;
Gizmos.DrawLine(top, top + Vector3.left * endCapSize);
Gizmos.DrawLine(top, top + Vector3.right * endCapSize);
Gizmos.DrawLine(bottom, bottom + Vector3.left * endCapSize);
Gizmos.DrawLine(bottom, bottom + Vector3.right * endCapSize);
}
// Screen edge positions
Vector3 topEdge = position + Vector3.up * halfHeight;
Vector3 bottomEdge = position + Vector3.down * halfHeight;