3Dpic.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. //const category = ['1', '2', '3'];
  11. window.initThreeJS = init;
  12. function init() {
  13. canvas = document.getElementById("c");
  14. const content = document.getElementById('content');
  15. for (let i = 0; i < rvtName.length; i++) {
  16. const scene = new THREE.Scene();
  17. // make a list item
  18. const isotope = document.createElement('div');
  19. isotope.className = 'isotope-item ' + categoryTable[i] + ' ' + year[i] + ' col-sm-6 col-md-4 col-lg-3';
  20. content.appendChild(isotope);
  21. const thumbnail = document.createElement('div');
  22. thumbnail.className = 'thumbnail';
  23. isotope.appendChild(thumbnail);
  24. const element = document.createElement('div');
  25. //element.className = 'thumb-preview list-item';
  26. element.className = 'thumb-preview';
  27. //element.className = 'list-item';
  28. thumbnail.appendChild(element);
  29. const a = document.createElement('div');
  30. a.className = "thumb-image";
  31. a.href = "assets/images/projects/project-8.png";
  32. element.appendChild(a);
  33. /*
  34. const thumbImage = document.createElement('a');
  35. thumbImage.className = 'thumb-image';
  36. preview.appendChild(thumbImage);
  37. const element = document.createElement( 'div' );
  38. element.className = 'list-item';
  39. */
  40. const sceneElement = document.createElement('div');
  41. element.appendChild(sceneElement);
  42. const img = document.createElement('img');
  43. img.src = "assets/images/projects/project-8.png";
  44. img.className = 'img-responsive';
  45. img.alt = 'Project';
  46. sceneElement.appendChild(img);
  47. // the element that represents the area we want to render the scene
  48. scene.userData.element = sceneElement;
  49. thumbnail.appendChild(element);
  50. const h5 = document.createElement('a');
  51. h5.innerText = rvtName[i];
  52. h5.setAttribute("href", "component-detail.php?object=" + rvtName[i] + "&type=" + type);
  53. thumbnail.appendChild(h5);
  54. const smallRvt = document.createElement('small');
  55. smallRvt.innerText = ".rvt";
  56. h5.appendChild(smallRvt);
  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. /*var year = Math.floor(Math.random() * 11) + 2010;
  65. var month = Math.floor(Math.random() * 12) + 1;
  66. var date = Math.floor(Math.random() * 28) + 1;
  67. const pullRight = document.createElement('small');
  68. pullRight.className = 'pull-right text-muted';
  69. pullRight.innerText = year + '/' + month + '/' + date;
  70. mgdescription.appendChild(pullRight);*/
  71. const camera = new THREE.PerspectiveCamera(50, 1, 1, 1000);
  72. camera.position.set(0, 2.5, 8);
  73. scene.userData.camera = camera;
  74. //scene = new THREE.Scene();
  75. scene.background = new THREE.Color(0x15608c);
  76. const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444);
  77. hemiLight.position.set(0, 20, 0);
  78. scene.add(hemiLight);
  79. const spotLight = new THREE.SpotLight(0xffffff);
  80. spotLight.position.set(15, 40, 35);
  81. spotLight.castShadow = true;
  82. spotLight.intensity = 0.5;
  83. scene.add(spotLight);
  84. const spotLight2 = new THREE.SpotLight(0xffffff);
  85. spotLight2.position.set(15, 40, -35);
  86. spotLight2.castShadow = true;
  87. spotLight2.intensity = 0.5;
  88. scene.add(spotLight2);
  89. const dirLight = new THREE.DirectionalLight(0xffffff);
  90. dirLight.position.set(5, 5, 0);
  91. dirLight.castShadow = true;
  92. dirLight.shadow.camera.top = 1;
  93. dirLight.shadow.camera.bottom = -1;
  94. dirLight.shadow.camera.left = -1;
  95. dirLight.shadow.camera.right = 1;
  96. dirLight.shadow.camera.near = 0.1;
  97. dirLight.shadow.camera.far = 20;
  98. scene.add(dirLight);
  99. const mesh = new THREE.Mesh(new THREE.PlaneGeometry(50, 50), new THREE.MeshPhongMaterial({
  100. color: 0x999999,
  101. depthWrite: false
  102. }));
  103. mesh.rotation.x = -Math.PI / 2;
  104. mesh.receiveShadow = true;
  105. scene.add(mesh);
  106. const grid = new THREE.GridHelper(50, 50, 0x888888, 0x888888);
  107. scene.add(grid);
  108. const controls = new OrbitControls(scene.userData.camera, scene.userData.element);
  109. controls.target.set(0, 2, 0);
  110. controls.minDistance = 0.5;
  111. controls.maxDistance = 10;
  112. controls.maxPolarAngle = 0.5 * Math.PI;
  113. controls.autoRotate = true;
  114. scene.userData.controls = controls;
  115. var loader = new GLTFLoader();
  116. loader.setPath("./assets/glb/Revit元件/Components/Arch/MEP/" + type + "/");
  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 updateSize() {
  156. const width = canvas.clientWidth;
  157. const height = canvas.clientHeight;
  158. if (canvas.width !== width || canvas.height !== height) {
  159. renderer.setSize(width, height, false);
  160. }
  161. }
  162. function animate() {
  163. render();
  164. requestAnimationFrame(animate);
  165. }
  166. function render() {
  167. updateSize();
  168. canvas.style.transform = `translateY(${window.scrollY}px)`;
  169. renderer.setClearColor(0xffffff);
  170. renderer.setScissorTest(false);
  171. renderer.clear();
  172. renderer.setClearColor(0xe0e0e0);
  173. renderer.setScissorTest(true);
  174. scenes.forEach(function (scene) {
  175. // get the element that is a place holder for where we want to
  176. // draw the scene
  177. const element = scene.userData.element;
  178. // get its position relative to the page's viewport
  179. const rect = element.getBoundingClientRect();
  180. // check if it's offscreen. If so skip it
  181. if (rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  182. rect.right < 0 || rect.left > renderer.domElement.clientWidth) {
  183. return; // it's off screen
  184. }
  185. // set the viewport
  186. const width = rect.right - rect.left;
  187. const height = rect.bottom - rect.top;
  188. const left = rect.left;
  189. const bottom = renderer.domElement.clientHeight - rect.bottom;
  190. renderer.setViewport(left, bottom, width, height);
  191. renderer.setScissor(left, bottom, width, height);
  192. const camera = scene.userData.camera;
  193. //camera.aspect = width / height; // not changing in this example
  194. //camera.updateProjectionMatrix();
  195. scene.userData.controls.update();
  196. renderer.render(scene, camera);
  197. });
  198. }