- Home /
How to get vertex color before assigning color via Mesh.colors?
I have a mesh that has vertex colors already painted in (from Maya). I can confirm they are there using a shader to see vertex color. Is there a way to sample those vertex colors from a script using the Mesh.colors api? It seems like that array of colors is empty until I assign it a new array of colors.
Same question worded another way: I want to get the vertex colors of a mesh via script, as the mesh currently is. I don't want to assign a new array of colors (which would override the current vertex colors) in order to sample them. Is there a way to do this?
I am also having this issue but only for builds (debug works fine), the colours work when the mesh is put directly into the scene but if I instanciate it in real-time the colour data seems to be empty. I'm using Unity 2020.1.0b2, but I've had this issue in earlier versions. I get around it by manually placing all my meshes in the scene with a material that uses vertex colours then I delete them. However this seems like really ugly hack especially as I build more and more models.
That doesn't make much sense. If you place an object in the scene or instantiate it at runtime you do not instantiate the mesh referenced by the $$anonymous$$eshFilter / Skinned$$anonymous$$eshRenderer. The actual $$anonymous$$esh asset does not get duplicated at all unless you manually duplicate it (which wouldn't make much sense).
Keep in $$anonymous$$d that manipulating or reading the "mesh" property of a $$anonymous$$eshFilter would automatically duplicate the mesh for this meshfilter only. Such auto instantiatings have to be destroyed manually.
I doubt that your "solution" would actually fix that issue. It's more likely that the mesh hasn't been imported as readable. If a mesh or texture isn't marked as readable then it's data is just loaded in the beginning, directly uploaded to the GPU and from that point on no longer available to the scripting environment. By marking a texture or mesh read / wri$$anonymous$$ble Unity will essentially hold a copy in RA$$anonymous$$. So you really should be careful about what assets you mark as readable.
Finally do not do general tests in alpha or beta versions or if you don't post any errors or bugs here in UnityAnswers. If you are a beta tester please read the beta tester guide. Actual verifed bugs should be reported through Unity's bug reporter tool from inside Unity. Anything else related to alpha / beta versions should belong to the beta forum
Answer by Bunny83 · Mar 19, 2020 at 10:58 AM
If the mesh is imported in Unity, make sure you mark it as readable in the inspector. You can check if a mesh is readable with the isReadable property.
That's just the "Read/Write Enabled" option on the imported mesh right? I've got that on and I can manipulate the mesh, it's just that the color information is emptied out (only in builds)
I've added another question explaining it in better detail here: https://answers.unity.com/questions/1709168/vertex-color-emptied-after-instanciating-mesh-at-r.html
Your answer
Follow this Question
Related Questions
AddAdditionalVertexStreams and static meshes 0 Answers
Grass slides along the terrain? How to paint vertex alpha in the new blender version for unity? 0 Answers
Mobile Transparent Vertex Color Depth Issue 1 Answer
Persistent data values in shaders 3 Answers
How to get vertex color's alpha channel to work in URP 1 Answer