From 087ea9773686501c1281cfd4e9b08148361cc572 Mon Sep 17 00:00:00 2001 From: Andy Baker Date: Fri, 22 Mar 2024 11:47:06 +0000 Subject: [PATCH] Camera framing --- Assets/Scenes/Basic Example.unity | 38 +++++++++++++----- Assets/Scripts/AutoFrameCamera.cs | 53 ++++++++++++++++++++++++++ Assets/Scripts/AutoFrameCamera.cs.meta | 3 ++ Assets/Scripts/PolyhydraGenerator.cs | 2 + 4 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 Assets/Scripts/AutoFrameCamera.cs create mode 100644 Assets/Scripts/AutoFrameCamera.cs.meta diff --git a/Assets/Scenes/Basic Example.unity b/Assets/Scenes/Basic Example.unity index 3444da0..6175764 100644 --- a/Assets/Scenes/Basic Example.unity +++ b/Assets/Scenes/Basic Example.unity @@ -210,13 +210,13 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 170076733} - m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261} - m_LocalPosition: {x: 0, y: 3, z: 0} + m_LocalRotation: {x: -0.21394798, y: -0.053004153, z: 0.10154636, w: 0.9701058} + m_LocalPosition: {x: 0, y: 0.7828307, z: -2.8960624} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 1 + m_Father: {fileID: 534669905} + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} --- !u!114 &170076736 MonoBehaviour: @@ -250,6 +250,7 @@ GameObject: - component: {fileID: 534669904} - component: {fileID: 534669903} - component: {fileID: 534669906} + - component: {fileID: 534669907} m_Layer: 0 m_Name: Main Camera m_TagString: MainCamera @@ -288,8 +289,8 @@ Camera: y: 0 width: 1 height: 1 - near clip plane: 0.3 - far clip plane: 1000 + near clip plane: 0.01 + far clip plane: 100 field of view: 60 orthographic: 0 orthographic size: 5 @@ -316,10 +317,11 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 534669902} m_LocalRotation: {x: 0.6013322, y: -0.116301194, z: 0.089038365, w: 0.78545904} - m_LocalPosition: {x: 9.068966, y: 0.8795006, z: -0.2274036} + m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 - m_Children: [] + m_Children: + - {fileID: 170076735} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -356,6 +358,22 @@ MonoBehaviour: m_RequiresDepthTexture: 0 m_RequiresColorTexture: 0 m_Version: 2 +--- !u!114 &534669907 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 534669902} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: bd4d0deaa0564b019f45068386d0ed6e, type: 3} + m_Name: + m_EditorClassIdentifier: + targetObject: {fileID: 832160059} + azimuthAngle: 0 + polarAngle: 45 + zoom: 0.8 --- !u!1 &832160059 GameObject: m_ObjectHideFlags: 0 @@ -455,7 +473,7 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 2 + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1682631075 GameObject: @@ -502,7 +520,7 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 3 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &1682631078 MonoBehaviour: diff --git a/Assets/Scripts/AutoFrameCamera.cs b/Assets/Scripts/AutoFrameCamera.cs new file mode 100644 index 0000000..58934e5 --- /dev/null +++ b/Assets/Scripts/AutoFrameCamera.cs @@ -0,0 +1,53 @@ +using UnityEngine; + +public class AutoFrameCamera : MonoBehaviour +{ + public GameObject targetObject; + public float azimuthAngle = 45.0f; + public float polarAngle = 30.0f; + public float zoom = 0.0f; + + private Vector3 targetPosition; + private float distanceToTarget; + + void Update() + { + if (targetObject != null) + { + UpdateTargetPosition(); + UpdateCameraPosition(); + } + } + + private void UpdateTargetPosition() + { + // Calculate the bounding box in world space + Renderer targetRenderer = targetObject.GetComponent(); + if (targetRenderer != null) + { + Bounds bounds = targetRenderer.bounds; + targetPosition = bounds.center; + + // Adjust distance based on bounding box size and desired zoom + float maxBoundSize = Mathf.Max(bounds.size.x, bounds.size.y, bounds.size.z); + distanceToTarget = (0.5f * maxBoundSize) / Mathf.Tan(Camera.main.fieldOfView * 0.5f * Mathf.Deg2Rad); + distanceToTarget += zoom; + } + } + + private void UpdateCameraPosition() + { + // Calculate camera position from azimuth and polar angles + float azimuthRad = azimuthAngle * Mathf.Deg2Rad; + float polarRad = polarAngle * Mathf.Deg2Rad; + + Vector3 cameraOffset = new Vector3( + distanceToTarget * Mathf.Sin(polarRad) * Mathf.Cos(azimuthRad), + distanceToTarget * Mathf.Cos(polarRad), + distanceToTarget * Mathf.Sin(polarRad) * Mathf.Sin(azimuthRad) + ); + + transform.position = targetPosition + cameraOffset; + transform.LookAt(targetPosition); + } +} \ No newline at end of file diff --git a/Assets/Scripts/AutoFrameCamera.cs.meta b/Assets/Scripts/AutoFrameCamera.cs.meta new file mode 100644 index 0000000..81eddd3 --- /dev/null +++ b/Assets/Scripts/AutoFrameCamera.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bd4d0deaa0564b019f45068386d0ed6e +timeCreated: 1711102870 \ No newline at end of file diff --git a/Assets/Scripts/PolyhydraGenerator.cs b/Assets/Scripts/PolyhydraGenerator.cs index 2a09f67..7e49a2c 100644 --- a/Assets/Scripts/PolyhydraGenerator.cs +++ b/Assets/Scripts/PolyhydraGenerator.cs @@ -58,6 +58,8 @@ private void Build() { var mf = gameObject.GetComponent(); mf.mesh = settings.BuildAll(appearanceSettings); + // TODO check if this is neccessary + mf.mesh.RecalculateBounds(); } private void OnValidate()