Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

why unity specular looks different to custom specular?

Discussion in 'Shaders' started by shmomo, Jan 23, 2016.

  1. shmomo

    shmomo

    Joined:
    Oct 11, 2011
    Posts:
    119
    here is an image of my blinn phong specular vertex shader and unitys mobile bumped specular shader.

    its subtle but the results are different. any idea why they might differ?

    custom.jpg unity.jpg

    here is my blinn phong code..

    Code (CSharp):
    1.                 float4 lightDirection = normalize(_WorldSpaceLightPos0);
    2.  
    3.                 float diffuseReflection = max(dot(input.normal, lightDirection), 0.0);
    4.  
    5.                 float4 ambientLight = UNITY_LIGHTMODEL_AMBIENT * _Color;
    6.  
    7.                 float3 viewDirection = normalize(_WorldSpaceCameraPos);
    8.  
    9.                 float3 halfAngleVector = normalize(lightDirection + viewDirection);
    10.  
    11.                 float specularLight = pow(max(dot(input.normal, halfAngleVector), 0.0), _Shininess * 128.0);
    12.                 if (diffuseReflection <= 0) specularLight = 0;
    13.  
    14.                 float4 specularReflection = _LightColor0 * specularLight;
    15.  
    16.                 output.color = (_Color * _LightColor0 * diffuseReflection) + ambientLight + specularReflection;
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    11,616
    shmomo likes this.
  3. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Yeah, just look at Mobile Bump Specular source code. Unity computes viewDir and halfDir per-vertex and the rest is per-pixel. It looks like you're computing everything per-vertex?
     
    shmomo likes this.
  4. shmomo

    shmomo

    Joined:
    Oct 11, 2011
    Posts:
    119
    So with surface shaders the surf function gets used by a fragment function somewhere?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    11,616
    No, but Unity has a habit of sharing code so I, likely incorrectly, assumed there would be some code in that file the default shader is using. You might want to go the route @Michal_ suggested instead (it'll be in that same download).
     
  6. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    The surf function is called somewhere inside fragment shader. The idea is to decouple material properties from actual lighting. You define material properties inside surface shader. Things like albedo color, emissive color etc. Unity will then generate actual vertex/fragment shader that will take material properties and compute lighting. Unity will in fact generate many different shaders for different light types, fog, lightmaps, shadows etc. Relevant shader is then picked up at run-time based on lights in scene. This way you don't have to worry about how many light are there in your scene or what type these light are. It will just work.
    You can select surface shader and click on "Show Generated Code" button in Unity to see how the generated shaders look like.
     
    shmomo likes this.
unityunity