Pixel perfect textures in VRML

Return to frerichs.net Home Page
david [at] frerichs.net

Designers often lament over how poor textures can look in VRML worlds. This quality is caused by the point-sampling technique of texture mapping used by most software rendering engines. Since textures are mapped into 3D space, they are subject to distortion when not perfectly aligned with the browser's camera. 

So what do I do if I want quality images? How do I get pixel perfect textures in VRML? Well, I have just the equation you are looking for. It turns out that if you align the Viewpoint with the texture and place the camera at just the right distance, the pixels in the texture exactly match the pixels on the screen.

To see the results, look at the embedded .wrl above. This world has a high-resolution texture that is pretty unreadable when it is at an angle. But click on the texture and it will snap to a viewpoint created using my equation. You will notice how you suddenly get a full fidelity view of the texture with perfect 1-to-1 mapping of texture pixels to screen pixels. Now you can read the text without getting eyestrain!

The diagram above explains how you do it:

If the embedded .wrl is narrower than it is tall, use this equation:

Zdist = [ embed_width * (x1-x2) ] / [ 2 * horiz_texture_res * tan(fov/2) ]
If the embedded .wrl is shorter than it is wide, then use this equation:
Zdist = [ embed_height * (y1-y2) ] / [ 2 * vert_texture_res * tan(fov/2) ]
The variables are defined as follows: Some assumptions are made in this equation in order to make the equation work:

In the above example, the height of the embed is smaller than the width, so we go with height. So per the second equation:

Zdist = [300 * (1-(-1))]/[2*256*tan(.785398/2)] = 2.829157172164

And that is it! You can take a look at the VRML source code to get the full picture. Now, put this equation to good use and get some beautiful looking textures in your worlds.