Search Unity

Mesh vertex colors different in Build compared to Editor

Discussion in 'General Graphics' started by GixG17, Jan 9, 2022.

  1. GixG17

    GixG17

    Joined:
    Aug 18, 2013
    Posts:
    105
    I'm trying to understand what would cause disparity between the Editor and a compiled Build when it comes to vertex data.

    I'm using a shared material on a gameObject and the shader is using the vertex color. It looks as intended in the Editor but, when I run the project as an executable, it's as if the vertex colors have different values (ie: white instead of black). The mesh isn't generated at runtime however it is instantiated and I am combining the meshes.

    Considering that a gameObject that was manually placed looks as intended in both Editor and Build, it's safe to assume that the cause would be when I'm combining the meshes. The problem is that I don't understand why it would be different from Editor to Build.

    I'm using this variation of what is provided in the Unity Documentation:
    Code (CSharp):
    1. void Combine() {
    2.         Matrix4x4 pTransform = gameObject.transform.worldToLocalMatrix;
    3.         MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
    4.         CombineInstance[] combine = new CombineInstance[meshFilters.Length];
    5.  
    6.         for (int i = 0; i < meshFilters.Length; i++) {
    7.             combine[i].mesh = meshFilters[i].sharedMesh;
    8.             combine[i].transform = pTransform * meshFilters[i].transform.localToWorldMatrix;
    9.  
    10.             meshFilters[i].gameObject.SetActive(false);
    11.         }
    12.         transform.GetComponent<MeshFilter>().mesh = new Mesh();
    13.  
    14.         CombineInstance[] tempCombine = new CombineInstance[combine.Length-1];
    15.         for (int c = 0; c < tempCombine.Length; c++) {
    16.             tempCombine[c] = combine[c+1];
    17.         }
    18.         transform.GetComponent<MeshFilter>().mesh.CombineMeshes(tempCombine, true, true);
    19.  
    20.         transform.gameObject.SetActive (true);
    21. }
    From my understanding of the script, "mesh" contains all of the information needed (including vertex color) and that is what is being carried over so I don't know why it would be overriding that information.

    Another disparity is on a vertex displacement shader to create movement like grass. As viewed in the Editor, the movements are subtle but, in the executable, the movements are intense. I'm assuming this is caused by the same problem.

    Any clue as to why that is? And, more importantly, how to rectify this? Thank you
     
  2. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Not sure, but check Project Settings -> Player -> Optimize Mesh Data is not enabled and maybe check vertex compression isn't enabled for color. If 'Optimize Mesh Data' is enabled it will strip vertex components that Unity thinks isn't used.
     
    Torbach78 and GixG17 like this.
  3. GixG17

    GixG17

    Joined:
    Aug 18, 2013
    Posts:
    105
    Very good catch!

    'Optimize Mesh Data' was enabled and 'Vertex Compression' didn't include color. Disabling 'Optimize Mesh Data' did the trick.

    I guess it has something to do with the fact that I'm instantiating an FBX file directly (with Unity's default material) so Unity considers vertex color unused.

    Thank you!