Search Unity

To Lambert or to not Lambert...

Discussion in 'Shaders' started by LightStriker, Jul 15, 2014.

  1. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    While publishing on mobile, is there a difference in performance in using Lambert or not using it and writing my own vert/frac?

    Let's say I have a mesh that is not lit, and only take vertex color and/or a texture?
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    It's pretty much always possible to write something more efficient than a surface shader for a mobile device. In general, surface shaders are only useful if you want to fully support Unity's lighting pipeline. If you don't want lighting at all, you should write a vertex/fragment shader by hand.
     
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Thanks, was wondering. Any idea how much of a difference it is? Is there a way to write vertex lighting as efficient as possible? Frankly, the pixel shaders are rather destructive on iPhone 4.
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Have you tried using the built-in VertexLit shaders? They should be pretty good.
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Yeah, but the issue is we need to use vertex color, and not a texture, so I need to write custom stuff anyway.
     
  6. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    You're probably aware of this, but just because I like to nitpick, "Lambert" is really just the part of lighting that does

    Code (csharp):
    1.  
    2. NdotL = dot(normalizedSurfaceNormal, normalizedDirectionToIncomingLight);
    3. litCol = albedo * lightCol * NdotL;
    4.  
    As soon as you do lighting you'll always have this term in there somewhere, whether custom vert/frag or built-in Surface Shaders, whether your lighting is per-vertex or per-pixel, whether you add a specular term or not.. Even for an alternative diffuse term "other than Lambert" such as Oren-Nayer this term is still in there somewhere!

    Just to ensure that no further misconceptions about Lambert enter the airwaves ;)
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Well, doing lighting per vertex instead of per pixel mean I only do this math once per vertex instead of one per pixel. That's a HUGE performance boost. Does Lambert allow per-vertex lighting?
     
  8. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    YES Lambert is just the albedo*NdotL formula, whether the N part is per-vertex normal or per-pixel normal is your choice essentially.
     
  9. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    How am I suppose to set Lambert to not perform any light math per pixel?
     
  10. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    At your current stage of understanding I'd suggest you use the built-in "VertexLit" surface shaders and the VertexLit (as opposed to Forward or Deferred) rendering path and you'll be good to go ;)

    If you do insist regardless on writing your own vert+frag shaders there's not much Unity or ShaderLab-specific to worry about, any of the 1000s of web tutorials on basic vertex lighting will do the trick and the Unity wiki I believe have all or most of the vertex-lit vert+frag shaders / templates you should realistically need.

    If you run into a specific issue, you might wanna post a new specific question with your shader code that's troubling you :D