123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- //平面裁剪封装类
- class ClipPlane {
- /**
- * Creates an instance of Roaming.
- * @param {*} viewer 需要传入
- * @param {*} options.modelUrl 需要传入,模型编辑器需要的载体
- * @param {*} options.ClipPlaneShow 可选,平面显隐
- * @param {*} options.clipPlaneScale 可选,平面的缩放系数
- * @memberof Roaming
- * @example
- *
- */
- constructor(viewer, options) {
- this.viewer = viewer;
- this.init(viewer);
- this.updateOptionsParams(options);
- }
- //初始化
- init(viewer) {
- this.s3mInstanceColc = new SuperMap3D.S3MInstanceCollection(viewer.scene._context);
- viewer.scene.primitives.add(this.s3mInstanceColc);
- this.clipPlanePositions = null;
- this.ClipPlaneShow = true;
- this.clipPlaneScale = 1;
- this.LocalToWorldMatrix = null;
- }
- /**
- * 更新可配置的内部参数
- * @param {object} options 配置项
- */
- updateOptionsParams(options) {
- if (!options) return;
- if (SuperMap3D.defined(options.modelUrl)) this.modelUrl = options.modelUrl;
- if (SuperMap3D.defined(options.ClipPlaneShow)) this.ClipPlaneShow = options.ClipPlaneShow;
- if (SuperMap3D.defined(options.clipPlaneScale)) this.clipPlaneScale = options.clipPlaneScale;
- }
-
- /**
- * 设置裁剪面位置
- * @param {object} _LocalToWorldMatrix 模型矩阵
- */
- setPlanePositions(_LocalToWorldMatrix) {
- this.LocalToWorldMatrix = _LocalToWorldMatrix;
- let width = 10 * this.clipPlaneScale;
- // 在本地坐标
- let _localPos4 = new SuperMap3D.Cartesian3(width, width, 0);
- let _localPos3 = new SuperMap3D.Cartesian3(width, -width, 0);
- let _localPos2 = new SuperMap3D.Cartesian3(-width, -width, 0);
- let _localPos1 = new SuperMap3D.Cartesian3(-width, width, 0);
- // 将本地坐标转世界坐标
- let _worldPoint1 = SuperMap3D.Matrix4.multiplyByPoint(_LocalToWorldMatrix, _localPos1, new SuperMap3D.Cartesian3());
- let _worldPoint2 = SuperMap3D.Matrix4.multiplyByPoint(_LocalToWorldMatrix, _localPos2, new SuperMap3D.Cartesian3());
- let _worldPoint3 = SuperMap3D.Matrix4.multiplyByPoint(_LocalToWorldMatrix, _localPos3, new SuperMap3D.Cartesian3());
- let _worldPoint4 = SuperMap3D.Matrix4.multiplyByPoint(_LocalToWorldMatrix, _localPos4, new SuperMap3D.Cartesian3());
- this.clipPlanePositions = [_worldPoint1, _worldPoint2, _worldPoint3, _worldPoint4];
- this.clipPlaneUpdate();
- }
- // 添加裁剪面entity
- addsurface() {
- this.planeSurface = this.viewer.entities.add({
- id: "clip-plane",
- polygon: {
- hierarchy: new SuperMap3D.CallbackProperty(() => {
- return {
- positions: this.clipPlanePositions,
- holes: []
- };
- }, false),
- show: new SuperMap3D.CallbackProperty(() => {
- return this.ClipPlaneShow
- }, false),
- material: SuperMap3D.Color.GOLD.withAlpha(0.2),
- outline: true,
- outlineColor: SuperMap3D.Color.GOLD,
- perPositionHeight: true
- }
- });
- }
- //添加编辑模型
- addModel(position) {
- let id = "clip-model";
- this.s3mInstanceColc.add(this.modelUrl, {
- id: id,
- position: position,
- scale: new SuperMap3D.Cartesian3(0, 0, 0)
- });
- let editEntity = this.s3mInstanceColc.getInstance(this.modelUrl, id);
- if (!this.modelEditor) this.addModelEditor(editEntity);
- else {
- this.modelEditor.setEditObject(editEntity);
- this.modelEditor.activate();
- }
- }
- // 执行添加裁剪面及编辑模型
- setClipPlane(position) {
- this.clear();
- let LocalToWorldMatrix = SuperMap3D.Transforms.eastNorthUpToFixedFrame(position);
- this.setPlanePositions(LocalToWorldMatrix);
- this.addsurface();
- this.addModel(position);
- }
- // 添加模型编辑器
- addModelEditor(model) {
- this.modelEditor = new SuperMap3D.ModelEditor({
- model: model,
- scene: this.viewer.scene,
- axesShow: {
- translation: true,
- rotation: true,
- scale: false
- }
- });
- this.modelEditor.activate();
- this.modelEditor.changedEvt.addEventListener(param => {
- this.setPlanePositions(param.modelMatrix)
- });
- }
- // 更新
- clipPlaneUpdate() {
- if (!this.clipPlanePositions) return;
- let layers = this.viewer.scene.layers.layerQueue;
- for (let layer of layers) {
- layer.setCustomClipPlane(
- this.clipPlanePositions[0],
- this.clipPlanePositions[1],
- this.clipPlanePositions[2]
- );
- }
- }
- /**
- * 设置裁剪面显隐
- * @param {object} val Boolean
- */
- setPlaneShow(val) {
- this.ClipPlaneShow = val;
- }
- /**
- *
- * @param {object} val Boolean
- */
- setModelEditorShow(val) {
- if (!this.modelEditor) return;
- if (!val) this.modelEditor.deactivate();
- else {
- let editEntity = this.s3mInstanceColc.getInstance(this.modelUrl, "clip-model");
- this.modelEditor.setEditObject(editEntity);
- this.modelEditor.activate();
- }
- }
- /**裁剪平面缩放系数
- * @param {object} val Number 默认1
- */
- setClipPlaneScale(val) {
- this.clipPlaneScale = val;
- if (this.LocalToWorldMatrix);
- this.setPlanePositions(this.LocalToWorldMatrix);
- }
- // 清除
- clear() {
- let layers = this.viewer.scene.layers.layerQueue;
- for (let layer of layers) {
- layer.clearCustomClipBox();
- }
- if (this.planeSurface) {
- this.viewer.entities.remove(this.planeSurface);
- this.modelEditor.deactivate();
- this.s3mInstanceColc.removeCollection(this.modelUrl);
- this.planeSurface = null;
- this.clipPlanePositions = null;
- this.LocalToWorldMatrix = null;
- }
- }
- /**
- * 销毁
- */
- destroy() {
- this.clear();
- if (this.modelEditor) this.modelEditor.destroy();
- }
- }
- window.ClipPlane = ClipPlane
|