Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

3d Skybox sample project

Discussion in 'General Graphics' started by MV10, Dec 17, 2015.

  1. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    All of the 3D skybox examples I could find for Unity were either very old or didn't include working code. I decided to build one to share. Later I will do a short writeup of what settings are required to make this work. I did use the graphics from a free skybox and planet asset from the asset store. I will give appropriate credit later in the full writeup.

    I couldn't upload the ZIP to the forum (too large at 52mb?) so I posted it here:

    http://jmp.sh/FjnqSvv

    Hopefully this will be useful to anyone wishing to explore this technique.





     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      49.7 KB
      Views:
      544
    • 1.jpg
      1.jpg
      File size:
      49.7 KB
      Views:
      548
    • 1.jpg
      1.jpg
      File size:
      49.7 KB
      Views:
      536
    Last edited: Dec 18, 2015
  2. Hormic

    Hormic

    Joined:
    Aug 12, 2014
    Posts:
    250
    Thank you for your demoscene, i'm interested in your example and how it works,
    But little explanation of what is going on would be very useful, so will you still come up with some explanation?
     
  3. smd863

    smd863

    Joined:
    Jan 26, 2014
    Posts:
    293
    It's essentially to get around floating point precision limitations; you can't have an object the size of a planet and an object the size of a house in the same scene. Your near and far distance planes will be so far apart that your depth buffer won't be able to correctly z-order nearby geometry, and your distant objects will run out of precision to correctly align all the vertices (just put any object at a position of "1e+07" in the Unity editor and you will see what happens).

    A 3D skybox is just a separate camera that renders background geometry at some scale factor larger than your main camera, and its movement will mirror the movement of your main camera at a smaller scale. Any geometry that is big enough and far away enough to not have any noticeable parallax for the player you can simply render into a regular skybox texture--it is effectively at "infinite" distance.

    Your main camera renders the foreground geometry, and if you set it up properly, you should get the illusion that your near geometry seamlessly blends in with distance objects like a huge mountain range or city.

    In this example, the green box is the foreground geometry and the Earth is the background geometry. Moving around the box would give you a little bit of parallax on the Earth that would give you the perception that it is very large and very far away when it is really just a regular-sized sphere rendered with a different camera.
     
    Hormic and MV10 like this.
  4. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Thanks Captain Science :) ... forgot all about this. I actually wrote a short article on this but then my wife's blog crashed and neither of us have had the time to mess with it. Sooner or later she's bound to hassle me until I post it...

    To expand on the explanation above, you need at least two layers -- your main camera scene and the skybox content. The Clear Flags on the cameras are set to Depth Only, which tells Unity that multiple cameras will be used to render the output. The camera with the lowest Depth clears the screen buffer before rendering, and higher Depth cameras just render on top of whatever the earlier cameras already output. Then you set the cameras' Culling Masks to only show the layers associated with the camera. Finally you script the skybox camera so that it tracks the motion of the main camera and that covers all the really important stuff.

    In practice you'll probably also have to change the layers used by the lights (and it's often possible to set your skybox textures to unlit), you may have to tweak the skybox camera's Near Clipping Plane (in an example like mine the skybox is very tiny), and you should keep the camera sizes the same, otherwise you'll get distortion.

    You can also achieve other cool effects by layering several cameras. I have one project that uses three background layers before the main camera is overlaid.
     
    Hormic likes this.
  5. Hormic

    Hormic

    Joined:
    Aug 12, 2014
    Posts:
    250
    Thank you both for answering. I played a bit with the scene and it is a very interesting concept, which i wasn't aware of,
    don't know if i will come in handy, cause it is more complicated to setup, so i will try to expand the scene with more background layers and see what i can achive.
     
  6. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    I was new to Unity when I threw it together, it was a very useful learning experience.

    You could also do something like apply slow random rotation to the skybox camera, which gives the impression that the player terrain is moving (tumbling through space, for example). I gather Half-Life was the first game to use this technique to create a feeling of depth in the background city skylines. There are also many well-known games that use this technique to render something Very Gigantic that actually interacts with the player. Probably the Bullet Storm wheel-of-death is one of the more well-known variants.

    Once you know how it works, it's funny to look at an image like this and realize they're composed of separate little chunks of scenery.

     
    Hormic likes this.
  7. Hormic

    Hormic

    Joined:
    Aug 12, 2014
    Posts:
    250
    Is it possible to spin the skybox camera, when you have to sync this camera with the main camera?
    But isn't this efect in your example with the random spin script on the skybox quads Gameobject,
    so the skybox is already rotating in the background in your example like you mentioned, i guess.
     
  8. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    No, sorry, I should have clarified. You'd either sync it or spin it, not both. I haven't looked at the original project that I uploaded in awhile, I forgot what I had done in that specific one. (I made a lot of small changes for the blog article.)
     
    Hormic likes this.
unityunity