I am making a game with a pixelation shader. I downloaded this shader from the web, and it works with the default render pipeline. I want to use the URP for other purposes. When using the URP, the pixelation shader did not work. I narrowed the problem down a little bit. I found out that OnRenderImage()
does not get called using the URP, but does get called with the default RP. I think there is a way to do this because of this thread. I don't understand what they are talking about because I am very new to shaders. I searched further and found this in the unity manual. However, I do not understand it. Can someone walk me through it, and convert this script to be compatible with the URP?
Here is the script:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent(typeof (Camera))]
[AddComponentMenu("Image Effects/Pixelate")]
public class Pixelate:MonoBehaviour{
public Shader shader;
int _pixelSizeX=1;
int _pixelSizeY=1;
Material _material; //pixelation material/shader
[Range(1,20)]
public int pixelSizeX=1;
[Range(1,20)]
public int pixelSizeY=1;
public bool lockXY=true;
void OnRenderImage(RenderTexture source, RenderTexture destination){
if(_material==null) _material=new Material(shader);
_material.SetInt("_PixelateX",pixelSizeX);
_material.SetInt("_PixelateY",pixelSizeY);
Graphics.Blit(source,destination,_material);
}
void OnDisable(){
DestroyImmediate(_material);
}
void Update(){
if(pixelSizeX!=_pixelSizeX){
_pixelSizeX=pixelSizeX;
if(lockXY) _pixelSizeY=pixelSizeY=_pixelSizeX;
}
if(pixelSizeY!=_pixelSizeY){
_pixelSizeY=pixelSizeY;
if(lockXY) _pixelSizeX=pixelSizeX=_pixelSizeY;
}
}
}