<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "https://www.web3d.org/specifications/x3d-3.0.dtd">
<X3D profile='Immersive' version='3.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='https://www.web3d.org/specifications/x3d-3.0.xsd'>
  <head>
    <meta content='ViewPositionOrientationPrototype.x3d' name='title'/>
    <meta content='Leonard Daly and Don Brutzman' name='creator'/>
    <meta content='Don Brutzman' name='translator'/>
    <meta content='1 November 2000' name='created'/>
    <meta content='28 November 2019' name='modified'/>
    <meta content='ViewPositionOrientation prototype provides local position and orientation as user navigates, with optional console output' name='description'/>
    <meta content='Note fix: metadata is no longer an allowed ProtoDeclare field name, since ProtoInstance already includes a metadata field' name='info'/>
    <meta content='ViewPositionOrientationExample.x3d' name='reference'/>
    <meta content='https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter14Prototypes/WhereAmIExample.x3d' name='reference'/>
    <meta content='https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter14Prototypes/WhereAmIPrototype.x3d' name='reference'/>
    <meta content='http://www.realism.com/Web3D/Examples#WhereAmI' name='reference'/>
    <meta content='http://www.realism.com/vrml/Example/WhereAmI/WhereAmI_Proto.wrl' name='reference'/>
    <meta content='https://www.web3d.org/x3d/content/examples/Savage/Tools/Authoring/ViewPositionOrientationPrototype.x3d' name='identifier'/>
    <meta content='X3D-Edit 3.3, https://www.web3d.org/x3d/tools/X3D-Edit' name='generator'/>
    <meta content='../../license.html' name='license'/>
  </head>
  <Scene>
    <!-- Thanks to Leonard Daly for the original design of this prototype -->
    <WorldInfo title='ViewPositionOrientationPrototype.x3d'/>
    <ProtoDeclare appinfo='ViewPositionOrientation provides provides console output of local position and orientation as user navigates' name='ViewPositionOrientation'>
      <ProtoInterface>
        <field accessType='inputOutput' appinfo='Whether or not ViewPositionOrientation sends output to console' name='enabled' type='SFBool' value='true'/>
        <field accessType='initializeOnly' appinfo='Output internal trace messages for debugging this node, intended for developer use only' name='traceEnabled' type='SFBool' value='true'/>
        <field accessType='inputOnly' appinfo='Ability to turn output tracing on/off at runtime' name='set_traceEnabled' type='SFBool'/>
        <field accessType='outputOnly' appinfo='Output local position' name='position_changed' type='SFVec3f'/>
        <field accessType='outputOnly' appinfo='Output local orientation' name='orientation_changed' type='SFRotation'/>
        <field accessType='outputOnly' appinfo='MFString value of new Viewpoint, suitable for use in string field of a Text node' name='outputViewpointString' type='MFString'/>
      </ProtoInterface>
      <ProtoBody>
        <ProximitySensor DEF='WhereSensor' size='1000000000 1000000000 1000000000'>
          <IS>
            <connect nodeField='enabled' protoField='enabled'/>
          </IS>
        </ProximitySensor>
        <!-- it's a big old world out there! large box likely handles most use cases. -->
        <Script DEF='OutputPositionOrientation'>
          <field accessType='initializeOnly' name='traceEnabled' type='SFBool'/>
          <field accessType='inputOnly' name='set_traceEnabled' type='SFBool'/>
          <field accessType='inputOnly' name='set_position' type='SFVec3f'/>
          <field accessType='inputOnly' name='set_orientation' type='SFRotation'/>
          <field accessType='initializeOnly' name='position' type='SFVec3f' value='0 0 0'/>
          <field accessType='initializeOnly' name='orientation' type='SFRotation' value='0 1 0 0'/>
          <field accessType='outputOnly' name='position_changed' type='SFVec3f'/>
          <field accessType='outputOnly' name='orientation_changed' type='SFRotation'/>
          <field accessType='outputOnly' name='outputViewpointString' type='MFString'/>
          <IS>
            <connect nodeField='traceEnabled' protoField='traceEnabled'/>
            <connect nodeField='set_traceEnabled' protoField='set_traceEnabled'/>
            <connect nodeField='position_changed' protoField='position_changed'/>
            <connect nodeField='orientation_changed' protoField='orientation_changed'/>
            <connect nodeField='outputViewpointString' protoField='outputViewpointString'/>
          </IS>
          <![CDATA[
ecmascript:

function roundoff (value, digits) // for local use only
{
	var resolution = 1;
	for (i = 1; i <= digits; i++ )
	{
		resolution *= 10;
	}
	return Math.round (value*resolution) / resolution; // round to resolution
}
function outputViewpoint ()
{
  var holdString = 
        '<Viewpoint position=\"' +
		roundoff (position.x, 1) + ' ' +
		roundoff (position.y, 1) + ' ' +
		roundoff (position.z, 1) +
        '\" orientation=\"' +
		roundoff (orientation.x, 3) + ' ' +
		roundoff (orientation.y, 3) + ' ' +
		roundoff (orientation.z, 3) + ' ' +
		roundoff (orientation.angle, 4) + '\"/>' ;
   tracePrint (holdString);
   outputViewpointString = new MFString (holdString);
}
function set_position (value)
{
	position = value; // save persistent value
	position_changed = position; // output event
	outputViewpoint ();
}
function set_orientation (value)
{
	orientation = value; // save persistent value
	orientation_changed = orientation; // output event
	outputViewpoint ();
}
function set_traceEnabled (value)
{
	traceEnabled = value;
	alwaysPrint ('traceEnabled=' + traceEnabled);
}
function alwaysPrint (text)
{
	Browser.println ('[ViewPositionOrientation] ' + text);
}
function tracePrint (text)
{
	if (traceEnabled) alwaysPrint (text);
}
]]>
        </Script>
        <ROUTE fromField='position_changed' fromNode='WhereSensor' toField='set_position' toNode='OutputPositionOrientation'/>
        <ROUTE fromField='orientation_changed' fromNode='WhereSensor' toField='set_orientation' toNode='OutputPositionOrientation'/>
      </ProtoBody>
    </ProtoDeclare>
    <!-- ==================== -->
    <Viewpoint description='ViewPositionOrientation prototype' position='0 0 14'/>
    <Anchor description='ViewPositionOrientation Example' parameter='"target=_blank"' url='"ViewPositionOrientationExample.x3d" "https://www.web3d.org/x3d/content/examples/Savage/Tools/Authoring/ViewPositionOrientationExample.x3d" "ViewPositionOrientationExample.wrl" "https://www.web3d.org/x3d/content/examples/Savage/Tools/Authoring/ViewPositionOrientationExample.wrl"'>
      <Shape>
        <Text string='"ViewPositionOrientationPrototype" "is a prototype definition file" "" "Click this text to see" "ViewPositionOrientationExample"'>
          <FontStyle justify='"MIDDLE" "MIDDLE"' size='1.2'/>
        </Text>
        <Appearance>
          <Material diffuseColor='0.6 0.8 0.4'/>
        </Appearance>
      </Shape>
      <!-- Selectable Text design pattern has transparent Box and TouchSensor description as a tooltip -->
      <Shape>
        <!-- Author TODO: to adjust transparent Box as text-selection assist, set width and height to match size, then set transparency='1' to make invisible. -->
        <Box size='16 7 .001'/>
        <Appearance>
          <Material transparency='1'/>
        </Appearance>
      </Shape>
    </Anchor>
  </Scene>
</X3D>