-
Notifications
You must be signed in to change notification settings - Fork 1
/
mulit.html
521 lines (486 loc) · 21.3 KB
/
mulit.html
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>多模型有序聚合</title>
<link rel="stylesheet" href="https://developer.api.autodesk.com/modelderivative/v2/viewers/6.*/style.min.css"
type="text/css">
<link rel="stylesheet" href="./static/clear.css">
<style>
.msg-box {
position: fixed;
right: 10px;
bottom: 10px;
z-index: 9;
width: 200px;
height: 240px;
background: rgba(250, 200, 209, .6);
border-radius: 3px;
}
.msg-title {
line-height: 36px;
padding: 0 15px;
font-size: 16px;
font-weight: bold;
color: #333;
}
.msg-main {
padding: 10px 15px;
font-size: 16px;
line-height: 28px;
color: #333;
}
.tag-name {
pointer-events: none;
width: 80px;
height: 36px;
font-size: 16px;
font-weight: 700;
line-height: 36px;
position: absolute;
overflow: visible;
color: #FF3333;
background: rgba(249, 205, 205, .4);
text-align: center;
border-radius: 3px;
}
</style>
</head>
<body>
<div id="myViewer"></div>
<div class="msg-box">
<h3 class="msg-title">构件信息:</h3>
<div class="msg-main">测试信息</div>
</div>
<script src="static/jquery.min.js"></script>
<script src="https://developer.api.autodesk.com/derivativeservice/v2/viewers/three.min.js?v=v2.17"></script>
<script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/6.*/viewer3D.min.js"></script>
<script>
(function () {
//replace with your own urns
this.urn_model1 = "./bim/1/0.svf";
this.urn_model2 = "./bim/2/0.svf";
this.urn_model3 = "./bim/3/0.svf";
this.urn_model4 = "./bim/4/0.svf";
this.urn_model5 = "./bim/5/xiaojiaoche.3DS.svf";
this.urn_model6 = "./cailiao/travel/0.svf";
this.urn_model7 = "./bim/6/Sphere.svf";
//model info array
this.modelArray = [
{
modelName: 'urn_model1',
path: urn_model1,
modelObj: null,
},
{
modelName: 'urn_model2',
path: urn_model2,
modelObj: null
},
{
modelName: 'urn_model3',
path: urn_model3,
modelObj: null
},
{
modelName: 'urn_model4',
path: urn_model4,
modelObj: null
},
{
modelName: 'urn_model5',
path: urn_model7,
modelObj: null
},
{
modelName: 'urn_model6',
path: urn_model7,
modelObj: null
}
];
this.loadMore = [
{
modelName: 'load_model1',
path: urn_model7,
modelObj: null,
},
{
modelName: 'load_model2',
path: urn_model7,
modelObj: null,
},
{
modelName: 'load_model3',
path: urn_model7,
modelObj: null,
},
{
modelName: 'load_model4',
path: urn_model7,
modelObj: null,
},
{
modelName: 'load_model5',
path: urn_model7,
modelObj: null,
},
];
this.db = {};
//viewer object
this.viewer = null;
function start() {
//option to initialize viewer.
var options = {
env: 'Local',
offline: 'true',
useADP: false,
};
//It looks the static function of Viewer does not support ES6
//still use ES5
Autodesk.Viewing.Initializer(options, function onInitialized() {
//get the viewer div
var viewerDiv = document.getElementById('myViewer');
//initialize the viewer object
viewer = new Autodesk.Viewing.Private.GuiViewer3D(viewerDiv, {
extensions: ["Autodesk.Viewing.MarkupsCore", "Autodesk.Viewing.ZoomWindow"]
});
// 关掉渐进式渲染来避免场景闪烁
viewer.setProgressiveRendering(false);
//load model one by one in sequence
globalPromise(modelArray);
// 多模型选择构建
viewer.addEventListener(Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT, function (event) {
if (event.selections.length === 0) {
// viewer.clearThemingColors();
return;
}
var color = new THREE.Vector4(0, 0.5, 0, 1);
var id = event.selections[0].dbIdArray[0];
viewer.setThemingColor(id, color);
var select_info = viewer.getAggregateSelection();
var html = '<p>当前构件dbid:' + select_info[0].selection[0] + '</p>';
html += '<p>当前模型id:' + select_info[0].model.id + '</p>';
html += '<p>模型地址:' + select_info[0].model.getData().urn + '</p>';
$(".msg-main").html(html);
console.log(event); // 当前选中信息集
console.log(select_info); // 当前选中id
});
// 摄像头新事件 当摄像头的 Transition 完成后就会被触发
viewer.addEventListener(Autodesk.Viewing.CAMERA_TRANSITION_COMPLETED, function () {
});
// 摄像头变化事件
viewer.addEventListener(Autodesk.Viewing.CAMERA_CHANGE_EVENT, function (rvt) {
//find out all pushpin markups
var $eles = $("div[id^='mymk']");
var DOMeles = $eles.get();
for (var index in DOMeles) {
//get each DOM element
var DOMEle = DOMeles[index];
var divEle = $('#' + DOMEle.id);
//get out the 3D coordination
var val = divEle.data('3DData');
var pushpinModelPt = JSON.parse(val);
//get the updated screen point
/*var screenpoint = viewer.worldToClient(new THREE.Vector3(
pushpinModelPt.x,
pushpinModelPt.y,
pushpinModelPt.z,));*/
var screenpoint = worldToClient(pushpinModelPt.id, db[pushpinModelPt.mid]);
//update the SVG position.
divEle.css({
'left': screenpoint.x,
'top': screenpoint.y
});
}
});
// 动画初始化触发
viewer.addEventListener(Autodesk.Viewing.ANIMATION_READY_EVENT, function (e) {
console.log(e)
});
});
}
//load model by promise
globalPromise = (modelArray) => {
var _this = this;
//each promise function
//input the index of model array
function promiseEachModel(index) {
return new Promise((resolve, reject) => {
var modelName = modelArray[index].modelName;
var _index = index;
_this.globalDocumentLoad(modelArray[index],
_onLoadModelSuccess,
_onLoadModelError);
//when the model is loaded
function _onLoadModelSuccess(model) {
db[model.id] = model;
if ([4, 5].includes(index)) {
setTimeout(function () {
drawPushpin(2, model)
}, 1000);
}
_this.viewer.addEventListener(
Autodesk.Viewing.GEOMETRY_LOADED_EVENT,
_onGeometryLoaded);
//map this item with the corresponding model in case of use
modelArray[index].modelObj = model
}
function _onLoadModelError(viewerErrorCode) {
console.error(modelName +
': Load Model Error, errorCode:' +
viewerErrorCode);
//any error
reject(modelName + ' Loading Failed!' + viewerErrorCode);
}
function _onGeometryLoaded(evt) {
if (!["urn_model2", "urn_model3", "urn_model5", "urn_model6"].includes(modelArray[index].modelName)) {
// _this.viewer.fitToView(evt.model.id)
var red = new THREE.Vector4(0, 0.5, 0, 0.5);
var it = evt.model.getData().instanceTree;
// viewer.setThemingColor(it.nodeAccess.rootId, red);
// console.log(it.nodeAccess.rootId)
// console.log(it)
for (var i in it.nodeAccess.dbIdToIndex) {
_this.viewer.setThemingColor(i, red, evt.model);
}
}
if (modelArray[index].modelName === "urn_model5") {
// console.log(evt.model.getData())
// 调整焦距
var focal = _this.viewer.getFocalLength();
_this.viewer.setFocalLength(focal - 20);
// 聚焦指定模型
_this.viewer.fitToView([1], evt.model);
var red = new THREE.Vector4(0.5, 0, 0, 0.5);
if (it) {
for (var i in it.nodeAccess.dbIdToIndex) {
_this.viewer.setThemingColor(i, red, evt.model);
}
}
}
if (modelArray[index].modelName === "urn_model6") {
// console.log(evt.model.getData())
var red = new THREE.Vector4(0, 0, 0.5, 0.5);
var it = evt.model.getData().instanceTree;
// viewer.setThemingColor(it.nodeAccess.rootId, red);
// console.log(it)
if (it) {
for (var i in it.nodeAccess.dbIdToIndex) {
_this.viewer.setThemingColor(i, red, evt.model);
}
}
}
if (index === 4) {
/*var model = evt.model;
var fragIdsArray = [0];
var i = 1;
var fragProxy = viewer.impl.getFragmentProxy(model, 0);
var timer = setInterval(function () {
// fragIdsArray.forEach((fragId, idx) => {
fragProxy.getAnimTransform();
fragProxy.position.x -= 0.1;
fragProxy.position.z -= 0.1;
fragProxy.position.y -= 0.1;
fragProxy.updateAnimTransform();
// });
// viewer.impl.sceneUpdated(true);
// viewer.impl.invalidate(false,true,true);
var act_dom = ".mymkid_" + model.id;
var screenpoint = worldToClient(2, model);
//update the SVG position.
$(act_dom).css({
'left': screenpoint.x,
'top': screenpoint.y
});
i++;
if (i >= 20) clearInterval(timer)
}, 1000);*/
var model = evt.model;
requestAnimationFrame( rotateHandler );
// 动画函数
var i = 1;
function rotateHandler(){
var fragProxy = viewer.impl.getFragmentProxy(model, 0);
// fragIdsArray.forEach((fragId, idx) => {
fragProxy.getAnimTransform();
fragProxy.position.x -= 0.1;
fragProxy.position.z -= 0.1;
fragProxy.position.y -= 0.1;
fragProxy.updateAnimTransform();
// });
viewer.impl.sceneUpdated(true);
var act_dom = ".mymkid_" + model.id;
var screenpoint = worldToClient(2, model);
//update the SVG position.
$(act_dom).css({
'left': screenpoint.x,
'top': screenpoint.y
});
i++;
// 500毫秒后要求下一个动画 frame
if(i>20) return;
setTimeout( () => {
requestAnimationFrame( rotateHandler );
}, 500 );
}
}
if (index === 5) {
// setTimeout(createTexture, 500);
var model = evt.model;
var fragProxy = viewer.impl.getFragmentProxy(model, 0);
fragProxy.getAnimTransform();
fragProxy.position.x += 0.005;
fragProxy.position.z += 0.001;
fragProxy.position.y += 0.005;
fragProxy.updateAnimTransform();
viewer.impl.sceneUpdated(true);
}
_this.viewer.removeEventListener(
Autodesk.Viewing.GEOMETRY_LOADED_EVENT,
_onGeometryLoaded);
resolve(modelArray[index]);
}
}); //end of new promise
} //end of each promise function
// this.modelArray.length
var indexArr = Array.apply(null, Array(this.modelArray.length)).map((item, index) => index);
//proces each promise
//refer to http://jsfiddle.net/jfriend00/h3zaw8u8/
function processArray(array, fn) {
var results = [];
return array.reduce(function (p, item) {
return p.then(function () {
return fn(item).then(function (data) {
results.push(data);
return results;
});
});
}, Promise.resolve());
}
//start to process
processArray(indexArr, promiseEachModel).then(function (result) {
//全部加载完成
console.log(result);
result.forEach(item => { // 透明目标模型
if (["urn_model2", "urn_model3"].includes(item.modelName)) viewer.hide(item.modelObj.id, item.modelObj);
});
var offset = {
x: 2.9802322387695312e-8,
y: 0.5,
z: 5.288600168906754e-9
};
// 动态添加模型
setTimeout(function () {
loadMore.forEach(function (item) {
viewer.loadModel(item.path,
{globalOffset: offset},
function (model) {
db[model.id] = model;
setTimeout(function () {
var red = new THREE.Vector4(Math.random(),Math.random(), Math.random(), Math.random());
var it = model.getData().instanceTree;
// viewer.setThemingColor(it.nodeAccess.rootId, red);
// console.log(it.nodeAccess.rootId)
// console.log(it)
for (var i in it.nodeAccess.dbIdToIndex) {
viewer.setThemingColor(i, red, model);
}
drawPushpin(2, model)
}, 1000);
},
function (err) {
console.log(err)
});
offset.y -= 0.5;
offset.x += 0.1;
})
}, 2000)
}, function (reason) {
console.log(reason);
});
}; //end of function globalPromise
var mat = new THREE.Matrix4();
//input the transformation
var loadOptions = {
placementTransform: mat,
};
//when document is being loaded
globalDocumentLoad = (doc, _onLoadModelSuccess, _onLoadModelError) => {
//if this is the first model
if (doc.modelName === "urn_model1") {
//load the first model
this.viewer.start(doc.path,
loadOptions,
_onLoadModelSuccess,
_onLoadModelError);
} else {
//other models
if (doc.modelName === "urn_model5") {
// loadOptions.globalOffset = {x: 100, y: 0, z:100};
} else if (doc.modelName === "urn_model6") {
loadOptions.globalOffset = {x: 2.55555555444, y: 0.111111111111, z: 2.11111111111111};
} else {
delete loadOptions.globalOffset
}
this.viewer.loadModel(doc.path,
loadOptions,
_onLoadModelSuccess,
_onLoadModelError);
}
};
// 调用
start();
// 生成div标签id
function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
// 生成标签
function drawPushpin(id, model) {
var screenpoint = worldToClient(id, model);
//convert 3D position to 2D screen coordination
/*var screenpoint = viewer.worldToClient(
new THREE.Vector3(pushpinModelPt.x,
pushpinModelPt.y,
pushpinModelPt.z,));*/
//build the div container
var randomId = makeid();
var htmlMarker = '<div id="mymk' + randomId + '" class="tag-name mymkid_'+model.id+'">张三</div>';
var parent = viewer.container;
$(parent).append(htmlMarker);
//build the svg element and draw a circle
//adjust to make the circle center is the position of the click point
var $container = $('#mymk' + randomId);
$container.css({
'left': screenpoint.x,
'top': screenpoint.y
});
//store 3D point data to the DOM
var div = $('#mymk' + randomId);
//add radius info with the 3D data
// var storeData = JSON.stringify(screenpoint);
var storeData = JSON.stringify({id: id, mid: model.id});
div.data('3DData', storeData);
}
// 计算屏幕位置
function worldToClient(dbid, model) {
var it = model.getData().instanceTree;
var gps = null;
it.enumNodeFragments(dbid, (fragId, dbid) => {
var nodebBox = new THREE.Box3();
var fragbBox = new THREE.Box3();
var fragList = model.getFragmentList();
fragList.getWorldBounds(fragId, fragbBox);
nodebBox.union(fragbBox);
var center = nodebBox.center();
gps = viewer.worldToClient(center);
}, true);
return gps;
}
}())
</script>
</body>
</html>