3
\$\begingroup\$

I'm rendering this chunk as a Unity mesh and I have just one material (with alpha) which is applied over all the mesh.

Everything in the chunk is automatically generated at runtime, so it's not possible to prepare anything in advance, even the materials.

Here the model is rendering correctly (from a specific point of view which works):

enter image description here

The problem is this:

enter image description here

And here you see that it works if the mesh isn't rendering on itself.

enter image description here

When the material is applied, if you watch on the transparent section you will see it semi-transparent (which is the correct and expected behaviour), or you will see it just in part, because the transparency gets overlapped by the parts of the mesh which are behind the transparent part.

In some cases it even disappears or make anything that is on it disappear.

enter image description here

Does anyone know a good shader that can fix this problem and that is a diffuse? I tried to modify zTest in the shader, but nothing changes, or the render gets even worse.

Here are the two shaders:

This is the one the material uses

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

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 50

CGPROGRAM
#pragma surface surf Lambert alpha

sampler2D _MainTex;
fixed4 _Color;

struct Input {
    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;
}
ENDCG
}

Fallback "Custom/VertexLit"
}

And here is the second, which the first refers to.

Shader "Custom/VertexLit" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _SpecColor ("Spec Color", Color) = (1,1,1,0)
    _Emission ("Emissive Color", Color) = (0,0,0,0)
    _Shininess ("Shininess", Range (0.1, 1)) = 0.7
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 100

    Alphatest Greater 0
    ZWrite Off
    ZTest Always
    Blend SrcAlpha OneMinusSrcAlpha 
    ColorMask RGB

    // Non-lightmapped
    Pass {
        Tags { "LightMode" = "Vertex" }
        Material {
            Diffuse [_Color]
            Ambient [_Color]
            Shininess [_Shininess]
            Specular [_SpecColor]
            Emission [_Emission]    
        }
        Lighting On
        SeparateSpecular On
        SetTexture [_MainTex] {
            Combine texture * primary DOUBLE, texture * primary
        } 
    }

    // Lightmapped, encoded as dLDR
    Pass {
        Tags { "LightMode" = "VertexLM" }

        BindChannels {
            Bind "Vertex", vertex
            Bind "normal", normal
            Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
            Bind "texcoord", texcoord1 // main uses 1st uv
        }
        SetTexture [unity_Lightmap] {
            matrix [unity_LightmapMatrix]
            constantColor [_Color]
            combine texture * constant
        }
        SetTexture [_MainTex] {
            combine texture * previous DOUBLE, texture * primary
        }
    }

    // Lightmapped, encoded as RGBM
    Pass {
        Tags { "LightMode" = "VertexLMRGBM" }

        BindChannels {
            Bind "Vertex", vertex
            Bind "normal", normal
            Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
            Bind "texcoord1", texcoord1 // unused
            Bind "texcoord", texcoord2 // main uses 1st uv
        }

        SetTexture [unity_Lightmap] {
            matrix [unity_LightmapMatrix]
            combine texture * texture alpha DOUBLE
        }
        SetTexture [unity_Lightmap] {
            constantColor [_Color]
            combine previous * constant
        }
        SetTexture [_MainTex] {
            combine texture * previous QUAD, texture * primary
        }
    }
}

}
\$\endgroup\$
  • \$\begingroup\$ It's a little difficult to tell exactly what's happening in those little images, can you describe the problem in words a bit more? \$\endgroup\$ – MichaelHouse Sep 25 '14 at 16:59
  • \$\begingroup\$ Well, There is a mesh which is generated at runtime and there is a part of it that is textured with a texture which should be partially transparent. Then, when the material is applied (it's just one for all the mesh), if you watch on the transparent section you will see it transparent, or you will see it just in part, because the transparency gets overlapped by the parts of the mesh which are behind the transparent part. \$\endgroup\$ – Stefa168 Sep 25 '14 at 17:11
  • \$\begingroup\$ Please edit the question to include the details. \$\endgroup\$ – MichaelHouse Sep 25 '14 at 17:11
  • \$\begingroup\$ The transparent part even makes everything that is over it transparent \$\endgroup\$ – Stefa168 Sep 25 '14 at 17:13
  • \$\begingroup\$ Can you post a video? \$\endgroup\$ – Inuart Sep 25 '14 at 19:38
1
\$\begingroup\$

It's a problem with sorting order of the objects. Why are both shaders in "Transparent" queue? You could just change the ground shader's queue to "Geometry" and fix the problem. Do you use transparency on the ground objects?

If you really need the transparent queue, you could set the queue to things like "Transparent+1" to force it render later than other transparent objects:

http://docs.unity3d.com/Manual/SL-SubshaderTags.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.