<?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='MaterialModulator.x3d' name='title'/>
    <meta content='Mimic a Material node and modulate fields as an animation effect' name='description'/>
    <meta content='Don Brutzman' name='creator'/>
    <meta content='10 March 2008' name='created'/>
    <meta content='3 August 2008' name='modified'/>
    <meta content='X3D prototype requiring Script inputOutput fields' name='subject'/>
    <meta content='MaterialModulator.png' name='image'/>
    <meta content='http://X3dGraphics.com/examples/X3dForWebAuthors/Chapter14-Prototypes/MaterialModulator.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>
    <ProtoDeclare appinfo='mimic a Material node and modulate fields as an animation effect' name='MaterialModulator'>
      <ProtoInterface>
        <field accessType='inputOutput' name='enabled' type='SFBool' value='true'/>
        <field accessType='inputOutput' name='diffuseColor' type='SFColor' value='0.8 0.8 0.8'/>
        <field accessType='inputOutput' name='emissiveColor' type='SFColor' value='0 0 0'/>
        <field accessType='inputOutput' name='specularColor' type='SFColor' value='0 0 0'/>
        <field accessType='inputOutput' name='transparency' type='SFFloat' value='0.0'/>
        <field accessType='inputOutput' name='shininess' type='SFFloat' value='0.2'/>
        <field accessType='inputOutput' name='ambientIntensity' type='SFFloat' value='0.2'/>
      </ProtoInterface>
      <ProtoBody>
        <Material DEF='MaterialNode'>
          <IS>
            <connect nodeField='diffuseColor' protoField='diffuseColor'/>
            <connect nodeField='emissiveColor' protoField='emissiveColor'/>
            <connect nodeField='specularColor' protoField='specularColor'/>
            <connect nodeField='transparency' protoField='transparency'/>
            <connect nodeField='shininess' protoField='shininess'/>
            <connect nodeField='ambientIntensity' protoField='ambientIntensity'/>
          </IS>
        </Material>
        <!-- Only first node (the node type) is renderable, others are along for the ride -->
        <Script DEF='MaterialModulatorScript'>
          <field accessType='inputOutput' name='enabled' type='SFBool'/>
          <field accessType='inputOutput' name='diffuseColor' type='SFColor'/>
          <field accessType='outputOnly' name='newColor' type='SFColor'/>
          <field accessType='inputOnly' name='clockTrigger' type='SFTime'/>
          <IS>
            <connect nodeField='enabled' protoField='enabled'/>
            <connect nodeField='diffuseColor' protoField='diffuseColor'/>
          </IS>
          <![CDATA[
ecmascript:
function initialize ()
{
    newColor = diffuseColor; // start with correct color
}

function clockTrigger (timeValue)
{
    if (!enabled) return;
    red   = newColor.r;
    green = newColor.g;
    blue  = newColor.b;
    
    // note different modulation rates for each color component, % is modulus operator
    newColor = new SFColor ((red + 0.02) % 1, (green + 0.03) % 1, (blue + 0.04) % 1);   
    Browser.print ('diffuseColor=(' + red +',' + green + ',' + blue + ') newColor=' + newColor.toString() + '\n');
}
]]>
        </Script>
        <ROUTE fromField='newColor' fromNode='MaterialModulatorScript' toField='diffuseColor' toNode='MaterialNode'/>
        <TimeSensor DEF='ModulationClock' cycleInterval='0.05' loop='true'/>
        <ROUTE fromField='cycleTime' fromNode='ModulationClock' toField='clockTrigger' toNode='MaterialModulatorScript'/>
      </ProtoBody>
    </ProtoDeclare>
    <!-- Rendered geometry follows prototype declaration -->
    <Shape>
      <Sphere/>
      <Appearance>
        <ProtoInstance containerField='material' name='MaterialModulator'>
          <fieldValue name='enabled' value='true'/>
          <fieldValue name='diffuseColor' value='0.5 0.1 0.1'/>
        </ProtoInstance>
      </Appearance>
    </Shape>
  </Scene>
</X3D>