Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Other Closed: Water Vertex Displacement Issue with CombineMeshes and Shader Graph

Discussion in 'Shader Graph' started by MaximusLawsy, Feb 29, 2024.

  1. MaximusLawsy

    MaximusLawsy

    Joined:
    Feb 18, 2024
    Posts:
    5
    Description:
    I am seeking assistance with a challenge I'm facing in Unity regarding CombineMeshes and Shader Graph. My objective is to combine water meshes to reduce draw calls, considering that each mesh is unique in its own way, and everything is procedural generated at runtime.
    Code (CSharp):
    1.  
    2. m_CombineInstance = new CombineInstance[filters.Length];
    3. for (var i = 0; i < m_CombineInstance.Length; i++)
    4. {
    5.     m_CombineInstance[i].mesh = filters[i].sharedMesh;
    6.     m_CombineInstance[i].transform = filters[i].transform.localToWorldMatrix;
    7.     filters[i].gameObject.SetActive(false);
    8. }
    9.  
    Problem Overview:
    • When testing individual meshes independently, everything behaves as anticipated with no discernible gaps; the meshes maintain their integrity even when combined.
    • Upon combining meshes using CombineMeshes, the world space position becomes slightly misaligned.
    • While all water features are correctly placed in the world space, a gap appears specifically at the four outer edges of the mesh when combined.
    • I can confirm that the issue is not related to the parent, as transforms are reset to defaults (zero).
    • The combined meshes represent the water features nearest to the island, and upon closer inspection, it's evident that the edges are not animating as expected.
    • In this current example, there are a total of 64 meshes combined.
    Screenshots.gif

    Mesh Details:
    The meshes represent SubMeshes and share the same material, utilizing Matrix transforms of the CombineMeshes function.
    Code (CSharp):
    1.  mesh.CombineMeshes(m_CombineInstance, true);
    Troubleshooting Steps:
    Attempted to use the filters Matrix without success.
    Code (CSharp):
    1. meshFilters[i].transform.localToWorldMatrix
    Tried setting the vertices Matrix to world space, without success.
    Code (CSharp):
    1. newVerts[i] = m.MultiplyPoint3x4(origVerts[i])
    Observations:
    • Noticed that the vertex displacement doesn't extend beyond the rendering bounds of the parent GameObject of the CombineMeshes.
    • Considering that the meshes are now SubMeshes, there is a suspicion that they might not be returning the correct world space position, despite attempting to set the vertices to world space positions (as shown in the snippet above).
    Additional Details:
    Unity Version: 2022.2
    OS: SteamDeck

    Request for Assistance:

    Seeking insights, suggestions, or solutions from the community regarding the misalignment issue when combining meshes and its impact on Shader Graph rendering. Any help or guidance would be greatly appreciated!
     
    Last edited: Feb 29, 2024
  2. MaximusLawsy

    MaximusLawsy

    Joined:
    Feb 18, 2024
    Posts:
    5
    Update:
    It appears that the problem may be attributed to the rendering bounds of CombineMeshes. In the provided test screenshot, the ShaderGraph accurately calculates the shader world positions. However, during wave animation, synchronization issues arise when combining the Ocean meshes into a single combined mesh.

    Screenshot from 2024-03-11 09-47-38.png

    Post:
    Perhaps this post is no longer suitable for the Shader Graph thread.
     
    Last edited: Mar 10, 2024
  3. MaximusLawsy

    MaximusLawsy

    Joined:
    Feb 18, 2024
    Posts:
    5
    Update:
    I can verify that this is not the root cause of the problem. Even after enlarging the rendering bounds of the combined meshes, the outcomes remain consistent.
    Code (CSharp):
    1. var mesh = new Mesh { name = "OceanCombinedMesh" };
    2. mesh.CombineMeshes(m_CombineInstance, true);
    3. var size = 512.0f;
    4. mesh.bounds = new Bounds(mesh.bounds.center, mesh.bounds.size + new Vector3(size, size, size));
     
  4. MaximusLawsy

    MaximusLawsy

    Joined:
    Feb 18, 2024
    Posts:
    5
    After conducting numerous tests to identify potential issues and rule out various possibilities, I have reached an impasse. My speculation is that the problem may stem from the fact that the combined mesh is treated as a single entity with all vertices stored in a single array, possibly causing the shader's vertex displacement to be slightly out of sync.

    I have decided to abandon this approach and revert to what has been proven to work. I will no longer pursue the combining of meshes.
     
  5. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,353
    I have read everything and I am not sure what is your exact problem to be honest, I can see first gif, but it's hard to read what is that, plus you never show us your shader graph, so it is impossible to determine what is wrong.

    Also where are your meshes, is all water combined into single mesh or we are talking only about the ligher blue square?
    I don't understand logic behind this, why it's out of sync?
     
  6. MaximusLawsy

    MaximusLawsy

    Joined:
    Feb 18, 2024
    Posts:
    5
    Thank you for your assistance. After extensive testing, I have determined that Shader Graph is not the issue. The problem lies with the CombinedMesh. I would like to remove this entire thread, but I am unsure how to do so. I have changed the status to "other" because the issue remains unresolved, and it would have been helpful to have the option to close the thread.
     
  7. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,353
    You could ask to move this thread to other section, but if you managed to determine what caused your issues then it might be helpful to others with similar problem in the future.
     
    MaximusLawsy likes this.