Initial commit
This commit is contained in:
185
Assets/Scripts/ZodiacCameraController.cs
Normal file
185
Assets/Scripts/ZodiacCameraController.cs
Normal file
@@ -0,0 +1,185 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ZodiacCameraController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform zodiacRoot;
|
||||
[SerializeField] private Camera zodiacCamera;
|
||||
|
||||
private int _currentIndex = 0;
|
||||
|
||||
private Dictionary<string, CameraPreset> _presets;
|
||||
|
||||
private struct CameraPreset
|
||||
{
|
||||
public Vector3 localPosition;
|
||||
public Vector3 localRotation;
|
||||
|
||||
public CameraPreset(Vector3 pos, Vector3 rot)
|
||||
{
|
||||
localPosition = pos;
|
||||
localRotation = rot;
|
||||
}
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_presets = new Dictionary<string, CameraPreset>
|
||||
{
|
||||
{ "Zodiac_Aries",
|
||||
new CameraPreset(
|
||||
new Vector3(52670f, 0f, 2644f),
|
||||
new Vector3(0f, 82.342f, 0f))
|
||||
},
|
||||
{ "Zodiac_Taurus",
|
||||
new CameraPreset(
|
||||
new Vector3(29399f, 17419f, 26891f),
|
||||
new Vector3(-1.675f, 69.34f, 0.769f))
|
||||
},
|
||||
{ "Zodiac_Gemini",
|
||||
new CameraPreset(
|
||||
new Vector3(27079f, 23051f, 31496f),
|
||||
new Vector3(-2.952f, 20.843f, 1.051f)
|
||||
)
|
||||
},
|
||||
{ "Zodiac_Cancer",
|
||||
new CameraPreset(
|
||||
new Vector3(5146f, 26343f, 37622f),
|
||||
new Vector3(-18.238f, -25.072f, 15.872f))
|
||||
},
|
||||
{ "Zodiac_Leo",
|
||||
new CameraPreset(
|
||||
new Vector3(-24035f, 18632f, 24187f),
|
||||
new Vector3(-11.603f, -31.992f, 4.569f)
|
||||
)
|
||||
},
|
||||
{ "Zodiac_Virgo",
|
||||
new CameraPreset(
|
||||
new Vector3(-66493f, 26979f, -12167f),
|
||||
new Vector3(-3.051f, -70.52f, 0.831f)
|
||||
)
|
||||
},
|
||||
{ "Zodiac_Libra",
|
||||
new CameraPreset(
|
||||
new Vector3(-47207f, 2331f, -17070f),
|
||||
new Vector3(5.864f, -45.753f, -3.965f))
|
||||
},
|
||||
{ "Zodiac_Scorpius",
|
||||
new CameraPreset(
|
||||
new Vector3(23243f, -3793f, -41775f),
|
||||
new Vector3(-3.387f, -108.118f, -1.408f))
|
||||
},
|
||||
{ "Zodiac_Ophiuchus",
|
||||
new CameraPreset(
|
||||
new Vector3(-5486f, -72738f, -60897f),
|
||||
new Vector3(-19.647f, 0.134f, 15.524f))
|
||||
},
|
||||
{ "Zodiac_Sagittarius",
|
||||
new CameraPreset(
|
||||
new Vector3(-22611f, -22913f, -34762f),
|
||||
new Vector3(-0.858f, -163.7f, 0.486f))
|
||||
},
|
||||
{ "Zodiac_Capricornus",
|
||||
new CameraPreset(
|
||||
new Vector3(-9422f, -33410f, -64497f),
|
||||
new Vector3(20.279f, 160.564f, 2f))
|
||||
},
|
||||
{ "Zodiac_Aquarius",
|
||||
new CameraPreset(
|
||||
new Vector3(23361f, -23299f, -35603f),
|
||||
new Vector3(-2.652f, 146.782f, 0f))
|
||||
},
|
||||
{ "Zodiac_Pisces",
|
||||
new CameraPreset(
|
||||
new Vector3(33456f, -10168f, -23088f),
|
||||
new Vector3(-9.924f, 109.295f, 3.294f))
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
//zodiacCamera.enabled = true;
|
||||
|
||||
if (zodiacRoot.childCount > 0)
|
||||
FocusCurrent();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.RightArrow))
|
||||
Next();
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.LeftArrow))
|
||||
Previous();
|
||||
}
|
||||
|
||||
void Next()
|
||||
{
|
||||
_currentIndex++;
|
||||
if (_currentIndex >= zodiacRoot.childCount)
|
||||
_currentIndex = 0;
|
||||
|
||||
FocusCurrent();
|
||||
}
|
||||
|
||||
void Previous()
|
||||
{
|
||||
_currentIndex--;
|
||||
if (_currentIndex < 0)
|
||||
_currentIndex = zodiacRoot.childCount - 1;
|
||||
|
||||
FocusCurrent();
|
||||
}
|
||||
|
||||
void FocusCurrent()
|
||||
{
|
||||
if (zodiacRoot == null || zodiacRoot.childCount == 0) return;
|
||||
|
||||
if (_currentIndex < 0 || _currentIndex >= zodiacRoot.childCount)
|
||||
_currentIndex = 0;
|
||||
|
||||
Transform constellation = zodiacRoot.GetChild(_currentIndex);
|
||||
|
||||
if (_presets.ContainsKey(constellation.name))
|
||||
{
|
||||
CameraPreset preset = _presets[constellation.name];
|
||||
|
||||
zodiacCamera.transform.SetParent(null);
|
||||
zodiacCamera.transform.position = preset.localPosition;
|
||||
zodiacCamera.transform.rotation = Quaternion.Euler(preset.localRotation);
|
||||
}
|
||||
else
|
||||
{
|
||||
ApplyFallback(constellation);
|
||||
}
|
||||
}
|
||||
|
||||
void ApplyFallback(Transform constellation)
|
||||
{
|
||||
Bounds bounds = CalculateBounds(constellation);
|
||||
Vector3 center = bounds.center;
|
||||
|
||||
Vector3 dir = new Vector3(-1f, 0f, 0f).normalized;
|
||||
float size = Mathf.Max(bounds.size.x, bounds.size.y);
|
||||
float distance = Mathf.Max(size * 1.6f, 1600f);
|
||||
|
||||
zodiacCamera.transform.SetParent(null);
|
||||
zodiacCamera.transform.position = center + dir * distance;
|
||||
zodiacCamera.transform.LookAt(center, Vector3.up);
|
||||
}
|
||||
|
||||
Bounds CalculateBounds(Transform root)
|
||||
{
|
||||
Renderer[] renderers = root.GetComponentsInChildren<Renderer>();
|
||||
|
||||
Bounds bounds = renderers[0].bounds;
|
||||
for (int i = 1; i < renderers.Length; i++)
|
||||
bounds.Encapsulate(renderers[i].bounds);
|
||||
|
||||
return bounds;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user