当前位置:   article > 正文

ThreeJs场景、相机、渲染器、添加obj模型和删除模型_threejs 删除已有模型

threejs 删除已有模型

相机

效果图

 1.安装threeJs

npm install three

 2.安装加载obj和mtl文件的插件

npm i --save three-obj-mtl-loader

3.安装轨道控件插件

npm install three-orbit-controls

4.引入

  1. // threeJs
  2. import * as THREE from "three";
  3. // 轨道控件插件
  4. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
  5. //obj
  6. import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";
  7. //mtl
  8. import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader";

5.变量

  1. // 场景
  2. scene: "",
  3. // 控制器
  4. controls: '',
  5. // 相机
  6. camera: "",
  7. // 渲染
  8. renderer: "",

6.加载模型

  1. /**加载模型 */
  2. loadPlant() {
  3. let that = this;
  4. let objLoader = new OBJLoader();
  5. let mtlLoader = new MTLLoader();
  6. mtlLoader.load('static/model/0112/red.mtl', function (materials) {
  7. materials.preload();
  8. objLoader.setMaterials(materials);
  9. objLoader.load('static/model/0112/red.obj', function (obj) {
  10. // console.log(obj.children[0]);
  11. obj.scale.set(1, 1, 1);
  12. // obj.children[0].receiveShadow = true;//允许接收阴影
  13. // obj.children[0].geometry.center(); //网格模型的几何体居中
  14. that.scene.add(obj);
  15. that.object = obj;
  16. // console.log(that.scene, "that.scene");
  17. },
  18. function (xhr) {
  19. console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
  20. },
  21. function (error) {
  22. console.log(error, "An error happened");
  23. }
  24. );
  25. }
  26. );
  27. },

7.场景

  1. initScene() {
  2. this.scene = new THREE.Scene();
  3. // const ambientLight = new THREE.AmbientLight(0xcccccc, 0.4);
  4. // this.scene.add(ambientLight);
  5. var axesHelper = new THREE.AxesHelper(15);
  6. this.scene.add(axesHelper);
  7. },

8.相机+灯光

  1. initCamera() {
  2. const aspect = window.innerWidth / innerHeight; //宽高可根据实际项目要求更改 如果是窗口高度改为innerHeight
  3. // this.camera = new THREE.PerspectiveCamera(45, aspect, 1, 2000);
  4. this.camera = new THREE.PerspectiveCamera(45, aspect, 1, 2000);
  5. // this.camera.position.set(100, 100, 100);
  6. this.camera.position.set(100, 100, 200);
  7. this.camera.name = "相机";
  8. this.camera.lookAt(this.scene.position); //设置相机方向(指向的场景对象)
  9. var directionalLight = new THREE.DirectionalLight(0xffffff, 1.5) //方向光
  10. directionalLight.position.set(1000, 1000, 1000)
  11. directionalLight.castShadow = true;
  12. directionalLight.shadow.camera.near = 0.5;
  13. directionalLight.shadow.camera.far = 300;
  14. directionalLight.shadow.camera.left = -50;
  15. directionalLight.shadow.camera.right = 50;
  16. directionalLight.shadow.camera.top = 200;
  17. directionalLight.shadow.camera.bottom = -100;
  18. this.scene.add(directionalLight)
  19. },

9.控制器

  1. initOrbitControls() {
  2. let controls = new OrbitControls(this.camera, this.renderer.domElement);
  3. this.controls = controls;
  4. },

10.渲染器

  1. initRenderer() {
  2. this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
  3. // 渲染器Renderer开启阴影计算
  4. this.renderer.setPixelRatio(window.devicePixelRatio);
  5. this.renderer.setSize(window.innerWidth, innerHeight);
  6. this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  7. this.container = document.getElementById("container");
  8. this.container.appendChild(this.renderer.domElement);
  9. },

11动画

  1. animate() {
  2. //执行渲染操作
  3. this.renderer.render(this.scene, this.camera);
  4. this.requestId = requestAnimationFrame(this.animate);
  5. },

12.初始化

  1. init() {
  2. this.initScene();
  3. this.initCamera();
  4. this.initRenderer();
  5. this.initOrbitControls();
  6. },

13.mounted调用

  1. mounted() {
  2. this.loadPlant();
  3. this.init();
  4. this.animate();
  5. },

如果模型加载不出来看看是否是以下原因

1.引用的路径有没有问题

2.是不是没有把模型放在静态目录下 我这里放在public目录下static下面

3.是不是模型太大了或者太小了 调整一下模型大小

4.mtl文件中的Tr值置为0或者将此行删除,Tr值是透明度的意思

14.删除模型

  1. // 删除
  2. del() {
  3. // this.scene.children[2]是模型
  4. this.deleteObject(this.scene.children[2])
  5. this.animate()
  6. },
  7. deleteObject(group) {
  8. if (group) {
  9. group.traverse(function (item) {
  10. if (item instanceof THREE.Mesh) {
  11. if (Array.isArray(item.material)) {
  12. item.material.forEach(a => {
  13. a.dispose()
  14. })
  15. } else {
  16. item.material.dispose() // 删除材质
  17. }
  18. item.geometry.dispose() // 删除几何体
  19. }
  20. item = null
  21. })
  22. // 删除场景对象scene的子对象group
  23. this.scene.remove(group);
  24. }
  25. },

全部代码

  1. <template>
  2. <div id="app">
  3. <div class="center">
  4. <button @click="del">删除</button>
  5. <div id="container" ref="container"></div>
  6. <canvas id="canvas" style="display: none">你的浏览器不支持canvas</canvas>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. import * as THREE from "three";
  12. // 轨道控件插件
  13. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
  14. import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";
  15. import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader";
  16. export default {
  17. data() {
  18. return {
  19. // 场景
  20. scene: "",
  21. // 控制器
  22. controls: '',
  23. // 相机
  24. camera: "",
  25. // 渲染
  26. renderer: "",
  27. };
  28. },
  29. methods: {
  30. // 删除
  31. del() {
  32. // this.scene.children[2]是模型
  33. this.deleteObject(this.scene.children[2])
  34. this.animate()
  35. },
  36. deleteObject(group) {
  37. if (group) {
  38. group.traverse(function (item) {
  39. if (item instanceof THREE.Mesh) {
  40. if (Array.isArray(item.material)) {
  41. item.material.forEach(a => {
  42. a.dispose()
  43. })
  44. } else {
  45. item.material.dispose() // 删除材质
  46. }
  47. item.geometry.dispose() // 删除几何体
  48. }
  49. item = null
  50. })
  51. // 删除场景对象scene的子对象group
  52. this.scene.remove(group);
  53. }
  54. },
  55. /**初始化 */
  56. initScene() {
  57. this.scene = new THREE.Scene();
  58. // const ambientLight = new THREE.AmbientLight(0xcccccc, 0.4);
  59. // this.scene.add(ambientLight);
  60. var axesHelper = new THREE.AxesHelper(15);
  61. this.scene.add(axesHelper);
  62. },
  63. initCamera() {
  64. const aspect = window.innerWidth / innerHeight; //宽高可根据实际项目要求更改 如果是窗口高度改为innerHeight
  65. // this.camera = new THREE.PerspectiveCamera(45, aspect, 1, 2000);
  66. this.camera = new THREE.PerspectiveCamera(45, aspect, 1, 2000);
  67. // this.camera.position.set(100, 100, 100);
  68. this.camera.position.set(100, 100, 200);
  69. this.camera.name = "相机";
  70. this.camera.lookAt(this.scene.position); //设置相机方向(指向的场景对象)
  71. var directionalLight = new THREE.DirectionalLight(0xffffff, 1.5) //方向光
  72. directionalLight.position.set(1000, 1000, 1000)
  73. directionalLight.castShadow = true;
  74. directionalLight.shadow.camera.near = 0.5;
  75. directionalLight.shadow.camera.far = 300;
  76. directionalLight.shadow.camera.left = -50;
  77. directionalLight.shadow.camera.right = 50;
  78. directionalLight.shadow.camera.top = 200;
  79. directionalLight.shadow.camera.bottom = -100;
  80. this.scene.add(directionalLight)
  81. },
  82. initRenderer() {
  83. this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
  84. // 渲染器Renderer开启阴影计算
  85. this.renderer.setPixelRatio(window.devicePixelRatio);
  86. this.renderer.setSize(window.innerWidth, innerHeight);
  87. this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  88. this.container = document.getElementById("container");
  89. this.container.appendChild(this.renderer.domElement);
  90. },
  91. // 初始化控制器
  92. initOrbitControls() {
  93. let controls = new OrbitControls(this.camera, this.renderer.domElement);
  94. this.controls = controls;
  95. },
  96. animate() {
  97. //执行渲染操作
  98. this.renderer.render(this.scene, this.camera);
  99. this.requestId = requestAnimationFrame(this.animate);
  100. },
  101. init() {
  102. this.initScene();
  103. this.initCamera();
  104. this.initRenderer();
  105. this.initOrbitControls();
  106. },
  107. /**加载模型 */
  108. loadPlant() {
  109. let that = this;
  110. let objLoader = new OBJLoader();
  111. let mtlLoader = new MTLLoader();
  112. mtlLoader.load('static/model/0112/red.mtl', function (materials) {
  113. materials.preload();
  114. objLoader.setMaterials(materials);
  115. objLoader.load('static/model/0112/red.obj', function (obj) {
  116. // console.log(obj.children[0]);
  117. obj.scale.set(1, 1, 1);
  118. // obj.children[0].receiveShadow = true;//允许接收阴影
  119. // obj.children[0].geometry.center(); //网格模型的几何体居中
  120. that.scene.add(obj);
  121. that.object = obj;
  122. // console.log(that.scene, "that.scene");
  123. },
  124. function (xhr) {
  125. // console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
  126. },
  127. function (error) {
  128. console.log(error, "An error happened");
  129. }
  130. );
  131. }
  132. );
  133. },
  134. },
  135. mounted() {
  136. this.loadPlant();
  137. this.init();
  138. this.animate();
  139. },
  140. };
  141. </script>
  142. <style lang="less">
  143. </style>

效果如下

 

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

闽ICP备14008679号