cesium 实现模型旋转缩放功能

本文介绍了如何使用Vue框架和Cesium库,通过Knockout实现对输入值的实时监听,进而动态调整3D模型的尺度、高度和旋转角度,以展示如何在Cesium环境中进行模型的实时调整操作。
摘要由CSDN通过智能技术生成

 实现思路主要是通过 Knockout 实现了对输入值的监听,当输入值发生变化时,分别执行不同的操作来实现模型的变换

 参考沙盒案例:3D Tiles Adjust Height - Cesium Sandcastle

<!--
 * @Description: 
 * @Author: yiyi
 * @Date: 2023-12-14 16:19:44
 * @LastEditTime: 2024-01-05 10:06:27
 * @LastEditors: yiyi
 * 加油搞起来
-->
<template>
  <div id="cesiumContainer"></div>
  <div id="toolbar" ref="toolbar" style="position: absolute;left: 0;
  top: 0;z-index: 99;background-color: #fff;width: 500px;">
    <div>Scale</div>
    <a-row>
      <a-col :span="12">
        <a-slider v-model:value="inputValue.scale" :min="0" :max="10" :step="1" @change="changeScale" />
      </a-col>
      <a-col :span="4">
        <a-input-number v-model:value="inputValue.scale" :min="0" :max="10" :step="1" style="margin-left: 16px" />
      </a-col>
    </a-row>
    <div>Height-offset</div>
    <a-row>
      <a-col :span="12">
        <a-slider v-model:value="inputValue.height" :min="0" :max="10" :step="1" @change="changeRotateH" />
      </a-col>
      <a-col :span="4">
        <a-input-number v-model:value="inputValue.height" :min="0" :max="10" :step="1" style="margin-left: 16px" />
      </a-col>
    </a-row>

    <div>RotateX</div>
    <a-row>
      <a-col :span="12">
        <a-slider v-model:value="inputValue.RotateX" :min="0" :max="10" :step="1" @change="changeRotateX" />
      </a-col>
      <a-col :span="4">
        <a-input-number v-model:value="inputValue.RotateX" :min="0" :max="10" :step="1" style="margin-left: 16px" />
      </a-col>
    </a-row>

    <div>RotateY</div>
    <a-row>
      <a-col :span="12">
        <a-slider v-model:value="inputValue.RotateY" :min="0" :max="10" :step="1" @change="changeRotateY" />
      </a-col>
      <a-col :span="4">
        <a-input-number v-model:value="inputValue.RotateY" :min="0" :max="10" :step="1" style="margin-left: 16px" />
      </a-col>
    </a-row>

    <div>RotateZ</div>
    <a-row>
      <a-col :span="12">
        <a-slider v-model:value="inputValue.RotateZ" :min="0" :max="10" :step="1" @change="changeRotateZ" />
      </a-col>
      <a-col :span="4">
        <a-input-number v-model:value="inputValue.RotateZ" :min="0" :max="10" :step="1" style="margin-left: 16px" />
      </a-col>
    </a-row>
  </div>
</template>
<script setup>
import { onMounted, reactive, ref, toRaw } from 'vue';
import * as Cesium from 'cesium';
import { RotateZ, RotateY, RotateX, RotateH } from './utils/move';
const inputValue = reactive({
  scale: 0,
  height: 0,
  RotateX: 0,
  RotateY: 0,
  RotateZ: 0
});
const toolbar = ref(null);
let viewer, tileset, m;
onMounted(() => {
  // cesium 初始化
  Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0ZDJiYWZjNy01YWIzLTQ5MDQtOWRjMC1lNzRjMzZlNDkxZjgiLCJpZCI6MTYzNzI4LCJpYXQiOjE2OTM0NzM4NTZ9.TZLI1V4-2pYZoVSbt-zOkV7Yx7elhjAsqfFt5h4Nrlk';
  //加载影像图层
  const esri = new Cesium.ArcGisMapServerImageryProvider({
    url: "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
    enablePickFeatures: false,
    layer: "img",
    style: "default",
    tileMatrixSetID: "w",
    format: "tiles",
    maximumLevel: 18,
  });
  window.viewer = viewer = new Cesium.Viewer('cesiumContainer', {
    terrainProvider: Cesium.createWorldTerrain({
      requestVertexNormals: true,
      requestWaterMask: true
    }),
    imageryProvider: esri,
    shouldAnimate: true, //是否允许动画
    selectionIndicator: false,
    baseLayerPicker: false,
    fullscreenButton: false,
    geocoder: false,
    homeButton: false,
    infoBox: false,
    sceneModePicker: false,
    timeline: false,
    navigationHelpButton: false,
    navigationInstructionsInitiallyVisible: false,
    showRenderLoopErrors: false,
    shadows: false,
  });
  // 显示帧率
  viewer.scene.debugShowFramesPerSecond = true;
  viewer._cesiumWidget._creditContainer.style.display = "none"; //去除版权信息
  viewer.scene.globe.depthTestAgainstTerrain = true;
  const origin = Cesium.Cartesian3.fromDegrees(117.22089726144343, 31.833569328835598, 60);
  const modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(origin);
  window.tileset = tileset = viewer.scene.primitives.add(
    Cesium.Model.fromGltf({
      id: 'site',   // 模型id
      position: origin,   // 模型位置
      url: '../public/Cesium_Air.glb',
      show: true, // default
      scale: 2.0, // double size
      maximumScale: 20000, // never larger than 20000 * model size (overrides minimumPixelSize)
      allowPicking: true,
      modelMatrix: modelMatrix,
      minimumPixelSize: 128,//最小像素大小,可以避免太小看不见
    })
  );
  m = tileset.modelMatrix;
  tileset.readyPromise.then(function (e) {
    var boundingSphere = tileset.boundingSphere;
    viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -0.5, boundingSphere.radius * 2));
    viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
  });
  Cesium.knockout.applyBindings(inputValue, toolbar.value);

 

});
const changeRotateZ = (v) => {
  RotateZ(v, m);
};
const changeRotateY = (v) => {
  RotateY(v, m);
};
const changeRotateX = (v) => {
  RotateX(v, m);
};
const changeRotateH = (v) => {
  RotateH(v, m);
};
const changeScale = (v) => {
  tileset.scale = v;
};
</script>

<style  scoped>
#cesiumContainer {
  width: 100%;
  height: 100%;
}

#cesiumContainer .cesium-viewer-bottom {
  display: none;
}

.floorSingle {
  position: absolute;
  z-index: 99;
  left: 0px;
  top: 30px;
  background-color: aliceblue;
}

.ant-select {
  position: absolute;
  z-index: 99;
  left: 0px;
  top: 30px;
}
</style>

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 要旋转Cesium模型,您可以使用Cesium的Transform类。Transform类允许您在三维空间中移动、旋转缩放对象。以下是一个简单的示例代码,演示如何使用Transform类将Cesium模型绕Y轴旋转45度: ```javascript // 加载模型 var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(position); var model = viewer.scene.primitives.add(Cesium.Model.fromGltf({ url : 'path/to/model.gltf', modelMatrix : modelMatrix, scale : 10.0 })); // 创建一个Transform对象来旋转模型 var rotation = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45)); var transform = Cesium.Matrix4.fromRotationTranslation(rotation, Cesium.Cartesian3.ZERO); // 应用变换 model.modelMatrix = Cesium.Matrix4.multiply(model.modelMatrix, transform, new Cesium.Matrix4()); ``` 在上面的代码中,首先加载模型并将其放置在地球表面上。然后,我们创建一个Matrix3对象,表示绕Z轴旋转45度的旋转矩阵。接下来,我们将Matrix3对象与一个零位移向量组合成一个Matrix4变换矩阵。最后,我们将这个Transform对象与模型的当前变换矩阵相乘,以将旋转应用于模型。 您可以根据需要更改旋转角度和轴向,以获得所需的旋转效果。 ### 回答2: Cesium是一种基于Web的3D地理信息系统框架,可以在浏览器中呈现高度交互的3D地球模型Cesium可以实现模型旋转功能,使用户能够在地球模型上自由改变模型的方向和角度。 通过Cesium的API,我们可以使用旋转方法来实现模型旋转。首先,我们需要加载并显示一个3D模型。然后,使用Cesium提供的旋转函数来改变模型的朝向。 在Cesium中,可以使用以下代码来旋转3D模型: ```javascript // 获取模型实例 var entity = viewer.entities.getById("模型的ID"); // 设置模型的方向 entity.orientation = new Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_Z, Cesium.Math.toRadians(角度)); // 更新模型状态 entity.modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(entity.position.getValue(viewer.clock.currentTime), entity.orientation, Cesium.Ellipsoid.WGS84, Cesium.Transforms.localFrameToFixedFrameGenerator('north', 'west')); ``` 在代码中,我们首先通过实体ID获取到相应的模型实例。然后,使用Cesium提供的Quaternion类来定义模型的朝向,其中`Cesium.Cartesian3.UNIT_Z`表示绕Z轴旋转,`Cesium.Math.toRadians(角度)`表示旋转的角度。最后,更新模型的状态使其适应新的方向。 通过上述的代码,我们可以在Cesium实现模型旋转效果。用户可以通过相关控制来操作模型旋转,让其在地球模型上呈现不同的方向和角度,从而实现多样化的展示和交互效果。 ### 回答3: Cesium 是一个用于构建三维地球和宇宙场景的 JavaScript 库。它可以实现模型旋转功能,让用户能够以各种角度观察和探索模型。 在 Cesium 中,模型旋转的基本原理是通过改变模型的方向来实现。首先,我们需要加载模型,并设置其初始的位置和方向。然后,通过修改模型旋转矩阵或四元数,可以实现模型旋转操作。 对于旋转矩阵,可以通过修改矩阵的旋转部分来改变模型的方向。Cesium 提供了一些内置的函数和工具,用于创建和操作旋转矩阵。通过改变矩阵的旋转参数,可以实现沿不同轴旋转的效果。 另一种实现模型旋转的方法是使用四元数。四元数也可以表示旋转,它是一个包含四个实数的向量。在 Cesium 中,可以使用内置的函数来创建和修改四元数。通过改变四元数的旋转参数,可以实现模型旋转效果。 在 Cesium 中,可以通过手动控制模型旋转参数来实现旋转效果,例如通过鼠标或触摸事件来改变旋转角度。此外,还可以通过调整旋转速度和增量来控制旋转的灵敏度。 总而言之,Cesium 提供了丰富的功能和工具,用于实现模型旋转效果。无论是使用旋转矩阵还是四元数,都可以通过改变模型的方向参数来实现模型旋转。这样,用户可以以不同的角度观察和探索模型,从而增强交互性和视觉效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值