3Dpic-detail.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 scene, camera, controls, renderer;
  9. const scenes = [];
  10. init();
  11. animate();
  12. function init() {
  13. const container = document.getElementById('content');
  14. scene = new THREE.Scene();
  15. camera = new THREE.PerspectiveCamera(50, 1, 1, 1000);
  16. camera.position.set(0, 2.5, 8);
  17. scene.userData.camera = camera;
  18. scene.background = new THREE.Color(0x15608c);
  19. const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444);
  20. hemiLight.position.set(0, 20, 0);
  21. scene.add(hemiLight);
  22. const spotLight = new THREE.SpotLight(0xffffff);
  23. spotLight.position.set(15, 40, 35);
  24. spotLight.castShadow = true;
  25. spotLight.intensity = 0.5;
  26. scene.add(spotLight);
  27. const spotLight2 = new THREE.SpotLight(0xffffff);
  28. spotLight2.position.set(15, 40, -35);
  29. spotLight2.castShadow = true;
  30. spotLight2.intensity = 0.5;
  31. scene.add(spotLight2);
  32. const dirLight = new THREE.DirectionalLight(0xffffff);
  33. dirLight.position.set(5, 5, 0);
  34. dirLight.castShadow = true;
  35. dirLight.shadow.camera.top = 1;
  36. dirLight.shadow.camera.bottom = -1;
  37. dirLight.shadow.camera.left = -1;
  38. dirLight.shadow.camera.right = 1;
  39. dirLight.shadow.camera.near = 0.1;
  40. dirLight.shadow.camera.far = 20;
  41. scene.add(dirLight);
  42. const mesh = new THREE.Mesh(new THREE.PlaneGeometry(50, 50), new THREE.MeshPhongMaterial({
  43. color: 0x999999,
  44. depthWrite: false
  45. }));
  46. mesh.rotation.x = -Math.PI / 2;
  47. mesh.receiveShadow = true;
  48. scene.add(mesh);
  49. const grid = new THREE.GridHelper(50, 50, 0x888888, 0x888888);
  50. scene.add(grid);
  51. var loader = new GLTFLoader();
  52. loader.setPath("./assets/glb/Revit元件/" + parent + "/");
  53. loader.load(name + ".glb", function (gltf) {
  54. var mroot = gltf.scene;
  55. var bbox = new THREE.Box3().setFromObject(mroot);
  56. var cent = bbox.getCenter(new THREE.Vector3());
  57. var size = bbox.getSize(new THREE.Vector3());
  58. //Rescale the object to normalized space
  59. var maxAxis = Math.max(size.x, size.y, size.z);
  60. mroot.scale.multiplyScalar(5.0 / maxAxis);
  61. bbox.setFromObject(mroot);
  62. bbox.getCenter(cent);
  63. bbox.getSize(size);
  64. //Reposition to 0,halfY,0
  65. mroot.position.copy(cent).multiplyScalar(-1);
  66. mroot.position.y += (size.y * 0.5);
  67. mroot.traverse(function (object) {
  68. if (object.isMesh) {
  69. object.castShadow = true;
  70. if (object.material.name == '玻璃') {
  71. object.material.transparent = true;
  72. object.material.opacity = 0;
  73. }
  74. }
  75. });
  76. scene.add(mroot);
  77. });
  78. scenes.push(scene);
  79. renderer = new THREE.WebGLRenderer({
  80. antialias: true
  81. });
  82. renderer.setClearColor(0xffffff, 1);
  83. renderer.setPixelRatio(window.devicePixelRatio);
  84. renderer.outputEncoding = THREE.sRGBEncoding;
  85. renderer.shadowMap.enabled = true;
  86. controls = new OrbitControls(camera, renderer.domElement);
  87. controls.target.set(0, 2, 0);
  88. controls.minDistance = 0.5;
  89. controls.maxDistance = 10;
  90. controls.maxPolarAngle = 0.5 * Math.PI;
  91. controls.autoRotate = true;
  92. scene.userData.controls = controls;
  93. container.appendChild(renderer.domElement);
  94. }
  95. function animate() {
  96. //render();
  97. const canvas = renderer.domElement.parentNode.parentNode;
  98. const width = canvas.clientWidth;
  99. const height = canvas.clientHeight;
  100. if (canvas.width !== width || canvas.height !== width) {
  101. renderer.setSize(width-30, width-30, false);
  102. }
  103. renderer.render(scene, camera);
  104. controls.update();
  105. requestAnimationFrame(animate);
  106. }