Cesium基础教程篇七
文章目录
- Cesium基础教程篇七
- 前言
- 一、Entity的介绍与使用
- 二、几何图形的简要学习
- 三、DataSource的管理方式
- 四、Entity的简单材质介绍
- 1. cesium内置材质:我们将 Color 类型的值设置给实体对象几何图形的material属性,material属性用于控制实体对象几何图形的外观样式。除了 [Color](https://cesium.com/learn/cesiumjs/ref-doc/Color.html) 以外,material属性还支持实现了[MaterialProperty ](https://cesium.com/learn/cesiumjs/ref-doc/MaterialProperty.html) 接口的类的实例。
- 1.1:[ColorMaterialProperty](https://cesium.com/learn/cesiumjs/ref-doc/ColorMaterialProperty.html):颜色材质,前面我们直接为material属性设置一Color值,最后这个Color值会转换成ColorMaterialProperty类型。
- 1.2:[ImageMaterialProperty](https://cesium.com/learn/cesiumjs/ref-doc/ImageMaterialProperty.html):图片材质(支持网络图片、图片元素、Canvas元素、视频元素)
- 1.3:[GridMaterialProperty](https://cesium.com/learn/cesiumjs/ref-doc/GridMaterialProperty.html):格网材质
- 1.4:[StripeMaterialProperty](https://cesium.com/learn/cesiumjs/ref-doc/StripeMaterialProperty.html):条纹材质
- 1.5:[PolylineGlowMaterialProperty](https://cesium.com/learn/cesiumjs/ref-doc/PolylineGlowMaterialProperty.html):发光线材质( 仅用于 PolylineGraphic )
- 1.6:[PolylineOutlineMaterialProperty](https://cesium.com/learn/cesiumjs/ref-doc/PolylineOutlineMaterialProperty.html):边线材质( 仅用于 PolylineGraphic )
- 2. 自定义材质
- 总结
前言
Cesium官网:官网链接
Cesium 是一个开源的、高性能的 3D 地球和地图可视化平台,它允许开发者创建基于 Web 的、交互式的地理空间应用程序。Cesium 的核心是一个 WebGL 驱动的引擎,它能够渲染大规模的地理数据集,支持高精度的地球模型和动态数据可视化。
一、Entity的介绍与使用
Entity 是Cesium中用于绘制几何图形的重要接口,比如在地球上绘制我们最熟悉的点、线、面。但是Cesium作为一个3D GIS引擎,它还内置了大量的3D图形对象,Entity类是用于绘制这些图形的一个重要接口不是唯一接口,Cesium还有Primitive和DrawCommand用于绘制图形)。
- Entity:官网沙盘地址
- 官网API:Entity API
api中介绍的大部分图形都在红框中,先大致了解些当中的几个主要参数
availability:有效性,此属性用于设置Entity对象的显示时间,即在指定的时间内显示。
position: 位置,设置Entity实体的位置,笛卡尔坐标
orientation :方向,用于设置实体对象的方向,比如添加一个模型后,这个模型朝东还是朝西
viewFrom :此属性用于锁定实体视角时的偏移量 Cartesian3,比如要将相机视角锁定到一个正在行驶的车辆模型上,可以通过viewer.trackedEntity 进行设置,viewFrom用于设置锁定视角时的偏移量。
parent: 设置Entity的父级Entity,表示Entity可以嵌套使用。
- 使用方式:创建的Entity要想在场景中被渲染出来,需要添加到viewer.entitites这个集合中。
let entity=new Cesium.Entity({
position:position,
point:{
pixelSize:20,
color:Cesium.Color.RED
}
})
viewer.entities.add(entity);
二、几何图形的简要学习
1. PointGraphic 点
1.1 官网API
1.2 参数介绍
show: 是否显示
pixelSize: 像素大小,即这个点显示的大小,单位为像素,比如分别设置10,和20对比
heightReference:
NONE:绝对高度,即从椭球面为0开始算
CLAMP TO GROUND:贴模型或者贴地形或者都贴,有三种类型
RELATIVE TO GROUND:高度相对于地形和模型
CLAMP TO TERRAIN:贴在地形上
RELATIVE TO TERRAIN :高度相对于地形
CLAMP TO 3D TILE:贴在模型上
RELATIVE_T0_3D_TILE:高度相对模型
outlineColor:边线颜色
outlineWidth: 边线宽度(像素)
scaleByDistance: 根据距离缩放(距离指相机到点的距离new Cesium.NearFarScalar(near, nearValue, far, farValue),远近坐标的缩放比例的设置)
translucencyByDistance: 根据距离设置透明度(距离指相机到点的距离new Cesium.NearFarScalar(near, nearValue, far, farValue),远近坐标的透明度的设置)
distanceDisplayCondition: 根据距离显示隐藏(距离指相机到点的距离)
disableDepthTestDistance: 深度测试的距离,可以用于屏蔽遮挡(旋转时多少距离内可以不被其他场景遮挡住)
该章节仅介绍点的知识,后面有需要私信再出点线面体广告牌墙体等
三、DataSource的管理方式
我们可以通过 CustomDataSource 类来对添加到场景中的实体对象进行分类控制(比如统一控制显隐状态)前面我们创建的Entity都是通过viewer.entities.add方法添加到场景中进行渲染的,当我们添加到viewer.entities中的实体对象比较多时想要控制部分实体的显隐会变得比较麻烦。
const dataSource = new Cesium.CustomDataSource('data');
const entity = dataSource.entities.add({
position : Cesium.Cartesian3.fromDegrees(x, y, z),
billboard : {
image : 'xxx.png'
}
});
viewer.dataSources.add(dataSource);
然后通过dataSource.show=visible去控制显示隐藏比entities去控制更方便
四、Entity的简单材质介绍
1. cesium内置材质:我们将 Color 类型的值设置给实体对象几何图形的material属性,material属性用于控制实体对象几何图形的外观样式。除了 Color 以外,material属性还支持实现了MaterialProperty 接口的类的实例。
1.1:ColorMaterialProperty:颜色材质,前面我们直接为material属性设置一Color值,最后这个Color值会转换成ColorMaterialProperty类型。
let position = Cesium.Cartesian3.fromDegrees(-75.59670696331766, 40.0387958759308, 51);
let entity = new Cesium.Entity({
position: position,
plane: {
plane: new Cesium.Plane(Cesium.Cartesian3.UNIT_X,1.0),
dimensions: new Cesium.Cartesian2(100, 100),
material:new Cesium.ColorMaterialProperty(Cesium.Color.RED),
//material:Cesium.Color.RED
}
})
viewer.entities.add(entity);
1.2:ImageMaterialProperty:图片材质(支持网络图片、图片元素、Canvas元素、视频元素)
let entity = new Cesium.Entity({
position: position,
plane: {
plane: new Cesium.Plane(Cesium.Cartesian3.UNIT_X,1.0),
dimensions: new Cesium.Cartesian2(100, 100),
material:new Cesium.ImageMaterialProperty({
image:"xxx.png",
transparent:true
})
}
})
viewer.entities.add(entity);
1.3:GridMaterialProperty:格网材质
let entity = new Cesium.Entity({
position: position,
plane: {
plane: new Cesium.Plane(Cesium.Cartesian3.UNIT_X,1.0),
dimensions: new Cesium.Cartesian2(100, 100),
material:new Cesium.GridMaterialProperty({ })
}
})
viewer.entities.add(entity);
1.4:StripeMaterialProperty:条纹材质
let entity = new Cesium.Entity({
position: position,
plane: {
plane: new Cesium.Plane(Cesium.Cartesian3.UNIT_X,1.0),
dimensions: new Cesium.Cartesian2(100, 100),
material: new Cesium.StripeMaterialProperty({
repeat: 10
})
}
})
viewer.entities.add(entity);
1.5:PolylineGlowMaterialProperty:发光线材质( 仅用于 PolylineGraphic )
let positions = [
x1, y1,z1,
x2, y2, z2
]
positions = Cesium.Cartesian3.fromDegreesArrayHeights([].concat.apply([], positions));
let entity = new Cesium.Entity({
polyline: {
positions: positions,
width:10,
material: new Cesium.PolylineGlowMaterialProperty({
color: Cesium.Color.fromCssColorString('#FF4500'),
glowPower: 0.3,
opacity: 0.6,
})
}
})
viewer.entities.add(entity);
1.6:PolylineOutlineMaterialProperty:边线材质( 仅用于 PolylineGraphic )
let positions = [
x1, y1,z1,
x2, y2, z2
]
positions = Cesium.Cartesian3.fromDegreesArrayHeights([].concat.apply([], positions));
let entity = new Cesium.Entity({
polyline: {
positions: positions,
width:10,
material: new Cesium.PolylineOutlineMaterialProperty({
color: Cesium.Color.fromCssColorString('#FF4500'),
outlineWidth:2,
outlineColor:Cesium.Color.BLUE
})
}
})
viewer.entities.add(entity);
cesium官网还有更多材质,可搜索MaterialProperty查看详细文档
2. 自定义材质
Cesium已经为我们定义了很多类型的材质,但是在实际项目中,我们往往还需要定义自己的材质类型,以满足项目的需求。自定义材质类型,我们需要实现 MaterialProperty 接口。
以官方文档为例尝试自定义流动线材质
1、首先定义材质
function PolylineFlowMaterialProperty(options) {
this._definitionChanged = new Cesium.Event();
this._image = options.image;
}
Object.defineProperties(PolylineFlowMaterialProperty.prototype, {
isConstant: {
get: function () {
return false;
},
},
definitionChanged: {
get: function () {
return this._definitionChanged;
},
},
});
PolylineFlowMaterialProperty.prototype.getType = function (time) {
return "PolylineFlow";
};
PolylineFlowMaterialProperty.prototype.getValue = function (time, result) {
if (!Cesium.defined(result)) {
result = {};
}
result.image = this._image;
return result;
};
PolylineFlowMaterialProperty.prototype.equals = function (other) {
return (
this === other || //
(other instanceof PolylineFlowMaterialProperty && //
Property.equals(this._image, other._image))
);
};
属性:
isConstant 是否为常量,我们直接返回false
definitionChanged 为一个Event类型,我们new 了一个Event为其赋值
方法:
getType 获取材质的类型,字符串标识,我们设置为‘PolylineFlow’
getValue 获取相关数据,我们只需要一个图片参数,将其从PolylineFlowMaterialProperty中传递到result
equals 用于判断两个材质是否相同,这是为了进行对象重用,当两个材质的参数都一摸一样,那代表是一样的,就不需要创建两个对象。
2、设置到cesium材质当中,并设置fabric属性
Cesium.Material.PolylineFlowType = 'PolylineFlow';
Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineFlowType, {
fabric: {
type: Cesium.Material.PolylineFlowType,
uniforms: {
image: "",
},
source: `
czm_material czm_getMaterial(czm_materialInput materialInput) {
czm_material material = czm_getDefaultMaterial(materialInput);
vec2 st = materialInput.st; //纹理坐标
float t =czm_frameNumber / 100.0;
vec4 imageColor =texture(image, vec2(fract( 5.*st.s - t),st.t));//从图片中采样颜色值
material.diffuse =imageColor.rgb * 1.5;
material.alpha =imageColor.a ;
return material;
}`
},
translucent: function (material) {
return true
}
})
着色器中的uniforms参数会通过材质类的getValue获取,source是材质类的着色器代码,这是一个固定的结构,在czm_getMaterial方法中计算着色器结果。
3、使用自定义材质
let positions = [
x1, y1, z1,
x2, y2, z2
]
positions = Cesium.Cartesian3.fromDegreesArrayHeights([].concat.apply([], positions));
let entity = new Cesium.Entity({
polyline: {
positions: positions,
width: 10,
material: new PolylineFlowMaterialProperty({
image: "xxx.png"
})
}
})
viewer.entities.add(entity);
4、效果
5、更多内容就如第一章说的本地运行源码找到ColorMaterialProperty.js,参考学习源码知识了解材质
6、彩蛋:可以使用components属性代替addMaterial方法中对象参数的source,学会去源码找答案
总结
这是第七章Entity基础知识学习,更多进一步的cesium相关知识请关注我,带你更深层次了解cesium
后续更多和cesium有关的更深度的内容可以在创奇社区找我 点击进入创奇社区