1
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
*******************************************************************************
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6
* Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
8
* All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
10
* The copyright notice above does not evidence any actual of intended
11
* publication of such source code, and is an unpublished work by create3000.
12
* This material contains CONFIDENTIAL INFORMATION that is the property of
15
* No permission is granted to copy, distribute, or create derivative works from
16
* the contents of this software, in whole or in part, without the prior written
17
* permission of create3000.
19
* NON-MILITARY USE ONLY
21
* All create3000 software are effectively free software with a non-military use
22
* restriction. It is free. Well commented source is provided. You may reuse the
23
* source in any way you please with the exception anything that uses it must be
24
* marked to indicate is contains 'non-military use only' components.
26
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
28
* Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
30
* This file is part of the Cobweb Project.
32
* Cobweb is free software: you can redistribute it and/or modify it under the
33
* terms of the GNU General Public License version 3 only, as published by the
34
* Free Software Foundation.
36
* Cobweb is distributed in the hope that it will be useful, but WITHOUT ANY
37
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
38
* A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
39
* details (a copy is included in the LICENSE file that accompanied this code).
41
* You should have received a copy of the GNU General Public License version 3
42
* along with Cobweb. If not, see <http://www.gnu.org/licenses/gpl.html> for a
43
* copy of the GPLv3 License.
45
* For Silvio, Joy and Adi.
47
******************************************************************************/
53
"cobweb/Basic/X3DFieldDefinition",
54
"cobweb/Basic/FieldDefinitionArray",
55
"cobweb/Components/ParticleSystems/X3DParticleEmitterNode",
56
"cobweb/Bits/X3DConstants",
57
"cobweb/Bits/X3DCast",
58
"standard/Math/Geometry/Triangle3",
59
"standard/Math/Numbers/Vector3",
60
"standard/Math/Algorithm",
66
X3DParticleEmitterNode,
76
vertex1 = new Vector3 (0, 0, 0),
77
vertex2 = new Vector3 (0, 0, 0),
78
vertex3 = new Vector3 (0, 0, 0),
79
direction = new Vector3 (0, 0, 0);
81
function SurfaceEmitter (executionContext)
83
X3DParticleEmitterNode .call (this, executionContext);
85
this .addType (X3DConstants .SurfaceEmitter);
87
this .surfaceNode = null;
88
this .areaSoFarArray = [ 0 ];
91
SurfaceEmitter .prototype = $.extend (Object .create (X3DParticleEmitterNode .prototype),
93
constructor: SurfaceEmitter,
94
fieldDefinitions: new FieldDefinitionArray ([
95
new X3DFieldDefinition (X3DConstants .inputOutput, "metadata", new Fields .SFNode ()),
96
new X3DFieldDefinition (X3DConstants .inputOutput, "speed", new Fields .SFFloat ()),
97
new X3DFieldDefinition (X3DConstants .inputOutput, "variation", new Fields .SFFloat (0.25)),
98
new X3DFieldDefinition (X3DConstants .initializeOnly, "mass", new Fields .SFFloat ()),
99
new X3DFieldDefinition (X3DConstants .initializeOnly, "surfaceArea", new Fields .SFFloat ()),
100
new X3DFieldDefinition (X3DConstants .initializeOnly, "surface", new Fields .SFNode ()),
102
getTypeName: function ()
104
return "SurfaceEmitter";
106
getComponentName: function ()
108
return "ParticleSystems";
110
getContainerField: function ()
114
initialize: function ()
116
X3DParticleEmitterNode .prototype .initialize .call (this);
118
this .surface_ .addInterest ("set_surface__", this);
120
this .set_surface__ ();
122
set_surface__: function ()
124
if (this .surfaceNode)
125
this .surfaceNode .removeInterest ("set_geometry__", this);
127
this .surfaceNode = X3DCast (X3DConstants .X3DGeometryNode, this .surface_);
129
if (this .surfaceNode)
130
this .surfaceNode .addInterest ("set_geometry__", this);
132
this .set_geometry__ ();
134
set_geometry__: function ()
136
if (this .surfaceNode)
138
delete this .getRandomPosition;
139
delete this .getRandomVelocity;
143
areaSoFarArray = this .areaSoFarArray,
144
vertices = this .surfaceNode .getVertices ();
146
areaSoFarArray .length = 1;
148
for (var i = 0, length = vertices .length; i < length; i += 12)
150
vertex1 .set (vertices [i + 0], vertices [i + 1], vertices [i + 2]);
151
vertex2 .set (vertices [i + 4], vertices [i + 5], vertices [i + 6]);
152
vertex3 .set (vertices [i + 8], vertices [i + 9], vertices [i + 10]);
154
areaSoFar += Triangle3 .area (vertex1, vertex2, vertex3);
155
areaSoFarArray .push (areaSoFar);
160
this .getRandomPosition = getPosition;
161
this .getRandomVelocity = this .getSphericalRandomVelocity;
163
direction .set (0, 0, 0);
166
getRandomPosition: function (position)
171
areaSoFarArray = this .areaSoFarArray,
172
length = areaSoFarArray .length,
173
fraction = Math .random () * areaSoFarArray [length - 1],
176
if (length == 1 || fraction <= areaSoFarArray [0])
180
else if (fraction >= areaSoFarArray [length - 1])
186
var index = Algorithm .upperBound (areaSoFarArray, 0, length, fraction, Algorithm .less);
198
// Random barycentric coordinates.
210
// Interpolate and set position.
214
vertices = this .surfaceNode .getVertices ();
218
position .x = u * vertices [i + 0] + v * vertices [i + 4] + t * vertices [i + 8];
219
position .y = u * vertices [i + 1] + v * vertices [i + 5] + t * vertices [i + 9];
220
position .z = u * vertices [i + 2] + v * vertices [i + 6] + t * vertices [i + 10];
224
normals = this .surfaceNode .getNormals ();
226
direction .x = u * normals [i + 0] + v * normals [i + 3] + t * normals [i + 6];
227
direction .y = u * normals [i + 1] + v * normals [i + 4] + t * normals [i + 7];
228
direction .z = u * normals [i + 2] + v * normals [i + 5] + t * normals [i + 8];
232
getRandomVelocity: function (velocity)
234
var speed = this .getRandomSpeed ();
236
velocity .x = direction .x * speed;
237
velocity .y = direction .y * speed;
238
velocity .z = direction .z * speed;
244
function getPosition (position)
246
return position .set (0, 0, 0);
249
return SurfaceEmitter;