Search Unity

Yet another alpha channel / transparency issue

Discussion in 'Shaders' started by ebbo, Apr 25, 2017.

  1. ebbo

    ebbo

    Joined:
    Apr 11, 2017
    Posts:
    1
    The goal: Have the shader support both clipping of Objects and transparency.

    First i must apologize. I know people have asked how to add transparency to a shader a hundred times before but I cannot for the life of me get it to work.

    I'm working on a Project based on this blog post:
    http://www.toxicfork.com/194/per-object-clipping-planes-shader-in-unity3d-5#comment-39481

    The shader works wonders at clipping Objects and helps me a lot. However adding transparency is my achilles heel.

    From what i've found, most people seem to solve their issues by adding tags like:
    Code (Code):
    1. Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    and using a function that looks like this:
    Code (CSharp):
    1. void surf (Input IN, inout SurfaceOutput o) {
    2.       o.Albedo = _Color.rgb;
    3.       o.Emission = _Color.rgb;
    4.       o.Alpha = _Color.a;
    5.     }
    However the shader in question don't have any functions like that. Between the different SubShaders and all the Passes and #pragmas i don't know how to add such functionality.

    Here is the shader I'm working on. Unmolested.
    Code (CSharp):
    1. Shader "Custom/StandardClippable" {
    2.     Properties
    3.     {
    4.         _Color("Color", Color) = (1,1,1,1)
    5.         _MainTex("Albedo", 2D) = "white" {}
    6.        
    7.         _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
    8.  
    9.         _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
    10.         [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
    11.         _MetallicGlossMap("Metallic", 2D) = "white" {}
    12.  
    13.         _BumpScale("Scale", Float) = 1.0
    14.         _BumpMap("Normal Map", 2D) = "bump" {}
    15.  
    16.         _Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
    17.         _ParallaxMap ("Height Map", 2D) = "black" {}
    18.  
    19.         _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
    20.         _OcclusionMap("Occlusion", 2D) = "white" {}
    21.  
    22.         _EmissionColor("Color", Color) = (0,0,0)
    23.         _EmissionMap("Emission", 2D) = "white" {}
    24.        
    25.         _DetailMask("Detail Mask", 2D) = "white" {}
    26.  
    27.         _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
    28.         _DetailNormalMapScale("Scale", Float) = 1.0
    29.         _DetailNormalMap("Normal Map", 2D) = "bump" {}
    30.  
    31.         [Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0
    32.  
    33.         // UI-only data
    34.         [HideInInspector] _EmissionScaleUI("Scale", Float) = 0.0
    35.         [HideInInspector] _EmissionColorUI("Color", Color) = (1,1,1)
    36.  
    37.         // Blending state
    38.         [HideInInspector] _Mode ("__mode", Float) = 0.0
    39.         [HideInInspector] _SrcBlend ("__src", Float) = 1.0
    40.         [HideInInspector] _DstBlend ("__dst", Float) = 0.0
    41.         [HideInInspector] _ZWrite ("__zw", Float) = 1.0
    42.  
    43.         _planePos    ("Clipping Plane Position",    Vector)    = ( 0, 0, 0, 1 )
    44.         _planePos2    ("Clipping Plane Position 2",    Vector)    = ( 0, 0, 0, 1 )
    45.         _planePos3    ("Clipping Plane Position 3",    Vector)    = ( 0, 0, 0, 1 )
    46.  
    47.         _planeNorm    ("Clipping Plane Normal",    Vector)    = ( 0, 1, 0, 1 )
    48.         _planeNorm2    ("Clipping Plane Normal 2",    Vector)    = ( 0, 1, 0, 1 )
    49.         _planeNorm3    ("Clipping Plane Normal 3",    Vector)    = ( 0, 1, 0, 1 )
    50.     }
    51.  
    52.     CGINCLUDE
    53.         #define UNITY_SETUP_BRDF_INPUT MetallicSetup
    54.     ENDCG
    55.  
    56.     SubShader
    57.     {
    58.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    59.         LOD 300
    60.    
    61.  
    62.         // ------------------------------------------------------------------
    63.         //  Base forward pass (directional light, emission, lightmaps, ...)
    64.         Pass
    65.         {
    66.             Name "FORWARD"
    67.             Tags { "LightMode" = "ForwardBase" }
    68.  
    69.             Blend [_SrcBlend] [_DstBlend]
    70.             ZWrite [_ZWrite]
    71.  
    72.             CGPROGRAM
    73.             #pragma target 3.0
    74.             // TEMPORARY: GLES2.0 temporarily disabled to prevent errors spam on devices without textureCubeLodEXT
    75.             #pragma exclude_renderers gles
    76.            
    77.             // -------------------------------------
    78.                    
    79.             #pragma shader_feature _NORMALMAP
    80.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    81.             #pragma shader_feature _EMISSION
    82.             #pragma shader_feature _METALLICGLOSSMAP
    83.             #pragma shader_feature ___ _DETAIL_MULX2
    84.             #pragma shader_feature _PARALLAXMAP
    85.            
    86.             #pragma multi_compile_fwdbase
    87.             #pragma multi_compile_fog
    88.                
    89.             #pragma vertex vertForwardBase
    90.             #pragma fragment fragForwardBase
    91.  
    92.             #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE
    93.  
    94.             #include "standard_clipped.cginc"
    95.  
    96.             ENDCG
    97.         }
    98.         // ------------------------------------------------------------------
    99.         //  Additive forward pass (one light per pass)
    100.         Pass
    101.         {
    102.             Name "FORWARD_DELTA"
    103.             Tags { "LightMode" = "ForwardAdd" }
    104.             Blend [_SrcBlend] One
    105.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    106.             ZWrite Off
    107.             ZTest LEqual
    108.  
    109.             CGPROGRAM
    110.             #pragma target 3.0
    111.             // GLES2.0 temporarily disabled to prevent errors spam on devices without textureCubeLodEXT
    112.             #pragma exclude_renderers gles
    113.  
    114.             // -------------------------------------
    115.  
    116.            
    117.             #pragma shader_feature _NORMALMAP
    118.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    119.             #pragma shader_feature _METALLICGLOSSMAP
    120.             #pragma shader_feature ___ _DETAIL_MULX2
    121.             #pragma shader_feature _PARALLAXMAP
    122.            
    123.             #pragma multi_compile_fwdadd_fullshadows
    124.             #pragma multi_compile_fog
    125.            
    126.             #pragma vertex vertForwardAdd
    127.             #pragma fragment fragForwardAdd
    128.  
    129.             #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE
    130.  
    131.             #include "standard_clipped.cginc"
    132.  
    133.             ENDCG
    134.         }
    135.         // ------------------------------------------------------------------
    136.         //  Shadow rendering pass
    137.         Pass {
    138.             Name "ShadowCaster"
    139.             Tags { "LightMode" = "ShadowCaster" }
    140.            
    141.             ZWrite On ZTest LEqual
    142.  
    143.             CGPROGRAM
    144.             #pragma target 3.0
    145.             // TEMPORARY: GLES2.0 temporarily disabled to prevent errors spam on devices without textureCubeLodEXT
    146.             #pragma exclude_renderers gles
    147.            
    148.             // -------------------------------------
    149.  
    150.  
    151.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    152.             #pragma multi_compile_shadowcaster
    153.  
    154.             #pragma vertex vertShadowCaster
    155.             #pragma fragment fragShadowCaster
    156.  
    157.             #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE
    158.  
    159.             #include "standard_shadow_clipped.cginc"
    160.  
    161.             ENDCG
    162.         }
    163.         // ------------------------------------------------------------------
    164.         //  Deferred pass
    165.         Pass
    166.         {
    167.             Name "DEFERRED"
    168.             Tags { "LightMode" = "Deferred" }
    169.  
    170.             CGPROGRAM
    171.             #pragma target 3.0
    172.             // TEMPORARY: GLES2.0 temporarily disabled to prevent errors spam on devices without textureCubeLodEXT
    173.             #pragma exclude_renderers nomrt gles
    174.            
    175.  
    176.             // -------------------------------------
    177.  
    178.             #pragma shader_feature _NORMALMAP
    179.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    180.             #pragma shader_feature _EMISSION
    181.             #pragma shader_feature _METALLICGLOSSMAP
    182.             #pragma shader_feature ___ _DETAIL_MULX2
    183.             #pragma shader_feature _PARALLAXMAP
    184.  
    185.             #pragma multi_compile ___ UNITY_HDR_ON
    186.             #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
    187.             #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
    188.             #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
    189.            
    190.             #pragma vertex vertDeferred
    191.             #pragma fragment fragDeferred
    192.  
    193.             #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE
    194.  
    195.             #include "standard_clipped.cginc"
    196.  
    197.             ENDCG
    198.         }
    199.  
    200.         // ------------------------------------------------------------------
    201.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    202.         // This pass it not used during regular rendering.
    203.         Pass
    204.         {
    205.             Name "META"
    206.             Tags { "LightMode"="Meta" }
    207.  
    208.             Cull Off
    209.  
    210.             CGPROGRAM
    211.             #pragma vertex vert_meta
    212.             #pragma fragment frag_meta
    213.  
    214.             #pragma shader_feature _EMISSION
    215.             #pragma shader_feature _METALLICGLOSSMAP
    216.             #pragma shader_feature ___ _DETAIL_MULX2
    217.  
    218.             #include "UnityStandardMeta.cginc"
    219.             ENDCG
    220.         }
    221.     }
    222.  
    223.     SubShader
    224.     {
    225.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    226.         LOD 150
    227.  
    228.         // ------------------------------------------------------------------
    229.         //  Base forward pass (directional light, emission, lightmaps, ...)
    230.         Pass
    231.         {
    232.             Name "FORWARD"
    233.             Tags { "LightMode" = "ForwardBase" }
    234.  
    235.             Blend [_SrcBlend] [_DstBlend]
    236.             ZWrite [_ZWrite]
    237.  
    238.             CGPROGRAM
    239.             #pragma target 2.0
    240.            
    241.             #pragma shader_feature _NORMALMAP
    242.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    243.             #pragma shader_feature _EMISSION
    244.             #pragma shader_feature _METALLICGLOSSMAP
    245.             #pragma shader_feature ___ _DETAIL_MULX2
    246.             // SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP
    247.  
    248.             #pragma skip_variants SHADOWS_SOFT DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
    249.  
    250.             #pragma multi_compile_fwdbase
    251.             #pragma multi_compile_fog
    252.    
    253.             #pragma vertex vertForwardBase
    254.             #pragma fragment fragForwardBase
    255.  
    256.             #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE
    257.  
    258.             #include "standard_clipped.cginc"
    259.  
    260.             ENDCG
    261.         }
    262.         // ------------------------------------------------------------------
    263.         //  Additive forward pass (one light per pass)
    264.         Pass
    265.         {
    266.             Name "FORWARD_DELTA"
    267.             Tags { "LightMode" = "ForwardAdd" }
    268.             Blend [_SrcBlend] One
    269.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    270.             ZWrite Off
    271.             ZTest LEqual
    272.            
    273.             CGPROGRAM
    274.             #pragma target 2.0
    275.  
    276.             #pragma shader_feature _NORMALMAP
    277.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    278.             #pragma shader_feature _METALLICGLOSSMAP
    279.             #pragma shader_feature ___ _DETAIL_MULX2
    280.             // SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP
    281.             #pragma skip_variants SHADOWS_SOFT
    282.            
    283.             #pragma multi_compile_fwdadd_fullshadows
    284.             #pragma multi_compile_fog
    285.            
    286.             #pragma vertex vertForwardAdd
    287.             #pragma fragment fragForwardAdd
    288.  
    289.             #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE
    290.  
    291.             #include "standard_clipped.cginc"
    292.  
    293.             ENDCG
    294.         }
    295.         // ------------------------------------------------------------------
    296.         //  Shadow rendering pass
    297.         Pass {
    298.             Name "ShadowCaster"
    299.             Tags { "LightMode" = "ShadowCaster" }
    300.            
    301.             ZWrite On ZTest LEqual
    302.  
    303.             CGPROGRAM
    304.             #pragma target 2.0
    305.  
    306.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    307.             #pragma skip_variants SHADOWS_SOFT
    308.             #pragma multi_compile_shadowcaster
    309.  
    310.             #pragma vertex vertShadowCaster
    311.             #pragma fragment fragShadowCaster
    312.  
    313.             #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE
    314.  
    315.             #include "standard_shadow_clipped.cginc"
    316.  
    317.             ENDCG
    318.         }
    319.  
    320.         // ------------------------------------------------------------------
    321.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    322.         // This pass it not used during regular rendering.
    323.         Pass
    324.         {
    325.             Name "META"
    326.             Tags { "LightMode"="Meta" }
    327.  
    328.             Cull Off
    329.  
    330.             CGPROGRAM
    331.             #pragma vertex vert_meta
    332.             #pragma fragment frag_meta
    333.  
    334.             #pragma shader_feature _EMISSION
    335.             #pragma shader_feature _METALLICGLOSSMAP
    336.             #pragma shader_feature ___ _DETAIL_MULX2
    337.  
    338.             #include "UnityStandardMeta.cginc"
    339.             ENDCG
    340.         }
    341.     }
    342.  
    343.     FallBack "VertexLit"
    344.     CustomEditor "StandardShaderGUI"
    345. }
    I'm at a loss.
    Thoughts?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    That's built off of the standard shader, and still using the standard shader editor, so just enable Fade or Transparency on the material in the inspector. There shouldn't be any code changes needed at all.