fuseMode.ts 7.9 KB

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