Core/DmlrPostProcess.cs

A post-process component that creates Sharpen, ColorAdjustments, AmbientOcclusion, Bloom and Tonemapping effects on the camera and tunes them from DmlrCoreDatabase settings. Reapplies when the core revision or the dynamic governor bias changes, and disables AO/Bloom when the governor reports a GPU drop.

using Sandbox;
using System;
using System.Linq;

namespace DmlrAddon;

/// <summary>
/// BASE DE CODAGE 1 · Post-traitement DMLR Ultra (Upscaler RCAS DLSS/FSR + Anti-Chute GPU) :
///  - Normal  : 1080p→2K / 2K→4K / 4K→8K · Piqué cristallin RCAS équilibré &amp; couleurs naturelles
///  - Extrême : 1080p→4K / 2K→8K / 4K→12K · Couleurs éclatantes + Noir Absolu profond (AgX, anti-doublon).
///  - Bouclier Anti-Chute : module automatiquement le coût d'AO/Bloom si le GPU sature sans perdre la netteté RCAS.
/// </summary>
public sealed class DmlrPostProcess : Component
{
	private Sharpen _sharpen;
	private ColorAdjustments _colors;
	private AmbientOcclusion _ao;
	private Bloom _bloom;
	private Tonemapping _tonemap;
	private int _appliedRevision = -1;
	private float _appliedBias = -1f;

	protected override void OnStart()
	{
		EnsureEffects();
		ApplyFromCore();
	}

	protected override void OnUpdate()
	{
		float gov = DmlrCoreDatabase.DynamicGovernorBias;
		if ( _appliedRevision == DmlrCoreDatabase.Revision && MathF.Abs( _appliedBias - gov ) < 0.08f )
			return;

		EnsureEffects();
		ApplyFromCore();
	}

	private void EnsureEffects()
	{
		_sharpen ??= Components.GetOrCreate<Sharpen>();
		_colors ??= Components.GetOrCreate<ColorAdjustments>();
		_ao ??= Components.GetOrCreate<AmbientOcclusion>();
		_bloom ??= Components.GetOrCreate<Bloom>();
		_tonemap ??= Components.GetOrCreate<Tonemapping>();
	}

	private void ApplyFromCore()
	{
		_appliedRevision = DmlrCoreDatabase.Revision;
		float gov = DmlrCoreDatabase.DynamicGovernorBias;
		_appliedBias = gov;
		bool xtreme = DmlrCoreDatabase.IsDiamantineXtreme;
		bool heavyDrop = gov < 0.72f;

		// RCAS Sharpen & Couleurs : quasi-gratuit en GPU, reste actif même sous bouclier anti-chute
		if ( _sharpen.IsValid() )
		{
			_sharpen.Enabled = true;
			_sharpen.Scale = xtreme ? 1.24f : 0.56f;
			_sharpen.TexelSize = xtreme ? 0.72f : 0.92f;
		}

		if ( _colors.IsValid() )
		{
			_colors.Enabled = true;
			_colors.Blend = 1.0f;
			_colors.Contrast = xtreme ? 1.26f : 1.09f;
			_colors.Saturation = xtreme ? 1.24f : 1.10f;
			_colors.Brightness = xtreme ? 0.94f : 0.99f;
		}

		bool hostHasAo = Scene.GetAllComponents<AmbientOcclusion>().Any( c => c.IsValid() && c != _ao && c.Enabled );
		bool hostHasBloom = Scene.GetAllComponents<Bloom>().Any( c => c.IsValid() && c != _bloom && c.Enabled );
		bool hostHasTonemap = Scene.GetAllComponents<Tonemapping>().Any( c => c.IsValid() && c != _tonemap && c.Enabled );

		// AO adaptatif : soulage automatiquement le GPU si une chute de FPS est détectée
		if ( _ao.IsValid() )
		{
			_ao.Enabled = !hostHasAo && !heavyDrop;
			if ( _ao.Enabled )
			{
				_ao.Radius = (int)MathF.Round( (xtreme ? 24f : 16f) * MathX.Lerp( 0.70f, 1.0f, gov ) );
				_ao.Intensity = (xtreme ? 0.80f : 0.54f) * MathX.Lerp( 0.75f, 1.0f, gov );
				_ao.FalloffRange = (xtreme ? 84f : 60f) * MathX.Lerp( 0.75f, 1.0f, gov );
			}
		}

		if ( _bloom.IsValid() )
		{
			_bloom.Enabled = xtreme && !hostHasBloom && !heavyDrop;
			if ( _bloom.Enabled )
			{
				_bloom.Threshold = 1.12f;
				_bloom.Strength = 0.16f;
				_bloom.Tint = Color.White;
			}
		}

		if ( _tonemap.IsValid() )
		{
			_tonemap.Enabled = xtreme && !hostHasTonemap;
			if ( _tonemap.Enabled )
			{
				_tonemap.Mode = Tonemapping.TonemappingMode.AgX;
				_tonemap.AutoExposureEnabled = false;
			}
		}
	}
}