0
\$\begingroup\$

I'm currently trying to learn how to write shaders, so I'm getting my feet wet with Unity's Shaderlab format. One of the first things I tried was to write a surface shader that supports vertex colors being assigned from code. Unfortunately, whenever I try to apply the shader to my object, it just renders the object completely transparent.

Shader "Custom/SVCA" {
    Properties {
        _Color ("Color", Color) = (1, 1, 1, 1)
        _MainTex ("Base (RGB) Trans (A)", 2D) = "white" { }
    }

    SubShader {
        Tags {
            "Queue"="Transparent"
            "RenderType"="Transparent"
        }

        CGPROGRAM
        #pragma surface surf Lambert alpha

        struct Input {
            float2 uv_MainTex;
            float4 color;
        };

        sampler2D _MainTex;
        fixed4 _Color;

        void surf(Input IN, inout SurfaceOutput o) {
            o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb * IN.color.rgb * _Color.rgb;
            o.Alpha = tex2D(_MainTex, IN.uv_MainTex).a * IN.color.a * _Color.a;
            o.Specular = 0.2;
            o.Gloss = 1.0;
        }
        ENDCG
    }

    FallBack "Diffuse", 1
}

What is causing this issue, and how can I fix it?

\$\endgroup\$
2
  • \$\begingroup\$ It might sound obvious, but did you make sure that you assign a color where the alpha-value is non-zero? The Unity color picker also picks an alpha value but doesn't visualize it very well. \$\endgroup\$
    – Philipp
    May 24 '16 at 14:54
  • \$\begingroup\$ @Philipp Yeah, I've already verified that the alpha value is not zero in the editor. The transparency issue still persists unfortunately. \$\endgroup\$ May 24 '16 at 14:56
0
\$\begingroup\$

Apparently all that was going wrong was happening when I declared the Input structure here:

struct Input {
    float2 uv_MainTex;
    float4 color;
};

All that was going wrong was going wrong due to the fact that I forgot to explicitly specify the COLOR semantic on this line:

float4 color;

With the COLOR semantic specified, the above line of code should look like this:

float4 color : COLOR;

Always remember to specify your semantics when they're required.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .