Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Dynamic batching seems to modify preset vertex normals and tangent - is this fixable?

Discussion in 'Shaders' started by ben_at_blowfish, Apr 16, 2014.

  1. ben_at_blowfish

    ben_at_blowfish

    Joined:
    Jun 18, 2013
    Posts:
    5
    So it took me a while to discover this was the problem, but it seems when meshes are dynamically batched it is possible for their vertex normal and tangent data to be modified in the rendering pipeline before it reaches the shader. :confused:

    It is very easy to see this happen if you create a shader that simply applies the normal xyz values as the colour rgb values in the shader. Then at run-time every now and then if you have a lot of batched objects you will see some flicker. I'm not using the normal values to set the colour in my proper shader, this is just so I can visualise what Unity is doing to my vertex data before it reaches the shader.

    Using dynamic batching is essential for my project, so I was wondering if anyone knows any settings or work around for this problem? I don't want to use Color and UV2 to encode my vertex data as I'm already using them for other bits of data.
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Best bet is to report a bug with a reproducible scene.
     
  3. ben_at_blowfish

    ben_at_blowfish

    Joined:
    Jun 18, 2013
    Posts:
    5
    Yep, will do this too.
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Dynamic batching puts multiple objects together into one object. That means they're all assigned a new object space. When this happens, Unity needs to update the objects vertex positions and normals accordingly (and, as a result, the tangents must also be altered because they need to remain orthogonal to the normals). If Unity didn't do this, normal maps would break as soon as an object was batched.

    Long story short, no it's not possible to have Unity not modify these values when batching - it would break things if it didn't.
     
  5. ben_at_blowfish

    ben_at_blowfish

    Joined:
    Jun 18, 2013
    Posts:
    5
    True that makes sense, I guess why originally I was asking if there was a setting to get it to not do that.

    I am already transforming my data in the normals using the object transformation in the hope of it being unravelled correctly but it doesn't seem to work. I had assumed normals would only be affected by the model rotations?

    This is what I am doing to my data before storing it in the normal attribute. Would you think this has any chance of being correct?
    Code (csharp):
    1. Vector3 fc = Quaternion.Inverse( m_Transform.rotation)*new Vector3(vertColour.r, vertColour.g, vertColour.b);
    Edit: Also the weird thing is that only some of the batched meshes exhibit flickering normals when moving around the scene.
     
    Last edited: Apr 16, 2014
  6. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    I had similar problems when I used vertex local data so I disabled batching on objects which use them with:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BreakBatch : MonoBehaviour {
    5.  
    6.     void Start () {
    7.         renderer.material = new Material(renderer.material);
    8.     }
    9.  
    10. }