spenibus.net
2014-04-13 22:06:27 GMT

A simple day night cycle, part 7 - Visibility, scale and trickery, also the end.

Step 12: cameras and scale trick -- As explained earlier, our objects are part of another layer invisible to the player camera. You may wonder why we did that, and the answer is simple. If we had put the sun and the moon directly in our world to be seen by the camera player, we would have needed to make them to scale. Huge spheres thousands of kilometres away. Which would render them invisble too unless we set our camera to have a near infinite rendering distance. Then you would perhaps suggest to simply reduce the scale to give the illusion of far away objects. And this is exactly what we do but there is a small catch. While the objects appear to be properly scaled, they are in reality closer than they should and when we move around the world, we notice they are simple objects hanging in the sky not so far away. Remember the camera in our system hierarchy ? This is where we get to use it. First, we setup our system camera as follows: ```` Clear Flags: Skybox Cullink Mask: dayNightCycle Depth: -1 ```` "dayNightCycle" is the layer to which all objects in our system are assigned. This camera will show nothing else than the sun, the moon and the skybox. The "Depth" parameter is used to make the system camera appear behind the player camera. Then we setup our player camera as follows: ```` Clear Flags: Depth only Cullink Mask: "Everything" except "dayNightCycle" Depth: 0 ```` The player camera will show everything except our system's objects. It will also stop rendering anything past the geometry. This leaves a transparent background where our system camera will appear and add the sun, the moon and the skybox. We setup the sun as follows (x,y,z): ```` position: 0 0 20 rotation: 0 180 0 scale: 1 1 1 ```` This is considered to be the position of the sun at midnight (although we will add a 2 hours offset for our 14:00 zenith). We setup the moon as follows (x,y,z): ```` position: 15 40 25 rotation: 45 315 0 scale: 1 1 1 ```` I will state the obvious here but putting the moon further away will make it look smaller than the sun. And since the moon is physically in front of the skybox and would appear before the clouds rather than being obscured by them as it should, we chose a position where the sky is clear. And to create the illusion, we add this in "Update()": ```` // sync fov cam.camera.fieldOfView = playerCamera.camera.fieldOfView; // rotate local camera with player camera cam.rotation = playerCamera.rotation; ```` First, we synchronise the field of view. If you have no intention to ever change it ingame, you can do this in "Start()". Then we synchronise the rotation of the cameras. This makes the world geometry, the sun, the moon and the skybox rotate in unison. And that is all we do. We do not synchronise any other movement as it would defeat the entire point. The reason we use another camera is precisely to remove player movement. The following video will illustrate what we just did: <iframe width="560" height="315" src="https://www.youtube.com/embed/xFxtuF7N7nk?rel=0" frameborder="0" allowfullscreen></iframe> ><https://www.youtube.com/watch?v=xFxtuF7N7nk> And another youtube video to illustrate the system under multiple perspectives: <iframe width="560" height="315" src="https://www.youtube.com/embed/WOud8Cr97Ck?rel=0" frameborder="0" allowfullscreen></iframe> ><https://www.youtube.com/watch?v=WOud8Cr97Ck> And we are done. This is a basic day night cycle. While it leaves the door open for a lot of optimisation and improvement, both artistic and technical, it works and adds to the atmosphere and credibility of the world we attempt to build. End of part 7, end of the series.