# Undo barycentric interpolation

How do I undo the interpolation of vertex attributes? I'm using unity shader graph which doesn't support nointerpolation. I assume I need to store 3 float3 with one component of each float3 being 1 and the rest 0, this will give the barycentric coordinate. But I don't know how to actually use them to undo the interpolation.

• do you have access to the barycentric weights? which geometry type are you using (triangles, quads, lines)? what is the shader language (glsl, hlsl)? Jan 12, 2023 at 15:56
• @Thomas: "what is the shader language (glsl, hlsl)?" The OP said that they're "using unity shader graph", so that's the "shader language". Jan 12, 2023 at 16:25
• Unity uses triangles as the primitive type. Shader Graph uses nodes, not code, but shaders can be written in hlsl. But I'm using a render pipeline which makes it hard to write shader code, so I'd rather use Shader Graph. I think to get the barycentric coordinate, we just have 3 float3 attributes (I think they are called varyings), (1,0,0),(0,1,0),(0,0,1), Which automatically get processed in the vertex->frag stage, and those values become the barycentric coordinate in the fragment stage. @Thomas Jan 12, 2023 at 16:32
• Does shader graph have "partial derivatives" (in glsl it is the function dFdx/dFdy) within its fragment stage? If so, I think you can back calculate each vertex value... Jan 12, 2023 at 19:43
• This sounds like an xy problem. What is the real problem your trying to solve. Jan 13, 2023 at 6:22

## 1 Answer

Even with the barycentric coordinates, you cannot "undo" interpolation. Interpolation uses 3 input values and produces one output value: $$f(A, B, C) = O$$. So to reverse that would mean that you have one equation with three unknowns, ignoring the coordinate itself. You cannot solve for 3 unknowns with one equation.

If Unity's Shader Graph tool doesn't support flat interpolation, then it's probably best to solve the specific problem rather than searching for a general solution. The way to do this will be particular to exactly the effect you're trying to achieve.

• I'm using the UV coordinate for the texture index(u), and texture tilling (v), but the UV gets interpolated which makes a border of textures around different textures (it's ugly!), I've tried setting a single triangle to always only have one texture, but since I'm using shared vertices, it doesn't make a difference. @Nicol Bolas Jan 12, 2023 at 16:39
• Isn't there a way to get the 3 vertices of the triangle the current fragment is in? Jan 12, 2023 at 16:42
• @Shiv-iwnl: "since I'm using shared vertices" Well there's your problem: stop sharing vertices when the vertex data clearly is not the same between the two triangles. Triangles that use two different textures do not share texture coordinates and therefore cannot share vertices, even if the positions at those vertices are the same. "Isn't there a way to get the 3 vertices of the triangle the current fragment is in?" No. Jan 12, 2023 at 16:46
• it seems like I can still get good results if I share vertices that have the same texture id, but not share when they have different ids. a hybrid approach. And I can also assign each vertex it's unique id instead of the whole triangle Jan 12, 2023 at 18:35
• @NicolBolas mathematically speaking you are right. But there might be ways to receive more values... And with these values it is solvable. For example: when giving each vertex a 3d vector (1,0,0) (0,1,0) and (0,0,1). Then you receive in the fragment stage the barycentric coordinates. I'm not familiar with shader graph, but if they support "partial derivatives", you can receive the barycentric coordinates of the neighboring pixels and all their varyings... And I bet, than it is calculate able... Jan 12, 2023 at 20:12