Change a single vertex color after mesh is created?
for example, I've created a simple mesh with six vertices two triangles, and colored each vertex white at the start, and provided a vertex shader on the object that renders the mesh.
It works fine.
However, if I want to change the color of a triangle, say, defined by vertices with indexes 0, 1, and 2, at runtime after the mesh has been initially created and colored:
myMesh.colors[0] = myNewColor;
myMesh.colors[1] = myNewColor;
myMesh.colors[2] = myNewColor;
(and myNewColor debugs to: RGBA(0.496, 0.647, 0.476, 0.000)
the new color doesn't stick - if I debug the vertices immediately after the obove statements, each vertex still has the old white color.
Is there something I am missing? No where to I see in the docs that mesh.colors is read only or anything... and I;m not getting any error codes...
Thanks for your help in advance
edit: if I debug my$$anonymous$$esh.colors, the old color is there not the newly assigned color
Answer by streeetwalker · May 03, 2018 at 05:48 PM
I figured it out - mesh.color must immutable, though nowhere in the documentation does it state this. you have to make a copy of the array, modify the copy and then assign the copy to mesh.colors
All the vertex attributes are actually properties. So when reading them you actually create a copy of that array on the managed side which is filled with those attributes. The mesh is actually stored on the native C++ side so manipulating an array on the managed side of course won't magically send the changes to the native side and upload them to the GPU.
The $$anonymous$$esh documentation page actually mentions this, though a bit hidden:
$$anonymous$$odifying vertex attributes every frame: a) get vertices b) modify them c) assign them back to the mesh.
yeah, I caught that. That section of the documentation gives that as an example, but it doesn't explicitly state that it must be done that way to make it work. And yes, it must be done that way!
Your answer
Follow this Question
Related Questions
Switching Solid Color Materials for a Texture Atlas 0 Answers
How can I bake texture colors to vertex colors inside Unity? 0 Answers
Blending two textures on a model 0 Answers
How do I assign colours to specific vertices on a mesh and interpolate between them? 1 Answer
Problem with transparency of an object 0 Answers