Add interactive dome view and five-chapter story
This commit is contained in:
189
Assets/DomeShift/DomeCameraRig.cs
Normal file
189
Assets/DomeShift/DomeCameraRig.cs
Normal file
@@ -0,0 +1,189 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace DomeShift
|
||||
{
|
||||
internal sealed class DomeCameraRig
|
||||
{
|
||||
private static readonly Vector3 OverviewOffset = new(15f, 67f, -79f);
|
||||
private static readonly Vector3 FollowOffset = new(0f, 16f, -20f);
|
||||
|
||||
private const float MinimumPitch = 18f;
|
||||
private const float MaximumPitch = 82f;
|
||||
private const float OrbitSensitivity = 0.24f;
|
||||
private const float ZoomStep = 0.85f;
|
||||
|
||||
private readonly View followView = new();
|
||||
private readonly View overviewView = new();
|
||||
private readonly View viewerView = new();
|
||||
private View activeView;
|
||||
private Vector2 lastPointer;
|
||||
private int dragButton = -1;
|
||||
private bool panning;
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
followView.Reset();
|
||||
overviewView.Reset();
|
||||
viewerView.Reset();
|
||||
dragButton = -1;
|
||||
}
|
||||
|
||||
public void Frame(Camera camera, DomeEnvironment profile, Transform environmentRoot, Transform player,
|
||||
bool title, bool overview, bool viewer, bool allowInput)
|
||||
{
|
||||
bool wideView = title || overview || viewer;
|
||||
View view = title ? null : viewer ? viewerView : overview ? overviewView : followView;
|
||||
if (activeView != view)
|
||||
{
|
||||
activeView = view;
|
||||
dragButton = -1;
|
||||
}
|
||||
|
||||
float aspect = Mathf.Max(camera.aspect, 0.1f);
|
||||
Vector3 center;
|
||||
Vector3 offset;
|
||||
float baseSize;
|
||||
if (wideView)
|
||||
{
|
||||
float shift = title ? Mathf.Min(profile.Width * 0.18f, aspect * 9f) : 0f;
|
||||
center = environmentRoot.position + new Vector3(-shift, profile.RoofHeight * 0.23f, -1f);
|
||||
offset = OverviewOffset;
|
||||
float projectedWidth = profile.Width * 0.5f + profile.Depth * 0.09f + 4.5f + shift;
|
||||
baseSize = Mathf.Max(profile.Depth * 0.35f + profile.RoofHeight * 0.62f + 3f, projectedWidth / aspect);
|
||||
if (viewer)
|
||||
baseSize = Mathf.Max(baseSize, FittedDomeSize(profile, aspect));
|
||||
}
|
||||
else
|
||||
{
|
||||
center = player.position + new Vector3(0f, 0.5f, 1.15f);
|
||||
offset = FollowOffset;
|
||||
baseSize = Mathf.Max(9.4f, 11.5f / aspect);
|
||||
}
|
||||
|
||||
if (view == null)
|
||||
{
|
||||
dragButton = -1;
|
||||
camera.transform.SetPositionAndRotation(center + offset, Quaternion.LookRotation(-offset));
|
||||
camera.orthographicSize = baseSize;
|
||||
return;
|
||||
}
|
||||
|
||||
float baseYaw = Mathf.Atan2(-offset.x, -offset.z) * Mathf.Rad2Deg;
|
||||
float basePitch = Mathf.Atan2(offset.y, new Vector2(offset.x, offset.z).magnitude) * Mathf.Rad2Deg;
|
||||
if (allowInput && Application.isFocused)
|
||||
ReadInput(camera, view, baseYaw, basePitch, baseSize, wideView);
|
||||
else
|
||||
dragButton = -1;
|
||||
|
||||
Vector2 panLimit = wideView ? new Vector2(profile.Width * 0.57f, profile.Depth * 0.57f) : Vector2.one * 7f;
|
||||
view.Pan.x = Mathf.Clamp(view.Pan.x, -panLimit.x, panLimit.x);
|
||||
view.Pan.y = Mathf.Clamp(view.Pan.y, -panLimit.y, panLimit.y);
|
||||
center += new Vector3(view.Pan.x, 0f, view.Pan.y);
|
||||
|
||||
float pitch = basePitch + view.PitchOffset;
|
||||
Quaternion rotation = Quaternion.Euler(pitch, baseYaw + view.Yaw, 0f);
|
||||
float size = baseSize * view.Zoom;
|
||||
// Orthographic scale is independent of distance. Back up at shallow pitches to preserve the foreground.
|
||||
float foregroundDistance = size / Mathf.Sin(pitch * Mathf.Deg2Rad) + 4f;
|
||||
float distance = Mathf.Max(offset.magnitude, Mathf.Min(foregroundDistance, camera.farClipPlane * 0.48f));
|
||||
camera.transform.SetPositionAndRotation(center - rotation * Vector3.forward * distance, rotation);
|
||||
camera.orthographicSize = size;
|
||||
}
|
||||
|
||||
private static float FittedDomeSize(DomeEnvironment profile, float aspect)
|
||||
{
|
||||
// Include the foundation and projecting airlock in the default overview framing.
|
||||
float halfWidth = profile.Width * (47.6f / 86f);
|
||||
float halfDepth = profile.Depth * (37.2f / 64f);
|
||||
float yaw = Mathf.Atan2(OverviewOffset.x, -OverviewOffset.z);
|
||||
float pitch = Mathf.Atan2(OverviewOffset.y, new Vector2(OverviewOffset.x, OverviewOffset.z).magnitude);
|
||||
float horizontal = halfWidth * Mathf.Cos(yaw) + halfDepth * Mathf.Sin(yaw);
|
||||
float depth = halfDepth * Mathf.Cos(yaw) + halfWidth * Mathf.Sin(yaw);
|
||||
float nearEdge = depth * Mathf.Sin(pitch) + (profile.RoofHeight * 0.23f + 1.4f) * Mathf.Cos(pitch);
|
||||
return Mathf.Max((horizontal + 2.5f) / aspect, nearEdge + 2f);
|
||||
}
|
||||
|
||||
private void ReadInput(Camera camera, View view, float baseYaw, float basePitch, float baseSize, bool wideView)
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Home))
|
||||
{
|
||||
view.Reset();
|
||||
dragButton = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
Vector2 pointer = Input.mousePosition;
|
||||
if (!camera.pixelRect.Contains(pointer))
|
||||
{
|
||||
dragButton = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
float wheel = Input.mouseScrollDelta.y;
|
||||
if (wheel != 0f)
|
||||
{
|
||||
float zoom = view.Zoom * Mathf.Pow(ZoomStep, Mathf.Clamp(wheel, -8f, 8f));
|
||||
view.Zoom = Mathf.Clamp(zoom, wideView ? 0.18f : 0.42f, wideView ? 2.25f : 2.1f);
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(2))
|
||||
{
|
||||
BeginDrag(2, pointer, true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
BeginDrag(1, pointer, Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift));
|
||||
return;
|
||||
}
|
||||
|
||||
if (dragButton < 0 || !Input.GetMouseButton(dragButton))
|
||||
{
|
||||
dragButton = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
Vector2 delta = pointer - lastPointer;
|
||||
lastPointer = pointer;
|
||||
if (panning)
|
||||
{
|
||||
Quaternion heading = Quaternion.Euler(0f, baseYaw + view.Yaw, 0f);
|
||||
float pitch = (basePitch + view.PitchOffset) * Mathf.Deg2Rad;
|
||||
float unitsPerPixel = baseSize * view.Zoom * 2f / Mathf.Max(camera.pixelHeight, 1);
|
||||
Vector3 worldDelta = heading * new Vector3(delta.x, 0f, delta.y / Mathf.Sin(pitch));
|
||||
view.Pan -= new Vector2(worldDelta.x, worldDelta.z) * unitsPerPixel;
|
||||
}
|
||||
else
|
||||
{
|
||||
float dpiScale = Mathf.Clamp(720f / Mathf.Max(camera.pixelHeight, 1), 0.45f, 1.6f);
|
||||
view.Yaw = Mathf.Repeat(view.Yaw + delta.x * OrbitSensitivity * dpiScale + 180f, 360f) - 180f;
|
||||
float pitch = basePitch + view.PitchOffset + delta.y * OrbitSensitivity * dpiScale;
|
||||
view.PitchOffset = Mathf.Clamp(pitch, MinimumPitch, MaximumPitch) - basePitch;
|
||||
}
|
||||
}
|
||||
|
||||
private void BeginDrag(int button, Vector2 pointer, bool pan)
|
||||
{
|
||||
dragButton = button;
|
||||
lastPointer = pointer;
|
||||
panning = pan;
|
||||
}
|
||||
|
||||
private sealed class View
|
||||
{
|
||||
public Vector2 Pan;
|
||||
public float Yaw;
|
||||
public float PitchOffset;
|
||||
public float Zoom = 1f;
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Pan = Vector2.zero;
|
||||
Yaw = 0f;
|
||||
PitchOffset = 0f;
|
||||
Zoom = 1f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user