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

Need to add alpha to a custom shader

Discussion in 'General Discussion' started by dabandabanda, Jun 20, 2019.

  1. dabandabanda

    dabandabanda

    Joined:
    Dec 20, 2018
    Posts:
    4
    I've been trying to modify this " image projection" shader by adding a controllable alpha channel. I'm not that good with shaders yet, so I'm seeking for help. Here is the code that works without any alpha implementations. Thanks!
    Code (CSharp):
    1. Shader"Custom/DiffuseWithShadow"
    2. {
    3. Properties
    4. {
    5.     _MainTex("Main Texture", 2D) = "white" {}
    6.     _ShadowMap("Shadow Map", 2D) = "white" {}
    7.     _Angle("Angle", Float) = 0.0
    8. }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType" = "Opaque" }
    12.         LOD 200
    13.  
    14. Pass
    15.         {
    16.             CGPROGRAM
    17.             // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
    18.             #pragma exclude_renderers gles
    19.  
    20.             #pragma vertex v
    21.             #pragma fragment p
    22.  
    23.             uniform sampler2D _MainTex;
    24.             uniform sampler2D _ShadowMap;
    25.             uniform float4x4 _LightViewProj;
    26.             uniform float _Angle;
    27.  
    28.  
    29.             struct VertexOut
    30.             {
    31.                 float4 position : POSITION;
    32.                 float2 uv : TEXCOORD0;
    33.                 float4 proj : TEXCOORD1;
    34.             };
    35.  
    36.             VertexOut v(float4 position : POSITION, float2 uv : TEXCOORD0)
    37.             {
    38.                 VertexOut OUT;
    39.  
    40.                 OUT.position = UnityObjectToClipPos(position);
    41.                 OUT.uv = uv;
    42.                 OUT.proj = mul(mul(unity_ObjectToWorld, float4(position.xyz, 1)), _LightViewProj);
    43.  
    44.                 return OUT;
    45.             }
    46.  
    47.             struct PixelOut
    48.             {
    49.                 float4  color : COLOR;
    50.             };
    51.  
    52.             PixelOut p(VertexOut IN)
    53.             {
    54.                 PixelOut OUT;
    55.  
    56.                 float2 ndc = float2(IN.proj.x / IN.proj.w, IN.proj.y / IN.proj.w);
    57.                 float2 uv = (1 + float2(ndc.x, ndc.y)) * 0.5;
    58.  
    59.                 float theta = _Angle * 3.14159 / 180;
    60.                 float2x2 matRot = float2x2(cos(theta), sin(theta),
    61.                                             -sin(theta), cos(theta));
    62.                 uv = mul(uv, matRot);
    63.  
    64.                 float4 c = tex2D(_ShadowMap, uv);
    65.  
    66.                 if (uv.x < 0 || uv.y < 0 ||
    67.                     uv.x > 1 || uv.y > 1 || c.a <= 0.00f)
    68.                     {
    69.                         c = tex2D(_MainTex, IN.uv);
    70.                     }
    71.  
    72.                 OUT.color = c;
    73.  
    74.                 return OUT;
    75.             }
    76.  
    77.             ENDCG
    78.         }
    79.  
    80.     }
    81.         FallBack"Diffuse"
    82.   }
    I found some useful sources, but I'm surely missing something. Here is why I've tried Add alpha to shader in Unity3D