HomeAbout inludoServicesGamesClientsDevelopmentsForumContact usHomeMail inludo


Skybox:


Download the source here (2.4 meg - sorry, it's those damn graphics!).

Intro:
Games and programs in 3D often want more than a static background, but one that moves and rotates with the camera. This sort of setup means you can't use Diector's inbuilt backdrop functions (which to be honest together with overlays are pretty rubbish and very basic).
There are many solutions, of which a skysphere is one. I did a tutorial for this, and eventually realised what an idiot I've been. So I have finally switched to a skybox. 'Why?' You may ask. And after I've finished sneering and looking superior I'll tell you why. Firstly, it's easier to create. Secondly, it doesn't get distorted as a skysphere does at the top and bottom. Thirdly, and perhaps most importantly, it's an absolute doddle to create the images for. You set up a 3D world and render 6 views, each at 90 degrees to the others, and with a field of view of 90 degrees. I am personally getting into Terragen to create landscapes and IT ROCKS.

Method:
You set up your skybox using my parent script.
You choose what textures you want in which orientation on ecah face of the sky box.
You choose a size to create it at (can be omitted, if so it uses a 'size' of 1.0 - see below).
Each frame or movement of the camera you re-position the cube at the same place as the camera.
And that's it. Tricky eh?

In Practice:
To succesfully use my code you need to use the following code::

  • Initialising the skybox object script.
    For this you create a new instance of the "[P] Skybox" script, with one of the following lines of code:

    tSkyBoxObj = script("[P] Skybox").new(tWorld, tName)
    tSkyBoxObj = script("[P] Skybox").new(tWorld, tName, tCamera)

    where tWorld is the name of the 3D member you wish to create the skybox object in.
    tName is a name to assign to this skybox. If you wish to create more skyboxes, they must each have a unique name.
    tCamera is the world number of the camera that the skybox will attach itself to. If omitted it defaults to 1 (the first camera).
  • Assign textures to each face.
    You do this by calling one of the following lines of code six times, once for each face:

    tSkyBoxObj.SetFace(tFace, tMember)
    tSkyBoxObj.SetFace(tFace, tMember, tOrientationCommand)
    tSkyBoxObj.SetFace(tFace, tMember, tlOrientationCommands)

    where tFace is a symbol indicating which face you wish to set. It must be one of the following:
    #front
    #back
    #left
    #right
    #up
    #down
    tMember is the member name or member reference of the bitmap member you wish to use to texture this face of the sky box with.
    tOrientationCommand is an optional extra that defines an operation to be performed to the texture. These are used to align and orientate the textures should they not be created in the correct fashion. Options are:
    #fliphorizontal - Flips the texture horizontally
    #flipvertical - Flips the texture vertically
    #rotatecw - Rotates the texture clockwise
    #rotateccw - Rotates the texture counter-clockwise
    tlOrientationCommands is a list of commands identical to those detailed above. Use this for instance if you need to rotate your texture 180 degrees to align it, eg [#rotatecw, #rotatecw]
  • Setting the size of the skybox.
    Do this using the following code:

    tSkyBoxObj.SetSize(tSize)

    where tSize is the size you wish to create the skybox at. Note this is measured in world units, and represents HALF the overall size of the skybox in each axis. IE if you call it with a tSize value of 10.0 you end up with a skybox that is 20.0 units wide, tall and deep.
  • Creating the skybox.
    Once you have assigned textures to each face and optionally set a size, you need to actually build the model. Do this with the following code:

    tSkyBoxObj.Build()

  • Updating the skysphere's position each frame.
    Couldn't be simpler. Just use the following line of code:

    tSkyBoxObj.Update()

    where tSkyBoxObj is the object reference returned when you created the skybox object.
    This simply moves the skybox so it's center is exactly where the chosen camera is. Effect complete!
  • Deleting the skybox when finished with.
    If you wish to delete all resources (textures, shaders, modelresource and model) created and used by the skybox, use the following code:

    tSkyBoxObj.Destroy()

    where tSkyBoxObj is the object reference returned when you created the skybox script instance.
    Note use this with care. If another model or object shares any of the resources created (unlikely with the modelresource, model, or shaders, but possibly with the textures) then this will delete them regardless. If you are using shared resources then manually delete them yourself.

    Click here to see a demo of the SkyBox


Development
Screenshots
Demos
Tutorials