Search Unity

Question Shaders broken after Unity upgrade

Discussion in 'Shaders' started by akalytapa, Jun 8, 2021.

  1. akalytapa

    akalytapa

    Joined:
    Jun 8, 2021
    Posts:
    7
    Hi guys! So I've been given an old unity project (v5.0.1), and I have to rebuild it with unity 2021.x.x. After fixing all issues I discovered some broken custom shaders.

    The code of shader:
    Code (CSharp):
    1. Shader "PA/Deck shader"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Diffuse color", Color) = ( 1, 1, 1, 1 )
    6.  
    7.         _RoofNormalMap ("Roof Normal map (RGB)", 2D) = "bump" {}
    8.         _RoofTex ("Roof texture (RGBA)", 2D) = "white" {}
    9.         _DeckWidth ("Deck width (4-30)", Range(4, 30)) = 4
    10.         _DeckHeight ("Deck height (4-30)", Range(4, 30)) = 10
    11.         _TextureWidth ("Texture width (2-1024)", Float) = 2
    12.         _TextureHeight ("Texture height (2-1024)", Float) = 2
    13.  
    14.         _SpecualColor ("Specular Material Color", Color) = (1, 1, 1, 1)
    15.         //_SpecShininess ("Shininess", Float) = 10
    16.         _SpecShininess ("Specular Shininess", Range(0, 70)) = 10
    17.         _SpecPower ("Specular power", Range(0, 10)) = 1
    18.         _RotationAngle("Rotation Angle (Radian)", Float) = 0.0
    19.     }
    20.  
    21.  
    22.     CGINCLUDE
    23.  
    24.     //uniform sampler2D _MainTex;
    25.  
    26.     ENDCG
    27.  
    28.     SubShader
    29.     {
    30.         Tags {"Queue" = "Transparent" "RenderType"="Transparent" }
    31.         //Tags { "RenderType"="Opaque" }
    32.         LOD 400
    33.  
    34.         Pass
    35.         {  
    36.             //LOD 200
    37.             Blend SrcAlpha OneMinusSrcAlpha
    38.  
    39.             CGPROGRAM
    40.  
    41.             #include "UnityCG.cginc"
    42.             #include "AutoLight.cginc"
    43.             #include "Lighting.cginc"
    44.  
    45.             #pragma vertex vert alpha
    46.             #pragma fragment frag alpha
    47.             #include "UnityCG.cginc"
    48.             #pragma target 3.0
    49.  
    50.             //uniform float4 _LightColor0;
    51.  
    52.             ///////////////////////////////////////////////////////////////////
    53.  
    54.             // input shader data
    55.             float4 _Color;
    56.             float _DeckWidth;
    57.             float _DeckHeight;
    58.             float _FanItemWidth;
    59.             float _TextureWidth;
    60.             float _TextureHeight;
    61.             float _RotationAngle;
    62.             float4x4 _MatrixChooseStripeds;
    63.             float4x4 _AncillaryMatrixChooseStripeds;
    64.  
    65.             sampler2D _RoofNormalMap;
    66.             sampler2D _RoofTex;
    67.  
    68.             float4 _MainTex_ST;
    69.  
    70.             float4 _SpecualColor;
    71.             float _SpecShininess;
    72.             float _SpecPower;
    73.  
    74.             ///////////////////////////////////////////////////////////////////
    75.             struct VS_INPUT
    76.             {
    77.                 float4 vertex   : POSITION;
    78.                 float3 normal   : NORMAL;
    79.                 float3 tangent   : TANGENT;
    80.                 float2 texCoord0 : TEXCOORD0;
    81.                 float2 texCoord1 : TEXCOORD1;
    82.             };
    83.             ///////////////////////////////////////////////////////////////////
    84.  
    85.             struct VS_OUTPUT
    86.             {
    87.                 float4 pos      : POSITION;
    88.                 float2 texCoord0 : TEXCOORD0;
    89.                 float2 texCoord1 : TEXCOORD1;
    90.                 float3 normal   : TEXCOORD2;
    91.                 float3 viewVec  : TEXCOORD3;
    92.                 float3 lightVec : TEXCOORD4;
    93.                 float atten       : TEXCOORD5;
    94.                 float3 tangent : TEXCOORD6;
    95.  
    96.             };
    97.             ///////////////////////////////////////////////////////////////////
    98.  
    99.             VS_OUTPUT vert(VS_INPUT i)
    100.             {
    101.                 VS_OUTPUT o = (VS_OUTPUT)0;
    102.                
    103.                 float sinX = sin (_RotationAngle);
    104.                 float cosX = cos (_RotationAngle);
    105.                 float2x2 rotationMatrix = float2x2( cosX, -sinX, sinX, cosX);
    106.                 i.texCoord0.xy = mul (i.texCoord0.xy, rotationMatrix );
    107.                
    108.                 // common calculation
    109.                 float4x4 modelMatrix = _Object2World;
    110.                 float atten;
    111.                 float3 lightDirection;
    112.  
    113.                 if (0.0 == _WorldSpaceLightPos0.w) // directional light?
    114.                 {
    115.                     atten = 1.0; // no attenuation
    116.                     lightDirection = normalize(_WorldSpaceLightPos0.rgb);
    117.                 }
    118.                 else // point or spot light
    119.                 {
    120.                     float3 vertexToLightSource = (_WorldSpaceLightPos0 - mul(modelMatrix, i.vertex)).rgb;
    121.                     float distance = length(vertexToLightSource);
    122.                     atten = 1.0 / distance; // linear attenuation
    123.                     lightDirection = normalize(vertexToLightSource);
    124.                 }
    125.  
    126.                 // Output transformed position:
    127.                 o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
    128.  
    129.                 // Output light vector:
    130.                 o.lightVec = lightDirection;//normalize(-WorldSpaceLightDir(i.vertex));
    131.  
    132.                 // Compute position in view space:
    133.                 float3 Pview = WorldSpaceViewDir(i.vertex);
    134.  
    135.                 // Transform the input normal to view space:
    136.                 o.normal = mul(_Object2World, float4(i.normal, 0));//normalize(i.normal);
    137.  
    138.                 // Compute the view direction in view space:
    139.                 o.viewVec = -normalize(Pview); //
    140.  
    141.                 // Propagate texture coordinate for the object:
    142.                 o.texCoord0 = i.texCoord0;
    143.  
    144.                 o.texCoord1 = i.texCoord1;
    145.  
    146.                 float numTextWidth = _DeckWidth / (_TextureWidth / 100);
    147.                 float numTextHeight = _DeckHeight / (_TextureHeight / 100);
    148.  
    149.                 float xScale = lerp(numTextHeight, numTextWidth, sinX);
    150.                 float yScale = lerp(numTextWidth, numTextHeight, sinX);
    151.  
    152.  
    153.                 // output
    154.                 //o.texCoord0.x *= numTextHeight;//numTextWidth;
    155.                 //o.texCoord0.y *= numTextWidth;//numTextHeight;
    156.  
    157.                 //float cCustomScale = 4.0; // custom scale from AtlAtl
    158.  
    159.                 o.texCoord0.x *= xScale;//numTextWidth;
    160.                 o.texCoord0.y *= yScale;//numTextHeight;
    161.  
    162.  
    163.                 // output atten
    164.                 o.atten = atten;
    165.  
    166.                 o.tangent = mul(_Object2World, float4(i.tangent, 0));
    167.  
    168.                 return o;
    169.             }
    170.  
    171.             ///////////////////////////////////////////////////////////////////
    172.  
    173.             struct PS_INPUT
    174.             {
    175.                 float2 texCoord0        : TEXCOORD0;
    176.                 float2 texCoord1        : TEXCOORD1;
    177.                 float3 normal        : TEXCOORD2;
    178.                 float3 viewVec        : TEXCOORD3;
    179.                 float3 lightVec        : TEXCOORD4;
    180.                 float atten            : TEXCOORD5;
    181.                 float3 tangent        : TEXCOORD6;
    182.             };
    183.             ///////////////////////////////////////////////////////////////////
    184.  
    185.             float4 frag(PS_INPUT i) : COLOR
    186.             {
    187.                 float4 resClr;
    188.  
    189.                 // simple output
    190.                 //float4 diffuseTexClr = tex2D(_MainTex, i.texCoord0);  
    191.                 float4 roofTexClr = tex2D(_RoofTex, i.texCoord0);
    192.  
    193.                 //
    194.                 float3 tangent = normalize(i.tangent);
    195.                 float3 normal = normalize(i.normal);
    196.                 float3 binormal = normalize(cross(normal, tangent) * tangent);
    197.                 float3x3 worldToTangent = float3x3(tangent, binormal, normal);
    198.  
    199.                 //
    200.                 float3 roofN = normalize(UnpackNormal(tex2D(_RoofNormalMap, i.texCoord0)).xyz);
    201.                 //roofN = mul(roofN, worldToTangent);
    202.                 //roofN = mul(_Object2World, float4(roofN, 0));
    203.  
    204.                 //float3 roofN = float3(0, 0, 1);
    205.                 //roofN = mul(_Object2World, float4(roofN, 0));
    206.  
    207.                 //
    208.                 float3 V = normalize(i.viewVec);
    209.                 float3 L = normalize(i.lightVec);
    210.                 float3 H = reflect(L, roofN);
    211.                 float NdotL = max(0, dot(roofN, L));
    212.                 float HdotV = max(0, dot(H, V));
    213.  
    214.  
    215.                 // ambient
    216.                 float3 ambientLighting = UNITY_LIGHTMODEL_AMBIENT.rgb * 2.3 * _Color.rgb * roofTexClr.rgb;// * diffuseTexClr;
    217.  
    218.                 // diffuse
    219.                 float3 diffuseLighting = _LightColor0.rgb * 1.5 * _Color.rgb * roofTexClr.rgb * i.atten * NdotL;//* diffuseTexClr * i.atten * NdotL;  
    220.  
    221.                 // specular
    222.                 float specPower = pow(HdotV, _SpecShininess) * _SpecPower;
    223.                 float3 specularColor = _LightColor0.rgb * i.atten * _SpecualColor.rgb * specPower;
    224.  
    225.                 // result          
    226.                 resClr.rgb = ambientLighting + diffuseLighting+ specularColor;  
    227.                 resClr.a = 1;//diffuseTexClr.a;
    228.  
    229.                 return resClr;
    230.             }
    231.             ///////////////////////////////////////////////////////////////////
    232.  
    233.             ENDCG
    234.         }
    235.     }
    236.     //Fallback "Diffuse"
    237. }
    Unity upgraded shader with:

    // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'


    I've already tried suggestions from: https://issuetracker.unity3d.com/is...ippos-star-faulty-replacement-breaking-shader, but didn't help.

    Though I'm bad at shaders I hope you guys would have any ideas.
     

    Attached Files: