Skip to content

Commit

Permalink
Camera framing
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Mar 22, 2024
1 parent 0eb823c commit 087ea97
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 10 deletions.
38 changes: 28 additions & 10 deletions Assets/Scenes/Basic Example.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
53 changes: 53 additions & 0 deletions Assets/Scripts/AutoFrameCamera.cs
Original file line number Diff line number Diff line change
@@ -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<Renderer>();
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);
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/AutoFrameCamera.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Assets/Scripts/PolyhydraGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ private void Build()
{
var mf = gameObject.GetComponent<MeshFilter>();
mf.mesh = settings.BuildAll(appearanceSettings);
// TODO check if this is neccessary
mf.mesh.RecalculateBounds();
}

private void OnValidate()
Expand Down

0 comments on commit 087ea97

Please sign in to comment.