gpt4 book ai didi

cesiumjs - 如何在Cesium上移动3D模型

转载 作者:行者123 更新时间:2023-12-01 18:56:16 32 4
gpt4 key购买 nike

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

所以现在,我正在尝试通过单击移动模型。当点击模型时。模型必须朝一个方向移动(在刻度上增加值 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);

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

最佳答案

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

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;

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

关于cesiumjs - 如何在Cesium上移动3D模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30795745/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com