-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLightningBolt.js
273 lines (239 loc) · 8.73 KB
/
LightningBolt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS202: Simplify dynamic range loops
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
// Base class for LightningBolt
class Bolt extends THREE.Line {
constructor(src, dest, thickness, color, density, circumference) {
if (thickness == null) { thickness = 1; }
if (color == null) { color = 0x0000ff; }
if (density == null) { density = 1; }
if (circumference == null) { circumference = 1; }
const material = new (THREE.LineBasicMaterial)({transparent: true, color: color, linewidth: thickness});
const geometry = new (THREE.Geometry);
super(geometry, material);
this.src = src;
this.dest = dest;
this.color = color;
this.thickness = thickness;
this.density = density;
this.circumference = circumference;
const results = LightningBolt.createBolt(this.src, this.dest, this.thickness, this.density, this.circumference);
this.setSegments(results);
}
strike(src, dest) {
if (src == null) { ({ src } = this); }
if (dest == null) { ({ dest } = this); }
this.src = src;
this.dest = dest;
return this.setSegments(LightningBolt.createBolt(src, dest, this.thickness, this.density, this.circumference));
}
setSegments(lines) {
const geometry = new THREE.Geometry();
const vertices = [];
for (let line of Array.from(lines)) {
if (vertices.indexOf(line.src) === -1) {
vertices.push(line.src);
}
if (vertices.indexOf(line.dest) === -1) {
vertices.push(line.dest);
}
}
geometry.vertices = vertices;
geometry.verticesNeedUpdate = true;
return this.geometry = geometry;
}
getPointOnBolt(percentage) {
let dest, src;
const pointIndex = percentage * ( this.geometry.vertices.size() - 1 );
let rounded = Math.round(pointIndex);
if (parseInt(pointIndex) === rounded) {
if (rounded === 0) { rounded = 1; }
src = this.geometry.vertices[rounded - 1];
dest = this.geometry.vertices[rounded];
} else {
if (rounded === (this.geometry.vertices.size() - 1)) { rounded = this.geometry.vertices.size() - 2; }
src = this.geometry.vertices[rounded];
dest = this.geometry.vertices[rounded + 1];
}
return this.getPointInBetweenByPerc(src, dest, 0.5);
}
getPointInBetweenByPerc(pointA, pointB, percentage) {
let dir = pointB.clone().sub(pointA);
const len = dir.length();
dir = dir.normalize().multiplyScalar(len * percentage);
return pointA.clone().add(dir);
}
static createBolt(src, dest, thickness, density, circumference) {
let i;
let asc, end;
const results = [];
const tangent = dest.clone().sub(src.clone());
const normal = new THREE.Vector3(tangent.y, -tangent.x, tangent.z).normalize();
const length = Measure.distanceBetween(src, dest);
let positions = [0];
for (i = 0, end = ((length / 4)) * density, asc = 0 <= end; asc ? i < end : i > end; asc ? i++ : i--) {
positions.push(Math.random());
}
positions = positions.sort();
const sway = 80;
const jaggedness = 1 / sway / circumference;
let prevPoint = src.clone();
let prevDisplacement = 0;
let prevDisplacementZ = 0;
i = 0;
for (let pos of Array.from(positions)) {
if (i === 0) {
i += 1;
continue;
}
const scale = (length * jaggedness) * (pos - positions[i - 1]);
const envelope = pos > .95 ? 20 * (1 - pos) : 1;
let displacement = Utils.random(-sway, sway) + Math.random();
displacement -= (displacement - prevDisplacement) * (1 - scale);
displacement *= envelope;
let displacementZ = Utils.random(-sway, sway) + Math.random();
displacementZ -= (displacementZ - prevDisplacementZ) * (1 - scale);
displacementZ *= envelope;
const x = src.x + (pos * tangent.x) + (displacement * normal.x);
const y = src.y + (pos * tangent.y) + (displacement * normal.y);
let z = src.z + (pos * tangent.z) + (displacement * normal.z);
z = displacementZ;
const point = new THREE.Vector3(x, y, z);
results.push({
src: prevPoint,
dest: point
});
prevPoint = point;
prevDisplacement = displacement;
prevDisplacementZ = displacementZ;
i += 1;
}
results.push({
src: prevPoint,
dest
});
return results;
}
}
class LightningBolt extends Bolt {
constructor(src, dest, thickness, color, density, circumference) {
if (thickness == null) { thickness = 1; }
if (color == null) { color = 0x0000ff; }
if (density == null) { density = 1; }
super(src, dest, thickness, color, density, circumference);
this.opacity = 0;
this.bolts = [];
this.numBranches = 0;
}
addBranch(thickness, color, density, circumference){
this.numBranches += 1;
const newBolt = new LightningBolt(new THREE.Vector3(), new THREE.Vector3(), thickness, color, density, circumference);
this.bolts.push(newBolt);
this.add(newBolt);
return newBolt;
}
_getBranchPoints() {
const branchPoints = [];
for (let i = 0, end = this.numBranches, asc = 0 <= end; asc ? i < end : i > end; asc ? i++ : i--) {
branchPoints.push(Math.random());
}
return branchPoints.sort();
}
_getTargetBranchPoint(bolt) {
const branchPoints = this._getBranchPoints();
return branchPoints[this.bolts.indexOf(bolt)];
}
getChildBoltStart(bolt) {
const targetBranchPoint = this._getTargetBranchPoint(bolt);
return this.getPointOnBolt(targetBranchPoint);
}
getChildBoltEnd(bolt) {
const targetBranchPoint = this._getTargetBranchPoint(bolt);
const boltSrc = this.getChildBoltStart(bolt);
const diff = new THREE.Vector3(this.dest.x - this.src.x, this.dest.y - this.src.y, this.dest.z - this.src.z);
// diff = new THREE.Vector3(bolt.dest.x - bolt.src.x, bolt.dest.y - bolt.src.y, bolt.dest.z - bolt.src.z)
const x = (diff.x * (1 - targetBranchPoint)) + boltSrc.x;
const y = (diff.y * (1 - targetBranchPoint)) + boltSrc.y;
const z = (diff.z * (1 - targetBranchPoint)) + boltSrc.z;
return new THREE.Vector3(x, y, z);
}
strike(src, dest) {
super.strike(src, dest);
return (() => {
const result = [];
for (let bolt of Array.from(this.bolts)) {
const boltSrc = this.getChildBoltStart(bolt);
const boltEnd = this.getChildBoltEnd(bolt);
result.push(bolt.strike(boltSrc, boltEnd));
}
return result;
})();
}
}
// Class which simulates lightning
//
// @param src - start point
// @param dest - dest point
// @param thickness - how thick the bolt is
// @param color - the color of the bolt
// @param density - how many changes of direction in one bolt
// @param circumference - how much the bolt spreads
// @param addBranches - callback instead of overrding addBranches
//
// @example
// line = new BranchLightning(
// new (THREE.Vector3)(0, 10, 0),
// new (THREE.Vector3)(0, 0, 0),
// 3, 'white', 50, 1, (parentBolt) ->
// for i in [0...1]
// newBolt = parentBolt.addBranch(parentBolt.thickness / 2, 'yellow', parentBolt.density / 2, parentBolt.circumference)
// for j in [0...1]
// newBolt.addBranch(newBolt.thickness / 3, 'orange', newBolt.density / 3, newBolt.circumference)
// )
//
// @example
// class MyBolt extends BranchLightning
// addBranches: ->
// for i in [0...1]
// newBolt = addBranch(@thickness / 2, 'yellow', @density / 2, @circumference)
// for j in [0...1]
// newBolt.addBranch(@thickness / 3, 'orange', @density / 3, @circumference)
//
// line = new MyBolt(
// new (THREE.Vector3)(0, 10, 0),
// new (THREE.Vector3)(0, 0, 0),
// 3, 'white', 10, 1
// )
//
// @example
// LightningBolt::getChildBoltEnd = (bolt) ->
// @dest
class BranchLightning extends LightningBolt {
constructor(src, dest, thickness, color, density, circumference, addBranches) {
super(src, dest, thickness, color, density, circumference);
if (addBranches != null) {
addBranches(this);
} else {
this.addBranches(this);
}
this.strike();
}
// Override this method to configure how the lightning bolt looks like
addBranches(parentBolt) {
return (() => {
const result = [];
for (let i = 0; i < 4; i++) {
var newBolt = parentBolt.addBranch(parentBolt.thickness / 2, 'yellow', parentBolt.density / 2, parentBolt.circumference);
result.push([0, 1].map((j) =>
newBolt.addBranch(newBolt.thickness / 3, 'orange', newBolt.density / 3, newBolt.circumference)));
}
return result;
})();
}
}