- Home /
Weird shader behavior , when displacing vertices.
Hello, I have a shader which displaces local positions of vertices. When I observe the result in the scene view, everything seems to be working alright (either EditMode or PlayMode). But when I play the scene I start to see objects jitter, behave strangely in the game view, (seemingly) based on a camera position.
Shader "Custom/VertDistort"
{
Properties
{
_Color ("Color", Color) = (1, 1, 1, 1)
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags {
"RenderType"="Opaque" "Queue"="Geometry"
}
Pass
{
Name "FORWARD"
Tags {"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
};
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
v.vertex.xyz *= 1.2;
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.vertex = UnityObjectToClipPos(v.vertex);
UNITY_TRANSFER_FOG(o, o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv) * _Color;
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Answer by _dns_ · Sep 18, 2018 at 03:47 PM
Hi, this could be due to dynamic batching: it merges multiple instances of meshes before "sending" vertices to the shader as one bigger mesh. This process changes the origin + local coordinates of the merged mesh, causing problems in some vertex shaders that base a transformation on the local origin, like the local scale you are applying. To check that: add a tag "DisableBatching"="True" in the SubShader. If it fixes the problem, you have the culprit :-)