<?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>
<!-- javascript code for rotation calculations was derived from: -->
<meta name='titlecontent='ChangingFog.x3d'/>
<meta name='descriptioncontent="A Fog node that adjusts as the viewer's orientation and position changes. This is a good candidate to become a Prototype since Fog does not automatically bind when inlined."/>
<meta name='creatorcontent='Matthew Braun'/>
<meta name='createdcontent='20 September 2001'/>
<meta name='modifiedcontent='20 October 2019'/>
<meta name='referencecontent=' http://astronomy.swin.edu.au/pbourke/geometry/rotate/ '/>
<meta name='rightscontent='Copyright (c) Matthew Braun 2001'/>
<meta name='subjectcontent='Fog'/>
<meta name='identifiercontent=' https://X3dGraphics.com/examples/X3dForWebAuthors/KelpForestExhibit/ChangingFog.x3d '/>
<meta name='generatorcontent='X3D-Edit 3.3, https://savage.nps.edu/X3D-Edit'/>
<meta name='licensecontent=' ../license.html'/>
</head>
<!-- -->
<Scene>
<WorldInfo title='ChangingFog.x3d'/>
<NavigationInfo avatarSize='0.01 0 0'/>
<Viewpoint description='StartfieldOfView='0.9position='0 0 0'/>
<Viewpoint description='Looking up from startorientation='1 0 0 1.57position='0 0 0'/>
<Viewpoint description='10m above, looking straight uporientation='1 0 0 1.57position='0 10 0'/>
<Viewpoint description='10m above startposition='0 10 0'/>
<Viewpoint description='10m above, looking straight downorientation='1 0 0 -1.57position='0 10 0'/>
<Viewpoint description='10m below, looking downorientation='1 0 0 -1.57position='0 -10 0'/>
<Viewpoint description='10m below startposition='0 -10 0'/>
<Viewpoint description='10m below, looking uporientation='1 0 0 1.57position='0 -10 0'/>
<!-- ROUTE information for Water node:  [from ChangeVisibility.visibility_changed to visibilityRange ] -->
<Fog DEF='Watercolor='0.2 0.2 0.4fogType='EXPONENTIAL'/>
<!-- Proximity sensor must be large enough to encompass the entire scene -->
<!-- ROUTE information for ProxSensor node:  [from position_changed to ChangeVisibility.get_depth ] [from orientation_changed to ChangeVisibility.set_visibility ] -->
<ProximitySensor DEF='ProxSensorsize='1000 1000 1000'/>
<!-- TimeSensor triggering reduces frequency of calculations for performance reasons. -->
<!-- ROUTE information for Clock node:  [from cycleTime to ChangeVisibility.get_clock_hit ] -->
<TimeSensor DEF='Clockloop='true'/>
<!-- ROUTE information for ChangeVisibility node:  [from Clock.cycleTime to get_clock_hit ] [from ProxSensor.position_changed to get_depth ] [from ProxSensor.orientation_changed to set_visibility ] [from visibility_changed to Water.visibilityRange ] -->
<Script DEF='ChangeVisibility'>
<field name='get_clock_hittype='SFTimeaccessType='inputOnly'/>
<field name='run_scripttype='SFBoolvalue='falseaccessType='initializeOnly'/>
<field name='get_depthtype='SFVec3faccessType='inputOnly'/>
<field name='visibility_changedtype='SFFloataccessType='outputOnly'/>
<field name='set_visibilitytype='SFRotationaccessType='inputOnly'/>
<!-- <field accessType='initializeOnly' name='checked' type='SFBool' value='false'/> <field accessType='initializeOnly' name='moved' type='SFBool' value='false'/> -->
<![CDATA[
      
ecmascript:
// REF: http://astronomy.swin.edu.au/pbourke/geometry/rotate/

function initialize () {
   visibility = 20;
   depth = 0;
   pos = (0,0,0);
   Browser.print ('Position output from ProximitySensor.');
}

function get_clock_hit (clock_msg) {
     run_script = true;
}

function get_depth ( position ) {

   pos = position;
   depth = position[1] - 30;   

}

function set_visibility( rotation ) {

 if (run_script) {

//z coordinate of the default viewpoint direction(0,0,-1)
   initZ = -1;  

   rX = rotation[0];  // x coordinate of the rotation
   rY = rotation[1];  // y coordinate of the rotation
   rZ = rotation[2];  // z coordinate of the rotation

   theta = rotation[3];  // angle of rotation in radians
	
Browser.print ('theta:' + theta);
     
   cosTheta = Math.cos(theta);
   sinTheta = Math.sin(theta);

Browser.print ('cosTheta:' + cosTheta + ' sinTheta:'+ sinTheta);


// calculate the y coordinate of the point after rotation
/* there are 8 other terms in the full conversion, but 6 are equal
to zero because of the choice of a starting point on the z-axis. The
other two are not calculated since all we need is the y coordinate
*/
   finalY = ((1 - cosTheta) * rY * rZ - rX * sinTheta) * initZ;

Browser.print ('final y:' + finalY);

//calculate the elevation/depression angle of the final point location

   elevation = Math.asin(finalY);

Browser.print ('elevation:' + elevation);

   directionFactor = 1 + 0.2 * (4 * elevation / Math.PI);
   depthAdjust = (60 + depth)/60
   depthFactor = Math.max(depthAdjust,0.05);

   visibility_changed =  60 * depthFactor * directionFactor; 
   Browser.print ('depth=' + depth + ', elevation=' + elevation + 
        ', visibility_changed=' + visibility_changed);
   run_script = false;
   
 }
}

    
]]>
</Script>
<ROUTE fromNode='ClockfromField='cycleTimetoNode='ChangeVisibilitytoField='get_clock_hit'/>
<ROUTE fromNode='ProxSensorfromField='position_changedtoNode='ChangeVisibilitytoField='get_depth'/>
<ROUTE fromNode='ProxSensorfromField='orientation_changedtoNode='ChangeVisibilitytoField='set_visibility'/>
<ROUTE fromNode='ChangeVisibilityfromField='visibility_changedtoNode='WatertoField='visibilityRange'/>
<!-- A set of arrows is used to show visibility and direction -->
<Transform DEF='Pointertranslation='0 0 -15'>
<Transform translation='0 4 0'>
<Shape>
<Cone bottomRadius='0.4'/>
<Appearance DEF='ShapeApp'>
<Material ambientIntensity='0.8diffuseColor='1 1 0.3shininess='0.6'/>
</Appearance>
</Shape>
</Transform>
<Shape>
<Cylinder height='6radius='0.2'/>
<Appearance USE='ShapeApp'/>
</Shape>
</Transform>
<Transform translation='0 10 0'>
<Transform USE='Pointer'/>
</Transform>
<Transform translation='0 -10 0'>
<Transform USE='Pointer'/>
</Transform>
<!-- A pair of disks used to show visibility -->
<Transform translation='0 15 0'>
<Shape DEF='Disk'>
<Cylinder height='0.01'/>
<Appearance USE='ShapeApp'/>
</Shape>
</Transform>
<Transform translation='0 -15 0'>
<Shape USE='Disk'/>
</Transform>
<!-- An indexed face set box used to bound the working area -->
<Transform scale='20 20 20'>
<Shape DEF='IFSBox'>
<Appearance>
<Material diffuseColor='1 1 1'/>
</Appearance>
<IndexedFaceSet ccw='falsecolorPerVertex='falsecolorIndex='0 2 2 2 2 1coordIndex='0 1 2 3 -1 7 6 5 4 -1 0 4 5 1 -1 1 5 6 2 -1 2 6 7 3 -1 3 7 4 0'>
<Coordinate point='-1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 -1.0 -1.0 1.0 -1.0 -1.0 -1.0 1.0 1.0 -1.0 1.0 1.0 -1.0 -1.0 -1.0 -1.0 -1.0'/>
<Color color='1 1 1 0 0 0 0.2 0.2 0.8'/>
</IndexedFaceSet>
</Shape>
</Transform>
</Scene>
</X3D>
<!--

Index for DEF nodes : ChangeVisibility, Clock, Disk, IFSBox, Pointer, ProxSensor, ShapeApp, Water

Index for Viewpoint images : Viewpoint_1, Viewpoint_2, Viewpoint_3, Viewpoint_4, Viewpoint_5, Viewpoint_6, Viewpoint_7, Viewpoint_8
-->

<!-- 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)
-->

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