Search Unity

Help Wanted Vertex Displacement with custom lighting creates issues

Discussion in 'Shader Graph' started by alanmthomas, Nov 23, 2019.

  1. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    I'm attempting to create a vegetation (grass and tree) shader combined with custom toon lighting that we've seen in many examples. The problem I'm running into is that both work fine on their own but doing vertex displacement on a custom lit object creates serious lighting artifacts. Has anyone been able to successfully combine the two?
     
  2. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    I've been able to determine (with the help of advice from some outside sources) that this issue is the result of the lighting information being passed in based on how the light(s) affect the non deformed mesh. Given this, has anyone determined a method for either computing the lighting based on the deformed mesh or having normal recalculation within the shader?
     
  3. A132LW

    A132LW

    Joined:
    Jun 21, 2017
    Posts:
    33
    Hope this helps!
     

    Attached Files:

  4. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    I'll give this a shot. Thanks! Any tips on how to use it? I'm getting errors when I import the package to my project.
     
  5. A132LW

    A132LW

    Joined:
    Jun 21, 2017
    Posts:
    33
    What version are you on? The errors might be from version issues since the graphs were designed on HDRP 7.1.2 @ 2019.3.0b5.
    I can downgrade the package.
     
  6. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    I'm using LWRP in 2019.2.13. Thanks!
     
  7. A132LW

    A132LW

    Joined:
    Jun 21, 2017
    Posts:
    33
    2019.2.0.f1 - LWRP
     

    Attached Files:

  8. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    Thanks so much!
     
  9. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    I'm a little confused on how this would work with the issue that I'm running into. I brought this into my project without any issues (thanks again for the new version) and applied it within the shader that I'm doing custom lighting and vertex displacement on but it seems that the normals are still creating issues. Any pointers on what I could be doing wrong?
     
  10. A132LW

    A132LW

    Joined:
    Jun 21, 2017
    Posts:
    33
    With these settings (default spot texture) on my example
    upload_2019-12-3_8-8-4.png ,
    can you see results as below?

    upload_2019-12-3_8-5-49.png
    upload_2019-12-3_8-6-56.png
    I think the normals do work in my graph; they are just not super accurate.
    If you can see the above from my example, then the issue might be on your end.

    (my shadergraph is based on https://halisavakis.com/my-take-on-shaders-water-shader/ - he talks about normal recalculation there)

    http://thedemonthrone.ca/projects/r...rain-part-20-normal-and-displacement-mapping/
    talks about using more reads to fix accuracy. I will try to make a variant with 8 reads to see if that helps.
     
    Last edited: Dec 3, 2019
  11. A132LW

    A132LW

    Joined:
    Jun 21, 2017
    Posts:
    33
  12. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    All of this works fine for me. I can get displacement using textures. My initial concern was how to displace vertices and recalculate normals for something that is using custom lighting (for a toon shader). I'm still not seeing how this helps with that issue. Sorry.
     
  13. A132LW

    A132LW

    Joined:
    Jun 21, 2017
    Posts:
    33
    I am assuming this is your workflow?:
    Displace vertices, then calculate normals for the displacement, then use shadergraph's normal blend node to add any extra normal detail. Then for a toon shader, simply replace all references to the mesh normal with the normal calculated from displacement.

    My graph is designed to recaculate normals from displacement. I apologize if that did not help.
    If you can post a picture of your issue or give me some more details, then I can take a look.

    Apologies if I haven't helped.
     
  14. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    Please don't apologize. I appreciate your help. Perhaps some images will illustrate the issue I'm facing. First is a tree without displacement and a toon shader applied. Next is the same with vertex displacement. You can see how the normals aren't being adjusted. Screen Shot 2019-12-04 at 3.11.20 AM.png Screen Shot 2019-12-04 at 3.16.22 AM.png
     
  15. grllle

    grllle

    Joined:
    May 4, 2015
    Posts:
    11
  16. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
  17. grllle

    grllle

    Joined:
    May 4, 2015
    Posts:
    11
    Maybe this is not correct but through search of similar use cases i got the impression that the normals (and maybe even tangents) need to be adjusted for correct light calculation. (e.g. https://www.tomlooman.com/rotate-mesh-vertex-shaders-ue4/)
    This makes sense to me and i have the feeling that i know how i would do that in a plain vertex/fragment-shader. I tried transforming normal vectors to object space, rotating, normalising and connecting it to the masters normal input but this does not work for me.
    But i have the feeling i don't understand normals in shader graph in general because for example connecting a tangent-based normal vector node to the masters normal input gives wrong results comparing to just leaving the normal input empty (it says 'tangent' in this case)
     
  18. A132LW

    A132LW

    Joined:
    Jun 21, 2017
    Posts:
    33
    So I tried this and it seems to help; the tangents will need to be recalculated as well. bandicam2019-12-0414-18-00-2711.gif

    But using my normal recalculation or the DDX/DDY-cross product method does not remove the shadow artifacts.
     
    Last edited: Dec 4, 2019
    grllle likes this.
  19. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    I believe it comes down to an order of execution issue. The shadow/light information is set prior to the displacement. I'm not sure how to overcome that.
     
    grllle likes this.
  20. A132LW

    A132LW

    Joined:
    Jun 21, 2017
    Posts:
    33
    This is definitely a bug.
    in LWRP:-
    upload_2019-12-4_15-50-26.png
    upload_2019-12-4_15-50-50.png
    in HDRP(with Lit master):
    upload_2019-12-4_15-59-49.png
    in HDRP with PBR:
    upload_2019-12-4_16-0-50.png

    Unfortunately, I don't know either.
     
    grllle likes this.
  21. A132LW

    A132LW

    Joined:
    Jun 21, 2017
    Posts:
    33