added: tools for changing camera settings, and camera settings processor

This commit is contained in:
Chris
2025-07-14 14:22:50 -04:00
parent c8f83603fd
commit bbfb583e3c
7 changed files with 307 additions and 157 deletions

View File

@@ -0,0 +1,128 @@
using Unity.Cinemachine;
using UnityEngine;
public struct CameraSettingSingleValue<T>{
// NOTE: Could add a locked variable here if needed??
public T originalValue;
public T targetValue;
public float smoothing;
public float velocityRef;
public Vector2 velocityRefV2;
public CameraSettingSingleValue(float defaultSmoothing, T original = default(T)){
originalValue = original;
targetValue = default(T);
smoothing = defaultSmoothing;
velocityRef = 0;
velocityRefV2 = default;
}
public void Reset(){
targetValue = originalValue;
}
}
public struct CameraSettingValues{
public CameraSettingSingleValue<float> orbitFollowTopHeight;
public CameraSettingSingleValue<float> orbitFollowTopRadius;
public CameraSettingSingleValue<float> orbitFollowCenterHeight;
public CameraSettingSingleValue<float> orbitFollowCenterRadius;
public CameraSettingSingleValue<float> orbitFollowBottomHeight;
public CameraSettingSingleValue<float> orbitFollowBottomRadius;
public CameraSettingSingleValue<Vector2> rotationComposerScreenPos;
public CameraSettingValues(float defaultSmoothing){
orbitFollowTopHeight = new CameraSettingSingleValue<float>(defaultSmoothing);
orbitFollowTopRadius = new CameraSettingSingleValue<float>(defaultSmoothing);
orbitFollowCenterHeight = new CameraSettingSingleValue<float>(defaultSmoothing);
orbitFollowCenterRadius = new CameraSettingSingleValue<float>(defaultSmoothing);
orbitFollowBottomHeight = new CameraSettingSingleValue<float>(defaultSmoothing);
orbitFollowBottomRadius = new CameraSettingSingleValue<float>(defaultSmoothing);
rotationComposerScreenPos = new CameraSettingSingleValue<Vector2>(defaultSmoothing);
}
}
public class CameraSettingsProcessor : MonoBehaviour{
public static CameraSettingsProcessor Instance{ get; private set; }
public static CameraSettingValues values = new(defaultSmoothing: .2f);
public static GameObject mainCamera;
private CinemachineOrbitalFollow orbit;
private CinemachineRotationComposer rotComp;
public void Awake(){
if (Instance != null && Instance != this) {
Destroy(this);
} else {
Instance = this;
}
mainCamera = gameObject;
orbit = mainCamera.GetComponent<CinemachineOrbitalFollow>();
rotComp = mainCamera.GetComponent<CinemachineRotationComposer>();
StashCameraOriginalSettings(this);
SetTargetsToOriginalValues(this);
}
public static void StashCameraOriginalSettings(CameraSettingsProcessor settings){
values.orbitFollowTopHeight.originalValue = settings.orbit.Orbits.Top.Height;
values.orbitFollowTopRadius.originalValue = settings.orbit.Orbits.Top.Radius;
values.orbitFollowCenterHeight.originalValue = settings.orbit.Orbits.Center.Height;
values.orbitFollowCenterRadius.originalValue = settings.orbit.Orbits.Center.Radius;
values.orbitFollowBottomHeight.originalValue = settings.orbit.Orbits.Bottom.Height;
values.orbitFollowBottomRadius.originalValue = settings.orbit.Orbits.Bottom.Radius;
values.rotationComposerScreenPos.originalValue = settings.rotComp.Composition.ScreenPosition;
}
public static void SetTargetsToOriginalValues(CameraSettingsProcessor settings){
values.orbitFollowTopHeight.Reset();
values.orbitFollowTopRadius.Reset();
values.orbitFollowCenterHeight.Reset();
values.orbitFollowCenterRadius.Reset();
values.orbitFollowBottomHeight.Reset();
values.orbitFollowBottomRadius.Reset();
values.rotationComposerScreenPos.Reset();
}
void ProcessCameraValues(){
orbit.Orbits.Top.Height = Mathf.SmoothDamp(orbit.Orbits.Top.Height,
values.orbitFollowTopHeight.targetValue, ref values.orbitFollowTopHeight.velocityRef,
values.orbitFollowTopHeight.smoothing);
orbit.Orbits.Top.Radius = Mathf.SmoothDamp(orbit.Orbits.Top.Radius,
values.orbitFollowTopRadius.targetValue, ref values.orbitFollowTopRadius.velocityRef,
values.orbitFollowTopRadius.smoothing);
orbit.Orbits.Center.Height = Mathf.SmoothDamp(orbit.Orbits.Center.Height,
values.orbitFollowCenterHeight.targetValue, ref values.orbitFollowCenterHeight.velocityRef,
values.orbitFollowCenterHeight.smoothing);
orbit.Orbits.Center.Radius = Mathf.SmoothDamp(orbit.Orbits.Center.Radius,
values.orbitFollowCenterRadius.targetValue, ref values.orbitFollowCenterRadius.velocityRef,
values.orbitFollowCenterRadius.smoothing);
orbit.Orbits.Bottom.Height = Mathf.SmoothDamp(orbit.Orbits.Bottom.Height,
values.orbitFollowBottomHeight.targetValue, ref values.orbitFollowBottomHeight.velocityRef,
values.orbitFollowBottomHeight.smoothing);
orbit.Orbits.Bottom.Radius = Mathf.SmoothDamp(orbit.Orbits.Bottom.Radius,
values.orbitFollowBottomRadius.targetValue, ref values.orbitFollowBottomRadius.velocityRef,
values.orbitFollowBottomRadius.smoothing);
rotComp.Composition.ScreenPosition = Vector2.SmoothDamp(rotComp.Composition.ScreenPosition,
values.rotationComposerScreenPos.targetValue, ref values.rotationComposerScreenPos.velocityRefV2,
values.rotationComposerScreenPos.smoothing);
}
void Update(){
ProcessCameraValues();
}
}