3Dpic.js 7.0 KB

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