The interpolation mode does not apply to variables
- Dominant language
- C++
- Stars
- 20.5k
- Forks
- 2.3k
- Avg merge
- 2d 16h
- Merged PRs (30d)
- 74
Description
**Describe the bug**
Contrary to what Filament's documentation states regarding the [interpolation mode](https://google.github.io/filament/Materials.html#materialdefinitions/materialblock/vertexandattributes:interpolation), setting it to **flat** does not seem to disable interpolation.
Inspecting Filament's source code confirms this:
- the interpolation is only used for the shader inputs [LINK](https://github.com/google/filament/blob/6d44db3ca02f1b21f1597c2b5023add804552a21/libs/filamat/src/shaders/ShaderGenerator.cpp#L440)
- it is not used for the user-defined variables [LINK](https://github.com/google/filament/blob/6d44db3ca02f1b21f1597c2b5023add804552a21/libs/filamat/src/shaders/ShaderGenerator.cpp#L448)
**To Reproduce**
For my use case, I'm implementing an effect that requires the position of the first vertex of each triangle to be available in the fragment shader.
I reduced the shader to the following minimal example to demonstrate the issue (uses the filamat API).
```
builder
.shading(filamat::MaterialBuilder::Shading::UNLIT)
.interpolation(filamat::MaterialBuilder::Interpolation::FLAT)
.variable(filamat::MaterialBuilder::Variable::CUSTOM0, "myPosition")
.materialVertex(R"(
void materialVertex(inout MaterialVertexInputs material) {
// Transform the vertex position to world-space and forward it to the fragment shader
//
// Because of the flat interpolation mode, the same value should be available for all the fragments
// of a face (in practice, the position of the provoking vertex)
material.myPosition = getWorldFromModelMatrix() * vec4(getPosition().xyz, 0.0);
})")
.material(R"(
void material(inout MaterialInputs material) {
prepareMaterial(material);
// Visualize the position passed from the vertex shader as a color
material.baseColor.rgb = variable_myPosition.xyz;
})")
```
For debugging, I output the first vertex position as a color.
When applied on a unit cube, here's what I get :

**Expected behavior**
The variables should not interpolate when the interpolation mode is set to **flat**.
In the previous example, I'm expecting each triangle to be colored with a flat color.
Instead, the value passed from the vertex shader to the fragment shader is interpolated and I have a gradient.
Contributor guide
Assessment
This issue has not been assessed yet.