threeProjectInfo.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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", "scaleRate"]),
  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 s = 0;
  196. if (model.includes("928.glb")) {
  197. s = that.scale / that.scaleRate;
  198. } else {
  199. s = that.scale;
  200. }
  201. let scale = 30 * s;
  202. obj.scene.scale.set(scale, scale, scale);
  203. that.scene.add(obj.scene);
  204. that.car = obj.scene;
  205. },
  206. (xhr) => {
  207. // console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
  208. },
  209. (error) => {
  210. tryHideFullScreenLoading();
  211. console.error(error);
  212. }
  213. );
  214. },
  215. // 初始化车模型
  216. initCar(model) {
  217. if (!model) return;
  218. if (!model.includes(".glb")) return;
  219. this.initCar0(model);
  220. },
  221. // 初始化
  222. init() {
  223. this.initScene();
  224. this.initCamera();
  225. this.initRenderer();
  226. this.initLight();
  227. this.controls = new OrbitControls(
  228. this.camera,
  229. this.renderer.domElement
  230. ); //创建控件对象
  231. this.controls.minDistance = 30;
  232. this.controls.maxDistance = 600;
  233. this.controls.update();
  234. },
  235. animate() {
  236. this.raf = requestAnimationFrame(this.animate);
  237. this.renderer.render(this.scene, this.camera);
  238. if (this.transformControls) {
  239. this.transformControls.update();
  240. }
  241. this.controls.update();
  242. },
  243. onWindowResize() {
  244. this.camera.aspect =
  245. this.container.clientWidth / this.container.clientHeight;
  246. this.camera.updateProjectionMatrix();
  247. this.renderer.setSize(
  248. this.container.clientWidth,
  249. this.container.clientHeight
  250. );
  251. },
  252. go() {
  253. this.container = document.getElementById(
  254. "containerThreeProjectInfo"
  255. );
  256. this.init();
  257. this.animate();
  258. window.addEventListener("resize", this.onWindowResize);
  259. },
  260. // 每编辑一个传感器则重新生成对应的物体
  261. reset(type) {
  262. this.canDrag = true;
  263. if (this.cacheList.length > 0) {
  264. this.scene.remove(...this.cacheList);
  265. this.cacheList.forEach((i) => {
  266. this.clearCache(i);
  267. });
  268. this.cacheList = [];
  269. }
  270. this.sensor = null;
  271. const obj1 = this.scene.getObjectByName("cube");
  272. if (obj1) {
  273. this.scene.remove(obj1);
  274. }
  275. this.xAngle = 0;
  276. this.yAngle = 0;
  277. this.zAngle = 0;
  278. let z = +this.$parent.formA.sensorX || 0;
  279. let x = +this.$parent.formA.sensorY || 0;
  280. let y = +this.$parent.formA.sensorZ || 0;
  281. let h = +this.$parent.formA.sensorP || 0;
  282. let p = +this.$parent.formA.sensorR || 0;
  283. let r = +this.$parent.formA.sensorH || 0;
  284. this.initContent(
  285. null,
  286. { x, y, z },
  287. {
  288. x: (-90 * Math.PI) / 180,
  289. y: 0,
  290. z: 0,
  291. },
  292. type
  293. );
  294. setTimeout(() => {
  295. this.initSensor({ x, y, z }, type, true);
  296. const obj = this.scene.getObjectByName("cube");
  297. obj.position.x = x;
  298. obj.position.y = y;
  299. obj.position.z = z;
  300. this.xAngle = ((h - 90) * Math.PI) / 180;
  301. this.yAngle = (p * Math.PI) / 180;
  302. this.zAngle = (r * Math.PI) / 180;
  303. obj.rotation.set(this.xAngle, this.yAngle, this.zAngle);
  304. }, 0);
  305. },
  306. // 初始化传感器
  307. initSensor(
  308. pos = { x: 0, y: 0, z: 0 },
  309. type = "camera",
  310. canMove = false
  311. ) {
  312. let Loader = new MTLLoader(); //材质文件加载器
  313. let loader = new OBJLoader(); //obj加载器
  314. let that = this;
  315. let mtlUrl = "";
  316. let objUrl = "";
  317. if (type === "camera") {
  318. mtlUrl = `${that.publicPath}sensor/camera/camera.mtl`;
  319. objUrl = `${that.publicPath}sensor/camera/camera.obj`;
  320. } else if (type === "ogt") {
  321. mtlUrl = `${that.publicPath}sensor/ogt/millimeter_wave_radar.mtl`;
  322. objUrl = `${that.publicPath}sensor/ogt/millimeter_wave_radar.obj`;
  323. } else if (type === "lidar") {
  324. mtlUrl = `${that.publicPath}sensor/lidar/LIDAR.mtl`;
  325. objUrl = `${that.publicPath}sensor/lidar/LIDAR.obj`;
  326. } else if (type === "gps") {
  327. mtlUrl = `${that.publicPath}sensor/gps/lidar.mtl`;
  328. objUrl = `${that.publicPath}sensor/gps/lidar.obj`;
  329. }
  330. if (type === "ogt") {
  331. showFullScreenLoading();
  332. }
  333. Loader.load(
  334. mtlUrl,
  335. function (materials) {
  336. loader.setMaterials(materials);
  337. loader.load(objUrl, function (obj) {
  338. if (type === "ogt") {
  339. tryHideFullScreenLoading();
  340. }
  341. that.cacheList.push(obj);
  342. for (let i = 0; i < obj.children.length; i++) {
  343. if (type === "camera") {
  344. obj.children[i].scale.set(0.8, 0.8, 0.8);
  345. } else if (type === "ogt") {
  346. obj.children[i].scale.set(0.2, 0.2, 0.2);
  347. } else if (type === "lidar") {
  348. obj.children[i].scale.set(0.1, 0.1, 0.1);
  349. } else if (type === "gps") {
  350. obj.children[i].scale.set(40, 40, 40);
  351. }
  352. }
  353. obj.position.set(pos.x, pos.y, pos.z);
  354. if (canMove) {
  355. that.sensor = obj;
  356. }
  357. that.scene.add(obj); //返回的组对象插入场景中
  358. });
  359. },
  360. (xhr) => {
  361. // console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
  362. },
  363. (error) => {
  364. if (type === "ogt") {
  365. tryHideFullScreenLoading();
  366. }
  367. console.error(error);
  368. }
  369. );
  370. },
  371. // 初始化已保存过的传感器
  372. initContentToShow(r, position, rotation, type) {
  373. var cubeGeometry = new THREE.ConeGeometry(
  374. r || 45 * this.scale,
  375. 150 * this.scale,
  376. 4,
  377. 1,
  378. false
  379. );
  380. cubeGeometry.translate(0, -75 * this.scale, 0);
  381. let obj = {
  382. transparent: true,
  383. opacity: 0.3,
  384. lightMapIntensity: 0.1,
  385. color: 0x4c4c4c,
  386. };
  387. if (type === "camera") {
  388. obj.emissive = 0x000080;
  389. } else if (type === "ogt") {
  390. obj.emissive = 0x008000;
  391. } else if (type === "lidar") {
  392. obj.emissive = 0xff4500;
  393. } else if (type === "gps") {
  394. obj.emissive = 0x8a2be2;
  395. }
  396. var cubeMaterial = new THREE.MeshLambertMaterial(obj);
  397. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  398. cube.name = this.geometryName || "cubeA";
  399. cube.rotateX((-90 * Math.PI) / 180);
  400. if (position) {
  401. cube.position.x = position.x;
  402. cube.position.y = position.y;
  403. cube.position.z = position.z;
  404. }
  405. if (rotation) {
  406. cube.rotation.set(
  407. ((rotation.x - 90) * Math.PI) / 180,
  408. (rotation.y * Math.PI) / 180,
  409. (rotation.z * Math.PI) / 180
  410. );
  411. }
  412. this.cacheList.push(cube);
  413. this.scene.add(cube);
  414. },
  415. // 生成一种类型的全部显示器
  416. showSensor(sensor, type) {
  417. if (sensor && Array.isArray(sensor) && sensor.length > 0) {
  418. sensor.forEach((item) => {
  419. this.initContentToShow(
  420. null,
  421. {
  422. z: Math.floor(item.sensorX / this.rate || 0),
  423. x: Math.floor(item.sensorY / this.rate || 0),
  424. y: Math.floor(item.sensorZ / this.rate || 0),
  425. },
  426. {
  427. x: +item.sensorP,
  428. y: +item.sensorR,
  429. z: +item.sensorH,
  430. },
  431. type
  432. );
  433. this.initSensor(
  434. {
  435. z: Math.floor(item.sensorX / this.rate || 0),
  436. x: Math.floor(item.sensorY / this.rate || 0),
  437. y: Math.floor(item.sensorZ / this.rate || 0),
  438. },
  439. type,
  440. false
  441. );
  442. });
  443. }
  444. },
  445. // 显示全部
  446. showAll(configList) {
  447. // 避免重复加载所有传感器
  448. if (!this.canDrag) return;
  449. this.canDrag = false;
  450. if (this.cacheList.length > 0) {
  451. this.scene.remove(...this.cacheList);
  452. this.cacheList = [];
  453. }
  454. const obj = this.scene.getObjectByName("cube");
  455. if (obj) {
  456. this.scene.remove(obj);
  457. }
  458. if (this.dragControls) {
  459. this.dragControls.deactivate();
  460. this.dragControls.dispose();
  461. this.dragControls = null;
  462. }
  463. if (this.transformControls) {
  464. this.transformControls.detach();
  465. }
  466. this.showSensor(configList.camera, "camera");
  467. this.showSensor(configList.ogt, "ogt");
  468. this.showSensor(configList.lidar, "lidar");
  469. this.showSensor(configList.gps, "gps");
  470. },
  471. removeScene() {
  472. this.clearScene();
  473. },
  474. clearCache(item) {
  475. if (item.geometry && item.geometry.dispose) item.geometry.dispose();
  476. if (item.material && item.material.dispose) item.material.dispose();
  477. },
  478. clearScene() {
  479. this.removeObj(this.scene);
  480. },
  481. removeObj(obj) {
  482. let arr = obj.children.filter((x) => x);
  483. arr.forEach((item) => {
  484. if (item.children.length) {
  485. this.removeObj(item);
  486. } else {
  487. this.clearCache(item);
  488. item.clear();
  489. }
  490. }),
  491. obj.clear();
  492. arr = null;
  493. },
  494. },
  495. mounted() {
  496. this.go();
  497. },
  498. destroyed() {
  499. window.removeEventListener("resize", this.onWindowResize);
  500. cancelAnimationFrame(this.raf);
  501. if (this.renderer) {
  502. this.renderer.renderLists.dispose();
  503. this.renderer.dispose();
  504. this.renderer.forceContextLoss();
  505. this.renderer.domElement = null;
  506. this.renderer.content = null;
  507. this.renderer = null;
  508. }
  509. if (this.dragControls) {
  510. this.dragControls.deactivate();
  511. this.dragControls.dispose();
  512. this.dragControls = null;
  513. }
  514. if (this.controls) {
  515. this.controls.dispose();
  516. this.controls = null;
  517. }
  518. if (this.transformControls) {
  519. this.transformControls.detach();
  520. this.transformControls.dispose();
  521. this.transformControls = null;
  522. }
  523. if (this.cacheList.length > 0) {
  524. this.cacheList = [];
  525. }
  526. this.clearScene();
  527. this.scene = null;
  528. this.camera = null;
  529. this.light = null;
  530. this.geometryName = null;
  531. this.mesh = null;
  532. this.cube = null;
  533. this.container = null;
  534. this.car = null;
  535. this.cubeTexture = null;
  536. this.raf = null;
  537. THREE.Cache.clear();
  538. },
  539. };
  540. </script>
  541. <style lang="less" scoped>
  542. #containerThreeProjectInfo {
  543. width: 100%;
  544. height: calc(100vh - 125px);
  545. }
  546. </style>