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

Applying vertex colors to a procedural mesh

Discussion in 'General Graphics' started by Yertii, Jul 9, 2020.

  1. Yertii

    Yertii

    Joined:
    Jul 9, 2020
    Posts:
    10
    Hello there,
    Recently I've had an issue where a procedural mesh I create isn't correctly changing color based off of a color array provided. I current have
    Code (CSharp):
    1.  
    2. for (int i = 0; i < GetComponent<MeshFilter>().sharedMesh.vertices.Length; i++)
    3. {
    4.        newColors.Add(Color.red);
    5. }
    6.  
    7. this.GetComponent<MeshFilter>().sharedMesh.colors = newColors.ToArray();
    which assigns Color.red to every vertex in the procedural mesh. However, even though it registers that it now has colors assigned the mesh remains gray. The ShaderGraph I'm using is
    which should just set the albedo of the material to the vertex color. To troubleshoot I tried making another example mesh that would just create a triangle with each color set to red (in the same way as the code above), and it was set fine. It might be worth mentioning that neither have any UVs set, as I don't plan on applying a texture, only vertex colors. Would anyone happen to know what I'm doing wrong here? I can't seem to pinpoint the reason as to why it would show on one procedural mesh and not the other.

    Other images:

    Procedural mesh that remains gray


    Triangle example that uses the same material (and works):


    If you need any more details I'll be happy to provide them.
     
  2. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    Maybe the mesh has 32 bit colors and the shader graph prioritizes those? Sorry I'm not super familiar with how the shader graph works, your code snippet looks OK to me though. Try setting colors32 on the mesh instead and see if that works.
     
  3. Yertii

    Yertii

    Joined:
    Jul 9, 2020
    Posts:
    10
    Already tried using 32 bit colors instead, no dice. I don't see how there is any difference whatsoever between the triangle I generated and the actual procedural mesh.
     
  4. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    Tried using SetColors() instead of .colors accessor? Seems like a long shot but worth a try.
     
  5. Yertii

    Yertii

    Joined:
    Jul 9, 2020
    Posts:
    10
    I don't see why that would change how it would work on one mesh and not the other. I just checked and that indeed gave the same result.
     
  6. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    well, not sure, maybe the geo construction? perhaps normals, W_tangets, smoothing, or culling?
    does the mesh behave "inside-out" bigger than a triangle? does it triangulate correctly? does it just sht-the-bed went sending v2frag?

    perhaps try adding a UV channel, see if something changes.
    procedural quad? procedural pentagon? are those results the same

    try giving that PBR shader some metal/shiny/emissive and a light, is the geo even working as expected?

    does the vertex color work if it is even more basic shader? try a simple unlit shader
     
    Last edited: Jul 10, 2020
  7. Yertii

    Yertii

    Joined:
    Jul 9, 2020
    Posts:
    10

    Normals are calculated using Mesh#RecalculateNormals, and other data is filled out as shown below:


    I've tried calculating the UVs, nothing changed. I don't believe it's the material itself because when applying other standard materials none of them would show up either on the mesh.I'm not exactly sure what you mean by "does it just sht-the-bed went sending v2frag?", and the it does indeed triangulate correctly, the wireframe view seems fine. I got the same results with an unlit shader, and switching to either quads or pentagons would break my current system of generation.
     
  8. Yertii

    Yertii

    Joined:
    Jul 9, 2020
    Posts:
    10
    A bit of an update, it would appear that if I go right next to the mesh with a very specific camera rotation the color shows on the mesh. After looking up this problem it would seem someone else had this issue and they fixed it by switching to deferred rendering: https://answers.unity.com/questions/1520490/material-color-doesnt-show-until-i-get-very-close.html

    However, it would seem they were using the built in rendering pipeline to allow this change, and as far as I know LWRP currently doesn't have any support for deferred rendering. Is there a different solution or fix to this?
     
  9. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    so nothing works on the mesh you made
    what about on default objects add>cube and apply your material

    also we can't know if all things are accounted for without opening your project and looking through everything

    LWRP is now URP btw

    did you get all this setup?
    https://docs.unity3d.com/Packages/c...versal@7.1/manual/InstallURPIntoAProject.html

    attached URP pipeline to graphics?
    https://docs.unity3d.com/Packages/c...1/manual/configuring-universalrp-for-use.html
     
    Last edited: Jul 10, 2020
  10. Yertii

    Yertii

    Joined:
    Jul 9, 2020
    Posts:
    10
    I initially started the project with the URP template so I'm assuming all of that setup was already done beforehand. I can't exactly just apply the material to any primitive object because it would just show up gray (it uses the vertex color as the albedo, which is provided via script). The triangle mesh I created however did appear to stay red no matter what angle I looked at it, so I'm at a bit of a loss.
     
  11. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    Does the mesh have multiple submeshes? Multiple materials assigned to it? Material needs tangents and none are supplied?
     
  12. Yertii

    Yertii

    Joined:
    Jul 9, 2020
    Posts:
    10
    Nope, nope and the shader doesn't require any tangents, you can see the ShaderGraph in the picture I posted above.
     
  13. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    Make sure the bounds are calculated on the mesh?
     
  14. Yertii

    Yertii

    Joined:
    Jul 9, 2020
    Posts:
    10
    I fail to see how bounds would affect it in any way, and after checking it indeed doesn't change the outcome.
     
  15. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    I fail to see how you are having this problem at all, just trying to be helpful.
     
  16. Yertii

    Yertii

    Joined:
    Jul 9, 2020
    Posts:
    10
    Well, thanks for you help anyways. I will continue to troubleshoot and see if I can find any solution to this peculiar problem.
     
  17. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    isolate variables to deduce
    1. remove [vertex_color] node in the PBR graph
    2. attach color-> albedo
    3. disable vertex coloring script so that it does not cause any interference with scene/mesh
    • if direct color fails the results are caused with cam + mesh + render settings
    • if the color works this result is likely caused by mesh + vertex_color script
     
  18. Yertii

    Yertii

    Joined:
    Jul 9, 2020
    Posts:
    10
    After doing this the procedural mesh still remained gray, and the other procedural triangle example changed properly to that albedo color. The effect of the color only showing up when being very close to the mesh at a certain angle remains for the large procedural mesh. I think it's more of a problem with the camera/rendering settings than the actual shader properties.
     
  19. Twinf0x

    Twinf0x

    Joined:
    Aug 28, 2018
    Posts:
    1
    I ran into the same problem yesterday. I don't know if you've already solved the problem, but for me changing the light settings did the trick.

    Go to "Window > Rendering > Lighting Settings" in "Other Settings" deactivate the fog and you should be able to see the vertex colors.