g05_gltfList.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  6. <meta name="author" content="火星科技 http://mars3d.cn " />
  7. <meta name="apple-touch-fullscreen" content="yes" />
  8. <meta name="apple-mobile-web-app-capable" content="yes" />
  9. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  10. <meta name="format-detection" content="telephone=no" />
  11. <meta name="x5-fullscreen" content="true" />
  12. <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  13. <!-- 标题及搜索关键字 -->
  14. <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  15. <meta
  16. name="description"
  17. content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型 gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包 地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
  18. />
  19. <link rel="shortcut icon" type="image/x-icon" href="" />
  20. <title>gltf模型库 </title>
  21. <!--第三方lib-->
  22. <script
  23. type="text/javascript"
  24. src="../lib/include-lib.js"
  25. libpath="../lib/"
  26. include="jquery,font-awesome,web-icons,bootstrap,bootstrap-checkbox,layer,toastr,haoutil,turf,mars3d,mars3d-widget"
  27. ></script>
  28. <link href="css/style.css" rel="stylesheet" />
  29. <style>
  30. .infoview div {
  31. margin-bottom: 5px;
  32. }
  33. .divGltfAll,
  34. .divDrawGltf {
  35. height: 30px;
  36. width: 130px;
  37. }
  38. .changeDiv {
  39. position: absolute;
  40. top: 20px;
  41. }
  42. .commonDrawGltf {
  43. position: absolute;
  44. top: 140px;
  45. }
  46. .commonDrawGltf div {
  47. margin-bottom: 20px;
  48. }
  49. .gltfPullDown {
  50. position: absolute;
  51. top: 80px;
  52. width: 300px;
  53. height: calc(100% - 160px);
  54. overflow: hidden;
  55. border: 1px solid rgba(0, 102, 255, 0.568);
  56. border-radius: 5px;
  57. background-color: #353e488e;
  58. }
  59. .pullDown select {
  60. height: 30px;
  61. width: 90%;
  62. margin-left: 12px;
  63. color: white;
  64. border-radius: 5px;
  65. background: none;
  66. line-height: 35px;
  67. }
  68. .gltfPullDown select option {
  69. background-color: #353e488e;
  70. height: 25px;
  71. color: white;
  72. }
  73. .gltfPullDown select :hover {
  74. background-color: gray;
  75. }
  76. .gltfImg {
  77. width: 112%;
  78. height: 95%;
  79. margin-left: -23px;
  80. overflow-x: hidden;
  81. overflow-y: auto;
  82. }
  83. .gltfImg li {
  84. display: inline-block;
  85. /* margin-left: -25px; */
  86. margin-right: 25px;
  87. width: 100px;
  88. text-align: center;
  89. padding-bottom: 10px;
  90. }
  91. .gltfImg img {
  92. width: 105px;
  93. height: 90px;
  94. border: 1.5px solid white;
  95. }
  96. </style>
  97. </head>
  98. <body class="dark">
  99. <!--加载前进行操作提示,优化用户体验-->
  100. <div id="mask" class="signmask" onclick="removeMask()"></div>
  101. <div id="mars3dContainer" class="mars3d-container"></div>
  102. <div class="infoview">
  103. <div class="checkbox checkbox-primary checkbox-inline">
  104. <input id="chkTestTerrain" class="styled" type="checkbox" />
  105. <label for="chkTestTerrain"> 深度检测 </label>
  106. </div>
  107. <div class="checkbox checkbox-primary checkbox-inline" title="屏蔽拾取地形坐标,避免穿透3dtiles模型">
  108. <input id="chk_onlyPickModelPosition" class="styled" type="checkbox" />
  109. <label for="chk_onlyPickModelPosition"> 仅在3dtiles上标绘 </label>
  110. </div>
  111. <input id="btnImpFile" type="button" class="btn btn-primary" value="打开" />
  112. <input id="btnSave" type="button" class="btn btn-primary" value="保存" />
  113. <input id="input_plot_file" type="file" accept=".json" style="display: none" />
  114. <input type="button" class="btn btn-danger" value="清除" onclick="deleteAll()" />
  115. </div>
  116. <!-- 下拉列表和图片列表的整体 -->
  117. <div class="infoview gltfPullDown">
  118. <!-- 下拉列表 -->
  119. <div class="pullDown">
  120. <select name="gltfPullDown" onchange="pullDown()"></select>
  121. </div>
  122. <!-- 模型图片 -->
  123. <div class="gltfImg"></div>
  124. </div>
  125. <!-- 切换视角到模型或地形(山区)的快捷按钮 -->
  126. <script type="text/javascript" src="js/center_terrain_3dtiles.js"></script>
  127. <script src="./js/common.js"></script>
  128. <script type="text/javascript">
  129. "use script"; //开发环境建议开启严格模式
  130. var map;
  131. var graphicLayer;
  132. var gltfListObj;
  133. let gltfItemObj;
  134. function initMap(options) {
  135. //合并属性参数,可覆盖config.json中的对应配置
  136. var mapOptions = mars3d.Util.merge(options, {
  137. scene: {
  138. center: { lat: 30.83351, lng: 116.354467, alt: 2743, heading: 359, pitch: -52 },
  139. clock: {
  140. currentTime: "2021-01-01 22:00:00",
  141. },
  142. },
  143. });
  144. //创建三维地球场景
  145. map = new mars3d.Map("mars3dContainer", mapOptions);
  146. //固定光照,避免gltf模型随时间存在亮度不一致。
  147. map.fixedLight = true;
  148. this.graphicLayer = new mars3d.layer.GraphicLayer({
  149. hasEdit: true,
  150. isAutoEditing: true, //绘制完成后是否自动激活编辑
  151. });
  152. map.addLayer(graphicLayer);
  153. var globe = map.scene.globe;
  154. $("#chkTestTerrain").change(function () {
  155. var val = $(this).is(":checked");
  156. map.scene.globe.depthTestAgainstTerrain = val;
  157. if (val) {
  158. toastr.info("深度监测打开后,您将无法看到地下或被地形遮挡的对象。");
  159. }
  160. });
  161. $("#chk_onlyPickModelPosition").change(function () {
  162. var val = $(this).is(":checked");
  163. //控制鼠标只取模型上的点,忽略地形上的点的拾取
  164. map.onlyPickModelPosition = val;
  165. });
  166. //创建完成,事件监听
  167. graphicLayer.on(mars3d.EventType.drawCreated, function (e) {
  168. var graphic = e.graphic;
  169. if (!graphicLayer.isContinued) {
  170. startEditing(graphic);
  171. }
  172. });
  173. //编辑修改了模型
  174. graphicLayer.on(
  175. [mars3d.EventType.editStart, mars3d.EventType.editMovePoint, mars3d.EventType.editStyle, mars3d.EventType.editRemovePoint],
  176. function (e) {
  177. var graphic = e.graphic;
  178. startEditing(graphic);
  179. }
  180. );
  181. //停止编辑修改模型
  182. graphicLayer.on([mars3d.EventType.editStop, mars3d.EventType.removeGraphic], function (e) {
  183. stopEditing();
  184. });
  185. $("#btnSave").click(function () {
  186. var strResult = graphicLayer.toGeoJSON();
  187. if (strResult == null) {
  188. layer.msg("当前没有标注任何数据,无需保存!");
  189. return;
  190. }
  191. haoutil.file.downloadFile("模型标绘.json", JSON.stringify(strResult));
  192. });
  193. $("#input_plot_file").change(function (e) {
  194. var file = this.files[0];
  195. var fileName = file.name;
  196. var fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase();
  197. if (fileType != "json") {
  198. layer.msg("文件类型不合法,请选择json格式标注文件!");
  199. clearSelectFile();
  200. return;
  201. }
  202. if (window.FileReader) {
  203. var reader = new FileReader();
  204. reader.readAsText(file, "UTF-8");
  205. reader.onloadend = function (e) {
  206. var json = this.result;
  207. graphicLayer.loadGeoJSON(json, {
  208. clear: true,
  209. flyTo: true,
  210. });
  211. clearSelectFile();
  212. };
  213. }
  214. });
  215. $("#btnImpFile").click(function () {
  216. $("#input_plot_file").click();
  217. });
  218. queryModelListData()
  219. .then(function (data) {
  220. initSelect(data);
  221. })
  222. .catch(function () {
  223. console.log("获取数据失败");
  224. });
  225. }
  226. // 下面是获取模型库的JSON数据
  227. function queryModelListData() {
  228. return new Promise(function (resolve, reject) {
  229. $.ajax({
  230. url: "//data.mars3d.cn/gltf/list.json",
  231. type: "GET",
  232. dataType: "json",
  233. success: function (data) {
  234. resolve(data);
  235. },
  236. error: function (data) {
  237. reject(data);
  238. },
  239. });
  240. });
  241. }
  242. function clearSelectFile() {
  243. if (!window.addEventListener) {
  244. document.getElementById("input_plot_file").outerHTML += ""; //IE
  245. } else {
  246. document.getElementById("input_plot_file").value = ""; //FF
  247. }
  248. }
  249. function deleteAll() {
  250. graphicLayer.clear();
  251. }
  252. // 创建下拉列表
  253. function initSelect(data) {
  254. gltfListObj = data;
  255. for (let key in data) {
  256. let html = `<option value="${key}">${key}(${data[key].length})</option>`;
  257. $(".pullDown select").append(html);
  258. }
  259. changeImgs(0);
  260. }
  261. //点击下拉框选项,切换图片列表
  262. function pullDown() {
  263. var index = $(".pullDown select option").index($(".pullDown select option:selected"));
  264. changeImgs(index);
  265. }
  266. //点击下拉列表,切换图片列表
  267. function changeImgs(index) {
  268. let arrName = Object.keys(gltfListObj);
  269. let arr = gltfListObj[arrName[index]];
  270. gltfItemObj = {};
  271. let html = `<ul>`;
  272. for (let i = 0; i < arr.length; i++) {
  273. let item = arr[i];
  274. item.image = mars3d.Util.template(item.image, { gltfServerUrl: "//data.mars3d.cn/gltf" });
  275. item.style.url = mars3d.Util.template(item.style.url, { gltfServerUrl: "//data.mars3d.cn/gltf" });
  276. gltfItemObj[i] = item;
  277. html += `<li onclick="drawGltf(${i})">
  278. <img src="${item.image}" alt="${name}"/>
  279. </li>`;
  280. }
  281. html += `</ul>`;
  282. $(".gltfImg").html(html);
  283. }
  284. // 绘制模型
  285. function drawGltf(index) {
  286. let item = gltfItemObj[index];
  287. console.log(item);
  288. graphicLayer.startDraw({
  289. type: "model",
  290. drawShow: true, //绘制时,是否显示模型,可避免在3dtiles上拾取坐标存在问题。
  291. style: item.style,
  292. });
  293. }
  294. //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】
  295. var timeTik;
  296. function startEditing(graphic) {
  297. clearTimeout(timeTik);
  298. var plotAttr = mars3d.widget.getClass("widgets/plotAttr/widget.js");
  299. if (plotAttr && plotAttr.isActivate) {
  300. plotAttr.startEditing(graphic, graphic.coordinates);
  301. } else {
  302. mars3d.widget.activate({
  303. map: map,
  304. uri: "widgets/plotAttr/widget.js",
  305. name: "属性编辑",
  306. graphic: graphic,
  307. lonlats: graphic.coordinates,
  308. });
  309. }
  310. }
  311. function stopEditing() {
  312. timeTik = setTimeout(function () {
  313. mars3d.widget.disable("widgets/plotAttr/widget.js");
  314. }, 200);
  315. }
  316. //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】
  317. </script>
  318. </body>
  319. </html>