Im working on intersection shader on URP that can recolor (grayscale) intersected area. So i got it working on SRP but when using URP its not working properly. I think its something wrong with GrabPass on URP. And i need help. Thanks)
Shader '''
Shader "Custom/Grayscale"
{
Properties
{
_Color("Color", Color) = (1,1,1,.2)
_Blend("Blend", Range(0,1)) = 0.0
}
SubShader
{
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
GrabPass
{
}
//stencil pass
Pass
{
Tags { "LightMode" = "SRPDefaultUnlit" }
Stencil
{
Ref 172
Comp Always
Pass Replace
ZFail Zero
}
Blend Zero One//Zero One
Cull Front
ZTest GEqual
ZWrite Off
}
//color pass
Pass
{
Tags { "LightMode" = "UniversalForward" }
Blend SrcColor OneMinusSrcAlpha
Stencil
{
Ref 172
Comp Equal
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _Color;
float _Blend;
sampler2D _GrabTexture;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 grabUV : TEXCOORD1;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.grabUV = ComputeGrabScreenPos(o.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
half4 bgColor = tex2Dproj(_GrabTexture, i.grabUV);
float intensity = bgColor.x * 0.299 + bgColor.y * 0.587 + bgColor.z * 0.114;
fixed4 bandw = fixed4(intensity, intensity, intensity, 1);
fixed4 lerped = lerp(bgColor, bandw, _Blend);
return lerped;
}
ENDCG
}
}
}
'''
Also added GrabPass render feature
'''
public class GrabPassFeature : ScriptableRendererFeature
{
[System.Serializable]
public class Setting
{
public string textureName = "_GrabTexture";
[Range(0, 1)] public float sampleDown = 0.5f;
public RenderPassEvent passEvent = RenderPassEvent.AfterRenderingPostProcessing;
public bool useBlitMat = true;
}
public Setting settings = new Setting();
class BlitPass : ScriptableRenderPass
{
Setting setting;
Material BlitMat;
//RenderTexture rt;
//RenderTargetIdentifier colorBuffer;
public BlitPass(Setting setting)
{
this.setting = setting;
BlitMat = new Material(Shader.Find("Hidden/Universal Render Pipeline/Blit"));
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
if (renderingData.cameraData.camera.cameraType == CameraType.SceneView)
return;
CommandBuffer cmd = CommandBufferPool.Get("Grab " + setting.textureName);
RenderTextureDescriptor desc = renderingData.cameraData.cameraTargetDescriptor;
int width = (int)(desc.width * setting.sampleDown);
int height = (int)(desc.height * setting.sampleDown);
int textureId = Shader.PropertyToID(setting.textureName);
cmd.ReleaseTemporaryRT(textureId);
cmd.GetTemporaryRT(textureId, width, height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
if (setting.useBlitMat)
cmd.Blit(renderingData.cameraData.targetTexture, textureId, BlitMat, 0);
else
cmd.Blit(renderingData.cameraData.targetTexture, textureId);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
BlitPass mBlitPass;
public override void Create()
{
mBlitPass = new BlitPass(settings) { renderPassEvent = settings.passEvent };
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(mBlitPass);
}
}
'''
I got bad skills at shaders, so a bit of explanation would be helpful)