【Cesium】模型平面裁切

javascript 复制代码
const scene = viewer.scene;

let tileset;
let targetY = 400.0;
let planeEntities = [];
let selectedPlane;  // 选择的切面
let clippingPlanes;  // 切面属性

// 当鼠标点击切面时,修改相关属性
const downHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
downHandler.setInputAction(function (movement) {
    const pickedObject = scene.pick(movement.position);
    if (
        Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        Cesium.defined(pickedObject.id.plane)
    ) {
        selectedPlane = pickedObject.id.plane;
        selectedPlane.material = Cesium.Color.WHITE.withAlpha(0.2); // 切面颜色
        selectedPlane.outlineColor = Cesium.Color.BLUE;  // 切面轮廓线颜色
        scene.screenSpaceCameraController.enableInputs = false;
    }
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);

// 鼠标弹出时回复 切面样式
const upHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
upHandler.setInputAction(function () {
    if (Cesium.defined(selectedPlane)) {
        selectedPlane.material = Cesium.Color.WHITE.withAlpha(0.1);
        selectedPlane.outlineColor = Cesium.Color.WHITE;
        selectedPlane = undefined;
    }

    scene.screenSpaceCameraController.enableInputs = true;
}, Cesium.ScreenSpaceEventType.LEFT_UP);

// 移动鼠标更新切面位置
const moveHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
moveHandler.setInputAction(function (movement) {
    if (Cesium.defined(selectedPlane)) {
        const deltaY = movement.startPosition.y - movement.endPosition.y;
        targetY += deltaY;
    }
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

function createPlaneUpdateFunction(plane) {
    return function () {
        plane.distance = targetY;
        return plane;
    };
}

// 加载3d tiles
function loadTileset(url) {
    // 指定一组修剪平面。剪裁平面有选择地禁用单个 gltf 模型、3D 图块集或地球的指定 ClippingPlane 对象列表外部区域中的渲染。
    clippingPlanes = new Cesium.ClippingPlaneCollection({
        planes: [  // ClippingPlane 对象的数组,用于有选择地禁用每个平面外部的呈现。
            new Cesium.ClippingPlane(
                new Cesium.Cartesian3(0.0, 0.0, -1.0),
                10.0
            ),
        ],
        edgeWidth: 2.0, // 设置高亮显示对象被剪裁边缘的宽度
    });

    tileset = viewer.scene.primitives.add(
        new Cesium.Cesium3DTileset({
            url: url,
            clippingPlanes: clippingPlanes,
        })
    );

    return tileset.readyPromise 
        .then(function () {
            const boundingSphere = tileset.boundingSphere;
            const radius = boundingSphere.radius;  // 3d tiles包围球体的半径
            viewer.zoomTo(tileset,new Cesium.HeadingPitchRange(0.5, -0.2, radius * 4.0));

            for (let i = 0; i < clippingPlanes.length; ++i) {
                const plane = clippingPlanes.get(i);
                const planeEntity = viewer.entities.add({
                    position: boundingSphere.center,
                    plane: {
                        dimensions: new Cesium.Cartesian2(radius * 1.5,radius * 1.8), // 切面的范围
                        material: Cesium.Color.WHITE.withAlpha(0.1),
                        plane: new Cesium.CallbackProperty(  // plane回调,位置
                            createPlaneUpdateFunction(plane),
                            false
                        ),
                        outline: true,
                        outlineColor: Cesium.Color.WHITE,
                    },
                });
                planeEntities.push(planeEntity);
            }
            return tileset;
        })
        .catch(function (error) {
            console.log(error);
        });
}

// 加载3d tiles,创建自己的3d tiles,并上传到cesium ion,也可本地加载3d tiles
const bimUrl = Cesium.IonResource.fromAssetId(2367681); 
loadTileset(bimUrl); 
相关推荐
sin°θ_陈11 小时前
前馈式3D Gaussian Splatting 研究地图(路线二):几何优先的前馈式 3DGS——前馈式 3DGS 如何重新拥抱多视图几何
深度学习·3d·webgl·三维重建·空间计算·3dgs·空间智能
星河耀银海2 天前
3D效果:HTML5 WebGL结合AI实现智能3D场景渲染
前端·人工智能·深度学习·3d·html5·webgl
AI科技星2 天前
光速螺旋量子几何统一场论——基于 v ≡ c 公理的四大基本力全维度求导证明与精准数值验证
c语言·开发语言·人工智能·算法·机器学习·平面
点量云实时渲染-小芹2 天前
Unity模型数字孪生虚拟仿真webgl推流卡实时云渲染推流
unity·webgl·数字孪生·实时云渲染·虚拟仿真·云推流
@猪大肠2 天前
SolidWorks2025 安装教程+附下载链接
平面·3d
duansamve2 天前
Vue3的Vite项目中直接引入的方式使用Cesium
cesium
WebGISer_白茶乌龙桃3 天前
基于 Cesium 的 GLB 建筑模型分层分房间点击拾取技术实现
前端·javascript·vue.js·webgl·cesium
sin°θ_陈3 天前
前馈式3D Gaussian Splatting 研究地图(路线一):像素对齐高斯的起点——pixelSplat 与 latentSplat 在解决什么
python·深度学习·3d·aigc·webgl·3dgs·空间智能
就是个名称3 天前
Chrome使用cesium.js或者three.js报错不支持webGL
javascript·chrome·webgl
yang_B6214 天前
最小二乘法 拟合平面
算法·平面·最小二乘法