CESIUM例子学习(一)——动态模型加载

    技术2024-02-20  93

    前言

    一直在学习cesium,时断时续地用它,但感觉哪里不对,什么都了解一点,但什么没有深入地去研究,都是浮于表面。于是下定决心:从头开始,从cesium的sandcastle例子开始,系统地学一下cesium。并把它记录下来,以便后面用时能很快找到。

    一、场景与动态模型加载

    把基本框架搭起来之后,就开始第一个例子。如下图(发动机叶片是转动的):

    viewer配置如下图:

    因为模型的发动机叶片是转动的,为了使叶片转动起来,shouldAnimate: true。

    二、模型调整

    这里的模型调整不是指模型本身的动画。模型在三维场景中除了有位置之外,还有模型的姿态。比如头朝哪边,左右倾斜多少度,模型缩放等等,代码如下:

    1、模型姿态

    function createModel (url, x, y, height) { viewer.entities.removeAll(); var position = Cesium.Cartesian3.fromDegrees(x, y, height); var heading = Cesium.Math.toRadians(135); var pitch = 0; var roll = 0; var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);// var orientation = Cesium.Transforms.headingPitchRollQuaternion( position, hpr ); var entity = viewer.entities.add({ name: url, position: position, orientation: orientation, model: { uri: url, minimumPixelSize: 128, maximumScale: 20000, }, }); viewer.trackedEntity = entity; }

    上面代码中,

     position设置entity的位置;         orientation: 设置entity的姿态;

    需要注意这一行: var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);

    cesium API中是这样说的:A rotation expressed as a heading, pitch, and roll. Heading is the rotation about the negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about the positive x axis.

    其中说到Heading是绕Z轴,Pitch是绕Y轴,而Roll是绕X轴。这里的x,y,z轴指的是模型坐标系中的x,y,z轴。而不是cesium中笛卡儿坐标系的x,y,z。旋转中心是建模时的模型中心。

    2、缩放

    模型缩放很简单,只要找到模型,设置模型scale属性即可,但要注意,netity并不一个只为模型服务的单一类,可以把它理解成容器,大多数的可视化要素都可以放里面,所以要找到模型,就需要找到它的模型属性,即entity.model,这就是模型。代码如下:

    entity.model.scale = 10 //模型放大10倍

    三、视点锁定

    在学习加载模型过程中,经常会碰到一个问题,就是运行没有报错,模型已经加载,但是就是看不到模型在哪里!Cesium提供了一个模型锁定的方法。即:

    viewer.trackedEntity = entity;

    这样就可以,这时视角不支持平移,只能放大和缩小。视点始终锁定在模型上,不用满世界去找模型。

     

     

    Processed: 0.020, SQL: 9