proto_sheep_00.x3d source
<?xml version="1.0" encoding="UTF-8"?>
<X3D>
<Scene>
<ProtoDeclare name='Sheep'>
<ProtoInterface>
<field accessType='inputOnly' type='MFFloat' name='packet_in'/>
<field accessType='outputOnly' type='MFFloat' name='packet_out'/>
</ProtoInterface>
<ProtoBody>
<!-- Sheep -->
<Transform DEF='Move-Tf'>
<Transform DEF='Action-Tf' translation='0 1.1 0'>
<!-- body -->
<Shape>
<Appearance DEF='Body-Ap'>
<Material diffuseColor='1 1 0.8'/>
</Appearance>
<Sphere/>
</Shape>

<!-- head -->
<Transform DEF='Head-Tf' rotation='1 0 0 0'>
<Transform translation='0 0.2 1' rotation='-1 0 0 0.5' scale='0.7 0.9 0.7'>
<Shape>
<Appearance DEF='Haed-Ap'>
<Material diffuseColor='0.3 0.2 0.2'/>
</Appearance>
<Sphere radius='0.7'/>
</Shape>

<!-- left ear -->
<Transform DEF='YearL' translation='0.6 0.4 0.1' scale='2 0.7 1'>
<Shape DEF='Year-Sp'>
<Appearance USE='Haed-Ap'/>
<Sphere radius='0.2'/>
</Shape>
</Transform>

<!-- right ear -->
<Transform DEF='YearR' translation='-0.6 0.4 0.1' scale='2 0.7 1'>
<Shape USE='Year-Sp'/>
</Transform>

<!-- left eye -->
<Transform DEF='EyeL' translation='0.35 0.25 0.5'>
<Shape DEF='Eye-Sp'>
<Appearance>
<Material diffuseColor='0 0 0.001'/>
</Appearance>
<Sphere radius='0.1'/>
</Shape>
</Transform>

<!-- right eye -->
<Transform DEF='EyeR' translation='-0.35 0.25 0.5'>
<Shape USE='Eye-Sp'/>
</Transform>
</Transform>
</Transform>

<!-- left forefoot -->
<Transform DEF='FootFL-Tf' rotation='1 0 -1 0.8'>
<Transform translation='0.75 0 0.75'>
<Shape DEF='Foot-Sp'>
<Appearance USE='Haed-Ap'/>
<Sphere radius='0.3'/>
</Shape>
</Transform>
</Transform>

<!-- right forefoot -->
<Transform DEF='FootFR-Tf' rotation='1 0 1 0.8'>
<Transform translation='-0.75 0 0.75'>
<Shape USE='Foot-Sp'/>
</Transform>
</Transform>

<!-- left hind leg -->
<Transform DEF='FootRL-Tf' rotation='-1 0 -1 0.8'>
<Transform translation='0.75 0 -0.75'>
<Shape USE='Foot-Sp'/>
</Transform>
</Transform>

<!-- right hind leg -->
<Transform DEF='FootRR-Tf' rotation='-1 0 1 0.8'>
<Transform translation='-0.75 0 -0.75'>
<Shape USE='Foot-Sp'/>
</Transform>
</Transform>

<!-- Tail -->
<Transform DEF='Tail-Tf' rotation='1 0 0 0.3'>
<Transform translation='0 0 -1'>
<Shape DEF='Foot-Sp'>
<Appearance USE='Body-Ap'/>
<Sphere radius='0.2'/>
</Shape>
</Transform>
</Transform>
</Transform>

<!-- Shadow of this sheep -->
<Transform DEF='Shadow-Tf' translation='-0.1 0 -0.1' scale='1 0.01 1'>
<Shape>
<Appearance DEF='Shadow-Ap'>
<Material diffuseColor='0 0 0.001' transparency='0.5'/>
</Appearance>
<Sphere radius='1.05'/>
</Shape>
</Transform>

<Viewpoint DEF='Vp' position='5 2.5 5' orientation='0 1 0 0.785'/>
<TouchSensor DEF='ThS'/>
</Transform>

<!-- viewpoint script -->
<Script DEF='View-Sc'>
<field accessType='inputOnly' type='SFTime' name='touchTime'/>
<field accessType='outputOnly' type='SFBool' name='set_bind'/>
<![CDATA[ecmascript:
function touchTime () {

set_bind = TRUE;

}
]]>
</Script>
<ROUTE fromNode='ThS' fromField='touchTime' toNode='View-Sc' toField='touchTime'/>
<ROUTE fromNode='View-Sc' fromField='set_bind' toNode='Vp' toField='set_bind'/>

<!-- legs angle script -->
<Script DEF='Foot-Sc' directOutput='TRUE'>
<field accessType='inputOnly' type='SFFloat' name='set_value'/>
<field accessType='initializeOnly' type='MFNode' name='Foots'>
<Transform USE='FootFL-Tf'/>
<Transform USE='FootFR-Tf'/>
<Transform USE='FootRL-Tf'/>
<Transform USE='FootRR-Tf'/>
</field>
<![CDATA[ecmascript:
function set_value (f) {

for ( var i = 0; i < 4; i++ ) Foots[i].rotation[3] = f;

}
]]>
</Script>

<!-- shadow scale script -->
<Script DEF='Shadow-Sc'>
<field accessType='inputOnly' type='SFVec3f' name='set_translation'/>
<field accessType='outputOnly' type='SFVec3f' name='scale'/>
<![CDATA[ecmascript:
function set_translation (vec) {

var s = 1 / (vec.y - 0.1);
scale = new SFVec3f ( s, 0.01, s );

}
]]>
</Script>
<ROUTE fromNode='Action-Tf' fromField='translation' toNode='Shadow-Sc' toField='set_translation'/>
<ROUTE fromNode='Shadow-Sc' fromField='scale' toNode='Shadow-Tf' toField='scale'/>

<!-- jump animation -->
<TimeSensor DEF='Jump-TiS' cycleInterval='3'/>
<PositionInterpolator DEF='Jump-Body-PI'
key='0 0.1 0.5 0.9 1'
keyValue='
0 1.1 0
0 1 0
0 2.6 0
0 1 0
0 1.1 0'
/>
<ROUTE fromNode='Jump-TiS' fromField='fraction_changed' toNode='Jump-Body-PI' toField='set_fraction'/>
<ROUTE fromNode='Jump-Body-PI' fromField='value_changed' toNode='Action-Tf' toField='translation'/>
<ScalarInterpolator DEF='Jump-Foot-SI'
key='0 0.1 0.2 0.5 0.7 0.9 1'
keyValue='0.8 0.5 1.2 0.2 0.2 0.9 0.8'
/>
<ROUTE fromNode='Jump-TiS' fromField='fraction_changed' toNode='Jump-Foot-SI' toField='set_fraction'/>
<ROUTE fromNode='Jump-Foot-SI' fromField='value_changed' toNode='Foot-Sc' toField='set_value'/>
<OrientationInterpolator DEF='Jump-Head-OI'
key='0 0.1 0.5 0.7 0.9 1'
keyValue='
1 0 0 0
1 0 0 0.2
1 0 0 -0.3
1 0 0 0.2
1 0 0 0.3
1 0 0 0'
/>
<ROUTE fromNode='Jump-TiS' fromField='fraction_changed' toNode='Jump-Head-OI' toField='set_fraction'/>
<ROUTE fromNode='Jump-Head-OI' fromField='value_changed' toNode='Head-Tf' toField='rotation'/>
<PositionInterpolator DEF='Jump-Scale-PI'
key='0 0.9 0.95 1'
keyValue='
1 1 1
1 1 1
1.1 0.8 1
1 1 1'
/>
<ROUTE fromNode='Jump-TiS' fromField='fraction_changed' toNode='Jump-Scale-PI' toField='set_fraction'/>
<ROUTE fromNode='Jump-Scale-PI' fromField='value_changed' toNode='Move-Tf' toField='scale'/>
<PositionInterpolator DEF='Jump-Position-PI'
key='0 1'
/>
<ROUTE fromNode='Jump-TiS' fromField='fraction_changed' toNode='Jump-Position-PI' toField='set_fraction'/>
<ROUTE fromNode='Jump-Position-PI' fromField='value_changed' toNode='Move-Tf' toField='translation'/>

<!-- rotate animation -->
<TimeSensor DEF='Rot-TiS' cycleInterval='2'/>
<OrientationInterpolator DEF='Rot-OI'
key='0 1'
/>

<ROUTE fromNode='Rot-TiS' fromField='fraction_changed' toNode='Rot-OI' toField='set_fraction'/>
<ROUTE fromNode='Rot-OI' fromField='value_changed' toNode='Move-Tf' toField='rotation'/>
<TimeSensor DEF='Eat-TiS' cycleInterval='1.5'/>
<OrientationInterpolator DEF='Eat-Head-OI'
key='0 0.2 0.5 0.7 1'
keyValue='
1 0 0 0
1 0 0 0.6
1 0 0 0.4
1 0 0 0.6
1 0 0 0'
/>

<!-- eat animation -->
<ROUTE fromNode='Eat-TiS' fromField='fraction_changed' toNode='Eat-Head-OI' toField='set_fraction'/>
<ROUTE fromNode='Eat-Head-OI' fromField='value_changed' toNode='Head-Tf' toField='rotation'/>
<PositionInterpolator DEF='Eat-Body-PI'
key='0 0.2 0.5 0.7 1'
keyValue='
0 1.1 0
0 1 0
0 1.1 0
0 1 0
0 1.1 0'
/>
<ROUTE fromNode='Eat-TiS' fromField='fraction_changed' toNode='Eat-Body-PI' toField='set_fraction'/>
<ROUTE fromNode='Eat-Body-PI' fromField='value_changed' toNode='Action-Tf' toField='translation'/>
<PositionInterpolator DEF='EatPos-PI'
key='0 0.25 0.5 0.75 1'
/>
<ROUTE fromNode='Eat-TiS' fromField='fraction_changed' toNode='EatPos-PI' toField='set_fraction'/>
<ROUTE fromNode='EatPos-PI' fromField='value_changed' toNode='Move-Tf' toField='translation'/>
<OrientationInterpolator DEF='EatRot-OI'
key='0 0.25 0.5 0.75 1'
/>
<ROUTE fromNode='Eat-TiS' fromField='fraction_changed' toNode='EatRot-OI' toField='set_fraction'/>
<ROUTE fromNode='EatRot-OI' fromField='value_changed' toNode='Move-Tf' toField='rotation'/>

<!-- actions script -->
<TimeSensor DEF='Action-TiS' loop='TRUE'/>

<Script DEF='Action-Sc' directOutput='TRUE'>
<field accessType='inputOnly' type='MFFloat' name='receivePacket'/>
<field accessType='inputOnly' type='SFTime' name='startAction'/>
<field accessType='inputOnly' type='SFTime' name='transmitPacket'/>
<field accessType='outputOnly' type='MFVec3f' name='keyValueJumpPosition'/>
<field accessType='outputOnly' type='MFRotation' name='keyValueRotation'/>
<field accessType='outputOnly' type='MFVec3f' name='keyValueEatPosition'/>
<field accessType='outputOnly' type='MFRotation' name='keyValueEatRotation'/>
<field accessType='outputOnly' type='MFFloat' name='packet_out'/>
<field accessType='initializeOnly' type='SFInt32' name='thisId' value='-1'/>
<field accessType='initializeOnly' type='SFNode' name='thisSheep'>
<Transform USE='Move-Tf'/>
</field>
<field accessType='initializeOnly' type='MFNode' name='TimeSensors'>
<TimeSensor USE='Jump-TiS'/>
<TimeSensor USE='Rot-TiS'/>
<TimeSensor USE='Eat-TiS'/>
</field>
<field accessType='initializeOnly' type='SFVec3f' name='targetPos' value='0 -99 0'/>
<field accessType='initializeOnly' type='SFVec3f' name='initDirection' value='0 0 1'/>
<field accessType='initializeOnly' type='SFInt32' name='nearestId' value='-1'/>
<field accessType='initializeOnly' type='SFVec3f' name='nearestPos' value='0 0 0'/>
<IS>
<connect nodeField='receivePacket' protoField='packet_in'/>
<connect nodeField='packet_out' protoField='packet_out'/>
</IS>
<![CDATA[ecmascript:

function initialize () {

// (1-1)
thisSheep.rotation.setAxis( new SFVec3f ( 0, 1, 0 ) );

}

function receivePacket (p) {

var receiveId = p[0];
var receivePos = new SFVec3f ( p[1], p[2], p[3] );

// (2-1)
if ( thisId < 0 ) {

thisId = receiveId;
thisSheep.translation = receivePos;
thisSheep.rotation[3] = Math.random() * 2 * Math.PI;
return;

}

// (2-2)
var thisPos = thisSheep.translation;

// (2-2-1)
if ( nearestId < 0 ) {

nearestId = receiveId;
nearestPos = receivePos;

}
// (2-2-2)
else if ( nearestId == receiveId ) {

nearestPos = receivePos;

}
else {

var receive2thisLength = thisPos.subtract( receivePos ).length();
var nearest2thisLength = thisPos.subtract( nearestPos ).length();

if ( receive2thisLength < nearest2thisLength ) {

nearestId = receiveId;
nearestPos = receivePos;

}
else return;
}

// (2-3)
targetPos = thisPos.subtract( nearestPos ).normalize().multiply( 7.0 ).add( nearestPos );

}

function startAction (t) {

// (3-1)
if ( thisId < 0 ) return;

// (3-2)
for ( var i=0; i<TimeSensors.length; i++) if ( TimeSensors[i].isActive ) return;

// (3-3)
var thisPos = thisSheep.translation;
var thisRot = thisSheep.rotation;
var vecThis2Target = targetPos.subtract( thisPos );
var thisDirection = thisRot.multVec( initDirection );

if ( targetPos.y == -99 || vecThis2Target.length() < ( Math.random() * 1.0 + 2.0 ) ) {

var p0 = new MFVec3f();
p0[0] = p0[1] = p0[2] = p0[3] = thisPos;
p0[4] = thisDirection.multiply( Math.random() * 0.2 + 0.1 ).add( thisPos );

var newAngle = thisRot.angle + Math.random() - 0.5;
var axis = thisRot.getAxis();
var newRot = new SFRotation ( axis, newAngle );

var r0 = new MFRotation();
r0[0] = r0[1] = r0[2] = thisRot;
r0[3] = r0[4] = newRot;

keyValueEatPosition = p0;
keyValueEatRotation = r0;

TimeSensors[2].startTime = t + Math.random() * 0.5;

return;
}

// (3-4)
var cosDirAndTarget = thisDirection.dot( vecThis2Target.normalize() );

if ( cosDirAndTarget < 0.9 ) {

var r0 = new MFRotation();
r0[0] = thisRot;
r0[1] = new SFRotation ( initDirection, vecThis2Target );
keyValueRotation = r0;

TimeSensors[1].startTime = t;

}
else {

var p0 = new MFVec3f();
p0[0] = thisPos;
p0[1] = thisDirection.multiply( 3.0 ).add( thisPos );
keyValueJumpPosition = p0;

TimeSensors[0].startTime = t;

}

}

function transmitPacket () {

if ( thisId < 0 ) return;

var packet = new MFFloat();
packet[0] = thisId;
packet[1] = thisSheep.translation[0];
packet[2] = thisSheep.translation[1];
packet[3] = thisSheep.translation[2];
packet_out = packet;

}
]]>
</Script>
<ROUTE fromNode='Action-TiS' fromField='cycleTime' toNode='Action-Sc' toField='startAction'/>
<ROUTE fromNode='Jump-TiS' fromField='cycleTime' toNode='Action-Sc' toField='transmitPacket'/>
<ROUTE fromNode='Eat-TiS' fromField='cycleTime' toNode='Action-Sc' toField='transmitPacket'/>
<ROUTE fromNode='Action-Sc' fromField='keyValueJumpPosition' toNode='Jump-Position-PI' toField='keyValue'/>
<ROUTE fromNode='Action-Sc' fromField='keyValueRotation' toNode='Rot-OI' toField='keyValue'/>
<ROUTE fromNode='Action-Sc' fromField='keyValueEatPosition' toNode='EatPos-PI' toField='keyValue'/>
<ROUTE fromNode='Action-Sc' fromField='keyValueEatRotation' toNode='EatRot-OI' toField='keyValue'/>
</ProtoBody>
</ProtoDeclare>

<!-- Sheep instance -->
<ProtoInstance name='Sheep' containerField='children'/>

</Scene>
</X3D>