threeProjectInfo.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <template>
  2. <div>
  3. <div id="containerThreeProjectInfo"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import * as THREE from "three";
  8. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
  9. import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader.js";
  10. import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader.js";
  11. import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
  12. import {
  13. showFullScreenLoading,
  14. tryHideFullScreenLoading,
  15. } from "../../../axios/filter";
  16. import { mapState } from "vuex";
  17. export default {
  18. name: "threeProjectInfo", // 项目详情中的threeJS
  19. components: {},
  20. data() {
  21. return {
  22. publicPath: process.env.BASE_URL,
  23. scene: null,
  24. camera: null,
  25. renderer: null,
  26. light: null,
  27. transformControls: null,
  28. geometryName: null,
  29. controls: null,
  30. mesh: null,
  31. cube: null,
  32. cacheList: [],
  33. container: null,
  34. car: null,
  35. cubeTexture: null,
  36. raf: null,
  37. canDrag: true, // 是否可移动
  38. dragControls: null,
  39. sensor: null, // 当前操作的传感器配置
  40. ogt: null,
  41. // scale: 2, // 物体加载换算倍数
  42. // rate: 20, // 坐标换算倍数
  43. };
  44. },
  45. computed: {
  46. ...mapState(["scale"]),
  47. // 坐标换算倍数
  48. rate() {
  49. return 40 / this.scale;
  50. },
  51. },
  52. props: {
  53. carModel: {
  54. type: String,
  55. default: "",
  56. },
  57. // configList: {
  58. // type: Object,
  59. // default: {
  60. // camera: [],
  61. // ogt: [],
  62. // lidar: [],
  63. // gps: [],
  64. // },
  65. // },
  66. },
  67. watch: {
  68. carModel(newVal, oldVal) {
  69. if (newVal && newVal != oldVal) {
  70. this.initCar(newVal);
  71. }
  72. },
  73. // configList(newVal, oldVal) {
  74. // newVal, oldVal都没有值
  75. // if (newVal && newVal != oldVal) {
  76. // if (
  77. // newVal.camera.length > 0 ||
  78. // newVal.ogt.length > 0 ||
  79. // newVal.lidar.length > 0 ||
  80. // newVal.gps.length > 0
  81. // ) {
  82. // this.showAll();
  83. // }
  84. // }
  85. // },
  86. },
  87. methods: {
  88. // 场景
  89. initScene() {
  90. this.scene = new THREE.Scene();
  91. let axes = new THREE.AxesHelper(1500);
  92. this.scene.add(axes);
  93. const gridHelper = new THREE.GridHelper(1000, 100);
  94. gridHelper.material.opacity = 0.25;
  95. gridHelper.material.transparent = true;
  96. this.scene.add(gridHelper);
  97. },
  98. // 相机
  99. initCamera() {
  100. this.camera = new THREE.PerspectiveCamera(
  101. 75,
  102. this.container.clientWidth / this.container.clientHeight,
  103. 0.1,
  104. 1000
  105. );
  106. this.camera.position.set(200, 200, 200);
  107. this.scene.add(this.camera);
  108. },
  109. // 渲染器
  110. initRenderer() {
  111. this.renderer = new THREE.WebGLRenderer({
  112. antialias: true,
  113. alpha: true,
  114. });
  115. this.renderer.setSize(
  116. this.container.clientWidth,
  117. this.container.clientHeight
  118. );
  119. this.renderer.setClearColor("#272727");
  120. this.container.appendChild(this.renderer.domElement);
  121. },
  122. // 初始化灯光
  123. initLight() {
  124. var hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444);
  125. hemiLight.position.set(0, 20, 0);
  126. this.scene.add(hemiLight);
  127. // 环境光会均匀的照亮场景中的所有物体
  128. const light = new THREE.AmbientLight(0x5c5c5c, 0.4); // soft white light
  129. this.scene.add(light);
  130. // 平行光是沿着特定方向发射的光
  131. const dirLight = new THREE.DirectionalLight(0xffffff, 0.5);
  132. this.scene.add(dirLight);
  133. },
  134. // 初始化模型
  135. initContent(r, position, rotation, type) {
  136. var cubeGeometry = new THREE.ConeGeometry(
  137. r || 45 * this.scale,
  138. 150 * this.scale,
  139. 4,
  140. 1,
  141. false
  142. );
  143. cubeGeometry.translate(0, -75 * this.scale, 0);
  144. let obj = {
  145. color: 0x4c4c4c,
  146. transparent: true,
  147. opacity: 0.3,
  148. lightMapIntensity: 0.1,
  149. };
  150. if (type === "camera") {
  151. obj.emissive = 0x0000ff;
  152. } else if (type === "ogt") {
  153. obj.emissive = 0x008000;
  154. } else if (type === "lidar") {
  155. obj.emissive = 0xff4500;
  156. } else if (type === "gps") {
  157. obj.emissive = 0x8a2be2;
  158. }
  159. var cubeMaterial = new THREE.MeshLambertMaterial(obj);
  160. this.cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  161. this.cube.name = this.geometryName || "cube";
  162. if (position) {
  163. this.cube.position.x = position.x;
  164. this.cube.position.y = position.y;
  165. this.cube.position.z = position.z;
  166. }
  167. if (rotation) {
  168. this.cube.rotateX(rotation.x);
  169. this.cube.rotateY(rotation.y);
  170. this.cube.rotateZ(rotation.z);
  171. }
  172. this.scene.add(this.cube);
  173. if (this.transformControls) {
  174. this.transformControls.attach(this.cube);
  175. }
  176. },
  177. initCar0(model) {
  178. if (this.car) {
  179. this.scene.remove(this.car);
  180. this.removeObj(this.car);
  181. this.car = null;
  182. }
  183. showFullScreenLoading();
  184. var that = this;
  185. var loader = new GLTFLoader(); //创建一个FBX加载器
  186. loader.load(
  187. model,
  188. function (obj) {
  189. tryHideFullScreenLoading();
  190. obj.scene.rotation.set(
  191. (-90 * Math.PI) / 180,
  192. 0,
  193. (-180 * Math.PI) / 180
  194. );
  195. let scale = 30 * that.scale;
  196. obj.scene.scale.set(scale, scale, scale);
  197. that.scene.add(obj.scene);
  198. that.car = obj.scene;
  199. },
  200. (xhr) => {
  201. // console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
  202. },
  203. (error) => {
  204. tryHideFullScreenLoading();
  205. console.error(error);
  206. }
  207. );
  208. },
  209. // 初始化车模型
  210. initCar(model) {
  211. if (!model) return;
  212. if (!model.includes(".glb")) return;
  213. this.initCar0(model);
  214. },
  215. // 初始化
  216. init() {
  217. this.initScene();
  218. this.initCamera();
  219. this.initRenderer();
  220. this.initLight();
  221. this.controls = new OrbitControls(
  222. this.camera,
  223. this.renderer.domElement
  224. ); //创建控件对象
  225. this.controls.minDistance = 30;
  226. this.controls.maxDistance = 600;
  227. this.controls.update();
  228. },
  229. animate() {
  230. this.raf = requestAnimationFrame(this.animate);
  231. this.renderer.render(this.scene, this.camera);
  232. if (this.transformControls) {
  233. this.transformControls.update();
  234. }
  235. this.controls.update();
  236. },
  237. onWindowResize() {
  238. this.camera.aspect =
  239. this.container.clientWidth / this.container.clientHeight;
  240. this.camera.updateProjectionMatrix();
  241. this.renderer.setSize(
  242. this.container.clientWidth,
  243. this.container.clientHeight
  244. );
  245. },
  246. go() {
  247. this.container = document.getElementById(
  248. "containerThreeProjectInfo"
  249. );
  250. this.init();
  251. this.animate();
  252. window.addEventListener("resize", this.onWindowResize);
  253. },
  254. // 每编辑一个传感器则重新生成对应的物体
  255. reset(type) {
  256. this.canDrag = true;
  257. if (this.cacheList.length > 0) {
  258. this.scene.remove(...this.cacheList);
  259. this.cacheList.forEach((i) => {
  260. this.clearCache(i);
  261. });
  262. this.cacheList = [];
  263. }
  264. this.sensor = null;
  265. const obj1 = this.scene.getObjectByName("cube");
  266. if (obj1) {
  267. this.scene.remove(obj1);
  268. }
  269. this.xAngle = 0;
  270. this.yAngle = 0;
  271. this.zAngle = 0;
  272. let z = +this.$parent.formA.sensorX || 0;
  273. let x = +this.$parent.formA.sensorY || 0;
  274. let y = +this.$parent.formA.sensorZ || 0;
  275. let h = +this.$parent.formA.sensorP || 0;
  276. let p = +this.$parent.formA.sensorR || 0;
  277. let r = +this.$parent.formA.sensorH || 0;
  278. this.initContent(
  279. null,
  280. { x, y, z },
  281. {
  282. x: (-90 * Math.PI) / 180,
  283. y: 0,
  284. z: 0,
  285. },
  286. type
  287. );
  288. setTimeout(() => {
  289. this.initSensor({ x, y, z }, type, true);
  290. const obj = this.scene.getObjectByName("cube");
  291. obj.position.x = x;
  292. obj.position.y = y;
  293. obj.position.z = z;
  294. this.xAngle = ((h - 90) * Math.PI) / 180;
  295. this.yAngle = (p * Math.PI) / 180;
  296. this.zAngle = (r * Math.PI) / 180;
  297. obj.rotation.set(this.xAngle, this.yAngle, this.zAngle);
  298. }, 0);
  299. },
  300. // 初始化传感器
  301. initSensor(
  302. pos = { x: 0, y: 0, z: 0 },
  303. type = "camera",
  304. canMove = false
  305. ) {
  306. let Loader = new MTLLoader(); //材质文件加载器
  307. let loader = new OBJLoader(); //obj加载器
  308. let that = this;
  309. let mtlUrl = "";
  310. let objUrl = "";
  311. if (type === "camera") {
  312. mtlUrl = `${that.publicPath}sensor/camera/camera.mtl`;
  313. objUrl = `${that.publicPath}sensor/camera/camera.obj`;
  314. } else if (type === "ogt") {
  315. mtlUrl = `${that.publicPath}sensor/ogt/millimeter_wave_radar.mtl`;
  316. objUrl = `${that.publicPath}sensor/ogt/millimeter_wave_radar.obj`;
  317. } else if (type === "lidar") {
  318. mtlUrl = `${that.publicPath}sensor/lidar/LIDAR.mtl`;
  319. objUrl = `${that.publicPath}sensor/lidar/LIDAR.obj`;
  320. } else if (type === "gps") {
  321. mtlUrl = `${that.publicPath}sensor/gps/lidar.mtl`;
  322. objUrl = `${that.publicPath}sensor/gps/lidar.obj`;
  323. }
  324. if (type === "ogt") {
  325. showFullScreenLoading();
  326. }
  327. Loader.load(
  328. mtlUrl,
  329. function (materials) {
  330. loader.setMaterials(materials);
  331. loader.load(objUrl, function (obj) {
  332. if (type === "ogt") {
  333. tryHideFullScreenLoading();
  334. }
  335. that.cacheList.push(obj);
  336. for (let i = 0; i < obj.children.length; i++) {
  337. if (type === "camera") {
  338. obj.children[i].scale.set(0.8, 0.8, 0.8);
  339. } else if (type === "ogt") {
  340. obj.children[i].scale.set(0.2, 0.2, 0.2);
  341. } else if (type === "lidar") {
  342. obj.children[i].scale.set(0.1, 0.1, 0.1);
  343. } else if (type === "gps") {
  344. obj.children[i].scale.set(40, 40, 40);
  345. }
  346. }
  347. obj.position.set(pos.x, pos.y, pos.z);
  348. if (canMove) {
  349. that.sensor = obj;
  350. }
  351. that.scene.add(obj); //返回的组对象插入场景中
  352. });
  353. },
  354. (xhr) => {
  355. // console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
  356. },
  357. (error) => {
  358. if (type === "ogt") {
  359. tryHideFullScreenLoading();
  360. }
  361. console.error(error);
  362. }
  363. );
  364. },
  365. // 初始化已保存过的传感器
  366. initContentToShow(r, position, rotation, type) {
  367. var cubeGeometry = new THREE.ConeGeometry(
  368. r || 45 * this.scale,
  369. 150 * this.scale,
  370. 4,
  371. 1,
  372. false
  373. );
  374. cubeGeometry.translate(0, -75 * this.scale, 0);
  375. let obj = {
  376. transparent: true,
  377. opacity: 0.3,
  378. lightMapIntensity: 0.1,
  379. color: 0x4c4c4c,
  380. };
  381. if (type === "camera") {
  382. obj.emissive = 0x000080;
  383. } else if (type === "ogt") {
  384. obj.emissive = 0x008000;
  385. } else if (type === "lidar") {
  386. obj.emissive = 0xff4500;
  387. } else if (type === "gps") {
  388. obj.emissive = 0x8a2be2;
  389. }
  390. var cubeMaterial = new THREE.MeshLambertMaterial(obj);
  391. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  392. cube.name = this.geometryName || "cubeA";
  393. cube.rotateX((-90 * Math.PI) / 180);
  394. if (position) {
  395. cube.position.x = position.x;
  396. cube.position.y = position.y;
  397. cube.position.z = position.z;
  398. }
  399. if (rotation) {
  400. cube.rotation.set(
  401. ((rotation.x - 90) * Math.PI) / 180,
  402. (rotation.y * Math.PI) / 180,
  403. (rotation.z * Math.PI) / 180
  404. );
  405. }
  406. this.cacheList.push(cube);
  407. this.scene.add(cube);
  408. },
  409. // 生成一种类型的全部显示器
  410. showSensor(sensor, type) {
  411. if (sensor && Array.isArray(sensor) && sensor.length > 0) {
  412. sensor.forEach((item) => {
  413. this.initContentToShow(
  414. null,
  415. {
  416. z: Math.floor(item.sensorX / this.rate || 0),
  417. x: Math.floor(item.sensorY / this.rate || 0),
  418. y: Math.floor(item.sensorZ / this.rate || 0),
  419. },
  420. {
  421. x: +item.sensorP,
  422. y: +item.sensorR,
  423. z: +item.sensorH,
  424. },
  425. type
  426. );
  427. this.initSensor(
  428. {
  429. z: Math.floor(item.sensorX / this.rate || 0),
  430. x: Math.floor(item.sensorY / this.rate || 0),
  431. y: Math.floor(item.sensorZ / this.rate || 0),
  432. },
  433. type,
  434. false
  435. );
  436. });
  437. }
  438. },
  439. // 显示全部
  440. showAll(configList) {
  441. // 避免重复加载所有传感器
  442. if (!this.canDrag) return;
  443. this.canDrag = false;
  444. if (this.cacheList.length > 0) {
  445. this.scene.remove(...this.cacheList);
  446. this.cacheList = [];
  447. }
  448. const obj = this.scene.getObjectByName("cube");
  449. if (obj) {
  450. this.scene.remove(obj);
  451. }
  452. if (this.dragControls) {
  453. this.dragControls.deactivate();
  454. this.dragControls.dispose();
  455. this.dragControls = null;
  456. }
  457. if (this.transformControls) {
  458. this.transformControls.detach();
  459. }
  460. this.showSensor(configList.camera, "camera");
  461. this.showSensor(configList.ogt, "ogt");
  462. this.showSensor(configList.lidar, "lidar");
  463. this.showSensor(configList.gps, "gps");
  464. },
  465. removeScene() {
  466. this.clearScene();
  467. },
  468. clearCache(item) {
  469. if (item.geometry && item.geometry.dispose) item.geometry.dispose();
  470. if (item.material && item.material.dispose) item.material.dispose();
  471. },
  472. clearScene() {
  473. this.removeObj(this.scene);
  474. },
  475. removeObj(obj) {
  476. let arr = obj.children.filter((x) => x);
  477. arr.forEach((item) => {
  478. if (item.children.length) {
  479. this.removeObj(item);
  480. } else {
  481. this.clearCache(item);
  482. item.clear();
  483. }
  484. }),
  485. obj.clear();
  486. arr = null;
  487. },
  488. },
  489. mounted() {
  490. this.go();
  491. },
  492. destroyed() {
  493. window.removeEventListener("resize", this.onWindowResize);
  494. cancelAnimationFrame(this.raf);
  495. if (this.renderer) {
  496. this.renderer.renderLists.dispose();
  497. this.renderer.dispose();
  498. this.renderer.forceContextLoss();
  499. this.renderer.domElement = null;
  500. this.renderer.content = null;
  501. this.renderer = null;
  502. }
  503. if (this.dragControls) {
  504. this.dragControls.deactivate();
  505. this.dragControls.dispose();
  506. this.dragControls = null;
  507. }
  508. if (this.controls) {
  509. this.controls.dispose();
  510. this.controls = null;
  511. }
  512. if (this.transformControls) {
  513. this.transformControls.detach();
  514. this.transformControls.dispose();
  515. this.transformControls = null;
  516. }
  517. if (this.cacheList.length > 0) {
  518. this.cacheList = [];
  519. }
  520. this.clearScene();
  521. this.scene = null;
  522. this.camera = null;
  523. this.light = null;
  524. this.geometryName = null;
  525. this.mesh = null;
  526. this.cube = null;
  527. this.container = null;
  528. this.car = null;
  529. this.cubeTexture = null;
  530. this.raf = null;
  531. THREE.Cache.clear();
  532. },
  533. };
  534. </script>
  535. <style lang="less" scoped>
  536. #containerThreeProjectInfo {
  537. width: 100%;
  538. height: calc(100vh - 125px);
  539. }
  540. </style>