Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Standard PBR Shader with UV2 for second overlaping textures

Discussion in 'Shaders' started by mgrigorov_bg, Feb 5, 2019.

  1. mgrigorov_bg

    mgrigorov_bg

    Joined:
    May 14, 2016
    Posts:
    23
    Hello,

    I'm trying to modify the Standard/Roughness shader.
    I want the shader to have 4 additional slots for textures which operates with UV02 channel. The takes information for Tiling X / Y and Offset X / Y from UV2.

    I've created such shader from Mobile/Diffuse, however i can't replicate it in Standard/Roughness.

    Any ideas?
    I've posted the original Shader/Roughness code from the Unity package.


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

    mgrigorov_bg

    Joined:
    May 14, 2016
    Posts:
    23
    I've attached image of what i want to achieve with PBR/Standard/Roughness.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
  4. mgrigorov_bg

    mgrigorov_bg

    Joined:
    May 14, 2016
    Posts:
    23
    It seems to me like impossible task with the sufrace shader to reproduce the PBR 1:1 and add the needed details
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    The standard shader is a very difficult shader to modify because most of its code is in 6 separate .cginc files, which you'd need to modify or at least make copies of most of them just to make minor changes.


    You can replicate everything the standard shader does in a surface shader, though by default the inspector won't be as pretty unless you write a custom editor (though you would have to write one if you modified the standard shader anyway). It's also much easier to modify.
     
    Laiken likes this.