Search Unity

My transparency shader isn't working on my terrrain. It's black where it should be transparent.

Discussion in 'Shaders' started by draco_nite, Jan 19, 2018.

  1. draco_nite

    draco_nite

    Joined:
    Jul 17, 2015
    Posts:
    9
    The idea is that the alpha is supposed to approach 0 as a part of the terrain reaches 0 on the Y axis. Unfortunately, it's just black.


    Shader code: https://pastebin.com/hw4usHuP
     
  2. Balikk

    Balikk

    Joined:
    Oct 23, 2014
    Posts:
    49
    Hi,

    when you are under 0 your alpha will be negative. You can clamp it between 0 and 1 to have your effect. Your shader give that :
    Code (CSharp):
    1. Shader "Unlit/unlit"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.         SubShader
    8.     {
    9.         Tags { "RenderType"="Transparent" "Queue"="Transparent" "IgnoreProjector"="True"}
    10.         LOD 100
    11.  
    12.         ZWrite off
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             // make fog work
    20.             #pragma multi_compile_fog
    21.             #pragma enable_d3d11_debug_symbols
    22.             #include "UnityCG.cginc"
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 float4 worldPos : TEXCOORD1;
    32.                 UNITY_FOG_COORDS(1)
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.          
    38.             v2f vert (appdata v)
    39.             {
    40.                 v2f o;
    41.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
    42.                 o.vertex = UnityObjectToClipPos(v.vertex);
    43.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    44.                 UNITY_TRANSFER_FOG(o,o.vertex);
    45.                 return o;
    46.             }
    47.          
    48.             fixed4 frag (v2f i) : SV_Target
    49.             {
    50.                 // sample the texture
    51.                 fixed4 col = tex2D(_MainTex, i.uv);
    52.                 // apply fog
    53.                 UNITY_APPLY_FOG(i.fogCoord, col);
    54.                 if (i.worldPos.y > 1.0) {
    55.                     col.a = 1.0;
    56.                 }
    57.                 else {
    58.                     col.a = i.worldPos.y;
    59.                 }
    60.  
    61.                 // clamp col.a to avoid negative value
    62.                 col.a = clamp (col.a,0,1);
    63.                 return col;
    64.             }
    65.             ENDCG
    66.         }
    67.     }
    68. }
    And you don't need the alpha pragma in vertex fragment, it's for surface shader.
     
    draco_nite likes this.
  3. draco_nite

    draco_nite

    Joined:
    Jul 17, 2015
    Posts:
    9
    Thanks a ton, man.
     
    Balikk likes this.