~holger-seelig/cobweb.js/1.4

« back to all changes in this revision

Viewing changes to cobweb.js/cobweb/Components/ParticleSystems/PolylineEmitter.js

  • Committer: Holger Seelig
  • Date: 2016-04-23 04:10:18 UTC
  • Revision ID: holger.seelig@yahoo.de-20160423041018-p2co33alldequj0a
Added PolylineEmitter.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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",
9
12
],
10
13
function ($,
11
14
          Fields,
12
15
          X3DFieldDefinition,
13
16
          FieldDefinitionArray,
14
 
          X3DParticleEmitterNode, 
15
 
          X3DConstants)
 
17
          X3DParticleEmitterNode,
 
18
          IndexedLineSet,
 
19
          X3DConstants,
 
20
          Vector3,
 
21
          Algorithm)
16
22
{
17
23
"use strict";
18
24
 
 
25
        var vector = new Vector3 (0, 0, 0);
 
26
 
19
27
        function PolylineEmitter (executionContext)
20
28
        {
21
29
                X3DParticleEmitterNode .call (this, executionContext);
22
30
 
23
31
                this .addType (X3DConstants .PolylineEmitter);
 
32
 
 
33
                this .direction        = new Vector3 (0, 0, 0);
 
34
                this .polylineNode     = new IndexedLineSet (executionContext);
 
35
                this .polylines        = [ ];
 
36
                this .lengthSoFarArray = [ 0 ];
24
37
        }
25
38
 
26
39
        PolylineEmitter .prototype = $.extend (Object .create (X3DParticleEmitterNode .prototype),
48
61
                {
49
62
                        return "emitter";
50
63
                },
 
64
                initialize: function ()
 
65
                {
 
66
                        X3DParticleEmitterNode .prototype .initialize .call (this);
 
67
 
 
68
                        this .direction_ .addInterest (this, "set_direction__");
 
69
 
 
70
                        this .coordIndex_ .addFieldInterest (this .polylineNode .coordIndex_);
 
71
                        this .coord_      .addFieldInterest (this .polylineNode .coord_);
 
72
                
 
73
                        this .polylineNode .coordIndex_ = this .coordIndex_;
 
74
                        this .polylineNode .coord_      = this .coord_;
 
75
 
 
76
                        this .polylineNode .addInterest (this, "set_polyline");
 
77
                        this .polylineNode .setup ();
 
78
 
 
79
                        this .set_direction__ ();
 
80
                        this .set_polyline ();
 
81
                },
 
82
                set_direction__: function ()
 
83
                {
 
84
                        this .direction .assign (this .direction_ .getValue ()) .normalize ();
 
85
 
 
86
                        if (this .direction .equals (Vector3 .Zero))
 
87
                                this .getRandomVelocity = this .getSphericalRandomVelocity;
 
88
                        else
 
89
                                delete this .getRandomVelocity;
 
90
                },
 
91
                set_polyline: function ()
 
92
                {
 
93
                        var polylines = this .polylineNode .getPolylines (this .polylines);
 
94
 
 
95
                        if (polylines .length)
 
96
                        {
 
97
                                delete this .getRandomPosition;
 
98
 
 
99
                                var
 
100
                                        lengthSoFar      = 0,
 
101
                                        lengthSoFarArray = this .lengthSoFarArray;
 
102
                
 
103
                                lengthSoFarArray .length = 1;
 
104
 
 
105
                                for (var i = 0, length = polylines .length; i < length; i += 2)
 
106
                                {
 
107
                                        lengthSoFar += vector .assign (polylines [i + 1]) .subtract (polylines [i]) .abs ();
 
108
                                        lengthSoFarArray .push (lengthSoFar);
 
109
                                }
 
110
                        }
 
111
                        else
 
112
                        {
 
113
                                this .getRandomPosition = getPosition;
 
114
                        }
 
115
                },
 
116
                getRandomPosition: function (position)
 
117
                {
 
118
                        // Determine index0 and weight.
 
119
 
 
120
                        var
 
121
                                lengthSoFarArray = this .lengthSoFarArray,
 
122
                                length           = lengthSoFarArray .length,
 
123
                                fraction         = Math .random () * lengthSoFarArray [length - 1],
 
124
                                index0           = 0,
 
125
                                index1           = 0,
 
126
                                weight           = 0;
 
127
 
 
128
                        if (length == 1 || fraction <= lengthSoFarArray [0])
 
129
                        {
 
130
                                index0 = 0;
 
131
                                weight = 0;
 
132
                        }
 
133
                        else if (fraction >= lengthSoFarArray [length - 1])
 
134
                        {
 
135
                                index0 = length - 2;
 
136
                                weight = 1;
 
137
                        }
 
138
                        else
 
139
                        {
 
140
                                var index = Algorithm .upperBound (lengthSoFarArray, 0, length, fraction, Algorithm .less);
 
141
 
 
142
                                if (index < length)
 
143
                                {
 
144
                                        index1 = index;
 
145
                                        index0 = index - 1;
 
146
                        
 
147
                                        var
 
148
                                                key0 = lengthSoFarArray [index0],
 
149
                                                key1 = lengthSoFarArray [index1];
 
150
                        
 
151
                                        weight = Algorithm .clamp ((fraction - key0) / (key1 - key0), 0, 1);
 
152
                                }
 
153
                                else
 
154
                                {
 
155
                                        index0 = 0;
 
156
                                        weight = 0;
 
157
                                }
 
158
                        }
 
159
 
 
160
                        // Interpolate and set position.
 
161
 
 
162
                        index0 *= 2;
 
163
                        index1  = index0 + 1;
 
164
 
 
165
                        var
 
166
                                vertex1 = this .polylines [index0],
 
167
                                vertex2 = this .polylines [index1];
 
168
        
 
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);
 
173
 
 
174
                        return position;
 
175
                },
 
176
                getRandomVelocity: function (velocity)
 
177
                {
 
178
                        var
 
179
                                direction = this .direction,
 
180
                                speed     = this .getRandomSpeed ();
 
181
 
 
182
                        velocity .x = direction .x * speed;
 
183
                        velocity .y = direction .y * speed;
 
184
                        velocity .z = direction .z * speed;
 
185
 
 
186
                        return velocity;
 
187
                },
51
188
        });
52
189
 
 
190
        function getPosition (position)
 
191
        {
 
192
                return this .position .set (0, 0, 0);
 
193
        }
 
194
 
53
195
        return PolylineEmitter;
54
196
});
55
197