- Home /
Dynamic batching and object local vertex position
I am writing a shader that would pulsate an object by moving vertex position in local coordinates.
It works fine if applied to one object only. However, if it is applied to multiple objects and if they are batched, then local coordinates get messed up and objects pulsate around a common center. Normals are also messed up as can be seen by color. Instead of having a object relative direction, they are relative to a collective of all the batched objects.
Shader "Custom/Pulsar" {
Properties {
_GrowFactor ("Grow Factor", Float) = 0.4
}
SubShader {
Pass {
Tags { "Queue"="Geometry" }
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include <UnityCG.cginc>
float _GrowFactor;
struct VertexOutput{
float4 pos : SV_POSITION;
float4 col : COLOR;
};
VertexOutput vert(appdata_full v){
VertexOutput o;
float4 vPos = v.vertex + float4(v.normal.xyz, 1.0)*abs(_GrowFactor*_SinTime.w);
o.pos = mul(UNITY_MATRIX_MVP, vPos);
o.col = float4(v.normal, 1.0);
return o;
}
float4 frag(VertexOutput o): COLOR0 {
return o.col;
}
ENDCG
}
}
FallBack "Diffuse"
}
Is there a way that vertices can be processed in their local coordinates even after batching, possibly in a particular transformation space ?
A little late, but I'm having the same issue right now. I don't think it is possible. It depends on how Unity is doing the batching internally. If they would do seperate draw calls and just share the resources among those consecutive draw calls, they would have to provide different UNITY_$$anonymous$$ATRIX_$$anonymous$$VP matrices for each. That would be nice but I think they are using a different approach.
I ended up just using one material instance for each object. I had many objects dynamically instantiated, so I used just this line after instantiation: renderer.material = renderer.material;
, which automatically creates a material instance.
Edit: Just found the new DisableBatching tag to disable batching for this shader completely. http://docs.unity3d.com/500/Documentation/$$anonymous$$anual/SL-SubshaderTags.html
This was a tricky bug to diagnose, but the DisableBatching tag worked great! Thanks!! (verified on Unity 5.4)
Tags { "DisableBatching"="True" }
Your answer
Follow this Question
Related Questions
How do I set up custom shader options without killing batching? 0 Answers
Dissable Batching in Shader 0 Answers
Dynamic Batching and vertex shader animation 2 Answers
Does dynamic batching and vertex shader work together? 1 Answer
Add new vertex attributes for a shader,Create vertex attributes 0 Answers