Part 2: Dynamically Creating VRML, createVrmlFromString
Details Details
 More of this Feature
• Part 1: createVrmlFromString
 
 Join The Discussion
"market trends..."
AHEDSTROM
 
 Related Resources
• Tutorials
• Learning VRML
 
 From Other Guides
• JavaScript
• HTML/XML
• Perl
 

The beginning of the file is a simple beginning, a Background and Viewpoint Node. The unusual Node is the

DEF ROOT Transform{ children[ ]}

This ROOT node is what we will add to later in the script. What we want to do is take a piece of VRML for a Box and add a bunch of them to a scene. The VRML, outside of the Script would look like:


Transform { translation x y z
	children Transform {
		children	[ 
			Shape { appearance Appearance { 
				material DEF WHITE Material {  
					diffuseColor 1 1 1 }
				}	
				geometry Box { size 7 7 7 } 
			} 
		] 
	} 
}

The text in the script is the above VRML, substituting some values, the x y z values from the for loops and a random color for the diffuseColor parameters and a random value from 0 to 7 for the size parameter of the Box. The for loops increment the x y z values used in the translation, creating the cube type arrangement of the boxes. The getRandom() function returns a value between 0 and 1 so we simply substitute a call to that function instead of the hard coded value. Finally to add some more variety to the scene we substitute more calls to getRandom to vary the size of the Box, randomly.

You must be very careful arranging the text in the script as each line must start with a single quote. The "+" operator concatenates the strings together. Also note that the work is all contained within a function called initialize, a special name in JavaScript Script nodes to ensure that it get's executed.

Finally we must get a handle to the "Browser" object and add the VRML, now in a giant string, into the scene graph. This is accomplished via:

new_f = Browser.createVrmlFromString( str_f );
ROOT.addChildren = new_f;

Note that it's here where we use that ROOT Node defined earlier.

Generating VRML dynamically is a fabulous way of creating highly complex scenes with very little code. Give it a try and you won't be glad you did, I sure was!

ZZZNEWSLETTERSIGNUP1ZZZ
[Tutorials] [Web3D Technology Comparison] [Virtual Humans]
[Virtual Reality] [Art] [People of Web3D]
[Panoramic Imaging] [FAQs] [Companies]

Previous page > 3D Shopping for All Seasons > Page 1, 2