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 +
						   '  }' +
						   '}');
	 newNodes[totalNumNodes] = freshNodes[0];
	 ++totalNumNodes;
      }
   }
}

"
}

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


mrl