<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 4.0//EN" "https://www.web3d.org/specifications/x3d-4.0.dtd">
<X3D profile='Immersive' version='4.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='https://www.web3d.org/specifications/x3d-4.0.xsd'>
  <head>
    <meta content='ECMAScriptSaiCreateNodesFromPrototype.x3d' name='title'/>
    <meta content='Java Scene Authoring Interface (JSAI) scene and source code example that show creation of nodes from a prototype.' name='description'/>
    <meta content='X3D Working Group' name='creator'/>
    <meta content='23 February 2005' name='created'/>
    <meta content='28 September 2025' name='modified'/>
    <meta content='https://www.web3d.org/files/specifications/19777-1/V3.3/Part1/X3D_ECMAScript.html' name='reference'/>
    <meta content='X3D language bindings, ISO/IEC 19777-1, Part 1: ECMAScript, B.6 Create nodes from a prototype' name='specificationSection'/>
    <meta content='https://www.web3d.org/files/specifications/19777-1/V3.3/Part1/examples.html#CreateNodesFromAPrototype' name='specificationUrl'/>
    <meta content='X3D-Edit 4.0, https://www.web3d.org/x3d/tools/X3D-Edit' name='generator'/>
    <meta content='https://www.web3d.org/x3d/content/examples/Basic/X3dSpecifications/ECMAScriptSaiCreateNodesFromPrototype.x3d' name='identifier'/>
    <meta content='../license.html' name='license'/>
  </head>
  <Scene>
    <WorldInfo title='ECMAScriptSaiCreateNodesFromPrototype.x3d'/>
    <ProtoDeclare name='ColoredSphere'>
      <ProtoInterface>
        <field accessType='initializeOnly' name='color' type='SFColor' value='0 1 0'/>
      </ProtoInterface>
      <ProtoBody>
        <Shape>
          <Sphere/>
          <Appearance>
            <Material>
              <IS>
                <connect nodeField='diffuseColor' protoField='color'/>
              </IS>
            </Material>
          </Appearance>
        </Shape>
      </ProtoBody>
    </ProtoDeclare>
    <Script DEF='SC'>
      <![CDATA[
ecmascript:
function initialize() {
    // Create nodes directly in the parent scene
    scene = Browser.currentScene;

	// TODO scene.protoDeclarations not defined in
	// https://www.web3d.org/specifications/19777-1/V3.3/Part1/functions.html
	// but Table 7.5 — Execution context properties does define .protos
	// For precise clarity, consider deprecating/renaming .protos as .protoDeclarations
	
    // Look through proto declarations for ColoredSphere
//  protoDecls = scene.protoDeclarations;
    protoDecls = scene.protos;

    var protoDecl;

    for(i=0; i < protoDecls.length; i++) {
        if (protoDecls[i].name == 'ColoredSphere')
            protoDecl = protoDecls[i];
    }

    // Create an instance of RedSphere
    instance = protoDecl.newInstance();

    // Set the color field to Red
    instance.color = new SFColor(1,0,0);

    // Add the created proto to the scene
    scene.rootNodes[0] = instance;
}
]]>
    </Script>
  </Scene>
</X3D>