90 lines
2.2 KiB
C#
90 lines
2.2 KiB
C#
|
|
using MMMaellon.LightSync;
|
|
using UdonSharp;
|
|
using UnityEngine;
|
|
using CameraSystem;
|
|
using VRC.Udon.Common;
|
|
|
|
|
|
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
|
|
public class CameraAnchor : UdonSharpBehaviour
|
|
{
|
|
[SerializeField] private CameraSystem_Console CameraSystemManager;
|
|
[Space]
|
|
[SerializeField] private float FOV = 60.0f;
|
|
[SerializeField] private float NearClippingPlane = 0.3f;
|
|
[SerializeField] private float FarClippingPlane = 1000.0f;
|
|
[Space]
|
|
[Tooltip("Changing the Z scale of this object will change the FOV of the attached camera.")]
|
|
[SerializeField] private Transform CameraRoot;
|
|
|
|
[UdonSynced] private int _AttachedCameraIndex = -1;
|
|
|
|
private Camera _AttachedCamera = null;
|
|
|
|
|
|
void LateUpdate()
|
|
{
|
|
if (_AttachedCamera)
|
|
{
|
|
if (CameraRoot.childCount <= 0)
|
|
{
|
|
_AttachedCamera = null;
|
|
}
|
|
else
|
|
{
|
|
_AttachedCamera.fieldOfView = FOV * CameraRoot.transform.localScale.z;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void OnDeserialization(DeserializationResult Result)
|
|
{
|
|
_AttachCamera_Synced();
|
|
|
|
base.OnDeserialization(Result);
|
|
}
|
|
|
|
|
|
public void AttachCamera(Camera CameraComponent)
|
|
{
|
|
if (CameraSystemManager == null)
|
|
{
|
|
Debug.LogError("[CameraAnchor] No CameraSystemManager set for " + gameObject.name + "; can't find a camera without it");
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < CameraSystemManager.camerasObjects.Length; i++)
|
|
{
|
|
if (CameraSystemManager.camerasObjects[i] == CameraComponent)
|
|
{
|
|
Debug.Log("[CameraAnchor] Attaching camera " + i + " to anchor " + gameObject.name);
|
|
_AttachedCameraIndex = i;
|
|
_AttachCamera_Synced();
|
|
RequestSerialization();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private void _AttachCamera_Synced()
|
|
{
|
|
_AttachedCamera = CameraSystemManager.camerasObjects[_AttachedCameraIndex];
|
|
_AttachedCamera.gameObject.SetActive(true);
|
|
_AttachedCamera.transform.parent = CameraRoot;
|
|
_AttachedCamera.fieldOfView = FOV;
|
|
_AttachedCamera.nearClipPlane = NearClippingPlane;
|
|
_AttachedCamera.farClipPlane = FarClippingPlane;
|
|
_AttachedCamera.GetComponent<LightSync>().TeleportToLocalSpace(Vector3.zero, Quaternion.identity, true);
|
|
}
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnDrawGizmos()
|
|
{
|
|
Gizmos.DrawIcon(transform.position, "CameraAnchor", true);
|
|
}
|
|
#endif
|
|
}
|