Cesium快速入门18:Entity材质设置

前面的课程里,我们先后讲了 Entity(实体)和 Primitive(图元)这两种"物体"。

它们都能贴材质,只不过用法有简有繁。

今天先集中火力把 Entity 的材质系统 撸一遍,看看 Cesium 到底给我们备好了哪些"现成布料",以及怎么换布料。


一、Entity 材质入口:material 属性

以矩形为例,只要在配置项里写一行 material: ... 就能换皮肤。

既可以直给颜色,也能塞一个正式的 MaterialProperty 对象。

复制代码
rectangle: {
  coordinates: Cesium.Rectangle.fromDegrees(...),
  material: Cesium.Color.RED.withAlpha(0.5)   // 直接写颜色,底层会自动包成 ColorMaterialProperty
}

二、MaterialProperty 是什么?

MaterialProperty 是所有材质属性的"老祖宗",一个抽象基类,不能直接 new

它只规定派生类必须实现哪些方法和属性。

我们真正用的是它的那群"子子孙孙"------

名字统一以 ***MaterialProperty 结尾,随手一搜就能找到。


三、官方常备"布料"清单

下面 4 种最常用,无需自己写着色器,直接配参数即可。

  • 纯色布料(ColorMaterialProperty)

    和直接写颜色等效,写不写都行。

    const material = new Cesium.ColorMaterialProperty(
    Cesium.Color.RED.withAlpha(0.5)
    );

  • 棋盘格(CheckerboardMaterialProperty)

    黑白格子,可改颜色、可改密度。

复制代码
const material = new Cesium.CheckerboardMaterialProperty({
  evenColor: Cesium.Color.WHITE,  // 偶数格
  oddColor: Cesium.Color.BLACK,   // 奇数格
  repeat: new Cesium.Cartesian2(4, 4) // 横向、纵向各重复 4 次
});
  • 条纹布(StripeMaterialProperty)

    横条 or 竖条,随你挑。

复制代码
const stripMaterial = new Cesium.StripeMaterialProperty({
  evenColor: Cesium.Color.WHITE,
  oddColor: Cesium.Color.BLACK,
  repeat: 32,                     // 条纹数量
  offset: 0.0,                    // 起始偏移
  orientation: Cesium.StripeOrientation.HORIZONTAL // 或 VERTICAL
});
  • 网格布(GridMaterialProperty)

    把面切成"豆腐块",适合画地块、棋盘。

复制代码
const gridMaterial = new Cesium.GridMaterialProperty({
  color: Cesium.Color.YELLOW,     // 网格线颜色
  cellAlpha: 0.2,                 // 格子填充透明度
  lineCount: new Cesium.Cartesian2(8, 8),   // 横、竖各 8 条线
  lineThickness: new Cesium.Cartesian2(2.0, 2.0) // 线宽(像素)
});

四、把布料穿到矩形上

无论哪种材质,最后都交给 material 字段即可。

复制代码
const redRectangle = viewer.entities.add({
  name: "Red translucent rectangle",
  rectangle: {
    coordinates: Cesium.Rectangle.fromDegrees(114.0, 23.4, 114.4, 23.6),
    extrudedHeight: 30000.0,      // 挤出高度,单位米
    material: material,           // 上面任意一种材质
  },
});

小结一句话

Entity 换皮肤 = 找 ***MaterialProperty → 配参数 → 扔给 material

官方给的"布料"不够炫?后面我们教你亲手写着色器,想怎么酷就怎么酷。

相关推荐
coder攻城狮1 天前
VTK系列1:在屏幕绘制多边形
c++·3d
PHOSKEY1 天前
3D工业相机如何“读透”每一个字符?快速识别、高精度3D测量
数码相机·3d
XX風1 天前
7.2 harris 3d
3d
多恩Stone1 天前
【3D-AICG 系列-3】Trellis 2 的O-voxel (下) Material: Volumetric Surface Attributes
人工智能·3d·aigc
多恩Stone1 天前
【3D-AICG 系列-1】Trellis v1 和 Trellis v2 的区别和改进
人工智能·pytorch·python·算法·3d·aigc
三年模拟五年烧烤1 天前
easy-threesdk快速一键搭建threejs3d可视化场景
3d·threejs
top_designer1 天前
Materialize:手绘地表太假?“PBR 纹理炼金术” 5分钟生成次世代材质
游戏·3d·aigc·材质·设计师·游戏美术·pbr
esmap1 天前
OpenClaw与ESMAP AOA定位系统融合技术分析
前端·人工智能·计算机视觉·3d·ai·js
dump linux1 天前
Linux 显示服务器与合成器架构详解
linux·驱动开发·3d
多恩Stone1 天前
【3D-AICG 系列-2】Trellis 2 的O-voxel (上) Shape: Flexible Dual Grid
人工智能·python·算法·3d·aigc