当前位置:   article > 正文

cesium——加载3DTiles,模型的选中,高亮效果_csium鼠标挪动,模型cesium3dtileset

csium鼠标挪动,模型cesium3dtileset

3DTiles是一种用于组织和传输大规模地理数据的规范,旨在提供一种高效、可扩展的方式来加载和显示复杂的3D模型。它革新了地理数据可视化领域,为创建逼真的三维地图提供了新的可能性。

3DTiles采用了一种层次化的数据结构,将地理信息按照空间范围划分成小块,类似于3D瓦片。每个瓦片包含地理实体的几何形状、纹理贴图和层次结构信息。这种切片和分层的方式使得数据可以根据需要按需加载,从而降低了网络传输和计算资源的需求。

使用3DTiles,开发人员可以加载和呈现各种地理数据,如建筑物、地形、植被和其他点云数据等。它为基于Web的GIS应用提供了更好的性能和交互体验。用户可以在浏览器中平滑地导航和浏览模型,实时更改视图和展示参数。

此外,3DTiles的规范性设计使得它可以与其他GIS和地理数据标准(如地理坐标系、投影等)无缝集成。它的开放性和普及性使得很多GIS软件和服务已经支持3DTiles标准,从而为用户和开发人员提供了丰富的工具和资源。

总之,3DTiles为地理数据的可视化和交互提供了一种全新的方式。其中,加载3DTiles模型并实现选中和高亮效果是常见的需求之一。

因此,本文将介绍如何实现模型的选中和高亮效果。我们将详细讨论Cesium中的picking机制,引导读者根据用户的鼠标点击或触摸事件来选中模型。进一步,我们将讨论如何改变选中模型的外观,例如更改颜色、变更材质等,从而实现高亮效果。

如代码所示,主要通过修改片元着色器或color属性,实现模型的左键选中,右键取消效果,移入高亮效果;

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  6. <meta name="viewport"
  7. content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
  8. <title>3D Tiles</title>
  9. <link href="../../Build/Cesium/Widgets/widgets.css" rel="stylesheet">
  10. <link href="./css/pretty.css" rel="stylesheet">
  11. <script src="./js/jquery.min.js"></script>
  12. <script type="text/javascript" src="../../Build/Cesium/Cesium.js"></script>
  13. </head>
  14. <body>
  15. <div id="cesiumContainer"></div>
  16. <script>
  17. function onload(Cesium) {
  18. console.log(Cesium.Ellipsoid.WGS84)
  19. var obj = [6378137.0, 6378137.0, 6356752.3142451793];
  20. Cesium.Ellipsoid.WGS84 = Object.freeze(new Cesium.Ellipsoid(obj[0], obj[1], obj[2]));
  21. var viewer = new Cesium.Viewer('cesiumContainer');
  22. const options = {
  23. skipLevelOfDetail: true,
  24. shadows: Cesium.ShadowMode.CAST_ONLY,
  25. baseScreenSpaceError: 1024,
  26. skipScreenSpaceErrorFactor: 16,
  27. skipLevels: 1,
  28. immediatelyLoadDesiredLevelOfDetail: false,
  29. loadSiblings: false,
  30. cullWithChildrenBounds: true,
  31. dynamicScreenSpaceError: true,
  32. dynamicScreenSpaceErrorHeightFalloff: 0.5,
  33. url: './SampleData/line/tileset.json'
  34. }
  35. const tileset = new Cesium.Cesium3DTileset(options)
  36. viewer.terrainProvider.visible = false;
  37. var ts = viewer.scene.primitives.add(tileset);
  38. console.log("ts", ts)
  39. ts.readyPromise.then(function () {
  40. var boundingSphere = ts.boundingSphere;
  41. viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -0.5, boundingSphere.radius));
  42. viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
  43. }).otherwise(function (error) {
  44. throw (error);
  45. });
  46. var screenSpaceEventHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  47. let pickedObjectArr = [], shader = null
  48. screenSpaceEventHandler.setInputAction(function (e) {
  49. let pickedObject = viewer.scene.pick(e.position);
  50. if (pickedObject && pickedObject._content && pickedObject._content._features && pickedObject._content._features.length > 0) {
  51. pickedObjectArr.push(pickedObject)
  52. let feature = pickedObject._content._features[0]
  53. const _model = feature.content._model
  54. _model._shouldRegenerateShaders = true
  55. console.log(_model._sourcePrograms)
  56. console.log(_model._rendererResources.sourceShaders)
  57. shader = _model._rendererResources.sourceShaders[1]
  58. _model._rendererResources.sourceShaders[1] = _model._rendererResources.sourceShaders[1].replace("gl_FragColor = vec4(color, 1.0);", "gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);");
  59. setTimeout(() => {
  60. _model._shouldRegenerateShaders = false
  61. }, 1000)
  62. console.log(_model._rendererResources.sourceShaders[1])
  63. }
  64. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  65. screenSpaceEventHandler.setInputAction(function (e) {
  66. while (pickedObjectArr.length > 0) {
  67. let element = pickedObjectArr.shift();
  68. let feature = element._content._features[0]
  69. const _model = feature.content._model
  70. _model._shouldRegenerateShaders = true
  71. console.log(_model._sourcePrograms)
  72. console.log(_model._rendererResources.sourceShaders)
  73. _model._rendererResources.sourceShaders[1] = shader
  74. console.log(shader)
  75. }
  76. }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  77. let feature, _feature, tile
  78. const highlightColor = new Cesium.Color(1.0, 1.0, 0.0, 0.4);
  79. const oldColor = new Cesium.Color();
  80. screenSpaceEventHandler.setInputAction(function (e) {
  81. const picked = viewer.scene.pick(e.endPosition);
  82. if (picked instanceof Cesium.Cesium3DTileFeature) {
  83. // Picked a feature
  84. feature = picked;
  85. if (feature === _feature && feature !== undefined) {
  86. console.log("相同")
  87. return
  88. }
  89. const currentFeature = _feature;
  90. if (currentFeature && !currentFeature.content.isDestroyed()) {
  91. currentFeature.color = oldColor;
  92. // viewer.scene.requestRender();
  93. }
  94. tile = picked.content.tile;
  95. Cesium.Color.clone(feature.color, oldColor);
  96. feature.color = highlightColor;
  97. // viewer.scene.requestRender();
  98. _feature = feature;
  99. } else {
  100. if (feature !== undefined && feature !== null) {
  101. console.log("非模型,清除颜色")
  102. feature.color = oldColor;
  103. // viewer.scene.requestRender();
  104. feature = null
  105. _feature = null
  106. }
  107. }
  108. }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  109. $('#loadingbar').remove();
  110. }
  111. if (typeof Cesium !== 'undefined') {
  112. window.startupCalled = true;
  113. onload(Cesium);
  114. }
  115. </script>
  116. </body>
  117. </html>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/99736
推荐阅读
相关标签
  

闽ICP备14008679号