<p>Even with the barycentric coordinates, you cannot &quot;undo&quot; interpolation. Interpolation uses 3 input values and produces one output value: <span class="math-container">$f(A, B, C) = O$</span>. So to reverse that would mean that you have one equation with <em>three</em> unknowns, ignoring the coordinate itself. You cannot solve for 3 unknowns with one equation.</p>
<p>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.</p>

CC BY-SA 4.0
2023-01-12T16:39:28.750Z
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
Shiv-iwnl
CC BY-SA 4.0
2023-01-12T16:42:18.050Z
Isn't there a way to get the 3 vertices of the triangle the current fragment is in?
Shiv-iwnl
CC BY-SA 4.0
2023-01-12T16:46:58.823Z
@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.
Nicol Bolas
CC BY-SA 4.0
2023-01-12T18:35:31.543Z
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
Shiv-iwnl
CC BY-SA 4.0
2023-01-12T20:12:07.247Z
@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...
Thomas
CC BY-SA 4.0
2023-01-12T16:33:13.143Z
2023-01-12T16:33:13.143Z
Nicol Bolas
<p>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.</p>

CC BY-SA 4.0
2023-01-12T15:56:39.353Z
do you have access to the barycentric weights? which geometry type are you using (triangles, quads, lines)? what is the shader language (glsl, hlsl)?
Thomas
CC BY-SA 4.0
2023-01-12T16:25:13.203Z
@Thomas: "*what is the shader language (glsl, hlsl)?*" The OP said that they're "using unity shader graph", so that's the "shader language".
Nicol Bolas
CC BY-SA 4.0
2023-01-12T16:32:14.427Z
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
Shiv-iwnl
CC BY-SA 4.0
2023-01-12T19:43:58.053Z
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...
Thomas
CC BY-SA 4.0
2023-01-12T21:53:29.133Z
yes, there are 3 derivative nodes, ddx, ddxy,ddy, how would I calculate them?
Shiv-iwnl
CC BY-SA 4.0
2023-01-13T06:09:43.060Z
@Thomas I was watching a video today where the ddx and ddy nodes were used to calculate the face normals in the fragment stage. The cross of the ddx and ddy of the vertex position in world space were transformed into tangent space and then used as fragment normal. Link: https://youtu.be/1HTMoCaY0xw
Shiv-iwnl
CC BY-SA 4.0
2023-01-13T06:22:11.063Z
This sounds like an xy problem. What is the real problem your trying to solve.
joojaa
CC BY-SA 4.0
2023-01-13T08:41:40.080Z
@joojaa You are right, finding a way to access each vertex parameter within the fragment stage is not the ideal way to solve the texturing problem. But I think to find a way to access these vertex parameters within the fragment stage can be used for other things, which maybe help others with similar problems.
Thomas
CC BY-SA 4.0
2023-01-13T08:50:19.470Z
@Shiv-iwnl as you can see on the youtube link, there the partial derivatives are used for normal calculation. finding neighboring (interpolated) values of a varying variable can be done by adding the value of the current fragment with the "ddx" value, with respect to the exact implementation of the "ddx" or "ddy" command. When having the left and upper barycentric coordinate and their interpolated value of the variable of interest, you can undo the interpolation. BUT: be aware, that there comes the floating point precision into the game.
Thomas
CC BY-SA 4.0
2023-01-13T08:53:54.513Z
So when your triangle is spanned over the entire screen, the neighboring barycentric coordinates will be very close to the barycentric coordinate of the current fragment. therefore the calculation of the Vertex values might be correct with respect to an high epsilon. Also be aware, that border fragments (fragments at the edge of an triangle) might give wrong results.
Thomas
<interpolation><fragment-shader>
Undo barycentric interpolation
Shiv-iwnl
# Undo barycentric interpolation

(Asked by: username_0 on 2023-01-12)

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.

**Tags:** interpolation, fragment-shader

> **Comments:**
> **username_1**: do you have access to the barycentric weights? which geometry type are you using (triangles, quads, lines)? what is the shader language (glsl, hlsl)?  
> **username_2**: @username_1: "*what is the shader language (glsl, hlsl)?*" The OP said that they're "using unity shader graph", so that's the "shader language".  
> **username_0**: 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. @username_1  
> **username_1**: 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...  
> **username_0**: yes, there are 3 derivative nodes, ddx, ddxy,ddy, how would I calculate them?  
> **username_0**: @username_1 I was watching a video today where the ddx and ddy nodes were used to calculate the face normals in the fragment stage. The cross of the ddx and ddy of the vertex position in world space were transformed into tangent space and then used as fragment normal. Link: https://youtu.be/1HTMoCaY0xw  
> **username_3**: This sounds like an xy problem. What is the real problem your trying to solve.  
> **username_1**: @username_3 You are right, finding a way to access each vertex parameter within the fragment stage is not the ideal way to solve the texturing problem. But I think to find a way to access these vertex parameters within the fragment stage can be used for other things, which maybe help others with similar problems.  
> **username_1**: @username_0 as you can see on the youtube link, there the partial derivatives are used for normal calculation. finding neighboring (interpolated) values of a varying variable can be done by adding the value of the current fragment with the "ddx" value, with respect to the exact implementation of the "ddx" or "ddy" command. When having the left and upper barycentric coordinate and their interpolated value of the variable of interest, you can undo the interpolation. BUT: be aware, that there comes the floating point precision into the game.  
> **username_1**: So when your triangle is spanned over the entire screen, the neighboring barycentric coordinates will be very close to the barycentric coordinate of the current fragment. therefore the calculation of the Vertex values might be correct with respect to an high epsilon. Also be aware, that border fragments (fragments at the edge of an triangle) might give wrong results.  

---

## 1 answers

### Answer by username_2 on 2023-01-12. Score: 0

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.

> **Comments:**
> **username_0**: 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. @username_2  
> **username_0**: Isn't there a way to get the 3 vertices of the triangle the current fragment is in?  
> **username_2**: @username_0: "*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.  
> **username_0**: 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  
> **username_1**: @username_2 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...  

---

