Cesium基础知识-创建几何模型

  1. viewer = new Cesium.Viewer('cesiumContainer');
  2. //创建条纹
  3. function CreateTiaoWen() {
  4.     viewer.entities.add({
  5.         rectangle: {
  6.             coordinates: Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),
  7.             material: new Cesium.StripeMaterialProperty({
  8.                 evenColor: Cesium.Color.RED,
  9.                 oddColor: Cesium.Color.BLUE,
  10.                 repeat: 5
  11.             })
  12.         }
  13.     });
  14. }
  15. //CreateTiaoWen();
  16. //使用几何图形和外观
  17. function CreateTiaoWen2() {
  18.     var scene = viewer.scene;
  19.     var instance = new Cesium.GeometryInstance({
  20.         geometry: new Cesium.RectangleGeometry({
  21.             rectangle: Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),
  22.             vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
  23.         })
  24.     });
  25.     scene.primitives.add(new Cesium.Primitive({
  26.         geometryInstances: instance,
  27.         appearance: new Cesium.EllipsoidSurfaceAppearance({
  28.             material: Cesium.Material.fromType('Stripe')
  29.         })
  30.     }));
  31. }
  32. //CreateTiaoWen2();
  33. //使用几何图形和外观
  34. function CreateTiaoWen3() {
  35.     var scene = viewer.scene;
  36.     var instance = new Cesium.GeometryInstance({
  37.         geometry: new Cesium.RectangleGeometry({
  38.             rectangle: Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),
  39.             vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
  40.         })
  41.     });
  42.     var anotherInstance = new Cesium.GeometryInstance({
  43.         geometry: new Cesium.RectangleGeometry({
  44.             rectangle: Cesium.Rectangle.fromDegrees(-85.0, 20.0, -75.0, 30.0),
  45.             vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
  46.         })
  47.     });
  48.     scene.primitives.add(new Cesium.Primitive({
  49.         geometryInstances: [instance, anotherInstance],
  50.         appearance: new Cesium.EllipsoidSurfaceAppearance({
  51.             material: Cesium.Material.fromType('Stripe')
  52.         })
  53.     }));
  54. }
  55. //CreateTiaoWen3();
  56. //使用几何图形和外观
  57. function CreateTiaoWen4() {
  58.     var scene = viewer.scene;
  59.     var instance = new Cesium.GeometryInstance({
  60.         geometry: new Cesium.RectangleGeometry({
  61.             rectangle: Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),
  62.             vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
  63.         }),
  64.         attributes: {
  65.             color: new Cesium.ColorGeometryInstanceAttribute(0.0, 0.0, 1.0, 0.8)
  66.         }
  67.     });
  68.     var anotherInstance = new Cesium.GeometryInstance({
  69.         geometry: new Cesium.RectangleGeometry({
  70.             rectangle: Cesium.Rectangle.fromDegrees(-85.0, 20.0, -75.0, 30.0),
  71.             vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
  72.         }),
  73.         attributes: {
  74.             color: new Cesium.ColorGeometryInstanceAttribute(1.0, 0.0, 0.0, 0.8)
  75.         }
  76.     });
  77.     scene.primitives.add(new Cesium.Primitive({
  78.         geometryInstances: [instance, anotherInstance],
  79.         appearance: new Cesium.PerInstanceColorAppearance()
  80.     }));
  81. }
  82. //CreateTiaoWen4();
  83. //使用几何图形和外观
  84. function CreateTiaoWen5() {
  85.     var scene = viewer.scene;
  86.     var instances = [];
  87.     for (var lon = -180.0; lon < 180.0; lon += 5.0) {
  88.         for (var lat = -85.0; lat < 85.0; lat += 5.0) {
  89.             instances.push(new Cesium.GeometryInstance({
  90.                 geometry: new Cesium.RectangleGeometry({
  91.                     rectangle: Cesium.Rectangle.fromDegrees(lon, lat, lon + 5.0, lat + 5.0),
  92.                     vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
  93.                 }),
  94.                 id: 'rectangle:' + lon + "---" + lat,
  95.                 attributes: {
  96.                     color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromRandom({
  97.                         alpha: 0.5
  98.                     }))
  99.                 }
  100.             }));
  101.         }
  102.     }
  103.     scene.primitives.add(new Cesium.Primitive({
  104.         geometryInstances: instances,
  105.         appearance: new Cesium.PerInstanceColorAppearance()
  106.     }));
  107.     var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  108.     handler.setInputAction(function (movement) {
  109.         var pick = scene.pick(movement.position);
  110.         if (Cesium.defined(pick)) {
  111.             console.log('Mouse clicked rectangle:' + pick.id);
  112.         }
  113.     }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  114. }
  115. //CreateTiaoWen5();
  116. //使用几何图形和外观
  117. function CreateTiaoWen6() {
  118.     var scene = viewer.scene;
  119.     var instance = new Cesium.GeometryInstance({
  120.         geometry: new Cesium.RectangleGeometry({
  121.             rectangle: Cesium.Rectangle.fromDegrees(-100.0, 30.0, -90.0, 40.0),
  122.             vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
  123.         }),
  124.         id: 'my rectangle',
  125.         attributes: {
  126.             color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.RED)
  127.         }
  128.     });
  129.     scene.primitives.add(new Cesium.Primitive({
  130.         geometryInstances: instance,
  131.         appearance: new Cesium.PerInstanceColorAppearance()
  132.     }));
  133.     var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  134.     handler.setInputAction(function (movement) {
  135.         var pick = scene.pick(movement.position);
  136.         if (Cesium.defined(pick) && (pick.id === 'my rectangle')) {
  137.             console.log('Mouse clicked rectangle.');
  138.         }
  139.     }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  140. }
  141. //CreateTiaoWen6();
  142. //使用几何球体EllipsoidGeometry创建
  143. function CreateTuoYuanQiu() {
  144.     var scene = viewer.scene;
  145.     var ellipsoidGeometry = new Cesium.EllipsoidGeometry({
  146.         vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
  147.         radii: new Cesium.Cartesian3(300000.0, 200000.0, 150000.0)
  148.     });
  149.     var cyanEllipsoidInstance = new Cesium.GeometryInstance({
  150.         geometry: ellipsoidGeometry,
  151.         modelMatrix: Cesium.Matrix4.multiplyByTranslation(
  152.             Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromDegrees(-100.0, 40.0)),
  153.             new Cesium.Cartesian3(0.0, 0.0, 150000.0),
  154.             new Cesium.Matrix4()
  155.         ),
  156.         attributes: {
  157.             color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.CYAN)
  158.         }
  159.     });
  160.     var orangeEllipsoidInstance = new Cesium.GeometryInstance({
  161.         geometry: ellipsoidGeometry,
  162.         modelMatrix: Cesium.Matrix4.multiplyByTranslation(
  163.             Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromDegrees(-100.0, 40.0)),
  164.             new Cesium.Cartesian3(0.0, 0.0, 450000.0),
  165.             new Cesium.Matrix4()
  166.         ),
  167.         attributes: {
  168.             color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.ORANGE)
  169.         }
  170.     });
  171.     scene.primitives.add(new Cesium.Primitive({
  172.         geometryInstances: [cyanEllipsoidInstance, orangeEllipsoidInstance],
  173.         appearance: new Cesium.PerInstanceColorAppearance({
  174.             translucent: false,
  175.             closed: true
  176.         })
  177.     }));
  178. }
  179. //CreateTuoYuanQiu();
  180. //使用几何球体EllipsoidGeometry创建,修改颜色
  181. function ChangeColor() {
  182.     var scene = viewer.scene;
  183.     var circleInstance = new Cesium.GeometryInstance({
  184.         geometry: new Cesium.CircleGeometry({
  185.             center: Cesium.Cartesian3.fromDegrees(-95.0, 43.0),
  186.             radius: 250000.0,
  187.             vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
  188.         }),
  189.         attributes: {
  190.             color: Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1.0, 0.0, 0.0, 0.5))
  191.         },
  192.         id: 'circle'
  193.     });
  194.     var primitive = new Cesium.Primitive({
  195.         geometryInstances: circleInstance,
  196.         appearance: new Cesium.PerInstanceColorAppearance({
  197.             translucent: false,
  198.             closed: true
  199.         })
  200.     });
  201.     scene.primitives.add(primitive);
  202.     setInterval(function () {
  203.         var attributes = primitive.getGeometryInstanceAttributes('circle');
  204.         attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.fromRandom({
  205.             alpha: 1.0
  206.         }));
  207.     }, 2000);
  208. }
  209. //ChangeColor();
  210. //使用几何球体EllipsoidGeometry创建,修改颜色
  211. function ChangeColor1() {
  212.         var appearance = new Cesium.PerInstanceColorAppearance({
  213.             translucent: false,
  214.             closed: true
  215.         });
  216.         var anotherAppearance = new Cesium.PerInstanceColorAppearance({
  217.             renderState: {
  218.                 depthTest: {
  219.                     enabled: true
  220.                 },
  221.                 cull: {
  222.                     enabled: true,
  223.                     face: Cesium.CullFace.BACK
  224.                 }
  225.             }
  226.         });
  227.         var wall = Cesium.WallGeometry.fromConstantHeights({
  228.             positions : Cesium.Cartesian3.fromDegreesArray([
  229.               19.0, 47.0,
  230.               19.0, 48.0,
  231.               20.0, 48.0,
  232.               20.0, 47.0,
  233.               19.0, 47.0,
  234.             ]),
  235.             minimumHeight : 20000.0,
  236.             maximumHeight : 10000.0
  237.           });
  238.           var geometry = Cesium.WallGeometry.createGeometry(wall);
  239.           viewer.entities.add(geometry);
  240.           viewer.zoomTo(viewer.entities);
  241. }
  242. ChangeColor1();
  243. //创建墙
  244. function CreateWall() {
  245.     var redWall = viewer.entities.add({
  246.         name: "Red wall at height",
  247.         wall: {
  248.           positions: Cesium.Cartesian3.fromDegreesArrayHeights([
  249.             -115.0,
  250.             44.0,
  251.             200000.0,
  252.             -90.0,
  253.             44.0,
  254.             200000.0,
  255.           ]),
  256.           minimumHeights: [100000.0, 100000.0],
  257.           material: Cesium.Color.RED,
  258.         },
  259.       });
  260.       
  261.       var greenWall = viewer.entities.add({
  262.         name: "Green wall from surface with outline",
  263.         wall: {
  264.           positions: Cesium.Cartesian3.fromDegreesArrayHeights([
  265.             -107.0,
  266.             43.0,
  267.             100000.0,
  268.             -97.0,
  269.             43.0,
  270.             100000.0,
  271.             -97.0,
  272.             40.0,
  273.             100000.0,
  274.             -107.0,
  275.             40.0,
  276.             100000.0,
  277.             -107.0,
  278.             43.0,
  279.             100000.0,
  280.           ]),
  281.           material: Cesium.Color.GREEN,
  282.           outline: true,
  283.         },
  284.       });
  285.       
  286.       var blueWall = viewer.entities.add({
  287.         name: "Blue wall with sawtooth heights and outline",
  288.         wall: {
  289.           positions: Cesium.Cartesian3.fromDegreesArray([
  290.             -115.0,
  291.             50.0,
  292.             -112.5,
  293.             50.0,
  294.             -110.0,
  295.             50.0,
  296.             -107.5,
  297.             50.0,
  298.             -105.0,
  299.             50.0,
  300.             -102.5,
  301.             50.0,
  302.             -100.0,
  303.             50.0,
  304.             -97.5,
  305.             50.0,
  306.             -95.0,
  307.             50.0,
  308.             -92.5,
  309.             50.0,
  310.             -90.0,
  311.             50.0,
  312.           ]),
  313.           maximumHeights: [
  314.             100000,
  315.             200000,
  316.             100000,
  317.             200000,
  318.             100000,
  319.             200000,
  320.             100000,
  321.             200000,
  322.             100000,
  323.             200000,
  324.             100000,
  325.           ],
  326.           minimumHeights: [
  327.             0,
  328.             100000,
  329.             0,
  330.             100000,
  331.             0,
  332.             100000,
  333.             0,
  334.             100000,
  335.             0,
  336.             100000,
  337.             0,
  338.           ],
  339.           material: Cesium.Color.BLUE.withAlpha(0.5),
  340.           outline: true,
  341.           outlineColor: Cesium.Color.BLACK,
  342.         },
  343.       });
  344.       viewer.zoomTo(viewer.entities);
  345. }
  346. //CreateWall();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值