01.cesium中三维模型加载

本文介绍了如何使用Cesium库在Web应用中配置和加载3D场馆模型,包括设置经纬度、角度、模型URL和加载流程。还展示了如何创建一个封装类来简化操作并处理模型加载回调。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.先看展示示例

在这里插入图片描述

实现步骤

1.配置好参数

  CesiumMapRef.value.addVenue({
    id: "",
    createTime: "",
    modifyTime: "",
    name: "国博中心",
    angle: 30,
    longitude: 106.537751,
    latitude: 29.721438,
    scale: 1.0,
  });
const addVenue = () => {
	 let url = "/data/gbzx-01.gltf"; //模型地址
      options.url = url;
      options.locking = true;
      options.isBuilding = true;
      new venueObj(this.cesium_viewer, options);
}

2.创建3D场馆封装类

export default class venueObj {
  constructor(viewer, options) {
    this.viewer = viewer;

    this.callback = options.callback || function () {};
    //初始化
    this.init(options);
  }
  init(options) {
    if (!options) return false;

    /**
     * 鼠标事件
     */
    // this.handler = new Handler(this.viewer);
    
	//设置好经纬度
    const position = Cesium.Cartesian3.fromDegrees(
      options.longitude,
      options.latitude
    );
	
    const heading = Cesium.Math.toRadians(options.angle); //水平旋转
    const pitch = Cesium.Math.toRadians(0);
    const roll = Cesium.Math.toRadians(0);
    let headingPitchRoll = new Cesium.HeadingPitchRoll(heading, pitch, roll);
    let orientation = Cesium.Transforms.headingPitchRollQuaternion(
      position,
      headingPitchRoll
    );

    // console.log('[ options ] >', options)
    var entity = this.viewer.entities.add({
      locking: options.locking,
      isBuilding: options.isBuilding,
      name: options.name,
      position: position,
      orientation: orientation,

      model: {
        uri: options.url,
        scale: options.scale || 1,
        show: true,
      },
    });
    // viewer.scene.primitives.add(entity);

    // 模型加载
    var _this = this;
    Cesium.requestAnimationFrame(function () {
      _this.viewer.dataSourceDisplay.defaultDataSource._visualizers
        .reduce(function (a, b) {
          // console.log(`model iooikjkoj has loaded.`, a , b);
          return a instanceof Cesium.ModelVisualizer ? a : b;
        })
        ._modelHash[entity.id].modelPrimitive.readyPromise.then(function () {
          console.log(`model ${options.name} has loaded.`);
          _this.callback("loaded");
        });
    });
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值