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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to get Flat (non gouraud) shading

Discussion in 'Shaders' started by tomnullpointer, Sep 27, 2010.

  1. tomnullpointer

    tomnullpointer

    Joined:
    Sep 20, 2010
    Posts:
    142
    Hi all,

    In D3D i can specify a flat lighting model (rather than gouraud), this mean si can check out the look of a flat shaded verison of models without having to mess with the normals. Is there an equivalent way to do this in Unity/shaderlab? Or do I have to split all my trianlges up so as not to share vertexes and assign them all their own (3 equal) normals?
     
  2. tomnullpointer

    tomnullpointer

    Joined:
    Sep 20, 2010
    Posts:
    142
    hmm, im not sure you understand what im after.
    I still need lighting. but i need a flat shading model, not an interpolated one.
    ie every triangle is rendered with a single face normal for the light calculation.
     
  3. yvesbodson

    yvesbodson

    Joined:
    Jul 15, 2010
    Posts:
    64
    I read that you suggest to use 'the import settings"?
    Where can I set the import into Unity 3
    I import assets from 3DS and Maya but I never found any settings for import ?
    What did I miss here?
    Thanks to lighten this to me and maybe others
    Yves

    SORRY I realize that the Editor is the importer settings..
    Got it
    yves
     
  4. tomnullpointer

    tomnullpointer

    Joined:
    Sep 20, 2010
    Posts:
    142
    Im not importing anything, the mesh is generated on the fly.
     
  5. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Then just don't share vertices between faces that you want separated, as you suggested. You can of course share vertices across the triangles that you want to all have the same normal.
     
    Last edited: Sep 28, 2010
  6. tomnullpointer

    tomnullpointer

    Joined:
    Sep 20, 2010
    Posts:
    142
    Ah ok then,
    I just thought there might be a built in option to do it like there is in D3D.
    Seems pretty inefficient to have to increase the Vertex Buffers when essentially every 3 normals is the same.
     
  7. derPuppeteer

    derPuppeteer

    Joined:
    Mar 21, 2014
    Posts:
    23
    Is there now a way to set the shademode to flat?
    It would pretty much reduce the vertex count by a factor of 3 for me.

    Thank you!
    (For helping in a dead thread :D )
     
  8. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    You're probably far better off making things flat shaded by verts than by shader code.
     
  9. derPuppeteer

    derPuppeteer

    Joined:
    Mar 21, 2014
    Posts:
    23
    Hi Farfarer,
    thank you for your reply. But I won't be better of without flat shading.
    I am doing some procedural vertex animation and my meshes hit the 65k Vertices limit. And I need flat normals for my artstyle.
    Being able to get the normals by the provoking vertex would reduce my vertexcount almost by a factor of 3.

    In normal HLSL Code you could just say "shademode = Flat;" and you will be good. But in unity I have yet to find that option.
    Any ideas?

    EDIT:
    http://http.developer.nvidia.com/Cg/ShadeMode.html
     
  10. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    You are right, there should be that option in Unity, for style reasons and control over the appearence. It's totally a missing feature from Unity, that people ask about, that i have looked for, and that is inconvenient. It should be possible by shader. I don't believe that you can even get position of the 3 triangles of every vertex withing the shader, unless you are using a dx11 geometry shader, so you can't even do it with advanced CG code unless using the geometry shader.

    So yes, perhaps make a suggestion to Unity?

    If your vertices are already 65k then you can't duplicate them. obviously you have a setting that says make new mesh every 65k. you'd have to set that to 21k and duplicate them.

    Using DX11, i think its possible with a geometry shader, and vertex positions, someone else would know, i dont really! Do you have to take the 3 vertices, make new normals for them, and pass the normals on to the color function with ambient lighting info, does the geometry shader arrive prior to the lighting options?

    if they were less numerous, obviously you could write something like the following code and add it to wherever you have flat shading:


    this code was inteded for only planes, which had normals generated afterwards. i dont know if it works forother things. i just added a line for normals just now. chances are that you can make 21k meshes, slap this code on them and they will become flat ones.
    Code (csharp):
    1.         //---the following mesh code is an optional stylisation code
    2.         //---to make mesh force "flat shading"(flat retro shadows)....
    3.         var mesh : Mesh = pl.GetComponent(MeshFilter).mesh;
    4.         var myvertices : Vector3[]= mesh.vertices;
    5.         var mytriangles : int[] = mesh.triangles;
    6.         var mynormals : Vector3[]= new Vector3[mytriangles.Length];
    7.         var newvertices = new Vector3[mytriangles.Length];     
    8.         var newUVs =  new Vector2[mytriangles.Length];
    9.        
    10.         for (var TR:int = 0; TR < mytriangles.length ; TR ++){ 
    11.             newvertices[TR] = myvertices[mytriangles[TR]];
    12.             mynormals[TR] = mesh.normals[mytriangles[TR]];         
    13.             mytriangles[TR] = TR;
    14.            
    15.                   // sry i didnt reweite the UV's like for you,  it should be possible to copy each uv from old vertex same as the nromals/
    16.  
    17.         }
    18.         mesh.vertices = newvertices;
    19.         mesh.triangles = mytriangles;
    20.                 mesh.normals = mynormals;
    21.         mesh.uv = newUVs;
     
    Last edited: Apr 1, 2014