A starry sky dome

What if the Universe was just a giant ball and we're inside it? That's what we're going to imagine to implement a starry sky spherical background.

In computer graphics, you can create backgrounds to make the scene look bigger than it really is. You can use a spherical texture, or skydome, as we will use here. (A common alternative in many game engines is a cuboid skybox, constructed from six internal faces of a cube.)

Among the set of textures that we provided with this book is milky_way_tex.png. Drag a copy of this file into your res/drawable/ directory, if it's not there already.

Now, we can add the starry sky dome to our scene. Add the following code to MainActivity.setup():

        //Stars in the sky
        Transform stars = new Transform()
                .setParent(RenderBox.mainCamera.transform, false)
                .setLocalScale(Camera.Z_FAR * 0.99f, Camera.Z_FAR * 0.99f, Camera.Z_FAR * 0.99f)
                .addComponent(new Sphere(R.drawable.milky_way_tex, false));

This looks so much more celestial.

A starry sky dome

You might be wondering what that 0.99 factor is all about. Different GPUs deal with floating point numbers differently. While some might render a vertex at the draw distance one way, others might exhibit render glitches when the geometry is "on the edge" due to a floating point precision. In this case, we just pull the skybox toward the camera by an arbitrarily small factor. It is especially important in VR that the skybox be as far away as possible, so that it is not drawn with parallax. The fact that the skybox is in the same exact place for the left and right eye is what tricks your brain into thinking that it's infinitely far away. You may find that you need to tweak this factor to avoid holes in the skybox.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset