Assign colors to triangles based on position

Hello everyone, I would like all my triangles within a mesh to have different colors based on the position of the character. I already tried some things, but I did not quite get the result I wanted.

If you think that I just have to use a VertexColor Shader then you are wrong! I want to be able to change the colors while playing. Here is a video to further explain my question: 1

In the video I created an offset on the texture based on the character it’s position. But as you can see the transitions are blurry and I would like to have solid colors instead.

I had the idea to separate a complete mesh and put every single triangle in an index. Then afterwards I would assign colors to the triangles based on the position of the character. I just have no clue how to script such a thing, because I never worked with an index before.

So does anyone have any idea how to this? If you have a better/cleaner way of solving this problem than I would love to hear about it aswell

1 Answer

1

Actually I think you are wrong :slight_smile: You need to use a Vertex Color Shader and change the colours at the vertices while you are playing. Far more efficient than creating tons of different triangle meshes that will slow everything down.

You will need to find the triangle near to the player see my answer here: http://answers.unity3d.com/questions/424974/nearest-point-on-mesh.html

–

That all said - in order to have hard breaks on the colours using either method you will need to "double up" the vertices where those break need to happen the same way as doubling creates angular faces when setting normals.

–

Could you give me some example code maybe? And I get it that my suggested method will create a lot of objects. The triangle count and vertex count will remain the same though. Is having a lot of different objects that much of a problem? I will have a look into your suggestion though. Cause its does sound a bit easier

–