CinemachineNewFreeLook.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. #if CINEMACHINE_EXPERIMENTAL_VCAM
  2. using UnityEngine;
  3. using Cinemachine.Utility;
  4. using System;
  5. namespace Cinemachine
  6. {
  7. /// <summary>
  8. ///
  9. /// NOTE: THIS CLASS IS EXPERIMENTAL, AND NOT FOR PUBLIC USE
  10. ///
  11. /// Lighter-weight version of the CinemachineFreeLook, with extra radial axis.
  12. ///
  13. /// A Cinemachine Camera geared towards a 3rd person camera experience.
  14. /// The camera orbits around its subject with three separate camera rigs defining
  15. /// rings around the target. Each rig has its own radius, height offset, composer,
  16. /// and lens settings.
  17. /// Depending on the camera's position along the spline connecting these three rigs,
  18. /// these settings are interpolated to give the final camera position and state.
  19. /// </summary>
  20. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  21. [DisallowMultipleComponent]
  22. #if UNITY_2018_3_OR_NEWER
  23. [ExecuteAlways]
  24. #else
  25. [ExecuteInEditMode]
  26. #endif
  27. [AddComponentMenu("Cinemachine/CinemachineNewFreeLook")]
  28. [SaveDuringPlay]
  29. public class CinemachineNewFreeLook : CinemachineNewVirtualCamera
  30. {
  31. /// <summary>The Vertical axis. Value is 0..1. Chooses how to blend the child rigs</summary>
  32. [Tooltip("The Vertical axis. Value is 0..1. 0.5 is the middle rig. Chooses how to blend the child rigs")]
  33. [AxisStateProperty]
  34. public AxisState m_VerticalAxis = new AxisState(0, 1, false, true, 2f, 0.2f, 0.1f, "Mouse Y", false);
  35. [Tooltip("The Radial axis. Value is the base radius of the orbits")]
  36. [AxisStateProperty]
  37. public AxisState m_RadialAxis = new AxisState(1, 1, false, false, 100, 0f, 0f, "Mouse ScrollWheel", false);
  38. /// <summary>Defines the height and radius for an orbit</summary>
  39. [Serializable]
  40. public struct Orbit
  41. {
  42. /// <summary>Height relative to target</summary>
  43. public float m_Height;
  44. /// <summary>Radius of orbit</summary>
  45. public float m_Radius;
  46. }
  47. /// <summary>Order is Top, Middle, Bottom</summary>
  48. public Orbit[] m_Orbits = new Orbit[3];
  49. /// <summary></summary>
  50. [Tooltip("Controls how taut is the line that connects the rigs' orbits, which determines final placement on the Y axis")]
  51. [Range(0f, 1f)]
  52. public float m_SplineCurvature;
  53. /// <summary>Identifiers for accessing override settings for top and bottom rigs</summary>
  54. public enum RigID { Top, Bottom };
  55. /// <summary>Override settings for top and bottom rigs</summary>
  56. [Serializable]
  57. public class Rig
  58. {
  59. public bool m_CustomLens;
  60. public LensSettings m_Lens;
  61. public bool m_CustomBody;
  62. public TransposerSettings m_Body;
  63. public bool m_CustomAim;
  64. public ComposerSettings m_Aim;
  65. public bool m_CustomNoise;
  66. public PerlinNoiseSettings m_Noise;
  67. public void Validate()
  68. {
  69. if (m_Lens.FieldOfView == 0)
  70. m_Lens = LensSettings.Default;
  71. m_Lens.Validate();
  72. }
  73. /// <summary>Blendable settings for Transposer Transposer</summary>
  74. [Serializable] public class TransposerSettings
  75. {
  76. [Range(0f, 20f)] public float m_XDamping;
  77. [Range(0f, 20f)] public float m_YDamping;
  78. [Range(0f, 20f)] public float m_ZDamping;
  79. [Range(0f, 20f)] public float m_PitchDamping;
  80. [Range(0f, 20f)] public float m_YawDamping;
  81. [Range(0f, 20f)] public float m_RollDamping;
  82. internal void Lerp(CinemachineTransposer o, float t)
  83. {
  84. o.m_XDamping = Mathf.Lerp(o.m_XDamping, m_XDamping, t);
  85. o.m_YDamping = Mathf.Lerp(o.m_YDamping, m_YDamping, t);
  86. o.m_ZDamping = Mathf.Lerp(o.m_ZDamping, m_ZDamping, t);
  87. o.m_PitchDamping = Mathf.Lerp(o.m_PitchDamping, m_PitchDamping, t);
  88. o.m_YawDamping = Mathf.Lerp(o.m_YawDamping, m_YawDamping, t);
  89. o.m_RollDamping = Mathf.Lerp(o.m_RollDamping, m_RollDamping, t);
  90. }
  91. internal void PullFrom(CinemachineTransposer o)
  92. {
  93. m_XDamping = o.m_XDamping;
  94. m_YDamping = o.m_YDamping;
  95. m_ZDamping = o.m_ZDamping;
  96. m_PitchDamping = o.m_PitchDamping;
  97. m_YawDamping = o.m_YawDamping;
  98. m_RollDamping = o.m_RollDamping;
  99. }
  100. internal void PushTo(CinemachineTransposer o)
  101. {
  102. o.m_XDamping = m_XDamping;
  103. o.m_YDamping = m_YDamping;
  104. o.m_ZDamping = m_ZDamping;
  105. o.m_PitchDamping =m_PitchDamping;
  106. o.m_YawDamping = m_YawDamping;
  107. o.m_RollDamping = m_RollDamping;
  108. }
  109. }
  110. /// <summary>Blendable settings for Composer</summary>
  111. [Serializable] public class ComposerSettings
  112. {
  113. public Vector3 m_LookAtOffset;
  114. [Space]
  115. [Range(0f, 20)] public float m_HorizontalDamping;
  116. [Range(0f, 20)] public float m_VerticalDamping;
  117. [Space]
  118. [Range(0f, 1f)] public float m_ScreenX;
  119. [Range(0f, 1f)] public float m_ScreenY;
  120. [Range(0f, 1f)] public float m_DeadZoneWidth;
  121. [Range(0f, 1f)] public float m_DeadZoneHeight;
  122. [Range(0f, 2f)] public float m_SoftZoneWidth;
  123. [Range(0f, 2f)] public float m_SoftZoneHeight;
  124. [Range(-0.5f, 0.5f)] public float m_BiasX;
  125. [Range(-0.5f, 0.5f)] public float m_BiasY;
  126. internal void Lerp(CinemachineComposer c, float t)
  127. {
  128. c.m_TrackedObjectOffset = Vector3.Lerp(c.m_TrackedObjectOffset, m_LookAtOffset, t);
  129. c.m_HorizontalDamping = Mathf.Lerp(c.m_HorizontalDamping, m_HorizontalDamping, t);
  130. c.m_VerticalDamping = Mathf.Lerp(c.m_VerticalDamping, m_VerticalDamping, t);
  131. c.m_ScreenX = Mathf.Lerp(c.m_ScreenX, m_ScreenX, t);
  132. c.m_ScreenY = Mathf.Lerp(c.m_ScreenY, m_ScreenY, t);
  133. c.m_DeadZoneWidth = Mathf.Lerp(c.m_DeadZoneWidth, m_DeadZoneWidth, t);
  134. c.m_DeadZoneHeight = Mathf.Lerp(c.m_DeadZoneHeight, m_DeadZoneHeight, t);
  135. c.m_SoftZoneWidth = Mathf.Lerp(c.m_SoftZoneWidth, m_SoftZoneWidth, t);
  136. c.m_SoftZoneHeight = Mathf.Lerp(c.m_SoftZoneHeight, m_SoftZoneHeight, t);
  137. c.m_BiasX = Mathf.Lerp(c.m_BiasX, m_BiasX, t);
  138. c.m_BiasY = Mathf.Lerp(c.m_BiasY, m_BiasY, t);
  139. }
  140. internal void PullFrom(CinemachineComposer c)
  141. {
  142. m_LookAtOffset = c.m_TrackedObjectOffset;
  143. m_HorizontalDamping = c.m_HorizontalDamping;
  144. m_VerticalDamping = c.m_VerticalDamping;
  145. m_ScreenX = c.m_ScreenX;
  146. m_ScreenY = c.m_ScreenY;
  147. m_DeadZoneWidth = c.m_DeadZoneWidth;
  148. m_DeadZoneHeight = c.m_DeadZoneHeight;
  149. m_SoftZoneWidth = c.m_SoftZoneWidth;
  150. m_SoftZoneHeight = c.m_SoftZoneHeight;
  151. m_BiasX = c.m_BiasX;
  152. m_BiasY = c.m_BiasY;
  153. }
  154. internal void PushTo(CinemachineComposer c)
  155. {
  156. c.m_TrackedObjectOffset = m_LookAtOffset;
  157. c.m_HorizontalDamping = m_HorizontalDamping;
  158. c.m_VerticalDamping = m_VerticalDamping;
  159. c.m_ScreenX = m_ScreenX;
  160. c.m_ScreenY = m_ScreenY;
  161. c.m_DeadZoneWidth = m_DeadZoneWidth;
  162. c.m_DeadZoneHeight = m_DeadZoneHeight;
  163. c.m_SoftZoneWidth = m_SoftZoneWidth;
  164. c.m_SoftZoneHeight = m_SoftZoneHeight;
  165. c.m_BiasX = m_BiasX;
  166. c.m_BiasY = m_BiasY;
  167. }
  168. }
  169. /// <summary>Blendable settings for CinemachineBasicMultiChannelPerlin</summary>
  170. [Serializable] public class PerlinNoiseSettings
  171. {
  172. public float m_AmplitudeGain;
  173. public float m_FrequencyGain;
  174. internal void Lerp(CinemachineBasicMultiChannelPerlin p, float t)
  175. {
  176. p.m_AmplitudeGain = Mathf.Lerp(p.m_AmplitudeGain, m_AmplitudeGain, t);
  177. p.m_FrequencyGain =Mathf.Lerp(p.m_FrequencyGain, m_FrequencyGain, t);
  178. }
  179. internal void PullFrom(CinemachineBasicMultiChannelPerlin p)
  180. {
  181. m_AmplitudeGain = p.m_AmplitudeGain;
  182. m_FrequencyGain = p.m_FrequencyGain;
  183. }
  184. internal void PushTo(CinemachineBasicMultiChannelPerlin p)
  185. {
  186. p.m_AmplitudeGain = m_AmplitudeGain;
  187. p.m_FrequencyGain = m_FrequencyGain;
  188. }
  189. }
  190. }
  191. [SerializeField]
  192. internal Rig[] m_Rigs = new Rig[2] { new Rig(), new Rig() };
  193. /// <summary>Accessor for rig override settings</summary>
  194. public Rig RigSettings(RigID rig) { return m_Rigs[(int)rig]; }
  195. /// Easy access to the transposer (may be null)
  196. CinemachineTransposer Transposer
  197. {
  198. get { return ComponentCache[(int)CinemachineCore.Stage.Body] as CinemachineTransposer; }
  199. }
  200. /// <summary>Enforce bounds for fields, when changed in inspector.</summary>
  201. protected override void OnValidate()
  202. {
  203. base.OnValidate();
  204. for (int i = 0; i < m_Rigs.Length; ++i)
  205. m_Rigs[i].Validate();
  206. }
  207. private void Awake()
  208. {
  209. m_VerticalAxis.HasRecentering = true;
  210. m_RadialAxis.HasRecentering = false;
  211. }
  212. /// <summary>Updates the child rig cache</summary>
  213. protected override void OnEnable()
  214. {
  215. base.OnEnable();
  216. UpdateInputAxisProvider();
  217. }
  218. /// <summary>
  219. /// API for the inspector. Internal use only
  220. /// </summary>
  221. public void UpdateInputAxisProvider()
  222. {
  223. m_VerticalAxis.SetInputAxisProvider(0, null);
  224. m_RadialAxis.SetInputAxisProvider(1, null);
  225. var provider = GetInputAxisProvider();
  226. if (provider != null)
  227. {
  228. m_VerticalAxis.SetInputAxisProvider(1, provider);
  229. m_RadialAxis.SetInputAxisProvider(2, provider);
  230. }
  231. }
  232. void Reset()
  233. {
  234. DestroyComponents();
  235. #if UNITY_EDITOR
  236. var orbital = UnityEditor.Undo.AddComponent<CinemachineOrbitalTransposer>(gameObject);
  237. UnityEditor.Undo.AddComponent<CinemachineComposer>(gameObject);
  238. #else
  239. var orbital = gameObject.AddComponent<CinemachineOrbitalTransposer>();
  240. gameObject.AddComponent<CinemachineComposer>();
  241. #endif
  242. orbital.HideOffsetInInspector = true;
  243. orbital.m_BindingMode = CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp;
  244. InvalidateComponentCache();
  245. m_Rigs = new Rig[2] { new Rig(), new Rig() };
  246. // Default orbits
  247. m_Orbits = new Orbit[3];
  248. m_Orbits[0].m_Height = 10; m_Orbits[0].m_Radius = 4;
  249. m_Orbits[1].m_Height = 2.5f; m_Orbits[1].m_Radius = 8;
  250. m_Orbits[2].m_Height = -0.5f; m_Orbits[2].m_Radius = 5;
  251. m_SplineCurvature = 0.5f;
  252. }
  253. /// <summary>
  254. /// Force the virtual camera to assume a given position and orientation.
  255. /// Procedural placement then takes over
  256. /// </summary>
  257. /// <param name="pos">Worldspace pposition to take</param>
  258. /// <param name="rot">Worldspace orientation to take</param>
  259. public override void ForceCameraPosition(Vector3 pos, Quaternion rot)
  260. {
  261. base.ForceCameraPosition(pos, rot);
  262. m_VerticalAxis.Value = GetYAxisClosestValue(pos, State.ReferenceUp);
  263. }
  264. /// <summary>If we are transitioning from another FreeLook, grab the axis values from it.</summary>
  265. /// <param name="fromCam">The camera being deactivated. May be null.</param>
  266. /// <param name="worldUp">Default world Up, set by the CinemachineBrain</param>
  267. /// <param name="deltaTime">Delta time for time-based effects (ignore if less than or equal to 0)</param>
  268. public override void OnTransitionFromCamera(
  269. ICinemachineCamera fromCam, Vector3 worldUp, float deltaTime)
  270. {
  271. base.OnTransitionFromCamera(fromCam, worldUp, deltaTime);
  272. InvokeOnTransitionInExtensions(fromCam, worldUp, deltaTime);
  273. m_VerticalAxis.m_Recentering.DoRecentering(ref m_VerticalAxis, -1, 0.5f);
  274. m_VerticalAxis.m_Recentering.CancelRecentering();
  275. if (fromCam != null && m_Transitions.m_InheritPosition)
  276. {
  277. // Note: horizontal axis already taken care of by base class
  278. var cameraPos = fromCam.State.RawPosition;
  279. // Special handling for FreeLook: get an undamped outgoing position
  280. if (fromCam is CinemachineNewFreeLook)
  281. {
  282. var orbital = (fromCam as CinemachineNewFreeLook).Transposer;
  283. if (orbital != null)
  284. cameraPos = orbital.GetTargetCameraPosition(worldUp);
  285. }
  286. m_VerticalAxis.Value = GetYAxisClosestValue(cameraPos, worldUp);
  287. }
  288. }
  289. float GetYAxisClosestValue(Vector3 cameraPos, Vector3 up)
  290. {
  291. if (Follow != null)
  292. {
  293. // Rotate the camera pos to the back
  294. Quaternion q = Quaternion.FromToRotation(up, Vector3.up);
  295. Vector3 dir = q * (cameraPos - Follow.position);
  296. Vector3 flatDir = dir; flatDir.y = 0;
  297. if (!flatDir.AlmostZero())
  298. {
  299. float angle = Vector3.SignedAngle(flatDir, Vector3.back, Vector3.up);
  300. dir = Quaternion.AngleAxis(angle, Vector3.up) * dir;
  301. }
  302. dir.x = 0;
  303. // Sample the spline in a few places, find the 2 closest, and lerp
  304. int i0 = 0, i1 = 0;
  305. float a0 = 0, a1 = 0;
  306. const int NumSamples = 13;
  307. float step = 1f / (NumSamples-1);
  308. for (int i = 0; i < NumSamples; ++i)
  309. {
  310. float a = Vector3.SignedAngle(
  311. dir, GetLocalPositionForCameraFromInput(i * step), Vector3.right);
  312. if (i == 0)
  313. a0 = a1 = a;
  314. else
  315. {
  316. if (Mathf.Abs(a) < Mathf.Abs(a0))
  317. {
  318. a1 = a0;
  319. i1 = i0;
  320. a0 = a;
  321. i0 = i;
  322. }
  323. else if (Mathf.Abs(a) < Mathf.Abs(a1))
  324. {
  325. a1 = a;
  326. i1 = i;
  327. }
  328. }
  329. }
  330. if (Mathf.Sign(a0) == Mathf.Sign(a1))
  331. return i0 * step;
  332. float t = Mathf.Abs(a0) / (Mathf.Abs(a0) + Mathf.Abs(a1));
  333. return Mathf.Lerp(i0 * step, i1 * step, t);
  334. }
  335. return m_VerticalAxis.Value; // stay conservative
  336. }
  337. /// <summary>Internal use only. Called by CinemachineCore at designated update time
  338. /// so the vcam can position itself and track its targets. All 3 child rigs are updated,
  339. /// and a blend calculated, depending on the value of the Y axis.</summary>
  340. /// <param name="worldUp">Default world Up, set by the CinemachineBrain</param>
  341. /// <param name="deltaTime">Delta time for time-based effects (ignore if less than 0)</param>
  342. override public void InternalUpdateCameraState(Vector3 worldUp, float deltaTime)
  343. {
  344. FollowTargetAttachment = 1;
  345. LookAtTargetAttachment = 1;
  346. // Initialize the camera state, in case the game object got moved in the editor
  347. m_State = PullStateFromVirtualCamera(worldUp, ref m_Lens);
  348. m_Rigs[(int)RigID.Top].m_Lens.SnapshotCameraReadOnlyProperties(ref m_Lens);
  349. m_Rigs[(int)RigID.Bottom].m_Lens.SnapshotCameraReadOnlyProperties(ref m_Lens);
  350. // Update our axes
  351. bool activeCam = PreviousStateIsValid || CinemachineCore.Instance.IsLive(this);
  352. if (!activeCam || deltaTime < 0)
  353. m_VerticalAxis.m_Recentering.DoRecentering(ref m_VerticalAxis, -1, 0.5f);
  354. else
  355. {
  356. if (m_VerticalAxis.Update(deltaTime))
  357. m_VerticalAxis.m_Recentering.CancelRecentering();
  358. m_RadialAxis.Update(deltaTime);
  359. m_VerticalAxis.m_Recentering.DoRecentering(ref m_VerticalAxis, deltaTime, 0.5f);
  360. }
  361. // Blend the components
  362. if (mBlender == null)
  363. mBlender = new ComponentBlender(this);
  364. mBlender.Blend(GetVerticalAxisValue());
  365. // Blend the lens
  366. if (m_Rigs[mBlender.OtherRig].m_CustomLens)
  367. m_State.Lens = LensSettings.Lerp(
  368. m_State.Lens, m_Rigs[mBlender.OtherRig].m_Lens, mBlender.BlendAmount);
  369. // Do our stuff
  370. SetReferenceLookAtTargetInState(ref m_State);
  371. InvokeComponentPipeline(ref m_State, worldUp, deltaTime);
  372. ApplyPositionBlendMethod(ref m_State, m_Transitions.m_BlendHint);
  373. // Restore the components
  374. mBlender.Restore();
  375. // Push the raw position back to the game object's transform, so it
  376. // moves along with the camera.
  377. if (!UserIsDragging)
  378. {
  379. if (Follow != null)
  380. transform.position = State.RawPosition;
  381. if (LookAt != null)
  382. transform.rotation = State.RawOrientation;
  383. }
  384. // Signal that it's all done
  385. InvokePostPipelineStageCallback(this, CinemachineCore.Stage.Finalize, ref m_State, deltaTime);
  386. PreviousStateIsValid = true;
  387. }
  388. ComponentBlender mBlender;
  389. protected override void OnComponentCacheUpdated()
  390. {
  391. var transposer = Transposer;
  392. if (transposer != null)
  393. {
  394. transposer.HideOffsetInInspector = true;
  395. transposer.m_FollowOffset = new Vector3(
  396. 0, m_Orbits[1].m_Height, -m_Orbits[1].m_Radius);
  397. }
  398. }
  399. private float GetVerticalAxisValue()
  400. {
  401. float range = m_VerticalAxis.m_MaxValue - m_VerticalAxis.m_MinValue;
  402. return (range > UnityVectorExtensions.Epsilon) ? m_VerticalAxis.Value / range : 0.5f;
  403. }
  404. /// <summary>
  405. /// Returns the local position of the camera along the spline used to connect the
  406. /// three camera rigs. Does not take into account the current heading of the
  407. /// camera (or its target)
  408. /// </summary>
  409. /// <param name="t">The t-value for the camera on its spline. Internally clamped to
  410. /// the value [0,1]</param>
  411. /// <returns>The local offset (back + up) of the camera WRT its target based on the
  412. /// supplied t-value</returns>
  413. public Vector3 GetLocalPositionForCameraFromInput(float t)
  414. {
  415. UpdateCachedSpline();
  416. int n = 1;
  417. if (t > 0.5f)
  418. {
  419. t -= 0.5f;
  420. n = 2;
  421. }
  422. Vector3 pos = SplineHelpers.Bezier3(
  423. t * 2f, m_CachedKnots[n], m_CachedCtrl1[n], m_CachedCtrl2[n], m_CachedKnots[n+1]);
  424. pos *= Mathf.Max(0, m_RadialAxis.Value);
  425. return pos;
  426. }
  427. Vector2[] m_CachedOrbits;
  428. float m_CachedTension;
  429. Vector4[] m_CachedKnots;
  430. Vector4[] m_CachedCtrl1;
  431. Vector4[] m_CachedCtrl2;
  432. void UpdateCachedSpline()
  433. {
  434. bool cacheIsValid = (m_CachedOrbits != null && m_CachedTension == m_SplineCurvature);
  435. for (int i = 0; i < 3 && cacheIsValid; ++i)
  436. cacheIsValid = (m_CachedOrbits[i].y == m_Orbits[i].m_Height
  437. && m_CachedOrbits[i].x == m_Orbits[i].m_Radius);
  438. if (!cacheIsValid)
  439. {
  440. float t = m_SplineCurvature;
  441. m_CachedKnots = new Vector4[5];
  442. m_CachedCtrl1 = new Vector4[5];
  443. m_CachedCtrl2 = new Vector4[5];
  444. m_CachedKnots[1] = new Vector4(0, m_Orbits[2].m_Height, -m_Orbits[2].m_Radius, 0);
  445. m_CachedKnots[2] = new Vector4(0, m_Orbits[1].m_Height, -m_Orbits[1].m_Radius, 0);
  446. m_CachedKnots[3] = new Vector4(0, m_Orbits[0].m_Height, -m_Orbits[0].m_Radius, 0);
  447. m_CachedKnots[0] = Vector4.Lerp(m_CachedKnots[0], Vector4.zero, t);
  448. m_CachedKnots[4] = Vector4.Lerp(m_CachedKnots[3], Vector4.zero, t);
  449. SplineHelpers.ComputeSmoothControlPoints(
  450. ref m_CachedKnots, ref m_CachedCtrl1, ref m_CachedCtrl2);
  451. m_CachedOrbits = new Vector2[3];
  452. for (int i = 0; i < 3; ++i)
  453. m_CachedOrbits[i] = new Vector2(m_Orbits[i].m_Radius, m_Orbits[i].m_Height);
  454. m_CachedTension = m_SplineCurvature;
  455. }
  456. }
  457. // Crazy damn thing for blending components at the source level
  458. internal class ComponentBlender
  459. {
  460. Rig.TransposerSettings orbitalSaved = new Rig.TransposerSettings();
  461. Rig.ComposerSettings composerSaved = new Rig.ComposerSettings();
  462. Rig.PerlinNoiseSettings noiseSaved = new Rig.PerlinNoiseSettings();
  463. public int OtherRig { get; set; }
  464. public float BlendAmount { get; set; }
  465. CinemachineNewFreeLook mFreeLook;
  466. public ComponentBlender(CinemachineNewFreeLook freeLook) { mFreeLook = freeLook; }
  467. public void Blend(float y)
  468. {
  469. if (y < 0.5f)
  470. {
  471. BlendAmount = 1 - (y * 2);
  472. OtherRig = (int)RigID.Bottom;
  473. }
  474. else
  475. {
  476. BlendAmount = (y - 0.5f) * 2f;
  477. OtherRig = (int)RigID.Top;
  478. }
  479. var orbital = mFreeLook.Transposer;
  480. if (orbital != null && mFreeLook.m_Rigs[OtherRig].m_CustomBody)
  481. {
  482. orbitalSaved.PullFrom(orbital);
  483. mFreeLook.m_Rigs[OtherRig].m_Body.Lerp(orbital, BlendAmount);
  484. }
  485. if (orbital != null)
  486. orbital.m_FollowOffset = mFreeLook.GetLocalPositionForCameraFromInput(y);
  487. var components = mFreeLook.ComponentCache;
  488. var composer = components[(int)CinemachineCore.Stage.Aim] as CinemachineComposer;
  489. if (composer != null && mFreeLook.m_Rigs[OtherRig].m_CustomAim)
  490. {
  491. composerSaved.PullFrom(composer);
  492. mFreeLook.m_Rigs[OtherRig].m_Aim.Lerp(composer, BlendAmount);
  493. }
  494. var noise = components[(int)CinemachineCore.Stage.Noise] as CinemachineBasicMultiChannelPerlin;
  495. if (noise != null && mFreeLook.m_Rigs[OtherRig].m_CustomNoise)
  496. {
  497. noiseSaved.PullFrom(noise);
  498. mFreeLook.m_Rigs[OtherRig].m_Noise.Lerp(noise, BlendAmount);
  499. }
  500. }
  501. public void Restore()
  502. {
  503. var orbital = mFreeLook.Transposer;
  504. if (orbital != null && mFreeLook.m_Rigs[OtherRig].m_CustomBody)
  505. orbitalSaved.PushTo(orbital);
  506. if (orbital != null)
  507. orbital.m_FollowOffset = new Vector3(
  508. 0, mFreeLook.m_Orbits[1].m_Height, -mFreeLook.m_Orbits[1].m_Radius);
  509. var components = mFreeLook.ComponentCache;
  510. var composer = components[(int)CinemachineCore.Stage.Aim] as CinemachineComposer;
  511. if (composer != null && mFreeLook.m_Rigs[OtherRig].m_CustomAim)
  512. composerSaved.PushTo(composer);
  513. var noise = components[(int)CinemachineCore.Stage.Noise] as CinemachineBasicMultiChannelPerlin;
  514. if (noise != null && mFreeLook.m_Rigs[OtherRig].m_CustomNoise)
  515. noiseSaved.PushTo(noise);
  516. }
  517. }
  518. }
  519. }
  520. #endif