Cesium中添加entitie模型,实现贴地。

1.Cesium中添加entitie模型,实现贴地。

2. 添加模型

const createModel = (url) => {
	const entity = viewer.entities.add({
		name: '这是一个模型',
		position: Cesium.Cartesian3.fromDegrees({ -123.0744619, 44.0503706, 100 }),
		model: {
			uri: url,
			minimumPixelSize: 128,
			maximumScale: 2000,
			heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
		}
	})
};

这里的url(模型的链接),我是用node.js。express框架实现的静态资源
node.js

const express = require('express')
const app = express()
const port = 5555

app.get('/', (req, res) => {
    res.send('Hello World!')
})
app.use((req, res, next) => {
    //设置请求头
    res.set({
        'Access-Control-Allow-Credentials': true,
        'Access-Control-Max-Age': 1728000,
        'Access-Control-Allow-Origin': req.headers.origin || '*',
        'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
        'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
        'Content-Type': 'application/json; charset=utf-8'
    })
    req.method === 'OPTIONS' ? res.status(204).end() : next()
})

app.use(express.static('./static'))
app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

这里的static是存储静态资源的
2.实现贴地

heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
const entity = viewer.entities.add({
		name: '这是一个模型',
		position: Cesium.Cartesian3.fromDegrees({ -123.0744619, 44.0503706, 100 }),
		model: {
			uri: url,
			minimumPixelSize: 128,
			maximumScale: 2000,
			heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
		}
	})
viewer.trackedEntity = entity;

在这里插入图片描述
完整代码

<template>
  <div id="cesiumContainer"></div>
</template>
<script setup>
import { onMounted } from "vue";
import { Viewer, ArcGisMapServerImageryProvider, HeightReference, Cartographic, Transforms, Color,PointGraphics, HeadingPitchRoll, ScreenSpaceEventType, Math, UrlTemplateImageryProvider, createWorldTerrain, Cartesian3, ScreenSpaceEventHandler } from 'cesium';
// import ScreenSpaceEventHandler from "cesium/Source/Core/ScreenSpaceEventHandler";
const geogle = new ArcGisMapServerImageryProvider({
  url: 'http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
})
// console.log(, 'createWorldTerrain');
onMounted(() => {
  const viewer = new Viewer('cesiumContainer', {
    imageryProvider: geogle,
    selectionIndicator: false,
    shadows: true,
    shouldAnimate: true,
    animation: true, // 是否显示动画组件
    homeButton: false, // 是否显示Home按钮
    fullscreenButton: false, // 是否显示全屏
    baseLayerPicker: false, // 是否显示图层选择控件
    geocoder: false, // 是否显示地名查找控件
    sceneModePicker: false, // 是否显示投影方式选择
    navigationHelpButton: false, // 是否显示帮助按钮
    infoBox: true, // 是否显示点击要素之后显示信息
    requestRenderMode: true, // 启用请求渲染模式
    sceneMode: 3, //初始场景模式 1 2D模式 2 2D循环模式 3 3D模式  Cesium.SceneMode
    fullscreenElement: document.body, //全屏时渲染的HTML元素 暂时没发现用处//去除版权信息
    scene3DOnly: false, //每个几何实例将只能以3D渲染以节省GPU内存
    shouldAnimate: true,
    terrainProvider: new createWorldTerrain({
      requestVertexNormals: true,
      requestWaterMask: true
    })
  });
  viewer.cesiumWidget.creditContainer.style.display = "none";
  viewer.imageryLayers.addImageryProvider(new UrlTemplateImageryProvider({
    url: "http://webst02.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8&ltype=4",
    // url: 'https://wprd03.is.autonavi.com/appmaptile?&x=6666&y=3430&z=13&lang=zh_cn&size=2&scl=1&style=8&ltype=4',
    layer: "tdtAnnoLayer",
    style: "default",
    format: "image/jpeg",
    tileMatrixSetID: "GoogleMapsCompatible"
  }));
  viewer.scene.globe.depthTestAgainstTerrain = true
  // viewer.scene.globe.enableLighting = true;
  const createModel = (height, url) => {
    // console.log(height, 'height')
    // console.log(, '士大夫')
    viewer.entities.removeAll(); // 清空所以的entitie
    const position = Cartesian3.fromDegrees(
      // -123.0744619,
      // 44.0503706,
      98.644266,28.899404,
      100
    );
    const heading = Math.toRadians(135); // 朝向
    const pitch = 0; // 高度
    const roll = 0; // 滚动
    const hpr = new HeadingPitchRoll(heading, pitch, roll);
    const orientation = Transforms.headingPitchRollQuaternion(
      position,
      hpr
    ); // 取向  
    const d = Cartesian3.fromDegrees(-123.0744619,44.0503706);
    // 初始化实例
    const entity = viewer.entities.add({
      name: url,
      position: position, 
      orientation: orientation,
      model: {
        uri: url,
        minimumPixelSize: 128,
        maximumScale: 20000,
        // disableDepthTestDistance:99000000
        heightReference: HeightReference.CLAMP_TO_GROUND
      },
      point: new PointGraphics({
        show: true,
        pixelSize: 10,
        heightReference: HeightReference.CLAMP_TO_GROUND,
        disableDepthTestDistance: 99000000,
      }),
      label: {
        text: '是的',
        heightReference: HeightReference.CLAMP_TO_GROUND,
        disableDepthTestDistance: 99000000
      }
      // box: {
      //   dimensions: new Cartesian3(5.0, 2.0, 0),
      //   fill: false,
      //   outline: true,
      //   outlineColor: Color.YELLOW
      // }
    });
    // console.log(entity._position._value, 'entityentity')
    const positions = [
      new Cartographic(-1.31968, 0.69887),
      new Cartographic(-1.10489, 0.83923)
    ];
    viewer.trackedEntity = entity;
    const scene = viewer.scene;
    // const promise = scene.sampleHeightMostDetailed(entity._position._value);
    // console.log(promise, 'promise')
    // promise.then(res => {
    //   console.log(res, 'ds');
    // }, rej => {
    //   console.log(rej, 'rej')
    // })
  }
  // const getModelHeight = () => {
  //   const handler = ScreenSpaceEventHandler(viewer.canvas);
  //   handler.setInputAction((evt) => {
  //     const scene = viewer.scene;
  //     console.log(scene, 'scene');
  //   }, ScreenSpaceEventType.LEFT_CLICK)
  // }
  createModel(10, 'http://localhost:5555/CesiumMilkTruck.glb');
  // getModelHeight();

})
</script>
<style>
html,
body,
#cesiumContainer {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
</style>
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Cesium是一种基于WebGL的开源地理空间软件开发工具包,可以用于创建高性能的3D地球和地理信息系统。Cesium开发团队不仅提供了功能强大的地球模型,还提供了一系列实用工具和模块,其包括广告牌贴地模型。 广告牌贴地模型是一种在地球表面上展示2D图像的方式。传统的2D图片在地球模型上进行贴图时,会在观察角度改变时出现扭曲的情况。而通过Cesium的广告牌贴地模型,可以实现解决这个问题。 使用Cesium制作广告牌贴地模型非常简单。首先,我们需要准备一张2D图片或者图标,可以是产品logo、企业标识、地标建筑等。接下来,通过Cesium提供的API,我们可以将这个2D图片放置在地球表面上的特定位置。 在Cesium,贴图的位置和方向与地球表面的实际坐标位置相对应。这意味着我们可以根据需要将广告牌贴图放置在地球表面的任何位置。当观察者改变视角或者缩放地图时,Cesium会自动调整广告牌贴图的朝向和大小,以保证广告牌在不同视角下的合理展示。 通过Cesium的广告牌贴地模型,企业可以将自己的品牌或产品标识形象地展示在全球地图上,提高品牌的曝光度和知名度。同时,对于旅游、房地产等行业,也可以将景点、房产项目等相关信息直观地展示在地球模型上。 总之,Cesium的广告牌贴地模型为我们提供了一种简单而强大的方法,可以在地球模型上展示2D图片,实现了视角变化时贴图的自动调整。这为各行业的品牌推广和地理信息展示提供了全新的思路和可能性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值