3Dpic-detail.js 6.1 KB

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