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.