5
5
"cobweb/Basic/X3DFieldDefinition",
6
6
"cobweb/Basic/FieldDefinitionArray",
7
7
"cobweb/Components/ParticleSystems/X3DParticleEmitterNode",
8
"cobweb/Components/Rendering/IndexedLineSet",
8
9
"cobweb/Bits/X3DConstants",
10
"standard/Math/Numbers/Vector3",
11
"standard/Math/Algorithm",
12
15
X3DFieldDefinition,
13
16
FieldDefinitionArray,
14
X3DParticleEmitterNode,
17
X3DParticleEmitterNode,
25
var vector = new Vector3 (0, 0, 0);
19
27
function PolylineEmitter (executionContext)
21
29
X3DParticleEmitterNode .call (this, executionContext);
23
31
this .addType (X3DConstants .PolylineEmitter);
33
this .direction = new Vector3 (0, 0, 0);
34
this .polylineNode = new IndexedLineSet (executionContext);
35
this .polylines = [ ];
36
this .lengthSoFarArray = [ 0 ];
26
39
PolylineEmitter .prototype = $.extend (Object .create (X3DParticleEmitterNode .prototype),
64
initialize: function ()
66
X3DParticleEmitterNode .prototype .initialize .call (this);
68
this .direction_ .addInterest (this, "set_direction__");
70
this .coordIndex_ .addFieldInterest (this .polylineNode .coordIndex_);
71
this .coord_ .addFieldInterest (this .polylineNode .coord_);
73
this .polylineNode .coordIndex_ = this .coordIndex_;
74
this .polylineNode .coord_ = this .coord_;
76
this .polylineNode .addInterest (this, "set_polyline");
77
this .polylineNode .setup ();
79
this .set_direction__ ();
80
this .set_polyline ();
82
set_direction__: function ()
84
this .direction .assign (this .direction_ .getValue ()) .normalize ();
86
if (this .direction .equals (Vector3 .Zero))
87
this .getRandomVelocity = this .getSphericalRandomVelocity;
89
delete this .getRandomVelocity;
91
set_polyline: function ()
93
var polylines = this .polylineNode .getPolylines (this .polylines);
95
if (polylines .length)
97
delete this .getRandomPosition;
101
lengthSoFarArray = this .lengthSoFarArray;
103
lengthSoFarArray .length = 1;
105
for (var i = 0, length = polylines .length; i < length; i += 2)
107
lengthSoFar += vector .assign (polylines [i + 1]) .subtract (polylines [i]) .abs ();
108
lengthSoFarArray .push (lengthSoFar);
113
this .getRandomPosition = getPosition;
116
getRandomPosition: function (position)
118
// Determine index0 and weight.
121
lengthSoFarArray = this .lengthSoFarArray,
122
length = lengthSoFarArray .length,
123
fraction = Math .random () * lengthSoFarArray [length - 1],
128
if (length == 1 || fraction <= lengthSoFarArray [0])
133
else if (fraction >= lengthSoFarArray [length - 1])
140
var index = Algorithm .upperBound (lengthSoFarArray, 0, length, fraction, Algorithm .less);
148
key0 = lengthSoFarArray [index0],
149
key1 = lengthSoFarArray [index1];
151
weight = Algorithm .clamp ((fraction - key0) / (key1 - key0), 0, 1);
160
// Interpolate and set position.
166
vertex1 = this .polylines [index0],
167
vertex2 = this .polylines [index1];
169
position .x = vertex1 .x + weight * (vertex2 .x - vertex1 .x);
170
position .y = vertex1 .y + weight * (vertex2 .y - vertex1 .y);
171
position .z = vertex1 .z + weight * (vertex2 .z - vertex1 .z);
172
position .w = vertex1 .w + weight * (vertex2 .w - vertex1 .w);
176
getRandomVelocity: function (velocity)
179
direction = this .direction,
180
speed = this .getRandomSpeed ();
182
velocity .x = direction .x * speed;
183
velocity .y = direction .y * speed;
184
velocity .z = direction .z * speed;
190
function getPosition (position)
192
return this .position .set (0, 0, 0);
53
195
return PolylineEmitter;