First, I’d like to say, that I am a complete noob in URP.
Following some outdated tutorials, I created a simple Pixelize effect using URP ScriptableRenderPass. I switched RenderTargetHandle for RThandle, because I don’t like seeing warnings in my console.
using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
[Serializable]
public class PixelizeEffectPass : ScriptableRenderPass {
private const string PIXEL_BUFFER_PROPERTY = "_PixelBuffer";
private RenderTextureDescriptor descriptor;
private RTHandle cameraColorTarget;
private RTHandle pixelBuffer;
private int pixelScreenHeight, pixelScreenWidth;
private readonly CustomPostProcessRendererFeature.PixelizePassSettings settings;
public PixelizeEffectPass(CustomPostProcessRendererFeature.PixelizePassSettings settings) {
this.settings = settings;
renderPassEvent = settings.renderPassEvent;
}
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) {
pixelScreenHeight = settings.pixelScreenHeight;
pixelScreenWidth = (int)(pixelScreenHeight * renderingData.cameraData.camera.aspect + 0.5f);
descriptor = renderingData.cameraData.cameraTargetDescriptor;
descriptor.height = pixelScreenHeight;
descriptor.width = pixelScreenWidth;
descriptor.depthBufferBits = 0;
RenderingUtils.ReAllocateIfNeeded(ref pixelBuffer, descriptor, FilterMode.Point,
TextureWrapMode.Clamp, name: PIXEL_BUFFER_PROPERTY);
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
var cameraData = renderingData.cameraData;
if (cameraData.camera.cameraType != CameraType.Game) {
return;
}
CommandBuffer cmd = CommandBufferPool.Get();
using (new ProfilingScope(cmd, new ProfilingSampler("Pixelize Effect Pass"))) {
Blitter.BlitCameraTexture(cmd, cameraColorTarget, pixelBuffer);
Blitter.BlitCameraTexture(cmd, pixelBuffer, cameraColorTarget);
}
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
CommandBufferPool.Release(cmd);
}
public void SetTarget(RTHandle cameraColorTarget) {
this.cameraColorTarget = cameraColorTarget;
}
}
But there’s an issue: it only applies to Camera color target, so it can’t be used after Post Processing, only before it. I’m curious, if there is a way to make it apply after Post Processing. Once again, I am a complete noob, just followed some basic tutorials and then used Unity reference to tweak it away from obsolete parts. Also, I didn’t manage to make it work with materials using RTHandle, so I just compress the texture and then stretch it back with 2 Blits.
It works flawlessly without Post Processing or Before it, but if I put it After, the image isn’t pixelized. Also, when I put it before, the Post Processing gets applied on top and it isn’t pixelized, unlike objects, shadows, etc., so the whole thing looks bad (e.g. round bloom on top of pixelized sphere, that’s probably the easiest way to test it).
I thought to ask, because I might miss something, like making this a PostProcessing effect, or modifying some other texture.