Search Unity

Question Making shader property visible in material inspector

Discussion in 'Shaders' started by Development-Boldly-XR, May 14, 2020.

  1. Development-Boldly-XR

    Development-Boldly-XR

    Joined:
    Mar 8, 2019
    Posts:
    12
    Hi everyone, I am trying to add a stencil buffer to the Unity Standard shader and the Unity Particle Unlit shader. I have managed to do so with this tutorial, but for some reason the _StencilRef is not visible in the material inspector. The read shader is working fine, except I am unable to change the _StencilRef value via the inspector. I hope someone can help me show how to make this property visible in the inspector.

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Custom/StencilBuffer/StencilRead"
    4. {
    5.     Properties
    6.     {
    7.         _Color("Color", Color) = (1,1,1,1)
    8.         _MainTex("Albedo", 2D) = "white" {}
    9.  
    10.         _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
    11.  
    12.         _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
    13.         _GlossMapScale("Smoothness Scale", Range(0.0, 1.0)) = 1.0
    14.         [Enum(Metallic Alpha,0,Albedo Alpha,1)] _SmoothnessTextureChannel ("Smoothness texture channel", Float) = 0
    15.  
    16.         [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
    17.         _MetallicGlossMap("Metallic", 2D) = "white" {}
    18.  
    19.         [ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
    20.         [ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
    21.  
    22.         _BumpScale("Scale", Float) = 1.0
    23.         [Normal] _BumpMap("Normal Map", 2D) = "bump" {}
    24.  
    25.         _Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
    26.         _ParallaxMap ("Height Map", 2D) = "black" {}
    27.  
    28.         _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
    29.         _OcclusionMap("Occlusion", 2D) = "white" {}
    30.  
    31.         _EmissionColor("Color", Color) = (0,0,0)
    32.         _EmissionMap("Emission", 2D) = "white" {}
    33.  
    34.         _DetailMask("Detail Mask", 2D) = "white" {}
    35.  
    36.         _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
    37.         _DetailNormalMapScale("Scale", Float) = 1.0
    38.         [Normal] _DetailNormalMap("Normal Map", 2D) = "bump" {}
    39.  
    40.         [Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0
    41.  
    42.         // My custom Stencil Buffer Read shader
    43.         [IntRange] _StencilRef ("Stencil Reference Value", Range(0,255)) = 0
    44.         // End Stencil Buffer Read
    45.  
    46.         // Blending state
    47.         [HideInInspector] _Mode ("__mode", Float) = 0.0
    48.         [HideInInspector] _SrcBlend ("__src", Float) = 1.0
    49.         [HideInInspector] _DstBlend ("__dst", Float) = 0.0
    50.         [HideInInspector] _ZWrite ("__zw", Float) = 1.0
    51.     }
    52.  
    53.     CGINCLUDE
    54.         #define UNITY_SETUP_BRDF_INPUT MetallicSetup
    55.     ENDCG
    56.  
    57.     SubShader
    58.     {
    59.         Tags { "RenderType"="Opaque" "Queue"="Geometry" "PerformanceChecks"="False" }
    60.         LOD 300
    61.  
    62.         // My custom Stencil Buffer Read shader
    63.         Stencil{
    64.             Ref [_StencilRef]
    65.             Comp Equal
    66.         }
    67.         // End Stencil Buffer Read
    68.  
    69.         // ------------------------------------------------------------------
    70.         //  Base forward pass (directional light, emission, lightmaps, ...)
    71.         Pass
    72.         {
    73.             Name "FORWARD"
    74.             Tags { "LightMode" = "ForwardBase" }
    75.  
    76.             Blend [_SrcBlend] [_DstBlend]
    77.             ZWrite [_ZWrite]
    78.  
    79.             CGPROGRAM
    80.             #pragma target 3.0
    81.  
    82.             // -------------------------------------
    83.  
    84.             #pragma shader_feature_local _NORMALMAP
    85.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    86.             #pragma shader_feature _EMISSION
    87.             #pragma shader_feature_local _METALLICGLOSSMAP
    88.             #pragma shader_feature_local _DETAIL_MULX2
    89.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    90.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    91.             #pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
    92.             #pragma shader_feature_local _PARALLAXMAP
    93.  
    94.             #pragma multi_compile_fwdbase
    95.             #pragma multi_compile_fog
    96.             #pragma multi_compile_instancing
    97.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    98.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    99.  
    100.             #pragma vertex vertBase
    101.             #pragma fragment fragBase
    102.             #include "UnityStandardCoreForward.cginc"
    103.  
    104.             ENDCG
    105.         }
    106.         // ------------------------------------------------------------------
    107.         //  Additive forward pass (one light per pass)
    108.         Pass
    109.         {
    110.             Name "FORWARD_DELTA"
    111.             Tags { "LightMode" = "ForwardAdd" }
    112.             Blend [_SrcBlend] One
    113.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    114.             ZWrite Off
    115.             ZTest LEqual
    116.  
    117.             CGPROGRAM
    118.             #pragma target 3.0
    119.  
    120.             // -------------------------------------
    121.  
    122.  
    123.             #pragma shader_feature_local _NORMALMAP
    124.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    125.             #pragma shader_feature_local _METALLICGLOSSMAP
    126.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    127.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    128.             #pragma shader_feature_local _DETAIL_MULX2
    129.             #pragma shader_feature_local _PARALLAXMAP
    130.  
    131.             #pragma multi_compile_fwdadd_fullshadows
    132.             #pragma multi_compile_fog
    133.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    134.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    135.  
    136.             #pragma vertex vertAdd
    137.             #pragma fragment fragAdd
    138.             #include "UnityStandardCoreForward.cginc"
    139.  
    140.             ENDCG
    141.         }
    142.         // ------------------------------------------------------------------
    143.         //  Shadow rendering pass
    144.         Pass {
    145.             Name "ShadowCaster"
    146.             Tags { "LightMode" = "ShadowCaster" }
    147.  
    148.             ZWrite On ZTest LEqual
    149.  
    150.             CGPROGRAM
    151.             #pragma target 3.0
    152.  
    153.             // -------------------------------------
    154.  
    155.  
    156.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    157.             #pragma shader_feature_local _METALLICGLOSSMAP
    158.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    159.             #pragma shader_feature_local _PARALLAXMAP
    160.             #pragma multi_compile_shadowcaster
    161.             #pragma multi_compile_instancing
    162.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    163.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    164.  
    165.             #pragma vertex vertShadowCaster
    166.             #pragma fragment fragShadowCaster
    167.  
    168.             #include "UnityStandardShadow.cginc"
    169.  
    170.             ENDCG
    171.         }
    172.         // ------------------------------------------------------------------
    173.         //  Deferred pass
    174.         Pass
    175.         {
    176.             Name "DEFERRED"
    177.             Tags { "LightMode" = "Deferred" }
    178.  
    179.             CGPROGRAM
    180.             #pragma target 3.0
    181.             #pragma exclude_renderers nomrt
    182.  
    183.  
    184.             // -------------------------------------
    185.  
    186.             #pragma shader_feature_local _NORMALMAP
    187.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    188.             #pragma shader_feature _EMISSION
    189.             #pragma shader_feature_local _METALLICGLOSSMAP
    190.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    191.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    192.             #pragma shader_feature_local _DETAIL_MULX2
    193.             #pragma shader_feature_local _PARALLAXMAP
    194.  
    195.             #pragma multi_compile_prepassfinal
    196.             #pragma multi_compile_instancing
    197.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    198.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    199.  
    200.             #pragma vertex vertDeferred
    201.             #pragma fragment fragDeferred
    202.  
    203.             #include "UnityStandardCore.cginc"
    204.  
    205.             ENDCG
    206.         }
    207.  
    208.         // ------------------------------------------------------------------
    209.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    210.         // This pass it not used during regular rendering.
    211.         Pass
    212.         {
    213.             Name "META"
    214.             Tags { "LightMode"="Meta" }
    215.  
    216.             Cull Off
    217.  
    218.             CGPROGRAM
    219.             #pragma vertex vert_meta
    220.             #pragma fragment frag_meta
    221.  
    222.             #pragma shader_feature _EMISSION
    223.             #pragma shader_feature_local _METALLICGLOSSMAP
    224.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    225.             #pragma shader_feature_local _DETAIL_MULX2
    226.             #pragma shader_feature EDITOR_VISUALIZATION
    227.  
    228.             #include "UnityStandardMeta.cginc"
    229.             ENDCG
    230.         }
    231.     }
    232.  
    233.     SubShader
    234.     {
    235.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    236.         LOD 150
    237.  
    238.         // ------------------------------------------------------------------
    239.         //  Base forward pass (directional light, emission, lightmaps, ...)
    240.         Pass
    241.         {
    242.             Name "FORWARD"
    243.             Tags { "LightMode" = "ForwardBase" }
    244.  
    245.             Blend [_SrcBlend] [_DstBlend]
    246.             ZWrite [_ZWrite]
    247.  
    248.             CGPROGRAM
    249.             #pragma target 2.0
    250.  
    251.             #pragma shader_feature_local _NORMALMAP
    252.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    253.             #pragma shader_feature _EMISSION
    254.             #pragma shader_feature_local _METALLICGLOSSMAP
    255.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    256.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    257.             #pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
    258.             // SM2.0: NOT SUPPORTED shader_feature_local _DETAIL_MULX2
    259.             // SM2.0: NOT SUPPORTED shader_feature_local _PARALLAXMAP
    260.  
    261.             #pragma skip_variants SHADOWS_SOFT DIRLIGHTMAP_COMBINED
    262.  
    263.             #pragma multi_compile_fwdbase
    264.             #pragma multi_compile_fog
    265.  
    266.             #pragma vertex vertBase
    267.             #pragma fragment fragBase
    268.             #include "UnityStandardCoreForward.cginc"
    269.  
    270.             ENDCG
    271.         }
    272.         // ------------------------------------------------------------------
    273.         //  Additive forward pass (one light per pass)
    274.         Pass
    275.         {
    276.             Name "FORWARD_DELTA"
    277.             Tags { "LightMode" = "ForwardAdd" }
    278.             Blend [_SrcBlend] One
    279.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    280.             ZWrite Off
    281.             ZTest LEqual
    282.  
    283.             CGPROGRAM
    284.             #pragma target 2.0
    285.  
    286.             #pragma shader_feature_local _NORMALMAP
    287.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    288.             #pragma shader_feature_local _METALLICGLOSSMAP
    289.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    290.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    291.             #pragma shader_feature_local _DETAIL_MULX2
    292.             // SM2.0: NOT SUPPORTED shader_feature_local _PARALLAXMAP
    293.             #pragma skip_variants SHADOWS_SOFT
    294.  
    295.             #pragma multi_compile_fwdadd_fullshadows
    296.             #pragma multi_compile_fog
    297.  
    298.             #pragma vertex vertAdd
    299.             #pragma fragment fragAdd
    300.             #include "UnityStandardCoreForward.cginc"
    301.  
    302.             ENDCG
    303.         }
    304.         // ------------------------------------------------------------------
    305.         //  Shadow rendering pass
    306.         Pass {
    307.             Name "ShadowCaster"
    308.             Tags { "LightMode" = "ShadowCaster" }
    309.  
    310.             ZWrite On ZTest LEqual
    311.  
    312.             CGPROGRAM
    313.             #pragma target 2.0
    314.  
    315.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    316.             #pragma shader_feature_local _METALLICGLOSSMAP
    317.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    318.             #pragma skip_variants SHADOWS_SOFT
    319.             #pragma multi_compile_shadowcaster
    320.  
    321.             #pragma vertex vertShadowCaster
    322.             #pragma fragment fragShadowCaster
    323.  
    324.             #include "UnityStandardShadow.cginc"
    325.  
    326.             ENDCG
    327.         }
    328.  
    329.         // ------------------------------------------------------------------
    330.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    331.         // This pass it not used during regular rendering.
    332.         Pass
    333.         {
    334.             Name "META"
    335.             Tags { "LightMode"="Meta" }
    336.  
    337.             Cull Off
    338.  
    339.             CGPROGRAM
    340.             #pragma vertex vert_meta
    341.             #pragma fragment frag_meta
    342.  
    343.             #pragma shader_feature _EMISSION
    344.             #pragma shader_feature_local _METALLICGLOSSMAP
    345.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    346.             #pragma shader_feature_local _DETAIL_MULX2
    347.             #pragma shader_feature EDITOR_VISUALIZATION
    348.  
    349.             #include "UnityStandardMeta.cginc"
    350.             ENDCG
    351.         }
    352.     }
    353.  
    354.  
    355.     FallBack "VertexLit"
    356.     CustomEditor "StandardShaderGUI"
    357. }
    358.  
    To be a bit more clear, the only things I have added are;
    Code (CSharp):
    1. [IntRange] _StencilRef ("Stencil Reference Value", Range(0,255)) = 0
    2.  
    3. Stencil{
    4.             Ref [_StencilRef]
    5.             Comp Equal
    6.         }
    I don't have any experience with writing shaders myself, so any elaboration will be much appreciated!

    Thanks in advance!
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    You're using
    CustomEditor "StandardShaderGUI"
    which is Unity's custom material editor code for the standard shaders, thus it only shows the properties that are contained in those shaders and not any custom properties. Remove that line to show all the properties (though you will lose some styling), or make a copy of the StandardShaderGUI from the built-in shaders package you can download on a unity version's download page and then modify it to display that extra property.
     
  3. Development-Boldly-XR

    Development-Boldly-XR

    Joined:
    Mar 8, 2019
    Posts:
    12
    @Invertex Thanks alot that works. Initially I tried editing the StandardShaderGUI, but for some reason I get a "'BuildTargetDiscovery' is inaccessible due to its protection level" error, which I can't seem to resolve. Removing the CustomEditor "StandardShaderGUI" line indeed loses some styling, but everything is accessible and that's whats most important!
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Yeah you can't just edit the StandardShaderGUI file, since Unity already has its own copy that you'd be conflicting with. You have to copy, rename the class name and file name and then set your custom editor in your shader to that new name. Also should make sure it's put in an "Editor" folder since it uses Editor code and thus shouldn't be included in build.
     
    SanyaBane and guywald like this.
  5. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    If you put your own StandardShaderGUI file - it doesn't conflict with the Unity built in file.
    It overrides the Unity built in file and Unity would use this instead of its own one.

    But in order to use a StandardShaderGUI file duplicate - you will have to get rid of BuildTargetDiscovery from it - or somehow provide access to it.
    Otherwise you will get this error CS0122: 'BuildTargetDiscovery' is inaccessible due to its protection level.