fuseMode.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import { SDK, SceneModel, ModelAttrRange } from "../sdk";
  2. import { toRaw, watch, reactive, ref } from "vue";
  3. import { custom, getResource, getResources } from "@/env";
  4. import {
  5. diffArrayChange,
  6. shallowWatchArray,
  7. arrayChildEffectScope,
  8. showLoad,
  9. hideLoad,
  10. deepIsRevise,
  11. round,
  12. } from "@/utils";
  13. import {
  14. dynamicAddedModelIds,
  15. fuseModels,
  16. getFuseModelShowVariable,
  17. SceneType,
  18. SceneStatus,
  19. FuseModel,
  20. FuseModels,
  21. } from "@/store";
  22. import { currentLayout, RoutesName } from "@/router";
  23. import { isUnSet, unSet } from "@/utils/unset";
  24. // -----------------模型关联--------------------
  25. export const modelRange: ModelAttrRange = {
  26. opacityRange: { min: 0, max: 100, step: 0.1 },
  27. bottomRange: { min: -30, max: 70, step: 0.1 },
  28. scaleRange: { min: 0.1, max: 200, step: 0.1 },
  29. };
  30. export const sceneModelMap = reactive(new Map<FuseModel, SceneModel>());
  31. export const getSceneModel = (model?: FuseModel | null) =>
  32. model && sceneModelMap.get(toRaw(model));
  33. export const getFuseModel = (model?: SceneModel | null) => {
  34. if (!model) return null;
  35. for (const [k, v] of sceneModelMap.entries()) {
  36. if (toRaw(v) === toRaw(model)) {
  37. return k;
  38. }
  39. }
  40. };
  41. const setModels = (sdk: SDK, models: FuseModels, oldModels: FuseModels) => {
  42. const { added, deleted } = diffArrayChange(models, oldModels);
  43. for (const item of added) {
  44. if (getSceneModel(item)) {
  45. continue;
  46. }
  47. if (item.status !== SceneStatus.SUCCESS) {
  48. item.error = true;
  49. item.loaded = true;
  50. continue;
  51. }
  52. const itemRaw = toRaw(item);
  53. let sceneModel: SceneModel;
  54. try {
  55. console.error('addMode', item.url, item.url.map(getResources))
  56. sceneModel = sdk.addModel({
  57. ...itemRaw,
  58. ...modelRange,
  59. mode: RoutesName.signModel === currentLayout.value! ? "single" : "many",
  60. isDynamicAdded: dynamicAddedModelIds.value.some(
  61. (id) => itemRaw.id === id
  62. ),
  63. type: [SceneType.SWSS, SceneType.SWYDSS].includes(item.type)
  64. ? "laser"
  65. : item.modelType,
  66. url: [SceneType.SWSS, SceneType.SWYDSS].includes(item.type)
  67. ? item.url
  68. : item.url && item.url.map(getResources),
  69. fromType: item.type,
  70. });
  71. } catch (e) {
  72. console.error("模型加载失败", e);
  73. item.error = true;
  74. return;
  75. }
  76. sceneModelMap.set(itemRaw, sceneModel);
  77. let changeId: NodeJS.Timeout;
  78. sceneModel.bus.on("transformChanged", (transform) => {
  79. clearTimeout(changeId);
  80. changeId = setTimeout(() => {
  81. transform = { ...transform };
  82. if (transform.rotation) {
  83. transform.rotation = {
  84. x: round(transform.rotation.x, 5),
  85. y: round(transform.rotation.y, 5),
  86. z: round(transform.rotation.z, 5),
  87. };
  88. }
  89. if (transform.position) {
  90. transform.position = {
  91. x: round(transform.position.x, 5),
  92. y: round(transform.position.y, 5),
  93. z: round(transform.position.z, 5),
  94. };
  95. }
  96. delete transform.bottom;
  97. // if (transform.bottom) {
  98. // transform.bottom = round(transform.bottom, 2)
  99. // }
  100. if (transform.scale) {
  101. transform.scale = round(transform.scale, 2);
  102. }
  103. const updateKeys = Object.keys(transform);
  104. const update: any = {};
  105. for (const key of updateKeys) {
  106. update[key] = (item as any)[key];
  107. }
  108. if (deepIsRevise(update, transform)) {
  109. console.error('change', item)
  110. unSet(() => Object.assign(item, transform));
  111. }
  112. }, 16);
  113. });
  114. sceneModel.bus.on("changeSelect", (select) => {
  115. unSet(() => {
  116. if (custom.showMode === "fuse") {
  117. if (custom.currentModel === item && !select) {
  118. custom.currentModel = null;
  119. } else if (custom.currentModel !== item && select) {
  120. custom.currentModel = item;
  121. }
  122. }
  123. });
  124. });
  125. showLoad();
  126. sceneModel.bus.on("loadDone", () => {
  127. item.loaded = true;
  128. hideLoad();
  129. });
  130. sceneModel.bus.on("loadError", () => {
  131. item.error = true;
  132. item.show = false;
  133. custom.showModelsMap.delete(item);
  134. hideLoad();
  135. });
  136. sceneModel.bus.on("loadProgress", (progress) => (item.progress = progress));
  137. }
  138. for (const item of deleted) {
  139. console.error("销毁", item);
  140. getSceneModel(item)?.destroy();
  141. sceneModelMap.delete(item);
  142. }
  143. };
  144. export const activeModel = (status: {
  145. showMode: "fuse" | "pano";
  146. active?: FuseModel;
  147. fore?: boolean
  148. }) => {
  149. const oldStatus = {
  150. showMode: custom.showMode,
  151. active: custom.currentModel,
  152. };
  153. if (
  154. toRaw(status.active) === toRaw(oldStatus.active) &&
  155. status.showMode === oldStatus.showMode
  156. ) {
  157. return;
  158. }
  159. const model = status.active && getSceneModel(status.active)!;
  160. const oldModel = oldStatus.active && getSceneModel(oldStatus.active)!;
  161. if (oldModel) {
  162. oldModel.changeSelect(false);
  163. }
  164. if (model && status.active === oldStatus.active) {
  165. if (status.showMode === "pano") {
  166. model && model.flyInPano();
  167. } else {
  168. model && model.flyOutPano();
  169. }
  170. } else {
  171. if (oldStatus.showMode !== status.showMode) {
  172. if (oldStatus.showMode === "pano") {
  173. oldModel && oldModel.flyOutPano();
  174. }
  175. }
  176. if (status.showMode === "pano") {
  177. model && model.flyInPano();
  178. } else {
  179. console.log("select");
  180. }
  181. }
  182. setTimeout(() => {
  183. if (status.showMode !== "pano" && model) {
  184. console.error(status)
  185. if (oldStatus.showMode !== 'pano' || status.fore) {
  186. model && model.changeSelect(true);
  187. }
  188. }
  189. }, 35);
  190. custom.currentModel = status.active!;
  191. custom.showMode = status.showMode;
  192. };
  193. export const associationModels = (sdk: SDK) => {
  194. sdk.sceneBus.on("modeChange", (data) => {
  195. custom.showMode = data.mode;
  196. if (data.active) {
  197. custom.currentModel = getFuseModel(data.active)!;
  198. }
  199. });
  200. sdk.sceneBus.on("panoModelChange", (data) => {
  201. custom.showMode = "pano";
  202. custom.currentModel = getFuseModel(data)!;
  203. });
  204. const getModels = () =>
  205. fuseModels.value.filter(
  206. (model) => getSceneModel(model) || getFuseModelShowVariable(model).value
  207. );
  208. shallowWatchArray(getModels, (models, oldModels) => {
  209. setModels(sdk, models, oldModels);
  210. });
  211. arrayChildEffectScope(getModels, (item) => {
  212. const stopLoadedWatch = watch(
  213. () => item.loaded,
  214. (loaded) => {
  215. if (loaded) {
  216. const modelShow = getFuseModelShowVariable(item);
  217. watch(
  218. () => item.bottom,
  219. () => isUnSet || getSceneModel(item)?.changeBottom(item.bottom)
  220. // { immediate: true }
  221. );
  222. watch(
  223. () => item.opacity,
  224. () => isUnSet || getSceneModel(item)?.changeOpacity(item.opacity)
  225. // { immediate: true }
  226. );
  227. watch(
  228. () => item.scale,
  229. () => isUnSet || getSceneModel(item)?.changeScale(item.scale)
  230. // { immediate: true }
  231. );
  232. watch(
  233. () => item.position,
  234. () => {
  235. if (!isUnSet) {
  236. console.log('position', item.raw.modelTitle, toRaw(item.position))
  237. getSceneModel(item)?.changePosition(item.position);
  238. }
  239. }
  240. // { immediate: true }
  241. );
  242. watch(
  243. () => item.rotation,
  244. () => {
  245. if (!isUnSet) {
  246. console.log('rotation', item.raw.modelTitle, toRaw(item.rotation))
  247. getSceneModel(item)?.changeRotation(toRaw(item.rotation));
  248. }
  249. }
  250. // { immediate: true }
  251. );
  252. watch(
  253. () => modelShow.value,
  254. () => {
  255. const sceneModel = getSceneModel(item);
  256. if (!isUnSet && sceneModel) {
  257. sceneModel.changeSelect(false);
  258. sceneModel.changeShow(modelShow.value);
  259. }
  260. },
  261. { immediate: true }
  262. );
  263. stopLoadedWatch();
  264. }
  265. }
  266. // { immediate: true }
  267. );
  268. });
  269. };