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 }
}
|