- Home /
How can I add measured values to specific vertices on a mesh, then interpolate between them to get calculated values for all the vertices?
I would like to create a colour map across the surface of an entire 3D model. I have two parts to the problem as I think once I have the values assign I should be able to assign the colours:
1) I have some measured values that I want to assign to specific locations on the mesh.
2) I then want to interpolate between these measured values to get calculated values for all the vertices.
I can't seem to find a way to assign the measured values to the specific vertices. Probably a stupid question, but how do I know the locations of the vertices I want to assign the measured values to? (The model is not a regular shape) My instinct would be to make a matrix with the values of each vertex, but I'm not sure how to do this and how would I know which matrix value corresponds to a given vertex?
I am using C# to write this as a mobile application.
Answer by Harinezumi · Mar 22, 2018 at 10:49 AM
You need to create a mapping of the positions of the measured values to the positions of the vertices in the mesh. You can either do this with user input (e.g. the user clicks on parts of the mesh), or you come up with a clever algorithm (without knowing more about your project, I cannot be more specific).
For example, if you are taking temperature readings from a real-world human body at certain locations, and your mesh in the application is a human body, you can use raycasting to get the vertices they clicked on. Then you map your temperature (or whatever measurement) value to a color, and assign that color to the vertex or the texture. Then based on distance from those vertices, you interpolate on a gradient.
Assigning the color to the vertex has the disadvantage that you will need a shader that uses vertex colors (the default shader ignores them) and that the resolution of the colour map will depend on the resolution of your mesh. However, it is more intuitive how it works, and is easier to program.
Assigning colour to the texture, you need to be able to map the distance to texture space (full 3D to 2D in range [0-1]), but the mapping is not limited by the resolution of the mesh.
Thanks so much for your response!
The explanation for assigning the values and selecting vertices makes sense I think - thank you.
I have a really fine mesh so I don't think resolution should be an issue. I think this is the preferable method because I don't have a UV for the model and I've tried using blender to create one but due to the complex shape, this is proving difficult. Am I correct in thinking that using the shader means that I don't need a UV, but can apply the colour straight to the mesh assu$$anonymous$$g that the I select an appropriate shader?
Correct, if you are using a shader you don't need UVs, and especially if your mesh is fine (although it could be used for other things).
I searched for a standard shader with vertex colouring, these are the 2 links that came up (the second seems to be preferred for Unity 2017+):
https://answers.unity.com/questions/923726/unity-5-standard-shader-support-for-vertex-colors.html
https://forum.unity.com/threads/standard-shader-with-vertex-colors.316529/
That is super helpful - thank you so much :)
Your answer
Follow this Question
Related Questions
Using compute shader, how to take every vertex of a mesh and make a cube spawn and move to each one? 0 Answers
Multiple Cars not working 1 Answer
how do i fix how i make triangels in my mesh 0 Answers
Distribute terrain in zones 3 Answers
Why is my vertex array empty using GetComponent().mesh.vertices? 3 Answers