Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shaders problems after moving to URP

Discussion in 'Universal Render Pipeline' started by RoyalCoder, May 20, 2020.

  1. RoyalCoder

    RoyalCoder

    Joined:
    Oct 4, 2013
    Posts:
    301
    Hi friends,

    I just moved to URP and I got some problems with a few shader :( I'm noob in this aspect can I convert my previos shader to URP? Thanks in advance!

    https://ibb.co/Rywf7qs
    Shader 1:
    Code (CSharp):
    1. Shader "Paint/MetalURPTest" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _Detail ("Detail Texture", 2D) = "black" {}
    5.         _SpecularMap("Specular Map", 2D) = "white" {}
    6.         _Cube ("Cubemap", CUBE) = "" {}
    7.         _BumpMap ("Bumpmap (RGB Trans)", 2D) = "bump" {}
    8.         _Reflection ("Reflection", Range(0, 1)) = 0.5
    9.         _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    10.         _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    11.         _RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
    12.     }
    13.     SubShader {
    14.         Tags { "RenderType"="Opaque" }
    15.         LOD 200
    16.  
    17.         CGPROGRAM
    18.         #pragma surface surf BlinnPhong
    19.         #pragma target 3.0
    20.  
    21.         sampler2D _SpecularMap;
    22.         samplerCUBE _Cube;
    23.         half4 _Color;
    24.         half4 _DecalColor;
    25.         half _Reflection;
    26.         half _RimPower;
    27.         float _Shininess;
    28.         sampler2D _BumpMap;
    29.         sampler2D _Detail;
    30.  
    31.         struct Input {
    32.             float2 uv_BumpMap;
    33.             float2 uv_Detail;
    34.             float3 worldRefl;
    35.             float3 viewDir;
    36.             float2 uv_SpecularMap;
    37.             INTERNAL_DATA
    38.         };
    39.  
    40.         void surf (Input IN, inout SurfaceOutput o) {
    41.             half4 bump = tex2D(_BumpMap, IN.uv_BumpMap);
    42.             half4 detail = tex2D(_Detail, IN.uv_Detail);
    43.             half3 norm = UnpackNormal (bump);
    44.             half4 grain = tex2D (_SpecularMap, IN.uv_SpecularMap);
    45.             half4 refl = texCUBE (_Cube, WorldReflectionVector (IN, norm));
    46.             half rim1 = saturate(dot(normalize(IN.viewDir), norm));
    47.             half rim2 = 1 - rim1;
    48.             half3 color = lerp(_Color.rgb, detail.rgb, detail.a);
    49.             o.Normal = norm;
    50.             o.Gloss = _SpecColor.rgb * (1 - detail.a);
    51.             o.Albedo = lerp(color, color + refl.rgb * pow(rim1, _RimPower), _Reflection * (1 - detail.a / 2));
    52.             o.Alpha = 1;
    53.             o.Specular = (1 - grain.a * _SpecColor.a) * _Shininess * (1 - detail.a);
    54.             o.Emission = refl * _Reflection * pow(rim2, _RimPower) * (1 - detail.a);
    55.         }
    56.         ENDCG
    57.     }
    58.     FallBack "Specular"
    59. }
    60.  

    Shader 2:
    Code (CSharp):
    1.  
    2. //Simple Physical Shader was written by Ryan Gatts 2014
    3. //Based on concepts pioneered by Double Fine, Disney, Unreal Engine, Jim Blinn, Sean Murphy, Josh Ols, and Nicholas Francis.
    4.  
    5. Shader "ShaderSuperb/Session22/SimplePhysicalShaderCoat"
    6. {
    7.     Properties
    8.     {
    9.         _MainColor("Main Color", Color) = (0.5, 0.5, 0.5, 0.5)
    10.         _MainTex("Main Color Texture", 2D) = "white" {}
    11.         _Smoothness("Smoothness", Range(1, 12)) = 2.0
    12.         _SmoothMap("Smoothness Map", 2D) = "white" {}
    13.         _Metallicity("Metallicity", Range(0, 1)) = 0
    14.         _MetalMap("Metallicity Map", 2D) = "white" {}
    15.         _Wrap("Light Wrap", float) = 0.25
    16.         _NormalMap("Normal Map", 2D) = "bump" {}
    17.         _BumpDepth("Bump Depth", Range(0.1, 4.0)) = 1
    18.         _Coat("Clear Coat", Range(0, 1)) = 0
    19.         _CoatMap("Clear Coat Map", 2D) = "white" {}
    20.         _CoatSmooth("Coat Smoothness", Range(1, 12)) = 12
    21.         _RSRM("RSRM", 2D) = "gray" {}
    22.      
    23.     }
    24.  
    25.     SubShader
    26.     {
    27.         Pass
    28.         {
    29.             Tags{ "LightMode" = "ForwardBase" }
    30.             CGPROGRAM
    31.          
    32.                 #pragma vertex vert
    33.                 #pragma fragment frag
    34.                 #pragma target 3.0
    35.                 #pragma multi_compile_fwdadd_fullshadows
    36.                 #include "UnityCG.cginc"
    37.                 #include "AutoLight.cginc"
    38.              
    39.                 //user defined
    40.                 uniform sampler2D _MainTex;
    41.                 uniform sampler2D _NormalMap;
    42.                 uniform sampler2D _SmoothMap;
    43.                 uniform sampler2D _MetalMap;
    44.                 uniform sampler2D _CoatMap;
    45.                 uniform sampler2D _RSRM;
    46.                 uniform float4    _MainTex_ST;
    47.                 uniform float4    _NormalMap_ST;
    48.                 uniform float4    _SmoothMap_ST;
    49.                 uniform float4    _MetalMap_ST;
    50.                 uniform float4    _CoatMap_ST;
    51.                 uniform float4     _MainColor;
    52.                 uniform float     _Smoothness;
    53.                 uniform float    _Metallicity;
    54.                 uniform float    _Wrap;
    55.                 uniform float    _Coat;
    56.                 uniform float    _CoatSmooth;
    57.                 uniform float   _BumpDepth;
    58.              
    59.                 //unity defined
    60.                 uniform float4     _LightColor0;
    61.              
    62.                 //base input struct
    63.                 struct vertexInput
    64.                 {
    65.                     float4 vertex : POSITION;
    66.                     float3 normal : NORMAL;
    67.                     float4 texcoord : TEXCOORD0;
    68.                     float4 tangent : TANGENT;
    69.                 };
    70.              
    71.                 struct vertexOutput
    72.                 {
    73.                     float4 pos : SV_POSITION;
    74.                     float4 tex : TEXCOORD0;
    75.                     float4 posWorld : TEXCOORD1;
    76.                     float3 normalWorld : TEXCOORD2;
    77.                     float3 tangentWorld : TEXCOORD3;
    78.                     float3 binormalWorld : TEXCOORD4;
    79.                  
    80.                     LIGHTING_COORDS(5,6)
    81.                 };
    82.              
    83.                 //vertex function
    84.                 vertexOutput vert (vertexInput v)
    85.                 {
    86.                     vertexOutput o;
    87.                  
    88.                     float4x4 modelMatrix         = unity_ObjectToWorld;
    89.                     float4x4 modelMatrixInverse = unity_WorldToObject;
    90.                  
    91.                     o.normalWorld = normalize(mul(float4(v.normal, 0.0), unity_WorldToObject).xyz);
    92.                     o.tangentWorld = normalize(mul(unity_ObjectToWorld, half4(half3(v.tangent.xyz), 0)));
    93.                     o.binormalWorld = normalize(cross (o.normalWorld, o.tangentWorld) * v.tangent.w);
    94.                  
    95.                     o.posWorld = mul(unity_ObjectToWorld, v.vertex);
    96.                     o.pos = UnityObjectToClipPos(v.vertex);
    97.                     o.tex = v.texcoord;
    98.                  
    99.                     TRANSFER_VERTEX_TO_FRAGMENT(o); // for shadows
    100.                  
    101.                     return o;
    102.                  
    103.                 }
    104.              
    105.                 //take a -1 to 1 range and fit it 0 to 1
    106.                 float clamp01 (float toBeNormalized)
    107.                 {
    108.                     return toBeNormalized * 0.5 + 0.5;
    109.                 }
    110.              
    111.                 float3 calculateAmbientReflection( float3 rsrm , float texM )
    112.                 {
    113.                     float3 amb  = UNITY_LIGHTMODEL_AMBIENT.xyz;
    114.                     return  float3 (1.5 * rsrm * amb + amb * 0.5 * texM);
    115.                 }
    116.                  
    117.                 //fragment function
    118.                 float4 frag(vertexOutput i) : COLOR
    119.                 {
    120.                     float shadAtten = LIGHT_ATTENUATION(i);
    121.                  
    122.                     float4 tex    = tex2D(_MainTex,   i.tex.xy * _MainTex_ST.xy   + _MainTex_ST.zw);
    123.                     tex  = tex  * _MainColor;
    124.                     float  texS    = tex2D(_SmoothMap, i.tex.xy * _SmoothMap_ST.xy + _SmoothMap_ST.zw);
    125.                     texS = texS * _Smoothness;
    126.                     float  texM    = tex2D(_MetalMap,  i.tex.xy * _MetalMap_ST.xy  + _MetalMap_ST.zw);
    127.                     texM = texM * _Metallicity;
    128.                     float4 texN    = tex2D(_NormalMap, i.tex.xy * _NormalMap_ST.xy + _NormalMap_ST.zw);
    129.                     float nDepth = 8 / (_BumpDepth * 8);
    130.                     float4 texC1= tex2D(_CoatMap,    i.tex.xy * _CoatMap_ST.xy     + _CoatMap_ST.zw);
    131.                     float  texC = Luminance(texC1.rgb) * _Coat * texC1.a;
    132.                  
    133.                     //Unpack Normal
    134.                     half3 localCoords = half3(2.0 * texN.ag - float2(1.0, 1.0), 0.0);
    135.                     localCoords.z = nDepth;
    136.                  
    137.                     //normal transpose matrix
    138.                     float3x3 local2WorldTranspose = float3x3
    139.                     (
    140.                         i.tangentWorld,
    141.                         i.binormalWorld,
    142.                         i.normalWorld
    143.                     );
    144.                  
    145.                     //Calculate normal direction
    146.                     float3 normalDir = normalize( mul( localCoords, local2WorldTranspose));
    147.                  
    148.                     float3 N = normalize( normalDir);
    149.                     float3 V = normalize( _WorldSpaceCameraPos.xyz - i.posWorld.xyz);
    150.                     float3 fragmentToLight = _WorldSpaceLightPos0.xyz - i.posWorld.xyz;
    151.                     float  distanceToLight = length(fragmentToLight);
    152.                     float  atten = pow(2, -0.1 * distanceToLight * distanceToLight) * _WorldSpaceLightPos0.w + 1 - _WorldSpaceLightPos0.w; // (-0.1x^2)^2 for pointlights 1 for dirlights
    153.                     float3 L = (normalize(fragmentToLight)) * _WorldSpaceLightPos0.w + normalize(_WorldSpaceLightPos0.xyz) * (1 - _WorldSpaceLightPos0.w);
    154.                     float3 H = normalize( V + L );
    155.                     float3 worldReflect = reflect(V,N);
    156.                  
    157.                     //lighting
    158.                     float NdotL     = dot(N,L);
    159.                     float NdotV     = 1 - max(0.0, dot(N,V));
    160.                     float NdotH     = clamp(dot(N,H), 0, 1);
    161.                     float VdotL     = clamp01(dot(V,L));
    162.                     float wrap         = clamp(_Wrap, -0.25, 1.0);
    163.                  
    164.                     float4 texdesat = dot(tex.rgb, float3(0.3, 0.59, 0.11));
    165.                  
    166.                     float3 difftex    = lerp(tex, float4(0,0,0,0), pow(texM, 1)).xyz;
    167.                     float3 spectex    = lerp(texdesat, tex, texM).xyz;
    168.                  
    169.                     VdotL             = pow(VdotL, 0.85);
    170.                     float smooth     = 4 * pow(1.8, texS - 2) + 1.5;
    171.                     float smoothCoat= 4 * pow(1.8, _CoatSmooth - 2) + 1.5;
    172.                     float rim        = texM + (pow(NdotV, 1 + texS / 6)) * (1 - texM);
    173.                     float coatrim    = pow(NdotV, 1 + _CoatSmooth / 6);
    174.                     float bellclamp = (1 / (1 + pow(0.65 * acos(dot(N,L)), 16)));
    175.                  
    176.                     float3 rsrm     = tex2D(_RSRM, float2((1 - (texS - 1) * 0.09), 1 - clamp01(worldReflect.y)));
    177.                     float3 rsrmDiff = tex2D(_RSRM, float2(1, N.y));
    178.                     float3 rsrmCoat = tex2D(_RSRM, float2((1 - (_CoatSmooth - 1) * 0.09), 1 - clamp01(worldReflect.y)));
    179.                     float3 ambReflect     = calculateAmbientReflection(rsrm, texM);
    180.                     float3 ambReflectDiff = calculateAmbientReflection(rsrmDiff, texM);
    181.                     float3 ambReflectCoat = calculateAmbientReflection(rsrmCoat, 0);
    182.                  
    183.                  
    184.                     float3 spec = NdotH;
    185.                     spec =  pow (spec, smooth * VdotL) * log(smooth*(VdotL+1)) * bellclamp * texS * (1 / texS) * 0.5;
    186.                     spec *= shadAtten * atten * spectex.xyz * _LightColor0.rgb * (2+texM) * spectex.xyz;
    187.                     spec += ambReflect * spectex.rgb * rim * 2 - texC * 0.05;
    188.                  
    189.                     float3 coat = NdotH;
    190.                     coat = pow (coat, 4 * smoothCoat * VdotL) * log(smoothCoat*(VdotL+1)) * bellclamp * smoothCoat * (1 / smoothCoat) * 0.5;
    191.                     coat *= shadAtten * atten * texC * _LightColor0.rgb;
    192.                     coat += ambReflectCoat * texC * coatrim * 2;
    193.                  
    194.                     float3 diff = max(0, (pow(max(0, (NdotL * (1 - wrap) + wrap)), (2 * wrap + 1))));
    195.                     diff *= lerp(shadAtten, 1, wrap) * atten * difftex.xyz * _LightColor0.rgb * 2 * _LightColor0.rgb * difftex.xyz;
    196.                     diff += ambReflect * difftex.xyz * rim + ambReflectDiff * 2 * difftex.xyz;
    197.                  
    198.                     return float4 (atan(clamp(spec + diff + coat, 0, 2)), 1); //this is used to round off values above one and give better color reproduction in bright scenes
    199.                 }
    200.          
    201.             ENDCG
    202.         }
    203.         Pass
    204.         {
    205.             Tags{ "LightMode" = "ForwardAdd"}
    206.             Fog {Mode Off}
    207.             Blend One One
    208.             CGPROGRAM
    209.          
    210.                 #pragma vertex vert
    211.                 #pragma fragment frag
    212.                 #pragma target 3.0
    213.                 #include "UnityCG.cginc"
    214.              
    215.                 //user defined
    216.                 uniform sampler2D _MainTex;
    217.                 uniform sampler2D _NormalMap;
    218.                 uniform sampler2D _SmoothMap;
    219.                 uniform sampler2D _MetalMap;
    220.                 uniform sampler2D _CoatMap;
    221.                 uniform float4    _MainTex_ST;
    222.                 uniform float4    _NormalMap_ST;
    223.                 uniform float4    _SmoothMap_ST;
    224.                 uniform float4    _MetalMap_ST;
    225.                 uniform float4    _CoatMap_ST;
    226.                 uniform float4     _MainColor;
    227.                 uniform float     _Smoothness;
    228.                 uniform float    _Wrap;
    229.                 uniform float    _Coat;
    230.                 uniform float    _CoatSmooth;
    231.                 uniform float   _BumpDepth;
    232.                 uniform float    _Metallicity;
    233.              
    234.                 //unity defined
    235.                 uniform float4     _LightColor0;
    236.              
    237.                 //base input struct
    238.                 struct vertexInput
    239.                 {
    240.                     float4 vertex : POSITION;
    241.                     float3 normal : NORMAL;
    242.                     float4 texcoord : TEXCOORD0;
    243.                     float4 tangent : TANGENT;
    244.                 };
    245.              
    246.                 struct vertexOutput
    247.                 {
    248.                     float4 pos : SV_POSITION;
    249.                     float4 tex : TEXCOORD0;
    250.                     float4 posWorld : TEXCOORD1;
    251.                     float3 normalWorld : TEXCOORD2;
    252.                     float3 tangentWorld : TEXCOORD3;
    253.                     float3 binormalWorld : TEXCOORD4;
    254.                 };
    255.              
    256.                 //vertex function
    257.                 vertexOutput vert (vertexInput v)
    258.                 {
    259.                     vertexOutput o;
    260.                  
    261.                     float4x4 modelMatrix = unity_ObjectToWorld;
    262.                     float4x4 modelMatrixInverse = unity_WorldToObject;
    263.                  
    264.                     o.normalWorld = normalize( mul(float4(v.normal, 0.0), unity_WorldToObject).xyz);
    265.                     o.tangentWorld = normalize(mul(unity_ObjectToWorld, half4(half3(v.tangent.xyz), 0)));
    266.                     o.binormalWorld = normalize(cross (o.normalWorld, o.tangentWorld) * v.tangent.w);
    267.                  
    268.                     o.posWorld = mul(unity_ObjectToWorld, v.vertex);
    269.                     o.pos = UnityObjectToClipPos(v.vertex);
    270.                     o.tex = v.texcoord;
    271.                  
    272.                     return o;
    273.                 }
    274.              
    275.                 //fragment function
    276.                 float4 frag(vertexOutput i) : COLOR
    277.                 {
    278.                     float4 tex    = tex2D(_MainTex,   i.tex.xy * _MainTex_ST.xy   + _MainTex_ST.zw);
    279.                     tex = tex * _MainColor;
    280.                     float4 texS    = tex2D(_SmoothMap, i.tex.xy * _SmoothMap_ST.xy + _SmoothMap_ST.zw);
    281.                     texS = texS * _Smoothness;
    282.                     float4 texM    = tex2D(_MetalMap,  i.tex.xy * _MetalMap_ST.xy  + _MetalMap_ST.zw);
    283.                     texM = texM * _Metallicity;
    284.                     float4 texN    = tex2D(_NormalMap, i.tex.xy * _NormalMap_ST.xy + _NormalMap_ST.zw);
    285.                     float nDepth = 8 / (_BumpDepth * 8);
    286.                     float4 texC1= tex2D(_CoatMap,    i.tex.xy * _CoatMap_ST.xy     + _CoatMap_ST.zw);
    287.                     float  texC = Luminance(texC1.rgb) * _Coat * texC1.a;
    288.                  
    289.                     //Unpack Normal
    290.                     half3 localCoords = half3(2.0 * texN.ag - float2(1.0, 1.0), 0.0);
    291.                     localCoords.z = nDepth;
    292.                  
    293.                     //normal transpose matrix
    294.                     float3x3 local2WorldTranspose = float3x3
    295.                     (
    296.                         i.tangentWorld,
    297.                         i.binormalWorld,
    298.                         i.normalWorld
    299.                     );
    300.                  
    301.                     //Calculate normal direction
    302.                     float3 normalDir = normalize( mul( localCoords, local2WorldTranspose));
    303.                  
    304.                     float3 N = normalize(normalDir);
    305.                     float3 V = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
    306.                     float3 fragmentToLight = _WorldSpaceLightPos0.xyz - i.posWorld.xyz;
    307.                     float  distanceToLight = length(fragmentToLight);
    308.                     float  atten = pow(2, -0.1 * distanceToLight * distanceToLight) * _WorldSpaceLightPos0.w + 1 - _WorldSpaceLightPos0.w;
    309.                     float3 L = normalize(fragmentToLight) * _WorldSpaceLightPos0.w + normalize(_WorldSpaceLightPos0.xyz) * (1 - _WorldSpaceLightPos0.w);
    310.                     float3 H = normalize(V + L);
    311.                  
    312.                     //lighting
    313.                     float NdotL     = dot(N,L);
    314.                     float NdotH     = clamp(dot(N,H), 0, 1);
    315.                     float VdotL     = dot(V,L) * 0.5 + 0.5;
    316.                     float wrap         = clamp(_Wrap, -0.25, 1.0);
    317.                  
    318.                     float4 texdesat = dot(tex.rgb, float3(0.3, 0.59, 0.11));
    319.                  
    320.                     float3 difftex    = lerp(tex, float4(0,0,0,0), pow(texM, 1)).xyz;
    321.                     float3 spectex    = lerp(texdesat, tex, texM).xyz;
    322.                  
    323.                     VdotL             = pow(VdotL, 0.85);
    324.                     float smooth     = 4 * pow(1.8, texS - 2) + 1.5;
    325.                     float smoothCoat= 4 * pow(1.8, _CoatSmooth - 2) + 1.5;
    326.                     float bellclamp = (1 / (1 + pow(0.65 * acos(dot(N,L)), 16)));
    327.                  
    328.                     float3 spec = NdotH;
    329.                     spec =  pow(spec, smooth * VdotL) * log(smooth * (VdotL + 1)) * bellclamp;
    330.                     spec *= atten * spectex.xyz * _LightColor0.rgb * (2 + _Metallicity) * spectex.xyz;
    331.                     spec -= texC * 0.05;
    332.                  
    333.                     float3 coat = NdotH;
    334.                     coat = pow (coat, smoothCoat * VdotL) * log(smoothCoat*(VdotL+1)) * bellclamp * smoothCoat * (1 / smoothCoat) * 0.5;
    335.                     coat *= atten * texC * _LightColor0.rgb;
    336.                  
    337.                     float3 diff = max(0, (pow(max(0, (NdotL * (1 - wrap) + wrap)), (2 * wrap + 1))));
    338.                     diff *= atten * difftex.xyz * _LightColor0.rgb * 2 * difftex.xyz;
    339.                  
    340.                     return float4 (atan(clamp(spec + diff + coat, 0, 2)), 1); //this is used to round off values above one and give better color reproduction in bright scenes
    341.                  
    342.                 }
    343.          
    344.             ENDCG
    345.         }
    346.      
    347.     }
    348.     Fallback "Diffuse"
    349. }
     
    Last edited: Jun 11, 2020
  2. RoyalCoder

    RoyalCoder

    Joined:
    Oct 4, 2013
    Posts:
    301
    Anybody can guide me with this please?