Displaying multiple backgrounds simultaneously

The title of this might be misleading, I’m not sure how to concisely describe this problem.
So I’m fairly new to Unity, mostly just messing around with it in my spare time.
I want to implement having 2 versions of the same background on the screen at the same time.

Okay so imagine, I don’t know, some random image of a beach as a background, except its gray scale, with the player character in the middle of the screen. Now imagine there’s a box around the character, and the background inside the box changes to the beach but in full colour. As the character moves the box moves too, changing which parts of the background are in colour and which are gray scale.

I’ve thought about this for a while and I’m not sure what mechanism I would use to implement this.

Some ideas,

  • Shaders : converts rest of the image into grayscale, except that radius around player is original full color
  • Camera masks (2nd camera sees the full color image, while 1st one sees the grayscale)
  • Stencil shaders
  • Image effects: If your player is always at the center of the screen, then easily image effect would turn other parts to grayscale

Adding onto the image effect idea, even if your player wasn’t centered, you could have a script supplying your shader its current viewport position.

It just so happens that I’ve recently been learning shader programming, and I’ve been messing with a combination of effects that could definitely do what you’re asking.

So for fun and learning I decided to create this effect.

Create a new shader and paste this code inside:
Shader Code

// Default Sprite shader with additional Saturation parameters

Shader "Custom/ImageEffect/SaturationMaskSquare"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _Saturation("Saturation", Range(0,1)) = 1
        _NormalizedWidth("Normalized Width", Range(0,1)) = 0.5
        _NormalizedHeight("Normalized Height", Range(0,1)) = 0.5
        _NormalizedPositionX("Normalized Position X", Range(0,1)) = 0.5
        _NormalizedPositionY("Normalized Position Y", Range(0,1)) = 0.5
        [MaterialToggle] PixelSnap ("Pixel snap", float) = 0
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha

        Pass
        {
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0
            #pragma multi_compile _ PIXELSNAP_ON
            #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
            #include "UnityCG.cginc"
        
            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float2 texcoord  : TEXCOORD0;
                UNITY_VERTEX_OUTPUT_STEREO
            };
        
            fixed4 _Color;
            float _Saturation;
            float _NormalizedWidth;
            float _NormalizedHeight;
            float _NormalizedPositionX;
            float _NormalizedPositionY;
            sampler2D _MainTex;
            sampler2D _AlphaTex;

            // samples a texture color and uses an external texture for alpha is necessary
            fixed4 SampleSpriteTexture (float2 uv)
            {
                fixed4 color = tex2D (_MainTex, uv);

                #if ETC1_EXTERNAL_ALPHA
                // get the color from an external texture (usecase: Alpha support for ETC1 on android)
                color.a = tex2D (_AlphaTex, uv).r;
                #endif //ETC1_EXTERNAL_ALPHA

                return color;
            }

            v2f vert(appdata_t IN)
            {
                v2f OUT;
                UNITY_SETUP_INSTANCE_ID(IN);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color * _Color;
                #ifdef PIXELSNAP_ON
                OUT.vertex = UnityPixelSnap (OUT.vertex);
                #endif

                return OUT;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;

                // standard desaturation - all colors weighted equally in value
                // float3 desaturated = (c.r + c.g + c.b) / 3;

                // desaturation effect using color weighting based on the human eye's perception of color
                // results in a better looking grayscale that preserves contrast
                float3 colorWeight = float3(0.3, 0.59, 0.11);
                float3 desaturated = lerp(dot(c.rgb, colorWeight), c.rgb, _Saturation);

                float2 normalizedPosition = float2(_NormalizedPositionX, 1-_NormalizedPositionY);

                float2 min = float2(normalizedPosition.x - _NormalizedWidth, normalizedPosition.y - _NormalizedHeight);
                float2 max = float2(normalizedPosition.x + _NormalizedWidth, normalizedPosition.y + _NormalizedHeight);

                if (IN.texcoord.x < min.x || IN.texcoord.x > max.x || IN.texcoord.y < min.y || IN.texcoord.y > max.y) {
                    c.rgb = desaturated;
                }

                c.rgb *= c.a;

                return c;
            }
        ENDCG
        }
    }
}

That shader definitely has some unnecessary sprite-specific stuff, but I don’t know what’s important or not yet, so I’m leaving it as-is.

Then create a new Material and assign that shader to it.

Then slap this script on your main camera:
Main Camera Script

using UnityEngine;

public class SaturationMaskSquare : MonoBehaviour {
    public Material mat;
    public Transform target;
    [Range(0,1)] public float width;
    [Range(0,1)] public float height;
    [Range(0,1)] public float saturation;

    private void OnRenderImage(RenderTexture source, RenderTexture destination) {
        Vector2 viewportPosition = Camera.main.WorldToViewportPoint(target.position);
        mat.SetFloat("_NormalizedPositionX", viewportPosition.x);
        mat.SetFloat("_NormalizedPositionY", viewportPosition.y);
        mat.SetFloat("_NormalizedWidth", width);
        mat.SetFloat("_NormalizedHeight", height);
        mat.SetFloat("_Saturation", saturation);

        Graphics.Blit(source, destination, mat);
    }
}

In the inspector for that script, assign your player’s Transform, the material, and the parameters for the size and intensity of the effect.

The parameters will be applied to the material each frame, and then the material will be applied to everything the camera sees each frame in a post-process step.

theres also sprite masks coming,
https://blogs.unity3d.com/2016/06/13/2d-experimental-preview/
(asset store seems to have alternatives for current versions)

Thanks for the tips :slight_smile:
So far as what I’ve actually implemented, right now I have a two camera setup, using one camera to generate a RenderTexture of it’s view (of the scene in gray scale). This is used by a shader attached to a square sprite that follows the player around. This is a bit more complex than some other solutions but I get more freedom this way to alter what my little square displays (possibly revealing an entirely different scene if I feel like it).

The only problem I have now is that I can’t seem to render my new textured sprite on top of the background image. Swapping the material out for a the sprite default displays the square correctly (i.e on top), but for some reason my new shader seems to make my sprite invisible if the background is enabled. I’ll continue to play around and I’ll see what I come up with.

1 Like

Fixed. ZWrite was set to off for my square for some reason.

1 Like