Skip to content

模型裁剪

裁剪的保存和标绘、单体化相同,都是裁剪面要素统一保存在 cutDataSource(GeoJsonDataSource)中, 然后利用GeoJsonDataSourcetoGeoJson方法导出提交。

上述是数据存储,但是裁剪需要额外调用裁接口

模型裁剪绘制

JavaScript
var cutDataSource;
//裁剪的面要素会保存到该对象中
var promise = viewer.dataSources.add(new LSGlobe.GeoJsonDataSource("cutDataSource"));
promise.then(function(dataSource) {
    cutDataSource = dataSource;
}).otherwise(function(error) {

});

//裁剪的绘制
var cutHandler = new LSGlobe.ScreenSpaceEventHandler(viewer.scene.canvas);
cutHandler.setInputAction(function(movement) {
    //获取的坐标上添加标绘点,具体的坐标获取参照坐标转换
    var Pos = scene.pickGlobe(movement.position);
},LSGlobe.ScreenSpaceEventType.LEFT_CLICK);

//根据上面获取到的点集合经过处理后达到新的数据
var oValuePoint = ChangePolygonPosition(event.positions);//同压平的坐标处理方法

抠取模式

JavaScript
// 要裁剪的倾斜对象
var oPrimitive
if (!oPrimitive.pits) {
	const pitCollection = new LSGlobe.PitCollection({
		pits: [],
		// 传入的坑对象数组
		enabled: true,
		// 是否启用,设为false不显示;默认值为true。
		numberOfCascades: 4,
		// 级联数量,设为1或4:1——性能高;4——精度高;默认值为4。
		// 距离相机的最远作用距离,超过此距离后模型没有效果。距离越大精度越低,默认值为5000,最大距离限制为20000。
		maximumDistance: 8000,
		// 使用的映射纹理尺寸。数值越大性能越低占用显存越高但精度高;默认值为2048。
		size: 2048
	});
    oPrimitive.pits = pitCollection
}
var cutPolygonHierarchy = new LSGlobe.PolygonHierarchy(oValuePoint);
var pit = new LSGlobe.Pit({
	polygon: cutPolygonHierarchy,
	show: true
});

pit.name = CutName;
pit.id = CutId;

oPrimitive.pits.add(pit);

裁剪修改

主要通过操作裁剪集合数据 cutDataSource.entities,然后进行对应的裁剪类型进行操作

JavaScript
var entities = cutDataSource.entities.values;
for (var i = 0; i < entities.length; i++) {
	var entity = entities[i];
	var oProperties = entity.properties.getValue();
	var CutId = entity.id;
	var CutName = entity.name;
	if (tilesetid && oProperties.tilesetId != tilesetid) {
		continue;
	}
	if (entity.polygon && entity.show) {
		entity.polygon.outline = false;
		var cutPolygon = new LSGlobe.PolygonGeometry({
			polygonHierarchy: new LSGlobe.PolygonHierarchy(entity.polygon.hierarchy._value.positions),
			perPositionHeight: true
		});
		cutPolygon.name = CutName;
		cutPolygon.id = CutId;

		var tileset = getPrimitive(oProperties.tilesetId); //实景三维数据获取方法
		if (!tileset.pits) {
			const pitCollection = new LSGlobe.PitCollection({
				pits: [],
				// 传入的坑对象数组
				enabled: true,
				// 是否启用,设为false不显示;默认值为true。
				numberOfCascades: 4,
				// 级联数量,设为1或4:1——性能高;4——精度高;默认值为4。
				// 距离相机的最远作用距离,超过此距离后模型没有效果。距离越大精度越低,默认值为5000,最大距离限制为20000。
				maximumDistance: 8000,
				// 使用的映射纹理尺寸。数值越大性能越低占用显存越高但精度高;默认值为2048。
				size: 2048
			});
			tileset.pits = pitCollection
		}
		var cutPolygonHierarchy = new LSGlobe.PolygonHierarchy(oValuePoint);
		var pit = new LSGlobe.Pit({
			polygon: cutPolygonHierarchy,
			show: true
		});

		pit.name = CutName;
		pit.id = CutId;

		tileset.pits.add(pit);
	}
}

裁剪显示隐藏

JavaScript
var cutEntity = cutDataSource.entities.getById(id);//获取对应裁剪entity.
cutEntity.show=true;//true:显示,false:隐藏
var oProperties = cutEntity.properties.getValue();//获取裁剪的类型

// 获取primitive
export const getPrimitive = (name) => {
  const layer = viewer.scene.primitives._primitives.find(primitive => primitive.name && (primitive.name === name || primitive.name.id == name))
  return layer
}

var tileset = getPrimitive(oProperties.tilesetId);//实景三维数据获取方法

tileset.pits._pits[0].show=true;//true:显示,false:隐藏

裁剪飞行

提示

裁剪定位可以参照视角定位中的flyToBoundingSphere定位方法,

JavaScript
var entity = cutDataSource.entities.getById(id);

//方法1
viewer.flyto(entity)

//方法2
// 利用点集合定位,positions世界坐标数组
const boundingSphere = LSGlobe.BoundingSphere.fromPoints(entity.polygon.hierarchy.getValue().positions);
viewer.camera.flyToBoundingSphere(boundingSphere, {
    offset: new LSGlobe.HeadingPitchRange(0, -LSGlobe.Math.PI_OVER_TWO, 0)
});

裁剪删除

JavaScript
var cutEntity = cutDataSource.entities.getById(id);//根据裁剪id获取对应的参考entity
var oProperties = cutEntity.properties.getValue();//获取裁剪的类型
var tileset = getPageLODLayersById(oProperties.tilesetId);//实景三维数据获取方法

if(oProperties.cutType=="clicp"){
    tileset.cleanClipPolygons();
}else{
    for (var i = 0; i < tileset._pitPolygons.length; i++) {
        if (cutEntity.id == tileset._pitPolygons[i].id) {
            tileset._pitPolygons[i].show =false;
            tileset.updatePit();
        }
    }
}

cutDataSource.entities.removeById(id);

裁剪存储

裁剪存储是以 json 格式保存的,直接赋值给 oSubSceneData.clip,然后随场景保存接口一并提交保存

JavaScript
oSubSceneData.clip = cutDataSource.toGeoJson()

从场景数据中加载裁剪

裁剪数据获取接口查询场景裁剪信息,参数 jsonType 为 6;

JavaScript
var oGeoJson = result.data;
//result.data前面获取的压平数据json
var promise = LSGlobe.GeoJsonDataSource.load(oGeoJson, {});
promise.then(function(dataSource) {
	//将数据再次加载到初始化存储裁剪的对象中
	cutDataSource = dataSource;
	//添加到球中
	viewer.dataSources.add(dataSource);

	var entities = cutDataSource.entities._entities._array;
	for (var i = 0; i < entities.length; i++) {
		var entity = entities[i];
		var oProperties = entity.properties.getValue();
		var CutId = entity.id;
		var CutName = entity.name;
		if (tilesetid && oProperties.tilesetId != tilesetid) {
			continue;
		}
		if (entity.polygon && entity.show) {
			var tileset = getPrimitive(oProperties.tilesetId); //实景三维数据获取方法
			if (!tileset.pits) {
				const pitCollection = new LSGlobe.PitCollection({
					pits: [],
					// 传入的坑对象数组
					enabled: true,
					// 是否启用,设为false不显示;默认值为true。
					numberOfCascades: 4,
					// 级联数量,设为1或4:1——性能高;4——精度高;默认值为4。
					// 距离相机的最远作用距离,超过此距离后模型没有效果。距离越大精度越低,默认值为5000,最大距离限制为20000。
					maximumDistance: 8000,
					// 使用的映射纹理尺寸。数值越大性能越低占用显存越高但精度高;默认值为2048。
					size: 2048
				});
				tileset.pits = pitCollection
			}
			var cutPolygonHierarchy = new LSGlobe.PolygonHierarchy(entity.polygon.hierarchy.getValue().positions);
			var pit = new LSGlobe.Pit({
				polygon: cutPolygonHierarchy,
				show: true
			});

			pit.name = CutName;
			pit.id = CutId;

			tileset.pits.add(pit);
		}
	}
}).otherwise(function(error) {
	console.log(error);
});

Released under the MIT License.