ThreeModel1.vue 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. <template>
  2. <div class="three-model" @click="clickEvent">
  3. <loading ref="pageLoading"></loading>
  4. <div class="map-3d" :style="'transform: rotate('+mapDeg+'deg);'">
  5. <img :src="require('@assets/png/3dmap.png')" alt="">
  6. </div>
  7. <div class="three-html-layer">
  8. <div v-for="(info, index) of htmlLayer" :key="index"
  9. class="three-html-dom fan-info"
  10. :id="info.id"
  11. v-show="info.show"
  12. :style="'left: ' + info.x + 'px; top: ' + info.y + 'px;'"
  13. >
  14. <div class="fan-name pointer" @click.stop="info.click">{{info.name}}</div>
  15. </div>
  16. <div
  17. class="three-html-dom build-info"
  18. :id="fanInfoLayer.id"
  19. v-show="fanInfoLayer.show"
  20. :style="'left: ' + fanInfoLayer.x + 'px; top: ' + fanInfoLayer.y + 'px;'"
  21. >
  22. <div class="build-info-close" @click.stop="hideFanInfo">
  23. <i class="el-icon-close"></i>
  24. </div>
  25. <div class="build-info-item purple" :style="'left: '+circleXY[0].x+'px; top: -'+circleXY[0].y+'px'">
  26. <div class="build-info-item-num">97</div>
  27. <div class="build-info-item-text">健康度</div>
  28. </div>
  29. <div class="build-info-item blue" :style="'left: '+circleXY[1].x+'px; top: -'+circleXY[1].y+'px'">
  30. <div class="build-info-item-num">97</div>
  31. <div class="build-info-item-text">优</div>
  32. </div>
  33. <div class="build-info-item yellow" :style="'left: '+circleXY[2].x+'px; top: -'+circleXY[2].y+'px'">
  34. <div class="build-info-item-num">97</div>
  35. <div class="build-info-item-text">良</div>
  36. </div>
  37. <div class="build-info-item orange" :style="'left: '+circleXY[3].x+'px; top: -'+circleXY[3].y+'px'">
  38. <div class="build-info-item-num">97</div>
  39. <div class="build-info-item-text">差</div>
  40. </div>
  41. <div class="build-info-item red" :style="'left: '+circleXY[4].x+'px; top: -'+circleXY[4].y+'px'">
  42. <div class="build-info-item-num">97</div>
  43. <div class="build-info-item-text">故障</div>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. import loading from "@com/coms/loading/loading.vue";
  51. import * as THREE from "three";
  52. import { GLTFLoader } from "@node/three/examples/jsm/loaders/GLTFLoader.js";
  53. import { OrbitControls } from "@node/three/examples/jsm/controls/OrbitControls.js";
  54. import { GeometryUtils } from "@node/three/examples/jsm/utils/GeometryUtils.js";
  55. let camera, scene, renderer, controls;
  56. let mixers = [];
  57. let clock = new THREE.Clock();
  58. let fanAnimates = [];
  59. let fans = [];
  60. // let cylinder = null;
  61. export default {
  62. // 名称
  63. name: "ThreeModel1",
  64. // 使用组件
  65. components: {
  66. loading,
  67. },
  68. // 传入参数
  69. props: {},
  70. // 自定义事件
  71. emits: {
  72. when: null,
  73. },
  74. // 数据
  75. data() {
  76. return {
  77. pointer: false,
  78. htmlLayer: [
  79. { // 麻黄山
  80. id: "fan-mhs",
  81. x: 0,
  82. y: 0,
  83. show: true,
  84. ox: 50,
  85. oy: 10,
  86. position: null,
  87. name: "麻黄山",
  88. click: function () { console.log("麻黄山") },
  89. },
  90. { // 牛首山
  91. id: "fan-nss",
  92. x: 0,
  93. y: 0,
  94. show: true,
  95. ox: 35,
  96. oy: 0,
  97. position: null,
  98. name: "牛首山",
  99. click: function () { console.log("牛首山") },
  100. },
  101. { // 青山
  102. id: "fan-qs",
  103. x: 0,
  104. y: 0,
  105. show: true,
  106. ox: 60,
  107. oy: 10,
  108. position: null,
  109. name: "青山",
  110. click: function () { console.log("青山") },
  111. },
  112. { // 石板泉
  113. id: "fan-sbq",
  114. x: 0,
  115. y: 0,
  116. show: true,
  117. ox: 40,
  118. oy: 0,
  119. position: null,
  120. name: "石板泉",
  121. click: function () { console.log("石板泉") },
  122. },
  123. { // 香山
  124. id: "fan-qs",
  125. x: 0,
  126. y: 0,
  127. show: true,
  128. ox: 25,
  129. oy: 20,
  130. position: null,
  131. name: "香山",
  132. click: function () { console.log("香山") },
  133. },
  134. { // 大武口
  135. id: "light-dwk",
  136. x: 0,
  137. y: 0,
  138. show: true,
  139. ox: 350,
  140. oy: 80,
  141. position: null,
  142. name: "大武口",
  143. click: function () { console.log("大武口") },
  144. },
  145. { // 平罗
  146. id: "light-pl",
  147. x: 0,
  148. y: 0,
  149. show: true,
  150. ox: 350,
  151. oy: 80,
  152. position: null,
  153. name: "平罗",
  154. click: function () { console.log("平罗") },
  155. },
  156. { // 马场湖
  157. id: "light-mch",
  158. x: 0,
  159. y: 0,
  160. show: true,
  161. ox: 250,
  162. oy: 80,
  163. position: null,
  164. name: "马场湖",
  165. click: function () { console.log("马场湖") },
  166. },
  167. { // 宣和
  168. id: "light-xh",
  169. x: 0,
  170. y: 0,
  171. show: true,
  172. ox: 280,
  173. oy: 90,
  174. position: null,
  175. name: "宣和",
  176. click: function () { console.log("宣和") },
  177. },
  178. { // 海子井
  179. id: "light-hzj",
  180. x: 0,
  181. y: 0,
  182. show: true,
  183. ox: 310,
  184. oy: 120,
  185. position: null,
  186. name: "海子井",
  187. click: function () { console.log("海子井") },
  188. },
  189. ],
  190. fanInfoLayer: {
  191. id: "fan-info",
  192. x: 0,
  193. y: 0,
  194. show: false,
  195. ox: 70,
  196. oy: -40,
  197. position: null,
  198. },
  199. circleXY: [
  200. { x: (130*Math.cos(180/180*Math.PI)-33), y: (130*Math.sin(180/180*Math.PI)+33) },
  201. { x: (130*Math.cos(135/180*Math.PI)-33), y: (130*Math.sin(135/180*Math.PI)+33) },
  202. { x: (130*Math.cos(90/180*Math.PI)-33), y: (130*Math.sin(90/180*Math.PI)+33) },
  203. { x: (130*Math.cos(45/180*Math.PI)-33), y: (130*Math.sin(45/180*Math.PI)+33) },
  204. { x: (130*Math.cos(0/180*Math.PI)-33), y: (130*Math.sin(0/180*Math.PI)+33) },
  205. ],
  206. colors: [
  207. { colorName: 'green', state: 'dj', stateName: '待机', color: 0x05bb4c, },
  208. { colorName: 'blue', state: 'yx', stateName: '运行', color: 0x4b55ae, },
  209. { colorName: 'pink', state: 'xd', stateName: '限电', color: 0xc531c7, },
  210. { colorName: 'red', state: 'gz', stateName: '故障', color: 0xBA3237, },
  211. { colorName: 'orange', state: 'jx', stateName: '检修', color: 0xe17e23, },
  212. { colorName: 'gray', state: 'lx', stateName: '离线', color: 0x606769, },
  213. { colorName: 'white', state: 'sl', stateName: '受累', color: 0xffffff, },
  214. ],
  215. fanName: "",
  216. playAnimation: true,
  217. mapDeg: 0,
  218. };
  219. },
  220. // 函数
  221. methods: {
  222. // Vector3 to screen
  223. vector3ToScreen: function(position) {
  224. const centerX = this.$el.scrollWidth / 2;
  225. const centerY = this.$el.scrollHeight / 2;
  226. const v3 = new THREE.Vector3(position.x, position.y, position.z);
  227. const standardVec = v3.project(camera);
  228. const screenX = Math.round(centerX * standardVec.x + centerX);
  229. const screenY = Math.round(-centerY * standardVec.y + centerY);
  230. return { x: screenX, y: screenY };
  231. },
  232. // 场景
  233. initScene: function() {
  234. scene = new THREE.Scene();
  235. // scene.background = new THREE.Color(0xa0a0a0);
  236. },
  237. // 相机
  238. initCamera: function() {
  239. camera = new THREE.PerspectiveCamera(45, this.$el.scrollWidth / this.$el.scrollHeight, 0.1, 10000);
  240. camera.position.set(50, 50, 50);
  241. },
  242. // 坐标轴
  243. initAxesHelper: function() {
  244. const axesHelper = new THREE.AxesHelper(150);
  245. scene.add(axesHelper);
  246. axesHelper.position.set(0, 0, 0);
  247. },
  248. // 渲染器
  249. initRender: function() {
  250. renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
  251. // renderer.setClearAlpha(0);
  252. renderer.setSize(this.$el.scrollWidth, this.$el.scrollHeight);
  253. this.$el.append(renderer.domElement);
  254. },
  255. // 灯光
  256. initLight: function() {
  257. // let light = new THREE.PointLight(0xffffff, 2);
  258. let light = new THREE.AmbientLight(0xffffff, 5);
  259. // light.position.set(50, 50, 50);
  260. scene.add(light);
  261. },
  262. // 控制器
  263. initControls: function() {
  264. controls = new OrbitControls(camera, renderer.domElement);
  265. controls.enablePan = false;
  266. controls.maxPolarAngle = 45 / 180 * Math.PI;
  267. controls.minPolarAngle = 45 / 180 * Math.PI;
  268. controls.maxAzimuthAngle = 45 / 180 * Math.PI;
  269. controls.minAzimuthAngle = 45 / 180 * Math.PI;
  270. controls.enableKeys = false;
  271. controls.enableZoom = false;
  272. controls.update();
  273. // controls.maxAzimuthAngle = 50 / 180 * Math.PI;
  274. // controls.minAzimuthAngle = 40 / 180 * Math.PI;
  275. // controls.addEventListener("change", () => {
  276. // let cDeg = controls.getAzimuthalAngle() / Math.PI * 180;
  277. // this.mapDeg = cDeg - 40 - 5;
  278. // this.setEveryHTML();
  279. // });
  280. },
  281. // 初始化一个风机动画
  282. initFanAnimate: function(obj) {
  283. let fanAnimateObj = {
  284. speed: 0.05,
  285. rotate: 0,
  286. fan: obj.getObjectByName(`${obj.name}_leaf`),
  287. fan1: obj.getObjectByName(`${obj.name}_leaf_1`),
  288. fan2: obj.getObjectByName(`${obj.name}_leaf_2`),
  289. fan3: obj.getObjectByName(`${obj.name}_leaf_3`),
  290. };
  291. let fanAnimateFunction = function() {
  292. // fanAnimateObj.fan.rotateOnAxis(new THREE.Vector3(0, 1, 0), fanAnimateObj.speed);
  293. fanAnimateObj.fan.rotateY(fanAnimateObj.speed);
  294. // fanAnimateObj.fan1.rotateZ(fanAnimateObj.speed);
  295. // fanAnimateObj.fan2.rotateZ(fanAnimateObj.speed);
  296. // fanAnimateObj.fan3.rotateZ(fanAnimateObj.speed);
  297. // fanAnimateObj.rotate += fanAnimateObj.speed;
  298. // if (fanAnimateObj.rotate >= 360) {
  299. // fanAnimateObj.rotate = 0;
  300. // }
  301. fanAnimateObj.animateId = window.requestAnimationFrame(fanAnimateFunction);
  302. };
  303. fanAnimateObj.stop = function() {
  304. window.cancelAnimationFrame(fanAnimateObj.animateId);
  305. };
  306. fanAnimateObj.start = function() {
  307. fanAnimateFunction();
  308. };
  309. fanAnimateFunction();
  310. fanAnimates.push(fanAnimateObj);
  311. return fanAnimateObj;
  312. },
  313. // 清空风机动画
  314. clearFanAnimate: function() {
  315. while (fanAnimates.length > 0) {
  316. let fanAnimateObj = fanAnimates.shift();
  317. fanAnimateObj.stop();
  318. }
  319. this.playAnimation = false;
  320. },
  321. // 开始风机动画
  322. startFanAnimate: function () {
  323. fans.forEach(fan => {
  324. this.initFanAnimate(fan);
  325. });
  326. this.playAnimation = true;
  327. },
  328. // 内容
  329. initContent: function() {
  330. // 加载3D地面
  331. let loaderGround = new GLTFLoader(); /*实例化加载器*/
  332. loaderGround.load(
  333. "static/3d/fanvar.gltf",
  334. (gltf) => {
  335. gltf.scene.position.set(17, 10, -12);
  336. // console.log(gltf);
  337. scene.add(gltf.scene);
  338. let rootNode = gltf.scene.children[0];
  339. let fan = gltf.scene.children[0].children[0];
  340. rootNode.remove(fan);
  341. this.setFanPosition(rootNode, fan);
  342. },
  343. (xhr) => {},
  344. function(error) {
  345. console.error("load error!" + error.getWebGLErrorMessage());
  346. }
  347. );
  348. loaderGround.load(
  349. "static/3d/light.gltf",
  350. (gltf) => {
  351. gltf.scene.position.set(17, 10, -12);
  352. // console.log(gltf);
  353. scene.add(gltf.scene);
  354. let rootNode = gltf.scene.children[0];
  355. let light = gltf.scene.children[0].children[0];
  356. rootNode.remove(light);
  357. this.setLightPosition(rootNode, light);
  358. },
  359. (xhr) => {},
  360. function(error) {
  361. console.error("load error!" + error.getWebGLErrorMessage());
  362. }
  363. );
  364. loaderGround.load(
  365. "static/3d/build.gltf",
  366. (gltf) => {
  367. gltf.scene.position.set(0, 0, -35); // 10, 0, -50 -6, 0.13, -50
  368. scene.add(gltf.scene);
  369. gltf.scene.rotateY(120);
  370. scene.onAfterRender = () => {
  371. this.$emit("when");
  372. this.$refs.pageLoading.hide();
  373. }
  374. },
  375. (xhr) => {},
  376. function(error) {
  377. console.error("load error!" + error.getWebGLErrorMessage());
  378. }
  379. );
  380. },
  381. // 改变元素底盘的颜色(带底盘的)
  382. changeObjectColor: function (obj, colorName) {
  383. for (let i = 1; i <= 4; i++) {
  384. let c = obj.getObjectByName(`${obj.name}_c_${i}`);
  385. c.material = new THREE.MeshBasicMaterial({
  386. color: 0xf0f0f0,
  387. opacity: 0.8,
  388. transparent: true,
  389. });
  390. }
  391. let leaf = obj.getObjectByName(`${obj.name}_leaf`);
  392. for (let chind of leaf.children) {
  393. chind.material = new THREE.MeshBasicMaterial({
  394. color: 0xf0f0f0,
  395. opacity: 0.8,
  396. transparent: true,
  397. });
  398. }
  399. let chassis = obj.getObjectByName(`${obj.name}_chassis`);
  400. // let color = this.colors.find(t => t.colorName == colorName || t.state == colorName || t.stateName == colorName).color;
  401. chassis.material = new THREE.MeshBasicMaterial({
  402. // color: color,
  403. color: 0xf0f0f0,
  404. opacity: 0.8,
  405. transparent: true,
  406. });
  407. },
  408. // 改变元素的颜色(光伏)
  409. changeElColor: function (obj, colorName) {
  410. // let color = this.colors.find(t => t.colorName == colorName || t.state == colorName || t.stateName == colorName).color;
  411. // for (let i = 1; i <= 6; i++) {
  412. // let el = obj.getObjectByName(`${obj.name}_item_${i}`);
  413. // el.material = new THREE.MeshBasicMaterial({
  414. // color: color,
  415. // opacity: 0.8,
  416. // transparent: true,
  417. // });
  418. // }
  419. },
  420. // 设置风机name
  421. setFanName: function (obj, name) {
  422. obj.name = name;
  423. obj.userData.name = name;
  424. // "Cylinder881" "Cone277"
  425. let Cylinder881 = obj.getObjectByName("Cylinder881");
  426. let Cylinder882 = obj.getObjectByName("Cylinder882");
  427. let Cylinder883 = obj.getObjectByName("Cylinder883");
  428. let Cone277 = obj.getObjectByName("Cone277");
  429. Cylinder881.name = `${name}_c_1`;
  430. Cylinder881.userData.name = `${name}_c_1`;
  431. Cylinder882.name = `${name}_c_2`;
  432. Cylinder882.userData.name = `${name}_c_2`;
  433. Cylinder883.name = `${name}_c_3`;
  434. Cylinder883.userData.name = `${name}_c_3`;
  435. Cone277.name = `${name}_c_4`;
  436. Cone277.userData.name = `${name}_c_4`;
  437. // "Box719")
  438. let fanBig1_1 = obj.getObjectByName("Box719");
  439. let fanBig1_2 = obj.getObjectByName("Box720");
  440. let fanBig1_3 = obj.getObjectByName("Box721");
  441. fanBig1_1.name = `${name}_leaf_1`;
  442. fanBig1_1.userData.name = `${name}_leaf_1`;
  443. fanBig1_2.name = `${name}_leaf_2`;
  444. fanBig1_2.userData.name = `${name}_leaf_2`;
  445. fanBig1_3.name = `${name}_leaf_3`;
  446. fanBig1_3.userData.name = `${name}_leaf_3`;
  447. obj.children[obj.children.length - 1].name = `${name}_chassis`;
  448. obj.children[obj.children.length - 1].userData.name = `${name}_chassis`;
  449. obj.remove(fanBig1_1, fanBig1_2, fanBig1_3);
  450. var group = new THREE.Group();
  451. // var group = new THREE.Mesh(new THREE.BoxGeometry(0.01, 0.01, 0.01),
  452. // new THREE.MeshBasicMaterial({
  453. // color: 0xff0000
  454. // })
  455. // );
  456. group.position.set(group.position.x + 0.0067, group.position.y, group.position.z + 0.01);
  457. fanBig1_1.position.set(fanBig1_1.position.x - 0.0067, fanBig1_1.position.y , fanBig1_1.position.z- 0.01);
  458. fanBig1_2.position.set(fanBig1_2.position.x - 0.0067, fanBig1_2.position.y, fanBig1_2.position.z- 0.01);
  459. fanBig1_3.position.set(fanBig1_3.position.x - 0.0067, fanBig1_3.position.y, fanBig1_3.position.z- 0.01);
  460. group.add(fanBig1_1, fanBig1_2, fanBig1_3);
  461. obj.add(group);
  462. group.name = `${name}_leaf`;
  463. },
  464. // 设置风机位置 颜色
  465. setFanPosition: function (rootNode, obj) {
  466. // 麻黄山
  467. let fan_mhs = obj.clone(true);
  468. fan_mhs.position.set(10, 5, -4);
  469. this.setFanName(fan_mhs, "fan_mhs");
  470. rootNode.add(fan_mhs);
  471. // this.initCylinderGeometry(fan_mhs);
  472. this.htmlLayer[0].position = fan_mhs.position;
  473. // 牛首山
  474. let fan_nss = obj.clone(true);
  475. fan_nss.position.set(-4, 4, -15);
  476. this.setFanName(fan_nss, "fan_nss");
  477. rootNode.add(fan_nss);
  478. this.initCylinderGeometry(fan_nss); // 故障 显示红色圆柱
  479. this.htmlLayer[1].position = fan_nss.position;
  480. // 青山
  481. let fan_qs = obj.clone(true);
  482. fan_qs.position.set(15, 5, -9);
  483. this.setFanName(fan_qs, "fan_qs");
  484. rootNode.add(fan_qs);
  485. // this.initCylinderGeometry(fan_qs);
  486. this.htmlLayer[2].position = fan_qs.position;
  487. // 石板泉
  488. let fan_sbq = obj.clone(true);
  489. fan_sbq.position.set(4, 5, -2);
  490. this.setFanName(fan_sbq, "fan_sbq");
  491. rootNode.add(fan_sbq);
  492. // this.initCylinderGeometry(fan_sbq);
  493. this.htmlLayer[3].position = fan_sbq.position;
  494. // 香山
  495. let fan_xs = obj.clone(true);
  496. fan_xs.position.set(-9, 7, 21);
  497. this.setFanName(fan_xs, "fan_xs");
  498. rootNode.add(fan_xs);
  499. // this.initCylinderGeometry(fan_xs);
  500. this.htmlLayer[4].position = fan_xs.position;
  501. // 改一下颜色
  502. this.changeObjectColor(fan_mhs, 'green'); // 麻黄山
  503. this.changeObjectColor(fan_nss, 'xd'); // 牛首山
  504. this.changeObjectColor(fan_qs, '检修'); // 青山
  505. this.changeObjectColor(fan_sbq, 'white'); // 石板泉
  506. this.changeObjectColor(fan_xs, '运行'); // 香山
  507. // 风机存入数组
  508. fans.push(fan_mhs);
  509. fans.push(fan_nss);
  510. fans.push(fan_qs);
  511. fans.push(fan_sbq);
  512. fans.push(fan_xs);
  513. // 开始风机动画
  514. this.startFanAnimate();
  515. // 设置位置
  516. this.setEveryHTML();
  517. },
  518. // 设置光伏name
  519. setLightName: function (obj, name) {
  520. obj.name = name;
  521. obj.userData.name = name;
  522. let Box661 = obj.getObjectByName("Box661");
  523. let Box668 = obj.getObjectByName("Box668");
  524. let Box669 = obj.getObjectByName("Box669");
  525. let Box664 = obj.getObjectByName("Box664");
  526. let Box667 = obj.getObjectByName("Box667");
  527. let Box670 = obj.getObjectByName("Box670");
  528. Box661.name = `${name}_item_1`;
  529. Box661.userData.name = `${name}_item_1`;
  530. Box668.name = `${name}_item_2`;
  531. Box668.userData.name = `${name}_item_2`;
  532. Box669.name = `${name}_item_3`;
  533. Box669.userData.name = `${name}_item_3`;
  534. Box664.name = `${name}_item_4`;
  535. Box664.userData.name = `${name}_item_4`;
  536. Box667.name = `${name}_item_5`;
  537. Box667.userData.name = `${name}_item_5`;
  538. Box670.name = `${name}_item_6`;
  539. Box670.userData.name = `${name}_item_6`;
  540. },
  541. // 设置光伏位置 颜色
  542. setLightPosition: function (rootNode, obj) {
  543. obj.scale.set(200, 200, 200);
  544. // let Box660 = obj.getObjectByName("Box660");
  545. // let Box666 = obj.getObjectByName("Box666");
  546. // let Box662 = obj.getObjectByName("Box662");
  547. // let Box663 = obj.getObjectByName("Box663");
  548. // let Box659 = obj.getObjectByName("Box659");
  549. // let Box665 = obj.getObjectByName("Box665");
  550. // obj.remove(Box660);
  551. // obj.remove(Box666);
  552. // obj.remove(Box662);
  553. // obj.remove(Box663);
  554. // obj.remove(Box659);
  555. // obj.remove(Box665);
  556. // 大武口
  557. let light_dwk = obj.clone(true);
  558. light_dwk.position.set(-22, 0, -28); // -20 +30 // -2 0 -58 // -6, 0.13, -50 10, 0, -50
  559. // this.setLightName(light_dwk, "light_dwk");
  560. rootNode.add(light_dwk);
  561. this.htmlLayer[5].position = light_dwk.position;
  562. // 平罗
  563. let light_pl = obj.clone(true);
  564. light_pl.position.set(-26, 0.13, -25);
  565. // this.setLightName(light_pl, "light_pl");
  566. rootNode.add(light_pl);
  567. this.htmlLayer[6].position = light_pl.position;
  568. // 马场湖
  569. let light_mch = obj.clone(true);
  570. light_mch.position.set(-47, 2, 0);
  571. // this.setLightName(light_mch, "light_mch");
  572. rootNode.add(light_mch);
  573. this.htmlLayer[7].position = light_mch.position;
  574. // 宣和
  575. let light_xh = obj.clone(true);
  576. light_xh.position.set(-38, 4, 3);
  577. // this.setLightName(light_xh, "light_xh");
  578. rootNode.add(light_xh);
  579. this.htmlLayer[8].position = light_xh.position;
  580. // 海子井
  581. let light_hzj = obj.clone(true);
  582. light_hzj.position.set(-30, 2, 4);
  583. // this.setLightName(light_hzj, "light_hzj");
  584. rootNode.add(light_hzj);
  585. this.htmlLayer[9].position = light_hzj.position;
  586. // 改变颜色
  587. this.changeElColor(light_dwk, 'green'); // 大武口
  588. this.changeElColor(light_pl, '离线'); // 平罗
  589. this.changeElColor(light_mch, 'yx'); // 马场湖
  590. this.changeElColor(light_xh, 'red'); // 宣和
  591. this.changeElColor(light_hzj, '受累'); // 海子井
  592. // 设置位置
  593. this.setEveryHTML();
  594. },
  595. // 创建一个圆柱
  596. initCylinderGeometry: function(obj, mode="fan") {
  597. let cr = 1.7;
  598. let xyz = [5.75, 6.85, -0.63];
  599. switch (mode) {
  600. case "fan":
  601. cr = 1.7;
  602. xyz = [5.75, 6.85, -0.63];
  603. break;
  604. case "light":
  605. cr = 3;
  606. xyz = [5.75, 6.85, -0.63];
  607. break;
  608. default:
  609. break;
  610. }
  611. let geometry = new THREE.CylinderGeometry(cr, cr, 4, 128, 1, true);
  612. //加载纹理
  613. let texture = new THREE.TextureLoader().load("static/3d/beam-texture-red.png");
  614. texture.wrapS = texture.wrapT = THREE.RepeatWrapping; //每个都重复
  615. texture.repeat.set(1, 1);
  616. texture.needsUpdate = true;
  617. let materials = [
  618. //圆柱侧面材质,使用纹理贴图
  619. new THREE.MeshBasicMaterial({
  620. map: texture,
  621. side: THREE.FrontSide,
  622. transparent: true,
  623. }),
  624. //圆柱顶材质
  625. new THREE.MeshBasicMaterial({
  626. transparent: true,
  627. opacity: 0,
  628. side: THREE.DoubleSide,
  629. // side: THREE.DoubleSide,THREE.FrontSide THREE.BackSide
  630. }),
  631. //圆柱底材质
  632. new THREE.MeshBasicMaterial({
  633. transparent: true,
  634. opacity: 0,
  635. side: THREE.DoubleSide,
  636. }),
  637. ];
  638. let cylinder = new THREE.Mesh(geometry, materials);
  639. cylinder.position.set(obj.position.x + xyz[0], obj.position.y + xyz[1], obj.position.z + xyz[2]);
  640. scene.add(cylinder);
  641. },
  642. // 设置每一个html的位置
  643. setEveryHTML: function() {
  644. this.htmlLayer.forEach(value => {
  645. if (value.show && value.position) {
  646. const screen = this.vector3ToScreen(value.position);
  647. value.x = screen.x + value.ox;
  648. value.y = screen.y + value.oy;
  649. }
  650. });
  651. },
  652. // 判断时候是可点击对象 返回null 或object
  653. getClickObject: function(intersects) {
  654. const names = ["fan"];
  655. const namesRoot = ["fanvar"];
  656. let x = true;
  657. let obj = null;
  658. for (let intersect of intersects) {
  659. let temObj = intersect.object;
  660. while (x) {
  661. for (let name of names) {
  662. if (temObj.name.indexOf(`${name}_`) == 0) {
  663. obj = temObj;
  664. x = false;
  665. break;
  666. }
  667. }
  668. if (x && temObj.parent) {
  669. temObj = temObj.parent;
  670. continue;
  671. }
  672. break;
  673. }
  674. }
  675. if (obj) {
  676. while (obj.parent && !namesRoot.includes(obj.parent.name)) {
  677. obj = obj.parent;
  678. }
  679. }
  680. if (obj && !namesRoot.includes(obj.parent.name)) {
  681. obj = null;
  682. }
  683. return obj;
  684. },
  685. // 显示风机弹出层
  686. showFanInfo: function(position) {
  687. this.fanInfoLayer.position = {
  688. x: position.x,
  689. y: position.y,
  690. z: position.z,
  691. };
  692. const screen = this.vector3ToScreen(this.fanInfoLayer.position);
  693. this.fanInfoLayer.x = screen.x + this.fanInfoLayer.ox;
  694. this.fanInfoLayer.y = screen.y + this.fanInfoLayer.oy;
  695. this.fanInfoLayer.show = true;
  696. },
  697. // 隐藏风机弹出层
  698. hideFanInfo: function () {
  699. this.fanInfoLayer.show = false;
  700. },
  701. // 点击事件
  702. clickEvent: function(event) {
  703. event.preventDefault();
  704. let vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);
  705. vector = vector.unproject(camera);
  706. let raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
  707. let intersects = raycaster.intersectObjects(scene.children, true);
  708. // console.log(intersects); // 输出点击的对象信息
  709. // 找是否是可点击对象
  710. let obj = this.getClickObject(intersects);
  711. if (obj) {
  712. this.elClick(obj.name);
  713. this.showFanInfo(obj.position);
  714. }
  715. },
  716. // 元素点击事件 绑值
  717. elClick: function (name) {
  718. switch (name) {
  719. case "fan_mhs": // 麻黄山
  720. console.log("麻黄山");
  721. break;
  722. case "fan_nss": // 牛首山
  723. console.log("牛首山");
  724. break;
  725. case "fan_qs": // 青山
  726. console.log("青山");
  727. break;
  728. case "fan_sbq": // 石板泉
  729. console.log("石板泉");
  730. break;
  731. case "fan_xs": // 香山
  732. console.log("香山");
  733. break;
  734. default:
  735. break;
  736. }
  737. },
  738. // 初始化云
  739. initCloud: function () {
  740. },
  741. // 初始化
  742. initThree: function() {
  743. this.initScene();
  744. // this.initAxesHelper();
  745. this.initCamera();
  746. this.initRender();
  747. this.initLight();
  748. this.initControls();
  749. this.initContent();
  750. this.initCloud();
  751. renderer.setAnimationLoop(this.animate);
  752. },
  753. // 动画
  754. animate: function() {
  755. renderer.render(scene, camera);
  756. },
  757. },
  758. // 生命周期钩子
  759. beforeCreate() {
  760. // 创建前
  761. },
  762. created() {
  763. // 创建后
  764. },
  765. beforeMount() {
  766. // 渲染前
  767. },
  768. mounted() {
  769. // 渲染后
  770. this.initThree();
  771. },
  772. beforeUpdate() {
  773. // 数据更新前
  774. },
  775. updated() {
  776. // 数据更新后
  777. },
  778. beforeUnmount() {
  779. // 销毁前
  780. renderer.setAnimationLoop(null);
  781. camera = null;
  782. scene = null;
  783. renderer = null;
  784. },
  785. };
  786. </script>
  787. <style lang="less" scoped>
  788. .three-model {
  789. position: relative;
  790. overflow: hidden;
  791. // left: -112px;
  792. .map-3d {
  793. position: absolute;
  794. width: 1118px;
  795. height: 678px;
  796. left: calc(50% - 559px);
  797. top: calc(50% - 339px);
  798. z-index: -1;
  799. map {
  800. width: 100%;
  801. height: 100%;
  802. }
  803. }
  804. .three-html-layer {
  805. .three-html-dom {
  806. position: absolute;
  807. }
  808. .fan-name {
  809. display: inline-block;
  810. height: 22px;
  811. background: #222632;
  812. border: 1px solid #646464;
  813. border-radius: 10px;
  814. padding: 0 10px;
  815. font-size: 12px;
  816. font-weight: 400;
  817. color: #FFFFFF;
  818. line-height: 22px;
  819. }
  820. .build-info {
  821. width: 166px;
  822. height: 33px;
  823. // position: relative;
  824. .build-info-close {
  825. position: absolute;
  826. width: 40px;
  827. height: 40px;
  828. top: -20px;
  829. left: -20px;
  830. border-radius: 50%;
  831. background: #1a1f2fd8;
  832. border: 1px solid #05bb4c;
  833. box-shadow: 0px 8px 17px 1px #05bb4c66;
  834. cursor: pointer;
  835. color: @gray-l;
  836. transition: all 0.3s;
  837. display: flex;
  838. align-items: center;
  839. justify-content: center;
  840. font-size: 14px;
  841. &:hover {
  842. color: #05bb4c;
  843. }
  844. }
  845. .build-info-item {
  846. width: 66px;
  847. height: 66px;
  848. border-radius: 50%;
  849. background: #1A1F2FE5;
  850. position: absolute;
  851. &::after {
  852. content: "";
  853. position: absolute;
  854. width: 44px;
  855. height: 2px;
  856. left: calc(50% - 22px);
  857. top: calc(50% - 1px);
  858. }
  859. .build-info-item-num {
  860. font-size: 22px;
  861. font-weight: 500;
  862. color: #FFFFFF;
  863. text-align: center;
  864. line-height: 1.2;
  865. margin-top: 6px;
  866. margin-bottom: 3px;
  867. }
  868. .build-info-item-text {
  869. font-size: 12px;
  870. color: #FFFFFF;
  871. font-weight: 400;
  872. text-align: center;
  873. }
  874. &.blue {
  875. border: 2px solid #1DA0D7;
  876. &::after {
  877. background: #1DA0D7;
  878. }
  879. }
  880. &.red {
  881. border: 2px solid #FF0000;
  882. &::after {
  883. background: #FF0000;
  884. }
  885. }
  886. &.green {
  887. border: 2px solid #05BB4C;
  888. &::after {
  889. background: #05BB4C;
  890. }
  891. }
  892. &.purple {
  893. border: 2px solid #323E6F;
  894. &::after {
  895. background: #323E6F;
  896. }
  897. }
  898. &.orange {
  899. border: 2px solid #DB5520;
  900. &::after {
  901. background: #DB5520;
  902. }
  903. }
  904. &.yellow {
  905. border: 2px solid #EDB32F;
  906. &::after {
  907. background: #EDB32F;
  908. }
  909. }
  910. }
  911. }
  912. }
  913. }
  914. </style>