正常图(DisableBatching为True时,即使两个物体本来是应该静态合批的,但此时由于此DisableBatching为True,从而两个物体无法进行合批,下方会有截图FrameDebugger的渲染情况)
不正常(顶点位置被改动了,但是DisableBatching为False)默认为False即允许合批,会导致顶点位置改动不正常现象(波动明显变快了,由于顶点位置异常导致!异常原因是合批网格导致,合批网格会导致Shader获取到的模型顶点位置不是属于自身模型(球体))
(合批了哦)
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
Shader "Unlit/DisableBatchingTest"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags {
"RenderType"="Opaque"
"DisableBatching" = "True" //若存在改顶点位置时,需要开启即禁止合批物体 ,可尝试将True改为False会发生不正常现象
}
LOD 100
Pass
{
Tags{
"LightMode" = "ForwardBase"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 normal : NORMAL;
float4 pos : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.pos = mul(unity_ObjectToWorld, v.vertex);
o.normal = UnityObjectToWorldNormal(v.normal);
half ran = max(0, sin((v.vertex.x + v.vertex.y + v.vertex.z) * 20 * _Time.y)) * 0.05;//波动值
v.vertex.xyz = v.vertex.xyz + v.normal * ran;//根据法线向外扩顶点位置(此时改变了顶点位置)
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed3 normal = normalize(i.normal);
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.pos));
col.rgb = fixed3(1,0,0) * (dot(normal, lightDir) * 0.5 + 0.5);
return col;
}
ENDCG
}
}
}