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
| const preloadedModels = {};
const toggleModelVisibility = (id) => { Object.keys(preloadedModels).forEach((key) => { const model = preloadedModels[key]; if (key === id) { model.hidden = false; } else { model.hidden = true; } }); };
const floors = [ { drawableName: "model0", model: true, rotation: { x: 90, y: -155.54446027115023, z: 0, }, scale: 5.66, modelCoords: [121, 31, 2], }, { drawableName: "model1", model: false, rotation: { x: 0, y: 0, z: 0, }, scale: 1, modelCoords: [0, 0, 0], }, { drawableName: "model2", model: true, rotation: { x: 90, y: -154.54739701601238, z: 0, }, scale: 10.03, modelCoords: [121, 31, 4], }, { drawableName: "model3", model: false, rotation: { x: 0, y: 0, z: 0, }, scale: 1, modelCoords: [0, 0, 0], }, ]
const preloadModels = (onComplete) => { let loadedCount = 0; const total = floors.length;
floors.forEach(({ drawableName, scale, modelCoords, rotation }) => { const newModelUrl = './model/' + PROJ + '/' + drawableName + '.glb'; const options = { id: drawableName, obj: newModelUrl, type: 'gltf', scale: scale, units: 'meters', };
tb.loadObj(options, (model) => { model.setCoords(modelCoords); model.setRotation(rotation); window.tb.add(model); preloadedModels[drawableName] = model;
model.hidden = true;
loadedCount++; if(loadedCount === total && onComplete) { onComplete(); } }); }); };
mapInstance.on('load', async () => {
mapInstance.addLayer({ id: 'custom_layer', type: 'custom', renderingMode: '3d', onAdd: function(map, mbxContext) { window.tb = new Threebox(map, mbxContext, { defaultLights: true, antialias: true, }); window.tb.enableSelectingObjects = true;
preloadModels(() => { console.log('所有模型加载完成!'); }); }, render: function(gl, matrix) { tb.update(); } });
});
|