3Dpic-favorite.js 7.8 KB

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