1、内部属性单体

官网案例:https://sandcastle.cesium.com/?src=3D%20Tiles%20Feature%20Styling.html
image.png

  1. // A demo of interactive 3D Tiles styling
  2. // Styling language Documentation: https://github.com/CesiumGS/3d-tiles/tree/master/specification/Styling
  3. // Building data courtesy of NYC OpenData portal: http://www1.nyc.gov/site/doitt/initiatives/3d-building.page
  4. var viewer = new Cesium.Viewer("cesiumContainer", {
  5. terrainProvider: Cesium.createWorldTerrain(),
  6. });
  7. viewer.scene.globe.depthTestAgainstTerrain = true;
  8. // Set the initial camera view to look at Manhattan
  9. var initialPosition = Cesium.Cartesian3.fromDegrees(
  10. -74.01881302800248,
  11. 40.69114333714821,
  12. 753
  13. );
  14. var initialOrientation = new Cesium.HeadingPitchRoll.fromDegrees(
  15. 21.27879878293835,
  16. -21.34390550872461,
  17. 0.0716951918898415
  18. );
  19. viewer.scene.camera.setView({
  20. destination: initialPosition,
  21. orientation: initialOrientation,
  22. endTransform: Cesium.Matrix4.IDENTITY,
  23. });
  24. // Load the NYC buildings tileset.
  25. var tileset = new Cesium.Cesium3DTileset({
  26. url: Cesium.IonResource.fromAssetId(75343),
  27. });
  28. viewer.scene.primitives.add(tileset);
  29. // Color buildings based on their height.
  30. function colorByHeight() {
  31. tileset.style = new Cesium.Cesium3DTileStyle({
  32. color: {
  33. conditions: [
  34. ["${Height} >= 300", "rgba(45, 0, 75, 0.5)"],
  35. ["${Height} >= 200", "rgb(102, 71, 151)"],
  36. ["${Height} >= 100", "rgb(170, 162, 204)"],
  37. ["${Height} >= 50", "rgb(224, 226, 238)"],
  38. ["${Height} >= 25", "rgb(252, 230, 200)"],
  39. ["${Height} >= 10", "rgb(248, 176, 87)"],
  40. ["${Height} >= 5", "rgb(198, 106, 11)"],
  41. ["true", "rgb(127, 59, 8)"],
  42. ],
  43. },
  44. });
  45. }
  46. // Color buildings by their latitude coordinate.
  47. function colorByLatitude() {
  48. tileset.style = new Cesium.Cesium3DTileStyle({
  49. defines: {
  50. latitudeRadians: "radians(${Latitude})",
  51. },
  52. color: {
  53. conditions: [
  54. ["${latitudeRadians} >= 0.7125", "color('purple')"],
  55. ["${latitudeRadians} >= 0.712", "color('red')"],
  56. ["${latitudeRadians} >= 0.7115", "color('orange')"],
  57. ["${latitudeRadians} >= 0.711", "color('yellow')"],
  58. ["${latitudeRadians} >= 0.7105", "color('lime')"],
  59. ["${latitudeRadians} >= 0.710", "color('cyan')"],
  60. ["true", "color('blue')"],
  61. ],
  62. },
  63. });
  64. }
  65. // Color buildings by distance from a landmark.
  66. function colorByDistance() {
  67. tileset.style = new Cesium.Cesium3DTileStyle({
  68. defines: {
  69. distance:
  70. "distance(vec2(radians(${Longitude}), radians(${Latitude})), vec2(-1.291777521, 0.7105706624))",
  71. },
  72. color: {
  73. conditions: [
  74. ["${distance} > 0.0012", "color('gray')"],
  75. [
  76. "${distance} > 0.0008",
  77. "mix(color('yellow'), color('red'), (${distance} - 0.008) / 0.0004)",
  78. ],
  79. [
  80. "${distance} > 0.0004",
  81. "mix(color('green'), color('yellow'), (${distance} - 0.0004) / 0.0004)",
  82. ],
  83. ["${distance} < 0.00001", "color('white')"],
  84. [
  85. "true",
  86. "mix(color('blue'), color('green'), ${distance} / 0.0004)",
  87. ],
  88. ],
  89. },
  90. });
  91. }

2、矢量面叠加单体化:通过classificationType属性来进行单体化

  1. 首先添加3dtiles模型
  2. 绘制矢量面,其中矢量面的坐标可手动绘制,保存;
  3. 矢量面叠加到3dtiles模型上
  4. 添加一个移动的面及样式,绑定移动事件,移动时获取当前选中的面的坐标,设定移动面的坐标为当前选中的面

    3、primitives添加模型,通过classificationType属性来进行单体化

官网案例:https://sandcastle.cesium.com/?src=Classification.html&label=All
image.png

  1. 先添加primitive模型

    1. var treeHighlight3 = scene.primitives.add(new Cesium.ClassificationPrimitive({
    2. geometryInstances : new Cesium.GeometryInstance({
    3. geometry : new Cesium.EllipsoidGeometry({
    4. radii : new Cesium.Cartesian3(2.45, 2.45, 3.0)
    5. }),
    6. modelMatrix : modelMatrix,
    7. attributes : {
    8. color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromCssColorString('#004FFF').withAlpha(0.5)),
    9. show : new Cesium.ShowGeometryInstanceAttribute(true)
    10. },
    11. id : 'volume 3'
    12. }),
    13. classificationType : Cesium.ClassificationType.CESIUM_3D_TILE
    14. }));
  2. 加入鼠标移入事件设定样式

    1. var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
    2. handler.setInputAction(function(movement) {
    3. var pickedObject = scene.pick(movement.endPosition);
    4. if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.id)) {
    5. if (pickedObject.id === currentObjectId) {
    6. return;
    7. }
    8. if (Cesium.defined(currentObjectId)) {
    9. attributes = currentPrimitive.getGeometryInstanceAttributes(currentObjectId);
    10. attributes.color = currentColor;
    11. attributes.show = currentShow;
    12. currentObjectId = undefined;
    13. currentPrimitive = undefined;
    14. currentColor = undefined;
    15. currentShow = undefined;
    16. }
    17. }
    18. if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.primitive) && Cesium.defined(pickedObject.id) && Cesium.defined(pickedObject.primitive.getGeometryInstanceAttributes)) {
    19. currentObjectId = pickedObject.id;
    20. currentPrimitive = pickedObject.primitive;
    21. attributes = currentPrimitive.getGeometryInstanceAttributes(currentObjectId);
    22. currentColor = attributes.color;
    23. currentShow = attributes.show;
    24. if (!scene.invertClassification) {
    25. attributes.color = [255, 0, 255, 128];
    26. }
    27. attributes.show = [1];
    28. } else if (Cesium.defined(currentObjectId)) {
    29. attributes = currentPrimitive.getGeometryInstanceAttributes(currentObjectId);
    30. attributes.color = currentColor;
    31. attributes.show = currentShow;
    32. currentObjectId = undefined;
    33. currentPrimitive = undefined;
    34. currentColor = undefined;
    35. }
    36. }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

    image.png
    image.png