Search Unity

Question PBR shader with aditional separate texture mask

Discussion in 'Shaders' started by L5-SevenD, Apr 13, 2021.

  1. L5-SevenD

    L5-SevenD

    Joined:
    Oct 24, 2018
    Posts:
    1
    Hello,

    I have a problem with a shader which I can't solve.
    In this shader I want to use a render texture from a camera as albedo or base color including its transparency.
    Additionally I want to mask this texture with a separate black & white mask so it is only projected on the parts of the model where the mask allows it.

    I am no shader programmer so I'm afraid can't solve this by myself.
    Ideally it should work as the standard pbr or the autodesk interactive shader with the additional mask.

    The shader I have so far looks like this but it does not work:


    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Custom/PrintMask"
    4. {
    5.     Properties {
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.         [NoScaleOffset] _MainTex ("Albedo (RGB)", 2D) = "white" {}
    8.         [Toggle] _UseMetallicMap ("Use Metallic Map", Float) = 0.0
    9.         [NoScaleOffset] _MetallicGlossMap("Metallic", 2D) = "black" {}
    10.         [Gamma] _Metallic ("Metallic", Range(0,1)) = 0.0
    11.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    12.         _BumpScale("Scale", Float) = 1.0
    13.         [NoScaleOffset] _BumpMap("Normal Map", 2D) = "bump" {}
    14.         _Mask ("Culling Mask", 2D) = "white" {}
    15.         _Cutoff("Alpha Cutoff", Range(0.01,1)) = 0.5
    16.     }
    17.  
    18.     SubShader {
    19.  
    20.         Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
    21.         Blend SrcAlpha OneMinusSrcAlpha
    22.         LOD 200
    23.         ZWrite Off
    24.         Cull Off
    25.  
    26.         Pass { // Prepass depth write
    27.             ColorMask 0
    28.             ZWrite On
    29.             CGPROGRAM
    30.             #pragma vertex vert
    31.             #pragma fragment frag
    32.  
    33.             #include "UnityCG.cginc"
    34.             struct v2f {
    35.                 float4 vertex : SV_POSITION;
    36.                 float2 texcoord : TEXCOORD0;
    37.             };
    38.             sampler2D _MainTex;
    39.             fixed _Cutoff;
    40.             v2f vert (appdata_img v)
    41.             {
    42.                 v2f o;
    43.                 o.vertex = UnityObjectToClipPos(v.vertex);
    44.                 o.texcoord = v.texcoord;
    45.                 return o;
    46.             }
    47.             fixed4 frag (v2f i) : SV_Target
    48.             {
    49.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    50.                 clip(col.a - _Cutoff);
    51.                 return 0;
    52.             }
    53.             ENDCG
    54.         }
    55.  
    56.         Pass {
    57.             Tags {"LightMode"="ShadowCaster"}
    58.             ZWrite On
    59.             Cull Off
    60.             CGPROGRAM
    61.             #pragma vertex vert
    62.             #pragma fragment frag
    63.             #pragma multi_compile_shadowcaster
    64.             #include "UnityCG.cginc"
    65.             struct v2f {
    66.                 V2F_SHADOW_CASTER;
    67.                 float2 texcoord : TEXCOORD1;
    68.             };
    69.             v2f vert(appdata_base v)
    70.             {
    71.                 v2f o;
    72.                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    73.                 o.texcoord = v.texcoord;
    74.                 return o;
    75.             }
    76.  
    77.             sampler2D _MainTex;
    78.             fixed _Cutoff;
    79.             float4 frag(v2f i) : SV_Target
    80.             {
    81.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    82.                 clip(col.a - _Cutoff);
    83.                 SHADOW_CASTER_FRAGMENT(i)
    84.             }
    85.             ENDCG
    86.         }
    87.  
    88.         CGPROGRAM
    89.         #pragma surface surf Standard fullforwardshadows alpha:fade nolightmap
    90.         #pragma shader_feature _USEMETALLICMAP_ON
    91.         #pragma target 3.0
    92.         sampler2D _MainTex;
    93.         sampler2D _MetallicGlossMap;
    94.         sampler2D _BumpMap;
    95.         struct Input {
    96.             float2 uv_MainTex;
    97.             fixed facing : VFACE;
    98.         };
    99.         half _Glossiness;
    100.         half _Metallic;
    101.         fixed4 _Color;
    102.         half _BumpScale;
    103.         fixed _Cutoff;
    104.         void surf (Input IN, inout SurfaceOutputStandard o) {
    105.             // Albedo comes from a texture tinted by color
    106.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    107.             o.Albedo = c.rgb;
    108.             #ifdef _USEMETALLICMAP_ON
    109.             fixed4 mg = tex2D(_MetallicGlossMap, IN.uv_MainTex);
    110.             o.Metallic = mg.r;
    111.             o.Smoothness = mg.a;
    112.             #else
    113.             o.Metallic = _Metallic;
    114.             o.Smoothness = _Glossiness;
    115.             #endif
    116.             // Rescales the alpha on the blended pass
    117.             o.Alpha = saturate(c.a / _Cutoff);
    118.             o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale);
    119.             o.Normal.z *= IN.facing;
    120.         }
    121.         ENDCG
    122.  
    123.         Blend SrcAlpha OneMinusSrcAlpha
    124.         Pass
    125.         {
    126.           SetTexture [_Mask] {combine texture}
    127.           SetTexture [_MainTex] {combine texture, previous}
    128.         }
    129.     }
    130.  
    131.     FallBack "Diffuse"
    132. }
    133.  
    I hope someone can help me with this problem.