model.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * [ONEMAP.M.projectController]
  3. * @return {[object]}
  4. */
  5. define(function () {
  6. /**
  7. * 初始化并订阅事件
  8. * @return {[type]} [description]
  9. */
  10. /**
  11. * 模块数据 用于数据存储和外部调用
  12. * @type {Object}
  13. * 数据存放
  14. */
  15. var modValue = {
  16. QXSY: {},
  17. cancelSelected: null,
  18. }
  19. function prototypeBind() {
  20. //1.0GLTG插件
  21. var fixGltf = function (gltf) {
  22. if (!gltf.extensionsUsed) {
  23. return;
  24. }
  25. var v = gltf.extensionsUsed.indexOf('KHR_technique_webgl');
  26. var t = gltf.extensionsRequired.indexOf('KHR_technique_webgl');
  27. // 中招了。。
  28. if (v !== -1) {
  29. gltf.extensionsRequired.splice(t, 1, 'KHR_techniques_webgl');
  30. gltf.extensionsUsed.splice(v, 1, 'KHR_techniques_webgl');
  31. gltf.extensions = gltf.extensions || {};
  32. gltf.extensions['KHR_techniques_webgl'] = {};
  33. gltf.extensions['KHR_techniques_webgl'].programs = gltf.programs;
  34. gltf.extensions['KHR_techniques_webgl'].shaders = gltf.shaders;
  35. gltf.extensions['KHR_techniques_webgl'].techniques = gltf.techniques;
  36. var techniques = gltf.extensions['KHR_techniques_webgl'].techniques;
  37. gltf.materials.forEach(function (mat, index) {
  38. gltf.materials[index].extensions['KHR_technique_webgl'].values = gltf.materials[index].values;
  39. gltf.materials[index].extensions['KHR_techniques_webgl'] = gltf.materials[index].extensions['KHR_technique_webgl'];
  40. var vtxfMaterialExtension = gltf.materials[index].extensions['KHR_techniques_webgl'];
  41. for (var value in vtxfMaterialExtension.values) {
  42. var us = techniques[vtxfMaterialExtension.technique].uniforms;
  43. for (var key in us) {
  44. if (us[key] === value) {
  45. vtxfMaterialExtension.values[key] = vtxfMaterialExtension.values[value];
  46. delete vtxfMaterialExtension.values[value];
  47. break;
  48. }
  49. }
  50. };
  51. });
  52. techniques.forEach(function (t) {
  53. for (var attribute in t.attributes) {
  54. var name = t.attributes[attribute];
  55. t.attributes[attribute] = t.parameters[name];
  56. };
  57. for (var uniform in t.uniforms) {
  58. var name = t.uniforms[uniform];
  59. t.uniforms[uniform] = t.parameters[name];
  60. };
  61. });
  62. }
  63. }
  64. Object.defineProperties(Cesium.Model.prototype, {
  65. _cachedGltf: {
  66. set: function (value) {
  67. this._vtxf_cachedGltf = value;
  68. if (this._vtxf_cachedGltf && this._vtxf_cachedGltf._gltf) {
  69. fixGltf(this._vtxf_cachedGltf._gltf);
  70. }
  71. },
  72. get: function () {
  73. return this._vtxf_cachedGltf;
  74. }
  75. }
  76. });
  77. }
  78. function add(item, cancelSelected) {
  79. modValue.cancelSelected = cancelSelected
  80. // if (ONEMAP.M.myLayers.checkLength() >= map23DConfig.layerMaxLength) {
  81. // ONEMAP.C.publisher.publish({ type: 'warning', message: '图层数量已达上限' }, 'noteBar::add');
  82. // modValue.cancelSelected(item.id)
  83. // return;
  84. // }
  85. var modelData = {
  86. "category": "倾斜摄影",
  87. "info": "",
  88. "name": item.title,
  89. "url": item.c_url,
  90. "layerInfo": item
  91. }
  92. var guid = null
  93. var options = {};
  94. guid = add3DQX(modelData);
  95. options = {
  96. action: "add",
  97. DOM: {
  98. guid: guid,
  99. type: "QXSY",
  100. name: item.title,
  101. },
  102. mod: "QXSY"
  103. }
  104. if (!guid) {
  105. return
  106. }
  107. ONEMAP.M.myLayers.myLayerControl(options); // 添加信息到“我的图层”
  108. ONEMAP.C.publisher.subscribe(layerAction, guid);
  109. }
  110. function remove(options) {
  111. var guid = options.guid ? options.guid : "QXSY--" + options.id;
  112. modValue.cancelSelected(guid);
  113. removeQXSY({ guid: guid });
  114. ONEMAP.M.myLayers.myLayerControl({
  115. action: "remove",
  116. DOMid: guid
  117. });
  118. // 移除数据层
  119. }
  120. function layerAction(options) {
  121. if (options.guid.split("--")[0] == "QXSY") {
  122. if (options.action == "remove") {
  123. remove(options)
  124. // 执行传入方法,取消勾选
  125. modValue.cancelSelected(options.guid.split("--")[1]);
  126. } else if (options.action == "opacity") {
  127. opacityQXSY(options)
  128. } else if (options.action == "controlShow") {
  129. controlShowQXSY(options)
  130. } else if (options.action == "location") {
  131. locationQXSY(options)
  132. }
  133. }
  134. }
  135. /**
  136. * 加载/移除倾斜摄影
  137. * @return {[type]} [description]
  138. */
  139. function add3DQX(options) {
  140. // var guid = map23DControl.buildGuid('QXSY-' + options.layerInfo.id + "-");
  141. var guid = 'QXSY--' + options.layerInfo.id
  142. var tilesetUrl = options.url
  143. var tileset = new Cesium.Cesium3DTileset({
  144. url: tilesetUrl,
  145. show: true,
  146. });
  147. modValue.tileset = tileset
  148. map3DViewer.map.scene.primitives.add(tileset, 1);
  149. tileset.readyPromise.then(function (tileset) {
  150. // mapControl.viewer.camera.viewBoundingSphere(tileset.boundingSphere, new Cesium.HeadingPitchRange(0, -0.5, 0));
  151. // changeHeight(0)
  152. });
  153. map3DViewer.map.zoomTo(tileset);
  154. modValue.QXSY[guid] = tileset
  155. return guid;
  156. };
  157. function removeQXSY(options) {
  158. map3DViewer.map.scene.primitives.remove(modValue.QXSY[options.guid]);
  159. delete modValue.QXSY[options.guid];
  160. }
  161. function opacityQXSY(options) {
  162. // if (options.options.opacity)
  163. // modValue.QXSY[options.guid].show = true
  164. // if (!options.options.opacity)
  165. // modValue.QXSY[options.guid].show = false
  166. modValue.QXSY[options.guid].style = new Cesium.Cesium3DTileStyle({
  167. // //写法一
  168. color: "color('rgba(255,255,255," + options.options.opacity + ")')",
  169. // //写法2
  170. // color: {
  171. // evaluateColor: function (feature, result) {
  172. // let color = new Cesium.Color(1, 1, 1, options.options.opacity);
  173. // return color;
  174. // }
  175. // },
  176. show: true,
  177. })
  178. }
  179. function controlShowQXSY(options) {
  180. if (options.options.show)
  181. modValue.QXSY[options.guid].show = true
  182. if (!options.options.show)
  183. modValue.QXSY[options.guid].show = false
  184. }
  185. function locationQXSY(options) {
  186. map3DViewer.map.zoomTo(modValue.QXSY[options.guid])
  187. }
  188. return ONEMAP.M.threeData = {
  189. add: add,
  190. remove: remove,
  191. }
  192. });