[Unity 3D Tutorial] Procedural Sun and Stars - Part 1 of 2

Опубликовано: 30 Сентябрь 2024
на канале: Re: cOg Mission
4,998
111

Welcome to my first Unity tutorial! In this two-part video we will develop the day-night cycle of my introductory video. Firstly, we will create a sun that passes through the sky on a tilted orbit, adjusting the intensity and fog according to time of day. Secondly, we will create a night sky that allows for milky-way-type clustered bands as well as size and colour variations, twinkling and fading per individual star, based on distance from the horizon or dawn and dusk.

Introductory Unity video:    • Unity Journey of Discovery: Hello Wor...  
Sun, Earth and Moon introduction:    • Sun, Earth and Moon: Introduction and...  
Sun and Earth:    • The Dance of the Sun and the Earth  
Earth and Moon:    • The Dance of the Earth and the Moon  

---*----* My Playlists *----*---
Unity Journey Of Discovery (Tutorials):    • Unity Journey of Discovery: Hello Wor...  
The Main Course (Maths and Science Concepts Explained Visually:    • Sun, Earth and Moon: Introduction and...  
Eco-Pool Natural Swimming Pool (Build and Progress):    • Green/Eco/Natural Pool, Part 1: Plann...  
4K Treats (Ultra HD content):    • Marine Life Photos Part 1 4K UHD  
Finger Snacks (Shorter, Unclassified Videos):    • Ribbon Eel (Rhinomuraena Quaesita) Sw...  
*----*----*----*----*----*----*----*

Background music: www.bensound.com

Hello and welcome to my first Unity tutorial! In this two-part video we will develop the day-night cycle of this scene. Firstly, we will create a sun that passes through the sky on a tilted orbit, adjusting the intensity and fog according to time of day. Secondly, we will create a night sky that allows for milky-way-type clustered bands as well as size and colour variations, twinkling and fading per individual star, based on distance from the horizon or dawn and dusk.
We start off with an empty scene containing just a camera and directional light. I’m going to add some existing scripts to the camera. The first one allows us to let the camera always follow a certain object and always look towards another – we’ll maybe use that a bit later. The second one is a fly-cam script that allows us to move the camera around and look in different directions. For now, I’ve fixed the movement to only allow looking straight up and down, and I’ll set the limits here from 0 for the front to 180 for the back. You’ll see in a moment what I mean. The script allows you to specify camera movements at specific times to simulate a fly-through, but we won’t use that here, so I’ll just switch it off. And that should be it for the camera.
For the directional light, let’s reduce the shadow strength and set the Render Mode to Important. The rest look fine for now. I’m adding a simple default terrain of 1000 by 1000 units and centring it at 0. Let’s also centre the camera, although we want it slightly above the terrain on the y axis. The position of a directional light doesn’t play a role, so you can place it wherever you like, I have it centred as well, or you can also place it somewhere out of the way. The rotation is what drives directional lights, and I’m setting it to shine from directly above. So, we should be able to see our camera and light in our scene view at 0 - let’s increase the Gizmos slightly, and there they are.
Now, to orientate ourselves while the scene is running, let’s add some objects as pointers or beacons. Let’s place them inside an empty game object, which we also position at 0. I’m placing two cylinders front-left and front-right, and two spheres top-left and top-right. Any material will do for the spheres, but I do want them to emit light, so they’ll be visible at night when the scene is dark.
If we run the scene now, we’ll see our front beacons, and looking up with the camera we’ll see the top beacons, with the sun directly in-between, since we’ve set the direction of the sun as straight-down. We can also keep looking up and backwards until we see the horizon behind us, upside down. I’m going to keep it like this for now, since it makes following the rotation of the sun and stars seamless. Let’s just move the camera a bit further from the ground.
OK, let’s start coding. Maybe just rename our light to sun. I’m creating another empty game object to add the scripts to, just to keep it together, let’s call it controllers. We’re going to create a new C# script, call it DayNightController, and attached it to the game object.
We start off with a type Transform for the sun, which we’ll use to adjust the directional light, and a type Light for the sun, which we’ll adjust to obtain different effects like dawn, mid-day and dusk light. Next, we need an input for time of day when the scene starts. One way to do this is to work just in seconds, for example seconds passed since midnight. This aligns to the way Unity measures time...