Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Interpolator ... not interpolating?

Discussion in 'Shaders' started by Testfor, Dec 16, 2017.

  1. Testfor

    Testfor

    Joined:
    Jan 22, 2015
    Posts:
    55
    Hi!
    I wrote a shader that basically generates quads, I have one interpolator that represents the distance to the centre of the quad but I see on the fragment shader that it has the value "of the vertex" and it´s not been interpolated. Here the code:

    Code (CSharp):
    1. Shader "Unlit/ParticleShader"
    2. {
    3.     Properties
    4.     {
    5.     }
    6.     SubShader
    7.     {
    8.         Pass
    9.         {
    10.             Blend SrcAlpha OneMinusSrcAlpha
    11.             Cull Off
    12.             ZWrite Off
    13.             ZTest Always
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #pragma target 3.5
    18.             #include "UnityCG.cginc"
    19.  
    20.             struct Particle
    21.             {
    22.                 float4 Position;
    23.                 float2 Velocity;
    24.             };
    25.             StructuredBuffer<Particle> uParticles;
    26.  
    27.             struct appdata
    28.             {
    29.                 uint vid : SV_VertexID;
    30.             };
    31.             struct v2f
    32.             {
    33.                 float4 vertex    : SV_POSITION;
    34.                 float distCent    : TEXCOORD2;
    35.             };
    36.            
    37.             v2f vert (appdata v)
    38.             {
    39.                 v2f o;
    40.                 uint fetchId            = v.vid / 6;
    41.                 Particle tmpParticle    = uParticles[fetchId];
    42.                
    43.                 float3 quad[6];
    44.                 quad[0] = float3(-1.0,  1.0, 0.0);
    45.                 quad[1] = float3( 1.0, -1.0, 0.0);
    46.                 quad[2] = float3(-1.0, -1.0, 0.0);
    47.  
    48.                 quad[3] = float3(-1.0,  1.0, 0.0);
    49.                 quad[4] = float3( 1.0,  1.0, 0.0);
    50.                 quad[5] = float3( 1.0, -1.0, 0.0);
    51.  
    52.                 float aspect = _ScreenParams.x / _ScreenParams.y;
    53.  
    54.                 float dToCenter = distance(quad[v.vid % 6].xy, float2(0.0, 0.0));
    55.                 float3 pos    = tmpParticle.Position + quad[v.vid % 6];
    56.                 // ... and scale it down + aspect correction
    57.                 pos.x        /= aspect;
    58.                 pos            *= 0.1;
    59.  
    60.                 o.vertex    = float4(pos, 1.0);
    61.                 o.distCent = dToCenter;
    62.                 return o;
    63.             }
    64.            
    65.             float4 frag (v2f i) : SV_Target
    66.             {
    67.                 return float4(i.distCent * 0.5,0.0,0.0,1.0);
    68.             }
    69.             ENDCG
    70.         }
    71.     }
    72. }
    73.  
    Is this happening because the value is been generated on the shader?

    Thanks a lot!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    It's working perfectly.

    You're passing the distance from the center of a quad each vertex. By definition each vertex of a square or rectangular quad is the same distance to the center. Thus the interpolated value is constant across the tris.

    If you want the distance you'll need to actually pass the quad's xy position and do the distance() in the fragment shader.
     
  3. Testfor

    Testfor

    Joined:
    Jan 22, 2015
    Posts:
    55
    I just realised that, sleeping is a great debugging tool! Thanks!