当前位置:   article > 正文

使用three.js 简单加载STL格式3D模型_three.js加载stl

three.js加载stl

上述是简单版本

 一、使用three.js中的STLLoader加载模块

引入three.js 引入STLLoader加载模块。

  1. // 引入three.js
  2. import * as THREE from 'three';
  3. import { STLLoader } from 'three/examples/jsm/loaders/STLLoader';

  1. const containers = ref(null);
  2. let scene, camera, renderer, controls, raycaster, mouse, size, distance, boundingBox, mesh, initialCameraPosition, initialCameraTarget, modelCenter, labelRenderer;

初始化场景,相机,渲染函数,鼠标控制等全局变量。

 在初始化方法中,将灯光,环境光,渲染容器,相机,鼠标控制等进行初始化帮绑定。

  1. const initScene = () => {
  2. scene = new THREE.Scene();
  3. const bgColor = new THREE.Color("#e7e9e9");
  4. scene.background = bgColor;
  5. const ambient = new THREE.AmbientLight(0xffffff, 1);
  6. scene.add(ambient);
  7. const spotLight = new THREE.SpotLight(0xffffff, 1.0);
  8. spotLight.intensity = 2.0;
  9. spotLight.angle = Math.PI / 2;
  10. spotLight.position.set(0, 0, -200);
  11. scene.add(spotLight);
  12. const pointLight = new THREE.PointLight(0xffffff, 10);
  13. pointLight.position.set(400, 0, 0);
  14. scene.add(pointLight);
  15. const pointLightHelper = new THREE.PointLightHelper(pointLight, 10);
  16. scene.add(pointLightHelper);
  17. const directionalLight = new THREE.DirectionalLight(0xffffff, 2);
  18. directionalLight.position.set(200, 200, 200);
  19. scene.add(directionalLight);
  20. camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
  21. renderer = new THREE.WebGLRenderer();
  22. renderer.antialias = true
  23. renderer.setSize(window.innerWidth, window.innerHeight);
  24. renderer.setPixelRatio(window.devicePixelRatio)
  25. containers.value.appendChild(renderer.domElement);
  26. labelRenderer = new CSS2DRenderer();
  27. labelRenderer.setSize(containers.value.offsetWidth, containers.value.offsetHeight);
  28. labelRenderer.domElement.style.position = 'absolute';
  29. // 相对标签原位置位置偏移大小
  30. labelRenderer.domElement.style.top = '0px';
  31. labelRenderer.domElement.style.left = '0px';
  32. // //设置.pointerEvents=none,以免模型标签HTML元素遮挡鼠标选择场景模型
  33. labelRenderer.domElement.style.pointerEvents = 'none';
  34. containers.value.appendChild(labelRenderer.domElement);
  35. controls = new OrbitControls(camera, renderer.domElement);
  36. controls.enableZoom = false;
  37. controls.enableRotate = true;
  38. raycaster = new THREE.Raycaster();
  39. mouse = new THREE.Vector2();
  40. };

然后就是渲染加载stl模块及加载模型。'/modle/1.stl' 这里既可以是静态文件还可以是文件地址。

这一块主要是对加载模型材质的选择,并对加载的模型进行了描边,需要注意的是模型的缩放因子也需要在描边上添加,以及后面模型测量尺寸的展示都要除以这个缩放因子才是实际的尺寸。

这里还涉及到一个相机的初始位置的设置,模型初始化加载后,是呈现一个倾斜角度展示的,而不是看到的正面。并且拿全局变量进行了记录。在后面交互功能,相当于重置模型位置时要是用。

  1. const loadModel = () => {
  2. const loader = new STLLoader();
  3. loader.load('/modle/1.stl', (geometry) => {
  4. const material = new THREE.MeshPhongMaterial({
  5. color: '#918b84',
  6. side: THREE.DoubleSide,
  7. specular: '#918b84',
  8. shininess: 12
  9. });
  10. mesh = new THREE.Mesh(geometry, material);
  11. // var surfaceArea = computeSurfaceArea(geometry);
  12. // var volume = computeModelVolume(geometry);
  13. // console.log('表面积:', surfaceArea, "体积", volume.toFixed(3));
  14. // 缩放模型尺寸
  15. const scale = 0.4; // 缩放因子
  16. const edges = new THREE.EdgesGeometry(geometry); // 根据模型几何体生成边缘几何体
  17. const edgeMaterial = new THREE.MeshPhysicalMaterial({ color: 0x000000, linewidth: 2 });
  18. const edgeMesh = new THREE.LineSegments(edges, edgeMaterial); // 创建线条对象
  19. edgeMesh.scale.set(scale, scale, scale);
  20. scene.add(edgeMesh);
  21. mesh.scale.set(scale, scale, scale);
  22. // 计算模型边界
  23. boundingBox = new THREE.Box3().setFromObject(mesh);
  24. // 计算模型尺寸
  25. size = new THREE.Vector3();
  26. boundingBox.getSize(size);
  27. // 计算与边界框最远点的距离
  28. distance = size.length() * 2;
  29. // 计算辅助坐标轴中心位置
  30. modelCenter = new THREE.Vector3();
  31. boundingBox.getCenter(modelCenter);
  32. //设置旋转中心是模型的中心
  33. controls.target.copy(modelCenter);
  34. // 设置相机位置和目标点
  35. const cameraPosition = modelCenter.clone().add(new THREE.Vector3(1, 1, 1).normalize().multiplyScalar(distance));
  36. camera.position.copy(cameraPosition);
  37. camera.lookAt(modelCenter);
  38. initialCameraPosition = cameraPosition
  39. initialCameraTarget = modelCenter
  40. scene.add(mesh)
  41. });
  42. };

  1. const handleResize = () => {
  2. camera.aspect = window.innerWidth / window.innerHeight;
  3. camera.updateProjectionMatrix();
  4. renderer.setSize(window.innerWidth, window.innerHeight);
  5. labelRenderer.setSize(window.innerWidth, window.innerHeight);
  6. };
  7. const animate = () => {
  8. requestAnimationFrame(animate);
  9. controls.update();
  10. TWEEN.update();
  11. renderer.render(scene, camera);
  12. labelRenderer.render(scene, camera);
  13. };

最后一个是根据浏览器窗口重新设置尺寸,一个是调用渲染函数进行渲染,到此一个STL模型就加载出来了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/91543
推荐阅读
相关标签
  

闽ICP备14008679号