I want to invert every objects color in my entire game from the press of a button in a script.

I know nothing about shaders, thats not an exaggeration I do not know what any form of code for shaders does. I figured that making some system that would Invert the colors on a button press would be easy but that couldn’t be farther from the truth. I just want something that works like a lightswitch, I turn it one way and its negative, the other and its normal. But everything says to apply a shader to an object infront of the camera. I’ve literally downloaded one and slapped it straight onto a fresh material with no results. I just dont know why I cant just apply a simple screen space post processing effect that does this on the camera and call it a day. It feels like something that should be as simple as bloom or any other effect, but it’s really complicated and I am not ready to learn a whole new language for shader programming.

Hello, to create this post processing effect…


Create a new shader file named ‘Negative.shader’ in a Resources folder (e.g. Assets/Shaders/Resources/Negative.shader) and paste this code into it:

Shader "Custom/Negative"
{
  HLSLINCLUDE
      #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
      TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);

      float4 Frag(VaryingsDefault i) : SV_Target
      {
          float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
        // invert the color
          color.rgb = 1 - color.rgb;
          return color;
      }
  ENDHLSL
  SubShader
  {
      Cull Off ZWrite Off ZTest Always
      Pass
      {
          HLSLPROGRAM
              #pragma vertex VertDefault
              #pragma fragment Frag
          ENDHLSL
      }
  }
}

Create the script Negative.cs in the same folder:

using System;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

[Serializable]
[PostProcess(typeof(NegativeRenderer), PostProcessEvent.AfterStack, "Custom/Negative")]
public sealed class Negative : PostProcessEffectSettings
{
}

public sealed class NegativeRenderer : PostProcessEffectRenderer<Negative>
{
    public override void Render(PostProcessRenderContext context)
    {
        var sheet = context.propertySheets.Get(Shader.Find("Custom/Negative"));
        context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
    }
}

Now you should be able to select the new post processing effect Custom/Negative when setting up the profile/volume/camera for post processing. In the post processing volume check ‘Is Global’.


To turn the effect on and off by pressing ‘n’, attach the following script to the game object that holds the post processing volume:

using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class PostProcessing : MonoBehaviour
{
    Negative negative;

    void Start()
    {
        negative = gameObject.GetComponent<PostProcessVolume>().sharedProfile.GetSetting<Negative>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.N))
        {
            negative.active = !negative.active;
        }
    }
}

If you have any issues with this, let me know.