以下代码来自Cesium 论坛:3DTileset rotation - CesiumJS - Cesium Community
在1.118中测试可行,可直接在Sandcastle中运行:
const viewer = new Cesium.Viewer("cesiumContainer", {
terrain: Cesium.Terrain.fromWorldTerrain(),
});
// Disable the ("globe") rotation for left mouse drags
viewer.scene.screenSpaceCameraController.enableRotate = false;
let tileset = viewer.scene.primitives.add(
await Cesium.Cesium3DTileset.fromIonAssetId(40866)
);
// Store the transform of the root node of the
// tileset as the "root transform", assign this
// as the initial model matrix, and set the
// transform of the root node to be the identity
const rootTransform = Cesium.Matrix4.clone(tileset.root.transform);
tileset.modelMatrix = Cesium.Matrix4.clone(rootTransform);
tileset.root.transform = Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY);
const fullRotationMatrix = Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY);
let selectedTileset;
let initialMousePosition;
const enableTransformEditor = true;
const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(async (click) => {
const pickedObject = viewer.scene.pick(click.position);
if (enableTransformEditor && pickedObject && !pickedObject.id) {
selectedTileset = pickedObject.primitive;
initialMousePosition = Cesium.Cartesian2.clone(click.position);
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
handler.setInputAction((click) => {
selectedTileset = null;
initialMousePosition = null;
}, Cesium.ScreenSpaceEventType.LEFT_UP);
handler.setInputAction((movement) => {
if (selectedTileset && initialMousePosition) {
const translation = new Cesium.Cartesian3();
Cesium.Matrix4.getTranslation(selectedTileset.modelMatrix, translation);
const dx = movement.endPosition.x - initialMousePosition.x;
const yaw = Cesium.Math.toRadians(dx);
// Compute the rotation matrix, and multiply it
// to the "full rotation matrix"
const rotation3 = Cesium.Matrix3.fromRotationZ(yaw);
const rotationMatrix = Cesium.Matrix4.fromRotation(rotation3);
Cesium.Matrix4.multiply(
fullRotationMatrix,
rotationMatrix,
fullRotationMatrix
);
// Compute the new model matrix from the
// root transform and the full rotation matrix
const newModelMatrix = Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY);
Cesium.Matrix4.multiply(rootTransform, fullRotationMatrix, newModelMatrix);
// Apply the new model matrix
selectedTileset.modelMatrix = newModelMatrix;
// Update initialMousePosition for the next movement
initialMousePosition = Cesium.Cartesian2.clone(movement.endPosition);
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
viewer.zoomTo(tileset);