How do I bake my Shader Graph shader?

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.

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.

2 Likes

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

BAKED LIGHT


SHADER

THE CUSTOM FUNCTION

void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
{
#if SHADERGRAPH_PREVIEW
   Direction = half3(0.5, 0.5, 0);
   Color = 1;
   DistanceAtten = 1;
   ShadowAtten = 1;
#else
#if SHADOWS_SCREEN
   half4 clipPos = TransformWorldToHClip(WorldPos);
   half4 shadowCoord = ComputeScreenPos(clipPos);
#else
   half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
#endif
   Light mainLight = GetMainLight(shadowCoord);
   Direction = mainLight.direction;
   Color = mainLight.color;
   DistanceAtten = mainLight.distanceAttenuation;
   ShadowAtten = mainLight.shadowAttenuation;
#endif
}
1 Like

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.

Mainly because I need shadows from other objects. But, I could try and fake up something, I suppose. Thanks again for your help!

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?

Hey @stor314 , I ended up going all real-time lighting, making optimizations elsewhere so it still performed well on mobile. It ended up working pretty well. You can see the final result here: Prologue: “Four Months Earlier” — Linda & Joan

3 Likes

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.

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.

@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.

@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.

1 Like

you cant be serious in that reply thats kinda scary that an official standpoint of Unity is pfft why use baked lighting anyway…

2 Likes