Manipulate Directional Light with it's own deticated shader?

I’ve been looking around at Day and Night Cycles in Unity and they all seem very intuitive. I got one working using C# to rotate my Sun and Moon directional lights around nicely giving a solid impression. It still needs work but I figured something…

Anything in C# is executing on the CPU side, and since it’s the light I want to animate I figure I should give this job to the GPU. Why can’t I just make a shader to animate the vertices of my directional lights? I know I was able to do this in just a raw OpenGL/C++ program I wrote some time ago, but is this possible in Unity?

A directional light doesn’t have vertices, it doesn’t even have a shader.*

It exists as a simplified abstraction of a light that helps control all of the elements that go into adding directional lighting to shaders. In its simplest form a directional light is just a vector direction property on an object’s shader. That value just happens to be getting set to the same value for all lit shaders in the scene. Additionally that direction together with the camera position and orientation are used for setting up rendering shadow maps which has to happen on the CPU, so even if you were able to rotate the light in a shader the shadows wouldn’t match.

  • In deferred lighting it could be argued that lights have their own shaders, however I would say it is more accurate to say the application of lighting has a shader and the light object itself is still an abstract thing that happens to be driving a separate shader which happens to only do lighting.

I see, thank you for your response. So any animation to a light would just have to happen in C# script then.

GPU vs CPU calculation really makes my head spin because I always ask “Is this right? Should I do this job here or there?”

Just do everything on cpu until it’s time to optimise. Chances are you’ll find some things will become very obvious where they don’t belong.

1 Like