Search Unity

Question Any idea how to add metallic map to this?

Discussion in 'Shaders' started by GradientOGames, Sep 26, 2022.

  1. GradientOGames

    GradientOGames

    Joined:
    Feb 8, 2021
    Posts:
    28
    I'd like to know how I would add a metallic map to the urp 2d sprite-lit-default.

    As far as I know with shader stuff, I need to add something like:
    "_MetallicMap("Metallic Map", 2D) = "white" {}" (correct me if I'm wrong)

    Code (CSharp):
    1. Shader "Universal Render Pipeline/2D/Sprite-Lit-Default"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Diffuse", 2D) = "white" {}
    6.         _MaskTex("Mask", 2D) = "white" {}
    7.         _NormalMap("Normal Map", 2D) = "bump" {}
    8.  
    9.         // Legacy properties. They're here so that materials using this shader can gracefully fallback to the legacy sprite shader.
    10.         [HideInInspector] _Color("Tint", Color) = (1,1,1,1)
    11.         [HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
    12.         [HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
    13.         [HideInInspector] _AlphaTex("External Alpha", 2D) = "white" {}
    14.         [HideInInspector] _EnableExternalAlpha("Enable External Alpha", Float) = 0
    15.     }
    16.  
    17.     HLSLINCLUDE
    18.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    19.     ENDHLSL
    20.  
    21.     SubShader
    22.     {
    23.         Tags {"Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
    24.  
    25.         Blend SrcAlpha OneMinusSrcAlpha
    26.         Cull Off
    27.         ZWrite Off
    28.  
    29.         Pass
    30.         {
    31.             Tags { "LightMode" = "Universal2D" }
    32.             HLSLPROGRAM
    33.             #pragma vertex CombinedShapeLightVertex
    34.             #pragma fragment CombinedShapeLightFragment
    35.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
    36.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
    37.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
    38.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
    39.  
    40.             struct Attributes
    41.             {
    42.                 float3 positionOS   : POSITION;
    43.                 float4 color        : COLOR;
    44.                 float2  uv           : TEXCOORD0;
    45.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    46.             };
    47.  
    48.             struct Varyings
    49.             {
    50.                 float4  positionCS  : SV_POSITION;
    51.                 half4   color       : COLOR;
    52.                 float2    uv          : TEXCOORD0;
    53.                 half2    lightingUV  : TEXCOORD1;
    54.                 UNITY_VERTEX_OUTPUT_STEREO
    55.             };
    56.  
    57.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
    58.  
    59.             TEXTURE2D(_MainTex);
    60.             SAMPLER(sampler_MainTex);
    61.             TEXTURE2D(_MaskTex);
    62.             SAMPLER(sampler_MaskTex);
    63.             TEXTURE2D(_NormalMap);
    64.             SAMPLER(sampler_NormalMap);
    65.             half4 _MainTex_ST;
    66.             half4 _NormalMap_ST;
    67.  
    68.             #if USE_SHAPE_LIGHT_TYPE_0
    69.             SHAPE_LIGHT(0)
    70.             #endif
    71.  
    72.             #if USE_SHAPE_LIGHT_TYPE_1
    73.             SHAPE_LIGHT(1)
    74.             #endif
    75.  
    76.             #if USE_SHAPE_LIGHT_TYPE_2
    77.             SHAPE_LIGHT(2)
    78.             #endif
    79.  
    80.             #if USE_SHAPE_LIGHT_TYPE_3
    81.             SHAPE_LIGHT(3)
    82.             #endif
    83.  
    84.             Varyings CombinedShapeLightVertex(Attributes v)
    85.             {
    86.                 Varyings o = (Varyings)0;
    87.                 UNITY_SETUP_INSTANCE_ID(v);
    88.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    89.  
    90.                 o.positionCS = TransformObjectToHClip(v.positionOS);
    91.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    92.                 float4 clipVertex = o.positionCS / o.positionCS.w;
    93.                 o.lightingUV = ComputeScreenPos(clipVertex).xy;
    94.                 o.color = v.color;
    95.                 return o;
    96.             }
    97.  
    98.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
    99.  
    100.             half4 CombinedShapeLightFragment(Varyings i) : SV_Target
    101.             {
    102.                 half4 main = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    103.                 half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);
    104.  
    105.                 return CombinedShapeLightShared(main, mask, i.lightingUV);
    106.             }
    107.             ENDHLSL
    108.         }
    109.  
    110.         Pass
    111.         {
    112.             Tags { "LightMode" = "NormalsRendering"}
    113.             HLSLPROGRAM
    114.             #pragma vertex NormalsRenderingVertex
    115.             #pragma fragment NormalsRenderingFragment
    116.  
    117.             struct Attributes
    118.             {
    119.                 float3 positionOS   : POSITION;
    120.                 float4 color        : COLOR;
    121.                 float2 uv            : TEXCOORD0;
    122.                 float4 tangent      : TANGENT;
    123.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    124.             };
    125.  
    126.             struct Varyings
    127.             {
    128.                 float4  positionCS        : SV_POSITION;
    129.                 half4   color            : COLOR;
    130.                 float2    uv                : TEXCOORD0;
    131.                 half3   normalWS        : TEXCOORD1;
    132.                 half3   tangentWS        : TEXCOORD2;
    133.                 half3   bitangentWS        : TEXCOORD3;
    134.                 UNITY_VERTEX_OUTPUT_STEREO
    135.             };
    136.  
    137.             TEXTURE2D(_MainTex);
    138.             SAMPLER(sampler_MainTex);
    139.             TEXTURE2D(_NormalMap);
    140.             SAMPLER(sampler_NormalMap);
    141.             half4 _NormalMap_ST;  // Is this the right way to do this?
    142.  
    143.             Varyings NormalsRenderingVertex(Attributes attributes)
    144.             {
    145.                 Varyings o = (Varyings)0;
    146.                 UNITY_SETUP_INSTANCE_ID(attributes);
    147.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    148.  
    149.                 o.positionCS = TransformObjectToHClip(attributes.positionOS);
    150.                 o.uv = TRANSFORM_TEX(attributes.uv, _NormalMap);
    151.                 o.uv = attributes.uv;
    152.                 o.color = attributes.color;
    153.                 o.normalWS = TransformObjectToWorldDir(float3(0, 0, -1));
    154.                 o.tangentWS = TransformObjectToWorldDir(attributes.tangent.xyz);
    155.                 o.bitangentWS = cross(o.normalWS, o.tangentWS) * attributes.tangent.w;
    156.                 return o;
    157.             }
    158.  
    159.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"
    160.  
    161.             half4 NormalsRenderingFragment(Varyings i) : SV_Target
    162.             {
    163.                 half4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    164.                 half3 normalTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv));
    165.                 return NormalsRenderingShared(mainTex, normalTS, i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz);
    166.             }
    167.             ENDHLSL
    168.         }
    169.         Pass
    170.         {
    171.             Tags { "LightMode" = "UniversalForward" "Queue"="Transparent" "RenderType"="Transparent"}
    172.  
    173.             HLSLPROGRAM
    174.             #pragma vertex UnlitVertex
    175.             #pragma fragment UnlitFragment
    176.  
    177.             struct Attributes
    178.             {
    179.                 float3 positionOS   : POSITION;
    180.                 float4 color        : COLOR;
    181.                 float2 uv            : TEXCOORD0;
    182.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    183.             };
    184.  
    185.             struct Varyings
    186.             {
    187.                 float4  positionCS        : SV_POSITION;
    188.                 float4  color            : COLOR;
    189.                 float2    uv                : TEXCOORD0;
    190.                 UNITY_VERTEX_OUTPUT_STEREO
    191.             };
    192.  
    193.             TEXTURE2D(_MainTex);
    194.             SAMPLER(sampler_MainTex);
    195.             float4 _MainTex_ST;
    196.  
    197.             Varyings UnlitVertex(Attributes attributes)
    198.             {
    199.                 Varyings o = (Varyings)0;
    200.                 UNITY_SETUP_INSTANCE_ID(attributes);
    201.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    202.  
    203.                 o.positionCS = TransformObjectToHClip(attributes.positionOS);
    204.                 o.uv = TRANSFORM_TEX(attributes.uv, _MainTex);
    205.                 o.uv = attributes.uv;
    206.                 o.color = attributes.color;
    207.                 return o;
    208.             }
    209.  
    210.             float4 UnlitFragment(Varyings i) : SV_Target
    211.             {
    212.                 float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    213.                 return mainTex;
    214.             }
    215.             ENDHLSL
    216.         }
    217.     }
    218.  
    219.     Fallback "Sprites/Default"
    220. }
    221.  
    Thanks a million!