how to make the sun light behave like the real sun in 3d

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By snarkmultimedia
:warning: Old Version Published before Godot 3 was released.

I new on godot and 3d games, I’m trying to make the sun light behave like the real sun in 3d scean, from sunrise to sunset, changing the color and the position of the light. is propable add some script to the sun light object.if someone can point me in the right direction it will be fantastic.

Could you maybe show us what you’ve tried, to give us a better idea of what you’re trying to do?

keke | 2017-02-19 20:01

There could be many ways, moving a skybox, rotating a light, modifying environment, using shaders.

As keke said, try to expand a bit more on details of your game, some captures, some game with a similar effect.

eons | 2017-02-20 18:57

one example I find was this. https://www.youtube.com/watch?v=2w9-laN4MuE
there was other where you can see the sun, I’m working on the 3d assets, it will be a dating game, and i want to simulate the day and night time

snarkmultimedia | 2017-02-20 23:18

Ok, thanks! I’ll see what I can do. I only have experience in 2D godot, so this will be a learning experience for me too…

keke | 2017-02-21 03:47

:bust_in_silhouette: Reply From: snarkmultimedia

one example I find was this. https://www.youtube.com/watch?v=2w9-laN4MuE
there was other where you can see the sun, I’m working on the 3d assets, it will be a dating game, and i want to simulate the day and night time

:bust_in_silhouette: Reply From: keke

Here’s a download of an example I threw together: day_night_example_for_snarkmultimedia

EDIT: Here’s the same thing, but using an AnimationPlayer instead of a script, as suggested by Warlaan: day_night_example_for_snarkmultimedia(animation_player)

Here’s the script:

extends Position3D

#change these!
var speed=.2
const change_color_range=PI/8

onready var white_sun=get_node("white_sun")
onready var yellow_sun=get_node("yellow_sun")


func _ready():
	set_process(true)

func _process(delta):
	#get the current rotation, and move it a bit...
	var new_rotation=get_rotation()
	new_rotation.z+=(PI*speed)*delta
	
	#make sure it's in the range 0 to 2PI...
	if(new_rotation.z>2*PI):
		new_rotation.z-=2*PI
	elif(new_rotation.z<0):
		new_rotation.z+=2*PI
	
	#set it...
	set_rotation(new_rotation)
	
	#Now we turn one, the other, or both suns off.
	if(new_rotation.z>2*PI-change_color_range):
		yellow_sun.set_hidden(false)
		white_sun.set_hidden(true)
	elif(new_rotation.z>PI and new_rotation.z<PI+change_color_range):
		yellow_sun.set_hidden(false)
		white_sun.set_hidden(true)
	elif(new_rotation.z<PI):
		yellow_sun.set_hidden(true)
		white_sun.set_hidden(true)
	else:
		yellow_sun.set_hidden(true)
		white_sun.set_hidden(false)

It’s just a Position3D that rotates with two directional light children that turn themselves on/off depending on the angle.

So at low angles, the yellow sunrise/set light turns on, and the white light turns off. At high angles, only the white light is on. Both are off when the sun is below the horizon.

I hope this helps!

keke | 2017-02-21 06:08

You should definitely use an animation player for that, you can replace the whole script with one animation.
Also you can animate the light color instead of switching two light sources on and off.

Warlaan | 2017-02-21 09:10

Man so many thanks for the example, It was very kind of you. My experience is with java and libgdx for 2d games, i’m using godot for my first 3d project and I still do not know much. If there is nything that can help you with, just say.

snarkmultimedia | 2017-02-21 11:36

Well, snarkmultimedia did ask for a script, and a script does have some advantages - like easily changing the angle the sun turns yellow at during runtime.

(But yeah I probably should have just used an animationplayer :P)

keke | 2017-02-22 03:44