Game/PostProcessing/DefaultTonemapping.cs

Component attached to the system scene camera that controls a Tonemapping component on the same GameObject. Each update it enables or disables that Tonemapping depending on whether any other global Tonemapping exists in the loaded scene (either on a camera or inside an infinite PostProcessVolume).

/// <summary>
/// Drives the game's default <see cref="Tonemapping"/> on the same GameObject (the system scene camera).
/// The default is disabled whenever the loaded scene provides its own global tonemapping, i.e. an enabled
/// <see cref="Tonemapping"/> that is on a camera or inside an infinite <see cref="PostProcessVolume"/>.
/// </summary>
[Title( "Default Tonemapping" ), Category( "Post Processing" ), Icon( "exposure" )]
public sealed class DefaultTonemapping : Component
{
	protected override void OnUpdate()
	{
		var tonemapping = GetComponent<Tonemapping>( true );
		if ( !tonemapping.IsValid() ) return;

		var enable = !Scene.GetAllComponents<Tonemapping>().Any( x => x != tonemapping && IsGlobal( x ) );
		if ( tonemapping.Enabled != enable )
			tonemapping.Enabled = enable;
	}

	static bool IsGlobal( Tonemapping tonemapping )
	{
		var volume = tonemapping.GetComponentInParent<PostProcessVolume>( false, true );
		if ( volume is null ) return true; // On a camera, applies everywhere that camera renders

		return volume.SceneVolume.Type == Sandbox.Volumes.SceneVolume.VolumeTypes.Infinite;
	}
}