void UpdateComponentEditors(CinemachineVirtualCameraBase obj) { if (m_cachedReferenceObject != obj) { DestroyComponentEditors(); m_cachedReferenceObject = obj; if (obj != null) { MonoBehaviour[] components = obj.gameObject.GetComponents <MonoBehaviour>(); m_editors = new UnityEditor.Editor[components.Length + 1]; CreateCachedEditor(obj.gameObject.GetComponent <Transform>(), null, ref m_editors[0]); for (int i = 0; i < components.Length; ++i) { CreateCachedEditor(components[i], null, ref m_editors[i + 1]); } } } }
private int GetCameraIndex(Object obj) { if (obj == null || mCameraIndexLookup == null) { return(0); } CinemachineVirtualCameraBase vcam = obj as CinemachineVirtualCameraBase; if (vcam == null) { return(0); } if (!mCameraIndexLookup.ContainsKey(vcam)) { return(0); } return(mCameraIndexLookup[vcam]); }
protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (stage == CinemachineCore.Stage.Body) { var position = state.RawPosition; position.x = 0.0f; state.RawPosition = position; } if (stage == CinemachineCore.Stage.Noise) { var noise = (vcam as CinemachineVirtualCamera).GetCinemachineComponent <CinemachineBasicMultiChannelPerlin>(); var rigidbody = vcam.Follow.GetComponent <Rigidbody2D>(); noise.m_AmplitudeGain = ((rigidbody.velocity - m_velocity).y / Time.fixedDeltaTime) * Time.deltaTime; m_velocity = rigidbody.velocity; } }
CinemachineVirtualCameraBase[] Randomize(CinemachineVirtualCameraBase[] src) { List <Pair> pairs = new List <Pair>(); for (int i = 0; i < src.Length; ++i) { Pair p = new Pair(); p.a = i; p.b = Random.Range(0, 1000f); pairs.Add(p); } pairs.Sort((p1, p2) => (int)p1.b - (int)p2.b); CinemachineVirtualCameraBase[] dst = new CinemachineVirtualCameraBase[src.Length]; Pair[] result = pairs.ToArray(); for (int i = 0; i < src.Length; ++i) { dst[i] = src[result[i].a]; } return(dst); }
// 4 protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { // 5 if (stage == CinemachineCore.Stage.Body) { // 6 Vector3 pos = state.FinalPosition; // 7 Vector3 pos2 = new Vector3(Round(pos.x), Round(pos.y), pos.z); // 8 state.PositionCorrection += pos2 - pos; } }
public override Playable CreateTrackMixer( PlayableGraph graph, GameObject go, int inputCount) { foreach (var c in GetClips()) { CinemachineShot shot = (CinemachineShot)c.asset; CinemachineVirtualCameraBase vcam = shot.VirtualCamera.Resolve(graph.GetResolver()); if (vcam != null) { c.displayName = vcam.Name; } } var mixer = ScriptPlayable <CinemachineMixer> .Create(graph); mixer.SetInputCount(inputCount); return(mixer); }
protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (vcam.Follow) { if (stage == CinemachineCore.Stage.Aim) { if (startRotatation == null) { startRotatation = transform.localRotation.eulerAngles; } Vector2 deltaInput = new Vector2(x_input, y_input); startRotatation.x += deltaInput.x * xSensitivity * Time.deltaTime; startRotatation.y += deltaInput.y * ySensitivity * Time.deltaTime; startRotatation.y = Mathf.Clamp(startRotatation.y, -maxAngle, maxAngle); state.RawOrientation = Quaternion.Euler(-startRotatation.y, startRotatation.x, wallRunCameraTilt); } } }
protected virtual void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (stage != 1) { return; } if (!this.IsValid) { this.DestroyProfileCopy(); } else { if (!this.m_FocusTracksTarget) { this.DestroyProfileCopy(); } else { if (Object.op_Equality((Object)this.mProfileCopy, (Object)null) || this.mCachedProfileIsInvalid) { this.CreateProfileCopy(); } DepthOfField depthOfField; // ISSUE: cast to a reference type if (this.mProfileCopy.TryGetSettings <DepthOfField>((M0&)ref depthOfField)) { float num1 = this.m_FocusOffset; if (((CameraState) ref state).get_HasLookAt()) { double num2 = (double)num1; Vector3 vector3 = Vector3.op_Subtraction(((CameraState) ref state).get_FinalPosition(), ((CameraState) ref state).get_ReferenceLookAt()); double magnitude = (double)((Vector3) ref vector3).get_magnitude(); num1 = (float)(num2 + magnitude); } ((ParameterOverride <float>)depthOfField.focusDistance).value = (__Null)(double)Mathf.Max(0.0f, num1); } } ((CameraState) ref state).AddCustomBlendable(new CameraState.CustomBlendable((Object)this, 1f)); } }
protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { //UnityEngine.Profiling.Profiler.BeginSample("CinemachinePostProcessing.PostPipelineStageCallback"); // Set the focus after the camera has been fully positioned. // GML todo: what about collider? if (stage == CinemachineCore.Stage.Aim) { if (!IsValid) { DestroyProfileCopy(); } else { // Handle Follow Focus if (!m_FocusTracksTarget) { DestroyProfileCopy(); } else { if (mProfileCopy == null || mCachedProfileIsInvalid) { CreateProfileCopy(); } DepthOfField dof; if (mProfileCopy.TryGetSettings(out dof)) { float focusDistance = m_FocusOffset; if (state.HasLookAt) { focusDistance += (state.FinalPosition - state.ReferenceLookAt).magnitude; } dof.focusDistance.value = Mathf.Max(0, focusDistance); } } // Apply the post-processing state.AddCustomBlendable(new CameraState.CustomBlendable(this, 1)); } } //UnityEngine.Profiling.Profiler.EndSample(); }
public override bool SetCameraState(string stateName) { bool stateSet = false; int count = m_virtualCameras.Length; // First, loop through virtual cameras and find the virtual camera. for (int i = 0; i < count; i++) { if (m_virtualCameras[i].StateName == stateName) { activeCameraIndex = i; m_activeState = m_virtualCameras[activeCameraIndex].VirtualCamera; m_activeState.gameObject.SetActive(true); // Swap positions with top of list. var temp = m_virtualCameras[0]; m_virtualCameras[0] = m_virtualCameras[i]; m_virtualCameras[i] = temp; stateSet = true; break; } } if (stateSet) { int priority = 0; for (int i = count - 1; i > 0; i--) { if (i == 0) { break; } //if (m_virtualCameras[i].VirtualCamera == m_activeState) continue; m_virtualCameras[i].VirtualCamera.Priority = priority; m_virtualCameras[i].VirtualCamera.gameObject.SetActive(false); priority++; } } return(stateSet); }
protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (stage == CinemachineCore.Stage.Body) { var pos = state.RawPosition; if (ignoreX) { pos.x = initialXPosition; } if (ignoreY) { pos.y = initialYPosition; } state.RawPosition = pos; } }
protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { //var extra = GetExtraState<VcamExtraState>(vcam); if (!enabled || deltaTime < 0) { return; } if (stage == CinemachineCore.Stage.Noise) { state.PositionCorrection = new Vector3(0f, positionYNoise.Evaluate(stepTime) * blurMultiply, 0f) ; state.OrientationCorrection = Quaternion.Euler(0f, rotationYNoise.Evaluate(stepTime) * blurMultiply, rotationZNoise.Evaluate(stepTime) * blurMultiply) ; } }
protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime ) { if (vcam.Follow == null) { return; } if (stage != CinemachineCore.Stage.Aim) { return; } if (inputs == null) { return; } if (startingRot == null) { startingRot = transform.localRotation.eulerAngles; } var deltaRot = inputs.Look * Time.deltaTime; startingRot.x += deltaRot.x; if (invertY) { startingRot.y += deltaRot.y; } else { startingRot.y -= deltaRot.y; } startingRot.y = Mathf.Clamp(startingRot.y, -clampViewY, clampViewY); state.RawOrientation = Quaternion.Euler(new Vector3(startingRot.y, startingRot.x, 0f)); }
public override void OnInspectorGUI() { CinemachineVirtualCameraBase obj = mVirtualCameraProperty.exposedReferenceValue as CinemachineVirtualCameraBase; if (obj == null) { serializedObject.Update(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(mVirtualCameraProperty, kVirtualCameraLabel, GUILayout.ExpandWidth(true)); obj = mVirtualCameraProperty.exposedReferenceValue as CinemachineVirtualCameraBase; if ((obj == null) && GUILayout.Button(new GUIContent("Create"), GUILayout.ExpandWidth(false))) { CinemachineVirtualCameraBase vcam = CinemachineMenu.CreateDefaultVirtualCamera(); mVirtualCameraProperty.exposedReferenceValue = vcam; } EditorGUILayout.EndHorizontal(); serializedObject.ApplyModifiedProperties(); } else { serializedObject.Update(); DrawPropertiesExcluding(serializedObject, m_excludeFields); // Create an editor for each of the cinemachine virtual cam and its components UpdateComponentEditors(obj); if (m_editors != null) { foreach (UnityEditor.Editor e in m_editors) { EditorGUILayout.Separator(); if (e.target.GetType() != typeof(Transform)) { GUILayout.Box("", new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(1) }); EditorGUILayout.LabelField(e.target.GetType().Name, EditorStyles.boldLabel); } e.OnInspectorGUI(); } } serializedObject.ApplyModifiedProperties(); } }
protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (stage == m_ApplyAfter) { bool preserveAim = m_PreserveComposition && state.HasLookAt && stage > CinemachineCore.Stage.Body; Vector3 screenOffset = Vector2.zero; if (preserveAim) { screenOffset = state.RawOrientation.GetCameraRotationToTarget( state.ReferenceLookAt - state.CorrectedPosition, state.ReferenceUp); } Vector3 offset = m_Offset; if (m_ScreenProportion) { float dis = (state.ReferenceLookAt - transform.position).magnitude - offset.z; float disY = Mathf.Tan(25 * Mathf.Deg2Rad) * dis; float disX = disY * Screen.width / Screen.height; offset = new Vector3(disX * m_Offset.x, disY * m_Offset.y, m_Offset.z); } offset = state.RawOrientation * offset; state.PositionCorrection += offset; if (!preserveAim) { //state.ReferenceLookAt += offset; } else { var q = Quaternion.LookRotation( state.ReferenceLookAt - state.CorrectedPosition, state.ReferenceUp); q = q.ApplyCameraRotation(-screenOffset, state.ReferenceUp); state.RawOrientation = q; } m_AutoAdjust.AutoAdjustOffset(vcam, state, deltaTime, ref m_Offset); } }
/// <summary> /// TrackAsset implementation /// </summary> /// <param name="graph"></param> /// <param name="go"></param> /// <param name="inputCount"></param> /// <returns></returns> public override Playable CreateTrackMixer( PlayableGraph graph, GameObject go, int inputCount) { #if !UNITY_2019_2_OR_NEWER // Hack to set the display name of the clip to match the vcam foreach (var c in GetClips()) { CinemachineShot shot = (CinemachineShot)c.asset; CinemachineVirtualCameraBase vcam = shot.VirtualCamera.Resolve(graph.GetResolver()); if (vcam != null) { c.displayName = vcam.Name; } } #endif var mixer = ScriptPlayable <CinemachineMixer> .Create(graph); mixer.SetInputCount(inputCount); return(mixer); }
#pragma warning disable CS0115 // 'CinemachineTrack.CreateTrackMixer(PlayableGraph, GameObject, int)': no suitable method found to override public override Playable CreateTrackMixer( #pragma warning restore CS0115 // 'CinemachineTrack.CreateTrackMixer(PlayableGraph, GameObject, int)': no suitable method found to override PlayableGraph graph, GameObject go, int inputCount) { // Hack to set the display name of the clip to match the vcam foreach (var c in GetClips()) { CinemachineShot shot = (CinemachineShot)c.asset; CinemachineVirtualCameraBase vcam = shot.VirtualCamera.Resolve(graph.GetResolver()); if (vcam != null) { c.displayName = vcam.Name; } } var mixer = ScriptPlayable <CinemachineMixer> .Create(graph); mixer.SetInputCount(inputCount); return(mixer); }
static public CinemachineVirtualCameraBase CreateStaticVcamFromSceneView() { CinemachineVirtualCameraBase vcam = CinemachineMenu.CreateStaticVirtualCamera(); vcam.m_StandbyUpdate = CinemachineVirtualCameraBase.StandbyUpdateMode.Never; #if false // GML this is too bold. What if timeline is a child of something moving? // also, SetActive(false) prevents the animator from being able to animate the object vcam.gameObject.SetActive(false); #if UNITY_2018_3_OR_NEWER var d = TimelineEditor.inspectedDirector; if (d != null) { Undo.SetTransformParent(vcam.transform, d.transform, ""); } #endif #endif return(vcam); }
protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (stage == CinemachineCore.Stage.Body) { //if (Input.GetKeyDown("a")) //{ // transform.eulerAngles += new Vector3(0f, -5f, 0f); //} //if (Input.GetKeyDown("d")) //{ // transform.eulerAngles += new Vector3(0f, +5f, 0f); //} //var pos = state.RawPosition; //pos.y = m_YPosition; //pos.x = m_XPosition; //state.RawPosition = pos; } }
/// <summary>Callback to preform the zoom adjustment</summary> protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { VcamExtraState extra = GetExtraState <VcamExtraState>(vcam); if (!enabled || deltaTime < 0) { extra.m_previousFrameZoom = state.Lens.FieldOfView; } // Set the zoom after the body has been positioned, but before the aim, // so that composer can compose using the updated fov. if (stage == CinemachineCore.Stage.Body) { // Try to reproduce the target width float targetWidth = Mathf.Max(m_Width, 0); float fov = 179f; float d = Vector3.Distance(state.CorrectedPosition, state.ReferenceLookAt); if (d > UnityVectorExtensions.Epsilon) { // Clamp targetWidth to FOV min/max float minW = d * 2f * Mathf.Tan(m_MinFOV * Mathf.Deg2Rad / 2f); float maxW = d * 2f * Mathf.Tan(m_MaxFOV * Mathf.Deg2Rad / 2f); targetWidth = Mathf.Clamp(targetWidth, minW, maxW); // Apply damping if (deltaTime >= 0 && m_Damping > 0) { float currentWidth = d * 2f * Mathf.Tan(extra.m_previousFrameZoom * Mathf.Deg2Rad / 2f); float delta = targetWidth - currentWidth; delta = Damper.Damp(delta, m_Damping, deltaTime); targetWidth = currentWidth + delta; } fov = 2f * Mathf.Atan(targetWidth / (2 * d)) * Mathf.Rad2Deg; } LensSettings lens = state.Lens; lens.FieldOfView = extra.m_previousFrameZoom = Mathf.Clamp(fov, m_MinFOV, m_MaxFOV); state.Lens = lens; } }
/// <summary> /// Updates FadeOut shader on the specified FadeOutMaterial. /// </summary> /// <param name="vcam">The virtual camera being processed</param> /// <param name="stage">The current pipeline stage</param> /// <param name="state">The current virtual camera state</param> /// <param name="deltaTime">The current applicable deltaTime</param> protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (stage == CinemachineCore.Stage.Finalize) { if (m_FadeOutMaterial == null || !m_FadeOutMaterial.HasProperty(k_MaxDistanceID) || !m_FadeOutMaterial.HasProperty(k_MinDistanceID)) { return; } if (m_SetToCameraToLookAtDistance && vcam.LookAt != null) { m_MaxDistance = Vector3.Distance(vcam.transform.position, vcam.LookAt.position) - m_LookAtTargetRadius; } m_FadeOutMaterial.SetFloat(k_MaxDistanceID, m_MaxDistance); m_FadeOutMaterial.SetFloat(k_MinDistanceID, m_MinDistance); } }
protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (vcam.Follow) { if (stage == CinemachineCore.Stage.Aim) { if (startingRotation == null) { startingRotation = transform.localRotation.eulerAngles; } if (inputManager) { Vector2 deltaInput = inputManager.GetMouseDelta(); startingRotation.x += deltaInput.x * verticalSpeed * Time.deltaTime; startingRotation.y += deltaInput.y * horizontalSpeed * Time.deltaTime; startingRotation.y = Mathf.Clamp(startingRotation.y, -clampAngle, clampAngle); state.RawOrientation = Quaternion.Euler(-startingRotation.y, startingRotation.x, 0f); } } } }
public override void OnInspectorGUI() { BeginInspector(); SerializedProperty vcamProperty = FindProperty(x => x.VirtualCamera); EditorGUI.indentLevel = 0; // otherwise subeditor layouts get screwed up Rect rect; CinemachineVirtualCameraBase vcam = vcamProperty.exposedReferenceValue as CinemachineVirtualCameraBase; if (vcam != null) { EditorGUILayout.PropertyField(vcamProperty, kVirtualCameraLabel); } else { GUIContent createLabel = new GUIContent("Create"); Vector2 createSize = GUI.skin.button.CalcSize(createLabel); rect = EditorGUILayout.GetControlRect(true); rect.width -= createSize.x; EditorGUI.PropertyField(rect, vcamProperty, kVirtualCameraLabel); rect.x += rect.width; rect.width = createSize.x; if (GUI.Button(rect, createLabel)) { vcam = CinemachineMenu.CreateDefaultVirtualCamera(); vcamProperty.exposedReferenceValue = vcam; } serializedObject.ApplyModifiedProperties(); } DrawRemainingPropertiesInInspector(); if (vcam != null) { DrawSubeditors(vcam); } }
/// <summary> /// Create the default camera rig. If there is a Main Camera in the scene it will be configured appropriately, /// otherwise a camera will be added to the scene. /// </summary> private void SetupMainCamera() { if (defaultCameraSetup == null) { CreateDefaultClearShot(); } else { if (defaultCameraSetup.gameObject.scene.IsValid()) { defaultCameraRig = defaultCameraSetup; } else { defaultCameraRig = Instantiate(defaultCameraSetup); defaultCameraRig.gameObject.name = "Default Cinemachine ClearShot Camera"; } } Camera camera = Camera.main; CinemachineBrain brain = camera.GetComponent <CinemachineBrain>(); if (brain == null) { Debug.LogWarning("Camera did not have a Cinemachine brain, adding one. You should probably add one to your camera in the scene."); brain = camera.gameObject.AddComponent <CinemachineBrain>(); } if (camera.GetComponent <AudioListener>() == null) { Debug.LogWarning("Camera did not have an audio listener, adding one. You should probably add one to your camera in the scene."); camera.gameObject.AddComponent <AudioListener>(); } if (camera.GetComponent <FlareLayer>() == null) { Debug.LogWarning("Camera did not have an Flare Layer, adding one. You should probably add one to your camera in the scene."); camera.gameObject.AddComponent <FlareLayer>(); } }
private void OnTriggerEnter(Collider other) { mixer.enabled = true; if ((CinemachineVirtualCameraBase)brain.ActiveVirtualCamera == mixer) { return; } float weight = volume.GetWeight(); cameraB.transform.SetParent(mixer.transform, true); cameraB.enabled = true; if (modifiable) { if (weight < 0.5f) { cameraA = (CinemachineVirtualCameraBase)brain.ActiveVirtualCamera; cameraA.transform.SetParent(mixer.transform, true); cameraA.enabled = true; } else { cameraB = (CinemachineVirtualCameraBase)brain.ActiveVirtualCamera; cameraB.transform.SetParent(mixer.transform, true); cameraB.enabled = true; } } else { cameraA.transform.SetParent(mixer.transform, true); cameraA.enabled = true; } var x = mixer.ChildCameras; mixer.SetWeight(cameraA, 1f - weight); mixer.SetWeight(cameraB, weight); }
internal static void DrawVirtualCameraBaseGizmos(CinemachineVirtualCameraBase vcam, GizmoType selectionType) { // Don't draw gizmos on hidden stuff if ((vcam.VirtualCameraGameObject.hideFlags & (HideFlags.HideInHierarchy | HideFlags.HideInInspector)) != 0) { return; } CameraState state = vcam.State; Gizmos.DrawIcon(state.FinalPosition, "Cinemachine/cm_logo_lg.png", true); CinemachineBrainEditor.DrawCameraFrustumGizmo( CinemachineCore.Instance.FindPotentialTargetBrain(vcam), state.Lens, Matrix4x4.TRS( state.FinalPosition, UnityQuaternionExtensions.Normalized(state.FinalOrientation), Vector3.one), CinemachineCore.Instance.IsLive(vcam) ? CinemachineSettings.CinemachineCoreSettings.ActiveGizmoColour : CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour); }
protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (stage == CinemachineCore.Stage.Body) { var pos = state.RawPosition; if (LockOnXAxis) { pos.x = XPosition; } if (LockOnYAxis) { pos.y = YPosition; } if (LockOnZAxis) { pos.z = ZPosition; } state.RawPosition = pos; } }
/// <summary> /// Locks position /// </summary> /// <param name="vcam"></param> /// <param name="stage"></param> /// <param name="state"></param> /// <param name="deltaTime"></param> protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (enabled && stage == CinemachineCore.Stage.Body) { var pos = state.RawPosition; if (LockXAxis) { pos.x = _forcedPosition.x; } if (LockYAxis) { pos.y = _forcedPosition.y; } if (LockZAxis) { pos.z = _forcedPosition.z; } state.RawPosition = pos; } }
public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount) { // Hack to set the display name of the clip to match the vcam foreach (var c in GetClips()) { MYCinemachineShot shot = (MYCinemachineShot)c.asset; CinemachineVirtualCameraBase vcam = shot.VirtualCamera; if (!vcam) { vcam = shot.GetCinemachineVm(go); } if (vcam != null) { c.displayName = vcam.Name; } } var mixer = ScriptPlayable <CinemachineMixer> .Create(graph); mixer.SetInputCount(inputCount); return(mixer); }
protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (stage == CinemachineCore.Stage.Body) { float minY = bounds.min.y + Camera.main.orthographicSize; float maxY = bounds.max.y - Camera.main.orthographicSize; var pos = state.RawPosition; if (pos.y < minY) { pos.y = minY; } if (pos.y > maxY) { pos.y = maxY; } state.RawPosition = pos; } }