Search Unity

Problem with Specular Light shader.

Discussion in 'Shaders' started by unity_qVcVUUYUTy628A, Feb 20, 2018.

?

.

  1. .

    1 vote(s)
    100.0%
  2. .

    0 vote(s)
    0.0%
  1. unity_qVcVUUYUTy628A

    unity_qVcVUUYUTy628A

    Joined:
    Feb 20, 2018
    Posts:
    33
    Hello,
    I just made simple shader diffuse+specular+ambient light, but i have an issue about Specular. When Im increasing saturation of _Color property, object become very very bright immediately.
    here is my code:

    Code (CSharp):
    1. Properties{
    2.         _Color("Color", Color) = (1,1,1,1)
    3.         _Texture("Texture", 2D) = "white" {}
    4.         _NormalMap("Normal Map", 2D) = "white" {}
    5.         _Diffuse("Diffuse", Range(0,1)) = 1
    6.         _Specular("Specular", Range(0,1)) = 1
    7.         _Ambient("Ambient", Range(0,1)) = 1
    8.         _SpecularColor("SpecularColor", Color) = (1,1,1,1)
    9.     }
    10.  
    11.         Subshader{
    12.         Tags{"Queue" = "Transparent" "Ignore Projector" = "True" "RenderType" = "Transparent"}
    13.         Pass{
    14.         Tags{"LightMode" = "ForwardBase"}
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.         CGPROGRAM
    17.         #pragma vertex vert
    18.         #pragma fragment frag
    19.  
    20.         uniform float4 _Color;
    21.         uniform sampler2D _Texture;
    22.         uniform float4 _Texture_ST;
    23.         uniform sampler2D _NormalMap;
    24.         uniform float4 _NormalMap_ST;
    25.  
    26.         uniform float4 _LightColor0;
    27.         uniform float _Diffuse;
    28.         uniform float _Specular;
    29.         uniform float4 _SpecularColor;
    30.         uniform float _Ambient;
    31.  
    32.         struct vertexInput {
    33.         float4 vertex : POSITION;
    34.         float4 normal : NORMAL;
    35.         float4 tangent : TANGENT;
    36.         float4 tex : TEXCOORD0;
    37.         };
    38.  
    39.         struct vertexOutput {
    40.             float4 position : SV_POSITION;
    41.             float4 tex : TEXCOORD0;
    42.             float4 normalMap : TEXCOORD1;
    43.             float3 normalWorld : TEXCOORD2;
    44.             float3 tangentWorld : TEXCOORD3;
    45.             float3 binormalWorld : TEXCOORD4;
    46.             float3 view : TEXCOORD5;
    47.         };
    48.  
    49.         float3 convertNM(float4 color) {
    50.             #if defined(UNITY_NO_DXT5nm)
    51.                 return color.rgb * 2 - 1;
    52.             #else
    53.                 float3 newColor = float3(color.a * 2 - 1, color.g * 2 - 1, 0);
    54.                 newColor.z = sqrt(1.0 - dot(newColor, newColor));
    55.                 return newColor;
    56.             #endif
    57.         }
    58.  
    59.         float3 Specular(float3 normal, float3 light, float3 view)
    60.         {
    61.             float3 halfway = light + view;
    62.             return _Specular * pow(max(0, dot(normal, halfway)), 100);
    63.         }
    64.        
    65.         vertexOutput vert(vertexInput v) {
    66.             vertexOutput o;
    67.             o.position = UnityObjectToClipPos(v.vertex);
    68.             o.tex.xy = v.tex.xy * _Texture_ST.xy + _Texture_ST.zw;
    69.             o.normalMap.xy = v.tex.xy * _NormalMap_ST.xy + _NormalMap_ST.zw;
    70.            
    71.             o.view = mul(unity_ObjectToWorld, v.vertex).xyz;
    72.            
    73.             o.normalWorld = normalize(mul(float4(v.normal.xyz, 0.0), unity_WorldToObject).xyz);
    74.             o.tangentWorld = normalize(mul(unity_ObjectToWorld, float4(v.tangent.xyz, 0.0)).xyz);
    75.             o.binormalWorld = normalize(cross(o.normalWorld, o.tangentWorld) * v.tangent.w);
    76.  
    77.             return o;
    78.         }
    79.  
    80.         float4 frag(vertexOutput i) : COLOR{
    81.             float3x3 TBN = float3x3(i.tangentWorld, i.binormalWorld, i.normalWorld);
    82.             float4 NM = tex2D(_NormalMap, i.normalMap.xy);
    83.             float3 normal = convertNM(NM);
    84.             float3 pixelNormal = normalize(mul(normal, TBN));
    85.  
    86.             float3 LightDirection = normalize(_WorldSpaceLightPos0.xyz);
    87.             float3 WSVD = normalize(_WorldSpaceCameraPos - i.view);
    88.  
    89.             float3 diffuse = _Diffuse * _LightColor0.rgb * max(0, dot(pixelNormal, LightDirection));
    90.             float3 specular = Specular(pixelNormal, LightDirection, WSVD) * _SpecularColor;
    91.             float3 ambient = _Ambient * UNITY_LIGHTMODEL_AMBIENT;      
    92.  
    93.             return float4(diffuse + specular + ambient, 1) * _Color;
    94.         }
    95.         ENDCG
    96.         }
    97.     }
     
  2. LukasCh

    LukasCh

    Unity Technologies

    Joined:
    Mar 9, 2015
    Posts:
    102
    Well with _Color property everything is okey, however only specular is very strong, but that is expected when u do pow(x, 100), just try smaller numbers.
     
  3. unity_qVcVUUYUTy628A

    unity_qVcVUUYUTy628A

    Joined:
    Feb 20, 2018
    Posts:
    33
    It was actually another problem, but I allready solved it. I just forgot to normalize "halfway" vector :) Everything is fine right now.
     
    LukasCh likes this.