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!

• Maybe the second half of this answer? May 17, 2021 at 6:10
• I know this wasn't part of your question, just to let you know: my solution is based on a Sun object with a Track To constraint pointing at Earth, since sun lamps are infinite lights always only pointing in one direction. I did this to avoid using a point lamp but still have the sun always shine on Earth no matter where one of the objects is. Because the Shader to RGB node doesn't work in Cycles there is no way to directly determine which side is lit. May 17, 2021 at 13:00

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.

• Thank you! Just one question, when I move the sun, it doesn't rotate automatically to be pointing at the earth like your file does. How can I do that? May 17, 2021 at 17:49
• Never mind, I fixed it! I forgot to add the constraint to the Sun object. Thanks so much for your help! May 17, 2021 at 17:55
• @RyanKingArt My mistake, I should have put that in the answer... maybe I'll edit it later. Actually when I first did this a while ago I wanted to use the sun's texture coordinates instead of adding an extra empty, but I couldn't get this to work. So I guess there's a new tutorial coming? ;) May 17, 2021 at 17:58