Search Unity

Help with custom mobileBumpedSpec shader

Discussion in 'Shaders' started by Magoga, Aug 25, 2013.

  1. Magoga

    Magoga

    Joined:
    Jan 24, 2013
    Posts:
    134
    when the object is in the scene in the 0,0,0 coordinate, all rendered normally. However, if strong enough to shift in any direction, for example 100 meters, it is rendered as horseradish knows what .. and has anywhere Redner same .. Of course I already went through all the shader can not understand where the error ... head is steaming...



    shader written in CG .. and at once to get rid of unnecessary questions like "why not use SurfaceShaders"

    I have a few vertex modifications, like
    "o.pos.z - = v.texcoord1.y * _DepthFake * .01;" which I understood to SurfaceShaders not do ..
    and in any case .. shader for mobile .. and this is rendered more quickly than analogue SurfaceShader, maybe removed "all unnecessary" ..

    Code (csharp):
    1. Shader "Joust/Character/Metal"
    2. {
    3.     Properties
    4.     {
    5.         _TintColor ("TintColor(RGB)", Color) = (0.5,0.5,0.5,1.0)
    6.         _TintColor2 ("TintColor2(RGB)", Color) = (0.5,0.5,0.5,1.0)
    7.         _MainTex ("Base (RGB), TintMask(A)", 2D) = "grey" {}
    8.         _NormalMap ("Normal Map", 2D) = "bump" {}
    9.         _Specular ("Specular", Float) = 1.0
    10.         _Shininess ("Shininess", Float) = 10
    11.        
    12.         _LightColor1 ("_LightColor(RGB)", Color) = (0.5,0.5,0.5,1.0)
    13.        
    14.         _REFP ("Reflection Power", Range(0.01,5.0)) = 0.1
    15.         _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
    16.        
    17.         _DepthFake ("_DepthFake", Float) = 0.05
    18.     }
    19.    
    20.    SubShader
    21.    {
    22.       LOD 400
    23.       Pass
    24.       {    
    25.          Tags { "LightMode" = "ForwardBase" }
    26.  
    27.          CGPROGRAM
    28.  
    29.          #pragma vertex vert  
    30.          #pragma fragment frag
    31.          #pragma only_renderers  d3d9  opengl  gles  
    32.  
    33.  
    34.          uniform sampler2D _NormalMap, _MainTex;  
    35.          samplerCUBE _Cube;
    36.          
    37.          uniform half _Shininess, _Specular, _REFP, _DepthFake;
    38.  
    39.          uniform fixed3 _LightColor0,  _TintColor, _LightColor1, _TintColor2;
    40.  
    41.  
    42.          struct appdata
    43.          {
    44.             half4 vertex : POSITION;
    45.             fixed2 texcoord : TEXCOORD0;
    46.             fixed3 normal : NORMAL;
    47.             fixed4 tangent : TANGENT;
    48.             fixed2 texcoord1 : TEXCOORD1;
    49.          };
    50.          
    51.          struct vtf
    52.          {
    53.             half4 pos : SV_POSITION;
    54.             half3 posWorld : TEXCOORD0;
    55.             fixed2 texcoord : TEXCOORD1;
    56.             fixed3 tangentWorld : TEXCOORD2;  
    57.             fixed3 normalWorld : TEXCOORD3;
    58.             fixed3 binormalWorld : TEXCOORD4;
    59.             fixed3 viewDir : TEXCOORD5;
    60.             fixed3 LightDir : TEXCOORD6;
    61.             fixed rim : TEXCOORD7;
    62.            
    63.          };
    64.  
    65.          vtf vert(appdata v)
    66.          {
    67.             vtf o;
    68.            
    69.             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    70.             o.pos.z -= v.texcoord1.y * _DepthFake * .01 ;
    71.            
    72.             o.tangentWorld = normalize(fixed3(mul(_Object2World, fixed4(v.tangent.xyz, 0.0)))).xyz;
    73.             o.normalWorld = normalize(mul(fixed4(v.normal, 0.0), _World2Object)).xyz;
    74.             o.binormalWorld = normalize(cross(o.normalWorld, o.tangentWorld) * v.tangent.w).xyz;
    75.  
    76.             o.posWorld = mul(_Object2World, v.vertex).xyz;
    77.             o.texcoord = v.texcoord;
    78.                        
    79.             o.viewDir = normalize(_WorldSpaceCameraPos - o.posWorld);
    80.             o.LightDir = normalize(_WorldSpaceLightPos0.xyz);
    81.            
    82.             o.rim = 1.0 - saturate(dot (normalize(o.viewDir), o.normalWorld));
    83.             o.rim = pow(o.rim,_REFP) * 4;
    84.            
    85.             return o;
    86.          }
    87.  
    88.          fixed4 frag(vtf i) : COLOR
    89.          {
    90.             fixed4 tex = tex2D(_MainTex, i.texcoord);
    91.                    
    92.             fixed4 norm = tex2D(_NormalMap, i.texcoord);
    93.             fixed3 localCoords = fixed3(2.0 * norm.xy - 1, 1 );
    94.  
    95.             half3x3 local2WorldTranspose = half3x3
    96.             (
    97.                i.tangentWorld,
    98.                i.binormalWorld,
    99.                i.normalWorld
    100.             );
    101.            
    102.             fixed3 normal = normalize(mul(localCoords, local2WorldTranspose));
    103.            
    104.             tex.rgb = (tex.rgb * _TintColor * norm.b + tex.rgb * _TintColor2 * norm.a) * 2 + tex.rgb * (1-(norm.b + norm.a));
    105.            
    106.             fixed diff = max(0.0, dot(normal, i.LightDir));
    107.             fixed diff1 = max(0.0, dot(normal, -i.LightDir));
    108.                    
    109.             diff *= diff * diff + (diff * 0.5);        
    110.                                
    111.             fixed3 h = normalize(i.LightDir + i.viewDir);
    112.             fixed3 h1 = normalize(-i.LightDir + i.viewDir);
    113.                    
    114.             fixed nh = max (0, dot (normal, h));   
    115.             fixed nh1 = max (0, dot (normal, h1));
    116.            
    117.             half gloss =  tex.a * _Specular;
    118.            
    119.             half spec = pow (nh, _Shininess) * gloss;          
    120.             half spec1 = pow (nh1, _Shininess) * gloss;
    121.            
    122.             half3 Lighting = ((diff1 + spec1) * _LightColor1  + (diff + spec) * _LightColor0  + UNITY_LIGHTMODEL_AMBIENT.xyz  ) * 2;
    123.  
    124.             fixed3 reflectedDir = reflect(-i.viewDir, normal);
    125.             fixed3 ref = texCUBE(_Cube, reflectedDir) * tex.a * i.rim;
    126.            
    127.             fixed4 c;
    128.             c.rgb = Lighting * tex.rgb + ref;
    129.             c.a = 1;
    130.  
    131.             return  c;
    132.          }
    133.  
    134.          ENDCG
    135.       }
    136.     }
    137.  
    138.  
    139.    SubShader
    140.    {
    141.       LOD 200
    142.       Pass
    143.       {    
    144.          Tags { "LightMode" = "ForwardBase" }
    145.  
    146.          CGPROGRAM
    147.  
    148.          #pragma vertex vert  
    149.          #pragma fragment frag
    150.          #pragma only_renderers  d3d9  opengl  gles  
    151.  
    152.  
    153.          uniform sampler2D _NormalMap, _MainTex;  
    154.          
    155.          uniform half _Shininess, _Specular, _DepthFake;
    156.  
    157.          uniform fixed3 _LightColor0,  _TintColor, _TintColor2;
    158.  
    159.          struct appdata
    160.          {
    161.             half4 vertex : POSITION;
    162.             fixed2 texcoord : TEXCOORD0;
    163.             fixed3 normal : NORMAL;
    164.             fixed4 tangent : TANGENT;
    165.             fixed2 texcoord1 : TEXCOORD1;
    166.          };
    167.          
    168.          struct vtf
    169.          {
    170.             half4 pos : SV_POSITION;
    171.             half3 posWorld : TEXCOORD0;
    172.             fixed2 texcoord : TEXCOORD1;
    173.             fixed3 tangentWorld : TEXCOORD2;  
    174.             fixed3 normalWorld : TEXCOORD3;
    175.             fixed3 binormalWorld : TEXCOORD4;
    176.             fixed3 viewDir : TEXCOORD5;
    177.             fixed3 LightDir : TEXCOORD6;
    178.            
    179.          };
    180.  
    181.          vtf vert(appdata v)
    182.          {
    183.             vtf o;
    184.            
    185.             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    186.             o.pos.z -= v.texcoord1.y * _DepthFake * .01 ;
    187.            
    188.             o.tangentWorld = normalize(fixed3(mul(_Object2World, fixed4(v.tangent.xyz, 0.0)))).xyz;
    189.             o.normalWorld = normalize(mul(fixed4(v.normal, 0.0), _World2Object)).xyz;
    190.             o.binormalWorld = normalize(cross(o.normalWorld, o.tangentWorld) * v.tangent.w).xyz;
    191.  
    192.             o.posWorld = mul(_Object2World, v.vertex).xyz;
    193.             o.texcoord = v.texcoord;
    194.                        
    195.             o.viewDir = normalize(_WorldSpaceCameraPos - o.posWorld);
    196.             o.LightDir = normalize(_WorldSpaceLightPos0.xyz);
    197.                        
    198.             return o;
    199.          }
    200.  
    201.          fixed4 frag(vtf i) : COLOR
    202.          {
    203.             fixed4 tex = tex2D(_MainTex, i.texcoord);
    204.                    
    205.             fixed4 norm = tex2D(_NormalMap, i.texcoord);
    206.             fixed3 localCoords = fixed3(2.0 * norm.xy - 1, 1 );
    207.  
    208.             half3x3 local2WorldTranspose = half3x3
    209.             (
    210.                i.tangentWorld,
    211.                i.binormalWorld,
    212.                i.normalWorld
    213.             );
    214.            
    215.             fixed3 normal = normalize(mul(localCoords, local2WorldTranspose));
    216.            
    217.             tex.rgb = (tex.rgb * _TintColor * norm.b + tex.rgb * _TintColor2 * norm.a) * 2 + tex.rgb * (1-(norm.b + norm.a));
    218.            
    219.             fixed3 diff = max(0.0, dot(normal, i.LightDir));
    220.                    
    221.             diff *= diff + 0.5;        
    222.                                
    223.             fixed3 h = normalize(i.LightDir + i.viewDir);
    224.                    
    225.             fixed nh = max (0, dot (normal, h));   
    226.            
    227.             half gloss =  tex.a * _Specular;
    228.            
    229.             half spec = pow (nh, _Shininess) * gloss;          
    230.            
    231.             half3 Lighting = ((diff + spec) * _LightColor0  + UNITY_LIGHTMODEL_AMBIENT.xyz) * 2;
    232.  
    233.             fixed4 c;
    234.             c.rgb = Lighting * tex.rgb ;
    235.             c.a = 1;
    236.  
    237.             return  c;
    238.          }
    239.  
    240.          ENDCG
    241.       }
    242. }
    243.  
    244. SubShader
    245. {
    246.  
    247.     Tags { "LightMode" = "Vertex" }
    248.     Pass
    249.     {
    250.             color (1,1,1,1)
    251.     }
    252. }
    253.  
    254. }
    255.  
     
  2. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    Try to compute lighting in tangent space instead of world space.