3Dpic.js 7.5 KB

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