Unity Lights source code & runtime 'baking' shadows

After seeing my game’s performance drop immensely when using dynamic shadows, I realized that all my lights were re-drawing their shadow maps despite 98% of what they’re lighting is static, procedural meshes. However, there isn’t an option to manually re-draw the shadow maps from scripts with built-in lights, and it seems the source code is not to be found anywhere.

So I went to search on the Internets for a custom implementation of unity lights. And the closest thing I found was the command buffer showcase project from unity itself, which implements custom point lights for deferred rendering, but it lacks shadowing support and other things like culling.

It would be very useful for many procedural games to only update certain lights from script when needed, and leave them ‘static’ otherwise, effectively having the cheap performance of a deferred light.

Has anybody tried adding shadows into that project, or created/found something similar?

PS: Think of it as the reflection probe’s refresh mode option, but for point/spot/directional shadow casting lights

3 Likes

That’s actually… pretty great idea! I don’t think there’s such implemented or if it will be, when.

This would actually be really useful for me to.

Well then, after some development, I present to you, results:

Close: Unity3D lights: 212.9 fps
Close: Custom dyna: 146.9 fps
Close: Custom static: 355.4 fps

Far: Unity3D lights: 342.6 fps
Far: Custom dyna: 358.6 fps
Far: Custom static: 905.5 fps

See [Custom lighting in unity3d - Album on Imgur](http:// Custom lighting in unity3d - Album on Imgur) for pictures to verify there’s virtually no visual difference.

Tested at ‘very high resolution’ from unity, and 1024x1024 cubemaps for my custom lights.

Currently missing:
Receive/cast shadow option on renderers does nothing, but layers work.
Probably some fancy optimizations unity does that I don’t know.
Support for forward rendering
Support for non-HDR camera
Doesn’t show up for the scene view camera in the editor. (Can’t find that camera from script reliably?)
Soft shadows
Dynamic lighting function (?), it’s coded into the shader.

Currently has:
Simple viewport occlusion
Dynamic level of detail (low res in distance)
Intensity settings
Color settings
Shadow bias settings
Resolution settings (~64x64 → 8192x8192 which wrecks any gpu)
Shadow toggle option
‘Dynamic’ toggle
Manual update shadows from script

My guess is that my level of detail is a bit more aggressive than unity’s, resulting in the far fps being higher, but visually it seems pretty identical.

It’s rendering with a camera using a depthshader into a cubemap rendertexture, than rendering spheres scaled to the range of the point lights before image effects using the new command buffers. Compare depth from camera to depth from cubemap to see if shadow or not, etc.

Test scene doesn’t use dynamic batching to make it a bit more accurate (Most lights wouldn’t light one object), as I didn’t have a proper test scene ready. 10 point lights (with tiny sphere meshes), 48 cubes.

1 Like

Oh, the thread was moved from general. I will post it in the wip section soon.