Search Unity

Question Upgrading old shader to URP

Discussion in 'Universal Render Pipeline' started by HyeroDrimm, Mar 27, 2021.

  1. HyeroDrimm

    HyeroDrimm

    Joined:
    Jan 18, 2020
    Posts:
    4
    Hi
    Ok so i'm trying to make URP version for Tilt Brush Shaders for brushes. I got far but now i cant resolve this problem. When I set Smoothness to 1 back face of texture is getting white. On 0 back face has correct color.

    Also if anyone knows how to recreate in shaders graph the vert function and VFACE part i would be beyond greatefull.

    This is original code for old shader:
    Code (CSharp):
    1. // Copyright 2017 Google Inc.
    2. //
    3. // Licensed under the Apache License, Version 2.0 (the "License");
    4. // you may not use this file except in compliance with the License.
    5. // You may obtain a copy of the License at
    6. //
    7. //     http://www.apache.org/licenses/LICENSE-2.0
    8. //
    9. // Unless required by applicable law or agreed to in writing, software
    10. // distributed under the License is distributed on an "AS IS" BASIS,
    11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. // See the License for the specific language governing permissions and
    13. // limitations under the License.
    14.  
    15. Shader "Brush/StandardDoubleSided" {
    16. Properties {
    17.   _Color ("Main Color", Color) = (1,1,1,1)
    18.   _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
    19.   _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    20.   _MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
    21.   _BumpMap ("Normalmap", 2D) = "bump" {}
    22.   _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    23. }
    24.  
    25.   // -------------------------------------------------------------------------------------------- //
    26.   // DESKTOP VERSION.
    27.   // -------------------------------------------------------------------------------------------- //
    28.   SubShader {
    29.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    30.     LOD 400
    31.     Cull Off
    32.  
    33.     CGPROGRAM
    34.     #pragma target 3.0
    35.     #pragma surface surf StandardSpecular vertex:vert alphatest:_Cutoff addshadow
    36.     #pragma multi_compile __ AUDIO_REACTIVE
    37.     #pragma multi_compile __ TBT_LINEAR_TARGET
    38.  
    39.     #include "../../../Shaders/Include/Brush.cginc"
    40.  
    41.     struct Input {
    42.       float2 uv_MainTex;
    43.       float2 uv_BumpMap;
    44.       float4 color : Color;
    45.       fixed vface : VFACE;
    46.     };
    47.  
    48.     sampler2D _MainTex;
    49.     sampler2D _BumpMap;
    50.     fixed4 _Color;
    51.     half _Shininess;
    52.  
    53.     void vert (inout appdata_full i /*, out Input o*/) {
    54.       // UNITY_INITIALIZE_OUTPUT(Input, o);
    55.       // o.tangent = v.tangent;
    56.       i.color = TbVertToNative(i.color); // How to recreate this
    57.     }
    58.  
    59.     void surf (Input IN, inout SurfaceOutputStandardSpecular o) {
    60.       fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    61.       o.Albedo = tex.rgb * _Color.rgb * IN.color.rgb;
    62.       o.Smoothness = _Shininess;
    63.       o.Specular = _SpecColor;
    64.       o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    65.       o.Normal.z *= IN.vface; // How to recreate this
    66.  
    67.       o.Alpha = tex.a * IN.color.a;
    68.     }
    69.       ENDCG
    70.     }
    71.  
    72.   // -------------------------------------------------------------------------------------------- //
    73.   // MOBILE VERSION - Vert/Frag, MSAA + Alpha-To-Coverage, w/Bump.
    74.   // -------------------------------------------------------------------------------------------- //
    75.   SubShader {
    76.     Tags{ "Queue" = "Geometry" "IgnoreProjector" = "True" }
    77.     Cull off
    78.     LOD 201
    79.  
    80.     Pass {
    81.       Tags { "LightMode"="ForwardBase" }
    82.       AlphaToMask On
    83.  
    84.       CGPROGRAM
    85.         #pragma vertex vert
    86.         #pragma fragment frag
    87.         #pragma target 3.0
    88.  
    89.         #include "UnityCG.cginc"
    90.         #include "Lighting.cginc"
    91.  
    92.         // Disable all the things.
    93.         #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight noshadow
    94.  
    95.         struct appdata {
    96.           float4 vertex : POSITION;
    97.           float2 uv : TEXCOORD0;
    98.           half3 normal : NORMAL;
    99.           fixed4 color : COLOR;
    100.           float4 tangent : TANGENT;
    101.         };
    102.  
    103.         struct v2f {
    104.           float4 pos : SV_POSITION;
    105.           float2 uv : TEXCOORD0;
    106.           half3 worldNormal : NORMAL;
    107.           fixed4 color : COLOR;
    108.           half3 tspace0 : TEXCOORD1;
    109.           half3 tspace1 : TEXCOORD2;
    110.           half3 tspace2 : TEXCOORD3;
    111.         };
    112.  
    113.         sampler2D _MainTex;
    114.         float4 _MainTex_ST;
    115.         float4 _MainTex_TexelSize;
    116.         sampler2D _BumpMap;
    117.  
    118.         fixed _Cutoff;
    119.         half _MipScale;
    120.  
    121.         float ComputeMipLevel(float2 uv) {
    122.           float2 dx = ddx(uv);
    123.           float2 dy = ddy(uv);
    124.           float delta_max_sqr = max(dot(dx, dx), dot(dy, dy));
    125.           return max(0.0, 0.5 * log2(delta_max_sqr));
    126.         }
    127.  
    128.         v2f vert (appdata v) {
    129.           v2f o;
    130.           o.pos = UnityObjectToClipPos(v.vertex);
    131.           o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    132.           o.worldNormal = UnityObjectToWorldNormal(v.normal);
    133.           o.color = v.color;
    134.  
    135.           half3 wNormal = UnityObjectToWorldNormal(v.normal);
    136.           half3 wTangent = UnityObjectToWorldDir(v.tangent.xyz);
    137.           half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
    138.           half3 wBitangent = cross(wNormal, wTangent) * tangentSign;
    139.           o.tspace0 = half3(wTangent.x, wBitangent.x, wNormal.x);
    140.           o.tspace1 = half3(wTangent.y, wBitangent.y, wNormal.y);
    141.           o.tspace2 = half3(wTangent.z, wBitangent.z, wNormal.z);
    142.           return o;
    143.         }
    144.  
    145.         fixed4 frag (v2f i, fixed vface : VFACE) : SV_Target {
    146.           fixed4 col = i.color;
    147.           col.a = tex2D(_MainTex, i.uv).a * col.a;
    148.           col.a *= 1 + max(0, ComputeMipLevel(i.uv * _MainTex_TexelSize.zw)) * _MipScale;
    149.           col.a = (col.a - _Cutoff) / max(2 * fwidth(col.a), 0.0001) + 0.5;
    150.  
    151.           half3 tnormal = UnpackNormal(tex2D(_BumpMap, i.uv));
    152.           tnormal.z *= vface;
    153.  
    154.           // Transform normal from tangent to world space.
    155.           half3 worldNormal;
    156.           worldNormal.x = dot(i.tspace0, tnormal);
    157.           worldNormal.y = dot(i.tspace1, tnormal);
    158.           worldNormal.z = dot(i.tspace2, tnormal);
    159.  
    160.           fixed ndotl = saturate(dot(worldNormal, normalize(_WorldSpaceLightPos0.xyz)));
    161.           fixed3 lighting = ndotl * _LightColor0;
    162.           lighting += ShadeSH9(half4(worldNormal, 1.0));
    163.  
    164.           col.rgb *= lighting;
    165.  
    166.           return col;
    167.         }
    168.  
    169.       ENDCG
    170.     } // pass
    171.   } // subshader
    172.  
    173.   // -------------------------------------------------------------------------------------------- //
    174.   // MOBILE VERSION - Vert/Frag, Alpha Tested, w/Bump.
    175.   // -------------------------------------------------------------------------------------------- //
    176.   SubShader{
    177.     Tags{ "Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout" }
    178.     Cull off
    179.     LOD 200
    180.  
    181.     Pass {
    182.       Tags { "LightMode"="ForwardBase" }
    183.  
    184.       CGPROGRAM
    185.         #pragma vertex vert
    186.         #pragma fragment frag
    187.         #pragma target 3.0
    188.  
    189.         #include "UnityCG.cginc"
    190.         #include "Lighting.cginc"
    191.  
    192.         // Disable all the things.
    193.         #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight noshadow
    194.  
    195.         struct appdata {
    196.             float4 vertex : POSITION;
    197.             float2 uv : TEXCOORD0;
    198.             half3 normal : NORMAL;
    199.             fixed4 color : COLOR;
    200.             float4 tangent : TANGENT;
    201.         };
    202.  
    203.         struct v2f {
    204.             float4 pos : SV_POSITION;
    205.             float2 uv : TEXCOORD0;
    206.             half3 worldNormal : NORMAL;
    207.             fixed4 color : COLOR;
    208.             half3 tspace0 : TEXCOORD1;
    209.             half3 tspace1 : TEXCOORD2;
    210.             half3 tspace2 : TEXCOORD3;
    211.         };
    212.  
    213.         sampler2D _MainTex;
    214.         float4 _MainTex_ST;
    215.         sampler2D _BumpMap;
    216.  
    217.         fixed _Cutoff;
    218.  
    219.         v2f vert (appdata v) {
    220.           v2f o;
    221.           o.pos = UnityObjectToClipPos(v.vertex);
    222.           o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    223.           o.worldNormal = UnityObjectToWorldNormal(v.normal);
    224.           o.color = v.color;
    225.  
    226.           half3 wNormal = UnityObjectToWorldNormal(v.normal);
    227.           half3 wTangent = UnityObjectToWorldDir(v.tangent.xyz);
    228.           half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
    229.           half3 wBitangent = cross(wNormal, wTangent) * tangentSign;
    230.           o.tspace0 = half3(wTangent.x, wBitangent.x, wNormal.x);
    231.           o.tspace1 = half3(wTangent.y, wBitangent.y, wNormal.y);
    232.           o.tspace2 = half3(wTangent.z, wBitangent.z, wNormal.z);
    233.           return o;
    234.         }
    235.  
    236.         fixed4 frag (v2f i, fixed vface : VFACE) : SV_Target {
    237.           fixed4 col = i.color;
    238.           col.a = tex2D(_MainTex, i.uv).a * col.a;
    239.           if (col.a < _Cutoff) { discard; }
    240.           half3 tnormal = UnpackNormal(tex2D(_BumpMap, i.uv));
    241.           tnormal.z *= vface;
    242.  
    243.           // transform normal from tangent to world space
    244.           half3 worldNormal;
    245.           worldNormal.x = dot(i.tspace0, tnormal);
    246.           worldNormal.y = dot(i.tspace1, tnormal);
    247.           worldNormal.z = dot(i.tspace2, tnormal);
    248.  
    249.           fixed ndotl = saturate(dot(worldNormal, normalize(_WorldSpaceLightPos0.xyz)));
    250.           fixed3 lighting = ndotl * _LightColor0;
    251.           lighting += ShadeSH9(half4(worldNormal, 1.0));
    252.  
    253.           col.rgb *= lighting;
    254.           return col;
    255.         }
    256.       ENDCG
    257.     } // pass
    258.   } // subshader
    259.  
    260.   // -------------------------------------------------------------------------------------------- //
    261.   // MOBILE VERSION - Vert/Frag, NO ALPHA TEST, w/Bump.
    262.   // -------------------------------------------------------------------------------------------- //
    263.   SubShader{
    264.     Tags{ "Queue" = "Geometry" "IgnoreProjector" = "True" }
    265.     Cull off
    266.     LOD 199
    267.  
    268.     Pass {
    269.       Tags { "LightMode"="ForwardBase" }
    270.  
    271.       CGPROGRAM
    272.         #pragma vertex vert
    273.         #pragma fragment frag
    274.         #pragma target 3.0
    275.  
    276.         #include "UnityCG.cginc"
    277.         #include "Lighting.cginc"
    278.  
    279.         // Disable all the things.
    280.         #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight noshadow
    281.  
    282.         struct appdata {
    283.             float4 vertex : POSITION;
    284.             float2 uv : TEXCOORD0;
    285.             half3 normal : NORMAL;
    286.             fixed4 color : COLOR;
    287.             float4 tangent : TANGENT;
    288.         };
    289.  
    290.         struct v2f {
    291.             float4 pos : SV_POSITION;
    292.             float2 uv : TEXCOORD0;
    293.             half3 worldNormal : NORMAL;
    294.             fixed4 color : COLOR;
    295.             half3 tspace0 : TEXCOORD1;
    296.             half3 tspace1 : TEXCOORD2;
    297.             half3 tspace2 : TEXCOORD3;
    298.         };
    299.  
    300.         sampler2D _MainTex;
    301.         float4 _MainTex_ST;
    302.         sampler2D _BumpMap;
    303.  
    304.         fixed _Cutoff;
    305.  
    306.         v2f vert (appdata v) {
    307.           v2f o;
    308.           o.pos = UnityObjectToClipPos(v.vertex);
    309.           o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    310.           o.worldNormal = UnityObjectToWorldNormal(v.normal);
    311.           o.color = v.color;
    312.  
    313.           half3 wNormal = UnityObjectToWorldNormal(v.normal);
    314.           half3 wTangent = UnityObjectToWorldDir(v.tangent.xyz);
    315.           half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
    316.           half3 wBitangent = cross(wNormal, wTangent) * tangentSign;
    317.           o.tspace0 = half3(wTangent.x, wBitangent.x, wNormal.x);
    318.           o.tspace1 = half3(wTangent.y, wBitangent.y, wNormal.y);
    319.           o.tspace2 = half3(wTangent.z, wBitangent.z, wNormal.z);
    320.           return o;
    321.         }
    322.  
    323.         fixed4 frag (v2f i, fixed vface : VFACE) : SV_Target {
    324.           fixed4 col = i.color;
    325.           col.a = tex2D(_MainTex, i.uv).a * col.a;
    326.           //if (col.a < _Cutoff) { discard; }
    327.           half3 tnormal = UnpackNormal(tex2D(_BumpMap, i.uv));
    328.           tnormal.z *= vface;
    329.  
    330.           // transform normal from tangent to world space
    331.           half3 worldNormal;
    332.           worldNormal.x = dot(i.tspace0, tnormal);
    333.           worldNormal.y = dot(i.tspace1, tnormal);
    334.           worldNormal.z = dot(i.tspace2, tnormal);
    335.  
    336.           fixed ndotl = saturate(dot(worldNormal, normalize(_WorldSpaceLightPos0.xyz)));
    337.           fixed3 lighting = ndotl * _LightColor0;
    338.           lighting += ShadeSH9(half4(worldNormal, 1.0));
    339.  
    340.           col.rgb *= lighting;
    341.           return col;
    342.         }
    343.       ENDCG
    344.     } // pass
    345.   } // subshader
    346.  
    347.   // -------------------------------------------------------------------------------------------- //
    348.   // MOBILE VERSION -- vert/frag, MSAA + Alpha-To-Coverage, No Bump.
    349.   // -------------------------------------------------------------------------------------------- //
    350.   SubShader {
    351.     Tags{ "Queue" = "Geometry" "IgnoreProjector" = "True" }
    352.     Cull Off
    353.     LOD 150
    354.  
    355.     Pass {
    356.       Tags { "LightMode"="ForwardBase" }
    357.       AlphaToMask On
    358.  
    359.       CGPROGRAM
    360.         #pragma vertex vert
    361.         #pragma fragment frag
    362.         #pragma target 3.0
    363.  
    364.         #include "UnityCG.cginc"
    365.         #include "Lighting.cginc"
    366.  
    367.         // Disable all the things.
    368.         #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight noshadow
    369.  
    370.         struct appdata {
    371.             float4 vertex : POSITION;
    372.             float2 uv : TEXCOORD0;
    373.             half3 normal : NORMAL;
    374.             fixed4 color : COLOR;
    375.         };
    376.  
    377.         struct v2f {
    378.             float4 pos : SV_POSITION;
    379.             float2 uv : TEXCOORD0;
    380.             half3 worldNormal : NORMAL;
    381.             fixed4 color : COLOR;
    382.         };
    383.  
    384.         sampler2D _MainTex;
    385.         float4 _MainTex_ST;
    386.         float4 _MainTex_TexelSize;
    387.  
    388.         fixed _Cutoff;
    389.         half _MipScale;
    390.  
    391.         float ComputeMipLevel(float2 uv) {
    392.           float2 dx = ddx(uv);
    393.           float2 dy = ddy(uv);
    394.           float delta_max_sqr = max(dot(dx, dx), dot(dy, dy));
    395.           return max(0.0, 0.5 * log2(delta_max_sqr));
    396.         }
    397.  
    398.         v2f vert (appdata v) {
    399.           v2f o;
    400.           o.pos = UnityObjectToClipPos(v.vertex);
    401.           o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    402.           o.worldNormal = UnityObjectToWorldNormal(v.normal);
    403.           o.color = v.color;
    404.           return o;
    405.         }
    406.  
    407.         fixed4 frag (v2f i, fixed vface : VFACE) : SV_Target {
    408.           fixed4 col = i.color;
    409.           col.a *= tex2D(_MainTex, i.uv).a;
    410.           col.a *= 1 + max(0, ComputeMipLevel(i.uv * _MainTex_TexelSize.zw)) * _MipScale;
    411.           col.a = (col.a - _Cutoff) / max(2 * fwidth(col.a), 0.0001) + 0.5;
    412.  
    413.           half3 worldNormal = normalize(i.worldNormal * vface);
    414.  
    415.           fixed ndotl = saturate(dot(worldNormal, normalize(_WorldSpaceLightPos0.xyz)));
    416.           fixed3 lighting = ndotl * _LightColor0;
    417.           lighting += ShadeSH9(half4(worldNormal, 1.0));
    418.  
    419.           col.rgb *= lighting;
    420.  
    421.           // TODO(jcowles): only apply a discard when MSAA is disabled. This kills the nicely
    422.           // anti-aliased edges above, however that anti-aliasing manifests as bloom when in LDR
    423.           // mode.
    424.           if (col.a < _Cutoff) {
    425.             discard;
    426.           }
    427.           col.a = 1.0;
    428.           return col;
    429.         }
    430.       ENDCG
    431.     } // pass
    432.   } // subshader
    433.  
    434.   // -------------------------------------------------------------------------------------------- //
    435.   // MOBILE VERSION -- vert/frag, NO CUTOUT, NO BUMP.
    436.   // -------------------------------------------------------------------------------------------- //
    437.   SubShader {
    438.     Tags{ "Queue" = "Geometry" "IgnoreProjector" = "True" }
    439.     Cull Off
    440.     LOD 149
    441.  
    442.     Pass {
    443.       Tags { "LightMode"="ForwardBase" }
    444.  
    445.       CGPROGRAM
    446.         #pragma vertex vert
    447.         #pragma fragment frag
    448.         #pragma target 3.0
    449.  
    450.         #include "UnityCG.cginc"
    451.         #include "Lighting.cginc"
    452.  
    453.         // Disable all the things.
    454.         #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight noshadow
    455.  
    456.         struct appdata {
    457.             float4 vertex : POSITION;
    458.             float2 uv : TEXCOORD0;
    459.             half3 normal : NORMAL;
    460.             fixed4 color : COLOR;
    461.         };
    462.  
    463.         struct v2f {
    464.             float4 pos : SV_POSITION;
    465.             float2 uv : TEXCOORD0;
    466.             half3 worldNormal : NORMAL;
    467.             fixed4 color : COLOR;
    468.         };
    469.  
    470.         sampler2D _MainTex;
    471.         float4 _MainTex_ST;
    472.         float4 _MainTex_TexelSize;
    473.  
    474.         fixed _Cutoff;
    475.         half _MipScale;
    476.  
    477.         float ComputeMipLevel(float2 uv) {
    478.           float2 dx = ddx(uv);
    479.           float2 dy = ddy(uv);
    480.           float delta_max_sqr = max(dot(dx, dx), dot(dy, dy));
    481.           return max(0.0, 0.5 * log2(delta_max_sqr));
    482.         }
    483.  
    484.         v2f vert (appdata v) {
    485.           v2f o;
    486.           o.pos = UnityObjectToClipPos(v.vertex);
    487.           o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    488.           o.worldNormal = UnityObjectToWorldNormal(v.normal);
    489.           o.color = v.color;
    490.           return o;
    491.         }
    492.  
    493.         fixed4 frag (v2f i, fixed vface : VFACE) : SV_Target {
    494.           fixed4 col = i.color;
    495.           col.a = 1;
    496.  
    497.           half3 worldNormal = normalize(i.worldNormal * vface);
    498.  
    499.           fixed ndotl = saturate(dot(worldNormal, normalize(_WorldSpaceLightPos0.xyz)));
    500.           fixed3 lighting = ndotl * _LightColor0;
    501.           lighting += ShadeSH9(half4(worldNormal, 1.0));
    502.  
    503.           col.rgb *= lighting;
    504.  
    505.           return col;
    506.         }
    507.       ENDCG
    508.     } // pass
    509.   } // subshader
    510.  
    511.   // -------------------------------------------------------------------------------------------- //
    512.   // MOBILE VERSION - Lambert SurfaceShader, Alpha Test, No Bump.
    513.   // -------------------------------------------------------------------------------------------- //
    514.   SubShader {
    515.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    516.     LOD 50
    517.     Cull Off
    518.  
    519.     CGPROGRAM
    520.       #pragma surface surf Lambert vertex:vert alphatest:_Cutoff
    521.       #pragma target 3.0
    522.  
    523.       sampler2D _MainTex;
    524.       fixed4 _Color;
    525.  
    526.       struct Input {
    527.         float2 uv_MainTex;
    528.         float4 color : COLOR;
    529.         fixed vface : VFACE;
    530.       };
    531.  
    532.       void vert (inout appdata_full v) {
    533.       }
    534.  
    535.       void surf (Input IN, inout SurfaceOutput o) {
    536.         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    537.         o.Albedo = c.rgb * IN.color.rgb;
    538.         o.Alpha = c.a * IN.color.a;
    539.         o.Normal = float3(0,0,IN.vface);
    540.       }
    541.     ENDCG
    542.   }
    543.  
    544.   FallBack "Transparent/Cutout/VertexLit"
    545. }
    546.  

    And here are Screens with smoothness(shigniness) set to 0 and 1 and also the Shader Graph:
    https://drive.google.com/drive/folders/1kudrTOSsVvQaDgDuK2dc3sEvYAyC8A6t?usp=sharing

    If anyone want to have a look at original https://github.com/googlevr/tilt-brush-toolkit
     
  2. HyeroDrimm

    HyeroDrimm

    Joined:
    Jan 18, 2020
    Posts:
    4
    Dennisrudolph and PutridEx like this.
  3. Dennisrudolph

    Dennisrudolph

    Joined:
    Aug 24, 2017
    Posts:
    6
    Hi, did you manage to convert them? I am having the same problem. Thanks!