2

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;
        }
    }
}
gbe
  • 1,003
  • 1
  • 7
  • 23
  • I would rather go through [this one](https://learn.unity.com/tutorial/custom-render-passes-with-urp) and [this](https://theslidefactory.com/see-through-objects-with-stencil-buffers-using-unity-urp/) is a good example usecase ... Though .. yours looks rather like a [Post-Processing](https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@7.1/manual/integration-with-post-processing.html) question – derHugo Jun 05 '21 at 21:56
  • @derHugo I don’t see how those links would help, but if they give me some more experience, I could try it. I just want to know how to use a similar method to `OnRenderImage` hat works with URP, and how to use it. If you think post-processing tutorials are what I need to use, I will try that. – gbe Jun 05 '21 at 22:07
  • And @derHugo, my question about writing a c# script, and those links look like they are just writing shaders in a different language. Here is the link of the files I added to my game. https://ax23w4.itch.io/pixelate. I think it could help you understand the question better. – gbe Jun 06 '21 at 01:48

0 Answers0