3Dpic-favorite.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 < id.length; i++) {
  16. var type = path[i].split('/');
  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. isotope.className = 'isotope-item ' + revitVersion[i] + ' ' + categoryTable[i] + ' col-sm-6 col-md-4 col-lg-3';
  22. content.appendChild(isotope);
  23. const thumbnail = document.createElement('div');
  24. thumbnail.className = 'thumbnail2';
  25. isotope.appendChild(thumbnail);
  26. const element = document.createElement('div');
  27. //element.className = 'thumb-preview list-item';
  28. element.className = 'thumb-preview';
  29. //element.className = 'list-item';
  30. thumbnail.appendChild(element);
  31. const a = document.createElement('a');
  32. a.className = "thumb-image";
  33. a.href = "assets/images/projects/project-8.png";
  34. element.appendChild(a);
  35. /*
  36. const thumbImage = document.createElement('a');
  37. thumbImage.className = 'thumb-image';
  38. preview.appendChild(thumbImage);
  39. const element = document.createElement( 'div' );
  40. element.className = 'list-item';
  41. */
  42. const sceneElement = document.createElement('div');
  43. element.appendChild(sceneElement);
  44. const img = document.createElement('img');
  45. img.src = "assets/images/projects/project-8.png";
  46. img.className = 'img-responsive';
  47. img.alt = 'Project';
  48. sceneElement.appendChild(img);
  49. // the element that represents the area we want to render the scene
  50. scene.userData.element = sceneElement;
  51. thumbnail.appendChild(element);
  52. const h5 = document.createElement('a');
  53. h5.innerText = type[1];
  54. h5.setAttribute("href","component-detail.php?object="+type[1]+"&type="+type[0]);
  55. thumbnail.appendChild(h5);
  56. const smallRvt = document.createElement('small');
  57. smallRvt.innerText = ".rvt";
  58. h5.appendChild(smallRvt);
  59. const mgdescription = document.createElement('div');
  60. mgdescription.className = 'mg-description';
  61. thumbnail.appendChild(mgdescription);
  62. const checkbox = document.createElement('input');
  63. checkbox.type = "checkbox";
  64. checkbox.value = id[i];
  65. checkbox.name = "delCheck";
  66. mgdescription.appendChild(checkbox);
  67. const pullRight = document.createElement('small');
  68. pullRight.className = 'pull-right text-muted';
  69. pullRight.innerText = 'Revit '+ revitVersion[i];
  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[0] + "/");
  117. var name = type[1] + ".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. }
  199. $('input[name="delCheck"]').change(function() {
  200. delFavoriteList = [];
  201. $('input[name="delCheck"]:checked').each(function() {
  202. delFavoriteList.push($(this).val());
  203. });
  204. });