@tonybfox/threejs-tools 1.0.5 → 1.0.7

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.
Files changed (41) hide show
  1. package/dist/asset-loader/index.cjs +3 -417
  2. package/dist/asset-loader/index.mjs +1 -6
  3. package/dist/camera/index.cjs +1 -393
  4. package/dist/camera/index.mjs +1 -6
  5. package/dist/chunk-4O4ENFL7.mjs +83 -0
  6. package/dist/chunk-55YVGK52.mjs +1 -0
  7. package/dist/chunk-B75TYEOO.mjs +44 -0
  8. package/dist/chunk-CLSRN5D2.mjs +1 -0
  9. package/dist/chunk-EQRUOKFV.mjs +1 -0
  10. package/dist/chunk-JRJBW66X.mjs +1 -0
  11. package/dist/chunk-OJFYE56U.mjs +1 -0
  12. package/dist/chunk-WZ4X7GQ2.mjs +1 -0
  13. package/dist/chunk-Z5VL3O6M.mjs +43 -0
  14. package/dist/compass/index.cjs +3 -304
  15. package/dist/compass/index.mjs +1 -6
  16. package/dist/grid/index.cjs +3 -159
  17. package/dist/grid/index.mjs +1 -6
  18. package/dist/index.cjs +7 -5406
  19. package/dist/index.mjs +1 -384
  20. package/dist/measurements/index.cjs +1 -1197
  21. package/dist/measurements/index.mjs +1 -8
  22. package/dist/sunlight/index.cjs +1 -440
  23. package/dist/sunlight/index.d.mts +19 -0
  24. package/dist/sunlight/index.d.ts +19 -0
  25. package/dist/sunlight/index.mjs +1 -6
  26. package/dist/terrain/index.cjs +1 -422
  27. package/dist/terrain/index.mjs +1 -6
  28. package/dist/transform-controls/index.cjs +1 -1586
  29. package/dist/transform-controls/index.mjs +1 -12
  30. package/dist/view-helper/index.cjs +1 -435
  31. package/dist/view-helper/index.mjs +1 -6
  32. package/package.json +1 -1
  33. package/dist/chunk-2CDI7ORN.mjs +0 -163
  34. package/dist/chunk-FBTT6MU6.mjs +0 -386
  35. package/dist/chunk-IAZH4OHC.mjs +0 -399
  36. package/dist/chunk-LUE7VHLK.mjs +0 -422
  37. package/dist/chunk-OZKJ3GAD.mjs +0 -1160
  38. package/dist/chunk-W4DAAAW6.mjs +0 -404
  39. package/dist/chunk-XA7OKYSM.mjs +0 -357
  40. package/dist/chunk-YMMLYGHV.mjs +0 -1578
  41. package/dist/chunk-ZNGFST7K.mjs +0 -348
@@ -1,404 +0,0 @@
1
- // packages/sunlight/src/SunLightTool.ts
2
- import * as THREE from "three";
3
- var DAY_MS = 24 * 60 * 60 * 1e3;
4
- var DEG2RAD = Math.PI / 180;
5
- var RAD2DEG = 180 / Math.PI;
6
- var J1970 = 2440588;
7
- var J2000 = 2451545;
8
- var clamp = (value, min, max) => Math.min(Math.max(value, min), max);
9
- var normalizeLongitude = (value) => {
10
- const normalized = ((value + 180) % 360 + 360) % 360 - 180;
11
- return normalized;
12
- };
13
- var isLeapYear = (year) => year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
14
- var limitDayOfYear = (day, year) => {
15
- const maxDay = isLeapYear(year) ? 366 : 365;
16
- return clamp(Math.floor(day), 1, maxDay);
17
- };
18
- var normalizeHours = (hours) => {
19
- const normalized = hours % 24;
20
- return normalized < 0 ? normalized + 24 : normalized;
21
- };
22
- var TWILIGHT_SUN_COLOR = new THREE.Color(16747612);
23
- var TWILIGHT_AMBIENT_COLOR = new THREE.Color(16757898);
24
- var TWILIGHT_SKY_COLOR = new THREE.Color(16750950);
25
- var TWILIGHT_GROUND_COLOR = new THREE.Color(3549732);
26
- var toJulian = (date) => date.valueOf() / DAY_MS - 0.5 + J1970;
27
- var toDays = (date) => toJulian(date) - J2000;
28
- var solarMeanAnomaly = (days) => DEG2RAD * (357.5291 + 0.98560028 * days);
29
- var eclipticLongitude = (meanAnomaly) => {
30
- const c = DEG2RAD * (1.9148 * Math.sin(meanAnomaly) + 0.02 * Math.sin(2 * meanAnomaly) + 3e-4 * Math.sin(3 * meanAnomaly));
31
- const p = DEG2RAD * 102.9372;
32
- return meanAnomaly + c + p + Math.PI;
33
- };
34
- var declination = (longitude) => {
35
- const e = DEG2RAD * 23.4397;
36
- return Math.asin(Math.sin(e) * Math.sin(longitude));
37
- };
38
- var rightAscension = (longitude) => {
39
- const e = DEG2RAD * 23.4397;
40
- return Math.atan2(Math.sin(longitude) * Math.cos(e), Math.cos(longitude));
41
- };
42
- var siderealTime = (days, longitudeWest) => DEG2RAD * (280.16 + 360.9856235 * days) - longitudeWest;
43
- var solarAzimuth = (hourAngle, latitudeRad, declinationRad) => Math.atan2(
44
- Math.sin(hourAngle),
45
- Math.cos(hourAngle) * Math.sin(latitudeRad) - Math.tan(declinationRad) * Math.cos(latitudeRad)
46
- );
47
- var solarAltitude = (hourAngle, latitudeRad, declinationRad) => Math.asin(
48
- Math.sin(latitudeRad) * Math.sin(declinationRad) + Math.cos(latitudeRad) * Math.cos(declinationRad) * Math.cos(hourAngle)
49
- );
50
- var computeDayOfYear = (date) => {
51
- const startOfYear = Date.UTC(date.getUTCFullYear(), 0, 0);
52
- const diff = date.getTime() - startOfYear;
53
- return Math.floor(diff / (24 * 60 * 60 * 1e3));
54
- };
55
- var buildWeatherPreset = (weather, solarAltitude2) => {
56
- const altitudeFactor = clamp(Math.sin(solarAltitude2) + 0.1, 0, 1);
57
- switch (weather) {
58
- case "sunny":
59
- return {
60
- sunIntensity: 1.5 * altitudeFactor,
61
- sunColor: new THREE.Color(16773839),
62
- ambientIntensity: 0.35 + 0.45 * altitudeFactor,
63
- ambientColor: new THREE.Color(16115663),
64
- hemisphereIntensity: 0.4 + 0.4 * altitudeFactor,
65
- hemisphereSkyColor: new THREE.Color(12114175),
66
- hemisphereGroundColor: new THREE.Color(16044458),
67
- shadowBias: -5e-4
68
- };
69
- case "partly-cloudy":
70
- return {
71
- sunIntensity: 1.1 * altitudeFactor,
72
- sunColor: new THREE.Color(16774885),
73
- ambientIntensity: 0.45 + 0.35 * altitudeFactor,
74
- ambientColor: new THREE.Color(15265009),
75
- hemisphereIntensity: 0.5 + 0.3 * altitudeFactor,
76
- hemisphereSkyColor: new THREE.Color(13162475),
77
- hemisphereGroundColor: new THREE.Color(15129026),
78
- shadowBias: -3e-4
79
- };
80
- case "overcast":
81
- return {
82
- sunIntensity: 0.35 * altitudeFactor,
83
- sunColor: new THREE.Color(15791359),
84
- ambientIntensity: 0.75,
85
- ambientColor: new THREE.Color(14278117),
86
- hemisphereIntensity: 0.8,
87
- hemisphereSkyColor: new THREE.Color(13489631),
88
- hemisphereGroundColor: new THREE.Color(13158600),
89
- shadowBias: -1e-4
90
- };
91
- default:
92
- return {
93
- sunIntensity: 1 * altitudeFactor,
94
- sunColor: new THREE.Color(16767411),
95
- ambientIntensity: 0.35 + 0.25 * altitudeFactor,
96
- ambientColor: new THREE.Color(16769988),
97
- hemisphereIntensity: 0.4 + 0.3 * altitudeFactor,
98
- hemisphereSkyColor: new THREE.Color(16763043),
99
- hemisphereGroundColor: new THREE.Color(6243898),
100
- shadowBias: -2e-4
101
- };
102
- }
103
- };
104
- var SunLightTool = class extends THREE.EventDispatcher {
105
- constructor(scene, options = {}) {
106
- super();
107
- this.createdLight = false;
108
- this.createdHemisphere = false;
109
- this.scene = scene;
110
- const defaultLatitude = options.latitude ?? 51.5072;
111
- const defaultLongitude = options.longitude ?? -0.1276;
112
- this.latitude = clamp(defaultLatitude, -90, 90);
113
- this.longitude = normalizeLongitude(defaultLongitude);
114
- this.referenceYear = options.referenceYear ?? (/* @__PURE__ */ new Date()).getUTCFullYear();
115
- this.dayOfYear = limitDayOfYear(
116
- options.dayOfYear ?? 172,
117
- this.referenceYear
118
- );
119
- this.timeOfDay = normalizeHours(options.timeOfDay ?? 12);
120
- this.timeZoneOffsetMinutes = options.timeZoneOffsetMinutes ?? 0;
121
- this.weather = options.weather ?? "sunny";
122
- this.useSystemTime = options.useSystemTime ?? false;
123
- this.lightDistance = options.lightDistance ?? 150;
124
- this.shadowCameraSize = options.shadowCameraSize ?? 100;
125
- this.shadowCameraFar = options.shadowCameraFar ?? 600;
126
- const existingDirectional = options.light;
127
- this.light = existingDirectional ?? new THREE.DirectionalLight(16777215, 1);
128
- this.createdLight = !existingDirectional;
129
- if (!this.light.parent) {
130
- this.scene.add(this.light);
131
- }
132
- if (!this.light.target.parent) {
133
- this.scene.add(this.light.target);
134
- }
135
- this.configureShadowMap(this.light);
136
- if (options.ambientLight) {
137
- this.ambientLight = options.ambientLight;
138
- }
139
- if (options.enableHemisphereLight ?? true) {
140
- this.hemisphereLight = new THREE.HemisphereLight(
141
- options.hemisphereSkyColor ?? 12244223,
142
- options.hemisphereGroundColor ?? 7628634,
143
- options.hemisphereIntensity ?? 0.45
144
- );
145
- this.scene.add(this.hemisphereLight);
146
- this.createdHemisphere = true;
147
- }
148
- if (options.showHelper) {
149
- this.helper = new THREE.DirectionalLightHelper(
150
- this.light,
151
- options.helperSize ?? 25,
152
- options.helperColor ?? 16765567
153
- );
154
- this.scene.add(this.helper);
155
- }
156
- this.state = {
157
- date: /* @__PURE__ */ new Date(),
158
- latitude: this.latitude,
159
- longitude: this.longitude,
160
- solarAzimuth: 0,
161
- solarAltitude: 0,
162
- weather: this.weather,
163
- useSystemTime: this.useSystemTime
164
- };
165
- this.update();
166
- }
167
- getLight() {
168
- return this.light;
169
- }
170
- getHemisphereLight() {
171
- return this.hemisphereLight;
172
- }
173
- getState() {
174
- return { ...this.state, date: new Date(this.state.date.getTime()) };
175
- }
176
- getDayOfYear() {
177
- return this.dayOfYear;
178
- }
179
- getTimeOfDay() {
180
- return this.timeOfDay;
181
- }
182
- getTimeZoneOffset() {
183
- return this.timeZoneOffsetMinutes;
184
- }
185
- getWeather() {
186
- return this.weather;
187
- }
188
- usesSystemTime() {
189
- return this.useSystemTime;
190
- }
191
- setLatitude(latitude) {
192
- const clamped = clamp(latitude, -90, 90);
193
- if (clamped === this.latitude) return;
194
- this.latitude = clamped;
195
- this.update();
196
- }
197
- setLongitude(longitude) {
198
- const normalized = normalizeLongitude(longitude);
199
- if (normalized === this.longitude) return;
200
- this.longitude = normalized;
201
- this.update();
202
- }
203
- setLocation(latitude, longitude) {
204
- let changed = false;
205
- const lat = clamp(latitude, -90, 90);
206
- if (lat !== this.latitude) {
207
- this.latitude = lat;
208
- changed = true;
209
- }
210
- const lon = normalizeLongitude(longitude);
211
- if (lon !== this.longitude) {
212
- this.longitude = lon;
213
- changed = true;
214
- }
215
- if (changed) {
216
- this.update();
217
- }
218
- }
219
- setDayOfYear(day) {
220
- const limited = limitDayOfYear(day, this.referenceYear);
221
- if (limited === this.dayOfYear) return;
222
- this.dayOfYear = limited;
223
- this.update();
224
- }
225
- setTimeOfDay(hours) {
226
- const normalized = normalizeHours(hours);
227
- if (normalized === this.timeOfDay) return;
228
- this.timeOfDay = normalized;
229
- this.update();
230
- }
231
- setTimeZoneOffset(minutes) {
232
- if (minutes === this.timeZoneOffsetMinutes) return;
233
- this.timeZoneOffsetMinutes = minutes;
234
- this.update();
235
- }
236
- setWeather(weather) {
237
- if (weather === this.weather) return;
238
- this.weather = weather;
239
- this.dispatchEvent({ type: "weatherChanged", weather });
240
- this.update();
241
- }
242
- setUseSystemTime(enabled) {
243
- if (enabled === this.useSystemTime) return;
244
- this.useSystemTime = enabled;
245
- this.dispatchEvent({
246
- type: "systemTimeToggled",
247
- useSystemTime: this.useSystemTime
248
- });
249
- this.update();
250
- }
251
- update(dateOverride) {
252
- const date = this.computeDate(dateOverride);
253
- const position = this.computeSunPosition(date);
254
- this.applySunPosition(position, date);
255
- this.state = {
256
- date,
257
- latitude: this.latitude,
258
- longitude: this.longitude,
259
- solarAzimuth: position.azimuth,
260
- solarAltitude: position.altitude,
261
- weather: this.weather,
262
- useSystemTime: this.useSystemTime
263
- };
264
- this.applyWeather(position.altitude);
265
- this.dispatchEvent({ type: "stateChanged", state: this.getState() });
266
- }
267
- dispose() {
268
- if (this.helper) {
269
- this.scene.remove(this.helper);
270
- this.helper.dispose();
271
- this.helper = void 0;
272
- }
273
- if (this.createdHemisphere && this.hemisphereLight) {
274
- this.scene.remove(this.hemisphereLight);
275
- this.hemisphereLight.dispose();
276
- this.hemisphereLight = void 0;
277
- }
278
- if (this.createdLight && this.light) {
279
- this.scene.remove(this.light);
280
- if (this.light.shadow?.map) {
281
- this.light.shadow.map.dispose();
282
- }
283
- this.light.dispose();
284
- }
285
- }
286
- configureShadowMap(light) {
287
- light.castShadow = true;
288
- const camera = light.shadow.camera;
289
- if (camera instanceof THREE.OrthographicCamera && typeof this.shadowCameraSize === "number") {
290
- const size = this.shadowCameraSize;
291
- camera.left = -size;
292
- camera.right = size;
293
- camera.top = size;
294
- camera.bottom = -size;
295
- camera.near = 0.5;
296
- camera.far = this.shadowCameraFar;
297
- camera.updateProjectionMatrix();
298
- }
299
- light.shadow.mapSize.set(2048, 2048);
300
- light.shadow.bias = -5e-4;
301
- }
302
- computeDate(dateOverride) {
303
- if (dateOverride) {
304
- return new Date(dateOverride.getTime());
305
- }
306
- if (this.useSystemTime) {
307
- const now = /* @__PURE__ */ new Date();
308
- this.referenceYear = now.getUTCFullYear();
309
- this.dayOfYear = computeDayOfYear(now);
310
- this.timeOfDay = now.getUTCHours() + now.getUTCMinutes() / 60 + now.getUTCSeconds() / 3600;
311
- return now;
312
- }
313
- const daysInYear = isLeapYear(this.referenceYear) ? 366 : 365;
314
- const dayIndex = clamp(this.dayOfYear, 1, daysInYear);
315
- const date = new Date(Date.UTC(this.referenceYear, 0, 1, 0, 0, 0, 0));
316
- const minutesFromYearStart = (dayIndex - 1) * 24 * 60 + this.timeOfDay * 60 - this.timeZoneOffsetMinutes;
317
- date.setUTCMinutes(minutesFromYearStart);
318
- return date;
319
- }
320
- computeSunPosition(date) {
321
- const days = toDays(date);
322
- const lw = -this.longitude * DEG2RAD;
323
- const phi = this.latitude * DEG2RAD;
324
- const meanAnomaly = solarMeanAnomaly(days);
325
- const longitude = eclipticLongitude(meanAnomaly);
326
- const decl = declination(longitude);
327
- const ra = rightAscension(longitude);
328
- const sidereal = siderealTime(days, lw);
329
- const hourAngle = sidereal - ra;
330
- return {
331
- azimuth: solarAzimuth(hourAngle, phi, decl),
332
- altitude: solarAltitude(hourAngle, phi, decl)
333
- };
334
- }
335
- applySunPosition(position, date) {
336
- const radius = this.lightDistance;
337
- const altitude = position.altitude;
338
- const azimuth = position.azimuth;
339
- const x = -radius * Math.cos(altitude) * Math.sin(azimuth);
340
- const y = radius * Math.sin(altitude);
341
- const z = radius * Math.cos(altitude) * Math.cos(azimuth);
342
- this.light.position.set(x, y, z);
343
- this.light.target.position.set(0, 0, 0);
344
- this.light.target.updateMatrixWorld();
345
- if (this.helper) {
346
- this.helper.update();
347
- }
348
- if (!this.useSystemTime) {
349
- this.referenceYear = date.getUTCFullYear();
350
- }
351
- }
352
- applyWeather(solarAltitude2) {
353
- const preset = buildWeatherPreset(this.weather, solarAltitude2);
354
- const altitudeDeg = solarAltitude2 * RAD2DEG;
355
- const twilightStartDeg = -6;
356
- const daylightDeg = 4;
357
- const daylightRange = daylightDeg - twilightStartDeg;
358
- const daylightFactor = clamp(
359
- (altitudeDeg - twilightStartDeg) / daylightRange,
360
- 0,
361
- 1
362
- );
363
- const twilightFactor = 1 - daylightFactor;
364
- const sunColor = preset.sunColor.lerp(TWILIGHT_SUN_COLOR, twilightFactor);
365
- const ambientColor = preset.ambientColor.lerp(
366
- TWILIGHT_AMBIENT_COLOR,
367
- twilightFactor
368
- );
369
- const skyColor = preset.hemisphereSkyColor.lerp(
370
- TWILIGHT_SKY_COLOR,
371
- twilightFactor
372
- );
373
- const groundColor = preset.hemisphereGroundColor.lerp(
374
- TWILIGHT_GROUND_COLOR,
375
- twilightFactor
376
- );
377
- const adjustedSunIntensity = preset.sunIntensity * daylightFactor;
378
- const adjustedAmbientIntensity = Math.max(
379
- 0.05,
380
- preset.ambientIntensity * (0.3 + 0.7 * daylightFactor)
381
- );
382
- const adjustedHemisphereIntensity = Math.max(
383
- 0.05,
384
- preset.hemisphereIntensity * (0.35 + 0.65 * daylightFactor)
385
- );
386
- this.light.intensity = adjustedSunIntensity;
387
- this.light.visible = adjustedSunIntensity > 1e-3;
388
- this.light.color.copy(sunColor);
389
- this.light.shadow.bias = preset.shadowBias;
390
- if (this.ambientLight) {
391
- this.ambientLight.intensity = adjustedAmbientIntensity;
392
- this.ambientLight.color.copy(ambientColor);
393
- }
394
- if (this.hemisphereLight) {
395
- this.hemisphereLight.intensity = adjustedHemisphereIntensity;
396
- this.hemisphereLight.color.copy(skyColor);
397
- this.hemisphereLight.groundColor.copy(groundColor);
398
- }
399
- }
400
- };
401
-
402
- export {
403
- SunLightTool
404
- };