Search Unity

Transparent shadow caster with fully opaque shadows?

Discussion in 'General Graphics' started by TrentSterling, Jan 28, 2018.

  1. TrentSterling

    TrentSterling

    Joined:
    Jan 4, 2013
    Posts:
    99
    Back in the Unity 4 days - transparent objects could only cast fully opaque shadows. In Unity 5 we got dithered shadows and PCF filtering with soft shadows.

    I've got some rooftops I'd like to fade to transparent when my character walks into a building, but I'd like for the shadows to remain fully dark/opaque. In Unity 4 this would've been the default behavior.

    The only solution I can find is to duplicate all of the meshrenderers, apply an opaque material, and then tick "Cast Shadows: shadows only".

    It seems like a really crappy solution though. Anyone got any ideas? Can we toggle this feature? Do I need some shader voodoo? Or should I seriously keep 2 copies of all my roof objects?:mad:
     
  2. TrentSterling

    TrentSterling

    Joined:
    Jan 4, 2013
    Posts:
    99
    Click here to see a gif of this in action!


    Success! Got some help on discord.

    Just use the alpha slider in the material. Works in Unity 2017.

    Thanks porkington :)


    Code (CSharp):
    1. Shader "Custom/NewSurfaceShader" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.         _Alpha ("Alpha", Range(0,1)) = 0.0
    8.     }
    9.     SubShader {
    10.         Tags { "RenderType"="Transparent" "Queue"="Transparent" }
    11.         LOD 200
    12.        
    13.         CGPROGRAM
    14.         // Physically based Standard lighting model, and enable shadows on all light types
    15.         #pragma surface surf Standard fullforwardshadows alpha:fade
    16.  
    17.         // Use shader model 3.0 target, to get nicer looking lighting
    18.         #pragma target 3.0
    19.  
    20.         sampler2D _MainTex;
    21.  
    22.         struct Input {
    23.             float2 uv_MainTex;
    24.         };
    25.  
    26.         half _Glossiness;
    27.         half _Metallic;
    28.         fixed4 _Color;
    29.         half _Alpha;
    30.  
    31.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    32.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    33.         // #pragma instancing_options assumeuniformscaling
    34.         UNITY_INSTANCING_CBUFFER_START(Props)
    35.             // put more per-instance properties here
    36.         UNITY_INSTANCING_CBUFFER_END
    37.  
    38.         void surf (Input IN, inout SurfaceOutputStandard o) {
    39.             // Albedo comes from a texture tinted by color
    40.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    41.             o.Albedo = c.rgb;
    42.             // Metallic and smoothness come from slider variables
    43.             o.Metallic = _Metallic;
    44.             o.Smoothness = _Glossiness;
    45.             o.Alpha = _Alpha;
    46.         }
    47.         ENDCG
    48.     }
    49.     FallBack "Diffuse"
    50. }
    51.  
     
    theANMATOR2b likes this.
  3. unity_fNL8P9eDljC9vA

    unity_fNL8P9eDljC9vA

    Joined:
    Jan 10, 2020
    Posts:
    2
    Could you explain on how you did it? Cheers