Cesium ——(Entity)實(shí)體與 實(shí)體中材質(zhì)(material)

定義簡(jiǎn)介

Entity是Cesium中的一個(gè)重要概念御毅,它代表了在三維場(chǎng)景中的實(shí)體對(duì)象。這些實(shí)體可以是地球上的點(diǎn)嘁字、線夫椭、面掸掸,也可以是飛機(jī)、汽車蹭秋、船舶等動(dòng)態(tài)物體扰付。

屬性

Entity具有豐富的屬性,包括位置仁讨、方向羽莺、速度、外觀等洞豁。通過設(shè)置這些屬性盐固,開發(fā)者可以精確地控制實(shí)體在三維場(chǎng)景中的表現(xiàn)形式。每一個(gè)Entity都存在一個(gè)id丈挟,name刁卜,position,orientation曙咽,還有與之關(guān)聯(lián)的實(shí)體蛔趴。

關(guān)聯(lián)實(shí)體

1.png

相關(guān)Entity的方法實(shí)例

添加屬性

viewer.entities.add()

刪除指定id的實(shí)體

viewer.entities.removeById(1)
這個(gè)id就是實(shí)體entity的id

刪除全部實(shí)體

viewer.entities.removeAll()

實(shí)體中材質(zhì)(material)

Material在Cesium中負(fù)責(zé)定義實(shí)體的外觀特性,如顏色例朱、透明度孝情、紋理等。

  1. ColorMaterial(顏色材質(zhì)): 允許開發(fā)者定義實(shí)體的顏色洒嗤,從而為實(shí)體添加生動(dòng)的色彩箫荡。
entity.material = new Cesium.ColorMaterialProperty(Cesium.Color.RED);
  1. ImageMaterial(圖片材質(zhì)): 允許將圖片應(yīng)用到實(shí)體上,以展示詳細(xì)的紋理渔隶。
entity.material = new Cesium.ImageMaterialProperty({
  image: 'path/to/texture.png',
});
  1. CheckerboardMaterial(棋盤格材質(zhì)): 創(chuàng)建一個(gè)有趣的棋盤格效果羔挡,使實(shí)體更具視覺吸引力。
entity.material = new Cesium.CheckerboardMaterialProperty({
  evenColor: Cesium.Color.WHITE,
  oddColor: Cesium.Color.BLACK,
  repeat: new Cesium.Cartesian2(8, 8),
});

具體實(shí)現(xiàn)代碼


<template>
 <div class="hello">
    <div id="cesiumContainer"></div>
    <section class="mapList">
      <header class="mapList-header">Entity</header>
      <article class="mapList-center">
        <div class="mapList-center-list">
          <el-button type="primary" @click="handelParentShow">加載Box</el-button>
          <el-button type="primary" @click="handelParentHide">隱藏Box</el-button>
        </div>
        <div class="mapList-center-list">
          <el-button type="primary" @click="handelTrajectoryShow">加載Model</el-button>
          <el-button type="primary" @click="handelTrajectoryHide">關(guān)閉Model</el-button>
        </div>
        <div class="mapList-center-list">
          <el-button type="primary" @click="handelPolygonShow">加載Polygon</el-button>
          <el-button type="primary" @click="handelPolygonHide">關(guān)閉Polygon</el-button>
        </div>
        <div class="mapList-center-list">
          <el-button type="primary" @click="handelWallShow">加載Wall</el-button>
          <el-button type="primary" @click="handelWallHide">關(guān)閉Wall</el-button>
        </div>
        <div class="mapList-center-list">
          <el-button type="primary" @click="handelLabelShow">加載標(biāo)簽</el-button>
          <el-button type="primary" @click="handelLabelHide">關(guān)閉標(biāo)簽</el-button>
        </div>
        <div class="mapList-center-list">
          <el-button type="primary" @click="handel3DTilesShow">加載3DTiles</el-button>
          <el-button type="primary" @click="handel3DTilesHide">關(guān)閉3DTiles</el-button>
        </div>
      </article>
    </section>
  </div>
</template>
<script> 
import { onMounted } from "vue"; 
const Cesium = window.Cesium; 
export default {
 name: "CesiumViewer", 
 props: {},
 setup() {
   onMounted(() => {
     try {
       initMap(Cesium);
     } catch (error) {
       console.log(error);
     }
   });
   /**
     * @description: 加載Box
     * @return {*}
     */
    const handelParentShow = ()=>{ 
      for (let i=0;i<3;i++){
      const height = 100.0 + (200.0 * i);
      window.viewer.entities.add({
              id:i,
              parent : new Cesium.Entity(),
              position : Cesium.Cartesian3.fromDegrees(104.1, 30.6, height),
              box : {
                  dimensions : new Cesium.Cartesian3(90.0, 90.0, 90.0),
                  material : Cesium.Color.fromRandom({alpha : 1.0})
              }
          })
      }
    }
    /**
     * @description: 隱藏Box
     * @return {*}
     */   
    const handelParentHide = () =>{
      window.viewer.entities.removeAll()
    } 
    /* const handelParentHide = ()=>{ 
      window.viewer.entities.removeById(1)  
    } */
   /**
    * @description: 加載軌跡動(dòng)畫
    * @return {*}
    */    
   const handelTrajectoryShow = () =>{
    /* const position = Cesium.Cartesian3.fromDegrees(104.1, 30.6, 200)
    const property = new Cesium.SampledPositionProperty();
    console.log(property);
    const start = Cesium.JulianDate.fromDate(new Date());
    property.addSample(start.clone(), position)
    window.viewer.entities.add(new Cesium.Entity({
            orientation: new Cesium.VelocityOrientationProperty(property),
            model: new Cesium.ModelGraphics({
                uri: '/static/SampleData/gltf/坦克/gltf2.gltf' 
            }),
            position: position,
        })); */
    const positions = new Cesium.SampledPositionProperty();
    const startTime = window.viewer.clock.currentTime;
    positions.addSample(startTime, Cesium.Cartesian3.fromDegrees(104.1, 30.6, 800));
    const stopTime = Cesium.JulianDate.addSeconds(startTime,30, new Cesium.JulianDate());
    positions.addSample(stopTime, Cesium.Cartesian3.fromDegrees(104.11, 30.6, 800))
    const position = Cesium.Cartesian3.fromDegrees(104.1, 30.6, 800) 
    // 設(shè)置飛翔角度
    const headingPitchRoll = new Cesium.HeadingPitchRoll(0, Cesium.Math.toRadians(5), 0);
    const orientation = Cesium.Transforms.headingPitchRollQuaternion(position, headingPitchRoll);
    window.viewer.entities.add({
            id: "tk",
            availability: new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
                    start: startTime,
                    stop: stopTime
            })]),
            position: positions,
            orientation:orientation,
            model:new Cesium.ModelGraphics({
              uri: '/static/SampleData/gltf/坦克/gltf2.gltf', 
              silhouetteColor: Cesium.Color.RED,
              silhouetteSize:2.0,
              color:Cesium.Color.RED,
            }), 
            path: {
              resolution: 0.2,
              material: new Cesium.PolylineGlowMaterialProperty({
                glowPower: 0.1,
                color: Cesium.Color.RED
              }),
              width: 5
            }
        });
    window.viewer.trackedEntity = window.viewer.entities.getById("tk");
   }
   /**
    * @description: 關(guān)閉軌跡動(dòng)畫
    * @return {*}
    */   
   const handelTrajectoryHide = () =>{
     window.viewer.entities.removeById('tk') 
   }
   /**
    * @description: 加載Polygon
    * @return {*}
    */  
   const handelPolygonShow = () =>{ 
   /*  let range = [121.1911,31.53762,121.1921,30.87844,121.9246,30.83999,121.8824,31.34811];
    let positions = Cesium.Cartesian3.fromDegreesArray(range); 
    let holeRange1 = [121.2671,31.4566,121.2382,31.3481,121.3659,31.3934]; 
    let hole1 = { 
        positions: Cesium.Cartesian3.fromDegreesArray(holeRange1) 
    };  */
    window.viewer.entities.add({
         id:'Polygon',
         parent : new Cesium.Entity(),
         position : Cesium.Cartesian3.fromDegrees(112, 40, 300),
         /* polygon: {
          hierarchy: { 
              positions,
              holes: [hole1]  
          },
          height:500,
          extrudedHeight:500,
          material: new Cesium.ImageMaterialProperty ({
                          image:"static/SampleData/pbr/MaterialJson/M_Brick_Clay_Old_BaseTexMap.png"
                      }),
          outline:true, 
          outlineColor:Cesium.Color.WHITE,
          outlineWidth:2.0
        } */
        polygon: {
          hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray(
            [104.1, 30.6,104.2, 30.6,104.2, 30.7,104.1, 30.7]
          )),
          height:300,
          extrudedHeight:100,
          material: new Cesium.ImageMaterialProperty ({
                          image:"static/SampleData/pbr/MaterialJson/MI_Pebble01_BaseTexMap.png"
                      }),
          outline:true, 
          outlineColor:Cesium.Color.WHITE,
          outlineWidth:2.0
        }
     }) 
    window.viewer.flyTo(window.viewer.entities.getById("Polygon"));
   }
   /**
    * @description: 關(guān)閉Polygon
    * @return {*}
    */   
   const handelPolygonHide = () =>{
     window.viewer.entities.removeById('Polygon') 
   }
   /**
    * @description: 加載wall
    * @return {*}
    */   
   const handelWallShow = ()=>{
    window.viewer.entities.add({
        id: "Wall",
        position : Cesium.Cartesian3.fromDegrees(112, 40, 300),
        wall:{
            positions:Cesium.Cartesian3.fromDegreesArray([112, 40, 112.001, 40]),
            maximumHeights:[200,200],
            material: new Cesium.ImageMaterialProperty ({
                          image:"static/SampleData/pbr/MaterialJson/M_Brick_Clay_Old_BaseTexMap.png"
                      }),
        },
        shadows:true 
    })
    window.viewer.flyTo(window.viewer.entities.getById("Wall"));
   }
   /**
    * @description: 關(guān)閉Wall
    * @return {*}
    */   
   const handelWallHide = () =>{
     window.viewer.entities.removeById('Wall') 
   }
   /**
    * @description: 加載標(biāo)簽
    * @return {*}
    */   
   const handelLabelShow =()=>{
    window.viewer.entities.add({
        id: "label",
        position : Cesium.Cartesian3.fromDegrees(104.1, 30.6, 500),
        label : {
          text : "DIST",
          font:"48px sans-serif",
          style:Cesium.LabelStyle.FILL_AND_OUTLINE,
          fillColor:Cesium.Color.RED,
          outlineColor:Cesium.Color.YELLOW,
          outlineWidth:2.0,
          showBackground:true,
          backgroundColor:new Cesium.Color(255, 255, 0, 0.8),
          horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
          verticalOrigin:Cesium.VerticalOrigin.BOTTOM,
          translucencyByDistance:new Cesium.NearFarScalar(1.5e5, 1.0, 1.5e7, 0.0),
          pixelOffset : new Cesium.Cartesian2(0.0, -20),
          pixelOffsetScaleByDistance : new Cesium.NearFarScalar(1.0e3, 1.0, 1.5e6, 0.0),
          disableDepthTestDistance:Number.POSITIVE_INFINITY 
        }, 
    })
   }
   /**
    * @description: 關(guān)閉標(biāo)簽
    * @return {*}
    */   
   const handelLabelHide = () =>{
     window.viewer.entities.removeById('label') 
   }
   /**
    * @description: 
    * @return {*}
    */   
   const handel3DTilesShow=()=>{
      const tilesModel = window.viewer.scene.primitives.add(
        new Cesium.Cesium3DTileset({
          url: "static/SampleData/Scene/testm3DTiles.json"
        })
      );
      tilesModel.readyPromise
        .then(currentModel => {
          // 定位到模型
          window.viewer.zoomTo(
            currentModel,
            new Cesium.HeadingPitchRange(
              0.5,
              -0.2,
              currentModel.boundingSphere.radius * 1.0
            )
          );
        })
        .otherwise(error => {
          console.log(error);
        });
   }
   /**
    * @description: 關(guān)閉kml
    * @return {*}
    */   
   const handel3DTilesHide=()=>{
    window.viewer.dataSources.removeAll()
   }
   return {
    handelParentShow,handelParentHide,
    handelTrajectoryShow,handelTrajectoryHide,
    handelPolygonShow,handelPolygonHide,
    handelWallShow,handelWallHide,
    handelLabelShow,handelLabelHide,
    handel3DTilesShow,handel3DTilesHide
  };
 },
};
/**
 * @description: 初始化球體
 * @param {*} Cesium
 * @return {*}
 */
const initMap = (Cesium) => {
  // alpha 默認(rèn)值為 false,與標(biāo)準(zhǔn) WebGL 默認(rèn)值 true 相比
  const viewer = new Cesium.Viewer("cesiumContainer", {  
    animation: false, // 是否顯示動(dòng)畫控件
    shouldAnimate: true,
    homeButton: false, // 是否顯示Home按鈕
    fullscreenButton: false, // 是否顯示全屏按鈕
    baseLayerPicker: false, // 是否顯示圖層選擇控件
    geocoder: false, // 是否顯示地名查找控件
    timeline: false, // 是否顯示時(shí)間線控件
    sceneModePicker: false, // 是否顯示投影方式控件
    navigationHelpButton: false, // 是否顯示幫助信息控件
    infoBox: false, // 是否顯示點(diǎn)擊要素之后顯示的信息
    scene3DOnly: false, // 每個(gè)幾何實(shí)例將只能以3D渲染以節(jié)省GPU內(nèi)存
    sceneMode: 3, // 初始場(chǎng)景模式 1 2D模式 2 2D循環(huán)模式 3 3D模式  Cesium.SceneMode
    fullscreenElement: document.body, // 全屏?xí)r渲染的HTML元素 暫時(shí)沒發(fā)現(xiàn)用處
    skyAtmosphere: false,// 關(guān)閉地球光環(huán)
    requestRenderMode: true, // 啟用請(qǐng)求渲染模式
    orderIndependentTranslucency: false, // 去掉大氣層黑圈
    contextOptions: {
      webgl: {
        alpha: true
      }
    },
    imageryProvider: new Cesium.UrlTemplateImageryProvider({
      url:
        "https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}"
    })
  });
  window.viewer = viewer
  /* // 獲取圖層集合
  const imageryLayers = viewer.imageryLayers;
  // 將圖像轉(zhuǎn)成圖層
  const UrlTemplate = new Cesium.UrlTemplateImageryProvider({
    url:
      "http://webst02.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8"
  });
  // 添加圖層
  imageryLayers.add(new Cesium.ImageryLayer(UrlTemplate)); */
  // 隱藏版權(quán)
  viewer._cesiumWidget._creditContainer.style.display = "none";
  const boundingSphere =new Cesium.BoundingSphere(
       new Cesium.Cartesian3.fromDegrees(104.1, 30.6, 800),
       1000
     );
  // 定位到初始位置
  // 將相機(jī)移到當(dāng)前視圖包含所提供的包圍球的位置婉弹。
  viewer.camera.flyToBoundingSphere(boundingSphere, {
    // 動(dòng)畫睬魂,定位到初始位置的過渡時(shí)間终吼,設(shè)置成0镀赌,就沒有動(dòng)畫
    duration: 0
  });
  // 隱藏版權(quán)
  viewer._cesiumWidget._creditContainer.style.display = "none";
  // 數(shù)據(jù)源集合
  viewer.dataSources.add(new Cesium.KmlDataSource.load('static/SampleData/kml/登頂四姑娘山三峰.kml'),
     {
         camera: viewer.scene.camera,
         canvas: viewer.scene.canvas
     });
}


</script>
<style scoped >
#cesiumContainer {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  margin: 0;
  overflow: hidden;
  padding: 0;
  font-family: sans-serif;
}
.mapList{
   position: absolute;
   z-index: 2;
   width: 15rem;
   height: 100%;
   background-color: #fff;
   top: 0;
   left: 0;
   padding: 0.5rem;
}
.mapList-header{
  height: 3rem;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.mapList-center-list{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 3rem;
}
</style> 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市际跪,隨后出現(xiàn)的幾起案子商佛,更是在濱河造成了極大的恐慌,老刑警劉巖姆打,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件良姆,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡幔戏,警方通過查閱死者的電腦和手機(jī)玛追,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闲延,“玉大人痊剖,你說我怎么就攤上這事±萘幔” “怎么了陆馁?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)合愈。 經(jīng)常有香客問我叮贩,道長(zhǎng),這世上最難降的妖魔是什么佛析? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任益老,我火速辦了婚禮,結(jié)果婚禮上寸莫,老公的妹妹穿的比我還像新娘捺萌。我一直安慰自己,他們只是感情好储狭,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布互婿。 她就那樣靜靜地躺著,像睡著了一般辽狈。 火紅的嫁衣襯著肌膚如雪慈参。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天刮萌,我揣著相機(jī)與錄音驮配,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛壮锻,可吹牛的內(nèi)容都是我干的琐旁。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼猜绣,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼灰殴!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起掰邢,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤牺陶,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后辣之,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體掰伸,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年怀估,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了狮鸭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡多搀,死狀恐怖歧蕉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情酗昼,我是刑警寧澤廊谓,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站麻削,受9級(jí)特大地震影響蒸痹,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜呛哟,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一叠荠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧扫责,春花似錦榛鼎、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至苏揣,卻和暖如春黄鳍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背平匈。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工框沟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留藏古,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓忍燥,卻偏偏與公主長(zhǎng)得像拧晕,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子梅垄,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容