Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Surface shader nointerpolation for vertex attributes

Discussion in 'Shaders' started by okluskyond, May 24, 2019.

  1. okluskyond

    okluskyond

    Joined:
    Dec 6, 2017
    Posts:
    7
    Hi,

    is it possible to not interpolate vertex attributes in surface shaders?

    Code (CSharp):
    1. struct Input
    2. {
    3.     nointerpolation float4 color;
    4.     float3 worldPos;
    5.     float3 cameraRelativeWorldPos;
    6.     float3 worldNormal;
    7.     INTERNAL_DATA
    8. };
    With this approach in surface shader color is still interpolated in fragments, in "plain hlsl" this is enough to disable interpolation.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,688
    Unfortunately, no. There's no way to use nointerpolation in Surface Shaders.

    When writing a straight vertex fragment shader, the struct you're messing with is specifically the one that the shader uses to pass data between the vertex and the fragment, which requires semantics assignments, and can optionally use things like nointerpolation.

    In Surface Shaders the Input struct is not directly used for passing data between the vertex and fragment. Instead it's used by the shader generator to determine what kind of data is needed (for variable names it recognizes), and fills that struct in from data in another struct. Custom data set in the vertex function gets copied out of a temp Input struct created for the vertex shader and packed together, so if you have two float2 values it'll pack that into a single float4. Then it'll unpack that into the Input struct created for the fragment shader.
     
  3. okluskyond

    okluskyond

    Joined:
    Dec 6, 2017
    Posts:
    7
    Thanks for your reply.

    Well then it seems that the best solution is to rewrite Surface shader as combination Vertex/Fragment shader, to achieve the specified functionality. But it's quite unfortunate that surface shaders doesn't preserve this functionality during compilation. Because I've seen other forums, including you, messing with flat shading getting to the point where this question just pops out.