# NeHe Lesson 23 – Sphere Mapping

Spherical Mapping

###### Sphere Mapping

This post on sphere mapping is part of ongoing series on implementing the legacy NeHe lessons (originally done in C++ with OpenGL 1.x) using the three.js library. This post covers  Lesson 23.

In computer graphics, sphere mapping (or spherical environment mapping) is a type of reflection mapping that approximates reflective surfaces by considering the environment to be an infinitely far-away spherical wall. This environment is stored as a texture (image) depicting what a mirrored sphere would look like if it were placed in that environment.

So how does this magic happen? Once again, three.js does almost all the heavy lifting. However, there are a couple  interesting wrinkles, notably how the texture gets mapped onto the sphere and how the background is rendered.

Please see the article linked above on the Geo-F/X website for all the details.

###### Mapping on to the Sphere

In earlier lessons (like Lesson 9 and Lesson 17) we covered UV coordinates and how they are used to map a texture onto a shape.  But in those cases, the texture was flat and the target shape was flat as well.  In this case, we use a texture that has a “fake” curvature to it. See the lesson for details on how this was done.  Here is the spherized image:

You can most clearly see the effect of the spherical filter in the “halo” in the sky above  El Capitan.  The details on the math used to map the spherized image are in the article.

###### Rendering the Background

We map the texture onto a sphere which appears to be in the foreground and can be spun and moved independently of the background.  So how is this done?  The answer is that the demo contains two scenes, a foreground scene containing the sphere and a second scene which is the background. Then the background is a second scene that has a single plane geometry covering the entire view of the second scene.  Finally, the properties of the scene are set such that it is always rendered behind the first scene.  Details of all this are in the lesson itself.

###### The Lessons and Source

More information and a live demo of this lesson can be found at Geo-F/X here.  As always, the sources are on github here. Feel free to contact me at rkwright@geofx.com or comment on this article directly below.