2 using UnityEngine.Serialization;
7 /// As a part of the Cinemachine Pipeline implementing the Noise stage, this
8 /// component adds Perlin Noise to the Camera state, in the Correction
9 /// channel of the CameraState.
11 /// The noise is created by using a predefined noise profile asset. This defines the
12 /// shape of the noise over time. You can scale this in amplitude or in time, to produce
13 /// a large family of different noises using the same profile.
15 /// <seealso cref="NoiseSettings"/>
16 [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
17 [AddComponentMenu("")] // Don't display in add component menu
19 public class CinemachineBasicMultiChannelPerlin : CinemachineComponentBase
22 /// Serialized property for referencing a NoiseSettings asset
24 [Tooltip("The asset containing the Noise Profile. Define the frequencies and amplitudes there to make a characteristic noise profile. Make your own or just use one of the many presets.")]
25 [FormerlySerializedAs("m_Definition")]
26 [NoiseSettingsProperty]
27 public NoiseSettings m_NoiseProfile;
30 /// Gain to apply to the amplitudes defined in the settings asset.
32 [Tooltip("Gain to apply to the amplitudes defined in the NoiseSettings asset. 1 is normal. Setting this to 0 completely mutes the noise.")]
33 public float m_AmplitudeGain = 1f;
36 /// Scale factor to apply to the frequencies defined in the settings asset.
38 [Tooltip("Scale factor to apply to the frequencies defined in the NoiseSettings asset. 1 is normal. Larger magnitudes will make the noise shake more rapidly.")]
39 public float m_FrequencyGain = 1f;
41 /// <summary>True if the component is valid, i.e. it has a noise definition and is enabled.</summary>
42 public override bool IsValid { get { return enabled && m_NoiseProfile != null; } }
44 /// <summary>Get the Cinemachine Pipeline stage that this component implements.
45 /// Always returns the Noise stage</summary>
46 public override CinemachineCore.Stage Stage { get { return CinemachineCore.Stage.Noise; } }
48 /// <summary>Applies noise to the Correction channel of the CameraState if the
49 /// delta time is greater than 0. Otherwise, does nothing.</summary>
50 /// <param name="curState">The current camera state</param>
51 /// <param name="deltaTime">How much to advance the perlin noise generator.
52 /// Noise is only applied if this value is greater than or equal to 0</param>
53 public override void MutateCameraState(ref CameraState curState, float deltaTime)
55 if (!IsValid || deltaTime < 0)
61 mNoiseTime += deltaTime * m_FrequencyGain;
62 curState.PositionCorrection += curState.CorrectedOrientation * NoiseSettings.GetCombinedFilterResults(
63 m_NoiseProfile.PositionNoise, mNoiseTime, mNoiseOffsets) * m_AmplitudeGain;
64 Quaternion rotNoise = Quaternion.Euler(NoiseSettings.GetCombinedFilterResults(
65 m_NoiseProfile.OrientationNoise, mNoiseTime, mNoiseOffsets) * m_AmplitudeGain);
66 curState.OrientationCorrection = curState.OrientationCorrection * rotNoise;
69 private bool mInitialized = false;
70 private float mNoiseTime = 0;
72 [SerializeField][HideInInspector]
73 private Vector3 mNoiseOffsets = Vector3.zero;
75 /// <summary>Generate a new random seed</summary>
78 mNoiseOffsets = new Vector3(
79 Random.Range(-1000f, 1000f),
80 Random.Range(-1000f, 1000f),
81 Random.Range(-1000f, 1000f));
88 if (mNoiseOffsets == Vector3.zero)