当前位置:   article > 正文

three.js 加载stl文件并解析_three 加载stl

three 加载stl

效果:

加载stl格式的文件需要设置材质material;stl、obj都是静态模型,不可以包含动画,fbx除了包含几何、材质信息,可以存储骨骼动画等数据。

代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>.stl格式模型加载</title>
  8. <script src="../three.js"></script>
  9. <script src="../OrbitControls.js"></script>
  10. <script src="../STLLoader.js"></script>
  11. </head>
  12. <body>
  13. <script>
  14. let N = 128;
  15. let analyser = null;
  16. // 1,创建场景
  17. var scene = new THREE.Scene();
  18. // 创建STL加载器
  19. var stlLoader = new THREE.STLLoader();
  20. stlLoader.load('./LH1223链条组装 - 短链条螺栓.STL',geometry => {
  21. var material = new THREE.MeshPhongMaterial({color:0xDDDADA});
  22. var mesh = new THREE.Mesh(geometry,material);
  23. scene.add(mesh);
  24. })
  25. // 3,创建灯光
  26. var point = new THREE.PointLight(0xffffff);
  27. point.position.set(200,200,200);
  28. scene.add(point);
  29. var point2 = new THREE.PointLight(0xffffff);
  30. point2.position.set(200,200,-200);
  31. scene.add(point2);
  32. // 4,创建相机对象
  33. var width = window.innerWidth;
  34. var height = window.innerHeight;
  35. var k = width / height;
  36. var s = 200;
  37. var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, -2000, 2000);
  38. camera.position.set(100, 100, 120);
  39. camera.lookAt(scene.position);
  40. // 5,创建渲染器
  41. var renderer = new THREE.WebGLRenderer();
  42. renderer.setSize(width, height);
  43. document.body.appendChild(renderer.domElement);
  44. function render() {
  45. renderer.render(scene, camera);
  46. requestAnimationFrame(render);
  47. }
  48. var controls = new THREE.OrbitControls(camera, renderer.domElement);
  49. var axes = new THREE.AxesHelper(500);
  50. scene.add(axes);
  51. render();
  52. </script>
  53. </body>
  54. </html>

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