<?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://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter14Prototypes/MaterialModulator.x3d '/>
<meta name='generatorcontent='X3D-Edit 3.3, 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: EventToggler, MaterialModulatorInstance, MaterialModulatorScript, MaterialNode, ModulationClock, TouchTextInterface

Index for ProtoDeclare definition: MaterialModulator
-->
<Scene>
<WorldInfo title='MaterialModulator.x3d'/>
<ProtoDeclare name='MaterialModulatorappinfo='mimic a Material node and modulate the diffuseColor field as an animation effect'  documentation=' https://www.web3d.org/x3d/content/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.println ('diffuseColor=(' + red +',' + green + ',' + blue + ') newColor=' + newColor.toString());
	}
}
function set_enabled (newValue)
{
	enabled = newValue;
}

        
]]>
</Script>
<!-- Clock tickles Script to wake up and compute a new value -->
< ROUTE  fromNode=' MaterialModulatorScript' fromField='newColor' toNode=' MaterialNode' toField='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=' ModulationClock' fromField='cycleTime' toNode=' MaterialModulatorScript' toField='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=' TouchTextInterface' fromField='isActive' toNode=' EventToggler' toField='set_boolean'/>
< ROUTE  fromNode=' EventToggler' fromField='toggle' toNode=' MaterialModulatorInstance' toField='enabled'/>
</Transform>
</Scene>
</X3D>
<!--

to top <!-- Event Graph ROUTE Table shows event connections -->
 
<!-- Index for DEF nodes: EventToggler, MaterialModulatorInstance, MaterialModulatorScript, MaterialNode, ModulationClock, TouchTextInterface

Index for ProtoDeclare definition: MaterialModulator
-->

Event Graph ROUTE Table entries with 4 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.

TouchTextInterface
TouchSensor
isActive
SFBool

ROUTE
event to
(1)
EventToggler
BooleanToggle
set_boolean
SFBool

 
 
then
EventToggler
BooleanToggle
toggle
SFBool

ROUTE
event to
(2)
MaterialModulatorInstance
ProtoInstance
enabled
SFBool


ModulationClock
TimeSensor
cycleTime
SFTime

ROUTE
event to
(1)
MaterialModulatorScript
Script
clockTrigger
SFTime

 
 
then
MaterialModulatorScript
Script
newColor
SFColor

ROUTE
event to
(2)
MaterialNode
Material
diffuseColor
SFColor


      MaterialModulatorScript
Script
newColor
SFColor

ROUTE
event to
(1)
MaterialNode
Material
diffuseColor
SFColor


      MaterialModulatorInstance
ProtoInstance
MaterialModulator
No ROUTE connection found for output from this node.
Contains SFNode/MFNode field with indirect access to other nodes. 


-->

<!-- Online at
https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter14Prototypes/MaterialModulatorIndex.html -->
<!-- Version control at
https://sourceforge.net/p/x3d/code/HEAD/tree/www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter14Prototypes/MaterialModulator.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)
    <ProtoInstance name='ProtoName'> <field name='fieldName'/> </ProtoInstance> -->

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