light.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // 引入依赖
  2. import { watch, ref, reactive, toRefs, onBeforeUnmount, onMounted } from "vue";
  3. import { storeState } from '../../../js/store/store.js' //简单局部状态管理
  4. import tool from '../../../js/tool/tool.js' //提示等工具
  5. import resource from '../../../js/local/lang.js' //语言资源
  6. import { initHandler, handlerDrawing, clearHandlerDrawing } from "../../../js/common/drawHandler.js" //公共handler.js
  7. import createTooltip from '../../../js/tool/tooltip2.js'
  8. import config from "./config.js";
  9. function addPointSymbol(props) {
  10. // 设置默认值数据
  11. let state = reactive({
  12. lightSelectId: 0,
  13. lightColor: "#FFFFFF",
  14. cutoffDistance: 200,
  15. lightDecay: 5,
  16. lightIntensity: 5,
  17. spotLightAngle: 30,
  18. visibleModel: true,
  19. dockFontShow: true, //停靠图标显示
  20. modelPosition: [0, 0, 0],
  21. modelLongitude: '',
  22. modelLatitude: '',
  23. modelHeight: '',
  24. bubbleShow: false,
  25. visiblePositions:false,
  26. })
  27. // 传入props改变默认值
  28. if (props) {
  29. for (let key in props) {
  30. if (state.hasOwnProperty(key)) {
  31. state[key] = props[key]
  32. } else {
  33. tool.Message.errorMsg(resource.AttributeError + key);
  34. }
  35. }
  36. }
  37. // 初始化数据
  38. let currentSelectedEntity, selectdeLightSource, s3mInstanceColc;
  39. let isAddPointLight = false;
  40. let entityLightPairs = new Map(); // Entity和点光源对象的键值对
  41. let modelUrl = '/data/s3m/light.s3m';
  42. let modelEditor;
  43. const bubble = ref(null);
  44. if (storeState.isViewer) {
  45. if (!window.tooltip) {
  46. window.tooltip = createTooltip(viewer._element);
  47. }
  48. s3mInstanceColc = new Cesium.S3MInstanceCollection(scene._context);
  49. viewer.scene.primitives.add(s3mInstanceColc);
  50. }
  51. //viewer 初始化完成的监听
  52. watch(() => storeState.isViewer, val => {
  53. if (val) {
  54. if (!window.tooltip) {
  55. window.tooltip = createTooltip(viewer._element);
  56. }
  57. s3mInstanceColc = new Cesium.S3MInstanceCollection(scene._context);
  58. viewer.scene.primitives.add(s3mInstanceColc);
  59. }
  60. });
  61. function addModel(position, pointLight) {
  62. let id = "light-model-" + new Date().getTime();
  63. s3mInstanceColc.add(modelUrl, {
  64. id: id,
  65. position: position,
  66. scale: new Cesium.Cartesian3(2, 2, 2),
  67. });
  68. currentSelectedEntity = s3mInstanceColc.getInstance(modelUrl, id);
  69. entityLightPairs.set(id, pointLight);
  70. }
  71. function getTooltipText() {
  72. if (state.lightSelectId === 0) {
  73. return '<p>点击确认点光源位置</p><p>选中光源模型可编辑与删除</p>'
  74. }
  75. return '<p>点击第一点确认光源位置</p><p>第二点确认光源方向</p><p>选中光源模型可编辑与删除</p>'
  76. }
  77. let isModelMoving = false;
  78. function addModelEditor(model) {
  79. modelEditor = new Cesium.ModelEditor({
  80. model: model,
  81. scene: viewer.scene,
  82. axesShow: {
  83. "translation": true,
  84. "rotation": false,
  85. "scale": true
  86. }
  87. });
  88. modelEditor.activate();
  89. modelEditor.changedEvt.addEventListener((param) => {
  90. let Cartesian3 = new Cesium.Cartesian3();
  91. Cesium.Matrix4.getTranslation(param.modelMatrix, Cartesian3);
  92. if (Cartesian3) {
  93. selectdeLightSource.position = Cartesian3;
  94. isModelMoving = true;
  95. changeSlider(() => isModelMoving = false)
  96. let position= tool.CartesiantoDegrees(Cartesian3);
  97. state.modelPosition.length = 0;
  98. state.modelPosition.push(...[position[0].toFixed(6),position[1].toFixed(6),position[2].toFixed(2)])
  99. }
  100. })
  101. }
  102. function drawPolyline() {
  103. if (!window.handlerPolyline) {
  104. initHandler("Polyline");
  105. }
  106. handlerDrawing("Polyline").then(
  107. res => {
  108. if (state.lightSelectId === 1) {
  109. addSpotLight(res.result.object.positions)
  110. } else {
  111. addDirectionalLight(res.result.object.positions)
  112. }
  113. let handlerPolyline = window.handlerPolyline;
  114. handlerPolyline.polyline.show = false;
  115. window.polylineTransparent.show = false; //半透线隐藏
  116. handlerPolyline.deactivate();
  117. tooltip.setVisible(false);
  118. },
  119. (err) => {
  120. console.log(err);
  121. }
  122. );
  123. window.handlerPolyline.activate();
  124. }
  125. function addLight() {
  126. let test = getTooltipText()
  127. window.tooltip.showAt(test, '400px');
  128. viewer.eventManager.addEventListener("CLICK", click_light, true);
  129. if (state.lightSelectId === 0) {
  130. viewer.enableCursorStyle = false;
  131. viewer._element.style.cursor = "";
  132. document.body.classList.add("measureCur");
  133. isAddPointLight = true;
  134. return;
  135. }
  136. drawPolyline();
  137. }
  138. function click_light(e) {
  139. if (isAddPointLight) {
  140. isAddPointLight = false;
  141. viewer.enableCursorStyle = true;
  142. document.body.classList.remove("measureCur");
  143. let targetPosition = scene.pickPosition(e.message.position);
  144. addPointLight(targetPosition);
  145. tooltip.setVisible(false);
  146. } else {
  147. let symbol = viewer.scene.pick(e.message.position) || viewer.selectedEntity;
  148. if (symbol && symbol.id && typeof (symbol.id) === 'string' && symbol.id.indexOf("light-model-") != -1) {
  149. if (currentSelectedEntity && currentSelectedEntity.id === symbol.id.id) return;
  150. currentSelectedEntity = symbol;
  151. selectdeLightSource = entityLightPairs.get(symbol.id);
  152. if (!modelEditor) addModelEditor(symbol.primitive)
  153. else modelEditor.setEditObject(symbol.primitive);
  154. //
  155. bubble.value.style.top = (e.message.position.y - 160) + 'px';
  156. bubble.value.style.left = (e.message.position.x) + 'px';
  157. console.log(modelEditor)
  158. let position = tool.CartesiantoDegrees(modelEditor._position);
  159. state.modelPosition.length = 0;
  160. state.modelPosition.push(...[position[0].toFixed(6),position[1].toFixed(6),position[2].toFixed(2)]);
  161. state.bubbleShow = true;
  162. return;
  163. }
  164. currentSelectedEntity = undefined;
  165. selectdeLightSource = undefined;
  166. if (modelEditor) modelEditor.deactivate();
  167. state.bubbleShow = false
  168. }
  169. }
  170. function addPointLight(position0) {
  171. let position = addModleHeight(position0);
  172. let options = {
  173. color: Cesium.Color.fromCssColorString(state.lightColor),
  174. cutoffDistance: Number(state.cutoffDistance),
  175. decay: Number(state.lightDecay),
  176. intensity: Number(state.lightIntensity),
  177. };
  178. let pointLight = new Cesium.PointLight(position, options);
  179. selectdeLightSource = pointLight;
  180. viewer.scene.addLightSource(pointLight);
  181. addModel(position, pointLight)
  182. }
  183. function addSpotLight(positions) {
  184. let position1 = addModleHeight(positions[0]);
  185. let position2 = positions[1];
  186. let options = {
  187. color: Cesium.Color.fromCssColorString(state.lightColor),
  188. distance: Number(state.cutoffDistance),
  189. decay: Number(state.lightDecay),
  190. intensity: Number(state.lightIntensity),
  191. angle: Cesium.Math.toRadians(Number(state.spotLightAngle))
  192. };
  193. let spotLight = new Cesium.SpotLight(
  194. position1,
  195. position2,
  196. options
  197. );
  198. selectdeLightSource = spotLight;
  199. viewer.scene.addLightSource(spotLight);
  200. addModel(position1, spotLight)
  201. }
  202. function addDirectionalLight(positions) {
  203. let position1 = addModleHeight(positions[0]);
  204. let position2 = positions[1];
  205. let options = {
  206. targetPosition: position2,
  207. color: Cesium.Color.fromCssColorString(state.lightColor),
  208. intensity: Number(state.lightIntensity),
  209. };
  210. let directionalLight = new Cesium.DirectionalLight(
  211. position1,
  212. options
  213. );
  214. selectdeLightSource = directionalLight;
  215. viewer.scene.addLightSource(directionalLight);
  216. addModel(position1, directionalLight)
  217. }
  218. function addModleHeight(Cartesian3) {
  219. let Cartographic = Cesium.Cartographic.fromCartesian(Cartesian3);
  220. Cartographic.height += 0.5;
  221. return Cesium.Cartographic.toCartesian(Cartographic)
  222. }
  223. function clearLight() {
  224. if (currentSelectedEntity) {
  225. entityLightPairs.delete(currentSelectedEntity.id);
  226. s3mInstanceColc.removeInstance(modelUrl, currentSelectedEntity.id);
  227. currentSelectedEntity = null;
  228. }
  229. if (selectdeLightSource) viewer.scene.removeLightSource(selectdeLightSource);
  230. if (entityLightPairs.size === 0) viewer.eventManager.removeEventListener("CLICK", click_light);
  231. if (modelEditor) modelEditor.deactivate();
  232. isAddPointLight = false;
  233. selectdeLightSource = null;
  234. clearHandlerDrawing();
  235. viewer.enableCursorStyle = true;
  236. document.body.classList.remove("measureCur");
  237. tooltip.setVisible(false);
  238. closeBubble();
  239. }
  240. //关闭气泡
  241. function closeBubble() {
  242. state.bubbleShow = false
  243. }
  244. //悬停气泡
  245. function dockBubble(val) {
  246. if (val) {
  247. bubble.value.classList.add('bubbleDock');
  248. state.dockFontShow = false
  249. } else {
  250. bubble.value.classList.remove('bubbleDock');
  251. state.dockFontShow = true
  252. }
  253. }
  254. watch(() => state.lightColor, val => {
  255. if (!selectdeLightSource) return;
  256. let color = Cesium.Color.fromCssColorString(val);
  257. selectdeLightSource.color = color;
  258. })
  259. watch(() => state.cutoffDistance, val => {
  260. if (!selectdeLightSource) return;
  261. if (selectdeLightSource.lightType === 1) {
  262. selectdeLightSource.cutoffDistance = Number(val);
  263. return;
  264. }
  265. if (selectdeLightSource.lightType === 2) {
  266. selectdeLightSource.distance = Number(val);
  267. return;
  268. }
  269. })
  270. watch(() => state.lightDecay, val => {
  271. if (!selectdeLightSource) return;
  272. if (selectdeLightSource.lightType !== 0) {
  273. selectdeLightSource.decay = Number(val);
  274. }
  275. })
  276. watch(() => state.lightIntensity, val => {
  277. if (!selectdeLightSource) return;
  278. selectdeLightSource.intensity = Number(val);
  279. })
  280. watch(() => state.spotLightAngle, val => {
  281. if (!selectdeLightSource) return;
  282. if (selectdeLightSource.lightType === 2) {
  283. selectdeLightSource.angle = Cesium.Math.toRadians(Number(val));
  284. }
  285. })
  286. watch(() => state.visibleModel, val => {
  287. if (!currentSelectedEntity) return;
  288. if (modelEditor) modelEditor.deactivate();
  289. s3mInstanceColc.visible = val
  290. })
  291. watch(() => state.modelPosition, val => {
  292. if (!currentSelectedEntity || isModelMoving) return;
  293. changeSlider(() => {
  294. let lon = Number(val[0]);
  295. let lat = Number(val[1]);
  296. let hei = Number(val[2]);
  297. let position = Cesium.Cartesian3.fromDegrees(lon, lat, hei);
  298. selectdeLightSource.position = position;
  299. currentSelectedEntity.primitive.updatePosition(position);
  300. if (modelEditor) modelEditor.setEditObject(currentSelectedEntity.primitive);
  301. })
  302. },
  303. {
  304. deep: true
  305. })
  306. let timer;
  307. // 防止滑块快速滑动的多次执行
  308. function changeSlider(callback) {
  309. if (timer) clearTimeout(timer);
  310. timer = setTimeout(function () {
  311. callback()
  312. }, 1000);
  313. }
  314. // 销毁
  315. onBeforeUnmount(() => {
  316. // if (viewer) viewer.eventManager.removeEventListener("CLICK", click_light);
  317. });
  318. return {
  319. ...toRefs(state),
  320. config,
  321. addLight,
  322. clearLight,
  323. bubble,
  324. closeBubble,
  325. dockBubble
  326. };
  327. };
  328. export default addPointSymbol