<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.1//EN" "http://www.web3d.org/specifications/x3d-3.1.dtd">
<X3D profile='Immersive' version='3.1' xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='http://www.web3d.org/specifications/x3d-3.1.xsd'>
  <head>
    <meta content='CreateX3DFromStringRandomSpheres.x3d' name='title'/>
    <meta content='Sample world constructed in a Script node using the EcmaScript Browser call createX3DFromString.' name='description'/>
    <meta content='originally by Vincent Gardet, revised by Curt Blais, adapted to X3D by Don Brutzman and MV4205 class' name='creator'/>
    <meta content='3 May 2007' name='created'/>
    <meta content='6 September 2010' name='modified'/>
    <meta content='CreateVrmlFromStringRandomBoxes.x3d' name='reference'/>
    <meta content='CreateX3DFromStringRandomSpheres.png' name='image'/>
    <meta content='See CreateVrmlFromString tutorial at' name='reference'/>
    <meta content='http://www.gardet.nom.fr/vrmlcity/tut_cvfs.htm' name='reference'/>
    <meta content='create X3D from string, ecmascript' name='subject'/>
    <meta content='X3D browser support for Browser.createX3DFromString() is poor' name='warning'/>
    <meta content='Xj3D fails on embedded quote marks in sceneString Script' name='warning'/>
    <meta content='this scene will not work in VRML97 browsers' name='warning'/>
    <meta content='http://www.web3d.org/x3d/content/examples/Basic/course/CreateX3DFromStringRandomSpheres.x3d' name='identifier'/>
    <meta content='X3D-Edit 3.2, https://savage.nps.edu/X3D-Edit' name='generator'/>
    <meta content='../license.html' name='license'/>
  </head>
  <Scene>
    <NavigationInfo avatarSize='0.1 1.6 2.0' headlight='false' speed='5' type='"EXAMINE" "WALK" "ANY"'/>
    <Background skyAngle='1.1 1.57' skyColor='.3 .3 1 .4 .4 1 .7 .7 .9'/>
    <DirectionalLight direction='0 -.5 -1' intensity='1'/>
    <DirectionalLight direction='-1 -.5 .5' intensity='.9'/>
    <DirectionalLight direction='1 -.5 .5' intensity='.8'/>
    <Viewpoint description='Interaction view' position='50 1.6 150'/>
    <Viewpoint description='Plan (top-down) view' orientation='1 0 0 -1.54' position='50 200 50'/>
    <Shape>
      <Appearance>
        <Material diffuseColor='.3 .3 1' transparency='0.5'/>
      </Appearance>
      <Box size='1000 0.01 1000'/>
    </Shape>
    <Transform translation='50 1.6 140'>
      <!-- the following is a common interaction design pattern in X3D -->
      <Billboard axisOfRotation='0 1 0'>
        <Shape>
          <Appearance>
            <Material diffuseColor='0.1 0.3 0.2' emissiveColor='0.3 0.6 0.9'/>
          </Appearance>
          <Text string='"Ten random spheres" "computed at runtime"'>
            <FontStyle justify='"MIDDLE" "MIDDLE"'/>
          </Text>
        </Shape>
        <TouchSensor DEF='TouchText' description='Touch text to recompute random spheres'/>
        <!-- Transparent Box makes the text much more accessible/clickable -->
        <Shape>
          <Box size='8 2 0.1'/>
          <Appearance>
            <Material transparency='1'/>
          </Appearance>
        </Shape>
      </Billboard>
    </Transform>
    <!-- ROOT is used at initialize time as parent for randomly generated Shape children -->
    <Group DEF='HoldsAutogeneratedContent'/>
    <Script DEF='SphereGeneratorScript' directOutput='true'>
      <field accessType='initializeOnly' name='ModifiableExternalNode' type='SFNode'>
        <Group USE='HoldsAutogeneratedContent'/>
      </field>
      <field accessType='inputOnly' name='recompute' type='SFBool'/>
      <![CDATA[ecmascript:

function R ()
{
	return Math.random();
}
function recompute (isActive)
{
	if (isActive==true) initialize();
}

function initialize()
{
  for (i=0; i < 10; i++)
  {
    rand1 = 100*R();
    rand2 = 100*R();
    rand3 = 20*R();
    rand4 = 40*R();
    rand5 = 20*R();
    sceneString =    '<X3D version="3.1" profile="Interchange">\n' +
                     '  <Scene>\n' +
                     '      <Transform translation="' + rand1 + ' 0 ' + rand2 + '" >\n' +
                     '           <Shape>\n' +
                     '                   <Appearance>\n' +
                     '                      <Material diffuseColor="' + R() + ' ' + R() + ' ' + R() + '"/>\n' +
                     '                   </Appearance>\n' +
                     '                   <Sphere radius="' + (rand3 / 2) + '"/>\n' +
                     '           </Shape>\n' +
                     '      </Transform>\n' +
                     '  </Scene>\n' +
                     '</X3D>\n' +
                     '<!--==============================================-->\n';

       Browser.print (sceneString);
//     newNode = Browser.createVrmlFromString(sceneString);
       newNode = Browser.createX3DFromString(sceneString);
       ModifiableExternalNode.children[i] = newNode[0];
  }
}]]>
    </Script>
    <ROUTE fromField='isActive' fromNode='TouchText' toField='recompute' toNode='SphereGeneratorScript'/>
  </Scene>
</X3D>
