Search Unity

Custom leaves and double sided material

Discussion in 'General Graphics' started by konsic, Apr 3, 2019.

  1. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    If I want to create custom leaves on a tree, is it wise to use double sided material?

    So, I would like to use quad with leaf texture on it, but quad is only showing one side.

    Is it OK and performant to use double sided material or is there other better way to do it?
     
  2. Sh-Shahrabi

    Sh-Shahrabi

    Joined:
    Sep 28, 2018
    Posts:
    56
    Depends on how many leaves you want to have. I might be wrong, I never tested this but I assume adding double sided to it would more or less double your render load. If you are going to do leaves, there are some optimizations you could use. Use Cut out with fragment discard instead of semi transparency. Try using GPU instancing, so that your draw calls don't go over board. And if you have lighting in (this is not optimization, but it will look alot better) don't use the quad's normal as actual normals, pass on a spherical mapped normal to get smoothed shading, with slight variation
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    A custom double sided material is often the best way to go about this, especially if you're not trying to use a single material on the entire tree / plant. You likely want to avoid using a double sided material on the trunk and branches of a tree if you have a decent amount of that geometry, but for anything that is essentially flat geometry it's great.

    Not really, no. Compared to the alternative of doubling the geometry, it's much cheaper. Doubling the geometry means doubling the vertex count as you can't reuse the vertex data for both sides due to needing to flip the vertex normals. For double sided geometry you can do trivially flip the normal in a shader with VFACE or SV_IsFrontFacing.

    It is true you're potentially doubling the fill rate if you have a perfectly even number of quads facing away and towards the camera, but so would doubling the geometry in that case, or even switching to using camera facing quads. If you have geometry that is already facing the camera, Cull Off has zero impact on the performance, and the normal flipping is an insignificant amount of additional math.

    Cut out can be much more expensive than alpha blending. It depends heavily on the platform you're targeting, and if you have ZWrite On or Off. For mobile, you should avoid ZWrite On w/ alpha testing. Alpha blending will be much less expensive. For desktop / console, ZWrite On w/ alpha testing can be less expensive than alpha blending if there are a lot of overlapping layers, as there often are for for leaves, but if this is an issue overall will depend a lot on the rest of your scene and where your bottlenecks are.

    If @konsic is using individual quads, Unity will be dynamically or statically batching the geometry already (which is generally going to be faster to render than instancing), or automatically using instancing if using a Surface Shader or Shader Graph. There's not really anything that needs to be done here for either of these to happen.

    Yep, this is a really popular technique for faking the more complex nature of foliage lighting.
    https://web.archive.org/web/20180805124238/https://ericchadwick.com/img/tree_shading_examples.html
    https://simonschreibt.de/gat/airborn-trees/
    Lots more links out there on the topic.
     
    Sh-Shahrabi and konsic like this.
  4. florianalexandru05

    florianalexandru05

    Joined:
    Mar 31, 2014
    Posts:
    1,813
    Custom normals are fine, I actually like them but there is a problem with Unity and custom normals, the normal maps shade incorrectly with custom mesh normals!! Has anyone noticed this, some faces shader wrong despite flipping backface?
     
  5. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    simply mirroring or flipping the normal based on vface unfortunately produces right the opposite of what you would like to achieve when using custom smoothed normals.
    for this reason guerrilla games in hzd e.g. flips normals in view space if the mesh uses smoothed normals and only touches the z component. i guess newer cti shaders support this as well :)