-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVirtualCameraRig.cs
More file actions
209 lines (177 loc) · 7.95 KB
/
Copy pathVirtualCameraRig.cs
File metadata and controls
209 lines (177 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Cinemachine;
using Game.Rendering;
using Game.Simulation;
using System;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
namespace FirstPersonCamera
{
/// <summary>
/// Handles game object creation and rigging.
/// </summary>
internal class VirtualCameraRig
{
const float TRANSITION_DAMPEN = 3.5f;
/// <summary>
/// The core transform which handles position and rotation.
/// </summary>
public Transform Parent
{
get
{
return _target;
}
}
/// <summary>
/// A seperate GameObject that holds the virtual camera.
/// </summary>
public Transform CameraTransform
{
get;
private set;
}
/// <summary>
/// The child transform of the parent, used to represent
/// pitch rotation.
/// </summary>
public Transform RigTransform
{
get;
private set;
}
/// <summary>
/// Event for transition end
/// </summary>
public Action<bool> OnTransitionComplete;
private readonly CameraDataModel _model;
private readonly Transform _target;
private readonly CinemachineVirtualCamera _virtualCamera;
private readonly CameraUpdateSystem _cameraUpdateSystem;
private quaternion startRotation;
private float3 startPosition;
private float startFOV;
public VirtualCameraRig( CameraDataModel model, Transform target )
{
_model = model;
var world = World.DefaultGameObjectInjectionWorld;
_cameraUpdateSystem = world.GetExistingSystemManaged<CameraUpdateSystem>( );
_target = target;
RigTransform = new GameObject( "FirstPersonCamera_SubObject" ).transform;
RigTransform.parent = target;
RigTransform.position = target.position;
CameraTransform = new GameObject( "FirstPersonCamera_VCam" ).transform;
_virtualCamera = CameraTransform.gameObject.AddComponent<CinemachineVirtualCamera>( );
_virtualCamera.Follow = target;
Configure( );
}
/// <summary>
/// Configure the rig settings.
/// </summary>
private void Configure( )
{
// Get settings from the working camera
var workingCamera = ( CinemachineVirtualCamera ) _cameraUpdateSystem.orbitCameraController.virtualCamera;
_virtualCamera.m_Lens = workingCamera.m_Lens;
_virtualCamera.Priority = 0;
_virtualCamera.m_Lens.NearClipPlane = 0.03f;
startFOV = workingCamera.m_Lens.FieldOfView;
_virtualCamera.m_Lens.FieldOfView = startFOV;
CameraTransform.position = _cameraUpdateSystem.activeCamera.transform.position;
Parent.position = CameraTransform.position;
_model.Position = Parent.position;
_model.Rotation = Parent.rotation;
_model.ChildRotation = RigTransform.localRotation;
}
/// <summary>
/// Update the rig transforms, applying linear interpolation
/// if specified and transitions.
/// </summary>
/// <param name="shouldLerp"></param>
public void Update( bool noLerp = false )
{
// Check if transitioning, we dampen the position more for smoothness
if ( _model.IsTransitioningIn || _model.IsTransitioningOut )
{
// Transition to the target position
if ( _model.IsTransitioningIn )
{
Parent.position = Vector3.Lerp( Parent.position, _model.Position, TRANSITION_DAMPEN * Time.deltaTime );
Parent.rotation = Quaternion.Lerp( Parent.rotation, _model.Rotation, TRANSITION_DAMPEN * Time.deltaTime );
RigTransform.localRotation = Quaternion.Lerp( RigTransform.localRotation, _model.ChildRotation, TRANSITION_DAMPEN * Time.deltaTime );
CameraTransform.transform.position = RigTransform.position;
CameraTransform.transform.rotation = RigTransform.rotation;
_virtualCamera.m_Lens.FieldOfView = math.lerp( _virtualCamera.m_Lens.FieldOfView, 70f, TRANSITION_DAMPEN * Time.deltaTime );
// We've arrived
if ( Vector3.Distance( Parent.position, _model.Position ) <= 0.125f )
{
_model.IsTransitioningIn = false;
OnTransitionComplete?.Invoke( false );
}
}
// Transition to the orbit camera
else if ( _model.IsTransitioningOut )
{
Parent.position = Vector3.Lerp( Parent.position, startPosition, TRANSITION_DAMPEN * Time.deltaTime );
Parent.rotation = Quaternion.Lerp( Parent.rotation, startRotation, TRANSITION_DAMPEN * Time.deltaTime );
RigTransform.localRotation = Quaternion.Lerp( RigTransform.localRotation, Quaternion.identity, TRANSITION_DAMPEN * Time.deltaTime );
CameraTransform.transform.position = RigTransform.position;
CameraTransform.transform.rotation = RigTransform.rotation;
_virtualCamera.m_Lens.FieldOfView = math.lerp( _virtualCamera.m_Lens.FieldOfView, startFOV, TRANSITION_DAMPEN * Time.deltaTime );
// We've arrived
if ( Vector3.Distance( Parent.position, startPosition ) <= 0.125f )
{
_model.IsTransitioningOut = false;
_virtualCamera.Priority = 0;
OnTransitionComplete?.Invoke( true );
}
}
}
else
{
if ( !noLerp )
Parent.position = Vector3.Lerp( Parent.position, _model.Position, 10f * Time.deltaTime );
else
Parent.position = _model.Position;
Parent.rotation = Quaternion.Lerp( Parent.rotation, _model.Rotation, 12f * Time.deltaTime );
RigTransform.localRotation = Quaternion.Lerp( RigTransform.localRotation, _model.ChildRotation, 12f * Time.deltaTime );
CameraTransform.transform.position = RigTransform.position;
CameraTransform.transform.rotation = RigTransform.rotation;
}
}
/// <summary>
/// Set the rig to active or disabled
/// </summary>
/// <param name="isActive"></param>
public void SetActive( bool isActive )
{
if ( _model.IsTransitioningIn )
_virtualCamera.Priority = 99999;
//else if ( _model.IsTransitioningOut )
// _virtualCamera.Priority = 0;
// Update base position of FPS camera
if ( isActive )
{
var cam = _cameraUpdateSystem.activeCamera;
startRotation = cam.transform.rotation;
startPosition = cam.transform.position;
Parent.position = startPosition;
_model.Position = startPosition;
}
else
{
//_cameraUpdateSystem.orbitCameraController.rotation = Parent.rotation.eulerAngles;
//_cameraUpdateSystem.orbitCameraController.zoom = _cameraUpdateSystem.orbitCameraController.m_ZoomRange.x;
//_cameraUpdateSystem.orbitCameraController.pivot = Parent.position;
}
}
/// <summary>
/// Update the camera near clip plane
/// </summary>
/// <param name="nearClipPlane"></param>
public void UpdateNearClipPlane( float nearClipPlane = 0.03f )
{
_virtualCamera.m_Lens.NearClipPlane = nearClipPlane;
}
}
}