<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.3//EN" "https://www.web3d.org/specifications/x3d-3.3.dtd">
<X3D profile='Immersive' version='3.3 xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation =' https://www.web3d.org/specifications/x3d-3.3.xsd '>
<head>
<meta name='titlecontent='MaterialModulator.x3d'/>
<meta name='descriptioncontent='Mimic a Material node and modulate the diffuseColor field as an animation effect, provided as a prototype for reusability.'/>
<meta name='hintcontent='Learning suggestion for authors: try changing the modulation script so that it goes from [0 ... 1] and then [1 ... 0] alternating, rather than abruptly shifting from 1 immediately back to 0.'/>
<meta name='creatorcontent='Don Brutzman'/>
<meta name='createdcontent='10 March 2008'/>
<meta name='modifiedcontent='20 October 2019'/>
<meta name='subjectcontent='X3D prototype requiring Script inputOutput fields'/>
<meta name='Imagecontent='MaterialModulator.png'/>
<meta name='identifiercontent=' https://X3dGraphics.com/examples/X3dForWebAuthors/Chapter14Prototypes/MaterialModulator.x3d '/>
<meta name='generatorcontent='X3D-Edit 3.3, https://savage.nps.edu/X3D-Edit'/>
<meta name='licensecontent=' ../license.html'/>
</head>
<!-- -->
<Scene>
<WorldInfo title='MaterialModulator.x3d'/>
<ProtoDeclare name='MaterialModulatorappinfo='mimic a Material node and modulate the diffuseColor field as an animation effect' documentation=' https://x3dgraphics.com/examples/X3dForWebAuthors/Chapter14Prototypes/MaterialModulatorIndex.html '>
<ProtoInterface>
<field name='enabledtype='SFBoolvalue='trueaccessType='inputOutput'
 appinfo='default value true'/>

<field name='diffuseColortype='SFColorvalue='0.8 0.8 0.8accessType='inputOutput'
 appinfo='default value 0.8 0.8 0.8'/>

<field name='emissiveColortype='SFColorvalue='0 0 0accessType='inputOutput'
 appinfo='default value 0 0 0'/>

<field name='specularColortype='SFColorvalue='0 0 0accessType='inputOutput'
 appinfo='default value 0 0 0'/>

<field name='transparencytype='SFFloatvalue='0.0accessType='inputOutput'
 appinfo='default value 0.0'/>

<field name='shininesstype='SFFloatvalue='0.2accessType='inputOutput'
 appinfo='default value 0.2'/>

<field name='ambientIntensitytype='SFFloatvalue='0.2accessType='inputOutput'
 appinfo='default value 0.2'/>
</ProtoInterface>
<ProtoBody>
<!-- ROUTE information for MaterialNode node:  [from MaterialModulatorScript.newColor to diffuseColor ] -->
<Material DEF='MaterialNode'>
<IS>
<connect nodeField='diffuseColorprotoField='diffuseColor'/>
<connect nodeField='emissiveColorprotoField='emissiveColor'/>
<connect nodeField='specularColorprotoField='specularColor'/>
<connect nodeField='transparencyprotoField='transparency'/>
<connect nodeField='shininessprotoField='shininess'/>
<connect nodeField='ambientIntensityprotoField='ambientIntensity'/>
</IS>
</Material>
<!-- Only first node (the node type) is renderable, others are along for the ride -->
<!-- ROUTE information for MaterialModulatorScript node:  [from ModulationClock.cycleTime to clockTrigger ] [from newColor to MaterialNode.diffuseColor ] -->
<Script DEF='MaterialModulatorScript'>
<field name='enabledtype='SFBoolaccessType='inputOutput'/>
<field name='diffuseColortype='SFColoraccessType='inputOutput'/>
<field name='newColortype='SFColoraccessType='outputOnly'/>
<field name='clockTriggertype='SFTimeaccessType='inputOnly'/>
<IS>
<connect nodeField='enabledprotoField='enabled'/>
<connect nodeField='diffuseColorprotoField='diffuseColor'/>
</IS>
<![CDATA[
          
ecmascript:
function initialize ()
{
    newColor = diffuseColor; // start with original 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);
	if (enabled)
	{
		Browser.print ('diffuseColor=(' + red +',' + green + ',' + blue + ') newColor=' + newColor.toString() + '\n');
	}
}
function set_enabled (newValue)
{
	enabled = newValue;
}

        
]]>
</Script>
<!-- Clock tickles Script to wake up and compute a new value -->
<ROUTE fromNode='MaterialModulatorScriptfromField='newColortoNode='MaterialNodetoField='diffuseColor'/>
<!-- ROUTE information for ModulationClock node:  [from cycleTime to MaterialModulatorScript.clockTrigger ] -->
<TimeSensor DEF='ModulationClockcycleInterval='0.1loop='true'>
<IS>
<connect nodeField='enabledprotoField='enabled'/>
</IS>
</TimeSensor>
<ROUTE fromNode='ModulationClockfromField='cycleTimetoNode='MaterialModulatorScripttoField='clockTrigger'/>
</ProtoBody>
</ProtoDeclare>
<!-- Rendered geometry for the ProtoInstance now follows prototype declaration -->
<Transform translation='0 1 0'>
<Shape>
<Sphere/>
<Appearance>
<!-- ROUTE information for MaterialModulatorInstance node:  [from EventToggler.toggle to enabled ] -->
<ProtoInstance name='MaterialModulatorDEF='MaterialModulatorInstancecontainerField='material'>
<fieldValue name='enabledvalue='true'/>
<fieldValue name='diffuseColorvalue='0.5 0.1 0.1'/>
<!-- fieldValue declarations for other Material attributes can appear here -->
</ProtoInstance>
</Appearance>
</Shape>
</Transform>
<!-- Selectable Text design pattern has transparent Box and TouchSensor description as a tooltip -->
<Transform translation='0 -2 0'>
<Shape>
<Text string=' "enable/disable"
"MaterialModulator"
'>
<FontStyle family='"SANS"justify='"MIDDLE" "MIDDLE"style='BOLD'/>
</Text>
<Appearance>
<Material diffuseColor='0.9 0.9 0.9'/>
</Appearance>
</Shape>
<Shape>
<Box size='8 2 .001'/>
<Appearance>
<Material transparency='1'/>
</Appearance>
</Shape>
<!-- Toggle text to enable/disable MaterialModulator -->
<!-- ROUTE information for TouchTextInterface node:  [from isActive to EventToggler.set_boolean ] -->
<TouchSensor DEF='TouchTextInterfacedescription='Select to enable/disable MaterialModulator'/>
<!-- ROUTE information for EventToggler node:  [from TouchTextInterface.isActive to set_boolean ] [from toggle to MaterialModulatorInstance.enabled ] -->
<BooleanToggle DEF='EventTogglercontainerField='children'/>
<ROUTE fromNode='TouchTextInterfacefromField='isActivetoNode='EventTogglertoField='set_boolean'/>
<ROUTE fromNode='EventTogglerfromField='toggletoNode='MaterialModulatorInstancetoField='enabled'/>
</Transform>
</Scene>
</X3D>
<!--

Index for ProtoDeclare definition : MaterialModulator

Index for DEF nodes : EventToggler, MaterialModulatorInstance, MaterialModulatorScript, MaterialNode, ModulationClock, TouchTextInterface
-->

<!-- Color key: <X3dNode DEF='idName' field='value'/> matches <XmlElement DEF='idName' attribute='value'/>
(Light blue background: behavior node) (Grey background: inserted documentation) (Magenta background: X3D Extensibility)
    <Prototype name='ProtoName'> <field name='fieldName'/> </Prototype> -->

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