This page will document some special techniques available to overcome nontrivial circumstances when implementing Three.js for use in mathematical physics. This makes the techniques available on the Internet without the inconveniences of other ultimately closed-source systems.

Contents

Geometry Issues
Marching Cubes
Transparency Issues
Camera Issues
Lighting Issues
iOS Rendering Issues


Geometry Issues

The vertices of geometries can be easily modified by assigning altered values to their components and setting the flag

geometry.verticesNeedUpdate =  true

Once rendering begins, modifying a geometry by adding or removing vertices will have no effect because the action does not reset WebGL buffers. The original geometry can be removed from the scene and replaced by the modified geometry.


Marching Cubes

While Three.js does not currently implement a proper marching cubes geometry, the underlying code for the marching cubes example can be adapted to produce implicit geometries other than metaballs. The MarchingCubes object takes two parameters: a size that determines the extent of its field in all three coordinate directions and a material. This code visualizes an implicit function of three variables:

var march = new THREE.MarchingCubes( size, material );
march.isolation = 0;

for ( var k = 0 ; k < size ; k++ ) {
  for ( var j = 0 ; j < size ; j++ ) {
    for ( var i = 0 ; i < size ; i++ ) {

      var x = x( size );
      var y = y( size );
      var z = z( size );

      march.field[ i + j*size + k*size*size ] = f( x, y, z );

    }
  }
}

scene.add( march );

The property isolation sets the isolevel of the implicit function surface. Since the field takes some time to evaluate, there is a method end() that can be used to invoke a callback function for further processing of the output:

march.end( callback() );

The MarchingCubes object has a method generateGeometry() that, as the names says, generates a nonbuffer geometry with vertices and faces. Be aware that by default this method returns vertex values with all three coordinates in the range [-1,1]. These must be scaled appropriately to produce a geometry matching the actual input function.


Transparency Issues

When rendering a scene in Three.js, objects are split into two basic groups: opaque and transparent. Opaque objects are rendered first from the front of the scene to the back, then transparent objects from the back of the scene to the front. For transparent objects to render properly from all camera angles, they must have distinct positions. Two transparent objects with the exact same position will render in the order they are added to the scene, so that one will always occlude the other.

When a mesh is created using a custom Geometry, its position defaults to (0,0,0) regardless of the values of the vertices in its underlying geometry. This will lead to unexpected occlusion for transparent objects, since they will by default all have exactly the same position. This can be fixed by centering the geometry in its own frame and using the returned variable to set the position of the mesh:

var c = geometry.center().multiply( -1 );
mesh.position.set( c.x, c.y, c.z );

Altering the position attribute without using set will not update the scene appropriately.

Sprites are drawn after transparent objects, but if included in the same scene will always be occluded. One solution to this is create a separate scene containing only sprites and render it after other objects.


Camera Issues

Since cameras are automatically added to rendered scenes, one may not think too much about their positions relative to other objects. The positions of automatically added cameras are with respect to the origin of the rendering space. If a created scene has an explicit offset, it can be centered in the rendering space by setting its position opposite to its offset. OrbitControls will then transform the rendered scene about the origin as expected.

If a camera is added to an object, its position is now with respect to that object. The default behavior of OrbitControls is then to transform the rendered scene about the origin of the object to which the camera is added. To transform about some other point, for example the explicit offset of the object, the attribute target of OrbitControls must be set to this point and the controls updated.


Lighting Issues

Lights can be made to track with a camera by adding them to the camera as children. For the lights to render, the camera must then be added to the scene. For scenes centered at the origin this presents no problems, but if the scene has an explicit offset the behavior of the lights is highly unexpected.

A light with a sense of direction shines from its position to a target, which by default is the origin. For a light added to a camera which is itself added to a scene with an explicit offset, the attribute target of the light should be set to the offset value. The object light.target must then be added to the scene itself in order for the illumination to render as expected.


iOS Rendering Issues

A Three.js scene embedded in an iframe will grow endlessly in size when viewed in iOS 8/9 with Mobile Safari. For an iframe with id=view, the workaround is

if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {

	view.style.width = getComputedStyle( view ).width;
	view.style.height = getComputedStyle( view ).height;
	view.setAttribute( 'scrolling', 'no' );

}

This code can follow the iframe immediately. There is no need to wait for the contents of the iframe to load in order to reset its style. All three attributes must be reset.

For a page with more than one iframe, retrieve the entire collection with

document.getElementsByTagName( 'iframe' )

and reset the attributes for all.

As of iOS 10.2 this fix is not always needed, as long as the attribute scrolling="no" is set on the iframe and width and height are specified with nonrelative values. The reset was necessary because Mobile Safari previously did not preserve the attributes when loading the page. When relative values are used for width and height, for example percentages, the reset is still needed for these two values.


Uploaded 2017.01.09 — Updated 2017.07.18 analyticphysics.com