Skip to content

The interpolation mode does not apply to variables #8778

@merwaaan

Description

@merwaaan

Describe the bug

Contrary to what Filament's documentation states regarding the interpolation mode, 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
  • it is not used for the user-defined variables LINK

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 :

Image

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.

Metadata

Metadata

Assignees

Labels

materialsIssue/request for the material system

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions