The Annotated VRML 97 Reference

1 Intro     Concepts     3 Nodes     4 Fields/Events    Conformance
A Grammar     B Java     C JavaScript     D Examples     E Related Info    References
Quick Java         Quick JavaScript         Quick Nodes   
 

  About the Book
  
Help
  Copyright © 1997-99
  Purchase the book from Amazon.com

Chapter 3:
Node Reference


Intro
Anchor
Appearance
AudioClip
Background
Billboard
Box
Collision
Color
ColorInterpolator
Cone
Coordinate
CoordinateInterpolator
Cylinder
CylinderSensor
DirectionalLight
ElevationGrid
Extrusion
Fog
FontStyle
Group
ImageTexture
IndexedFaceSet
IndexedLineSet
Inline
LOD
Material
MovieTexture
NavigationInfo
Normal
NormalInterpolator
OrientationInterpolator
PixelTexture
PlaneSensor
PointLight
PointSet
PositionInterpolator
ProximitySensor
ScalarInterpolator
Script
Shape
Sound
Sphere
SphereSensor
SpotLight
Switch
Text
TextureCoordinate
TextureTransform
TimeSensor
TouchSensor
Transform
Viewpoint
VisibilitySensor
WorldInfo

+3.43 Sphere

Sphere { 
  field SFFloat radius  1    # (0,INF)
}

The Sphere node specifies a sphere centred at (0, 0, 0) in the local coordinate system. The radius field specifies the radius of the sphere and shall be > 0.0. Figure 3-49 depicts the fields of the Sphere node.

Sphere node diagram

Figure 3-49: Sphere Node

When a texture is applied to a sphere, the texture covers the entire surface, wrapping counterclockwise from the back of the sphere (i.e., longitudinal arc intersecting the -Z-axis) when viewed from the top of the sphere. The texture has a seam at the back where the X=0 plane intersects the sphere and Z values are negative. TextureTransform affects the texture coordinates of the Sphere.

The Sphere node's geometry requires outside faces only. When viewed from the inside the results are undefined.

 

TIP: To create ellipsoid shapes, enclose a Sphere in a Transform and modify the scale field of the Transform.

TIP: Sphere nodes are specified in the geometry field of a Shape node; they may not be children of a Transform or Group node.

Browser implementors will make different compromises between rendering speed and the quality of spheres (and cones and cylinders and text). They may choose to display very coarse-looking spheres to make scenes render faster, which is good if you want your world to display smoothly on a wide variety of machines, but is bad if you want to guarantee that your world maintains a certain image quality. If maintaining quality is important, describe your shapes using the polygon-based primitives. To minimize file transmission time, you can generate the polygons using a Script. For example, this prototype will generate an approximation of a one-unit-radius Sphere, given the number of latitude and longitude samples:

  #VRML V2.0 utf8
  PROTO LatLongSphere [
    field SFInt32 numLat 4
    field SFInt32 numLong 4 ]
  {
    # Empty IndexedFaceSet, filled in by Script based
    #    on PROTO fields
    DEF IFS IndexedFaceSet {
      coord DEF C Coordinate { }
      texCoord DEF TC TextureCoordinate { }
      creaseAngle 3.14
    }
    DEF S Script {
      field SFInt32 numLat IS numLat
      field SFInt32 numLong IS numLong
      eventOut MFVec3f c_changed
      eventOut MFVec2f tc_changed
      eventOut MFInt32 ci_changed
      url "javascript:
        function initialize() {
          var r, angle, x, y, z;
          var i, j, polyIndex;
          // Compute coordinates, texture coordinates:
          for (i = 0; i < numLat; i++) {
            y = 2 * ( i / (numLat-1) ) - 1;
            r = Math.sqrt( 1 - y*y );
            for (j = 0; j < numLong; j++) {
              angle = 2 * Math.PI * j / numLong;
              x = -Math.sin(angle)*r;
              z = -Math.cos(angle)*r;
              c_changed[i*numLong+j] = new SFVec3f(x,y,z);
              tc_changed[i*numLong+j] =
              tc_channew SFVec2f( j/numLong, i/(numLat-1) );
            }
          }
          // And compute indices:
          for (i = 0; i < numLat-1; i++) {
            for (j = 0; j < numLong; j++) {
              polyIndex = 5*(i*numLong+j);
              ci_changed[polyIndex+0] = i*numLong+j;
              ci_changed[polyIndex+1] = i*numLong+(j+1)%numLong;
              ci_changed[polyIndex+2] = (i+1)*numLong+(j+1)%numLong;
              ci_changed[polyIndex+3] = (i+1)*numLong+j;
              ci_changed[polyIndex+4] = -1;  // End-of-polygon
            }
          }
        }"
    }
    ROUTE S.c_changed TO C.set_point
    ROUTE S.tc_changed TO TC.set_point
    ROUTE S.ci_changed TO IFS.set_coordIndex
  } # end PROTO
  Shape {
    appearance Appearance { material Material { } }
    geometry LatLongSphere { numLat 16 numLong 16 }
  } 

EXAMPLE (click to run): The following example illustrates a simple use of the Sphere node (see Figure 3-50). Notice how the last two Spheres, "carrot" and "hat rim," use the scale field of a Transform to deform the sphere:

#VRML V2.0 utf8
Group { children [
  Transform {            # Base of snowman
    translation 0 1 0
    children Shape {
      geometry Sphere { radius 1 }
      appearance DEF A1 Appearance {
        material Material {
          diffuseColor 1 1 1
          emissiveColor .3 .3 .3
        }
      }
    }
  }
  Transform {            # Middle of snowman
    translation 0 2.333 0
    children Shape {
      geometry Sphere { radius 0.66 }
      appearance USE A1
    }
  }
  Transform {            # Head of snowman
    translation 0 3.2 0
    children Shape {
      geometry Sphere { radius 0.4 }
      appearance USE A1
    }
  }
  Transform {            # Left eye stone
    translation .16 3.4 .3
    children Shape {
      geometry DEF S1 Sphere { radius 0.05 }
      appearance DEF A2 Appearance {
        material Material { diffuseColor 0 0 0 }
      }
    }
  }
  Transform {            # Right eye stone
    translation -.17 3.43 .3
    children Shape {
      geometry USE S1
      appearance Appearance {
        material Material { diffuseColor 0.2 0.2 0.2 }
      }
     }
  }
  Transform {            # Carrot nose
    translation 0 3.3 .5
    scale 0.5 0.5 2
    children Shape {
      geometry Sphere { radius 0.1 }
      appearance Appearance {
        material Material { diffuseColor 1.0 0.3 0.1 }
      }
    }
  }
  Transform {            # Hat cap
    translation 0 3.5 0
    children Shape {
      geometry Sphere { radius .2 }
      appearance Appearance {
        material Material { diffuseColor 1.0 0.0 0.0 }
      }
    }
  }
  Transform {            # Hat rim
    translation 0 3.55 0
    scale 2 .01 2
    children Shape {
      geometry Sphere { radius .4 }
      appearance Appearance {
        material Material { diffuseColor 1.0 0.0 0.0 }
      }
    }
  }
  Background { skyColor 1 1 1 }
  NavigationInfo { type "EXAMINE" }
] }

 

Sphere node example

Figure 3-50: Sphere Node Example