bimMain.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. let canvas, renderer;
  2. const scenes = [];
  3. const type = ['機械設備', '燈具', '火警裝置'];
  4. init();
  5. animate();
  6. function init() {
  7. canvas = document.getElementById("c");
  8. const content = document.getElementById('content');
  9. for (let i = 0; i < id.length; i++) {
  10. const scene = new THREE.Scene();
  11. //scene.background = new THREE.Color(0x000000);
  12. var typeID = Math.floor(Math.random() * 3);
  13. // make a list item
  14. const isotope = document.createElement('div');
  15. isotope.className = 'isotope-item ' + type[typeID] + ' col-sm-6 col-md-4 col-lg-3';
  16. content.appendChild(isotope);
  17. const thumbnail = document.createElement('div');
  18. thumbnail.className = 'thumbnail';
  19. isotope.appendChild(thumbnail);
  20. const element = document.createElement('div');
  21. //element.className = 'thumb-preview list-item';
  22. element.className = 'thumb-preview';
  23. //element.className = 'list-item';
  24. thumbnail.appendChild(element);
  25. const a = document.createElement('a');
  26. a.className = "thumb-image";
  27. a.href = "/Common/assets/images/projects/project-8.png";
  28. element.appendChild(a);
  29. /*
  30. const thumbImage = document.createElement('a');
  31. thumbImage.className = 'thumb-image';
  32. preview.appendChild(thumbImage);
  33. const element = document.createElement( 'div' );
  34. element.className = 'list-item';
  35. */
  36. const sceneElement = document.createElement('div');
  37. element.appendChild(sceneElement);
  38. const img = document.createElement('img');
  39. img.src = "/Common/assets/images/projects/project-8.png";
  40. img.className = 'img-responsive';
  41. img.alt = 'Project';
  42. sceneElement.appendChild(img);
  43. // the element that represents the area we want to render the scene
  44. scene.userData.element = sceneElement;
  45. thumbnail.appendChild(element);
  46. const h5 = document.createElement('h5');
  47. h5.innerText = rvtName[i];
  48. thumbnail.appendChild(h5);
  49. const smallRvt = document.createElement('small');
  50. smallRvt.innerText = ".rvt";
  51. h5.appendChild(smallRvt);
  52. const mgdescription = document.createElement('div');
  53. mgdescription.className = 'mg-description';
  54. thumbnail.appendChild(mgdescription);
  55. const pullLeft = document.createElement('small');
  56. pullLeft.className = 'pull-left text-muted';
  57. pullLeft.innerText = 'Design, Websites';
  58. mgdescription.appendChild(pullLeft);
  59. var year = Math.floor(Math.random() * 11) + 2010;
  60. var month = Math.floor(Math.random() * 12) + 1;
  61. var date = Math.floor(Math.random() * 28) + 1;
  62. const pullRight = document.createElement('small');
  63. pullRight.className = 'pull-right text-muted';
  64. pullRight.innerText = year + '/' + month + '/' + date;
  65. mgdescription.appendChild(pullRight);
  66. const camera = new THREE.PerspectiveCamera(50, 1, 1, 1000);
  67. camera.position.set(0, 2.5, 8);
  68. scene.userData.camera = camera;
  69. //scene = new THREE.Scene();
  70. scene.background = new THREE.Color(0x15608c);
  71. const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444);
  72. hemiLight.position.set(0, 20, 0);
  73. scene.add(hemiLight);
  74. const spotLight = new THREE.SpotLight(0xffffff);
  75. spotLight.position.set(15, 40, 35);
  76. spotLight.castShadow = true;
  77. spotLight.intensity = 0.5;
  78. scene.add(spotLight);
  79. const dirLight = new THREE.DirectionalLight(0xffffff);
  80. dirLight.position.set(5, 5, 0);
  81. dirLight.castShadow = true;
  82. dirLight.shadow.camera.top = 1;
  83. dirLight.shadow.camera.bottom = -1;
  84. dirLight.shadow.camera.left = -1;
  85. dirLight.shadow.camera.right = 1;
  86. dirLight.shadow.camera.near = 0.1;
  87. dirLight.shadow.camera.far = 20;
  88. scene.add(dirLight);
  89. const mesh = new THREE.Mesh(new THREE.PlaneGeometry(50, 50), new THREE.MeshPhongMaterial({
  90. color: 0x999999,
  91. depthWrite: false
  92. }));
  93. mesh.rotation.x = -Math.PI / 2;
  94. mesh.receiveShadow = true;
  95. scene.add(mesh);
  96. const grid = new THREE.GridHelper(50, 50, 0x888888, 0x888888);
  97. scene.add(grid);
  98. const controls = new OrbitControls(scene.userData.camera, scene.userData.element);
  99. controls.target.set(0, 2, 0);
  100. controls.minDistance = 0.5;
  101. controls.maxDistance = 10;
  102. controls.maxPolarAngle = 0.5 * Math.PI;
  103. controls.autoRotate = true;
  104. scene.userData.controls = controls;
  105. var loader = new GLTFLoader();
  106. loader.setPath("./assets/glb/Revit元件/Components/Arch/MEP/CA/");
  107. var name = id[i] + ".glb";
  108. loader.load(name, function (gltf) {
  109. var mroot = gltf.scene;
  110. var bbox = new THREE.Box3().setFromObject(mroot);
  111. var cent = bbox.getCenter(new THREE.Vector3());
  112. var size = bbox.getSize(new THREE.Vector3());
  113. //Rescale the object to normalized space
  114. var maxAxis = Math.max(size.x, size.y, size.z);
  115. mroot.scale.multiplyScalar(5.0 / maxAxis);
  116. bbox.setFromObject(mroot);
  117. bbox.getCenter(cent);
  118. bbox.getSize(size);
  119. //Reposition to 0,halfY,0
  120. mroot.position.copy(cent).multiplyScalar(-1);
  121. mroot.position.y += (size.y * 0.5);
  122. mroot.traverse(function (object) {
  123. if (object.isMesh) {
  124. object.castShadow = true;
  125. if (object.material.name == '玻璃') {
  126. object.material.transparent = true;
  127. object.material.opacity = 0;
  128. }
  129. }
  130. });
  131. scene.add(mroot);
  132. });
  133. scenes.push(scene);
  134. }
  135. renderer = new THREE.WebGLRenderer({
  136. canvas: canvas,
  137. antialias: true
  138. });
  139. renderer.setClearColor(0xffffff, 1);
  140. renderer.setPixelRatio(window.devicePixelRatio);
  141. renderer.outputEncoding = THREE.sRGBEncoding;
  142. renderer.shadowMap.enabled = true;
  143. }
  144. function updateSize() {
  145. const width = canvas.clientWidth;
  146. const height = canvas.clientHeight;
  147. if (canvas.width !== width || canvas.height !== height) {
  148. renderer.setSize(width, height, false);
  149. }
  150. }
  151. function animate() {
  152. render();
  153. requestAnimationFrame(animate);
  154. }
  155. function render() {
  156. updateSize();
  157. canvas.style.transform = `translateY(${window.scrollY}px)`;
  158. renderer.setClearColor(0xffffff);
  159. renderer.setScissorTest(false);
  160. renderer.clear();
  161. renderer.setClearColor(0xe0e0e0);
  162. renderer.setScissorTest(true);
  163. scenes.forEach(function (scene) {
  164. // get the element that is a place holder for where we want to
  165. // draw the scene
  166. const element = scene.userData.element;
  167. // get its position relative to the page's viewport
  168. const rect = element.getBoundingClientRect();
  169. // check if it's offscreen. If so skip it
  170. if (rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  171. rect.right < 0 || rect.left > renderer.domElement.clientWidth) {
  172. return; // it's off screen
  173. }
  174. // set the viewport
  175. const width = rect.right - rect.left;
  176. const height = rect.bottom - rect.top;
  177. const left = rect.left;
  178. const bottom = renderer.domElement.clientHeight - rect.bottom;
  179. renderer.setViewport(left, bottom, width, height);
  180. renderer.setScissor(left, bottom, width, height);
  181. const camera = scene.userData.camera;
  182. //camera.aspect = width / height; // not changing in this example
  183. //camera.updateProjectionMatrix();
  184. scene.userData.controls.update();
  185. renderer.render(scene, camera);
  186. });
  187. }