Search Unity

Question How do I bake my Shader Graph shader?

Discussion in 'Shader Graph' started by falsevac, Apr 2, 2020.

  1. falsevac

    falsevac

    Joined:
    Mar 16, 2016
    Posts:
    33
    I have a custom shader written in Shader Graph using a PBR and the LWRP (I'm on 2019.2 still).

    I bake my lightmaps, and the lightmaps themselves look correct I think, but my shader doesn't use them. Everythign is just black. Any ideas? I can't find anything in the documentation about this.

    Thanks.
     
  2. Elizabeth_LeGros

    Elizabeth_LeGros

    Unity Technologies

    Joined:
    Jan 7, 2020
    Posts:
    50
    Does this happen to be on a mesh that has "Generate Lightmap UVs" on import? We have an active bug where using the auto generated lightmap uvs doesnt work with shadergraph materials. You can try disabling that option and see if that fixes things.
     
    neptaco_ and falsevac like this.
  3. falsevac

    falsevac

    Joined:
    Mar 16, 2016
    Posts:
    33
    Hi, thanks for that suggestion. I tried disabling and using my own UVs, and that helped the "all black" problem.

    But, I'm still having trouble baking out my Shader Graph. I'll explain below. Any help would be very appreciated, thanks!

    I have a pretty simple "toon shader" that uses a Custom Function to get GetMainLight() and then draws a shadow based on attenuation. It then multiplies this with the mesh's vertex colors and puts it into PBR's emission value. All other PBR values are 0.0 or black. This produces the correct effect using realtime lights.

    Then, to bake, I switch my single Directional Light to "Baked", turn on Baked Global Illumination and set the mode to "Subtractive."

    * After baking, the lightmaps don't get used, because I'm using emission and not albedo. If I switch to albedo, then I do see lightmaps. I can also fix this by adding the "Backed GI" Shader Graph node and multiplying it with my emission value, just before I connect it to the PBR. So far so good, but…

    * The lightmaps don't look anything like my shader.

    * Do I need to define the meta pass somehow? I tried adding a custom function that passed the shadow color calculation into albedo if LIGHTWEIGHT_META_PASS_INCLUDED was defined. But that didn't help. I also just hooked up my shader to albedo instead of emission to see if that fixed the baking, but it didn't.

    So, I guess my question is: Is there any way to bake my stylized shadows into the lightmap using Shader Graph, so I can just use the lightmap texture directly?

    Or do I somehow need to pass the lightmap-texture samples I have now, through my shadow-node system? So, like, replacing the GetMainLight() part with the lightmap texture somehow?

    Screenshots of everything below. Thanks!

    REALTIME LIGHT

    Screen Shot 2020-04-02 at 3.30.05 PM.png

    BAKED LIGHT

    Screen Shot 2020-04-02 at 3.36.55 PM.png

    SHADER

    Screen Shot 2020-04-02 at 3.46.29 PM.png

    THE CUSTOM FUNCTION


    Code (CSharp):
    1. void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
    2. {
    3. #if SHADERGRAPH_PREVIEW
    4.    Direction = half3(0.5, 0.5, 0);
    5.    Color = 1;
    6.    DistanceAtten = 1;
    7.    ShadowAtten = 1;
    8. #else
    9. #if SHADOWS_SCREEN
    10.    half4 clipPos = TransformWorldToHClip(WorldPos);
    11.    half4 shadowCoord = ComputeScreenPos(clipPos);
    12. #else
    13.    half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    14. #endif
    15.    Light mainLight = GetMainLight(shadowCoord);
    16.    Direction = mainLight.direction;
    17.    Color = mainLight.color;
    18.    DistanceAtten = mainLight.distanceAttenuation;
    19.    ShadowAtten = mainLight.shadowAttenuation;
    20. #endif
    21. }
     
  4. Elizabeth_LeGros

    Elizabeth_LeGros

    Unity Technologies

    Joined:
    Jan 7, 2020
    Posts:
    50
    There's no way to apply shadergraph to the lighting system, but to be honest I am not sure that's what you want anyways. What are you hoping to achieve by having baked lights? If all your lighting is this stylized anyways, why not just fake it with your shader and have no lights at all? It seems like that solution would give you the look you want.
    But if you are dead set on using baked lightmaps, the main difference I am seeing is that the indirect light in your scene is canceling out all the stylized light in your scene. I would make nothing contribute to global illumination and then maybe just rely on environment gi or even passing in your own cubemap.
     
  5. falsevac

    falsevac

    Joined:
    Mar 16, 2016
    Posts:
    33
    Mainly because I need shadows from other objects. But, I could try and fake up something, I suppose. Thanks again for your help!
     
  6. stor314

    stor314

    Joined:
    Apr 26, 2019
    Posts:
    20
    Hi @falsevac, I know this post is almost a year old, I'm curious what route you ended up taking. I was going down the same rabbit hole you did trying to figure out how bake the shading from my custom shader graph into lightmaps, was trying to figure out how to manipulate the lightmaps in the shader and also add a metapass like you mentioned, glad I came across this post and saw @Elizabeth_LeGros 's answer before I spent too much time on that only for it to be fruitless.

    Did you end up faking all your lighting since you were unable to use lightmaps? Or was there something else you did to keep your stylized look without having to use a lot of realtime lighting?
     
  7. falsevac

    falsevac

    Joined:
    Mar 16, 2016
    Posts:
    33
  8. stor314

    stor314

    Joined:
    Apr 26, 2019
    Posts:
    20
    Thank you for the very quick response! The game looks great, I'll have to check it out. briefly, what other kind of optimizations did you make? I'm trying to make my game stylized yet still performant, not for mobile but for lower-end hardware.
     
  9. falsevac

    falsevac

    Joined:
    Mar 16, 2016
    Posts:
    33
    All sorts: optimizing the shader further, Occlusion Culling, model optimization, messing around with quality settings. There wasn't one trick unfortunately, and most optimizations are pretty game/scene specific.
     
  10. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    @falsevac Great job on the lighting! How did you manage to get such good shadows at such a high distance in URP? I have my main light shadow resolution set to the highest value (4096) and if I try to increase the max distance of the shadows so I can capture shadows casted by objects further away the shadow quality goes way down.
     
  11. falsevac

    falsevac

    Joined:
    Mar 16, 2016
    Posts:
    33
    @Vacummus A lot of the shadow shading is done in a custom shader (running light attenuation values through various passes to get a toon-like shadow effect), but there are also just straight-up shadows from the Lit Master node (this was originally done in LWRP) which just use cascading and hard outlines and fiddling with the normals to get the look.
     
    Vacummus likes this.
  12. StudioSquidInc

    StudioSquidInc

    Joined:
    Apr 13, 2021
    Posts:
    1
    you cant be serious in that reply thats kinda scary that an official standpoint of Unity is pfft why use baked lighting anyway....
     
    AllMightyNico likes this.