-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapData.js
86 lines (69 loc) · 2.73 KB
/
mapData.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
//mapData.js:
import * as THREE from 'three';
import { modelData } from './mapElements.js';
import { map } from './main.js';
import { MIN_LAT, MAX_LAT, MIN_LON, MAX_LON } from './main.js';
import { animations } from './animations.js';
import { materials } from './materials.js';
export function createModel(data, scene, loader) {
// Convert lat lon to model coordinates
const modelX = map(data.coordinates.lon, MIN_LON, MAX_LON, -3200, 3200);
const modelZ = map(data.coordinates.lat, MIN_LAT, MAX_LAT, 3200, -3200);
loader.load("public/models/"+data.modelName, function (gltf) {
// When the model is loaded
const model = gltf.scene;
// Add materials if exists
if (data.materialReference) {
for (const part in data.materialReference) {
const object = model.getObjectByName(part);
if (object && materials[data.materialReference[part]]) {
object.material = materials[data.materialReference[part]];
}
}
}
// Enable shadows for each mesh
model.traverse(function (object) {
if (object.isMesh) {
object.castShadow = true;
object.receiveShadow = true;
}
});
// Set model position
model.position.set(modelX, 0, modelZ);
// Add the model to the scene
scene.add(model);
// Add animation if exists
if (data.animationReference) {
for (const part in data.animationReference) {
if (animations[data.animationReference[part]]) {
const object = model.getObjectByName(part);
if (object) {
const wrapper = new THREE.Object3D();
object.parent.add(wrapper);
wrapper.add(object);
model.remove(object);
wrapper.name = part + '_wrapper';
}
data.animation = data.animation || {};
data.animation[part] = animations[data.animationReference[part]];
}
}
}
// Save the model to the model data
data.instance = model;
});
}
export function removeModel(modelData, scene) {
if (modelData.instance) {
scene.remove(modelData.instance);
modelData.instance = null;
}
}
export function checkModelVisibility(data, currentTime) {
let timeDifference = currentTime - data.timestamp;
// Convert difference from milliseconds to hours
timeDifference = timeDifference / (1000 * 60 * 60);
// If time difference is less than 1 hour, model should be visible
return timeDifference < 1;
}
export { modelData };