<?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=' BackgroundTimeOfDay.x3d '/>
<meta name='descriptioncontent='Interpolate between Background color arrays to show a gradually changing time-of-day effect.'/>
<meta name='creatorcontent='Don Brutzman and MV4205 class'/>
<meta name='createdcontent='22 April 2009'/>
<meta name='modifiedcontent='20 October 2019'/>
<meta name='referencecontent=' https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter11LightingEnvironment/BackgroundSelector.x3d '/>
<meta name='subjectcontent='X3D Background example'/>
<meta name='identifiercontent=' https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter11LightingEnvironmentalEffects/BackgroundTimeOfDay.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: AnimatedBackground, BackgroundColorInterpolator, TimeOfDayClock
-->
<Scene>
<WorldInfo title='BackgroundTimeOfDay.x3d'/>
<Transform translation='0 3 0'>
<Shape>
<Text string='"Background Time Of Day"'>
<FontStyle justify='"MIDDLE" "MIDDLE"'/>
</Text>
<Appearance>
<Material/>
</Appearance>
</Shape>
</Transform>
<!-- ROUTE information for TimeOfDayClock node:  [from fraction_changed to BackgroundColorInterpolator.set_fraction ] -->
<TimeSensor DEF='TimeOfDayClockcycleInterval='6loop='true'/>

<field name='set_fractiontype='SFFloataccessType='inputOnly'/>
<field name='groundColorSunrisetype='MFColorvalue='0.133 0.419 0 0.36 0.1 0 1 0.74 0.4 1 0.74 0.4 0 0.2 0.75 0 0.1 0.5accessType='initializeOnly'/>
<field name='groundColorNoontype='MFColorvalue='0.133 0.419 0 0.36 0.1 0 1 0.74 0.4 1 0.74 0.4 0 0 0.5 0 0 0.2accessType='initializeOnly'/>
<field name='groundColorSunsettype='MFColorvalue='0.133 0.419 0 0.36 0.1 0 1 0.74 0.4 1 0.74 0.4 0 0 0.5 0 0 0.2accessType='initializeOnly'/>
<field name='groundColorNighttype='MFColorvalue='0.133 0.419 0 0.36 0.1 0 1 0.74 0.4 1 0.74 0.4 0 0 0.5 0 0 0.2accessType='initializeOnly'/>
<field name='skyColorSunrisetype='MFColorvalue='1 1 0.2 1 1 0 0.36 0.63 1 0 0.4 1 0 0.4 1accessType='initializeOnly'/>
<field name='skyColorNoontype='MFColorvalue='0 0.035 0.34 0 0.015 0.44 0 0.05 0.5 0 0.1 0.6 0.44 0.8 1 1 1 0.7accessType='initializeOnly'/>
<field name='skyColorSunsettype='MFColorvalue='0 0 0.38 0 0 0.68 0.5 0.2 1 0.5 0.2 1 1 0.3 0 1 0.2 0.8accessType='initializeOnly'/>
<field name='skyColorNighttype='MFColorvalue='1 1 1 0.8 0.8 0.8 0.1 0.1 0.1 0 0 0 0 0 0accessType='initializeOnly'/>
<field name='groundColor_changedtype='MFColoraccessType='outputOnly'/>
<field name='skyColor_changedtype='MFColoraccessType='outputOnly'/>
<![CDATA[
      
ecmascript:

function initialize()
{
    Browser.println ('groundColorSunrise length=' + groundColorSunrise.length + ' '  + groundColorSunrise.toString());
    Browser.println ('groundColorNoon    length=' +    groundColorNoon.length +    ' '  + groundColorNoon.toString());
    Browser.println ('groundColorSunset  length=' +  groundColorSunset.length +  ' '  + groundColorSunset.toString());
    Browser.println ('groundColorNight   length=' +   groundColorNight.length +   ' '  + groundColorNight.toString());
    Browser.println ('skyColorSunrise    length=' +    skyColorSunrise.length +    ' '  + skyColorSunrise.toString());
    Browser.println ('skyColorNoon       length=' +       skyColorNoon.length +       ' '  + skyColorNoon.toString());
    Browser.println ('skyColorSunset     length=' +     skyColorSunset.length +     ' '  + skyColorSunset.toString());
    Browser.println ('skyColorNight      length=' +      skyColorNight.length +      ' '  + skyColorNight.toString());
}
function set_fraction (fraction) // fraction is input value sent by TimeSensor clock
{
    // Sunrise to Noon, fraction 0.0 to 0.25, interval=0.25
    if      (fraction < 0.25)
    {
        groundColor_changed = interpolate (groundColorSunrise, groundColorNoon, fraction, 0.00, 0.25);
           skyColor_changed = interpolate (   skyColorSunrise,    skyColorNoon, fraction, 0.00, 0.25);
    }
    // Noon to Evening, fraction 0.25 to 0.5, interval=0.25
    else if (fraction < 0.5)
    {
        groundColor_changed = interpolate (groundColorNoon, groundColorSunset, fraction, 0.25, 0.25);
           skyColor_changed = interpolate (   skyColorNoon,    skyColorSunset, fraction, 0.25, 0.25);
    }
    // Evening to Night, fraction 0.5 to 0.6, interval=0.1
    else if (fraction < 0.6)
    {
        groundColor_changed = interpolate (groundColorSunset, groundColorNight, fraction, 0.5, 0.1);
           skyColor_changed = interpolate (   skyColorSunset,    skyColorNight, fraction, 0.5, 0.1);
    }
    // Night (unchanging), fraction 0.6 to 0.95, interval=0.35
    else if (fraction < 0.95)
    {
        groundColor_changed = groundColorNight;
           skyColor_changed = skyColorNight;
    }
    // Night to Sunrise, fraction 0.95 to 1.0, interval=0.05
    else // (fraction < 1.0)
    {
        groundColor_changed = interpolate (groundColorNight, groundColorSunrise, fraction, 0.95, 0.05);
           skyColor_changed = interpolate (   skyColorNight,    skyColorSunrise, fraction, 0.95, 0.05);
    }
//    Browser.println ('groundColor_changed=' + groundColor_changed.toString());
//    Browser.println ('   skyColor_changed=' +    skyColor_changed.toString());
}
function interpolate (firstColorArray, secondColorArray, fraction, initialFraction, interval)
{
    f = (fraction - initialFraction) / interval; // f should range from 0 to 1
//  Browser.println ('initialFraction=' + initialFraction + ', fraction=' + fraction + ', f=' + f);

    color0 = firstColorArray[0] + (secondColorArray[0] - firstColorArray[0]) * f;
    color1 = firstColorArray[1] + (secondColorArray[1] - firstColorArray[1]) * f;
    color2 = firstColorArray[2] + (secondColorArray[2] - firstColorArray[2]) * f;
    color3 = firstColorArray[3] + (secondColorArray[3] - firstColorArray[3]) * f;
    color4 = firstColorArray[4] + (secondColorArray[4] - firstColorArray[4]) * f;
    return new MFColor (color0, color1, color2, color3, color4);
}

    
]]>
</Script>
< ROUTE  fromNode=' TimeOfDayClock' fromField='fraction_changed' toNode=' BackgroundColorInterpolator' toField='set_fraction'/>
<!-- ROUTE information for AnimatedBackground node:  [from BackgroundColorInterpolator.groundColor_changed to groundColor ] [from BackgroundColorInterpolator.skyColor_changed to skyColor ] -->
<Background DEF='AnimatedBackgroundgroundAngle='0.03 1.26 1.5 1.57groundColor='0.133333 0.419608 0 0.36 0.1 0 1 0.74 0.4 0 0 0.5 0 0 0.2skyAngle='0.03 0.05 1.5 1.57skyColor='1 1 1 0.8 0.8 0.8 0.1 0.1 0.1 0 0 0 0 0 0'/>

< ROUTE  fromNode=' BackgroundColorInterpolator' fromField='groundColor_changed' toNode=' AnimatedBackground' toField='groundColor'/>
< ROUTE  fromNode=' BackgroundColorInterpolator' fromField='skyColor_changed' toNode=' AnimatedBackground' toField='skyColor'/>
</Scene>
</X3D>
<!--

to top <!-- Event Graph ROUTE Table shows event connections -->
 
<!-- Index for DEF nodes: AnimatedBackground, BackgroundColorInterpolator, TimeOfDayClock
-->

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

TimeOfDayClock
TimeSensor
fraction_changed
SFFloat

ROUTE
event to
(1)
BackgroundColorInterpolator
Script
set_fraction
SFFloat

 
 
then
BackgroundColorInterpolator
Script
groundColor_changed
MFColor

ROUTE
event to
(2)
AnimatedBackground
Background
groundColor
MFColor
 
 
 
then
BackgroundColorInterpolator
Script
skyColor_changed
MFColor

ROUTE
event to
(2)
AnimatedBackground
Background
skyColor
MFColor


      BackgroundColorInterpolator
Script
groundColor_changed
MFColor

ROUTE
event to
(1)
AnimatedBackground
Background
groundColor
MFColor
BackgroundColorInterpolator
Script
skyColor_changed
MFColor

ROUTE
event to
(1)
AnimatedBackground
Background
skyColor
MFColor


-->

<!-- Online at
https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter11LightingEnvironmentalEffects/BackgroundTimeOfDayIndex.html -->
<!-- Version control at
https://sourceforge.net/p/x3d/code/HEAD/tree/www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter11LightingEnvironmentalEffects/BackgroundTimeOfDay.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)
-->

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