{ "X3D": {
    "encoding":"UTF-8",
    "@profile":"Immersive",
    "@version":"3.0",
    "@xsd:noNamespaceSchemaLocation":"http://www.web3d.org/specifications/x3d-3.0.xsd",
    "JSON schema":"http://www.web3d.org/specifications/x3d-3.3-JSONSchema.json",
    "head": {
        "meta": [
          {
            "@name":"title",
            "@content":"Figure30.5ScriptTorusBuilder.x3d"
          },
          {
            "@name":"creator",
            "@content":"Figure 30.5, The VRML 2.0 Sourcebook, Copyright [1997] By Andrea L. Ames, David R. Nadeau, and John L. Moreland"
          },
          {
            "@name":"reference",
            "@content":"http://www.wiley.com/legacy/compbooks/vrml2sbk/ch30/30fig05.htm"
          },
          {
            "@name":"translator",
            "@content":"Don Brutzman"
          },
          {
            "@name":"created",
            "@content":"28 September 2000"
          },
          {
            "@name":"modified",
            "@content":"11 January 2014"
          },
          {
            "@name":"description",
            "@content":"A torus shape built with a donut-maker program script. Also see Figures 31.6 and 31.7 for comparison."
          },
          {
            "@name":"identifier",
            "@content":"http://www.web3d.org/x3d/content/examples/Vrml2Sourcebook/Chapter30-Scripts/Figure30.5ScriptTorusBuilder.x3d"
          },
          {
            "@name":"generator",
            "@content":"X3D-Edit 3.3, https://savage.nps.edu/X3D-Edit"
          },
          {
            "@name":"license",
            "@content":"../../license.html"
          },
          {
            "@name":"translated",
            "@content":"19 March 2017"
          },
          {
            "@name":"generator",
            "@content":"X3dToJson.xslt, http://www.web3d.org/x3d/stylesheets/X3dToJson.html"
          },
          {
            "@name":"warning",
            "@content":"An experimental version of X3D JSON encoding is used for this scene.  Status online at http://www.web3d.org/wiki/index.php/X3D_JSON_Encoding"
          }
        ]
    },
    "Scene": {
        "-children":[
          { "Viewpoint":
            {
              "@description":"Torus",
              "@orientation":[1,0,0,-0.78],
              "@position":[0,5,5]
            }
          },
          { "Group":
            {
              "-children":[
                { "Shape":
                  {
                    "-geometry":
                      { "Extrusion":
                        {
                          "@DEF":"Donut",
                          "@beginCap":false,
                          "@convex":false,
                          "@creaseAngle":1.57,
                          "@endCap":false
                        }
                      },
                    "-appearance":
                      { "Appearance":
                        {
                          "-material":
                            { "Material":
                              {
                                "@diffuseColor":[0,0.4,0.8]
                              }
                            }
                        }
                      }
                  }
                },
                { "Script":
                  {
                    "@DEF":"DonutMakerExternalScript",
                    "@url":["donutmaker.js","http://www.web3d.org/x3d/content/examples/Vrml2Sourcebook/Chapter30-Scripts/donutmaker.js"],
                    "field": [
                      {
                        "@name":"spineRadius",
                        "@accessType":"initializeOnly",
                        "@type":"SFFloat",
                        "@value":2
                      },
                      {
                        "@name":"crossSectionRadius",
                        "@accessType":"initializeOnly",
                        "@type":"SFFloat",
                        "@value":1
                      },
                      {
                        "@name":"spineResolution",
                        "@accessType":"initializeOnly",
                        "@type":"SFInt32",
                        "@value":16
                      },
                      {
                        "@name":"crossSectionResolution",
                        "@accessType":"initializeOnly",
                        "@type":"SFInt32",
                        "@value":16
                      },
                      {
                        "@name":"set_spineRadius",
                        "@accessType":"inputOnly",
                        "@type":"SFFloat"
                      },
                      {
                        "@name":"set_crossSectionRadius",
                        "@accessType":"inputOnly",
                        "@type":"SFFloat"
                      },
                      {
                        "@name":"spine_changed",
                        "@accessType":"outputOnly",
                        "@type":"MFVec3f"
                      },
                      {
                        "@name":"crossSection_changed",
                        "@accessType":"outputOnly",
                        "@type":"MFVec2f"
                      }
                    ]
                  }
                },
                { "Script":
                  {
                    "@DEF":"DonutMakerInternalScript",
                    "field": [
                      {
                        "@name":"spineRadius",
                        "@accessType":"initializeOnly",
                        "@type":"SFFloat",
                        "@value":2
                      },
                      {
                        "@name":"crossSectionRadius",
                        "@accessType":"initializeOnly",
                        "@type":"SFFloat",
                        "@value":1
                      },
                      {
                        "@name":"spineResolution",
                        "@accessType":"initializeOnly",
                        "@type":"SFInt32",
                        "@value":16
                      },
                      {
                        "@name":"crossSectionResolution",
                        "@accessType":"initializeOnly",
                        "@type":"SFInt32",
                        "@value":16
                      },
                      {
                        "@name":"set_spineRadius",
                        "@accessType":"inputOnly",
                        "@type":"SFFloat"
                      },
                      {
                        "@name":"set_crossSectionRadius",
                        "@accessType":"inputOnly",
                        "@type":"SFFloat"
                      },
                      {
                        "@name":"spine_changed",
                        "@accessType":"outputOnly",
                        "@type":"MFVec3f"
                      },
                      {
                        "@name":"crossSection_changed",
                        "@accessType":"outputOnly",
                        "@type":"MFVec2f"
                      }
                    ],
                    "#sourceText":[
"ecmascript:",
"",
"function initialize( ) {",
"    generateCrossSection( );",
"    generateSpine( );",
"}",
"",
"function set_crossSectionRadius( csr, ts ) {",
"    crossSectionRadius = csr;",
"    generateCrossSection( );",
"//  Browser.print ('crossSectionRadius = ' + csr);",
"}",
"",
"function set_spineRadius( sr, ts ) {",
"    spineRadius = sr;",
"    generateSpine( );",
"//  Browser.print ('spineRadius = ' + sr);",
"}",
"",
"function generateCrossSection( ) {",
"    angle = 0.0;",
"    delta = (2 * 3.141592653) / crossSectionResolution;",
"    for ( i = 0; i <= crossSectionResolution; i++ ) {",
"        crossSection_changed[i][0] =  crossSectionRadius * Math.cos( angle );",
"        crossSection_changed[i][1] = -crossSectionRadius * Math.sin( angle );",
"        angle += delta;",
"    }",
"    // added code to make ends meet",
"    crossSection_changed[crossSectionResolution + 1][0] = crossSection_changed[0][0];",
"    crossSection_changed[crossSectionResolution + 1][1] = crossSection_changed[0][1];",
"}",
"",
"function generateSpine( ) {",
"    angle = 0.0;",
"    delta = (2 * 3.141592653) / spineResolution;",
"    for ( i = 0; i <= spineResolution; i++ ) {",
"        spine_changed[i][0] =  spineRadius * Math.cos( angle );",
"        spine_changed[i][1] =  0.0;",
"        spine_changed[i][2] = -spineRadius * Math.sin( angle );",
"        angle += delta;",
"    }",
"    // added code to make ends meet",
"    spine_changed[spineResolution + 1][0] = spine_changed[0][0];",
"    spine_changed[spineResolution + 1][1] = 0.0;",
"    spine_changed[spineResolution + 1][2] = spine_changed[0][2];",
"}"
]
                  }
                }
              ]
            }
          },
          {
            "#comment":"Either ROUTE from DonutMakerExternalScript or DonutMakerInternalScript, both methods work"
          },
          { "ROUTE":
            {
              "@fromField":"spine_changed",
              "@fromNode":"DonutMakerInternalScript",
              "@toField":"set_spine",
              "@toNode":"Donut"
            }
          },
          { "ROUTE":
            {
              "@fromField":"crossSection_changed",
              "@fromNode":"DonutMakerInternalScript",
              "@toField":"set_crossSection",
              "@toNode":"Donut"
            }
          }
        ]
    }
  }
}