2
\$\begingroup\$

I've been writing a custom surface shader which allows for vertex colors (with alphas) to be set programmatically. From test runs of the shader itself, it works perfectly fine, and I am able to set the color of vertices programmatically. Unfortunately, I ran into some other issues. Currently, the shader is rendering mesh faces quite strangely, and I cannot figure out why:

enter image description here

enter image description here

The shader code itself currently looks like this:

Shader "Custom/VertexColorSurface" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" { }
    }

    SubShader {
        Tags { "RenderType"="Cutout" }

        CGPROGRAM
        #pragma surface surf Lambert alpha
        #pragma target 3.0

        struct Input {
            float2 uv_MainTex;
            float4 color : COLOR;
        };

        sampler2D _MainTex;
        fixed4 _Color;

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

Currently the two solutions I've tried have been using ZWrite On and ColorMask 0 in a Pass block, and while both of these methods fix the face rendering issues, they totally screw up alpha blending, which is something I need.

What might be causing this issue, and how do I fix it?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

This is caused by the order in which unity normally renders objects. Transparent objects are the glitchy exception to this order, as unity tries to calculate draw only one transparent object per pixel for what it thinks are opaque objects.

There are a few approaches to this issue. You could use the Pass solution with Zwrite On and ColorMask 0, but in your case, you may simply want to update your tags:

Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}

For more info on what these tags are doing, check out SubShader Tags

Alternatively, if you don't want any transparency, you may want to consider using the "Standard" shader and select the "Cutout" rendering mode.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You may want to use "Queue"="AlphaTest" if you're using Transparent Cutout rendering, since this will render before translucent objects. \$\endgroup\$
    – DMGregory
    Jun 28, 2016 at 19:56

You must log in to answer this question.

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