clip-plane.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //平面裁剪封装类
  2. class ClipPlane {
  3. /**
  4. * Creates an instance of Roaming.
  5. * @param {*} viewer 需要传入
  6. * @param {*} options.modelUrl 需要传入,模型编辑器需要的载体
  7. * @param {*} options.ClipPlaneShow 可选,平面显隐
  8. * @param {*} options.clipPlaneScale 可选,平面的缩放系数
  9. * @memberof Roaming
  10. * @example
  11. *
  12. */
  13. constructor(viewer, options) {
  14. this.viewer = viewer;
  15. this.init(viewer);
  16. this.updateOptionsParams(options);
  17. }
  18. //初始化
  19. init(viewer) {
  20. this.s3mInstanceColc = new SuperMap3D.S3MInstanceCollection(viewer.scene._context);
  21. viewer.scene.primitives.add(this.s3mInstanceColc);
  22. this.clipPlanePositions = null;
  23. this.ClipPlaneShow = true;
  24. this.clipPlaneScale = 1;
  25. this.LocalToWorldMatrix = null;
  26. }
  27. /**
  28. * 更新可配置的内部参数
  29. * @param {object} options 配置项
  30. */
  31. updateOptionsParams(options) {
  32. if (!options) return;
  33. if (SuperMap3D.defined(options.modelUrl)) this.modelUrl = options.modelUrl;
  34. if (SuperMap3D.defined(options.ClipPlaneShow)) this.ClipPlaneShow = options.ClipPlaneShow;
  35. if (SuperMap3D.defined(options.clipPlaneScale)) this.clipPlaneScale = options.clipPlaneScale;
  36. }
  37. /**
  38. * 设置裁剪面位置
  39. * @param {object} _LocalToWorldMatrix 模型矩阵
  40. */
  41. setPlanePositions(_LocalToWorldMatrix) {
  42. this.LocalToWorldMatrix = _LocalToWorldMatrix;
  43. let width = 10 * this.clipPlaneScale;
  44. // 在本地坐标
  45. let _localPos4 = new SuperMap3D.Cartesian3(width, width, 0);
  46. let _localPos3 = new SuperMap3D.Cartesian3(width, -width, 0);
  47. let _localPos2 = new SuperMap3D.Cartesian3(-width, -width, 0);
  48. let _localPos1 = new SuperMap3D.Cartesian3(-width, width, 0);
  49. // 将本地坐标转世界坐标
  50. let _worldPoint1 = SuperMap3D.Matrix4.multiplyByPoint(_LocalToWorldMatrix, _localPos1, new SuperMap3D.Cartesian3());
  51. let _worldPoint2 = SuperMap3D.Matrix4.multiplyByPoint(_LocalToWorldMatrix, _localPos2, new SuperMap3D.Cartesian3());
  52. let _worldPoint3 = SuperMap3D.Matrix4.multiplyByPoint(_LocalToWorldMatrix, _localPos3, new SuperMap3D.Cartesian3());
  53. let _worldPoint4 = SuperMap3D.Matrix4.multiplyByPoint(_LocalToWorldMatrix, _localPos4, new SuperMap3D.Cartesian3());
  54. this.clipPlanePositions = [_worldPoint1, _worldPoint2, _worldPoint3, _worldPoint4];
  55. this.clipPlaneUpdate();
  56. }
  57. // 添加裁剪面entity
  58. addsurface() {
  59. this.planeSurface = this.viewer.entities.add({
  60. id: "clip-plane",
  61. polygon: {
  62. hierarchy: new SuperMap3D.CallbackProperty(() => {
  63. return {
  64. positions: this.clipPlanePositions,
  65. holes: []
  66. };
  67. }, false),
  68. show: new SuperMap3D.CallbackProperty(() => {
  69. return this.ClipPlaneShow
  70. }, false),
  71. material: SuperMap3D.Color.GOLD.withAlpha(0.2),
  72. outline: true,
  73. outlineColor: SuperMap3D.Color.GOLD,
  74. perPositionHeight: true
  75. }
  76. });
  77. }
  78. //添加编辑模型
  79. addModel(position) {
  80. let id = "clip-model";
  81. this.s3mInstanceColc.add(this.modelUrl, {
  82. id: id,
  83. position: position,
  84. scale: new SuperMap3D.Cartesian3(0, 0, 0)
  85. });
  86. let editEntity = this.s3mInstanceColc.getInstance(this.modelUrl, id);
  87. if (!this.modelEditor) this.addModelEditor(editEntity);
  88. else {
  89. this.modelEditor.setEditObject(editEntity);
  90. this.modelEditor.activate();
  91. }
  92. }
  93. // 执行添加裁剪面及编辑模型
  94. setClipPlane(position) {
  95. this.clear();
  96. let LocalToWorldMatrix = SuperMap3D.Transforms.eastNorthUpToFixedFrame(position);
  97. this.setPlanePositions(LocalToWorldMatrix);
  98. this.addsurface();
  99. this.addModel(position);
  100. }
  101. // 添加模型编辑器
  102. addModelEditor(model) {
  103. this.modelEditor = new SuperMap3D.ModelEditor({
  104. model: model,
  105. scene: this.viewer.scene,
  106. axesShow: {
  107. translation: true,
  108. rotation: true,
  109. scale: false
  110. }
  111. });
  112. this.modelEditor.activate();
  113. this.modelEditor.changedEvt.addEventListener(param => {
  114. this.setPlanePositions(param.modelMatrix)
  115. });
  116. }
  117. // 更新
  118. clipPlaneUpdate() {
  119. if (!this.clipPlanePositions) return;
  120. let layers = this.viewer.scene.layers.layerQueue;
  121. for (let layer of layers) {
  122. layer.setCustomClipPlane(
  123. this.clipPlanePositions[0],
  124. this.clipPlanePositions[1],
  125. this.clipPlanePositions[2]
  126. );
  127. }
  128. }
  129. /**
  130. * 设置裁剪面显隐
  131. * @param {object} val Boolean
  132. */
  133. setPlaneShow(val) {
  134. this.ClipPlaneShow = val;
  135. }
  136. /**
  137. *
  138. * @param {object} val Boolean
  139. */
  140. setModelEditorShow(val) {
  141. if (!this.modelEditor) return;
  142. if (!val) this.modelEditor.deactivate();
  143. else {
  144. let editEntity = this.s3mInstanceColc.getInstance(this.modelUrl, "clip-model");
  145. this.modelEditor.setEditObject(editEntity);
  146. this.modelEditor.activate();
  147. }
  148. }
  149. /**裁剪平面缩放系数
  150. * @param {object} val Number 默认1
  151. */
  152. setClipPlaneScale(val) {
  153. this.clipPlaneScale = val;
  154. if (this.LocalToWorldMatrix);
  155. this.setPlanePositions(this.LocalToWorldMatrix);
  156. }
  157. // 清除
  158. clear() {
  159. let layers = this.viewer.scene.layers.layerQueue;
  160. for (let layer of layers) {
  161. layer.clearCustomClipBox();
  162. }
  163. if (this.planeSurface) {
  164. this.viewer.entities.remove(this.planeSurface);
  165. this.modelEditor.deactivate();
  166. this.s3mInstanceColc.removeCollection(this.modelUrl);
  167. this.planeSurface = null;
  168. this.clipPlanePositions = null;
  169. this.LocalToWorldMatrix = null;
  170. }
  171. }
  172. /**
  173. * 销毁
  174. */
  175. destroy() {
  176. this.clear();
  177. if (this.modelEditor) this.modelEditor.destroy();
  178. }
  179. }
  180. window.ClipPlane = ClipPlane