- Home /
How to make vector displacement shader work with screen space depth-based edge detection image effect?
I'm using Unity's Vector Graphics package and this free Outline asset from the asset store to put outlines around my vector images. I set the shader's "Queue" to "Geometry" and "RenderType" to "Opaque", and I then tweaked the vector shader to do some vertex displacement that makes the vectors look like they're wiggling! Unfortunately, the outline acts as if the vertex displacement isn't happening and draws an outline around what the object would look like if no vertices were being displaced.
From what I understand, the depth buffer isn't getting properly updated with the displaced vertex positions. I tried writing a custom ShadowCaster pass from stuff I found online, but had no luck getting the outline to change in any way.
Here is my shader code:
Shader "Unlit/OpaqueJitterVector"
{
Properties
{
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Color("Tint", Color) = (1,1,1,1)
_NoiseTex("Noise Texture", 2D) = "white" {}
_Intensity("Intensity", Float) = 0.05
[MaterialToggle] PixelSnap("Pixel snap", Float) = 0
[HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
[HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
[PerRendererData] _AlphaTex("External Alpha", 2D) = "white" {}
[PerRendererData] _EnableExternalAlpha("Enable External Alpha", Float) = 0
}
SubShader
{
Tags
{
"Queue" = "Geometry"
"IgnoreProjector" = "True"
"RenderType" = "Opaque"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
Tags {"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex VectorVert
#pragma fragment SpriteFrag
#pragma target 2.0
#pragma multi_compile_instancing
#pragma multi_compile _ PIXELSNAP_ON
#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
#include "UnitySprites.cginc"
sampler2D _NoiseTex;
float _Intensity;
v2f VectorVert(appdata_t IN)
{
v2f OUT;
UNITY_SETUP_INSTANCE_ID(IN);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
OUT.vertex = UnityFlipSprite(IN.vertex, _Flip);
float interval = 0.15f; // seconds between change
float t = floor(_Time.y/interval);
t = t % 3; // this is the number of variants
t *= 0.1f; // this is necessary because the t's can't all be multiples of 1
float4 hi = float4(IN.vertex.x + t, IN.vertex.y + t, 0, 0);
half noiseVal = tex2Dlod(_NoiseTex, hi).r;
OUT.vertex.x += noiseVal * _Intensity;
OUT.vertex = UnityObjectToClipPos(OUT.vertex);
OUT.texcoord = IN.texcoord;
#ifdef UNITY_COLORSPACE_GAMMA
fixed4 color = IN.color;
#else
fixed4 color = fixed4(GammaToLinearSpace(IN.color.rgb), IN.color.a);
#endif
OUT.color = color * _Color * _RendererColor;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap(OUT.vertex);
#endif
return OUT;
}
ENDCG
}
}
}
And I can't post the outline code since it's from someone else's asset but here's a link to it. (It's free!)
Your answer
![](https://koobas.hobune.stream/wayback/20220613000758im_/https://answers.unity.com/themes/thub/images/avi.jpg)