I created an image effect shader for the camera that is supposed to pixelate the screen but no matter what I do it’s not running / working.
I am using Unity version 2019.3.6f1 with URP installed.
I noticed that the shader (and other similar ones) are not working anymore after installing URP
Does OnRenderImage() work with URP?
Shader "Hidden/Pixelated"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_ScreenWidth("screen width", float) = 320.0
_ScreenHeight("screen height", float) = 240.0
_CellSizeX("size of x cell", float) = 4.0
_CellSizeY("size of y cell", float) = 4.0
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float _ScreenWidth;
float _ScreenHeight;
float _CellSizeX;
float _CellSizeY;
fixed4 frag(v2f i) : SV_Target
{
float2 uv = i.uv;
float pixelX = _ScreenWidth / _CellSizeX;
float pixelY = _ScreenHeight / _CellSizeY;
return tex2D(_MainTex, float2(floor(pixelX * uv.x) / pixelX, floor(pixelY * uv.y) / pixelY));
}
ENDCG
}
}
}
And this is the script that’s attached to the main camera
using UnityEngine;
[ExecuteInEditMode]
public class PostProcess : MonoBehaviour
{
[SerializeField]
private Vector2 cellSize = new Vector2(4, 4);
private Material material;
private void Awake()
{
material = new Material(Shader.Find("Hidden/Pixelated"));
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
material.SetFloat("_ScreenWidth", Screen.width);
material.SetFloat("_ScreenHeight", Screen.height);
material.SetFloat("_CellSizeX", cellSize.x);
material.SetFloat("_CellSizeY", cellSize.y);
Graphics.Blit(source, destination, material);
}
}
The result is always as if no shader was applied even when changing the fragment function return to return fixed4(1, 0, 0, 1);
which should return a red screen if I am not wrong.
Adding the shader in the “Always included shaders” list did not solve the problem (Edit->Project Settings->Graphics)