Part 2: Dissecting the PROTO  

One of the interesting aspects of this PROTO is that rather than using the createVrmlFromScript method it uses the "set" method to build up the data. In this portion of the code:
p[k].x=t*Math.sin(fi); p[k].y=Math.cos(theta); p[k++].z=t*Math.cos(fi); } scrCoord.set_point = p;
The scrCoord.set_point = p works because previously the scrCoord was declared via the statement:
DEF INIT Script { field SFNode scrCoord USE scrCoordThe names don't have to be identical but makes reading the code easier. Also note that the set_point method works because "point" is an exposedField in the Coordinate node, and scrCoord is a Coordinate Node.
Similarly the calculations and setting of the texture coordinates are accomplished via:
#computer the texture coordinates var pt = new MFVec2f(); pt.length = n*n1; k=0; var sti=1.0/n, stj=1/(n1); for (j= 0; j< n; j++) for (i= 0; i< n1; i++) { pt[k].x=i*sti; pt[k++].y=j*stj;} scrTC.set_point = pt;
The indecies into the texture coordinates are set next via:
var ti = new MFInt32( ); ti.length = 5*(n1)*n; k=0; for (j= 0; j< n1; j++) for (i= 0; i< n; i++){ ti[k++]=i+n1*j; ti[k++]=i+n1*j+1; ti[k++]=i+n1*j+n1+1; ti[k++]=i+n1*j+n1; ti[k++]=1;} //print( ti ); scrIFS.set_texCoordIndex = ti;
Note that the print statements are a great way of debugging the code. Open up or view the VRML console to see the results of the print statements.
This is by no means a simple PROTO, that's one of the things that makes it interesting! It's no toy PROTO so dig in and learn about this stuff, and thanks again to Evgeny Demidov for allowing me to use this great code.
Previous page > 3D Shopping for All Seasons > Page 1, 2