Wednesday, 6 May 2015

SKYYYYYYY BOXXXXXX

Worlds need a sky and skies need a world. Our world didn't really have sky. So I set out on a quest to make sky. And I don't just mean any old static sky. I mean, moving clouds, dynamically updating sun kinda sky.

At first I thought about going for a sky dome. As in, cut a sphere in half and texture the inside of it with sky and clouds, that way I could rotate the sphere to mimic movement. However, I didn't like the limitation of having to position and scale a half dome around your level each time to set up the sky. Dragging one material to one slot is so much easier. I even thought about placing the dome away from the level, place a camera inside of it and update the cameras rotation based on the main camera rotation. Then render this camera instead of the skybox. This approach also seemed like too much hassle to set up for a sky.

So I set myself a challenge, to make a dynamic skybox using Shaderforge

And here it is:



Big ol' shader. Well, kinda. The sections on the left are all the same thing. They rotate a cube map by a parameter set on the material. There are 6 sections because there is 6 layers of clouds.

This is what a section looks like.


As well as the cloud layers rotating, I also wanted the position of the sun to change depending on the rotation of a directional light you set as the sun light. To achieve this I had to take a different approach to rotating, I couldn't use the previous way of rotation as this was only on one axis. The sun's rotation is on all 3 axis'. To overcome this, I made a function that calculates the direction for the sun. The sun is just a cubemap that gets rotated just like the clouds, apart from all 3 axis' not one.




Along with the background coloutr, all these layers are rotated accordingly then blended toegether to produce the final skybox, which looks something like this:


There is one current issue with the sky. Because of the way I've laid out the maps. (one long panoramic), Unity converts this into a cubemap on import. This distorts the top of the image which looks like this:


It isn't  the be all end all but obviously it cannot be a permanent thing. The way around this is to pre distort the images. Then when Unity generates the cubemap, it will effectively un distort them. The sun layer isnt affected because the sun is in the center of its cubemap, then the whole cubemap is rotated to update the position of the sun. Eventually, you'll be able to fade in and out the layers of clouds to simulate weather conditions. Another note is that the sky gets darker the lower the sun is and the sky colour is not dynamically changeable... yet. Just havent got the time to put it in :(

0 comments:

Post a Comment