3D Skybox

This isn’t a planned post, I was just not able to work on my main project last night, so I’m dropping in a link to a set of 3D Skybox scripts I threw together in Unity last week for a test (my project needs some large planets in the sky). It’s nothing spectacular, nor novel, but it has its uses.

You can find it, and details here: https://github.com/digitaljelly/3D-Skybox

What is a 3D Skybox?

A 3D Skybox is a trick employed to add distant 3D objects into a skybox, without the objects needing to exist miles into the distance. It uses a second camera that follows the player in such a way as to make small, close objects appear much larger and further away.

Why is this helpful?

Well, for one, your scene objects get caught in a closer shadow cascade, so shadows are crisp – even if your object appears huge. You also don’t run into floating-point precision issues that you might normally hit if your objects are thousands of units into the distance, or thousands of units large.

How does (your implementation) work?

It essentially uses a second camera that follows MainCamera rotation exactly and the MainCamera position to a smaller scale (this scale can be set in the relevant component, so things can appear larger/more distant, or less so). The script handles setting everything up at runtime, so long as you provide it with the LayerMask that you’ve also set your skybox objects to be in.

That said, it doesn’t yet execute in Editor mode, so you’ll likely need to just configure things up like so:

The important parts are mainly the LayerMask setup and ClearFlags. You want your MainCamera to not render out the Skybox, because the new Skybox Camera handles that. You then want to be able to define what objects in your scene are skybox elements and which are not – use the LayerMask for that.

Finally, there’s a workaround script provided for if you’re using Unity’s built-in fog. As the distant objects distances to the camera are faked, the fog doesn’t work as expected. The workaround script allows you to tweak the fog density on the skybox camera.

Hopefully I’ll be able to post up my “main” project details later this week, that’s the plan.

-M

Leave a Reply

Your email address will not be published. Required fields are marked *