@mcolabs/threebox-plugin 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @author peterqliu / https://github.com/peterqliu
3
+ * @author jscastro / https://github.com/jscastro76
4
+ */
5
+ import * as THREE from 'three';
6
+ import utils from '../utils/utils.js';
7
+ import material from '../utils/material.js';
8
+ import Objects from './objects.js';
9
+ import Object3D from './Object3D.js';
10
+
11
+ function Sphere(opt) {
12
+
13
+ opt = utils._validate(opt, Objects.prototype._defaults.sphere);
14
+ let geometry = new THREE.SphereGeometry(opt.radius, opt.sides, opt.sides);
15
+ let mat = material(opt)
16
+ let output = new THREE.Mesh(geometry, mat);
17
+ //[jscastro] we convert it in Object3D to add methods, bounding box, model, tooltip...
18
+ return new Object3D({ obj: output, units: opt.units, anchor: opt.anchor, adjustment: opt.adjustment, bbox: opt.bbox, tooltip: opt.tooltip, raycasted: opt.raycasted });
19
+
20
+ }
21
+
22
+ export default Sphere;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @author jscastro / https://github.com/jscastro76
3
+ */
4
+ import utils from '../utils/utils.js';
5
+ import Objects from './objects.js';
6
+ import { CSS2DObject } from './CSS2DRenderer.js';
7
+
8
+ function Tooltip(obj) {
9
+
10
+ obj = utils._validate(obj, Objects.prototype._defaults.tooltip);
11
+
12
+ if (obj.text) {
13
+
14
+ let divToolTip = Objects.prototype.drawTooltip(obj.text, obj.mapboxStyle);
15
+
16
+ let tooltip = new CSS2DObject(divToolTip);
17
+ tooltip.visible = false;
18
+ tooltip.name = "tooltip";
19
+ var userScaleGroup = Objects.prototype._makeGroup(tooltip, obj);
20
+ Objects.prototype._addMethods(userScaleGroup);
21
+ return userScaleGroup;
22
+ }
23
+
24
+ }
25
+
26
+ export default Tooltip;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @author peterqliu / https://github.com/peterqliu
3
+ * @author jscastro / https://github.com/jscastro76
4
+ */
5
+ import * as THREE from 'three';
6
+ import utils from '../utils/utils.js';
7
+ import material from '../utils/material.js';
8
+ import Objects from './objects.js';
9
+ import Object3D from './Object3D.js';
10
+
11
+ function tube(opt, world){
12
+
13
+ // validate and prep input geometry
14
+ opt = utils._validate(opt, Objects.prototype._defaults.tube);
15
+
16
+ let points = []
17
+ opt.geometry.forEach(p => {
18
+ points.push(new THREE.Vector3(p[0], p[1], p[2]));
19
+ })
20
+ const curve = new THREE.CatmullRomCurve3(points);
21
+ let tube = new THREE.TubeGeometry(curve, points.length, opt.radius, opt.sides, false);
22
+ let mat = material(opt);
23
+ let obj = new THREE.Mesh(tube, mat);
24
+ //[jscastro] we convert it in Object3D to add methods, bounding box, model, tooltip...
25
+ return new Object3D({ obj: obj, units: opt.units, anchor: opt.anchor, adjustment: opt.adjustment, bbox: opt.bbox, tooltip: opt.tooltip, raycasted: opt.raycasted });
26
+ }
27
+
28
+ export default tube;
@@ -0,0 +1,11 @@
1
+ const ValueGenerator = function(input) {
2
+ if(typeof input === 'object' && input.property !== undefined) // Value name comes from a property in each item
3
+ return (f => f.properties[input.property]);
4
+ else if(typeof input === 'object' && input.generator !== undefined) // Value name generated by a function run on each item
5
+ return input.generator;
6
+ else return (() => input);
7
+
8
+ return undefined;
9
+ }
10
+
11
+ export default ValueGenerator;
@@ -0,0 +1,23 @@
1
+ const WORLD_SIZE = 1024000; //TILE_SIZE * 2000
2
+ const MERCATOR_A = 6378137.0; // 900913 projection property. (Deprecated) Replaced by EARTH_RADIUS
3
+ const FOV_ORTHO = 0.1 / 180 * Math.PI; //Mapbox doesn't accept 0 as FOV
4
+ const FOV = Math.atan(3 / 4); //from Mapbox https://github.com/mapbox/mapbox-gl-js/blob/main/src/geo/transform.js#L93
5
+ const EARTH_RADIUS = 6371008.8; //from Mapbox https://github.com/mapbox/mapbox-gl-js/blob/0063cbd10a97218fb6a0f64c99bf18609b918f4c/src/geo/lng_lat.js#L11
6
+ const EARTH_CIRCUMFERENCE_EQUATOR = 40075017 //from Mapbox https://github.com/mapbox/mapbox-gl-js/blob/0063cbd10a97218fb6a0f64c99bf18609b918f4c/src/geo/lng_lat.js#L117
7
+
8
+ const ThreeboxConstants = {
9
+ WORLD_SIZE: WORLD_SIZE,
10
+ PROJECTION_WORLD_SIZE: WORLD_SIZE / (EARTH_RADIUS * Math.PI * 2),
11
+ MERCATOR_A: EARTH_RADIUS,
12
+ DEG2RAD: Math.PI / 180,
13
+ RAD2DEG: 180 / Math.PI,
14
+ EARTH_RADIUS: EARTH_RADIUS,
15
+ EARTH_CIRCUMFERENCE: 2 * Math.PI * EARTH_RADIUS, //40075000, // In meters
16
+ EARTH_CIRCUMFERENCE_EQUATOR: EARTH_CIRCUMFERENCE_EQUATOR,
17
+ FOV_ORTHO: FOV_ORTHO, // closest to 0
18
+ FOV: FOV, // Math.atan(3/4) radians. If this value is changed, FOV_DEGREES must be calculated
19
+ FOV_DEGREES: FOV * 180 / Math.PI, // Math.atan(3/4) in degrees
20
+ TILE_SIZE: 512
21
+ };
22
+
23
+ export default ThreeboxConstants;
@@ -0,0 +1,52 @@
1
+ // This module creates a THREE material from the options object provided into the Objects class.
2
+ // Users can do this in one of three ways:
3
+
4
+ // - provide a preset THREE.Material in the `material` parameter
5
+ // - specify a `material` string, `color`, and/or `opacity` as modifications of the default material
6
+ // - provide none of these parameters, to use the default material
7
+
8
+ import utils from './utils.js';
9
+ import * as THREE from 'three';
10
+
11
+ var defaults = {
12
+ material: 'MeshBasicMaterial',
13
+ color: 'black',
14
+ opacity: 1
15
+ };
16
+
17
+
18
+ function material (options) {
19
+
20
+ var output;
21
+
22
+ if (options) {
23
+
24
+ options = utils._validate(options, defaults);
25
+
26
+ // check if user provided material object
27
+ if (options.material && options.material.isMaterial) output = options.material;
28
+
29
+ // check if user provided any material parameters. create new material object based on that.
30
+ else if (options.material || options.color || options.opacity){
31
+ output = new THREE[options.material]({color: options.color, transparent: options.opacity<1});
32
+ }
33
+
34
+ // if neither, return default material
35
+ else output = generateDefaultMaterial();
36
+
37
+ output.opacity = options.opacity;
38
+ if (options.side) output.side = options.side
39
+
40
+ }
41
+
42
+ // if no options, return default
43
+ else output = generateDefaultMaterial();
44
+
45
+ function generateDefaultMaterial(){
46
+ return new THREE[defaults.material]({color: defaults.color});
47
+ }
48
+
49
+ return output
50
+ }
51
+
52
+ export default material;
@@ -0,0 +1,311 @@
1
+ /*
2
+ (c) 2011-2015, Vladimir Agafonkin
3
+ SunCalc is a JavaScript library for calculating sun/moon position and light phases.
4
+ https://github.com/mourner/suncalc
5
+ */
6
+
7
+ // shortcuts for easier to read formulas
8
+
9
+ var PI = Math.PI,
10
+ sin = Math.sin,
11
+ cos = Math.cos,
12
+ tan = Math.tan,
13
+ asin = Math.asin,
14
+ atan = Math.atan2,
15
+ acos = Math.acos,
16
+ rad = PI / 180;
17
+
18
+ // sun calculations are based on http://aa.quae.nl/en/reken/zonpositie.html formulas
19
+
20
+
21
+ // date/time constants and conversions
22
+
23
+ var dayMs = 1000 * 60 * 60 * 24,
24
+ J1970 = 2440588,
25
+ J2000 = 2451545;
26
+
27
+ function toJulian(date) { return date.valueOf() / dayMs - 0.5 + J1970; }
28
+ function fromJulian(j) { return new Date((j + 0.5 - J1970) * dayMs); }
29
+ function toDays(date) { return toJulian(date) - J2000; }
30
+
31
+ // general calculations for position
32
+
33
+ var e = rad * 23.4397; // obliquity of the Earth
34
+
35
+ function rightAscension(l, b) { return atan(sin(l) * cos(e) - tan(b) * sin(e), cos(l)); }
36
+ function declination(l, b) { return asin(sin(b) * cos(e) + cos(b) * sin(e) * sin(l)); }
37
+
38
+ function azimuth(H, phi, dec) { return atan(sin(H), cos(H) * sin(phi) - tan(dec) * cos(phi)); }
39
+ function altitude(H, phi, dec) { return asin(sin(phi) * sin(dec) + cos(phi) * cos(dec) * cos(H)); }
40
+
41
+ function siderealTime(d, lw) { return rad * (280.16 + 360.9856235 * d) - lw; }
42
+
43
+ function astroRefraction(h) {
44
+ if (h < 0) // the following formula works for positive altitudes only.
45
+ h = 0; // if h = -0.08901179 a div/0 would occur.
46
+
47
+ // formula 16.4 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
48
+ // 1.02 / tan(h + 10.26 / (h + 5.10)) h in degrees, result in arc minutes -> converted to rad:
49
+ return 0.0002967 / Math.tan(h + 0.00312536 / (h + 0.08901179));
50
+ }
51
+
52
+ // general sun calculations
53
+
54
+ function solarMeanAnomaly(d) { return rad * (357.5291 + 0.98560028 * d); }
55
+
56
+ function eclipticLongitude(M) {
57
+
58
+ var C = rad * (1.9148 * sin(M) + 0.02 * sin(2 * M) + 0.0003 * sin(3 * M)), // equation of center
59
+ P = rad * 102.9372; // perihelion of the Earth
60
+
61
+ return M + C + P + PI;
62
+ }
63
+
64
+ function sunCoords(d) {
65
+
66
+ var M = solarMeanAnomaly(d),
67
+ L = eclipticLongitude(M);
68
+
69
+ return {
70
+ dec: declination(L, 0),
71
+ ra: rightAscension(L, 0)
72
+ };
73
+ }
74
+
75
+
76
+ var SunCalc = {};
77
+
78
+
79
+ // calculates sun position for a given date and latitude/longitude
80
+
81
+ SunCalc.getPosition = function (date, lat, lng) {
82
+
83
+ var lw = rad * -lng,
84
+ phi = rad * lat,
85
+ d = toDays(date),
86
+
87
+ c = sunCoords(d),
88
+ H = siderealTime(d, lw) - c.ra;
89
+
90
+ return {
91
+ azimuth: azimuth(H, phi, c.dec),
92
+ altitude: altitude(H, phi, c.dec)
93
+ };
94
+ };
95
+
96
+ SunCalc.toJulian = function (date) {
97
+ return toJulian(date);
98
+ };
99
+
100
+ // sun times configuration (angle, morning name, evening name)
101
+
102
+ var times = SunCalc.times = [
103
+ [-0.833, 'sunrise', 'sunset'],
104
+ [-0.3, 'sunriseEnd', 'sunsetStart'],
105
+ [-6, 'dawn', 'dusk'],
106
+ [-12, 'nauticalDawn', 'nauticalDusk'],
107
+ [-18, 'nightEnd', 'night'],
108
+ [6, 'goldenHourEnd', 'goldenHour']
109
+ ];
110
+
111
+ // adds a custom time to the times config
112
+
113
+ SunCalc.addTime = function (angle, riseName, setName) {
114
+ times.push([angle, riseName, setName]);
115
+ };
116
+
117
+
118
+ // calculations for sun times
119
+
120
+ var J0 = 0.0009;
121
+
122
+ function julianCycle(d, lw) { return Math.round(d - J0 - lw / (2 * PI)); }
123
+
124
+ function approxTransit(Ht, lw, n) { return J0 + (Ht + lw) / (2 * PI) + n; }
125
+ function solarTransitJ(ds, M, L) { return J2000 + ds + 0.0053 * sin(M) - 0.0069 * sin(2 * L); }
126
+
127
+ function hourAngle(h, phi, d) { return acos((sin(h) - sin(phi) * sin(d)) / (cos(phi) * cos(d))); }
128
+ function observerAngle(height) { return -2.076 * Math.sqrt(height) / 60; }
129
+
130
+ // returns set time for the given sun altitude
131
+ function getSetJ(h, lw, phi, dec, n, M, L) {
132
+
133
+ var w = hourAngle(h, phi, dec),
134
+ a = approxTransit(w, lw, n);
135
+ return solarTransitJ(a, M, L);
136
+ }
137
+
138
+
139
+ // calculates sun times for a given date, latitude/longitude, and, optionally,
140
+ // the observer height (in meters) relative to the horizon
141
+
142
+ SunCalc.getTimes = function (date, lat, lng, height) {
143
+
144
+ height = height || 0;
145
+
146
+ var lw = rad * -lng,
147
+ phi = rad * lat,
148
+
149
+ dh = observerAngle(height),
150
+
151
+ d = toDays(date),
152
+ n = julianCycle(d, lw),
153
+ ds = approxTransit(0, lw, n),
154
+
155
+ M = solarMeanAnomaly(ds),
156
+ L = eclipticLongitude(M),
157
+ dec = declination(L, 0),
158
+
159
+ Jnoon = solarTransitJ(ds, M, L),
160
+
161
+ i, len, time, h0, Jset, Jrise;
162
+
163
+
164
+ var result = {
165
+ solarNoon: fromJulian(Jnoon),
166
+ nadir: fromJulian(Jnoon - 0.5)
167
+ };
168
+
169
+ for (i = 0, len = times.length; i < len; i += 1) {
170
+ time = times[i];
171
+ h0 = (time[0] + dh) * rad;
172
+
173
+ Jset = getSetJ(h0, lw, phi, dec, n, M, L);
174
+ Jrise = Jnoon - (Jset - Jnoon);
175
+
176
+ result[time[1]] = fromJulian(Jrise);
177
+ result[time[2]] = fromJulian(Jset);
178
+ }
179
+
180
+ return result;
181
+ };
182
+
183
+
184
+ // moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas
185
+
186
+ function moonCoords(d) { // geocentric ecliptic coordinates of the moon
187
+
188
+ var L = rad * (218.316 + 13.176396 * d), // ecliptic longitude
189
+ M = rad * (134.963 + 13.064993 * d), // mean anomaly
190
+ F = rad * (93.272 + 13.229350 * d), // mean distance
191
+
192
+ l = L + rad * 6.289 * sin(M), // longitude
193
+ b = rad * 5.128 * sin(F), // latitude
194
+ dt = 385001 - 20905 * cos(M); // distance to the moon in km
195
+
196
+ return {
197
+ ra: rightAscension(l, b),
198
+ dec: declination(l, b),
199
+ dist: dt
200
+ };
201
+ }
202
+
203
+ SunCalc.getMoonPosition = function (date, lat, lng) {
204
+
205
+ var lw = rad * -lng,
206
+ phi = rad * lat,
207
+ d = toDays(date),
208
+
209
+ c = moonCoords(d),
210
+ H = siderealTime(d, lw) - c.ra,
211
+ h = altitude(H, phi, c.dec),
212
+ // formula 14.1 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
213
+ pa = atan(sin(H), tan(phi) * cos(c.dec) - sin(c.dec) * cos(H));
214
+
215
+ h = h + astroRefraction(h); // altitude correction for refraction
216
+
217
+ return {
218
+ azimuth: azimuth(H, phi, c.dec),
219
+ altitude: h,
220
+ distance: c.dist,
221
+ parallacticAngle: pa
222
+ };
223
+ };
224
+
225
+
226
+ // calculations for illumination parameters of the moon,
227
+ // based on http://idlastro.gsfc.nasa.gov/ftp/pro/astro/mphase.pro formulas and
228
+ // Chapter 48 of "Astronomical Algorithms" 2nd edition by Jean Meeus (Willmann-Bell, Richmond) 1998.
229
+
230
+ SunCalc.getMoonIllumination = function (date) {
231
+
232
+ var d = toDays(date || new Date()),
233
+ s = sunCoords(d),
234
+ m = moonCoords(d),
235
+
236
+ sdist = 149598000, // distance from Earth to Sun in km
237
+
238
+ phi = acos(sin(s.dec) * sin(m.dec) + cos(s.dec) * cos(m.dec) * cos(s.ra - m.ra)),
239
+ inc = atan(sdist * sin(phi), m.dist - sdist * cos(phi)),
240
+ angle = atan(cos(s.dec) * sin(s.ra - m.ra), sin(s.dec) * cos(m.dec) -
241
+ cos(s.dec) * sin(m.dec) * cos(s.ra - m.ra));
242
+
243
+ return {
244
+ fraction: (1 + cos(inc)) / 2,
245
+ phase: 0.5 + 0.5 * inc * (angle < 0 ? -1 : 1) / Math.PI,
246
+ angle: angle
247
+ };
248
+ };
249
+
250
+
251
+ function hoursLater(date, h) {
252
+ return new Date(date.valueOf() + h * dayMs / 24);
253
+ }
254
+
255
+ // calculations for moon rise/set times are based on http://www.stargazing.net/kepler/moonrise.html article
256
+
257
+ SunCalc.getMoonTimes = function (date, lat, lng, inUTC) {
258
+ var t = new Date(date);
259
+ if (inUTC) t.setUTCHours(0, 0, 0, 0);
260
+ else t.setHours(0, 0, 0, 0);
261
+
262
+ var hc = 0.133 * rad,
263
+ h0 = SunCalc.getMoonPosition(t, lat, lng).altitude - hc,
264
+ h1, h2, rise, set, a, b, xe, ye, d, roots, x1, x2, dx;
265
+
266
+ // go in 2-hour chunks, each time seeing if a 3-point quadratic curve crosses zero (which means rise or set)
267
+ for (var i = 1; i <= 24; i += 2) {
268
+ h1 = SunCalc.getMoonPosition(hoursLater(t, i), lat, lng).altitude - hc;
269
+ h2 = SunCalc.getMoonPosition(hoursLater(t, i + 1), lat, lng).altitude - hc;
270
+
271
+ a = (h0 + h2) / 2 - h1;
272
+ b = (h2 - h0) / 2;
273
+ xe = -b / (2 * a);
274
+ ye = (a * xe + b) * xe + h1;
275
+ d = b * b - 4 * a * h1;
276
+ roots = 0;
277
+
278
+ if (d >= 0) {
279
+ dx = Math.sqrt(d) / (Math.abs(a) * 2);
280
+ x1 = xe - dx;
281
+ x2 = xe + dx;
282
+ if (Math.abs(x1) <= 1) roots++;
283
+ if (Math.abs(x2) <= 1) roots++;
284
+ if (x1 < -1) x1 = x2;
285
+ }
286
+
287
+ if (roots === 1) {
288
+ if (h0 < 0) rise = i + x1;
289
+ else set = i + x1;
290
+
291
+ } else if (roots === 2) {
292
+ rise = i + (ye < 0 ? x2 : x1);
293
+ set = i + (ye < 0 ? x1 : x2);
294
+ }
295
+
296
+ if (rise && set) break;
297
+
298
+ h0 = h2;
299
+ }
300
+
301
+ var result = {};
302
+
303
+ if (rise) result.rise = hoursLater(t, rise);
304
+ if (set) result.set = hoursLater(t, set);
305
+
306
+ if (!rise && !set) result[ye > 0 ? 'alwaysUp' : 'alwaysDown'] = true;
307
+
308
+ return result;
309
+ };
310
+
311
+ export default SunCalc;