Search Unity

How do I add alpha writes to this shader?

Discussion in 'Shaders' started by SoftwareGeezers, Jul 15, 2017.

  1. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    I draw the camera output to a RenderTexture with sprites drawn with the following shader (2D normal mapped sprite). The sprites appear when drawn to the screen, but are invisible when drawn to the RenderTexture. What do I need to change to write opacity to the RenderTexture? Thanks!
    Code (CSharp):
    1. Shader "SG/_ShipSurfaceShader" {
    2.     Properties {
    3.         _MainTex ("Main texture", 2D) = "white" {}
    4.         _NormalTex ("Normal map", 2D) = "bump" {}
    5.         _SpecColor ("Specular colour", Color) = (1,1,1,1)
    6.         _SpecPower ("Specular power", Range (0.0,2.0)) = 0.5
    7.     }
    8.     SubShader {
    9.         Tags { "Queue"="Transparent" }
    10.         Blend SrcAlpha OneMinusSrcAlpha
    11.         LOD 200
    12.        
    13.         CGPROGRAM
    14.         #pragma surface surf BlinnPhongTransparent alpha
    15.  
    16.         inline fixed4 LightingBlinnPhongTransparent (SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten) {
    17.             half3 h = normalize (lightDir + viewDir);
    18.             fixed diff = max (0, dot (s.Normal, lightDir));
    19.             float nh = max (0, dot (s.Normal, h));
    20.             float spec = pow (nh, s.Specular*32.0) * s.Gloss;
    21.             fixed4 c;
    22.             c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten);
    23.             c.a = s.Alpha;
    24.             return c;
    25.         }
    26.  
    27.         sampler2D _MainTex;
    28.         sampler2D _NormalTex;
    29.         half _SpecPower;
    30.  
    31.         struct Input {
    32.             float2 uv_MainTex;
    33.         };
    34.  
    35.         void surf (Input IN, inout SurfaceOutput o) {
    36.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
    37.             o.Albedo = c.rgb;
    38.             o.Alpha = c.a;
    39.             o.Normal = UnpackNormal(tex2D (_NormalTex, IN.uv_MainTex));
    40.             o.Specular = _SpecPower;
    41.             o.Gloss = 1;
    42.         }
    43.         ENDCG
    44.     }
    45.     FallBack "Diffuse"
    46. }
    47.  
    The below shows a quad with the RenderTexture applied visible in the scene. The planet and waypoint (Unity Sprites) are visible but the ship using the above shader isn't.

    Image1.png