Advertisement
mk88vvv

Untitled

Dec 9th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // A simple demo of 3D Tiles feature picking with hover and select behavior
  2. // Building data courtesy of NYC OpenData portal: http://www1.nyc.gov/site/doitt/initiatives/3d-building.page
  3. const viewer = new Cesium.Viewer("cesiumContainer", {
  4.   terrainProvider: Cesium.createWorldTerrain(),
  5. });
  6.  
  7. // HTML overlay for showing feature name on mouseover
  8. const nameOverlay = document.createElement("div");
  9. viewer.container.appendChild(nameOverlay);
  10. nameOverlay.className = "backdrop";
  11. nameOverlay.style.display = "none";
  12. nameOverlay.style.position = "absolute";
  13. nameOverlay.style.bottom = "0";
  14. nameOverlay.style.left = "0";
  15. nameOverlay.style["pointer-events"] = "none";
  16. nameOverlay.style.padding = "4px";
  17. nameOverlay.style.backgroundColor = "black";
  18. let entity;
  19. let entity2;
  20. //положение entity
  21. const height=0;
  22. const position = Cesium.Cartesian3.fromDegrees(
  23.   -123.0744619,
  24.   44.0503706,
  25.   height,
  26. );
  27. //положение entity2
  28. const position2 = Cesium.Cartesian3.fromDegrees(
  29.   -123.0745145,
  30.   44.05027029,
  31.   height,
  32. );
  33. const heading = Cesium.Math.toRadians(135);
  34. const pitch = 0;
  35. const roll = 0;
  36. const hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
  37. const orientation = Cesium.Transforms.headingPitchRollQuaternion(position,hpr);
  38. //entities
  39. entity = viewer.entities.add({
  40.   position: position,
  41.   orientation: orientation,
  42.   model: {
  43.     uri: "../SampleData/models/GroundVehicle/GroundVehicle.glb",
  44.     maximumScale: 20000,  
  45.     },
  46.   });
  47.  
  48. entity2 = viewer.entities.add({
  49.  position: position2,
  50.  orientation: orientation,
  51.  model: {
  52.    uri: "../SampleData/models/GroundVehicle/GroundVehicle.glb",
  53.    maximumScale: 20000,
  54.    },
  55.   });
  56.  
  57. //buttons
  58. //move to entity
  59. Sandcastle.addToggleButton("entity", viewer.zoomTo(viewer.entities), function (
  60.   checked
  61. ) {
  62.   viewer.zoomTo(viewer.entities);
  63. });
  64. //move to entity2
  65. Sandcastle.addToggleButton("entity2", viewer.zoomTo(entity2), function (
  66.   checked
  67. ) {
  68.   viewer.zoomTo(entity2);
  69. });
  70.   viewer.trackedEntity = entity;
  71.  
  72. // Information about the currently selected feature
  73. const selected = {
  74.   feature: undefined,
  75.   originalColor: new Cesium.Color(),
  76. };
  77.  
  78. // An entity object which will hold info about the currently selected feature for infobox display
  79. const selectedEntity = new Cesium.Entity();
  80.  
  81. // Get default left click handler for when a feature is not picked on left click
  82. const clickHandler = viewer.screenSpaceEventHandler.getInputAction(
  83.   Cesium.ScreenSpaceEventType.LEFT_CLICK
  84. );
  85.  
  86. // If silhouettes are supported, silhouette features in blue on mouse over and silhouette green on mouse click.
  87. // If silhouettes are not supported, change the feature color to yellow on mouse over and green on mouse click.
  88. if (
  89.   Cesium.PostProcessStageLibrary.isSilhouetteSupported(viewer.scene)
  90. ) {
  91.   // Silhouettes are supported
  92.   const silhouetteBlue = Cesium.PostProcessStageLibrary.createEdgeDetectionStage();
  93.   silhouetteBlue.uniforms.color = Cesium.Color.BLUE;
  94.   silhouetteBlue.uniforms.length = 0.01;
  95.   silhouetteBlue.selected = [];
  96.  
  97.   const silhouetteGreen = Cesium.PostProcessStageLibrary.createEdgeDetectionStage();
  98.   silhouetteGreen.uniforms.color = Cesium.Color.LIME;
  99.   silhouetteGreen.uniforms.length = 0.01;
  100.   silhouetteGreen.selected = [];
  101.  
  102.   viewer.scene.postProcessStages.add(
  103.     Cesium.PostProcessStageLibrary.createSilhouetteStage([
  104.       silhouetteBlue,
  105.       silhouetteGreen,
  106.     ])
  107.   );
  108.  
  109.   // Silhouette a feature blue on hover.
  110.   viewer.screenSpaceEventHandler.setInputAction(function onMouseMove(
  111.     movement
  112.   ) {
  113.     // If a feature was previously highlighted, undo the highlight
  114.     silhouetteBlue.selected = [];
  115.  
  116.     // Pick a new feature
  117.     const pickedFeature = viewer.scene.pick(movement.endPosition);
  118.     if (!Cesium.defined(pickedFeature)) {
  119.       nameOverlay.style.display = "none";
  120.       return;
  121.     }
  122.  
  123.     // A feature was picked, so show it's overlay content
  124.     nameOverlay.style.display = "block";
  125.     nameOverlay.style.bottom = `${
  126.       viewer.canvas.clientHeight - movement.endPosition.y
  127.     }px`;
  128.     nameOverlay.style.left = `${movement.endPosition.x}px`;
  129.     if (!Cesium.defined(pickedFeature) || !(pickedFeature instanceof Cesium.Cesium3DTileFeature)) {
  130.     nameOverlay.style.display = 'none';
  131.     return;
  132. }
  133.     const name = pickedFeature.getProperty("BIN");
  134.     nameOverlay.textContent = name;
  135.  
  136.     // Highlight the feature if it's not already selected.
  137.     if (pickedFeature !== selected.feature) {
  138.       silhouetteBlue.selected = [pickedFeature];
  139.     }
  140.   },
  141.   Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  142.  
  143.   // Silhouette a feature on selection and show metadata in the InfoBox.
  144.   viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(
  145.     movement
  146.   ) {
  147.     // If a feature was previously selected, undo the highlight
  148.     silhouetteGreen.selected = [];
  149.  
  150.     // Pick a new feature
  151.     const pickedFeature = viewer.scene.pick(movement.position);
  152.     if (!Cesium.defined(pickedFeature)) {
  153.       clickHandler(movement);
  154.       return;
  155.     }
  156.  
  157.     // Select the feature if it's not already selected
  158.     if (silhouetteGreen.selected[0] === pickedFeature) {
  159.       return;
  160.     }
  161.  
  162.     // Save the selected feature's original color
  163.     const highlightedFeature = silhouetteBlue.selected[0];
  164.     if (pickedFeature === highlightedFeature) {
  165.       silhouetteBlue.selected = [];
  166.     }
  167.  
  168.     // Highlight newly selected feature
  169.     silhouetteGreen.selected = [pickedFeature];
  170.  
  171.     // Set feature infobox description
  172.     if (!Cesium.defined(pickedFeature) || !(pickedFeature instanceof Cesium.Cesium3DTileFeature)) {
  173.     nameOverlay.style.display = 'none';
  174.     return;
  175. }
  176.     const featureName = pickedFeature.getProperty("name");
  177.     selectedEntity.name = featureName;
  178.     selectedEntity.description =
  179.       'Loading <div class="cesium-infoBox-loading"></div>';
  180.     viewer.selectedEntity = selectedEntity;
  181.     selectedEntity.description =
  182.       `${
  183.         '<table class="cesium-infoBox-defaultTable"><tbody>' +
  184.         "<tr><th>BIN</th><td>"
  185.       }${pickedFeature.getProperty("BIN")}</td></tr>` +
  186.       `<tr><th>DOITT ID</th><td>${pickedFeature.getProperty(
  187.         "DOITT_ID"
  188.       )}</td></tr>` +
  189.       `<tr><th>SOURCE ID</th><td>${pickedFeature.getProperty(
  190.         "SOURCE_ID"
  191.       )}</td></tr>` +
  192.       `</tbody></table>`;
  193.   },
  194.   Cesium.ScreenSpaceEventType.LEFT_CLICK);
  195. } else {
  196.   // Silhouettes are not supported. Instead, change the feature color.
  197.  
  198.   // Information about the currently highlighted feature
  199.   const highlighted = {
  200.     feature: undefined,
  201.     originalColor: new Cesium.Color(),
  202.   };
  203.  
  204.   // Color a feature yellow on hover.
  205.   viewer.screenSpaceEventHandler.setInputAction(function onMouseMove(
  206.     movement
  207.   ) {
  208.     // If a feature was previously highlighted, undo the highlight
  209.     if (Cesium.defined(highlighted.feature)) {
  210.       highlighted.feature.color = highlighted.originalColor;
  211.       highlighted.feature = undefined;
  212.     }
  213.     // Pick a new feature
  214.     const pickedFeature = viewer.scene.pick(movement.endPosition);
  215.     if (!Cesium.defined(pickedFeature)) {
  216.       nameOverlay.style.display = "none";
  217.       return;
  218.     }
  219.     // A feature was picked, so show it's overlay content
  220.     nameOverlay.style.display = "block";
  221.     nameOverlay.style.bottom = `${
  222.       viewer.canvas.clientHeight - movement.endPosition.y
  223.     }px`;
  224.     nameOverlay.style.left = `${movement.endPosition.x}px`;
  225.     let name = pickedFeature.getProperty("name");
  226.     if (!Cesium.defined(name)) {
  227.       name = pickedFeature.getProperty("id");
  228.     }
  229.     nameOverlay.textContent = name;
  230.     // Highlight the feature if it's not already selected.
  231.     if (pickedFeature !== selected.feature) {
  232.       highlighted.feature = pickedFeature;
  233.       Cesium.Color.clone(
  234.         pickedFeature.color,
  235.         highlighted.originalColor
  236.       );
  237.       pickedFeature.color = Cesium.Color.YELLOW;
  238.     }
  239.   },
  240.   Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  241.  
  242.   // Color a feature on selection and show metadata in the InfoBox.
  243.   viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(
  244.     movement
  245.   ) {
  246.     // If a feature was previously selected, undo the highlight
  247.     if (Cesium.defined(selected.feature)) {
  248.       selected.feature.color = selected.originalColor;
  249.       selected.feature = undefined;
  250.     }
  251.     // Pick a new feature
  252.     const pickedFeature = viewer.scene.pick(movement.position);
  253.     if (!Cesium.defined(pickedFeature)) {
  254.       clickHandler(movement);
  255.       return;
  256.     }
  257.     // Select the feature if it's not already selected
  258.     if (selected.feature === pickedFeature) {
  259.       return;
  260.     }
  261.     selected.feature = pickedFeature;
  262.     // Save the selected feature's original color
  263.     if (pickedFeature === highlighted.feature) {
  264.       Cesium.Color.clone(
  265.         highlighted.originalColor,
  266.         selected.originalColor
  267.       );
  268.       highlighted.feature = undefined;
  269.     } else {
  270.       Cesium.Color.clone(pickedFeature.color, selected.originalColor);
  271.     }
  272.     // Highlight newly selected feature
  273.     pickedFeature.color = Cesium.Color.LIME;
  274.     // Set feature infobox description
  275.     const featureName = pickedFeature.getProperty("name");
  276.     selectedEntity.name = featureName;
  277.     selectedEntity.description =
  278.       'Loading <div class="cesium-infoBox-loading"></div>';
  279.     viewer.selectedEntity = selectedEntity;
  280.     selectedEntity.description =
  281.       `${
  282.         '<table class="cesium-infoBox-defaultTable"><tbody>' +
  283.         "<tr><th>BIN</th><td>"
  284.       }${pickedFeature.getProperty("BIN")}</td></tr>` +
  285.       `<tr><th>DOITT ID</th><td>${pickedFeature.getProperty(
  286.         "DOITT_ID"
  287.       )}</td></tr>` +
  288.       `<tr><th>SOURCE ID</th><td>${pickedFeature.getProperty(
  289.         "SOURCE_ID"
  290.       )}</td></tr>` +
  291.       `<tr><th>Longitude</th><td>${pickedFeature.getProperty(
  292.         "longitude"
  293.       )}</td></tr>` +
  294.       `<tr><th>Latitude</th><td>${pickedFeature.getProperty(
  295.         "latitude"
  296.       )}</td></tr>` +
  297.       `<tr><th>Height</th><td>${pickedFeature.getProperty(
  298.         "height"
  299.       )}</td></tr>` +
  300.       `<tr><th>Terrain Height (Ellipsoid)</th><td>${pickedFeature.getProperty(
  301.         "TerrainHeight"
  302.       )}</td></tr>` +
  303.       `</tbody></table>`;
  304.   },
  305.   Cesium.ScreenSpaceEventType.LEFT_CLICK);
  306. }
  307.  
  308.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement