model.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. var modelData = {
  81. "category": "倾斜摄影",
  82. "info": "",
  83. "name": item.title,
  84. "url": item.c_url,
  85. "layerInfo": item
  86. }
  87. var guid = null
  88. var options = {};
  89. guid = add3DQX(modelData);
  90. options = {
  91. action: "add",
  92. DOM: {
  93. guid: guid,
  94. type: "QXSY",
  95. name: item.title,
  96. },
  97. mod: "QXSY"
  98. }
  99. if (!guid) {
  100. return
  101. }
  102. ONEMAP.M.myLayers.myLayerControl(options); // 添加信息到“我的图层”
  103. ONEMAP.C.publisher.subscribe(layerAction, guid);
  104. }
  105. function remove(options) {
  106. var guid = options.guid ? options.guid : "QXSY--" + options.id;
  107. modValue.cancelSelected(guid);
  108. removeQXSY({ guid: guid });
  109. ONEMAP.M.myLayers.myLayerControl({
  110. action: "remove",
  111. DOMid: guid
  112. });
  113. // 移除数据层
  114. }
  115. function layerAction(options) {
  116. if (options.guid.split("--")[0] == "QXSY") {
  117. if (options.action == "remove") {
  118. remove(options)
  119. // 执行传入方法,取消勾选
  120. modValue.cancelSelected(options.guid.split("--")[1]);
  121. } else if (options.action == "opacity") {
  122. opacityQXSY(options)
  123. } else if (options.action == "controlShow") {
  124. controlShowQXSY(options)
  125. } else if (options.action == "location") {
  126. locationQXSY(options)
  127. }
  128. }
  129. }
  130. /**
  131. * 加载/移除倾斜摄影
  132. * @return {[type]} [description]
  133. */
  134. function add3DQX(options) {
  135. // var guid = map23DControl.buildGuid('QXSY-' + options.layerInfo.id + "-");
  136. var guid = 'QXSY--' + options.layerInfo.id
  137. var tilesetUrl = options.url
  138. var tileset = new Cesium.Cesium3DTileset({
  139. url: tilesetUrl,
  140. show: true,
  141. });
  142. modValue.tileset = tileset
  143. map3DViewer.map.scene.primitives.add(tileset, 1);
  144. tileset.readyPromise.then(function (tileset) {
  145. // mapControl.viewer.camera.viewBoundingSphere(tileset.boundingSphere, new Cesium.HeadingPitchRange(0, -0.5, 0));
  146. // changeHeight(0)
  147. });
  148. map3DViewer.map.zoomTo(tileset);
  149. modValue.QXSY[guid] = tileset
  150. return guid;
  151. };
  152. function removeQXSY(options) {
  153. map3DViewer.map.scene.primitives.remove(modValue.QXSY[options.guid]);
  154. delete modValue.QXSY[options.guid];
  155. }
  156. function opacityQXSY(options) {
  157. // if (options.options.opacity)
  158. // modValue.QXSY[options.guid].show = true
  159. // if (!options.options.opacity)
  160. // modValue.QXSY[options.guid].show = false
  161. modValue.QXSY[options.guid].style = new Cesium.Cesium3DTileStyle({
  162. // //写法一
  163. color: "color('rgba(255,255,255," + options.options.opacity + ")')",
  164. // //写法2
  165. // color: {
  166. // evaluateColor: function (feature, result) {
  167. // let color = new Cesium.Color(1, 1, 1, options.options.opacity);
  168. // return color;
  169. // }
  170. // },
  171. show: true,
  172. })
  173. }
  174. function controlShowQXSY(options) {
  175. if (options.options.show)
  176. modValue.QXSY[options.guid].show = true
  177. if (!options.options.show)
  178. modValue.QXSY[options.guid].show = false
  179. }
  180. function locationQXSY(options) {
  181. map3DViewer.map.zoomTo(modValue.QXSY[options.guid])
  182. }
  183. return ONEMAP.M.threeData = {
  184. add: add,
  185. remove: remove,
  186. }
  187. });