<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.1//EN" "https://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 =' https://www.web3d.org/specifications/x3d-3.1.xsd ' >
<head>
<meta name='titlecontent=' CreateX3DFromStringRandomSpheres.x3d '/>
<meta name='descriptioncontent='Sample world constructed in a Script node using the EcmaScript Browser call createX3DFromString.'/>
<meta name='creatorcontent='originally by Vincent Gardet, revised by Curt Blais, adapted to X3D by Don Brutzman and MV4205 class'/>
<meta name='createdcontent='3 May 2007'/>
<meta name='modifiedcontent='12 October 2023'/>
<meta name='referencecontent=' CreateVrmlFromStringRandomBoxes.x3d '/>
<meta name='Imagecontent='CreateX3DFromStringRandomSpheres.png' />
<meta name='referencecontent='See CreateVrmlFromString tutorial at'/>
<meta name='referencecontent=' http://www.gardet.nom.fr/vrmlcity/tut_cvfs.htm '/>
<meta name='subjectcontent='create X3D from string, ecmascript'/>
<meta name=' warning content=' X3D browser support for Browser.createX3DFromString() is poor '/>
<meta name=' warning content=' Xj3D fails on embedded quote marks in sceneString Script '/>
<meta name=' warning content=' this scene will not work in VRML97 browsers '/>
<meta name='identifiercontent=' https://www.web3d.org/x3d/content/examples/Basic/course/CreateX3DFromStringRandomSpheres.x3d '/>
<meta name='generatorcontent='X3D-Edit 4.0, https://savage.nps.edu/X3D-Edit'/>
<meta name='licensecontent=' ../license.html'/>
</head>
<!--

to top <!-- Event Graph ROUTE Table shows event connections -->
 
<!-- Index for DEF nodes: HoldsAutogeneratedContent, SphereGeneratorScript, TouchText

Index for Viewpoint nodes: Viewpoint_1, Viewpoint_2
-->
<Scene>
<WorldInfo title='CreateX3DFromStringRandomSpheres.x3d'/>
<NavigationInfo avatarSize='0.1 1.6 2.0headlight='falsespeed='5type='"EXAMINE" "WALK" "ANY"'/>
<Background skyAngle='1.1 1.57skyColor='.3 .3 1 .4 .4 1 .7 .7 .9'/>
<DirectionalLight direction='0 -.5 -1'/>
<DirectionalLight direction='-1 -.5 .5intensity='.9'/>
<DirectionalLight direction='1 -.5 .5intensity='.8'/>
<Viewpoint description='Interaction viewposition='50 1.6 150'/>
<Viewpoint description='Plan (top-down) vieworientation='1 0 0 -1.54position='50 200 50'/>
<Shape>
<Appearance>
<Material diffuseColor='.3 .3 1transparency='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>
<Shape>
<Appearance>
<Material diffuseColor='0.1 0.3 0.2emissiveColor='0.3 0.6 0.9'/>
</Appearance>
<Text string='"Ten random spheres" "computed at runtime" "" "(view console for results)'>
<FontStyle justify='"MIDDLE" "MIDDLE"'/>
</Text>
</Shape>
<!-- ROUTE information for TouchText node:  [from isActive to SphereGeneratorScript.recompute ] -->
<TouchSensor DEF='TouchTextdescription='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'/>
<!-- ROUTE information for SphereGeneratorScript node:  [from TouchText.isActive to recompute ] -->
<Script DEF='SphereGeneratorScriptdirectOutput='true'>
<field name='ModifiableExternalNodetype='SFNodeaccessType='initializeOnly'>
<Group USE=' HoldsAutogeneratedContent'/>
</field>
<field name='recomputetype='SFBoolaccessType='inputOnly'/>
<![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.println (sceneString);
//     newNode = Browser.createVrmlFromString(sceneString);
       newNode = Browser.createX3DFromString(sceneString);
       ModifiableExternalNode.children[i] = newNode[0];
  }
}

    
]]>
</Script>
< ROUTE  fromNode=' TouchText' fromField='isActive' toNode=' SphereGeneratorScript' toField='recompute'/>
</Scene>
</X3D>
<!--

to top <!-- Event Graph ROUTE Table shows event connections -->
 
<!-- Index for DEF nodes: HoldsAutogeneratedContent, SphereGeneratorScript, TouchText

Index for Viewpoint nodes: Viewpoint_1, Viewpoint_2
-->

Event Graph ROUTE Table with 1 ROUTE connections total, showing X3D event-model relationships for this scene.

Each row shows an event cascade that may occur during a single timestamp interval between frame renderings, as part of the X3D execution model.

TouchText
TouchSensor
isActive
SFBool

ROUTE
event to
(1)
SphereGeneratorScript
Script
recompute
SFBool


      SphereGeneratorScript
Script
No ROUTE connection found for output from this node.
Contains SFNode field with indirect access to another node. 


-->

<!-- Online at
https://www.web3d.org/x3d/content/examples/Basic/course/CreateX3DFromStringRandomSpheresIndex.html -->
<!-- Version control at
https://sourceforge.net/p/x3d/code/HEAD/tree/www.web3d.org/x3d/content/examples/Basic/course/CreateX3DFromStringRandomSpheres.x3d -->

<!-- Color legend: X3D terminology <X3dNode DEF='idName' field='value'/> matches XML terminology <XmlElement DEF='idName' attribute='value'/>
(Light-blue background: event-based behavior node or statement) (Grey background inside box: inserted documentation) (Magenta background: X3D Extensibility)
-->

to top <!-- For additional help information about X3D scenes, please see X3D Tooltips, X3D Resources, and X3D Scene Authoring Hints. -->