It is been a while that I am looking for a way to bake light I runtime. The last post I have seen dates from 2018 and it does not seem impossible but I would like to know if someone knows a package or something else.
To understand the need, I am looking for this because I am making a game on mobile with procedural dungeons generation. The problem is that a mobile is not enough powerful to calculate shading in real time. I have tested some situations but it is far from the performances obtained when lights are baked.
So the objective is to generate the dungeon, bake light and let the player enjoys.
Could be wrong about this but if device can’t handle real time lighting then how will it handle baking them in runtime without insanely long wait times?
You might be better off optimizing shaders and lighting or if you use pre fabricated parts to generate the dungeons include baked lights for each segment.
I don’t need the best quality for the baking so it is pretty fast to bake. But I know that is could take some times to bake for a phone.
The problem of working with prefabs and bake lights for each, is the seam when they are put near to each other. I saw many advise like make a shader to fade from one map to the other. But I have no idea of how am I supposed to do that.
You dont want to “bake at runtime” as that would be ridiculous. You are not going to use your users PC for baking.
Instead what you want is to bake lightmaps into prefabs. so you can spawn parts of the dungeon complete with performant lightmapping. There is no official solution to this, you will have to roll your own.
That is exactly what I am looking for but I guess, I could be able to put rooms near to each other physically, I will need to create a scene for each one due to the fact that if I bake the lights for each prefab, the seams between one prefabt to another will be visible which I don’t want.
Yes you have to bake each prefab in its own scene for this workflow to work. You have to ask yourself if its really worthit.
An alternative is to bake lighting into the textures themselves in something like blender, which may have a slightly nicer workflow for something like this depending how you go about the task
Also your point about “mobile is not powerful enough to calculate shading in realtime” is incorrect. I work as CTO for a danish app company and we make realtime 3D content for mobiles that performs at 60FPS and uses realtime lighting, PBR etc with little effort - its actually not hard to optimize realtime for mobile (assuming the mobile phone is made within the last 5-6 years).
So as long as you optimize it should run absolutely fine on mobile.
I know that a unity package allows to bake lights for prefabs. So it is not necessary to create a scene for each room, just to create a scene and switch from one prefab to another with the lights baked in it. It could work I guess.
I would be very interested in what you are talking about the shading in real time at 60 fpson mobile. I did some tests to understand how different light mode baking work in unity (Shadow mask, Substractive …) and I also tried without any bake, and it was hard to reach the 60 fps even with my phone (Galaxy S22+). So if you can give me some advides to make it works. I would be very happy to read you.
If you need some picture of my scene and the stats, I can provide you them too.
There is no official unity package that bakes lights for prefabs, so whatever you are talking about is 3rd party. Something to be aware of - I tend to steer clear of anything 3rd party unless I am 100% sure it works without any issue, and that I will never need support. Otherwise its a risky dependency to add to a project.
Regarding achieving 60fps on mobile with realtime lighting - its impossible to tell without information.
What render pipeline are you using?
What is the geometry in the scene you are trying to render?
Have you used occlusion culling?
Have you setup reasonable near and far clip planes so that you are not rendering stuff 1000 units away from the camera?
What are your materials, are you using just a color texture, or do you have additionals such as normal maps and other maps?
What are the compression settings and resolution of said textures?
Do you have mipmaps enabled?
Are you using physics, and if so how many active rigidbodies?
Are you running any code - if you did, did you actually run the profiler to determine what is actually eating up the ms per frame?
Ultimately if you are not getting 60fps, thats due to your optimization + usage of the engine - not the engine itself. It is absolutely more than possible to get 60fps with really good looking graphics on mobile phones, if you are not doing things incorrectly.
Optimization is a complex topic and it could take a long time to achieve decent performance depending what you are doing and assets you are using. You have to run the profiler and analyse it , learning to use the profiler is the only way to achieve good performance as nobody else has access to your project (nor would they be willing to profile it for you even if they did )
I know that officially, nothing exists to perform lights baking on prefabs and what I have found is a third party, correct. But I have tried it and it seems to work for what I want to do.
What render pipeline are you using?
I am using the URP. What is the geometry in the scene you are trying to render?
Here is a quick look of the scene. Do not take care of the FPS; I am working on my laptop in eco mode.
Have you used occlusion culling?
I am using it but can tell why, it seems not working for now. But for the purpose of this scene, it is not matter since the entire scene can be handle by the camera. Have you setup reasonable near and far clip planes so that you are not rendering stuff 1000 units away from the camera?
The only thing visible is what you are seeing. Nothing far from the camera. What are your materials, are you using just a color texture, or do you have additionals such as normal maps and other maps?
I am using 1024x1024 texture with Albedo, Normals, Ambient Occlusion ans Metallic. What are the compression settings and resolution of said textures?
I did not change the compression for them:
Do you have mipmaps enabled?
I guess yes but not sure. I thought it was enabled by default. How can I check it ? Are you using physics, and if so how many active rigidbodies?
Only two rigid bodies in this scene which are my characters. Everything is static. Are you running any code - if you did, did you actually run the profiler to determine what is actually eating up the ms per frame?
Yes my code is running in fixedUpdate for the rigidbody (character movement) or else, nothing more.
Thats not what I mean. If you dont set the near and far clip plane to a reasonable range, its doing far more work than necessary. This is a trick used in a lot of XR applications for example to improve framerate easily.
You want your near and far distance to be as close together as possible without breaking your rendering. So experiment with editing those 2 values until you have them at a close range (something like 0.3 as Near and 100 as far, if possible)
Okay that is quite a few maps per material to be using with no compression.
For starters make sure to compress your textures to as small as they can go without losing too much visual fidelity. Secondly make sure mipmaps are turned on. I wont go into detail about how to do htis as both of these things are unity basics and googling " unity mipmap settings" or “unity texture compression settings” will yield many results including 100s from this forum.
Thirdly, check what contribution your metallic and ambient occlusion are really doing, vs using a flat value. remove those maps from each material and instead set a general value using the slider - if this doesnt destroy visual fidelity too much this will greatly improve performance by removing 2 texture samples per object being drawn.
Also when you say “everything is static” I hope you dont mean the rigidbodies too? As that would cause serious performance issues.
It looks like the core of your problem is your code (which is usually where people lose frame ms) - so we will need to see full images of the profiler to really give more targeted help
Everything is pointing to your project being very unoptmized though, based on the stats you are reporting (thats very low geometry and even with those textures etc, to get such low performance really must be code related). The fact that you have 82ms on CPU and only 9ms on render thread really points to this being code and/or physics related This is good news as it means something can be done about it without making things look “worse”
I think bakery might be able to do that, though I’m not sure. Check its specification.
Easily. If it can’t light the environment in realtime, it likely will be still able to light up the whole level over course of several seconds. Then you write it into a texture and here you go.
This has been done before in Cube2/Saurbraten. Basically that game was centered around octree-based level editing, and came with a builtin lightmapper. The lightmapper took something like a second to build the lighting, then you’d have realtime performance.
Similar thing was done in sims, which definitely cached some of the lighting.
Basically as long as you don’t try to calculate GI, and stick to simpler lighting, this sort of approach is perfectly doable.
I don’t know if it is good but I am going to the way of not using baking of at least, not completely.
I want to generate dungeons procedurally and even further, rooms too. I mean by that, a dungeon if composed of many rooms and a room is composed of many assets placed randomly in it.
Valves the lab renderer basicly was a realtime lightmap solution. It haven’t been updated in ages and probably won’t run on newer versions of Unity but it might give ideas
I made a 100% real-time ingame lightmapper! RLB - A progressive GPU ‘Runtime Lightmap Baker’ - No RTX, All platforms, no compute shaders needed, even runs on Mobile! You can bake scenes in seconds and have it refined in the background, running on all GPU’s shadermodel 3.0 and up!