6
6
"cobweb/Basic/FieldDefinitionArray",
7
7
"cobweb/Components/ParticleSystems/X3DParticleEmitterNode",
8
8
"cobweb/Bits/X3DConstants",
10
"standard/Math/Geometry/Triangle3",
11
"standard/Math/Numbers/Vector3",
12
"standard/Math/Algorithm",
12
16
X3DFieldDefinition,
13
17
FieldDefinitionArray,
14
18
X3DParticleEmitterNode,
28
vertex1 = new Vector3 (0, 0, 0),
29
vertex2 = new Vector3 (0, 0, 0),
30
vertex3 = new Vector3 (0, 0, 0),
31
direction = new Vector3 (0, 0, 0);
19
33
function SurfaceEmitter (executionContext)
21
35
X3DParticleEmitterNode .call (this, executionContext);
23
37
this .addType (X3DConstants .SurfaceEmitter);
39
this .surfaceNode = null;
40
this .areaSoFarArray = [ 0 ];
26
43
SurfaceEmitter .prototype = $.extend (Object .create (X3DParticleEmitterNode .prototype),
66
initialize: function ()
68
X3DParticleEmitterNode .prototype .initialize .call (this);
70
this .surface_ .addInterest (this, "set_surface__");
72
this .set_surface__ ();
74
set_surface__: function ()
76
if (this .surfaceNode)
77
this .surfaceNode .removeInterest (this, "set_geometry__");
79
this .surfaceNode = X3DCast (X3DConstants .X3DGeometryNode, this .surface_);
81
if (this .surfaceNode)
82
this .surfaceNode .addInterest (this, "set_geometry__");
84
this .set_geometry__ ();
86
set_geometry__: function ()
88
if (this .surfaceNode)
90
delete this .getRandomPosition;
91
delete this .getRandomVelocity;
95
areaSoFarArray = this .areaSoFarArray,
96
vertices = this .surfaceNode .getVertices ();
98
areaSoFarArray .length = 1;
100
for (var i = 0, length = vertices .length; i < length; i += 12)
102
vertex1 .set (vertices [i + 0], vertices [i + 1], vertices [i + 2]);
103
vertex2 .set (vertices [i + 4], vertices [i + 5], vertices [i + 6]);
104
vertex3 .set (vertices [i + 8], vertices [i + 9], vertices [i + 10]);
106
areaSoFar += Triangle3 .area (vertex1, vertex2, vertex3);
107
areaSoFarArray .push (areaSoFar);
112
this .getRandomPosition = getPosition;
113
this .getRandomVelocity = this .getSphericalRandomVelocity;
115
direction .set (0, 0, 0);
118
getRandomPosition: function (position)
120
// Determine index0 and weight.
123
areaSoFarArray = this .areaSoFarArray,
124
length = areaSoFarArray .length,
125
fraction = Math .random () * areaSoFarArray [length - 1],
130
if (length == 1 || fraction <= areaSoFarArray [0])
135
else if (fraction >= areaSoFarArray [length - 1])
142
var index = Algorithm .upperBound (areaSoFarArray, 0, length, fraction, Algorithm .less);
150
key0 = areaSoFarArray [index0],
151
key1 = areaSoFarArray [index1];
153
weight = Algorithm .clamp ((fraction - key0) / (key1 - key0), 0, 1);
162
// Interpolate and set position.
166
vertices = this .surfaceNode .getVertices ();
168
// Random barycentric coordinates.
182
position .x = u * vertices [i + 0] + v * vertices [i + 4] + t * vertices [i + 8];
183
position .y = u * vertices [i + 1] + v * vertices [i + 5] + t * vertices [i + 9];
184
position .z = u * vertices [i + 2] + v * vertices [i + 6] + t * vertices [i + 10];
188
normals = this .surfaceNode .getNormals ();
190
direction .x = u * normals [i + 0] + v * normals [i + 3] + t * normals [i + 6];
191
direction .y = u * normals [i + 1] + v * normals [i + 4] + t * normals [i + 7];
192
direction .z = u * normals [i + 2] + v * normals [i + 5] + t * normals [i + 8];
196
getRandomVelocity: function (velocity)
198
var speed = this .getRandomSpeed ();
200
velocity .x = direction .x * speed;
201
velocity .y = direction .y * speed;
202
velocity .z = direction .z * speed;
208
function getPosition (position)
210
return this .position .set (0, 0, 0);
51
213
return SurfaceEmitter;