threeVehicleConfiguration.vue 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  1. <template>
  2. <div>
  3. <div id="container"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import * as THREE from "three";
  8. import { TransformControls } from "three/examples/jsm/controls/TransformControls.js";
  9. import { DragControls } from "three/examples/jsm/controls/DragControls.js";
  10. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
  11. import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader.js";
  12. import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader.js";
  13. // import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader.js";
  14. // import { DDSLoader } from "three/examples/jsm/loaders/DDSLoader.js";
  15. import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
  16. import { ConvexGeometry } from "three/examples/jsm/geometries/ConvexGeometry.js";
  17. import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader";
  18. // import { LineGeometry } from "three/examples/jsm/lines/LineGeometry.js";
  19. // import { LineSegmentsGeometry } from "three/examples/jsm/lines/LineSegmentsGeometry.js";
  20. // import { LineMaterial } from "three/examples/jsm/lines/LineMaterial.js";
  21. // import { LineSegments2 } from "three/examples/jsm/lines/LineSegments2.js";
  22. // import { Line2 } from "three/examples/jsm/lines/Line2.js";
  23. import {
  24. showFullScreenLoading,
  25. tryHideFullScreenLoading,
  26. } from "../../../axios/filter";
  27. import { mapState } from "vuex";
  28. import CoordinateAxes from "../common/coordinateAxes";
  29. export default {
  30. name: "threeVehicleConfiguration", // 车辆配置详情中的threeJS
  31. components: {},
  32. data() {
  33. return {
  34. publicPath: process.env.BASE_URL,
  35. scene: null,
  36. scene2: null,
  37. camera: null,
  38. camera2: null,
  39. renderer: null,
  40. light: null,
  41. transformControls: null,
  42. geometryName: null,
  43. controls: null,
  44. mesh: null,
  45. cube: null,
  46. cacheList: [],
  47. xAngle: 0,
  48. yAngle: 0,
  49. zAngle: 0,
  50. container: null,
  51. car: null,
  52. cubeTexture: null,
  53. raf: null,
  54. canDrag: true, // 是否可移动
  55. dragControls: null,
  56. sensor: null, // 当前操作的传感器配置
  57. ogt: null,
  58. // scale: 2, // 物体加载换算倍数
  59. // rate: 20, // 坐标换算倍数
  60. };
  61. },
  62. computed: {
  63. ...mapState(["scale", "scaleRate"]),
  64. // 坐标换算倍数
  65. rate() {
  66. return 40 / this.scale;
  67. },
  68. },
  69. props: {
  70. xValue: {
  71. type: Number,
  72. default: 0,
  73. },
  74. yValue: {
  75. type: Number,
  76. default: 0,
  77. },
  78. zValue: {
  79. type: Number,
  80. default: 0,
  81. },
  82. hValue: {
  83. type: Number,
  84. default: 0,
  85. },
  86. pValue: {
  87. type: Number,
  88. default: 0,
  89. },
  90. rValue: {
  91. type: Number,
  92. default: 0,
  93. },
  94. carModel: {
  95. type: String,
  96. default: "",
  97. },
  98. isAdd: {
  99. type: Boolean,
  100. default: false,
  101. },
  102. configList: {
  103. // default: {},
  104. // type: Object,
  105. default: function () {
  106. return {};
  107. },
  108. type: Object,
  109. },
  110. allSensor: {
  111. default: function () {
  112. return {};
  113. },
  114. type: Object,
  115. },
  116. },
  117. watch: {
  118. xValue(newVal, oldVal) {
  119. if (newVal === oldVal) return;
  120. const obj = this.scene.getObjectByName("cube");
  121. if (obj) obj.position.x = newVal || 0;
  122. if (this.sensor) this.sensor.position.x = newVal || 0;
  123. },
  124. yValue(newVal, oldVal) {
  125. if (newVal === oldVal) return;
  126. const obj = this.scene.getObjectByName("cube");
  127. if (obj) obj.position.y = newVal || 0;
  128. if (this.sensor) this.sensor.position.y = newVal || 0;
  129. },
  130. zValue(newVal, oldVal) {
  131. if (newVal === oldVal) return;
  132. const obj = this.scene.getObjectByName("cube");
  133. if (obj) obj.position.z = newVal || 0;
  134. if (this.sensor) this.sensor.position.z = newVal || 0;
  135. },
  136. hValue(newVal, oldVal) {
  137. if (newVal === oldVal) return;
  138. // let value = (newVal || 0) - 90;
  139. let value = newVal || 0;
  140. value = (value * Math.PI) / 180;
  141. this.xAngle = value;
  142. const obj = this.scene.getObjectByName("cube");
  143. if (obj) {
  144. obj.rotation.set(this.xAngle, this.yAngle, this.zAngle);
  145. }
  146. // obj.rotateX(this.xAngle * -1);
  147. // obj.rotateX(value);
  148. // obj.rotation.x += value;
  149. // var axis = new THREE.Vector3(0, 1, 0); //向量axis
  150. // obj.rotateOnAxis(axis, value); //绕axis轴旋转π/8
  151. },
  152. pValue(newVal, oldVal) {
  153. if (newVal === oldVal) return;
  154. let value = newVal || 0;
  155. value = (value * Math.PI) / 180;
  156. this.yAngle = value;
  157. const obj = this.scene.getObjectByName("cube");
  158. if (obj) {
  159. obj.rotation.set(this.xAngle, this.yAngle, this.zAngle);
  160. }
  161. },
  162. rValue(newVal, oldVal) {
  163. if (newVal === oldVal) return;
  164. let value = newVal || 0;
  165. value = (value * Math.PI) / 180;
  166. this.zAngle = value;
  167. const obj = this.scene.getObjectByName("cube");
  168. if (obj) {
  169. obj.rotation.set(this.xAngle, this.yAngle, this.zAngle);
  170. }
  171. },
  172. carModel(newVal, oldVal) {
  173. if (newVal && newVal != oldVal) {
  174. this.initCar(newVal);
  175. }
  176. },
  177. },
  178. methods: {
  179. // 场景
  180. initScene() {
  181. this.scene = new THREE.Scene();
  182. let axes = new THREE.AxesHelper(1500);
  183. this.scene.add(axes);
  184. const gridHelper = new THREE.GridHelper(1000, 100);
  185. gridHelper.material.opacity = 0.25;
  186. gridHelper.material.transparent = true;
  187. this.scene.add(gridHelper);
  188. return;
  189. /* var cubeGeometry = new THREE.ConeGeometry(75, 150, 6, 1, false);
  190. cubeGeometry.translate(0, -75, 0);
  191. let obj = {
  192. color: 0x4c4c4c,
  193. // wireframe: true,
  194. // transparent: true,
  195. // opacity: 0.3,
  196. // ambient: 0x00ff00,
  197. // emissive: 0x00ff00,
  198. lightMapIntensity: 0.1,
  199. };
  200. var cubeMaterial = new THREE.MeshLambertMaterial(obj);
  201. let cube = new THREE.Mesh(cubeGeometry, cubeMaterial); */
  202. this.scene2 = new THREE.Scene();
  203. let cube = new CoordinateAxes();
  204. // this.scene2.background = null;
  205. // this.scene2.background = new THREE.Color(0x00000, 1.0);
  206. this.scene2.add(cube);
  207. // const geometry = new THREE.BoxGeometry(100, 100, 100);
  208. // const material = new THREE.MeshBasicMaterial({
  209. // color: 0xffffff,
  210. // opacity: 0.3,
  211. // transparent: true,
  212. // });
  213. // geometry.translate(100, 100, 100);
  214. // const cube = new THREE.Mesh(geometry, material);
  215. // this.scene.add(cube);
  216. // let geometry = new THREE.BoxGeometry(10, 10, 10, 2, 2, 2);
  217. // let material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
  218. // let object = new THREE.Mesh(geometry, material);
  219. // let edges = new THREE.VertexNormalsHelper(object, 2, 0x00ff00, 1);
  220. // this.scene.add(object);
  221. // this.scene.add(edges);
  222. const genCubeUrls = function (prefix, postfix) {
  223. return [
  224. prefix + "px" + postfix,
  225. prefix + "nx" + postfix,
  226. prefix + "py" + postfix,
  227. prefix + "ny" + postfix,
  228. prefix + "pz" + postfix,
  229. prefix + "nz" + postfix,
  230. ];
  231. };
  232. const urls = genCubeUrls(`${this.publicPath}Park3Med/`, ".jpg");
  233. // const urls = genCubeUrls(`${this.publicPath}6/`, ".png");
  234. const cubeTextureLoader = new THREE.CubeTextureLoader();
  235. const environmentMapTexture = cubeTextureLoader.load(urls);
  236. // this.scene.background = environmentMapTexture;
  237. },
  238. // 相机
  239. initCamera() {
  240. /* this.camera = new THREE.OrthographicCamera(
  241. this.container.clientWidth / -2,
  242. this.container.clientWidth / 2,
  243. this.container.clientHeight / 2,
  244. this.container.clientHeight / -2,
  245. 1,
  246. // 0.1,
  247. 1000
  248. ); */
  249. this.camera = new THREE.PerspectiveCamera(
  250. 75,
  251. this.container.clientWidth / this.container.clientHeight,
  252. 0.1,
  253. 1000
  254. );
  255. // this.camera = new THREE.PerspectiveCamera(45, 1.5, 1, 1000);
  256. // this.camera.position.set(600, 600, 600);
  257. this.camera.position.set(200, 200, 200);
  258. // this.camera.lookAt(this.scene.position);
  259. // this.scene.add(this.camera);
  260. // this.scene.add(this.camera2);
  261. this.camera2 = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
  262. // this.camera2 = new THREE.PerspectiveCamera(40, 1, 1, 1000);
  263. // this.camera2.position.copy(this.camera.position);
  264. // this.camera.layers.set(1);
  265. },
  266. // 渲染器
  267. initRenderer() {
  268. this.renderer = new THREE.WebGLRenderer({
  269. antialias: true,
  270. alpha: true,
  271. });
  272. this.renderer.setSize(
  273. this.container.clientWidth,
  274. this.container.clientHeight
  275. );
  276. this.renderer.setClearColor("#272727");
  277. this.container.appendChild(this.renderer.domElement);
  278. },
  279. // 初始化灯光
  280. initLight() {
  281. var hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444);
  282. hemiLight.position.set(0, 20, 0);
  283. this.scene.add(hemiLight);
  284. // 环境光会均匀的照亮场景中的所有物体
  285. const light = new THREE.AmbientLight(0x5c5c5c, 0.4); // soft white light
  286. this.scene.add(light);
  287. // 点光源
  288. // const light1 = new THREE.PointLight(0xffffff, 2, 100);
  289. // light1.position.set(0, 0, -100);
  290. // this.scene.add(light1);
  291. // 平行光是沿着特定方向发射的光
  292. const dirLight = new THREE.DirectionalLight(0xffffff, 0.5);
  293. // const dirLight = new THREE.DirectionalLight(0xffffff);
  294. // dirLight.position.set(0, 200, 100);
  295. // dirLight.castShadow = true;
  296. // dirLight.shadow.camera.top = 180;
  297. // dirLight.shadow.camera.bottom = -100;
  298. // dirLight.shadow.camera.left = -120;
  299. // dirLight.shadow.camera.right = 120;
  300. // dirLight.shadow.camera.near = 0.01;
  301. // dirLight.shadow.camera.far = 60;
  302. // dirLight.shadow.camera.top = 22;
  303. // dirLight.shadow.camera.bottom = -22;
  304. // dirLight.shadow.camera.left = -35;
  305. // dirLight.shadow.camera.right = 35;
  306. // // //设置阴影分辨率
  307. // dirLight.shadow.mapSize.width = 2048; // default
  308. // dirLight.shadow.mapSize.height = 2048; // default
  309. // //阴影限制
  310. // dirLight.shadow.radius = 1;
  311. // this.scene.add(dirLight);
  312. // const spotLight = new THREE.SpotLight(0xffffff);
  313. // spotLight.position.set(100, 1000, 100);
  314. // spotLight.castShadow = true;
  315. // spotLight.shadow.mapSize.width = 1024;
  316. // spotLight.shadow.mapSize.height = 1024;
  317. // spotLight.shadow.camera.near = 500;
  318. // spotLight.shadow.camera.far = 4000;
  319. // spotLight.shadow.camera.fov = 30;
  320. // this.scene.add(spotLight);
  321. },
  322. initTransform() {
  323. // 添加平移控件
  324. this.transformControls = new TransformControls(
  325. this.camera,
  326. this.renderer.domElement
  327. );
  328. this.transformControls.setMode("translate");
  329. // 可能的值有"world" 和 "local"
  330. this.transformControls.setSpace("world");
  331. this.transformControls.addEventListener("objectChange", (e) => {
  332. // console.log(e.target.object);
  333. // this.$emit(
  334. // "posChange",
  335. // ((e.target.object.rotation.x * 360) / Math.PI) >> 0,
  336. // "h"
  337. // );
  338. if (!this.canDrag) return;
  339. this.$emit(
  340. "posChange",
  341. (e.target.object.position.x + 0.5) >> 0,
  342. "x"
  343. );
  344. this.$emit(
  345. "posChange",
  346. (e.target.object.position.y + 0.5) >> 0,
  347. "y"
  348. );
  349. this.$emit(
  350. "posChange",
  351. (e.target.object.position.z + 0.5) >> 0,
  352. "z"
  353. );
  354. });
  355. this.transformControls.addEventListener("mouseDown", (e) => {
  356. this.controls.enabled = false;
  357. this.$emit("controlsEnabled", false);
  358. });
  359. this.transformControls.addEventListener("mouseUp", (e) => {
  360. this.controls.enabled = true;
  361. this.$emit("controlsEnabled", true);
  362. });
  363. this.scene.add(this.transformControls);
  364. },
  365. /**
  366. * 初始化模型
  367. * @param {*} r 半径
  368. * @param {*} position 位置
  369. * @param {*} rotation 旋转
  370. * @param {*} type 传感器类型
  371. * @param {*} index 该传感器所在数组下标
  372. */
  373. initContent1(r, position, rotation, type, index) {
  374. var cubeGeometry = new THREE.ConeGeometry(
  375. r || 45 * this.scale,
  376. 150 * this.scale,
  377. 4,
  378. 1,
  379. false
  380. );
  381. cubeGeometry.translate(0, -75 * this.scale, 0);
  382. let obj = {
  383. color: 0x4c4c4c,
  384. // wireframe: true,
  385. transparent: true,
  386. opacity: 0.3,
  387. // ambient: 0x00ff00,
  388. // emissive: 0xfaff72,
  389. lightMapIntensity: 0.1,
  390. // emissiveIntensity: 0.1,
  391. // envMap: this.cubeTexture,
  392. // side: THREE.DoubleSide,
  393. // side: 2,
  394. };
  395. if (type === "camera") {
  396. obj.emissive = 0x0000ff;
  397. // obj.emissive = 0x000080;
  398. } else if (type === "ogt") {
  399. // obj.emissive = 0x228B22;
  400. // obj.emissive = 0x006400;
  401. obj.emissive = 0x008000;
  402. } else if (type === "lidar") {
  403. // obj.emissive = 0xff0000;
  404. // obj.emissive = 0xE6A23C;
  405. obj.emissive = 0xff4500;
  406. } else if (type === "gps") {
  407. // obj.emissive = 0xfaff72;
  408. // obj.emissive = 0x9400D3;
  409. obj.emissive = 0x8a2be2;
  410. }
  411. // var cubeMaterial = new THREE.MeshBasicMaterial(obj);
  412. var cubeMaterial = new THREE.MeshLambertMaterial(obj);
  413. this.cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  414. this.cube.name = this.geometryName || "cube";
  415. if (position) {
  416. this.cube.position.x = position.x;
  417. this.cube.position.y = position.y;
  418. this.cube.position.z = position.z;
  419. }
  420. if (rotation) {
  421. this.cube.rotateX(rotation.x);
  422. this.cube.rotateY(rotation.y);
  423. this.cube.rotateZ(rotation.z);
  424. }
  425. this.scene.add(this.cube);
  426. if (this.transformControls) {
  427. this.transformControls.attach(this.cube);
  428. }
  429. },
  430. /**
  431. * 初始化模型
  432. * @param {*} position 位置
  433. * @param {*} rotation 旋转
  434. * @param {*} type 传感器类型
  435. * @param {*} index 该传感器所在数组下标
  436. * @param {*} canMove 如果用于单独展示则可拖拽,用于全部预览则不可拖拽
  437. */
  438. initContent(position, rotation, type, index, canMove) {
  439. let sensorId = this.configList[type][index].sensorId;
  440. let coordinate = this.allSensor[type].find(
  441. (i) => i.id === sensorId
  442. );
  443. if (!coordinate) {
  444. coordinate = {
  445. farDistance: 60,
  446. nearDistance: 0,
  447. fovH: 60,
  448. fovV: 60,
  449. fovHLeft: 60,
  450. fovVTop: 60,
  451. fovHRight: 60,
  452. fovVBottom: 60,
  453. };
  454. }
  455. // console.log(coordinate);
  456. let r = coordinate.farDistance * 100;
  457. let r1 = coordinate.nearDistance * 100;
  458. if (r <= 0 || r1 < 0 || r <= r1) {
  459. return;
  460. }
  461. if (type != "ogt") {
  462. this.initSenA(
  463. position,
  464. rotation,
  465. coordinate,
  466. r / (this.scale / 0.6),
  467. r1 / (this.scale / 0.6),
  468. type,
  469. canMove
  470. );
  471. } else {
  472. this.initSenB(
  473. position,
  474. rotation,
  475. coordinate,
  476. r / (this.scale / 0.6),
  477. r1 / (this.scale / 0.6),
  478. type,
  479. canMove
  480. );
  481. }
  482. },
  483. // 处理轴对称物体
  484. initSenA(position, rotation, coordinate, r, r1, type, canMove) {
  485. let x = 0,
  486. y = 0,
  487. z = r;
  488. let a = coordinate.fovH,
  489. b = coordinate.fovV;
  490. if (a >= 90) a = 89.99;
  491. if (b >= 90) b = 89.99;
  492. x = Math.tan(THREE.MathUtils.degToRad(a)) * r;
  493. y = Math.tan(THREE.MathUtils.degToRad(b)) * r;
  494. let pointsArr = [];
  495. if (r1 === 0) {
  496. pointsArr = [
  497. [0, 0, 0],
  498. [x, y, z],
  499. [x, -y, z],
  500. [-x, -y, z],
  501. [-x, y, z],
  502. ];
  503. } else {
  504. let x1 = 0,
  505. y1 = 0,
  506. z1 = r1;
  507. x1 = Math.tan(THREE.MathUtils.degToRad(a)) * r1;
  508. y1 = Math.tan(THREE.MathUtils.degToRad(b)) * r1;
  509. pointsArr = [
  510. [x1, y1, z1],
  511. [x1, -y1, z1],
  512. [-x1, -y1, z1],
  513. [-x1, y1, z1],
  514. [x, y, z],
  515. [x, -y, z],
  516. [-x, -y, z],
  517. [-x, y, z],
  518. ];
  519. }
  520. this.initMesh(position, rotation, pointsArr, type, canMove);
  521. },
  522. // 处理非轴对称物体
  523. initSenB(position, rotation, coordinateA, r, r1, type, canMove) {
  524. let x1 = 0,
  525. x2 = 0,
  526. y1 = 0,
  527. y2 = 0,
  528. z1 = r;
  529. let a = coordinateA.fovHLeft,
  530. b = coordinateA.fovVTop,
  531. c = coordinateA.fovHRight,
  532. d = coordinateA.fovVBottom;
  533. if (a >= 90) a = 89.99;
  534. if (b >= 90) b = 89.99;
  535. if (c >= 90) c = 89.99;
  536. if (d >= 90) d = 89.99;
  537. x1 = Math.tan(THREE.MathUtils.degToRad(a)) * r;
  538. y1 = Math.tan(THREE.MathUtils.degToRad(b)) * r;
  539. x2 = Math.tan(THREE.MathUtils.degToRad(c)) * r;
  540. y2 = Math.tan(THREE.MathUtils.degToRad(d)) * r;
  541. let pointsArr = [];
  542. if (r1 === 0) {
  543. pointsArr = [
  544. [0, 0, 0],
  545. [x1, y1, z1],
  546. [x1, -y2, z1],
  547. [-x2, -y2, z1],
  548. [-x2, y1, z1],
  549. ];
  550. } else {
  551. let m1 = 0,
  552. m2 = 0,
  553. n1 = 0,
  554. n2 = 0,
  555. z2 = r1;
  556. m1 = Math.tan(THREE.MathUtils.degToRad(a)) * r1;
  557. n1 = Math.tan(THREE.MathUtils.degToRad(b)) * r1;
  558. m2 = Math.tan(THREE.MathUtils.degToRad(c)) * r1;
  559. n2 = Math.tan(THREE.MathUtils.degToRad(d)) * r1;
  560. pointsArr = [
  561. [m1, n1, z2],
  562. [m1, -n2, z2],
  563. [-m2, -n2, z2],
  564. [-m2, n1, z2],
  565. [x1, y1, z1],
  566. [x1, -y2, z1],
  567. [-x2, -y2, z1],
  568. [-x2, y1, z1],
  569. ];
  570. }
  571. this.initMesh(position, rotation, pointsArr, type, canMove);
  572. },
  573. // canMove 如果用于单独展示则可拖拽,用于全部预览则不可拖拽
  574. initMesh(position, rotation, pointsArr, type, canMove = false) {
  575. let points = pointsArr.map(
  576. (d) => new THREE.Vector3(d[0], d[1], d[2])
  577. );
  578. let color = null;
  579. if (type === "camera") {
  580. color = 0x0000ff;
  581. } else if (type === "ogt") {
  582. color = 0x008000;
  583. } else if (type === "lidar") {
  584. color = 0xff4500;
  585. } else {
  586. color = 0x0000ff;
  587. }
  588. let material = new THREE.MeshPhongMaterial({
  589. emissive: color,
  590. color: 0x4c4c4c,
  591. transparent: true,
  592. opacity: 0.3,
  593. lightMapIntensity: 0.1,
  594. side: THREE.DoubleSide,
  595. });
  596. let tixing = new ConvexGeometry(points);
  597. //通过ConvexGeometry凸包绘制一个梯台,当然也可以使用ExtrudeGeometry挤压几何体,
  598. this.cube = new THREE.Mesh(tixing, material);
  599. // this.cube.name = this.geometryName || "cube";
  600. if (position) {
  601. this.cube.position.x = position.x;
  602. this.cube.position.y = position.y;
  603. this.cube.position.z = position.z;
  604. }
  605. // if (rotation) {
  606. // this.cube.rotateX(rotation.x);
  607. // this.cube.rotateY(rotation.y);
  608. // this.cube.rotateZ(rotation.z);
  609. // }
  610. if (canMove) {
  611. this.cube.name = this.geometryName || "cube";
  612. if (rotation) {
  613. this.cube.rotateX(rotation.x);
  614. this.cube.rotateY(rotation.y);
  615. this.cube.rotateZ(rotation.z);
  616. }
  617. if (this.transformControls) {
  618. this.transformControls.attach(this.cube);
  619. }
  620. } else {
  621. if (rotation) {
  622. this.cube.rotation.set(
  623. (rotation.x * Math.PI) / 180,
  624. (rotation.y * Math.PI) / 180,
  625. (rotation.z * Math.PI) / 180
  626. );
  627. }
  628. this.cube.name = this.geometryName || "cubeA";
  629. this.cacheList.push(this.cube);
  630. }
  631. this.scene.add(this.cube);
  632. },
  633. // 添加拖拽控件
  634. initDragControls() {
  635. // 过滤不是 Mesh 的物体,例如辅助网格
  636. var objects = [];
  637. for (let i = 0; i < this.scene.children.length; i++) {
  638. if (this.scene.children[i].isMesh) {
  639. objects.push(this.scene.children[i]);
  640. }
  641. }
  642. if (this.dragControls) {
  643. this.dragControls.deactivate();
  644. this.dragControls.dispose();
  645. this.dragControls = null;
  646. }
  647. // 初始化拖拽控件
  648. this.dragControls = new DragControls(
  649. objects,
  650. this.camera,
  651. this.renderer.domElement
  652. );
  653. // 鼠标略过事件
  654. // this.dragControls.addEventListener("hoveron", (event) => {
  655. // // 让变换控件对象和选中的对象绑定
  656. // this.transformControls.attach(event.object);
  657. // });
  658. this.dragControls.addEventListener("drag", (e) => {
  659. // console.log(e);
  660. if (!this.canDrag) return;
  661. // this.$emit("posChange", (e.object.position.x + 0.5) >> 0, "x");
  662. // this.$emit("posChange", (e.object.position.y + 0.5) >> 0, "y");
  663. // this.$emit("posChange", (e.object.position.z + 0.5) >> 0, "z");
  664. });
  665. this.dragControls.addEventListener("dragstart", (e) => {
  666. this.controls.enabled = false;
  667. });
  668. this.dragControls.addEventListener("dragend", (e) => {
  669. this.controls.enabled = true;
  670. });
  671. },
  672. initCar0(model) {
  673. if (this.car) {
  674. // console.log(this.car);
  675. this.scene.remove(this.car);
  676. this.removeObj(this.car);
  677. this.car = null;
  678. }
  679. // const loading = this.$loading({
  680. // lock: true,
  681. // text: "模型加载中,请稍等...",
  682. // // spinner: "el-icon-loading",
  683. // background: "rgba(0, 0, 0, 0.2)",
  684. // });
  685. showFullScreenLoading();
  686. var that = this;
  687. var loader = new GLTFLoader(); //创建一个FBX加载器
  688. const dracoLoader = new DRACOLoader(); //
  689. dracoLoader.setDecoderPath(`${this.publicPath}draco/`); // 设置public下的解码路径,注意最后面的/
  690. dracoLoader.setDecoderConfig({ type: "js" }); // 使用兼容性强的draco_decoder.js解码器
  691. dracoLoader.preload(); // 初始化_initDecoder 解码器
  692. loader.setDRACOLoader(dracoLoader); // gltfloader使用dracoLoader
  693. // loader.load(
  694. // `${this.publicPath}glb/AudiA6_10.glb`,
  695. // function (obj) {
  696. loader.load(
  697. model,
  698. function (obj) {
  699. // loading.close();
  700. tryHideFullScreenLoading();
  701. // console.log(obj);
  702. /* for (let i = 0; i < obj.children.length; i++) {
  703. // obj.children[i].position.set(0, -135, 0);
  704. // obj.children[i].position.set(18, 36, 0);
  705. // obj.children[i].scale.set(30, 30, 30);
  706. // obj.children[i].scale.set(0.45, 0.45, 0.45);
  707. obj.children[i].rotation.set(
  708. (-90 * Math.PI) / 180,
  709. 0,
  710. (-180 * Math.PI) / 180
  711. );
  712. // obj.children[i].transparent.set(0.5);
  713. // that.transformControls.attach(obj.children[i]);
  714. } */
  715. obj.scene.rotation.set(
  716. (-90 * Math.PI) / 180,
  717. 0,
  718. (-180 * Math.PI) / 180
  719. );
  720. let s = 0;
  721. if (model.includes("928.glb")) {
  722. s = that.scale / that.scaleRate;
  723. } else {
  724. s = that.scale;
  725. }
  726. let scale = 30 * s;
  727. obj.scene.scale.set(scale, scale, scale);
  728. // obj.scene.scale.set(30, 30, 30);
  729. // 查看动画数据 2个剪辑对象AnimationClip,一个有关键帧动画,一个没有
  730. // console.log(obj.animations);
  731. // that.scene.add(obj);
  732. that.scene.add(obj.scene);
  733. that.car = obj.scene;
  734. },
  735. (xhr) => {
  736. // console.log(xhr);
  737. // console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
  738. },
  739. (error) => {
  740. // loading.close();
  741. tryHideFullScreenLoading();
  742. console.error(error);
  743. }
  744. );
  745. },
  746. initCar1() {
  747. // const loader2 = new DDSLoader();
  748. // const map1 = loader2.load(
  749. // `${this.publicPath}q5/AudiQ5_Texture.dds`
  750. // );
  751. // const map2 = loader2.load(`${this.publicPath}q5/env3.dds`);
  752. // const map3 = loader2.load(
  753. // `${this.publicPath}q5/Tx_Blur_AudiQ5.dds`
  754. // );
  755. // const map4 = loader2.load(`${this.publicPath}q5/Tx_Driver.dds`);
  756. // const map6 = loader2.load(
  757. // `${this.publicPath}q5/Tx_ln_AudiQ5_09.dds`
  758. // );
  759. // var that = this;
  760. // var loader = new FBXLoader(); //创建一个FBX加载器
  761. // // var loader = new GLTFLoader(); //创建一个FBX加载器
  762. // // loader.load(`${this.publicPath}6/a8/audia8.fbx`, function (obj) {
  763. // // loader.load(`${this.publicPath}6/q5/audiq5.fbx`, function (obj) {
  764. // // loader.load(`${this.publicPath}6/a8/audia8.fbx`, function (obj) {
  765. // // loader.load(`${this.publicPath}a8/audia8.fbx`, function (obj) {
  766. // // loader.load(`${this.publicPath}q5/audiq5.fbx`, function (obj) {
  767. // // loader.load(`${this.publicPath}lb/audiq5.fbx`, function (obj) {
  768. // loader.load(
  769. // `${this.publicPath}lb/1/fbx/untitled.fbx`,
  770. // function (obj) {
  771. // // loader.load(`${this.publicPath}q6/audiq5.fbx`, function (obj) {
  772. // // loader.load(`${this.publicPath}untitled.fbx`, function (obj) {
  773. // // loader.load(`${this.publicPath}untitled.glb`, function (obj) {
  774. // // loader.load(`${this.publicPath}fbx/Samba Dancing.fbx`, function (obj) {
  775. // // 可以在控制台打印obj对象,找到animations属性
  776. // console.log(obj);
  777. // obj.name = "car";
  778. // // obj.map = map1;
  779. // function _ChangeMaterialEmissive(parent) {
  780. // parent.traverse(function (child) {
  781. // // if (child instanceof THREE.Mesh) {
  782. // if (child.isMesh) {
  783. // // child.material.emissive = new THREE.Color(1, 1, 1);
  784. // // child.material.emissive = child.material.color;
  785. // // child.material.emissiveIntensity = 1;
  786. // // child.material.emissiveMap = child.material.map;
  787. // // child.material.envMap = child.material.map;
  788. // // child.material.alphaMap = child.material.map;
  789. // // child.material.aoMap = child.material.map;
  790. // // child.material.bumpMap = child.material.map;
  791. // // child.material.lightMap = child.material.map;
  792. // // child.material.normalMap = child.material.map;
  793. // // child.material.specularMap = child.material.map;
  794. // // child.material.transparent = true;
  795. // child.castShadow = true;
  796. // child.receiveShadow = true;
  797. // }
  798. // });
  799. // }
  800. // _ChangeMaterialEmissive(obj);
  801. // // obj.traverse(function (child) {
  802. // // if (child.isMesh) {
  803. // // // child.material.emissive = new THREE.Color(1, 1, 1);
  804. // // child.material.emissive = child.material.color;
  805. // // child.material.emissiveIntensity = 1;
  806. // // child.material.emissiveMap = child.material.map;
  807. // // child.castShadow = true;
  808. // // child.receiveShadow = true;
  809. // // }
  810. // // });
  811. // /* for (let i = 0; i < obj.children.length; i++) {
  812. // // obj.children[i].position.set(0, -135, 0);
  813. // // obj.children[i].position.set(18, 36, 0);
  814. // // obj.children[i].scale.set(30, 30, 30);
  815. // // obj.children[i].scale.set(0.45, 0.45, 0.45);
  816. // obj.children[i].rotation.set(
  817. // (-90 * Math.PI) / 180,
  818. // 0,
  819. // (-180 * Math.PI) / 180
  820. // );
  821. // // obj.children[i].transparent.set(0.5);
  822. // // that.transformControls.attach(obj.children[i]);
  823. // } */
  824. // obj.rotation.set(
  825. // (-90 * Math.PI) / 180,
  826. // 0,
  827. // (-180 * Math.PI) / 180
  828. // );
  829. // // obj.scale.set(30, 30, 30);
  830. // // 查看动画数据 2个剪辑对象AnimationClip,一个有关键帧动画,一个没有
  831. // // console.log(obj.animations);
  832. // that.scene.add(obj);
  833. // // that.scene.add(obj.scene);
  834. // }
  835. // );
  836. },
  837. initCar2() {
  838. var that = this;
  839. var loader2 = new THREE.ObjectLoader();
  840. // var loader2 = new THREE.MeshLambertMaterial();
  841. // loader2.load(`${that.publicPath}4.js`, function (obj) {
  842. loader2.load(`${that.publicPath}car.json`, function (obj) {
  843. // loader2.load(`${that.publicPath}Lidar.json`, function (obj) {
  844. // console.log(obj);
  845. obj.name = "car";
  846. // console.log(obj.type);
  847. // obj.position.set(0, 0, 0);
  848. obj.scale.set(30, 30, 30);
  849. obj.rotation.set(
  850. (-90 * Math.PI) / 180,
  851. 0,
  852. (-90 * Math.PI) / 180
  853. );
  854. // obj.layers.set(1);
  855. // that.transformControls.attach(obj);
  856. that.scene.add(obj);
  857. });
  858. },
  859. initCar3() {
  860. var Loader = new MTLLoader(); //材质文件加载器
  861. var loader = new OBJLoader(); //obj加载器
  862. var that = this;
  863. Loader.load(
  864. // "../../../assets/common/image/female02/female02.mtl",
  865. // "../../../../public/female02/female02.mtl",
  866. // `${that.publicPath}male02/male02.mtl`,
  867. // `${that.publicPath}GTR.mtl`,
  868. // `${that.publicPath}lidar.mtl`,
  869. // `${that.publicPath}female02/female02.mtl`,
  870. // `${that.publicPath}new/audiq5.mtl`,
  871. // `${that.publicPath}lb/obj/audiq5.mtl`,
  872. // `${that.publicPath}lb/00obj/00.mtl`,
  873. `${that.publicPath}lb/1/obj/untitled.mtl`,
  874. function (materials) {
  875. // 返回一个包含材质的对象MaterialCreator
  876. // console.log(materials);
  877. // materials.transparent = true;
  878. // materials.opacity = 0.15;
  879. // materials.side = THREE.DoubleSide;
  880. // materials.depthWrite = false;
  881. //obj的模型会和MaterialCreator包含的材质对应起来
  882. loader.setMaterials(materials);
  883. loader.load(
  884. // "../../../../public/female02/female02.obj",
  885. // `${that.publicPath}female02/female02.obj`,
  886. // `${that.publicPath}new/audiq5.obj`,
  887. // `${that.publicPath}lb/00obj/00.obj`,
  888. `${that.publicPath}lb/1/obj/untitled.obj`,
  889. // `${that.publicPath}male02/male02.obj`,
  890. // `${that.publicPath}GTR.obj`,
  891. // `${that.publicPath}lidar.obj`,
  892. // `${that.publicPath}oddysey_2021.obj`,
  893. function (obj) {
  894. // console.log(obj);
  895. obj.name = "car";
  896. // console.log(obj.toJSON());
  897. // console.log(JSON.stringify(obj.toJSON()));
  898. // if (that.car) {
  899. // that.scene.remove(that.car);
  900. // }
  901. for (let i = 0; i < obj.children.length; i++) {
  902. // obj.children[i].position.set(0, -135, 0);
  903. // obj.children[i].position.set(0, -30, 0);
  904. obj.children[i].position.set(0, 0, 0);
  905. // obj.children[i].scale.set(30, 30, 30);
  906. obj.children[i].rotation.set(
  907. (-90 * Math.PI) / 180,
  908. 0,
  909. (-180 * Math.PI) / 180
  910. );
  911. // obj.children[i].transparent.set(0.5);
  912. // that.transformControls.attach(obj.children[i]);
  913. }
  914. // that.transformControls.attach(that.cube);
  915. // console.log(obj.children[0].transparent);
  916. // that.transformControls.attach(obj);
  917. // that.car = obj;
  918. that.scene.add(obj); //返回的组对象插入场景中
  919. // this.camera.lookAt(obj);
  920. // loading.close();
  921. // that.camera.lookAt(obj.position);
  922. // 加载后操作
  923. // obj.children[0].scale.set(200, 200, 200); //缩放球体网格模型
  924. // 通过调节参数,地球表面的凹凸感更强
  925. // obj.children[0].material.normalScale.set(3, 3);
  926. // obj.children[0].position.set(100, 0, 0);
  927. }
  928. );
  929. }
  930. );
  931. },
  932. // 初始化车模型
  933. initCar(model) {
  934. // const loading = this.$loading({
  935. // lock: true,
  936. // text: "模型加载中,请稍等...",
  937. // // spinner: "el-icon-loading",
  938. // background: "rgba(0, 0, 0, 0.2)",
  939. // });
  940. // const car = this.scene.getObjectByName("car");
  941. // console.log(car);
  942. // console.log(this.scene);
  943. // if (car) {
  944. // this.scene.remove(car);
  945. // }
  946. // console.log(type);
  947. // if (type === "2") {
  948. // this.initCar1();
  949. // } else if (type === "f") {
  950. // this.initCar2();
  951. // } else if (type === "7") {
  952. // this.initCar3();
  953. // } else {
  954. // this.initCar1();
  955. // }
  956. // console.log(model);
  957. if (!model) return;
  958. if (!model.includes(".glb")) return;
  959. this.initCar0(model);
  960. // console.log(this.scene);
  961. // var Loader = new MTLLoader(); //材质文件加载器
  962. // var loader = new OBJLoader(); //obj加载器
  963. // var that = this;
  964. /* var loader1 = new THREE.BufferGeometryLoader();
  965. loader1.load(`${that.publicPath}car.json`, function (geometry) {
  966. // loader1.load(`${that.publicPath}Lidar.json`, function (geometry) {
  967. // 控制台查看加载放回的threejs对象结构
  968. console.log(geometry);
  969. var material = new THREE.MeshLambertMaterial({
  970. color: 0x0000ff,
  971. }); //材质对象Material
  972. var mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
  973. that.scene.add(mesh); //网格模型添加到场景中
  974. }); */
  975. // 没有材质文件,系统自动设置Phong网格材质
  976. // loader.load("../../../assets/common/image/oddysey_2021.obj", function (obj) {
  977. // loader.load("../../../assets/common/image/tree.obj", function (obj) {
  978. // loader.load(`${that.publicPath}oddysey_2021.obj`, function (obj) {
  979. // loader.load(`${that.publicPath}GTR.obj`, function (obj) {
  980. /* loader.load(`${that.publicPath}Lidar.obj`, function (obj) {
  981. if (that.car) {
  982. that.scene.remove(that.car);
  983. }
  984. console.log(666);
  985. // console.log(JSON.stringify(obj.toJSON()));
  986. that.car = obj;
  987. // 控制台查看返回结构:包含一个网格模型Mesh的组Group
  988. console.log(obj);
  989. // loading.close();
  990. // 查看加载器生成的材质对象:MeshPhongMaterial
  991. // console.log(obj.children[0].material);
  992. that.scene.add(obj);
  993. for (let i = 0; i < obj.children.length; i++) {
  994. obj.children[i].position.set(0, 100, 0);
  995. obj.children[i].scale.set(20, 20, 20);
  996. // obj.children[i].layers.set(1);
  997. }
  998. }); */
  999. },
  1000. // 初始化
  1001. init() {
  1002. this.initScene();
  1003. this.initCamera();
  1004. this.initRenderer();
  1005. this.initTransform();
  1006. this.initLight();
  1007. // this.initDragControls();
  1008. this.controls = new OrbitControls(
  1009. this.camera,
  1010. // this.renderer.domElement
  1011. document.getElementById("container")
  1012. ); //创建控件对象
  1013. this.controls.minDistance = 30;
  1014. this.controls.maxDistance = 600;
  1015. this.controls.update();
  1016. // this.controls.target = this.cube.position
  1017. // this.controls.addEventListener("change", (e) => {
  1018. // // console.log(document.getElementById("container"));
  1019. // console.log(this.renderer.domElement);
  1020. // console.log(document.querySelector('#container canvas'));
  1021. // // console.log(this.controls.getAzimuthalAngle(),this.controls.getPolarAngle());
  1022. // });
  1023. },
  1024. animate() {
  1025. this.raf = requestAnimationFrame(this.animate);
  1026. // this.renderer.render(this.scene, this.camera);
  1027. if (this.transformControls) {
  1028. this.transformControls.update();
  1029. }
  1030. this.controls.update();
  1031. // this.renderer.setClearColor("#272727");
  1032. // this.renderer.setViewport(
  1033. // 0,
  1034. // 0,
  1035. // this.container.clientWidth,
  1036. // this.container.clientHeight
  1037. // );
  1038. this.renderer.render(this.scene, this.camera);
  1039. return;
  1040. // inset scene
  1041. // this.renderer.setClearColor(0xdddddd, 1);
  1042. // this.renderer.setClearColor(0xffffff, 0);
  1043. // this.renderer.setClearAlpha(0);
  1044. this.renderer.clearDepth(); // important!
  1045. // this.scene2.background = new THREE.Color('hsla(100%, 100%, 100%, 0)');
  1046. // this.scene2.background = new THREE.Color('rgba(255, 255, 255, 0)');
  1047. // this.scene2.background = new THREE.Color('transparent');
  1048. // this.scene2.background = 0xffffff;
  1049. this.renderer.setScissorTest(true);
  1050. // this.renderer.setClearColor(0xffffff, 0.3);
  1051. // this.renderer.clearColor();
  1052. // this.renderer.setClearAlpha(0);
  1053. let insetWidth = this.container.clientWidth / 6; // square
  1054. let insetHeight = this.container.clientHeight / 6;
  1055. let x = insetHeight / 45;
  1056. let y = insetHeight * 4.2;
  1057. this.renderer.setScissor(x, y, insetWidth, insetHeight);
  1058. this.renderer.setViewport(x, y, insetWidth, insetHeight);
  1059. this.camera2.position.copy(this.camera.position);
  1060. this.camera2.quaternion.copy(this.camera.quaternion);
  1061. this.renderer.render(this.scene2, this.camera2);
  1062. // this.renderer.setClearAlpha(0);
  1063. // this.scene2.background = null;
  1064. this.renderer.setScissorTest(false);
  1065. },
  1066. onWindowResize() {
  1067. this.camera.aspect =
  1068. this.container.clientWidth / this.container.clientHeight;
  1069. this.camera.updateProjectionMatrix();
  1070. this.renderer.setSize(
  1071. this.container.clientWidth,
  1072. this.container.clientHeight
  1073. );
  1074. let insetWidth = this.container.clientWidth / 6; // square
  1075. let insetHeight = this.container.clientHeight / 6;
  1076. this.camera2.aspect = insetWidth / insetHeight;
  1077. this.camera2.updateProjectionMatrix();
  1078. },
  1079. go() {
  1080. this.container = document.getElementById("container");
  1081. this.init();
  1082. this.animate();
  1083. window.addEventListener("resize", this.onWindowResize);
  1084. },
  1085. /**
  1086. * 每编辑一个传感器则重新生成对应的物体
  1087. * @param {*} type 传感器类型
  1088. * @param {*} index 该传感器所在数组下标
  1089. * @param {*} onlyClear 是否只用来清空效果
  1090. */
  1091. reset(type, index = -1, onlyClear = false) {
  1092. this.canDrag = true;
  1093. if (this.cacheList.length > 0) {
  1094. this.scene.remove(...this.cacheList);
  1095. this.cacheList.forEach((i) => {
  1096. this.clearCache(i);
  1097. });
  1098. this.cacheList = [];
  1099. }
  1100. this.sensor = null;
  1101. const obj1 = this.scene.getObjectByName("cube");
  1102. if (obj1) {
  1103. this.scene.remove(obj1);
  1104. }
  1105. this.xAngle = 0;
  1106. this.yAngle = 0;
  1107. this.zAngle = 0;
  1108. if (this.dragControls) {
  1109. this.dragControls.deactivate();
  1110. this.dragControls.dispose();
  1111. this.dragControls = null;
  1112. }
  1113. if (this.transformControls) {
  1114. this.transformControls.detach();
  1115. }
  1116. // onlyClear若为true,表示只清除当前展示的东西,没有可增加展示的
  1117. if (onlyClear) return;
  1118. let z = +this.$parent.formA.sensorX || 0;
  1119. let x = +this.$parent.formA.sensorY || 0;
  1120. let y = +this.$parent.formA.sensorZ || 0;
  1121. let h = +this.$parent.formA.sensorP || 0;
  1122. let p = +this.$parent.formA.sensorR || 0;
  1123. let r = +this.$parent.formA.sensorH || 0;
  1124. x = Math.floor(x / this.rate);
  1125. y = Math.floor(y / this.rate);
  1126. z = Math.floor(z / this.rate);
  1127. if (type != "gps") {
  1128. this.initContent(
  1129. { x, y, z },
  1130. {
  1131. // x: (-90 * Math.PI) / 180,
  1132. x: 0,
  1133. y: 0,
  1134. z: 0,
  1135. },
  1136. type,
  1137. index,
  1138. true
  1139. );
  1140. }
  1141. setTimeout(() => {
  1142. this.initSensor({ x, y, z }, type, true);
  1143. const obj = this.scene.getObjectByName("cube");
  1144. if (!obj) return;
  1145. obj.position.x = x;
  1146. obj.position.y = y;
  1147. obj.position.z = z;
  1148. // obj.rotateX(0 * -1);
  1149. // obj.rotateX((h * Math.PI) / 180);
  1150. // this.xAngle = ((h - 90) * Math.PI) / 180;
  1151. this.xAngle = (h * Math.PI) / 180;
  1152. // obj.rotateY(0 * -1);
  1153. // obj.rotateY((p * Math.PI) / 180);
  1154. this.yAngle = (p * Math.PI) / 180;
  1155. // obj.rotateZ(0 * -1);
  1156. // obj.rotateZ((r * Math.PI) / 180);
  1157. this.zAngle = (r * Math.PI) / 180;
  1158. obj.rotation.set(this.xAngle, this.yAngle, this.zAngle);
  1159. }, 0);
  1160. },
  1161. // 初始化传感器
  1162. initSensor(
  1163. pos = { x: 0, y: 0, z: 0 },
  1164. type = "camera",
  1165. canMove = false
  1166. ) {
  1167. let Loader = new MTLLoader(); //材质文件加载器
  1168. let loader = new OBJLoader(); //obj加载器
  1169. let that = this;
  1170. let mtlUrl = "";
  1171. let objUrl = "";
  1172. if (type === "ogt1") {
  1173. var loader2 = new THREE.ObjectLoader();
  1174. loader2.load(
  1175. `${that.publicPath}sensor/ogt/ogt.json`,
  1176. function (obj) {
  1177. // console.log(obj);
  1178. that.cacheList.push(obj);
  1179. for (let i = 0; i < obj.children.length; i++) {
  1180. obj.children[i].scale.set(0.2, 0.2, 0.2);
  1181. }
  1182. obj.position.set(pos.x, pos.y, pos.z);
  1183. obj.name = "ogt";
  1184. if (canMove) {
  1185. that.sensor = obj;
  1186. }
  1187. that.scene.add(obj);
  1188. }
  1189. );
  1190. return;
  1191. }
  1192. if (type === "ogt1" && this.ogt) {
  1193. setTimeout(() => {
  1194. let obj1 = this.ogt;
  1195. // for (let i = 0; i < obj1.children.length; i++) {
  1196. // obj1.children[i].scale.set(0.2, 0.2, 0.2);
  1197. // }
  1198. obj1.position.set(pos.x, pos.y, pos.z);
  1199. if (canMove) {
  1200. that.sensor = obj1;
  1201. }
  1202. obj1.name = "ogt";
  1203. // console.log(that.scene);
  1204. that.scene.add(obj1); //返回的组对象插入场景中
  1205. }, 600);
  1206. return;
  1207. }
  1208. if (type === "camera") {
  1209. mtlUrl = `${that.publicPath}sensor/camera/camera.mtl`;
  1210. objUrl = `${that.publicPath}sensor/camera/camera.obj`;
  1211. } else if (type === "ogt") {
  1212. mtlUrl = `${that.publicPath}sensor/ogt/millimeter_wave_radar.mtl`;
  1213. objUrl = `${that.publicPath}sensor/ogt/millimeter_wave_radar.obj`;
  1214. } else if (type === "lidar") {
  1215. mtlUrl = `${that.publicPath}sensor/lidar/LIDAR.mtl`;
  1216. objUrl = `${that.publicPath}sensor/lidar/LIDAR.obj`;
  1217. } else if (type === "gps") {
  1218. mtlUrl = `${that.publicPath}sensor/gps/lidar.mtl`;
  1219. objUrl = `${that.publicPath}sensor/gps/lidar.obj`;
  1220. }
  1221. let loading = null;
  1222. if (type === "ogt") {
  1223. // loading = this.$loading({
  1224. // lock: true,
  1225. // text: "模型加载中,请稍等...",
  1226. // // spinner: "el-icon-loading",
  1227. // background: "rgba(0, 0, 0, 0.2)",
  1228. // });
  1229. showFullScreenLoading();
  1230. }
  1231. Loader.load(
  1232. mtlUrl,
  1233. function (materials) {
  1234. // console.log(materials);
  1235. loader.setMaterials(materials);
  1236. loader.load(objUrl, function (obj) {
  1237. // console.log(obj);
  1238. // console.log(obj.clone());
  1239. // console.log(JSON.stringify(obj.toJSON()));
  1240. // if (type === "ogt" && !that.ogt) {
  1241. // that.ogt = obj.clone();
  1242. // }
  1243. if (type === "ogt") {
  1244. // loading.close();
  1245. tryHideFullScreenLoading();
  1246. }
  1247. that.cacheList.push(obj);
  1248. for (let i = 0; i < obj.children.length; i++) {
  1249. if (type === "camera") {
  1250. // let scale = 0.8 * that.scale;
  1251. obj.children[i].scale.set(0.8, 0.8, 0.8);
  1252. } else if (type === "ogt") {
  1253. obj.children[i].scale.set(0.2, 0.2, 0.2);
  1254. } else if (type === "lidar") {
  1255. obj.children[i].scale.set(0.1, 0.1, 0.1);
  1256. } else if (type === "gps") {
  1257. obj.children[i].scale.set(40, 40, 40);
  1258. }
  1259. // obj.children[i].position.set(0, 0, 0);
  1260. // obj.children[i].geometry.translate(pos.x, pos.y, pos.z);
  1261. // obj.children[i].position.set(pos.x, pos.y, pos.z);
  1262. // obj.children[i].geometry.translate(-pos.x, -pos.y, -pos.z);
  1263. // obj.children[i].scale.set(30, 30, 30);
  1264. }
  1265. obj.position.set(pos.x, pos.y, pos.z);
  1266. // obj.geometry.translate.set(pos.x, pos.y, pos.z);
  1267. if (canMove) {
  1268. that.sensor = obj;
  1269. if (type === "gps" && that.transformControls) {
  1270. that.transformControls.attach(that.sensor);
  1271. }
  1272. }
  1273. that.scene.add(obj); //返回的组对象插入场景中
  1274. });
  1275. },
  1276. (xhr) => {
  1277. // console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
  1278. },
  1279. (error) => {
  1280. if (type === "ogt") {
  1281. // loading.close();
  1282. tryHideFullScreenLoading();
  1283. }
  1284. console.error(error);
  1285. }
  1286. );
  1287. },
  1288. // 初始化已保存过的传感器
  1289. initContentToShow(r, position = { x: 0, y: 0, z: 0 }, rotation, type) {
  1290. var cubeGeometry = new THREE.ConeGeometry(
  1291. r || 45 * this.scale,
  1292. 150 * this.scale,
  1293. 4,
  1294. 1,
  1295. false
  1296. );
  1297. cubeGeometry.translate(0, -75 * this.scale, 0);
  1298. let obj = {
  1299. transparent: true,
  1300. opacity: 0.3,
  1301. // emissive: 0xfaff72,
  1302. lightMapIntensity: 0.1,
  1303. color: 0x4c4c4c,
  1304. };
  1305. if (type === "camera") {
  1306. obj.emissive = 0x000080;
  1307. // obj.color = 0xfaff72;
  1308. } else if (type === "ogt") {
  1309. obj.emissive = 0x008000;
  1310. // obj.color = 0x33cc66;
  1311. } else if (type === "lidar") {
  1312. // obj.emissive = 0xff0000;
  1313. // obj.color = 0xcc3366;
  1314. obj.emissive = 0xff4500;
  1315. } else if (type === "gps") {
  1316. // obj.emissive = 0xfaff72;
  1317. // obj.color = 0x3366cc;
  1318. obj.emissive = 0x8a2be2;
  1319. }
  1320. var cubeMaterial = new THREE.MeshLambertMaterial(obj);
  1321. // var cubeMaterial = new THREE.MeshBasicMaterial(obj);
  1322. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  1323. cube.name = this.geometryName || "cubeA";
  1324. cube.rotateX((-90 * Math.PI) / 180);
  1325. if (position) {
  1326. cube.position.x = position.x;
  1327. cube.position.y = position.y;
  1328. cube.position.z = position.z;
  1329. }
  1330. if (rotation) {
  1331. // cube.rotateX(0 * -1);
  1332. // cube.rotateX((rotation.x * Math.PI) / 180);
  1333. // cube.rotateY(0 * -1);
  1334. // cube.rotateY((rotation.y * Math.PI) / 180);
  1335. // cube.rotateZ(0 * -1);
  1336. // cube.rotateZ((rotation.z * Math.PI) / 180);
  1337. cube.rotation.set(
  1338. ((rotation.x - 90) * Math.PI) / 180,
  1339. (rotation.y * Math.PI) / 180,
  1340. (rotation.z * Math.PI) / 180
  1341. );
  1342. }
  1343. this.cacheList.push(cube);
  1344. this.scene.add(cube);
  1345. },
  1346. // 生成一种类型的全部显示器
  1347. showSensor(sensor, type) {
  1348. if (sensor && Array.isArray(sensor) && sensor.length > 0) {
  1349. sensor.forEach((item, index) => {
  1350. // 若是传感器没保存配置项,则不展示
  1351. if (
  1352. item.sensorX == null &&
  1353. item.sensorY == null &&
  1354. item.sensorZ == null
  1355. ) {
  1356. return;
  1357. }
  1358. if (type != "gps") {
  1359. this.initContent(
  1360. {
  1361. z: Math.floor(item.sensorX / this.rate || 0),
  1362. x: Math.floor(item.sensorY / this.rate || 0),
  1363. y: Math.floor(item.sensorZ / this.rate || 0),
  1364. },
  1365. {
  1366. x: +item.sensorP,
  1367. y: +item.sensorR,
  1368. z: +item.sensorH,
  1369. },
  1370. type,
  1371. index,
  1372. false
  1373. );
  1374. }
  1375. /* this.initContentToShow(
  1376. null,
  1377. {
  1378. z: Math.floor(item.sensorX / this.rate || 0),
  1379. x: Math.floor(item.sensorY / this.rate || 0),
  1380. y: Math.floor(item.sensorZ / this.rate || 0),
  1381. },
  1382. {
  1383. x: +item.sensorP,
  1384. y: +item.sensorR,
  1385. z: +item.sensorH,
  1386. },
  1387. type
  1388. ); */
  1389. this.initSensor(
  1390. {
  1391. z: Math.floor(item.sensorX / this.rate || 0),
  1392. x: Math.floor(item.sensorY / this.rate || 0),
  1393. y: Math.floor(item.sensorZ / this.rate || 0),
  1394. },
  1395. type,
  1396. false
  1397. );
  1398. });
  1399. }
  1400. },
  1401. // 显示全部
  1402. showAll() {
  1403. // 避免重复加载所有传感器
  1404. if (!this.canDrag) return;
  1405. this.canDrag = false;
  1406. if (this.cacheList.length > 0) {
  1407. this.scene.remove(...this.cacheList);
  1408. this.cacheList = [];
  1409. }
  1410. const obj = this.scene.getObjectByName("cube");
  1411. if (obj) {
  1412. this.scene.remove(obj);
  1413. }
  1414. if (this.dragControls) {
  1415. this.dragControls.deactivate();
  1416. this.dragControls.dispose();
  1417. this.dragControls = null;
  1418. }
  1419. if (this.transformControls) {
  1420. this.transformControls.detach();
  1421. }
  1422. // console.log(this.configList);
  1423. this.showSensor(this.configList.camera, "camera");
  1424. this.showSensor(this.configList.ogt, "ogt");
  1425. this.showSensor(this.configList.lidar, "lidar");
  1426. this.showSensor(this.configList.gps, "gps");
  1427. },
  1428. removeScene() {
  1429. this.clearScene();
  1430. },
  1431. clearCache(item) {
  1432. if (item.geometry && item.geometry.dispose) item.geometry.dispose();
  1433. if (item.material && item.material.dispose) item.material.dispose();
  1434. },
  1435. clearScene() {
  1436. this.removeObj(this.scene);
  1437. },
  1438. removeObj(obj) {
  1439. let arr = obj.children.filter((x) => x);
  1440. arr.forEach((item) => {
  1441. if (item.children.length) {
  1442. this.removeObj(item);
  1443. } else {
  1444. this.clearCache(item);
  1445. item.clear();
  1446. }
  1447. }),
  1448. obj.clear();
  1449. arr = null;
  1450. },
  1451. showTX() {
  1452. var pointsArr1 = [
  1453. // [5, -1, 33],
  1454. // [5, 11, 33],
  1455. // [-5, -1, 33],
  1456. // [-5, 11, 33],
  1457. [0, 0, 0],
  1458. [10, -11, 66],
  1459. [30, 31, 66],
  1460. [-20, -21, 66],
  1461. [-30, 21, 66],
  1462. ];
  1463. let x = 0,
  1464. y = 0,
  1465. z = 0;
  1466. let r = 60;
  1467. z = r;
  1468. let a = 120,
  1469. b = 40;
  1470. x = Math.tan(THREE.MathUtils.degToRad(a / 2)) * r;
  1471. y = Math.tan(THREE.MathUtils.degToRad(b / 2)) * r;
  1472. let x1 = 0,
  1473. y1 = 0,
  1474. z1 = 0;
  1475. let r1 = 20;
  1476. z1 = r1;
  1477. x1 = Math.tan(THREE.MathUtils.degToRad(a / 2)) * r1;
  1478. y1 = Math.tan(THREE.MathUtils.degToRad(b / 2)) * r1;
  1479. let pointsArr2 = [
  1480. [0, 0, 0],
  1481. // [-x1, y1, z1],
  1482. // [-x1, -y1, z1],
  1483. // [x1, y1, z1],
  1484. // [x1, -y1, z1],
  1485. [-x, y, z],
  1486. [-x, -y, z],
  1487. [x, y, z],
  1488. [x, -y, z],
  1489. ];
  1490. let x2 = 0,
  1491. y2 = 0,
  1492. z2 = 0,
  1493. x3 = 0,
  1494. y3 = 0,
  1495. z3 = 0,
  1496. x4 = 0,
  1497. y4 = 0,
  1498. z4 = 0,
  1499. x6 = 0,
  1500. y6 = 0,
  1501. z6 = 0,
  1502. r2 = 90,
  1503. a2 = 30,
  1504. b2 = 60,
  1505. c2 = 90,
  1506. d2 = 60;
  1507. x2 = Math.tan(THREE.MathUtils.degToRad(a2 / 2)) * r2;
  1508. y2 = Math.tan(THREE.MathUtils.degToRad(b2 / 2)) * r2;
  1509. x3 = Math.tan(THREE.MathUtils.degToRad(c2 / 2)) * r2;
  1510. y3 = Math.tan(THREE.MathUtils.degToRad(d2 / 2)) * r2;
  1511. let pointsArr3 = [
  1512. [0, 0, 0],
  1513. [-x2, y3, r2],
  1514. [-x3, -y3, r2],
  1515. [x2, y2, r2],
  1516. [x3, -y2, r2],
  1517. ];
  1518. // console.log(pointsArr3);
  1519. let points = pointsArr3.map(
  1520. (d) => new THREE.Vector3(d[0], d[1], d[2])
  1521. );
  1522. let material = new THREE.MeshPhongMaterial({
  1523. color: 0x2c85e1,
  1524. shininess: 60,
  1525. specular: 0x2c85e1,
  1526. // opacity: 0.5,
  1527. // transparent: true,
  1528. });
  1529. let tixing = new ConvexGeometry(points);
  1530. //通过ConvexGeometry凸包绘制一个梯台,当然也可以使用ExtrudeGeometry挤压几何体,
  1531. let tixingMesh = new THREE.Mesh(tixing, material);
  1532. this.scene.add(tixingMesh);
  1533. // console.log(tixingMesh);
  1534. /* let vertices = [];
  1535. vertices.push(-155, 24, 90);
  1536. vertices.push(-63, -24, 90);
  1537. vertices.push(155, 91, 90);
  1538. vertices.push(63, -90, 90);
  1539. const geometry1 = new THREE.BufferGeometry();
  1540. geometry1.setAttribute(
  1541. "position",
  1542. new THREE.Float32BufferAttribute(vertices, 3)
  1543. );
  1544. const material1 = new THREE.PointsMaterial({
  1545. color: 0xff0000,
  1546. size: 10,
  1547. fog: false,
  1548. });
  1549. const points1 = new THREE.Points(geometry1, material1);
  1550. this.scene.add(points1); */
  1551. // if (this.transformControls) {
  1552. // this.transformControls.attach(tixingMesh);
  1553. // }
  1554. // const a = new THREE.Vector3(1, 1, 0);
  1555. // const b = new THREE.Vector3(0, 0, 0);
  1556. // const d = a.distanceTo(b);
  1557. // const e = a.angleTo(b);
  1558. // console.log(d, e, (e * 180) / Math.PI, THREE.MathUtils.radToDeg(e));
  1559. // (value * Math.PI) / 180;
  1560. },
  1561. a() {
  1562. let line, thresholdLine, segments, thresholdSegments;
  1563. let renderer, scene, scene2, camera, camera2, controls;
  1564. let raycaster, sphereInter, sphereOnLine;
  1565. let matLine, matThresholdLine;
  1566. // viewport
  1567. let insetWidth;
  1568. let insetHeight;
  1569. const pointer = new THREE.Vector2(Infinity, Infinity);
  1570. init();
  1571. animate();
  1572. function init() {
  1573. let container = document.getElementById("container");
  1574. renderer = new THREE.WebGLRenderer({
  1575. antialias: true,
  1576. alpha: true,
  1577. });
  1578. renderer.setPixelRatio(window.devicePixelRatio);
  1579. renderer.setClearColor(0x00ff00);
  1580. renderer.setSize(container.clientWidth, container.clientHeight);
  1581. container.appendChild(renderer.domElement);
  1582. scene = new THREE.Scene();
  1583. camera = new THREE.PerspectiveCamera(
  1584. 40,
  1585. container.clientWidth / container.clientHeight,
  1586. 1,
  1587. 1000
  1588. );
  1589. camera.position.set(-40, 0, 60);
  1590. camera2 = new THREE.PerspectiveCamera(40, 1, 1, 1000);
  1591. camera2.position.copy(camera.position);
  1592. controls = new OrbitControls(camera, renderer.domElement);
  1593. controls.minDistance = 10;
  1594. controls.maxDistance = 500;
  1595. // raycaster = new THREE.Raycaster();
  1596. // raycaster.params.Line2 = {};
  1597. // raycaster.params.Line2.threshold = 0;
  1598. // const sphereGeometry = new THREE.SphereGeometry(0.25);
  1599. // const sphereInterMaterial = new THREE.MeshBasicMaterial({
  1600. // color: 0xff0000,
  1601. // depthTest: false,
  1602. // });
  1603. // const sphereOnLineMaterial = new THREE.MeshBasicMaterial({
  1604. // color: 0x00ff00,
  1605. // depthTest: false,
  1606. // });
  1607. // sphereInter = new THREE.Mesh(
  1608. // sphereGeometry,
  1609. // sphereInterMaterial
  1610. // );
  1611. // sphereOnLine = new THREE.Mesh(
  1612. // sphereGeometry,
  1613. // sphereOnLineMaterial
  1614. // );
  1615. // sphereInter.visible = false;
  1616. // sphereOnLine.visible = false;
  1617. // sphereInter.renderOrder = 10;
  1618. // sphereOnLine.renderOrder = 10;
  1619. // scene.add(sphereInter);
  1620. // scene.add(sphereOnLine);
  1621. // Position and THREE.Color Data
  1622. const positions = [];
  1623. const colors = [];
  1624. const points = [];
  1625. for (let i = -50; i < 50; i++) {
  1626. const t = i / 3;
  1627. points.push(
  1628. new THREE.Vector3(
  1629. t * Math.sin(2 * t),
  1630. t,
  1631. t * Math.cos(2 * t)
  1632. )
  1633. );
  1634. }
  1635. const spline = new THREE.CatmullRomCurve3(points);
  1636. const divisions = Math.round(3 * points.length);
  1637. const point = new THREE.Vector3();
  1638. const color = new THREE.Color();
  1639. for (let i = 0, l = divisions; i < l; i++) {
  1640. const t = i / l;
  1641. spline.getPoint(t, point);
  1642. positions.push(point.x, point.y, point.z);
  1643. color.setHSL(t, 1.0, 0.5);
  1644. colors.push(color.r, color.g, color.b);
  1645. }
  1646. const lineGeometry = new LineGeometry();
  1647. lineGeometry.setPositions(positions);
  1648. lineGeometry.setColors(colors);
  1649. const segmentsGeometry = new LineSegmentsGeometry();
  1650. segmentsGeometry.setPositions(positions);
  1651. segmentsGeometry.setColors(colors);
  1652. matLine = new LineMaterial({
  1653. color: 0xffffff,
  1654. linewidth: 1, // in world units with size attenuation, pixels otherwise
  1655. worldUnits: true,
  1656. vertexColors: true,
  1657. //resolution: // to be set by renderer, eventually
  1658. alphaToCoverage: true,
  1659. });
  1660. matThresholdLine = new LineMaterial({
  1661. color: 0xffffff,
  1662. linewidth: matLine.linewidth, // in world units with size attenuation, pixels otherwise
  1663. worldUnits: true,
  1664. // vertexColors: true,
  1665. transparent: true,
  1666. opacity: 0.2,
  1667. depthTest: false,
  1668. visible: false,
  1669. //resolution: // to be set by renderer, eventually
  1670. });
  1671. // segments = new LineSegments2(segmentsGeometry, matLine);
  1672. // segments.computeLineDistances();
  1673. // segments.scale.set(1, 1, 1);
  1674. // scene.add(segments);
  1675. // segments.visible = false;
  1676. // thresholdSegments = new LineSegments2(
  1677. // segmentsGeometry,
  1678. // matThresholdLine
  1679. // );
  1680. // thresholdSegments.computeLineDistances();
  1681. // thresholdSegments.scale.set(1, 1, 1);
  1682. // scene.add(thresholdSegments);
  1683. // thresholdSegments.visible = false;
  1684. line = new Line2(lineGeometry, matLine);
  1685. line.computeLineDistances();
  1686. line.scale.set(1, 1, 1);
  1687. // scene.add(line);
  1688. var cubeGeometry = new THREE.ConeGeometry(75, 150, 4, 1, false);
  1689. cubeGeometry.translate(0, -75, 0);
  1690. let obj = {
  1691. color: 0x4c4c4c,
  1692. // wireframe: true,
  1693. transparent: true,
  1694. opacity: 0.3,
  1695. // ambient: 0x00ff00,
  1696. emissive: 0x0000ff,
  1697. lightMapIntensity: 0.1,
  1698. };
  1699. var cubeMaterial = new THREE.MeshLambertMaterial(obj);
  1700. let cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  1701. scene.add(cube);
  1702. // thresholdLine = new Line2(lineGeometry, matThresholdLine);
  1703. // thresholdLine.computeLineDistances();
  1704. // thresholdLine.scale.set(1, 1, 1);
  1705. // scene.add(thresholdLine);
  1706. const geo = new THREE.BufferGeometry();
  1707. geo.setAttribute(
  1708. "position",
  1709. new THREE.Float32BufferAttribute(positions, 3)
  1710. );
  1711. geo.setAttribute(
  1712. "color",
  1713. new THREE.Float32BufferAttribute(colors, 3)
  1714. );
  1715. //
  1716. document.addEventListener("pointermove", onPointerMove);
  1717. window.addEventListener("resize", onWindowResize);
  1718. onWindowResize();
  1719. }
  1720. function onWindowResize() {
  1721. camera.aspect = container.clientWidth / container.clientHeight;
  1722. camera.updateProjectionMatrix();
  1723. renderer.setSize(container.clientWidth, container.clientHeight);
  1724. insetWidth = container.clientHeight / 6; // square
  1725. insetHeight = container.clientHeight / 6;
  1726. camera2.aspect = insetWidth / insetHeight;
  1727. camera2.updateProjectionMatrix();
  1728. }
  1729. function onPointerMove(event) {
  1730. pointer.x = (event.clientX / container.clientWidth) * 2 - 1;
  1731. pointer.y = -(event.clientY / container.clientHeight) * 2 + 1;
  1732. }
  1733. function animate() {
  1734. requestAnimationFrame(animate);
  1735. // main scene
  1736. renderer.setClearColor(0xdddddd);
  1737. renderer.setViewport(
  1738. 0,
  1739. 0,
  1740. container.clientWidth,
  1741. container.clientHeight
  1742. );
  1743. // raycaster.setFromCamera(pointer, camera);
  1744. // const obj = line.visible ? line : segments;
  1745. // const intersects = raycaster.intersectObject(obj, true);
  1746. // if (intersects.length > 0) {
  1747. // sphereInter.visible = true;
  1748. // sphereOnLine.visible = true;
  1749. // sphereInter.position.copy(intersects[0].point);
  1750. // sphereOnLine.position.copy(intersects[0].pointOnLine);
  1751. // const i = intersects[0].faceIndex;
  1752. // const colors =
  1753. // obj.geometry.getAttribute("instanceColorStart");
  1754. // const color = new THREE.Color().setRGB(
  1755. // colors.getX(i),
  1756. // colors.getY(i),
  1757. // colors.getZ(i)
  1758. // );
  1759. // sphereInter.material.color.copy(
  1760. // color.clone().offsetHSL(0.3, 0, 0)
  1761. // );
  1762. // sphereOnLine.material.color.copy(
  1763. // color.clone().offsetHSL(0.7, 0, 0)
  1764. // );
  1765. // renderer.domElement.style.cursor = "crosshair";
  1766. // } else {
  1767. // sphereInter.visible = false;
  1768. // sphereOnLine.visible = false;
  1769. // renderer.domElement.style.cursor = "";
  1770. // }
  1771. // renderer will set this eventually
  1772. matLine.resolution.set(
  1773. container.clientWidth,
  1774. container.clientHeight
  1775. ); // resolution of the viewport
  1776. matThresholdLine.resolution.set(
  1777. container.clientWidth,
  1778. container.clientHeight
  1779. ); // resolution of the viewport
  1780. // gpuPanel.startQuery();
  1781. renderer.render(scene, camera);
  1782. // gpuPanel.endQuery();
  1783. // inset scene
  1784. renderer.setClearColor(0xff00ff, 1);
  1785. renderer.clearDepth(); // important!
  1786. renderer.setScissorTest(true);
  1787. renderer.setScissor(20, 20, insetWidth, insetHeight);
  1788. renderer.setViewport(20, 20, insetWidth, insetHeight);
  1789. camera2.position.copy(camera.position);
  1790. camera2.quaternion.copy(camera.quaternion);
  1791. // renderer will set this eventually
  1792. matLine.resolution.set(insetWidth, insetHeight); // resolution of the inset viewport
  1793. scene2 = new THREE.Scene();
  1794. var cubeGeometry = new THREE.ConeGeometry(75, 150, 6, 1, false);
  1795. cubeGeometry.translate(0, -75, 0);
  1796. let obj = {
  1797. color: 0x4c4c4c,
  1798. // wireframe: true,
  1799. // transparent: true,
  1800. // opacity: 0.3,
  1801. // ambient: 0x00ff00,
  1802. // emissive: 0x00ff00,
  1803. lightMapIntensity: 0.1,
  1804. };
  1805. var cubeMaterial = new THREE.MeshLambertMaterial(obj);
  1806. let cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  1807. scene2.add(cube);
  1808. renderer.render(scene2, camera2);
  1809. renderer.setScissorTest(false);
  1810. }
  1811. },
  1812. },
  1813. mounted() {
  1814. // this.a();
  1815. this.go();
  1816. // this.showTX();
  1817. // console.log(THREE.Loader.Handlers.add);
  1818. // THREE.Loader.Handlers.add( /\.dds$/i, new DDSLoader() );
  1819. // console.log(new THREE.LoadingManager()); RGBELoader
  1820. // let dds = new THREE.LoadingManager();
  1821. // dds.addHandler(/\.dds$/i, new DDSLoader());
  1822. // dds.addHandler(/\.rgb$/i, new RGBELoader());
  1823. // console.log(dds);
  1824. // THREE.DefaultLoadingManager.addHandler(/\.dds$/i, new DDSLoader());
  1825. // THREE.DefaultLoadingManager.addHandler(/\.rgb$/i, new RGBELoader());
  1826. // console.log(THREE.DefaultLoadingManager);
  1827. // console.log(THREE.LoadingManager.addHandler);
  1828. // THREE.Loader.Handlers.add( /\.dds$/i, new DDSLoader());
  1829. // THREE.Loader.addHandler(/\.dds$/i, new DDSLoader());
  1830. // THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );
  1831. // THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );
  1832. // if (!this.isAdd) {
  1833. // setTimeout(() => {
  1834. // this.initCar();
  1835. // }, 270);
  1836. // }
  1837. },
  1838. destroyed() {
  1839. window.removeEventListener("resize", this.onWindowResize);
  1840. cancelAnimationFrame(this.raf);
  1841. if (this.renderer) {
  1842. this.renderer.renderLists.dispose();
  1843. this.renderer.dispose();
  1844. this.renderer.forceContextLoss();
  1845. this.renderer.domElement = null;
  1846. this.renderer.content = null;
  1847. this.renderer = null;
  1848. }
  1849. if (this.dragControls) {
  1850. this.dragControls.deactivate();
  1851. this.dragControls.dispose();
  1852. this.dragControls = null;
  1853. }
  1854. if (this.controls) {
  1855. this.controls.dispose();
  1856. this.controls = null;
  1857. }
  1858. if (this.transformControls) {
  1859. this.transformControls.detach();
  1860. this.transformControls.dispose();
  1861. this.transformControls = null;
  1862. }
  1863. if (this.cacheList.length > 0) {
  1864. // this.scene.remove(...this.cacheList);
  1865. this.cacheList = [];
  1866. }
  1867. this.clearScene();
  1868. this.scene = null;
  1869. this.scene2 = null;
  1870. this.camera = null;
  1871. this.camera2 = null;
  1872. this.light = null;
  1873. this.geometryName = null;
  1874. this.mesh = null;
  1875. this.cube = null;
  1876. this.container = null;
  1877. this.car = null;
  1878. this.cubeTexture = null;
  1879. this.raf = null;
  1880. THREE.Cache.clear();
  1881. },
  1882. };
  1883. </script>
  1884. <style lang="less" scoped>
  1885. #container {
  1886. width: 100%;
  1887. height: calc(100vh - 125px);
  1888. }
  1889. </style>