DrawClipping.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. function ClippingPlanes(arg) {
  2. this.viewer = arg.viewer;
  3. this.callback = arg.callback;
  4. this.activeShape = null;
  5. this.color = arg.color || "#f00";
  6. this.width = arg.width || 8;
  7. this.image = arg.image;
  8. this.endposition = []; //活动点
  9. this.floatingPoint = null; //标识点
  10. this.ischange = arg.change || 1;
  11. this.RedpylineID = Number(
  12. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  13. );
  14. this.getData = function() {
  15. return {
  16. objects: this.activeShape,
  17. position: this.endposition,
  18. RedpylineID: this.RedpylineID
  19. };
  20. };
  21. this.StartClippingPlanes = function() {
  22. let self = this;
  23. let cartesian = null;
  24. let viewer = self.viewer;
  25. let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  26. handler.setInputAction(movement => {
  27. cartesian = viewer.scene.pickPosition(movement.position);
  28. if (cartesian) {
  29. if (self.endposition.length == 0) {
  30. self.floatingPoint = self.createPoint(
  31. cartesian,
  32. viewer,
  33. self.width,
  34. self.color
  35. );
  36. self.endposition.push(cartesian);
  37. var dynamicPositions = new Cesium.CallbackProperty(function() {
  38. return new Cesium.PolygonHierarchy(self.endposition);
  39. }, false);
  40. self.ischange = 1;
  41. self.activeShape = self.createpolygon(
  42. dynamicPositions,
  43. viewer,
  44. self.width,
  45. self.color
  46. );
  47. }
  48. self.endposition.push(cartesian);
  49. }
  50. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  51. handler.setInputAction(function(event) {
  52. if (Cesium.defined(self.floatingPoint)) {
  53. var newPosition = viewer.scene.pickPosition(event.endPosition);
  54. if (Cesium.defined(newPosition)) {
  55. self.floatingPoint.position.setValue(newPosition);
  56. self.endposition.pop();
  57. self.endposition.push(newPosition);
  58. }
  59. }
  60. }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  61. handler.setInputAction(() => {
  62. handler.destroy();
  63. if (typeof self.callback == "function") {
  64. self.callback(self.activeShape, self.endposition);
  65. }
  66. viewer.entities.remove(self.floatingPoint);
  67. viewer.entities.remove(self.activeShape);
  68. self.getpoint(self.endposition);
  69. }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  70. };
  71. this.createPoint = function(worldPosition, viewer, width, color) {
  72. var point = viewer.entities.add({
  73. position: worldPosition,
  74. point: {
  75. pixelSize: width,
  76. color: Cesium.Color.fromCssColorString(color)
  77. }
  78. });
  79. return point;
  80. };
  81. this.createpolygon = function(
  82. worldPosition,
  83. viewer,
  84. width,
  85. color,
  86. extrudedHeight,
  87. height
  88. ) {
  89. const polygon = viewer.entities.add({
  90. polygon: {
  91. hierarchy:
  92. this.ischange == 1
  93. ? worldPosition
  94. : new Cesium.PolygonHierarchy(worldPosition),
  95. width: width,
  96. material:
  97. this.ischange == 1
  98. ? Cesium.Color.fromCssColorString(color).withAlpha(0.5)
  99. : new Cesium.ImageMaterialProperty({
  100. //映射到图像
  101. image: "./images/b.png", //资源地址
  102. repeat: new Cesium.Cartesian2(
  103. worldPosition.length / 2,
  104. worldPosition.length / 2
  105. ) //想x,y方向重复的次数
  106. }),
  107. extrudedHeight: extrudedHeight || 0,
  108. height: height || 0,
  109. closeTop: this.ischange == 1 ? true : false
  110. }
  111. });
  112. return polygon;
  113. };
  114. this.getpoint = function(data) {
  115. let points = [];
  116. data.forEach(item => {
  117. var lat = Cesium.Math.toDegrees(
  118. this.viewer.scene.globe.ellipsoid.cartesianToCartographic(item).latitude
  119. );
  120. var lng = Cesium.Math.toDegrees(
  121. this.viewer.scene.globe.ellipsoid.cartesianToCartographic(item)
  122. .longitude
  123. );
  124. var alt = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(item)
  125. .height;
  126. points.push([lng, lat, alt]);
  127. });
  128. points.push(points[0]);
  129. if (turf.booleanClockwise(turf.lineString(points))) {
  130. let pointstart = [];
  131. points.reverse().pop();
  132. pointstart.push(points[points.length - 1]);
  133. pointstart = pointstart.concat(points);
  134. this.getterrionclippingPlanes(data, true);
  135. } else {
  136. const pointes = points;
  137. this.getterrionclippingPlanes(data, false);
  138. }
  139. };
  140. this.getterrionclippingPlanes = function(points, checked) {
  141. if (checked) {
  142. points = points.reverse();
  143. }
  144. let viewer = this.viewer;
  145. var pointsLength = points.length;
  146. var clippingPlanes = []; // 存储ClippingPlane集合
  147. for (var i = 0; i < pointsLength; ++i) {
  148. var nextIndex = (i + 1) % pointsLength;
  149. var midpoint = Cesium.Cartesian3.add(
  150. points[i],
  151. points[nextIndex],
  152. new Cesium.Cartesian3()
  153. );
  154. midpoint = Cesium.Cartesian3.multiplyByScalar(midpoint, 0.5, midpoint);
  155. var up = Cesium.Cartesian3.normalize(midpoint, new Cesium.Cartesian3());
  156. var right = Cesium.Cartesian3.subtract(
  157. points[nextIndex],
  158. midpoint,
  159. new Cesium.Cartesian3()
  160. );
  161. right = Cesium.Cartesian3.normalize(right, right);
  162. var normal = Cesium.Cartesian3.cross(right, up, new Cesium.Cartesian3());
  163. normal = Cesium.Cartesian3.normalize(normal, normal);
  164. var originCenteredPlane = new Cesium.Plane(normal, 0.0);
  165. var distance = Cesium.Plane.getPointDistance(
  166. originCenteredPlane,
  167. midpoint
  168. );
  169. clippingPlanes.push(new Cesium.ClippingPlane(normal, distance));
  170. }
  171. var globe = viewer.scene.globe;
  172. globe.clippingPlanes = new Cesium.ClippingPlaneCollection({
  173. planes: clippingPlanes,
  174. edgeWidth: 0.0,
  175. edgeColor: Cesium.Color.WHITE
  176. });
  177. globe.backFaceCulling = true;
  178. globe.showSkirts = true;
  179. this.ischange = 2;
  180. this.createpolygon(points, this.viewer, 8, 0, -10);
  181. };
  182. this.unterrionclippingPlanes = function() {
  183. this.viewer.scene.globe.clippingPlanes = null;
  184. };
  185. }