Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

So flat shading doesn't work with the cloth component. Any alternatives?

Discussion in 'General Graphics' started by Bionicle_fanatic, Nov 8, 2017.

  1. Bionicle_fanatic

    Bionicle_fanatic

    Joined:
    Jun 8, 2013
    Posts:
    368
    The title, pretty much. Flat shading simply doesn't work with cloth, no matter what I try. Turning down the model's smoothing angle, stopping the mesh from welding vertice, even manually detaching each face in the model editor. Are there any alternatives to the cloth component, or any workarounds people have figured out?
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    One of these options should work (they don't depend on topology):
    • Use a high res normal map where each normal maps directly to the triangle of the mesh
    • Use a special shader that would calculate the normals
     
    theANMATOR2b likes this.
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    bgolus and hippocoder like this.
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    This would only work well when the cloth is at or near it's initial "pose". Any deformation and none of these surfaces will look flat anymore, just oddly creased.

    This. See @jvo3dc 's response.

    However I'd swap those two as for which is more efficient. For low end mobile GPUs the cost of a normalize and cross isn't insignificant, but they're also likely won't have support for geometry shaders. Any GPU with support for geometry shaders the cost is insignificant, and the derivative functions are essentially free. The harder part is going to be using these either of these in a Surface shader if that's needed.



    Note MacOS and iOS are both moving towards completely removing support for geometry shaders. The Metal graphics API does not offer them at all. Not including, and refusing to add support for geometry shaders really is properly courageous of them. I suspect a near future where Apple hardware drops support for OpenGL entirely considering their extreme push for companies (like Unity) to support Metal by default.

    There was a lot of discussion on not including geometry shader support for Vulkan (the successor to OpenGL) but it was ultimately included. It's something many hardware manufacturers have been fighting against since it's original inclusion.
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Why is it better to not have geometry shader support? Sorry, lacking in background knowledge and Google isn't being clear enough.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    My understanding is from a hardware stand point its inefficient. It's doing relatively small batches of work each with high bandwidth and potentially varying amounts of output data in the middle of a highly serialized operation.

    I'm having a hard time trying to think of a good metaphor for what's happening.

    You can try reading this.
    http://www.joshbarczak.com/blog/?p=667
     
    Last edited: Nov 9, 2017
    theANMATOR2b and hippocoder like this.
  7. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    In terms of efficiency I was really only thinking of the pixel shader option calculating the same value for every pixel, while the geometry shader option only calculating it once for every time it's needed. In terms of performance it might well be that the pixel shader solution is faster. It's more parallel and the derivatives are indeed essentially free. (As long as all pixel shading runs in parallel, so avoid conditional (if) statements in front of the use of derivatives.)

    I must say I don't use geometry shaders much. I usually premake my geometry and then clip what isn't needed. Just trying to make the GPU happy ;-) It would really help to have an option to disable automatic frustum culling for these objects where you move everything around in the vertex shader. It's getting a bit annoying to have to move all these objects into the view of every camera to avoid them being frustum culled.
     
    hippocoder likes this.
  8. Bionicle_fanatic

    Bionicle_fanatic

    Joined:
    Jun 8, 2013
    Posts:
    368
    That would would require a deep understanding of shader scripting, which I really can't be bothered to deal with, given the lack of documentation.
     
  9. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    theANMATOR2b likes this.
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Or you can get one of the (seemingly hundreds) of flat shading / "low poly" shaders out there for free or on the asset store.

    Most are sadly geometry shader based, but c'est la vie.
     
    hippocoder likes this.
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Random factoid: Naughty Dog basically stopped using tangent space normal maps several years ago, and many other AAA games as well. Why? Because it was slower to use a mesh with tangents than calculating it all in the fragment shader.

    Basically the cost of transferring the tangent and bitangent from the vertex shader to the fragment shader was the same or greater than passing the world position and doing a ton more math, in 2010. GPUs' math performance since then has increased by ~8x, but memory bandwidth (what limits transferring data) only ~2x, so it's only gotten better for doing it all in the shader.
     
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Oh, interesting! I guess tangent space normal maps are still a thing because fragment shader performance is pretty bad on old mobiles.

    Or is Unity just being a bit backward here?
     
  13. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    I wouldn't say they're being backwards, it's just where most tools still are today. Big AAA studios can more easily afford to write custom tools and retrain their artists. Everything under the sun outputs tangent space normal maps, and there are still some potential quality benefits of using interpolated tangent space over derivatives. The most basic problem is the same reason you can use derivatives to calculate flat normal maps, they're the "real" normals of the flat polygons. When you calculate the tangents with derivatives you also get the "real", ie: flat, tangents.

    Here's an example of the issue that causes.
     
    MadeFromPolygons and nat42 like this.
  14. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I've actually been using it probably since about 2009/2010. But that was because in the engine I used at that time, the tangent generation was usually not working correctly. Partial derivatives do suffer from minor artifacts, because the changing direction of the derivative. (Either comparing to the pixel on the left or the right and either the top or the bottom.)

    I do want to note that these are, at least what I did before, still tangent space normal maps. I just didn't use the tangents stored with the vertices. By combining the derivatives of the uv coordinates and the world position, you can still determine the tangent in the pixel shader.
     
    bgolus likes this.
  15. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    The example image above is using tangent space normal maps, but the "tangents" do not fully match what mesh tangents would produce. In Naughty Dog's case I believe they use derivative bump maps which are very similar to tangent space normal maps (to the point of you can often use tangent space normal maps in their place), but can be more easily and cheaply mixed. However they're limited to 45 degrees off of the normal, where as tangent space normal maps with tangents (either from the mesh or derivatives) can represent any direction.

    That image is from this section of my article on triplanar shading.
    https://medium.com/@bgolus/normal-mapping-for-a-triplanar-shader-10bf39dca05a#a6e3

    Note that one of the articles I link to directly refers to a book from 2006 on the topic. And really all of this dates back to the 70s when they originally made the (then) optimization to move the calculations to per-vertex and interpolating.
     
  16. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    This make me curious how to use that technique in unity. is it possible to remove mesh tangent in unity? never tried that though
     
  17. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    It is. First step, select the mesh and change the Tangents option to None (but keep Normals).

    If you check that link it has the ShaderLab source code for using derivatives instead of the mesh's vertex tangents to construct a tangent to world rotation matrix. The article is about how when using procedural texture mapping (in this case world space triplanar mapping) the mesh's tangents should never, ever be used, or needed ... except for when using Unity's Surface shaders just because Unity will always use the tangents. :mad:

    Note none of Unity's built in shaders do this, they all assume you want to use the mesh tangents when using a normal map.

    I also wish Unity did use derivatives for detail normals in the Standard shader since they allow you to choose the second UV, but still use the mesh tangents (which are based on the primary UV) leading to those normals almost guaranteed to be wrong. It relegates detail normals to hand wavy, nondescript bumpy normals.

    Curiously, Unity has had shader code in the UnityStandardUtils.cginc to do derivative maps since Unity 5.0 was released.
     
  18. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    well that's inconvenient, not in the mood to rewrite standard shaders :p. anyway i'll going to ask around in SRP thread then.
    @Bionicle_fanatic sorry for derailing man.
     
  19. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Does this help?
    https://www.assetstore.unity3d.com/en/#!/content/69971