如何在 Cesium 上移动 3D 模型

How to move 3D model on Cesium

我想使用键盘快捷键动态移动模型。我找不到相关的文章。

所以现在,我正在尝试通过点击移动模型。单击模型时。模型必须朝一个方向移动(在刻度上增加值 1)。在下方找到相应的沙堡代码。

var selectedMesh; var i=0;

var viewer = new Cesium.Viewer('cesiumContainer', {
    infoBox: false,
    selectionIndicator: false
});

var handle = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);

function createModel(url, height) {
    viewer.entities.removeAll();

    var position = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, height);
    var heading = Cesium.Math.toRadians(135);
    var pitch = 0;
    var roll = 0;
    var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, heading, pitch, roll);

    var entity = viewer.entities.add({
        name: url,
        position: position,
        orientation: orientation,
        model: {
            uri: url,
            minimumPixelSize: 128
        }
    });
    viewer.trackedEntity = entity;


    viewer.clock.onTick.addEventListener(function () {
        if (selectedMesh) {
            console.log("Before 0 : " + selectedMesh.primitive.modelMatrix[12]);
            selectedMesh.primitive.modelMatrix[12] = selectedMesh.primitive.modelMatrix[12] + 1;
            console.log("After 0 : " + selectedMesh.primitive.modelMatrix[12]);
        } 
    });
}

handle.setInputAction(function (movement) {
    console.log("LEFT CLICK");
    var pick = viewer.scene.pick(movement.position);
    if (Cesium.defined(pick) && Cesium.defined(pick.node) && Cesium.defined(pick.mesh)) {

        if (!selectedMesh) {
            selectedMesh = pick;
        }
    }
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

var options = [{
    text: 'Aircraft',
    onselect: function () {
        createModel('../../SampleData/models/CesiumAir/Cesium_Air.bgltf', 5000.0);
    }
}, {
    text: 'Ground vehicle',
    onselect: function () {
        createModel('../../SampleData/models/CesiumGround/Cesium_Ground.bgltf', 0);
    }
}, {
    text: 'Milk truck',
    onselect: function () {
        createModel('../../SampleData/models/CesiumMilkTruck/CesiumMilkTruck.bgltf', 0);
    }
}, {
    text: 'Skinned character',
    onselect: function () {
        createModel('../../SampleData/models/CesiumMan/Cesium_Man.bgltf', 0);
    }
}];

Sandcastle.addToolbarMenu(options);

当我点击时,模型第一次移动。在那之后,它停留在同一个地方。我已经在控制台中打印了该值。似乎价值没有改变。我不确定这里的问题。或者我错误地实施了转换。

如果您跟踪实体的当前纬度和经度,并根据用户输入调整纬度和经度,您需要做的就是更新实体的方向。

var lon = // the updated lon
var lat = // updated lat
var position = Cesium.Cartesian3.fromDegrees(lon, lat, height);

var heading = Cesium.Math.toRadians(135);
var pitch = 0;
var roll = 0;

// create an orientation based on the new position
var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, heading, pitch, roll);

然后你只需要更新实体的方向。

entity.orientation = orientation;

通过更改值,模型方向和位置将得到更新。