xuehualu0413-three-online.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="utf-8" />
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  5. <meta name="renderer" content="webkit">
  6. <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=no"/>
  7. <title>BimAngle.com - Trial version</title>
  8. <link rel="stylesheet" type="text/css" href="http://fs3.bimangle.net/js/three-gltf-viewer/gltf-viewer.css">
  9. </head>
  10. <body>
  11. <main class="wrap">
  12. <div id="viewer-local" class="viewer"></div>
  13. <div class="spinner"></div>
  14. </main>
  15. <script src="http://fs3.bimangle.net/js/three-gltf-viewer/gltf-viewer.js"></script>
  16. <script>
  17. //是否启用构件数据
  18. const EnableElementData = true;
  19. </script>
  20. <script>
  21. //three-gltf-viewer (BimAngle modified version)
  22. // dist: http://fs3.bimangle.net/js/three-gltf-viewer_dist.zip
  23. // src: http://fs3.bimangle.net/js/three-gltf-viewer_src.zip
  24. const hash = location.hash ? queryString.parse(location.hash) : {};
  25. let modelUrl = hash.model ||'./xuehualu0413.gltf';
  26. let element = document.getElementById('viewer-local');
  27. let viewer = new Viewer(element, {}, EnableElementData);
  28. viewer.load(modelUrl)
  29. .then(onSuccess)
  30. .catch(onError);
  31. function onSuccess(){
  32. let spinnerEl = document.querySelector('.spinner');
  33. if(spinnerEl) spinnerEl.style.display = 'none';
  34. console.log('Succeeded!');
  35. }
  36. function onError (error) {
  37. let message = (error||{}).message || error.toString();
  38. if (message.match(/ProgressEvent/)) {
  39. message = 'Unable to retrieve this file. Check JS console and browser network tab.';
  40. } else if (message.match(/Unexpected token/)) {
  41. message = `Unable to parse file content. Verify that this file is valid. Error: "${message}"`;
  42. } else if (error && error.target && error.target instanceof Image) {
  43. message = 'Missing texture: ' + error.target.src.split('/').pop();
  44. }
  45. window.alert(message);
  46. console.error(error);
  47. }
  48. </script>
  49. <script>
  50. function findElementNode(node){
  51. if(node.userData && node.userData.DbId){
  52. return node;
  53. }
  54. if(!!node.parent){
  55. return findElementNode(node.parent);
  56. }
  57. return null;
  58. }
  59. //输出场景树到控制台
  60. function printSceneTree(){
  61. viewer.model.printTree(true);
  62. }
  63. if(EnableElementData)
  64. {
  65. document.addEventListener("click", function(e){
  66. e.preventDefault();
  67. // https://segmentfault.com/a/1190000010490845
  68. //将鼠标点击位置的屏幕坐标转成threejs中的标准坐标,具体解释见代码释义
  69. const x = (e.clientX / window.innerWidth) * 2 - 1;
  70. const y = -(e.clientY / window.innerHeight) * 2 + 1;
  71. const results = viewer.hitTest(x,y);
  72. //console.log(result);
  73. if(results && results.length > 0){
  74. for(const r of results){
  75. var node = findElementNode(r.object);
  76. if(node != null){
  77. const data = node.userData;
  78. //r.object.material = viewer.selectionMaterialTop; //[viewer.selectionMaterialBase, r.object.material, viewer.selectionMaterialTop];
  79. console.log(`Click DbId: ${data.DbId}, Name: ${data.name}, ExId:${data.ExId}`);
  80. if(viewer.model && viewer.model.isReady){
  81. const dbId = data.DbId;
  82. //const dbIds = viewer.model.collectDbIds(dbId);
  83. viewer.model.select(dbId);
  84. }
  85. break;
  86. }
  87. }
  88. }else{
  89. if(viewer.model && viewer.model.isReady){
  90. //console.log(`Click DbId: None`);
  91. viewer.model.select([]);
  92. }
  93. }
  94. }
  95. ,false);
  96. document.addEventListener("mousemove", function(e){
  97. e.preventDefault();
  98. // https://segmentfault.com/a/1190000010490845
  99. //将鼠标点击位置的屏幕坐标转成threejs中的标准坐标,具体解释见代码释义
  100. const x = (e.clientX / window.innerWidth) * 2 - 1;
  101. const y = -(e.clientY / window.innerHeight) * 2 + 1;
  102. const results = viewer.hitTest(x,y);
  103. //console.log(result);
  104. if(results && results.length > 0){
  105. for(const r of results){
  106. var node = findElementNode(r.object);
  107. if(node != null){
  108. const data = node.userData;
  109. //r.object.material = viewer.selectionMaterialTop; //[viewer.selectionMaterialBase, r.object.material, viewer.selectionMaterialTop];
  110. console.log(`Hover DbId: ${data.DbId}, Name: ${data.name}, ExId:${data.ExId}`);
  111. if(viewer.model && viewer.model.isReady){
  112. const dbId = data.DbId;
  113. //const dbIds = viewer.model.collectDbIds(dbId);
  114. viewer.model.highlight(dbId);
  115. }
  116. break;
  117. }
  118. }
  119. }else{
  120. if(viewer.model && viewer.model.isReady){
  121. //console.log(`Hover DbId: None`);
  122. viewer.model.highlight([]);
  123. }
  124. }
  125. }, false);
  126. }
  127. </script>
  128. </body>