clip-cross.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // 引入依赖
  2. import { watch, reactive, toRefs, onBeforeUnmount } from "vue";
  3. import tool from '../../../js/tool/tool.js' //提示等工具
  4. import resource from '../../../js/local/lang.js' //语言资源
  5. import { storeState, storeDate } from '../../../js/store/store.js' //简单局部状态管理
  6. import { setAllModelClipBox, clearAllModelClip, isClippingPlaneSupported } from '../../../js/common/clip-model.js'
  7. function clipCrossAnalysis(props) {
  8. // 设置默认值数据
  9. let state = reactive({
  10. clipWidth: 5,
  11. clipHeight: 5,
  12. heading: 0,
  13. pitch: 0,
  14. roll: 0,
  15. extrude: 1,
  16. });
  17. // 传入props改变默认值
  18. if (props) {
  19. for (let key in props) {
  20. if (state.hasOwnProperty(key)) {
  21. state[key] = props[key]
  22. } else {
  23. tool.Message.errorMsg(resource.AttributeError + key);
  24. }
  25. }
  26. }
  27. // 初始化数据
  28. let layers;
  29. let screenSpaceEventHandler;
  30. let startClip, //裁剪标志
  31. box, boxPosition, dim, //entity
  32. position //裁剪区域
  33. if (storeState.isViewer) {
  34. screenSpaceEventHandler = new Cesium.ScreenSpaceEventHandler(
  35. viewer.scene.canvas
  36. );
  37. layers = viewer.scene.layers.layerQueue;
  38. }
  39. //viewer 初始化完成的监听
  40. watch(() => storeState.isViewer, val => {
  41. if (val) {
  42. screenSpaceEventHandler = new Cesium.ScreenSpaceEventHandler(
  43. viewer.scene.canvas
  44. );
  45. layers = viewer.scene.layers.layerQueue;
  46. }
  47. })
  48. /*
  49. ***cross分析模块***
  50. */
  51. // 分析
  52. function startCross(e) {
  53. e.preventDefault();
  54. if (!viewer) {
  55. return;
  56. }
  57. if (box) {
  58. clearCross()
  59. }
  60. start();
  61. startClip = true;
  62. box.show = true;
  63. };
  64. function start() {
  65. for (let layer of layers) {
  66. layer.selectEnabled = false;
  67. }
  68. // 添加盒子
  69. boxPosition = Cesium.Cartesian3.fromDegrees(0, 0, 0);
  70. dim = new Cesium.Cartesian3(
  71. state.clipWidth,
  72. state.clipHeight,
  73. 0.1
  74. );
  75. box = viewer.entities.add({
  76. // 标识盒
  77. id: "cross-clip-identify-box",
  78. position: boxPosition,
  79. show: false,
  80. box: {
  81. dimensions: dim,
  82. fill: false,
  83. outline: true,
  84. outlineColor: Cesium.Color.AQUA,
  85. outlineWidth: 5.0
  86. }
  87. });
  88. let hpr;
  89. screenSpaceEventHandler.setInputAction(movement => {
  90. if (startClip) {
  91. boxPosition = scene.pickPosition(movement.endPosition);
  92. if (!boxPosition) {
  93. return;
  94. }
  95. box.position = boxPosition;
  96. if (!hpr) {
  97. hpr = new Cesium.HeadingPitchRoll(
  98. Cesium.Math.toRadians(state.heading),
  99. Cesium.Math.toRadians(state.pitch),
  100. Cesium.Math.toRadians(state.roll)
  101. )
  102. }
  103. let orientation = Cesium.Transforms.headingPitchRollQuaternion(
  104. boxPosition,
  105. hpr
  106. );
  107. box.orientation = orientation;
  108. }
  109. }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  110. screenSpaceEventHandler.setInputAction(evt => {
  111. if (startClip) {
  112. position = scene.pickPosition(evt.position);
  113. if (!position) {
  114. return;
  115. }
  116. updateClip();
  117. startClip = false;
  118. box.show = false;
  119. }
  120. screenSpaceEventHandler.removeInputAction(
  121. Cesium.ScreenSpaceEventType.MOUSE_MOVE
  122. );
  123. screenSpaceEventHandler.removeInputAction(
  124. Cesium.ScreenSpaceEventType.LEFT_CLICK
  125. );
  126. hpr = null;
  127. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  128. };
  129. // 更新
  130. function updateClip() {
  131. for (let layer of layers) {
  132. layer.setCustomClipCross({
  133. position: position,
  134. dimensions: dim,
  135. heading: state.heading,
  136. pitch: state.pitch,
  137. roll: state.roll,
  138. extrudeDistance: Number(state.extrude)
  139. });
  140. }
  141. // 同时裁剪场景中的 glTF Model(Entity 加载的 .glb)
  142. // cross 裁剪本质是两个交叉平面+一个 extrude 距离形成的十字体
  143. // cross 默认效果是裁剪十字体内部(保留外部),对应 Box cutInside=true
  144. if (isClippingPlaneSupported() && position && dim) {
  145. setAllModelClipBox(viewer, {
  146. position: position,
  147. dimensions: new Cesium.Cartesian3(dim.x, dim.y, Number(state.extrude) || 0.1),
  148. heading: Cesium.Math.toRadians(state.heading)
  149. }, {
  150. cutInside: true // cross 默认裁剪十字体内部
  151. })
  152. }
  153. };
  154. // 清除
  155. function clearCross() {
  156. box && viewer.entities.removeById("cross-clip-identify-box");
  157. for (let layer of layers) {
  158. layer.clearCustomClipBox();
  159. }
  160. // 同时清除 glTF Model 的裁剪
  161. clearAllModelClip(viewer)
  162. startClip = false;
  163. box = undefined;
  164. screenSpaceEventHandler.removeInputAction(
  165. Cesium.ScreenSpaceEventType.MOUSE_MOVE
  166. );
  167. screenSpaceEventHandler.removeInputAction(
  168. Cesium.ScreenSpaceEventType.LEFT_CLICK
  169. );
  170. };
  171. // 监听
  172. watch(() => state.clipWidth, val => {
  173. let temp_width = Number(val);
  174. if (temp_width <= 0 || !box) {
  175. return;
  176. }
  177. box.box.dimensions = new Cesium.Cartesian3(
  178. state.clipWidth,
  179. state.clipHeight,
  180. 0.1
  181. );
  182. dim = new Cesium.Cartesian3(temp_width, state.clipHeight, state.extrude);
  183. updateClip();
  184. });
  185. watch(() => state.clipHeight, val => {
  186. let temp_height = Number(val);
  187. if (temp_height <= 0 || !box) {
  188. return;
  189. }
  190. box.box.dimensions = new Cesium.Cartesian3(
  191. state.clipWidth,
  192. state.clipHeight,
  193. 0.1
  194. );
  195. dim = new Cesium.Cartesian3(state.clipWidth, temp_height, state.extrude);
  196. updateClip();
  197. });
  198. watch(() => state.pitch, val => {
  199. if (val === "" || !box) {
  200. return;
  201. }
  202. let pitch = Number(val);
  203. let hpr = new Cesium.HeadingPitchRoll(
  204. Cesium.Math.toRadians(state.heading),
  205. Cesium.Math.toRadians(pitch),
  206. Cesium.Math.toRadians(state.roll)
  207. );
  208. let orientation = Cesium.Transforms.headingPitchRollQuaternion(
  209. boxPosition,
  210. hpr
  211. );
  212. box.orientation = orientation;
  213. updateClip();
  214. });
  215. watch(() => state.roll, val => {
  216. if (val === "" || !box) {
  217. return;
  218. }
  219. let roll = Number(val);
  220. let hpr = new Cesium.HeadingPitchRoll(
  221. Cesium.Math.toRadians(state.heading),
  222. Cesium.Math.toRadians(state.pitch),
  223. Cesium.Math.toRadians(roll)
  224. );
  225. let orientation = Cesium.Transforms.headingPitchRollQuaternion(
  226. boxPosition,
  227. hpr
  228. );
  229. box.orientation = orientation;
  230. updateClip();
  231. });
  232. watch(() => state.heading, val => {
  233. if (val === "" || !box) {
  234. return;
  235. }
  236. let heading = Number(val);
  237. let hpr = new Cesium.HeadingPitchRoll(
  238. Cesium.Math.toRadians(heading),
  239. Cesium.Math.toRadians(state.pitch),
  240. Cesium.Math.toRadians(state.roll)
  241. );
  242. let orientation = Cesium.Transforms.headingPitchRollQuaternion(
  243. boxPosition,
  244. hpr
  245. );
  246. box.orientation = orientation;
  247. updateClip();
  248. });
  249. watch(() => state.extrude, val => {
  250. let temp_extrudeDistance = Number(val);
  251. if (temp_extrudeDistance <= 0 || !box) {
  252. return;
  253. }
  254. updateClip();
  255. });
  256. // 销毁
  257. onBeforeUnmount(() => {
  258. screenSpaceEventHandler.destroy();
  259. layers = undefined;
  260. box = undefined;
  261. screenSpaceEventHandler = undefined;
  262. dim = undefined;
  263. })
  264. return {
  265. ...toRefs(state),
  266. startCross,
  267. clearCross
  268. };
  269. };
  270. export default clipCrossAnalysis