3Dpic.js 7.1 KB

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