Pass ForgeDocs Product page

Scripting

Screen Looks and presets

Switch the whole screen look

EffectPresetUtility.ApplyScreenPreset copies a preset's Pass Forge effects into the profile a Volume uses. It copies Pass Forge effects only, so call EffectPresetUtility.MatchUrpTonemapping after it: a look with AgX then shows AgX instead of URP's tonemapper.

using PassForge.Effects;
using UnityEngine;
using UnityEngine.Rendering;

// Switches the whole screen look, such as from a settings menu or for a level's mood.
public class ScreenLookSwitcher : MonoBehaviour
{
    [SerializeField] Volume m_Volume;             // a global Volume in the scene
    [SerializeField] VolumeProfile[] m_Presets;   // shipped presets or profiles of your own

    VolumeProfile m_Profile;

    void Awake()
    {
        // Volume.profile makes a private copy the first time it is read, so the profile asset is untouched.
        m_Profile = m_Volume.profile;
    }

    public void Apply(int index)
    {
        EffectPresetUtility.ApplyScreenPreset(m_Presets[index], m_Profile, turnOffOthers: true);

        // The preset calls copy Pass Forge effects only: this sets URP's Tonemapping to match a look with AgX.
        EffectPresetUtility.MatchUrpTonemapping(m_Profile);
    }

    public void TurnOff()
    {
        EffectPresetUtility.TurnOffScreenEffects(m_Profile);
        EffectPresetUtility.MatchUrpTonemapping(m_Profile);
    }

    void OnDestroy()
    {
        if (m_Profile != null)
            Destroy(m_Profile);   // the copy is yours to clean up
    }
}

Show a status look for a few seconds

A Pass Forge Screen Look with Activation set to From Script waits for Play, PlayFor, Toggle or CrossFadeTo.

using PassForge.Effects;
using UnityEngine;
using UnityEngine.Rendering;

// Status looks on Pass Forge Screen Look components whose Activation is From Script, so they wait for these calls.
public class StatusLooks : MonoBehaviour
{
    [SerializeField] PassForgeScreenLook m_Hit;           // such as a red edge
    [SerializeField] PassForgeScreenLook m_NightVision;
    [SerializeField] VolumeProfile m_Thermal;

    // Fades in, holds for the seconds, fades out. Calling it again restarts the seconds.
    public void OnHit() => m_Hit.PlayFor(0.5f);

    // On until it is toggled off; Play and Stop do the same in two calls.
    public void OnNightVisionKey() => m_NightVision.Toggle();

    // Blends the same component over to another look in 1.5 seconds.
    public void OnThermalKey() => m_NightVision.CrossFadeTo(m_Thermal, 1.5f);
}

Drive one setting, or fade a Volume

Get the effect from the Volume's profile with TryGet, then change a setting with Override. volume.weight fades every effect in the Volume at once.

using PassForge.Effects;
using UnityEngine;
using UnityEngine.Rendering;

// Drives one effect setting, such as from a UI Slider, and fades a whole Volume.
public class CrtSlider : MonoBehaviour
{
    [SerializeField] Volume m_Volume;

    VolumeProfile m_Profile;
    CrtEffect m_Crt;

    void Awake()
    {
        m_Profile = m_Volume.profile;   // a private copy; the asset is untouched

        // An effect a script adds needs Always Include in Project Settings > Pass Forge, or builds leave it out.
        if (!m_Profile.TryGet(out m_Crt))
            m_Crt = m_Profile.Add<CrtEffect>();
    }

    // For a Slider's On Value Changed.
    public void SetCrt(float amount)
    {
        m_Crt.active = true;
        // A Volume ignores a setting that is not overridden, so setting .value alone is not enough.
        m_Crt.intensity.Override(amount);
    }

    // Fades every effect in the Volume at once, such as for a cutscene.
    public void SetWeight(float weight) => m_Volume.weight = weight;

    void OnDestroy()
    {
        if (m_Profile != null)
            Destroy(m_Profile);
    }
}

An AgX setting for the options menu

AgX replaces URP's tonemapper, so turning it off must turn URP's tonemapper back on. AgxTonemappingEffect.Status and AgxTonemappingEffect.Describe give one plain line for the menu when AgX cannot show, and allocate nothing.

using PassForge.Effects;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

// An AgX on or off setting. Turning AgX off must turn URP's tonemapper back on, or the picture is left with none.
public class TonemapperOption : MonoBehaviour
{
    [SerializeField] Volume m_Volume;   // the Volume with Pass Forge's AgX Tonemapping

    public void UseAgx(bool on)
    {
        var profile = m_Volume.profile;   // a private copy; the asset is untouched

        if (profile.TryGet(out AgxTonemappingEffect agx))
            agx.intensity.Override(on ? 1f : 0f);

        if (profile.TryGet(out Tonemapping tonemapping))
            tonemapping.mode.Override(on ? TonemappingMode.None : TonemappingMode.Neutral);
    }

    // One plain line for the menu: whether AgX showed on the camera the last time it rendered, or why not.
    public static string StatusLine(Camera camera)
    {
        return AgxTonemappingEffect.Describe(AgxTonemappingEffect.Status(camera));
    }
}

A camera made by a script

Screen effects show only on cameras with Post Processing on, so a camera a script creates needs it switched on.

using UnityEngine;
using UnityEngine.Rendering.Universal;

// A camera made from a script, such as a strategy view or a photo mode, shows Pass Forge's screen effects only with
// Post Processing on, as the game's own camera has it.
public static class EffectsCamera
{
    public static Camera Create(string name, Camera like)
    {
        var camera = new GameObject(name).AddComponent<Camera>();
        var data = camera.GetUniversalAdditionalCameraData();
        data.renderPostProcessing = true;

        // The same Volumes reach it as reach the game's camera.
        if (like != null)
            data.volumeLayerMask = like.GetUniversalAdditionalCameraData().volumeLayerMask;

        return camera;
    }
}

Run your own graph

A Pass Graph Renderer Feature's graph can change while the game runs. The feature lives on the renderer asset, so put the old graph back when you are done.

using System.Collections.Generic;
using PassForge;
using UnityEngine;
using UnityEngine.Rendering.Universal;

// Runs a graph of your own on every Pass Graph Renderer Feature while this is enabled, such as one per level. The
// features live on the renderer asset, not in the scene, so this puts them back on the way out; otherwise leaving
// Play Mode would leave the project's renderer pointing at the swapped graph.
public class GraphSwitcher : MonoBehaviour
{
    [SerializeField] PassGraphAsset m_Graph;

    readonly List<PassGraphRendererFeature> m_Features = new List<PassGraphRendererFeature>();
    readonly List<PassGraphAsset> m_Original = new List<PassGraphAsset>();

    void OnEnable()
    {
        var pipeline = UniversalRenderPipeline.asset;
        if (pipeline == null)
            return;

        var renderers = pipeline.rendererDataList;

        for (int i = 0; i < renderers.Length; i++)
        {
            if (renderers[i] == null)
                continue;

            foreach (var feature in renderers[i].rendererFeatures)
            {
                // The effects features run Pass Forge's own graphs: leave them alone.
                if (feature is PassGraphRendererFeature passForge && !passForge.isEffectsFeature)
                {
                    m_Features.Add(passForge);
                    m_Original.Add(passForge.graph);
                    passForge.graph = m_Graph;
                }
            }
        }
    }

    void OnDisable()
    {
        for (int i = 0; i < m_Features.Count; i++)
        {
            if (m_Features[i] != null)
                m_Features[i].graph = m_Original[i];
        }

        m_Features.Clear();
        m_Original.Clear();
    }
}