-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
64 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...inemachine/Samples~/3D Samples/FadeOutNearbyObjects/CinemachineFadeOutShaderController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace Unity.Cinemachine.Samples | ||
{ | ||
/// <summary> | ||
/// An example add-on module for Cinemachine Virtual Camera for controlling | ||
/// the FadeOut shader included in our example package. | ||
/// </summary> | ||
[ExecuteAlways] | ||
public class CinemachineFadeOutShaderController : CinemachineExtension | ||
{ | ||
/// <summary>Radius of the look at target.</summary> | ||
[Tooltip("Radius of the look at target.")] | ||
public float LookAtTargetRadius = 1; | ||
|
||
[Serializable] | ||
public struct FadeOutRangeSettings | ||
{ | ||
[Tooltip("If not enabled, range will be automatically set to be between this virtual camera and LookAt target plus LookAtTargetRadius.")] | ||
public bool Manual; | ||
|
||
[Tooltip("Objects will be fully transparent at the near distance from the camera, and fully opaque at the far distance.")] | ||
[MinMaxRangeSlider(0, 20)] | ||
public Vector2 Range; | ||
} | ||
|
||
[EnabledProperty("Manual", "(automatic)")] | ||
public FadeOutRangeSettings FadeOutRange = new () { Range = new Vector2(0, 10) }; | ||
|
||
[Tooltip("If true, MaxDistance will be set to " + | ||
"distance between this virtual camera and LookAt target plus LookAtTargetRadius.")] | ||
public bool MaxDistanceControlledByCamera = true; | ||
|
||
/// <summary>Material using the FadeOut shader.</summary> | ||
[Tooltip("Material using the FadeOut shader.")] | ||
public Material FadeOutMaterial; | ||
|
||
static readonly int k_MaxDistanceID = Shader.PropertyToID("_MaxDistance"); | ||
static readonly int k_MinDistanceID = Shader.PropertyToID("_MinDistance"); | ||
|
||
/// <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 (FadeOutMaterial == null || !FadeOutMaterial.HasProperty(k_MaxDistanceID) || !FadeOutMaterial.HasProperty(k_MinDistanceID)) | ||
return; | ||
|
||
var range = FadeOutRange.Range; | ||
if (!FadeOutRange.Manual && vcam.LookAt != null) | ||
range = new(0, Vector3.Distance(vcam.transform.position, vcam.LookAt.position) + LookAtTargetRadius); | ||
FadeOutMaterial.SetFloat(k_MinDistanceID, range.x); | ||
FadeOutMaterial.SetFloat(k_MaxDistanceID, range.y); | ||
} | ||
} | ||
|
||
void OnValidate() | ||
{ | ||
LookAtTargetRadius = Math.Max(0, LookAtTargetRadius); | ||
FadeOutRange.Range.x = Math.Max(0, FadeOutRange.Range.x); | ||
FadeOutRange.Range.y = Math.Max(0, FadeOutRange.Range.y); | ||
FadeOutRange.Range.y = Math.Max(FadeOutRange.Range.x, FadeOutRange.Range.y); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...chine/Samples~/3D Samples/FadeOutNearbyObjects/CinemachineFadeOutShaderController.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
com.unity.cinemachine/Samples~/3D Samples/FadeOutNearbyObjects/FadeOut.mat.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.