如何在Cesium上移动3D模型

时间:2015-06-12 05:19:15

标签: 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);

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

1 个答案:

答案 0 :(得分:2)

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

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;

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