0
\$\begingroup\$

I am very much new to custom shaders. My actual problem is that I want to fade a mesh in and out via C# in Unity. However, the materials on the mesh I am using are opaque and setting them to transparent was causing me headaches with how the mesh appears (parts of the mesh were showing through the front / facing the camera in weird ways).

So I turned to shaders and I looked up a very basic shader implementation. Once this shader is applied to the material, I'm able to set the alpha channel quite easily while maintaining the general look of the mesh.

However, just changing from the Standard shader to this basic one, causes the mesh to look different. I thought it was lighting related, but I have tried to search for more information and I'm coming up short. I also wondered whether it was the Blend part of it below but I have tried a few combinations and none seem to work.

Shader problem

Here's the shader I'm using ... I believe this is just the most basic they can be:

Shader "Custom/TransparentShader"
{
    Properties
    {
        _Color("Color", Color) = (1,1,1,1)
        _MainTex("Texture", 2D) = "white" {}
    }
        SubShader
    {
        Blend SrcAlpha OneMinusSrcAlpha

        Pass {
            CGPROGRAM

            #include "UnityCG.cginc"

            #pragma vertex vert
            #pragma fragment frag

            sampler2D _MainTex;
            float4 _MainTex_ST;

            fixed4 _Color;

            struct appdata {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f {
                float4 position : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

            v2f vert(appdata v) {
                v2f o;
                o.position = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag(v2f i) : SV_TARGET{
                fixed4 col = tex2D(_MainTex, i.uv);
                col *= _Color;
                return col;
            }

            ENDCG
        }
    }
}

Is there an obvious (noobie) reason the mesh looks so different with this basic shader?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Myself, I'd keep using the transparent version of the standard shader rather than making your own, then fix the root problem of the depth ordering being wrong with a depth reservation pass. \$\endgroup\$
    – DMGregory
    Commented Aug 28, 2022 at 16:44
  • \$\begingroup\$ I'm sorry @DMGregory ... I've had a look at the linked answer and when I use this the entire mesh disappears. Did I misunderstand the answer? I changed the material to use the shader provided in the answer. I actually had no idea rendering transparent things was so difficult. I guess my existing understanding of things are in a 2D landscape as this is one of my first journeys into 3D. \$\endgroup\$ Commented Aug 29, 2022 at 3:32
  • \$\begingroup\$ You did misunderstand the answer. You need to render the object twice. Once, first, with the depth reservation shader from my answer. (this draws no visible pixels — it only writes to the depth buffer) Then a second time with the standard transparent shader (this actually draws shaded pixels from the object with the depth reservation blocking the "wrong" pixels from rendering). \$\endgroup\$
    – DMGregory
    Commented Aug 29, 2022 at 3:33
  • \$\begingroup\$ Ah. Okay well I'm back at work now so I'll have to give that another go later tonight. Thanks for explaining it to a noob like me. Really appreciate it. \$\endgroup\$ Commented Aug 29, 2022 at 4:55

1 Answer 1

0
\$\begingroup\$

The lack of actual light calculations causes this. The standard unity shader applies many lighting techniques that make that mesh stand out more. In your shader, you only use color to blend your texture.

\$\endgroup\$
1
  • \$\begingroup\$ You should probably go into more detail. I wouldn't expect this limited information to be helpful to someone who's new to shaders. \$\endgroup\$
    – Kevin
    Commented Aug 30, 2022 at 17:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.