I'm creating a Procedural Earth Shader, and I'd like to add city lights on the dark side of the Earth. I could just use a gradient texture, and map it to the dark side of the Earth, and use that as the factor for where the lights are. But I'd like to be able to rotate the Earth and the Sun Light, and have the city lights only show in the shadow area. Is there any way to do that with nodes? I'd really appreciate any help. Thanks!
In Cycles I used the following setup for my "Earth + Sun" model. First of all, I've got an Empty parented to my Earth object. The Empty has a Track To constraint, target is the sun object and Target Axis is -Z, so it's pointing away from the sun.
In the Earth material I use the Object output of a Texture Coordinate node with the Empty as object. A Separate XYZ gives the Z value. The "sunny side" of the Earth is equal or below zero and the shadow side is greater than zero. This I use as multiplier for the emission. Of course you can use a Color Ramp or other methods for sharper transitions or whatever. This is just to give the basic idea, you can always tweak here and there.
These are the objects:
This is the Z output I get from the Separate XYZ node as multiplier:
And my final node setup of the Earth material for this example (my real Earth model of course uses more complex textures etc):
Here are some examples how to tweak the transition from light to shadow. The above image of the Z output looks simply black on the one side, but since these are the object's texture coordinates the black part isn't simply 0 but goes from 0 in the middle to -1 on the outer edge. A Map Range node mapping the -1 to 1 range on a 0 to 1 range colored by a ramp shows this:
To make the transition sharper you can shrink down the range by changing the From Min and From Max values, e.g. to -0.1 and 0.1:
Or maybe you want to shift the city lights a little bit to the sunny side (people already turning on their lights as the daylight fades?), than you can do that by shifting the From Min and From Max values more to the negative side, like -0.3 and -0.1:
I guess you can imagine a lot more variations of manipulating this.