Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unlit Shader Issues

Discussion in 'Shaders' started by megar34, Apr 14, 2021.

  1. megar34

    megar34

    Joined:
    Feb 23, 2019
    Posts:
    17
    Hello there, i created a semi-transparent unlit shader but i got few problems.

    Code (CSharp):
    1. Shader "Unlit/Shader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.         SubShader
    8.     {
    9.         Tags { "RenderType" = "Background" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             Blend One One
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.  
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float2 uv : TEXCOORD0;
    25.                 float3 normal : NORMAL;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 float4 vertex : SV_POSITION;
    32.                 float3 worldNormal : TEXCOORD1;
    33.             };
    34.  
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.  
    38.             v2f vert (appdata v)
    39.             {
    40.                 v2f o;
    41.                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
    42.                 o.vertex = UnityObjectToClipPos(v.vertex);
    43.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    44.                 return o;
    45.             }
    46.  
    47.             fixed4 frag(v2f i) : SV_Target
    48.             {
    49.                 float3 forward = float3(1, 0, 0);
    50.                 float cosa = dot(-forward, normalize(i.worldNormal));
    51.                 float depth = (1 - cosa) * abs(sin(i.uv.y * 100));
    52.                 fixed4 col = float4(0, depth, depth * 0.25, 0);
    53.                 return col;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }
    59.  
    1. How to fix the transparency?
      3124135233131.PNG
    2. How do i get camera position in unlit shader?
    3. How do i get world position/vertex world position in unlit shader?