Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Flat triangles from lighting/vertex colors

Discussion in 'Shaders' started by Salmonman, Jul 14, 2016.

  1. Salmonman

    Salmonman

    Joined:
    Jul 14, 2016
    Posts:
    18
    I know very little about how to create shaders, but I need a specific one for my project and I can't find it already made.

    What I need basically is a shader that will have vertex colors, but instead of computing a merged color for each texel, I want it to average the values from the vertex color of each tri's 3 vertices, as well as the lighting information from the veritces, and use that resultant average color for every texel in the tri. The end result would be that every triangle rendered would be one solid color.



    Besides that, it would act like the default shader in every other respect, smoothness, sun reflections, and (hopefully) colored lighting just doing calculations for the verts instead of every single texel. Retaining regular shadows would be highly desired, and would be the only exception to the rule of having every tri be a solid color in the finished render.

    Would this be easy enough for me to do myself? If not I'm willing to commission someone to make it. If any more information is necessary, I would be glad to provide it.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    8,699
    Usually the way this is done isn't via a shader, but in the model itself. There's an asset on the store called PolyWorld (which that screenshot you posted is from afaik) that will take any mesh and distill it down to solid colored flat shaded mesh.
    http://qt-ent.com/PolyWorld/scripts/

    The other way to do this is using the nointerpolation keyword on shader vertex parameters. I posted about it here.
    http://forum.unity3d.com/threads/solved-flat-shading.410943/

    The benefit of the poly world route is you get a lot of control and can guarantee the color of each polygon be unique. It's also much easier to get any shader you want working with then. The downside is the models get much larger data wise because no vertices are shared between triangles. Another side effect is polygons on skinned meshes might not remain completely flat as the bigger weights can distort the normals.

    The nointerpolation method has the benefit of working on any mesh regardless of how they're setup, and don't require split edges making for much more efficient meshes. It'll also keep polygons flat shaded when using skinned meshes, though the direction they light might not perfectly match the surface orientation. There downside is you do have to make custom shaders for this as surface shaders, the "easy" way to get the same look as Unity's standard shader or other lighting models, doesn't support defining the interpolation type. There's also no guarantee that every triangle will have a unique color as the color and normal come from the first vertex of the triangle and sometimes two adjoining triangles can reuse the same vertex.

    There's a third method which is even more involved using geometry shaders to do the work that the poly world scripts do, but so it in real-time. This can get really expensive quickly.
     
  3. Salmonman

    Salmonman

    Joined:
    Jul 14, 2016
    Posts:
    18
    Hmm, thanks for the insight.
     
  4. CoCoNutti

    CoCoNutti

    Joined:
    Nov 30, 2009
    Posts:
    506
    This is what I do in 2020:
     
  5. blai30

    blai30

    Joined:
    Aug 18, 2015
    Posts:
    7
    Do you know how to apply this to Polybrush generated meshes? When I use Polybrush to modify the meshes, the shading becomes smooth.

    EDIT: To anyone reading this in April 2020 or later, add this to your terrain shader
     
    Last edited: Apr 20, 2020
unityunity