Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:14:39+00:00 2026-06-16T00:14:39+00:00

I’m using GC for writing shaders inside Unity3D . I’m using vertex colors attributes

  • 0

I’m using GC for writing shaders inside Unity3D.

I’m using vertex colors attributes for passing some parameters to the shader. They won’t be used so for defining colors, and should be forwarded from vertex shader to pixel shader without modifyng them.

This is the structure I’m taking as input from Unity3D to the vertex shader:

struct appdata_full {
    float4 vertex : POSITION;
    float4 tangent : TANGENT;
    float3 normal : NORMAL;
    float4 texcoord : TEXCOORD0;
    float4 texcoord1 : TEXCOORD1;
    fixed4 color : COLOR;
#if defined(SHADER_API_XBOX360)
    half4 texcoord2 : TEXCOORD2;
    half4 texcoord3 : TEXCOORD3;
    half4 texcoord4 : TEXCOORD4;
    half4 texcoord5 : TEXCOORD5;
#endif
};

This is the structure returned by vertex shader as input to the fragment:

struct v2f {
  float4 pos : SV_POSITION;
  float2  uv : TEXCOORD0;
  fixed4 col: COLOR;           
};

If I simply forward the parameter to the fragment shader, of course it will be interpolated:

v2f vert (appdata_full v)
{

  v2f output;
  //....
  output.col = v.color;
}

I’d like to pass v.color parameter not interpolated to the fragment shader.
Is this possible?if yes how?


EDIT

like Tim pointed out, this is the expected behavior, because of the shader can’t do anything else than interpolating colors if those are passed out from vertex shader to fragment.
I’ll try to explain better what I’m trying to achieve. I’m using per vertex colors to store other kind of information than colors. Without telling all details on what I’m doing with that, let’s say you can consider each color vertex as an id(each vertex of the same triangle, will have the same color. Actually each vertex of the same mesh).

So I used the color trick to mask some parameters because I have no other way to do this. Now this piece of information must be available at the fragment shader in some way.
If a pass as an out parameter of the vertex shader, this information encoded into a color will arrive interpolated at the fragment, that can’t no longer use it.

I’m looking for a way of propagating this information unchanged till the fragment shader (maybe is possible to use a global variable or something like that?if yes how?).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:14:39+00:00Added an answer on June 16, 2026 at 12:14 am

    I’m not sure this counts for an answer but it’s a little much for a comment. As Bjorke points out, the fragment shader will always receive an interpolated value. If/when Unity supports Opengl 4.0 you might have access to Interpolation qualifiers, namely ‘flat’ that disables interpolation, deriving all values from a provoking vertex.

    That said, the problem with trying to assign the same “color” value to all vertices of a triangle is that the vertex shader iterates over the vertices once, not per triangle. There will always be a “boundary” region where some vertex shares multiple edges with other vertices of a different “color” or “id”, see my dumb example below. When applied to a box at (0,0,0), the top will be red, the bottom green, and the middle blue.

    Shader "custom/colorbyheight" {
    Properties {
     _Unique_ID ("Unique Identifier", float) = 1.0
    }
    SubShader {
    Pass {
      CGPROGRAM
      #pragma vertex vert
      #pragma fragment frag
      #include "UnityCG.cginc"
      struct v2f {
          float4 pos : SV_POSITION;
          fixed4 color : COLOR;
      };
      uniform float _Unique_ID;
      v2f vert (appdata_base v)
      {
          v2f o;
          o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
          float3 worldpos = mul(_Object2World, v.vertex).xyz;
          if(worldpos[1] >= 0.0)
            o.color.xyz = 0.35;  //unique_id = 0.35
          else
            o.color.xyz = 0.1;   //unique_id = 0.1
          o.color.w = 1.0;
          return o;
      }
    
    
      fixed4 frag (v2f i) : COLOR0 { 
        // local unique_id's set by the vertex shader and stored in the color
        if(i.color.x >= 0.349 && i.color.x <=0.351)
            return float4(1.0,0.0,0.0,1.0); //red
        else if(i.color.x >= 0.099 && i.color.x <=0.11)
            return float4(0.0,1.0,0.0,1.0); //green
    
        // global unique_id set by a Unity script
        if(_Unique_ID == 42.0)
            return float4(1.0,1.0,1.0,1.0); //white
    
        // Fallback color = blue
        return float4(0.0,0.0,1.0,1.0);
      }
      ENDCG
    }
    } 
    }
    

    In your addendum note you say “Actually each vertex of the same mesh.” If that’s the case, why not use a modifiable property, like I have included above. Each mesh just needs a script then to change the unique_id.

    public class ModifyShader : MonoBehaviour {
    public float unique_id = 1;
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
        renderer.material.SetFloat( "_Unique_ID", unique_id );
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.