TL;DR: For a given point in a face/polygon how can I compute the distance between that point and all the vertices on the given face with shader nodes?
Overall intent: I want to visualize complex physical data on complex geometries. For this, I want to color each pixel based on a scalar value data from each vertex. Each vertex will be assigned a value from 0 to 1 based on a physical quantity (temperature, velocity, ...) and then mapped to an RGB value based on some colormap (viridis, parula, ...) for scientific visualization.
Current approach: I can currently assign any scalar value I want to a vertex through vertex colors within face loops using a python script (using the approach from the following threads: 1, 2, 3, 4, 5). I am using the colors as containers of 4 scalar values; each of of the 4 scalar values can then be separated and assigned specific meaning (Red can be the actual physical scalar value, Green can be the roughness, and so on.); the mapping between scalar and color is done through a color ramp which I modify to be some perceptually uniform colormap.
Problem I am trying to avoid: Because of the way blender interpolates the colors of the underlying triangles of the geometry, the colors distribution within a single face/polygon is not uniform (i.e. it does not reflect the actual distribution of the scalar field within the face) - something I learned from a previous question.
Envisioned solution: Coloring each individual pixel/point based on its coordinates and distance to the vertices of that specific face. I want to interpolate the scalar value extracted from the vertex color based on that distance and map the resulting scalar into a color. A simple test I did to showcase this was to assume one of the vertices of the cube ((x,y,z)generated = (0,0,1)) has a scalar value of 1 and all other vertices have a scalar value of 0. I computed the distance to the vertex of interest and used that as the new input to the color ramp nodes.
This yields exactly what I am looking for in terms of a color distribution that represents the scalar field.
Current problem / question: This solution works well enough for this simple situation where the distance to a single vertex is needed - I am assuming all other vertices have a scalar of 0 thus they can be ignored in the interpolation. However, the goal is to visualize complex geometries and data which means that I would need to compute the distance to all adjacent vertices in order to correctly interpolate the scalar value. So my questions essentially boil down to the TL;DR on top and also:
- Is what I am looking for feasible with this approach? Is some other approach preferable?
- In more practical terms, can I act on a face-by-face basis through the shader nodes and access the vertex data of the nodes on that face for a given pixel/point?
I am happy with answers and suggestions that steer to completely different approaches than I what I am currently trying to do, I briefly mentioned my overall intent for this very reason.
Full node setup to replicate this on the default cube: