VRML: Javascript Iteration


Regular Grid

#VRML V2.0 utf8

NavigationInfo { type "EXAMINE" }
Viewpoint { position -1 3 10 }
DEF ROOT Group {}
DEF LOADED ProximitySensor { size 1000 1000 1000 }

DEF SCRIPT Script {
   eventIn SFBool go
   eventOut MFNode newNodes
   url "vrmlscript:

function randR(l,h) { return Math.random()*(h-l)+l; }

function go(val) {
   if(val) {
      totalNumNodes = 0;
      for(i=-5; i<5; i++) {
	 for(j=-5; j<5; j++) {
	    tx = i;  ty = 0;  tz = j;
	    sy = randR(1,5);
	    freshNodes = Browser.createVrmlFromString('Transform {' +
						      '  translation ' + tx + ' 0 ' + tz +
						      '  scale 1 ' + sy + ' 1' +
						      '  children Shape {' +
						      '    appearance Appearance {' +
						      '      material Material {}' +
						      '    }' +
						      '    geometry Box { size .9 .9 .9 }'+
						      '  }' +
						      '}');
	    for(k=0; k<freshNodes.length; k++) {
	       newNodes[totalNumNodes+k] = freshNodes[k];
	    }
	    totalNumNodes += freshNodes.length;
	 }
      }
   }
}

"
}

ROUTE LOADED.isActive TO SCRIPT.go
ROUTE SCRIPT.newNodes TO ROOT.addChildren
(world)

##################################################

Random Placement and Color

#VRML V2.0 utf8

NavigationInfo { type "EXAMINE" }
DEF ROOT Group {}
DEF LOADED ProximitySensor { size 1000 1000 1000 }

DEF SCRIPT Script {
   eventIn SFBool go
   eventOut MFNode newNodes
   url "vrmlscript:

function randR(l,h) { return Math.random()*(h-l)+l; }

function go(val) {
   if(val) {
      totalNumNodes = 0;
      for(i=0; i<100; i++) {
	 tx = randR(-4,4);  ty = randR(-4,4);  tz = randR(-4,4);
	 rx = randR(-1,1);  ry = randR(-1,1);  rz = randR(-1,1);  ra = randR(0,2*Math.PI);
	 sx = randR(1,1000);  sy = randR(1,50);  sz = randR(1,150);
	 r = randR(.6,.8); g = randR(.4,.6); b = randR(.2,.3);
	 
	 choice = '';
	 which = Math.random() * 3;
	 if(which < 1) { choice = 'Box { size .01 .01 .01 }'; }
	 else if(which < 2) { choice = 'Sphere { radius .005 }'; }
	 else if(which < 3) { choice = 'Cone { bottomRadius .005 height .01 }'; }

	 freshNodes = Browser.createVrmlFromString('Transform {' +
						   '  translation ' + tx + ' ' + ty + ' ' + tz +
						   '  rotation ' + rx + ' ' + ry + ' ' + rz + ' ' + ra +
						   '  scale ' + sx + ' ' + sy + ' ' + sz +
						   '  children Shape {' +
						   '    appearance Appearance {' +
						   '      material Material {' +
						   '        diffuseColor '+r+' '+g+' '+b+
						   '      }'+
						   '    }' +
						   '    geometry ' + choice +
						   '  }' +
						   '}');
	 for(j=0; j < freshNodes.length; j++) {
	    newNodes[totalNumNodes+j] = freshNodes[j];
	 }
	 totalNumNodes += freshNodes.length;
      }
   }
}

"
}

ROUTE LOADED.isActive TO SCRIPT.go
ROUTE SCRIPT.newNodes TO ROOT.addChildren

(world)

##################################################

Random Keys and KeyValues

#VRML V2.0 utf8

NavigationInfo { type "EXAMINE" }
Viewpoint { position -1 3 10 }

DEF ROOT Group {}

DEF TIMER TimeSensor {
   cycleInterval 3
   loop TRUE
}

DEF LOADED ProximitySensor { size 1000 1000 1000 }

DEF SCRIPT Script {
   eventIn SFBool go
   eventOut MFNode newNodes
   field SFNode timer USE TIMER
   directOutput TRUE
   url "vrmlscript:

function randR(l,h) { return Math.random()*(h-l)+l; }

function go(val) {
   if(val) {
      totalNumNodes = 0;
      for(i=-5; i<5; i++) {
	 for(j=-5; j<5; j++) {
	    tx = i;  ty = 0;  tz = j;
	    sy = randR(1,5);
	    freshNodes = Browser.createVrmlFromString('Transform {' +
						      '  translation ' + tx + ' 0 ' + tz +
						      '  scale 1 ' + sy + ' 1' +
						      '  children Shape {' +
						      '    appearance Appearance {' +
						      '      material Material {}' +
						      '    }' +
						      '    geometry Box { size .9 .9 .9 }'+
						      '  }' +
						      '}');
	    transform = freshNodes[0];

	    // make interpolator
	    inodes = Browser.createVrmlFromString('PositionInterpolator {}');
	    interp = inodes[0];
	    
	    // set interpolator keys and values
	    interp.key = new MFFloat(0, .5, 1);
	    v = new MFVec3f(3);
	    v[0] = new SFVec3f(tx, 0, tz);
	    v[1] = new SFVec3f(tx, randR(-2,4), tz);
	    v[2] = new SFVec3f(tx, 0, tz);
	    interp.keyValue = v;

	    // wire timer to interpolator
	    Browser.addRoute(timer, 'fraction_changed', interp, 'set_fraction');

	    // wire interpolator to transform
	    Browser.addRoute(interp, 'value_changed', transform, 'translation');

	    // add interpolator node to eventOut array
	    newNodes[totalNumNodes] = interp;  ++totalNumNodes;

	    for(k=0; k<freshNodes.length; k++) {
	       newNodes[totalNumNodes+k] = freshNodes[k];
	    }
	    totalNumNodes += freshNodes.length;
	 }
      }
   }
}

"
}

ROUTE LOADED.isActive TO SCRIPT.go
ROUTE SCRIPT.newNodes TO ROOT.addChildren

(world)


mrl