Editor/SuperShotFilterExport.cs

Editor utility that generates C# code snippet to recreate a CameraComponent's post-processing setup from a FilterPreset. It reads the preset's PostFx settings and appends component creation lines and property assignments into a string builder.

using System.Globalization;
using System.Text;
using Sandbox;

namespace Editor.SuperShot;

public static class SuperShotFilterExport
{
	public static string BuildCameraSnippet( FilterPreset preset )
	{
		var fx = preset?.PostFx;
		var sb = new StringBuilder();

		sb.AppendLine( "// Add these components to the CameraComponent's GameObject." );
		sb.AppendLine( "// Assumes you have: CameraComponent camera;" );

		if ( preset is null )
		{
			sb.AppendLine( "// No filter preset selected." );
			return sb.ToString();
		}

		if ( fx is null || !fx.Any )
		{
			sb.AppendLine( "// This preset has no engine post-processing enabled." );
			return sb.ToString();
		}

		if ( fx.BloomEnabled )
		{
			sb.AppendLine( "var bloom = camera.GameObject.Components.Create<Bloom>();" );
			sb.AppendLine( $"bloom.Strength = {F( fx.BloomStrength )}f;" );
			sb.AppendLine( $"bloom.Threshold = {F( fx.BloomThreshold )}f;" );
		}

		if ( fx.TonemapEnabled )
		{
			sb.AppendLine( "var tonemap = camera.GameObject.Components.Create<Tonemapping>();" );
			sb.AppendLine( $"tonemap.ExposureCompensation = {F( fx.Exposure )}f;" );
		}

		if ( fx.ColorEnabled )
		{
			sb.AppendLine( "var color = camera.GameObject.Components.Create<ColorAdjustments>();" );
			sb.AppendLine( $"color.Brightness = {F( fx.ColorBrightness )}f;" );
			sb.AppendLine( $"color.Contrast = {F( fx.ColorContrast )}f;" );
			sb.AppendLine( $"color.Saturation = {F( fx.ColorSaturation )}f;" );
			sb.AppendLine( $"color.HueRotate = {F( fx.ColorHueRotate )}f;" );
		}

		if ( fx.DofEnabled )
		{
			sb.AppendLine( "var dof = camera.GameObject.Components.Create<DepthOfField>();" );
			sb.AppendLine( $"dof.FocalDistance = {F( fx.DofFocalDistance )}f;" );
			sb.AppendLine( $"dof.FocusRange = {F( fx.DofFocusRange )}f;" );
			sb.AppendLine( $"dof.BlurSize = {F( fx.DofBlurSize )}f;" );
			sb.AppendLine( $"dof.FrontBlur = {B( fx.DofFrontBlur )};" );
			sb.AppendLine( "dof.BackBlur = true;" );
		}

		if ( fx.SharpenEnabled )
		{
			sb.AppendLine( "var sharpen = camera.GameObject.Components.Create<Sharpen>();" );
			sb.AppendLine( $"sharpen.Scale = {F( fx.SharpenScale )}f;" );
		}

		if ( fx.ChromaEnabled )
		{
			sb.AppendLine( "var chroma = camera.GameObject.Components.Create<ChromaticAberration>();" );
			sb.AppendLine( $"chroma.Scale = {F( fx.ChromaScale )}f;" );
		}

		if ( fx.VignetteEnabled )
		{
			sb.AppendLine( "var vignette = camera.GameObject.Components.Create<Vignette>();" );
			sb.AppendLine( $"vignette.Intensity = {F( fx.VignetteIntensity )}f;" );
			sb.AppendLine( $"vignette.Roundness = {F( fx.VignetteRoundness )}f;" );
			sb.AppendLine( $"vignette.Smoothness = {F( fx.VignetteSmoothness )}f;" );
		}

		if ( fx.GrainEnabled )
		{
			sb.AppendLine( "var grain = camera.GameObject.Components.Create<FilmGrain>();" );
			sb.AppendLine( $"grain.Intensity = {F( fx.GrainIntensity )}f;" );
			sb.AppendLine( $"grain.Response = {F( fx.GrainResponse )}f;" );
		}

		if ( fx.PixelateEnabled )
		{
			sb.AppendLine( "var pixelate = camera.GameObject.Components.Create<Pixelate>();" );
			sb.AppendLine( $"pixelate.Scale = {F( fx.PixelateScale )}f;" );
		}

		if ( fx.AoEnabled )
		{
			sb.AppendLine( "var ao = camera.GameObject.Components.Create<AmbientOcclusion>();" );
			sb.AppendLine( $"ao.Intensity = {F( fx.AoIntensity )}f;" );
			sb.AppendLine( $"ao.Radius = {fx.AoRadius};" );
		}

		return sb.ToString();
	}

	static string F( float value )
	{
		return value.ToString( "0.###", CultureInfo.InvariantCulture );
	}

	static string B( bool value )
	{
		return value ? "true" : "false";
	}
}