当前位置:   article > 正文

three.js 加载obj文件并解析_javascript three obj

javascript three obj

效果:

stl、obj都是静态模型,不可以包含动画,fbx除了包含几何、材质信息,可以存储骨骼动画等数据。

说明:使用三维软件导出.obj模型文件的时候,会同时导出一个材质文件.mtl;   .obj.stl文件包含的信息一样都是几何体顶点相关数据,材质文件.mtl包含的是模型的材质信息,比如颜色、贴图路径等。可以只加载obj文件,默认材质是Phong  ;

只加载obj文件的话,直接将返回的数据添加到场景中即可;

  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>加载.obj模型文件</title>
  8. <script src="../three.js"></script>
  9. <script src="../OrbitControls.js"></script>
  10. <script src="../OBJLoader.js"></script>
  11. </head>
  12. <body>
  13. <script>
  14. // 1,创建场景
  15. var scene = new THREE.Scene();
  16. // 创建STL加载器
  17. var objLoader = new THREE.OBJLoader();
  18. objLoader.load('./FREE Bike Hardsurface KitbashID_174738173/8b6b8b6e9ffd125f0a2f5632b94083818f2f9c69/Bike_Parts_01.obj',
  19. geometry => { scene.add(geometry);
  20. })
  21. objLoader.load('./FREE Bike Hardsurface KitbashID_174738173/8b6b8b6e9ffd125f0a2f5632b94083818f2f9c69/Bike_Parts_02.obj',
  22. geometry => { scene.add(geometry);
  23. })
  24. objLoader.load('./FREE Bike Hardsurface KitbashID_174738173/8b6b8b6e9ffd125f0a2f5632b94083818f2f9c69/Bike_Parts_03.obj',
  25. geometry => { scene.add(geometry);
  26. })
  27. objLoader.load('./FREE Bike Hardsurface KitbashID_174738173/8b6b8b6e9ffd125f0a2f5632b94083818f2f9c69/Bike_Parts_04.obj',
  28. geometry => { scene.add(geometry);
  29. })
  30. objLoader.load('./FREE Bike Hardsurface KitbashID_174738173/8b6b8b6e9ffd125f0a2f5632b94083818f2f9c69/Bike_Parts_05.obj',
  31. geometry => { scene.add(geometry);
  32. })
  33. // 3,创建灯光
  34. var point = new THREE.PointLight(0xffffff, 0.5);
  35. point.position.set(200, 200, 200);
  36. scene.add(point);
  37. var point2 = new THREE.PointLight(0xffffff, 0.5);
  38. point2.position.set(-200, 200, 200);
  39. scene.add(point2);
  40. var point3 = new THREE.PointLight(0xffffff);
  41. point3.position.set(200, -200, 200);
  42. // 4,创建相机对象
  43. var width = window.innerWidth;
  44. var height = window.innerHeight;
  45. var k = width / height;
  46. var s = 50;
  47. var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, -2000, 2000);
  48. camera.position.set(0, 200, 100);
  49. camera.lookAt(scene.position);
  50. // 5,创建渲染器
  51. var renderer = new THREE.WebGLRenderer();
  52. renderer.setSize(width, height);
  53. document.body.appendChild(renderer.domElement);
  54. function render() {
  55. renderer.render(scene, camera);
  56. requestAnimationFrame(render);
  57. }
  58. var controls = new THREE.OrbitControls(camera, renderer.domElement);
  59. var axes = new THREE.AxesHelper(500);
  60. // scene.add(axes);
  61. render();
  62. </script>
  63. </body>
  64. </html>

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

闽ICP备14008679号