sdk.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. import cover from "./cover/index";
  2. import { createLoadPack, loadLib } from "@/utils";
  3. import {
  4. FuseModelAttrs,
  5. FuseModel,
  6. GuidePath,
  7. MeasureType,
  8. Measure as StoreMeasure,
  9. MeasurePosition,
  10. SceneType,
  11. scenes,
  12. Scene,
  13. } from "@/store";
  14. import type { Emitter } from "mitt";
  15. import {
  16. AnimationModel,
  17. AnimationModelAction,
  18. AnimationModelFrame,
  19. AnimationModelPath,
  20. TaggingPositionType,
  21. } from "@/api";
  22. import { Pos } from "@/components/drawing/dec";
  23. export enum SettingResourceType {
  24. map = "map",
  25. color = "color",
  26. envImage = "img",
  27. bottomImage = "bimg",
  28. icon = "icon",
  29. }
  30. type SceneModelAttrs = FuseModelAttrs & { select: boolean };
  31. export type SceneModel = ToChangeAPI<SceneModelAttrs> & {
  32. bus: Emitter<
  33. Pick<SceneModelAttrs, "select"> & {
  34. loadError: void;
  35. loadDone: void;
  36. loadProgress: number;
  37. changeSelect: boolean;
  38. transformChanged: {
  39. position?: SceneLocalPos;
  40. scale?: number;
  41. rotation?: SceneLocalPos;
  42. bottom?: number;
  43. };
  44. }
  45. >;
  46. destroy: () => void;
  47. moveModelTo: (mouse: {x: number, y: number}, pos?: Pos) => void
  48. enterScaleMode: () => void;
  49. enterRotateMode: () => void;
  50. enterMoveMode: () => void;
  51. leaveTransform: () => void;
  52. getDefaultRotation: () => SceneLocalPos;
  53. enterAlignment: () => void;
  54. leaveAlignment: () => void;
  55. enterScaleSet: () => ScaleSet;
  56. leaveScaleSet: () => void;
  57. supportPano: () => boolean;
  58. flyInPano: () => void;
  59. flyOutPano: () => void;
  60. };
  61. export interface ScaleSet {
  62. setLength: (length: number) => void;
  63. startMeasure: () => void;
  64. }
  65. export type ModelAttrRange = {
  66. [key in "opacity" | "bottom" | "scale" as `${key}Range`]: {
  67. min: number;
  68. max: number;
  69. step: number;
  70. };
  71. };
  72. export type AddModelProps = Pick<FuseModel, "url" | "id"> &
  73. FuseModelAttrs & {
  74. type: string;
  75. isDynamicAdded: boolean;
  76. mode: "many" | "single";
  77. fromType: any;
  78. } & ModelAttrRange;
  79. export type SceneGuidePath = Pick<GuidePath, "speed" | "time"> & Pose;
  80. export interface SceneGuide {
  81. bus: Emitter<{ changePoint: number; playComplete: void }>;
  82. play: () => void;
  83. pause: () => void;
  84. clear: () => void;
  85. }
  86. export type ScenePos = { localPos: SceneLocalPos; modelId: FuseModel["id"] };
  87. export type ScreenPos = {
  88. trueSide: boolean;
  89. pos: ScreenLocalPos;
  90. modelId: FuseModel["id"];
  91. };
  92. export interface CameraComeToProps {
  93. position: SceneLocalPos;
  94. target?: SceneLocalPos;
  95. dur?: number;
  96. modelId?: FuseModel["id"];
  97. distance?: 1 | 2 | 3;
  98. maxDis?: number;
  99. isFlyToTag?: boolean;
  100. }
  101. export type CalcPathProps = [
  102. [SceneGuidePath, SceneGuidePath],
  103. Partial<Pick<SceneGuidePath, "time" | "speed">>
  104. ];
  105. export interface MeasureBase {
  106. destroy?: () => void;
  107. show: () => void;
  108. hide: () => void;
  109. fly: () => void;
  110. bus: Emitter<{
  111. update: [MeasurePosition["point"][], MeasurePosition["modelId"][]];
  112. highlight: boolean;
  113. }>;
  114. changeSelect: (isHight: boolean) => void;
  115. setPositions: (
  116. points: MeasurePosition["point"][],
  117. modelIds: MeasurePosition["modelId"][]
  118. ) => void;
  119. }
  120. export type Measure<T extends StoreMeasure["type"] = StoreMeasure["type"]> =
  121. MeasureBase &
  122. (T extends MeasureType.area
  123. ? { getArea: () => { value: number } }
  124. : { getDistance: () => { value: number } });
  125. export type StartMeasure<T extends StoreMeasure["type"]> = Measure<T> & {
  126. bus: Emitter<{
  127. submit: [MeasurePosition["point"][], MeasurePosition["modelId"][]];
  128. cancel: void;
  129. invalidPoint: string;
  130. }>;
  131. };
  132. export type Pose =
  133. | {
  134. position: SceneLocalPos;
  135. target: SceneLocalPos;
  136. }
  137. | {
  138. panoId: any;
  139. model: SceneModel;
  140. posInModel: SceneLocalPos;
  141. rotInModel: SceneLocalPos;
  142. position: SceneLocalPos;
  143. target: SceneLocalPos;
  144. };
  145. export interface SDK {
  146. layout: HTMLDivElement;
  147. sceneBus: Emitter<{
  148. cameraChange: SceneLocalPos;
  149. panoModelChange: SceneModel;
  150. modeChange: { mode: "pano" | "fuse"; active: SceneModel };
  151. }>;
  152. setBackdrop: (
  153. drop: string,
  154. type: SettingResourceType,
  155. tb: { scale?: number; rotate?: number }
  156. ) => void;
  157. switchScene: (
  158. scene: { type: SceneType; num: string } | null
  159. ) => Promise<void>;
  160. startAddSth: () => void;
  161. endAddSth: () => void;
  162. addModel: (props: AddModelProps) => SceneModel;
  163. setCameraFov: (fov: number) => void;
  164. enableMap(dom: HTMLDivElement, latlng: number[]): void;
  165. switchMapType: (type: string) => void;
  166. showGrid: () => void;
  167. compassVisibility: (visibility: boolean) => void;
  168. canTurnToPanoMode: () => { model: SceneModel };
  169. hideGrid: () => void;
  170. calcPathInfo: (
  171. paths: CalcPathProps[0],
  172. info: CalcPathProps[1]
  173. ) => Required<CalcPathProps[1]>;
  174. getPositionByScreen: (
  175. screenPos: ScreenLocalPos,
  176. modelId?: FuseModel["id"]
  177. ) =>
  178. | (ScenePos & { worldPos: SceneLocalPos; localNormal: SceneLocalPos })
  179. | null;
  180. getScreenByPosition: (
  181. localPos: SceneLocalPos,
  182. modelId?: FuseModel["id"]
  183. ) => ScreenPos | null;
  184. screenshot: (width: number, height: number) => Promise<string>;
  185. getPose: () => Pose;
  186. comeTo: (pos: CameraComeToProps) => void;
  187. comeToByLatLng: (pos: number[]) => void;
  188. enterSceneGuide: (data: SceneGuidePath[]) => SceneGuide;
  189. drawMeasure<T extends StoreMeasure["type"]>(
  190. type: T,
  191. points: MeasurePosition["point"][],
  192. modelIds: MeasurePosition["modelId"][]
  193. ): Measure<T>;
  194. startMeasure<T extends StoreMeasure["type"]>(type: T): StartMeasure<T>;
  195. createTagging: (props: Tagging3DProps) => Tagging3D;
  196. createPath: (props: PathProps) => Path;
  197. createAnimationGroup: () => AnimationGroup;
  198. }
  199. export type PathProps = {
  200. // 线段名称
  201. name: string;
  202. // 是否显示名称,
  203. showName: boolean;
  204. // 文字大小
  205. fontSize: number;
  206. // 是否显示方向,
  207. showDirection: boolean;
  208. // 方向是否反向
  209. reverseDirection: boolean;
  210. line: {
  211. width: number;
  212. color: string;
  213. altitudeAboveGround: number;
  214. position?: SceneLocalPos;
  215. modelId?: string;
  216. };
  217. points: {
  218. // 点位名称
  219. name: string;
  220. position: SceneLocalPos;
  221. modelId: string;
  222. }[];
  223. };
  224. export type Path = {
  225. bus: Emitter<{
  226. activePoint: number;
  227. // 标注点击事件
  228. click: void;
  229. // 鼠标移入标注事件
  230. enter: void;
  231. // 鼠标移出标注事件
  232. leave: void;
  233. // 线段坐标更改事件
  234. linePositionChange: {
  235. pos: SceneLocalPos;
  236. modelId: string;
  237. };
  238. focus: boolean;
  239. // 路径点位置变更
  240. changePoints: PathProps["points"];
  241. drawed: void;
  242. }>;
  243. changeDirection: (show: boolean, reverse: boolean) => void;
  244. changeFontSize: (fontSize: number) => void;
  245. focus: (f: boolean) => void;
  246. highlight: (f: boolean) => void;
  247. changeVisibilityRange: (range: number) => void;
  248. changePointName: (index: number, name: string) => void;
  249. // 飞向路径
  250. fly: () => void;
  251. // 更改路径点
  252. changePathPoints: (
  253. points: Omit<PathProps["points"][number], "name">[]
  254. ) => void;
  255. // 播放路径,相机沿着路径运动,传入播放完毕回调
  256. play: (playedHandler: () => void) => void;
  257. // 停止播放路径
  258. pause: () => void;
  259. // 是否可编辑
  260. changeCanEdit: (canMove: boolean) => void;
  261. deletePoint: (index: number) => void;
  262. changeEditMode: (editMode: boolean) => void;
  263. // 可见性
  264. visibility: (visibility: boolean) => void;
  265. // 气泡是否可见
  266. visibilityName: (visibility: boolean) => void;
  267. // 更改标题气泡属性
  268. changeLine: (props: Partial<PathProps["line"]>) => void;
  269. // 更改标题气泡属性
  270. changeName: (name: string) => void;
  271. // 线段销毁
  272. destroy: () => void;
  273. };
  274. export type Tagging3DProps = {
  275. lineHeight: number;
  276. fontSize: number;
  277. // 标题
  278. title: string;
  279. // 标注类型 2d | 3d
  280. type: TaggingPositionType;
  281. // 模型id
  282. modelId: string;
  283. // 贴地射线获取的位置
  284. position: SceneLocalPos;
  285. normal: SceneLocalPos;
  286. // 是否可以移动
  287. canMove: boolean;
  288. // 贴地图片url
  289. image: string;
  290. // 贴地图片的变换
  291. mat: {
  292. scale: number;
  293. rotation: number;
  294. };
  295. };
  296. export type Tagging3D = {
  297. bus: Emitter<{
  298. // 标注点击事件
  299. click: void;
  300. // 鼠标移入标注事件
  301. enter: void;
  302. // 鼠标移出标注事件
  303. leave: void;
  304. // 位置变更
  305. changePosition: {
  306. pos: SceneLocalPos;
  307. modelId: string;
  308. normal: SceneLocalPos;
  309. };
  310. }>;
  311. changePosition: (position: {
  312. modelId: string;
  313. // 贴地射线获取的位置
  314. position: SceneLocalPos;
  315. normal: SceneLocalPos;
  316. }) => void;
  317. changeFontSize: (fontSize: number) => void;
  318. changeLineHeight: (lineHeight: number) => void;
  319. // 设置标题
  320. changeTitle: (title: string) => void;
  321. // 标题是否可见
  322. visibilityTitle: (visibility: boolean) => void;
  323. // 更改可拖拽移动
  324. changeCanMove: (canMove: boolean) => void;
  325. // 获取图标中心三维坐标
  326. getImageCenter: () => SceneLocalPos;
  327. // 更改图标
  328. changeImage: (url: string) => void;
  329. // 标注可见性
  330. visibility: (visibility: boolean) => void;
  331. // 标注图片变换,传入变换
  332. changeMat: (props: Tagging3DProps["mat"]) => void;
  333. // 更改热点类型
  334. changeType: (val: TaggingPositionType) => void;
  335. // 距离相机位置
  336. getCameraDisSquared: () => number;
  337. // 标注销毁
  338. destroy: () => void;
  339. };
  340. // 动画组对象
  341. export type AnimationGroup = {
  342. // 播放
  343. play: () => void;
  344. // 暂停
  345. pause: () => void;
  346. // 添加动画模型
  347. addAnimationModel: (data: AnimationModel) => AnimationModel3D;
  348. delayEndTime: () => number
  349. // 设置当前时间, 单位为秒
  350. setCurrentTime: (s: number) => void;
  351. bus: Emitter<{
  352. currentTime: number;
  353. }>;
  354. };
  355. export type AnimationModel3D = {
  356. getSupportActions: () => string[]
  357. // 销毁动画模型
  358. destroy: () => void;
  359. changeShow: (focus: boolean) => void
  360. // 更改动画模型可见性
  361. changeSelect: (show: boolean) => void;
  362. // 更改动画可见范围 不传为全局可见
  363. changeVisibilityRange: (range?: number) => void;
  364. // 更改模型名称
  365. changeTitle: (name: string) => void;
  366. // 更改名称字体大小
  367. changeFontSize: (size: number) => void;
  368. // 更改名称可见性
  369. visibilityTitle: (show: boolean) => void;
  370. getModelPose: () => {
  371. position?: SceneLocalPos;
  372. scale?: number;
  373. rotation?: SceneLocalPos;
  374. originPosition?: SceneLocalPos;
  375. };
  376. // 添加模型帧
  377. addFrame: (frame: AnimationModelFrame) => AnimationModelFrame3D;
  378. // 添加模型动作
  379. addAction: (frame: AnimationModelAction) => AnimationModelAction3D;
  380. // 添加模型路径
  381. addPath: (
  382. frame: Omit<AnimationModelPath, "pathId"> & { path: Path }
  383. ) => AnimationModelPath3D;
  384. // 获取当前模型旁白出现的适合位置,传入旁边dom的宽高,返回像素位置
  385. getCurrentSubtitlePixel: (size: { width: number; height: number }) => {
  386. x: number;
  387. y: number;
  388. };
  389. // 获取当前时间改模型的姿态
  390. getCurrentMat: () => {
  391. position?: SceneLocalPos;
  392. scale?: number;
  393. rotation?: SceneLocalPos;
  394. originPosition?: SceneLocalPos;
  395. };
  396. // 进入旋转
  397. enterRotateMode: () => void
  398. enterMoveMode: () => void
  399. enterScaleMode: () => void
  400. leaveTransform: () => void;
  401. // 动画帧姿态修改数据
  402. bus: Emitter<{
  403. loadDone: void
  404. changeSelect: boolean;
  405. transformChanged: {
  406. byControl: boolean
  407. position?: SceneLocalPos;
  408. scale?: number;
  409. rotation?: SceneLocalPos;
  410. originPosition?: SceneLocalPos;
  411. };
  412. }>;
  413. };
  414. export type AnimationModelFrame3D = {
  415. // 销毁动画模型帧
  416. destroy: () => void;
  417. // 修改帧播放时间 单位为秒
  418. changeTime: (s: number) => void;
  419. setMat: (mat: any) => void
  420. };
  421. export type AnimationModelAction3D = {
  422. // 销毁动画模型动作
  423. destroy: () => void;
  424. // 修改动作播放时间 单位为秒
  425. changeTime: (s: number) => void;
  426. // 修改动作幅度
  427. changeAmplitude: (n: number) => void;
  428. // 修改动作速度
  429. changeSpeed: (n: number) => void;
  430. // 修改动持续时间 单位为秒
  431. changeDuration: (n: number) => void;
  432. };
  433. export type AnimationModelPath3D = {
  434. // 销毁动画模型路径
  435. destroy: () => void;
  436. // 修改路径 传入参数为你之前返回的路径对象
  437. changePath: (path: Path | undefined) => void;
  438. // 修改播放是否要反向
  439. changeReverse: (reverse: boolean) => void;
  440. // 修改路径播放时间 单位为秒
  441. changeTime: (s: number) => void;
  442. // 修改路径续时间 单位为秒
  443. changeDuration: (n: number) => void;
  444. };
  445. export let sdk: SDK;
  446. export type InialSDKProps = {
  447. laserRoot?: string;
  448. ossRoot?: string;
  449. laserOSSRoot?: string;
  450. panoOSSRoot?: string;
  451. layout: HTMLDivElement;
  452. scenes: Scene[];
  453. lonlat?: number[];
  454. };
  455. export let initialed = false;
  456. export const initialSDK = async (props: InialSDKProps) => {
  457. if (initialed) return sdk;
  458. initialed = true;
  459. const libs = [
  460. `./lib/proj4/proj4.js`,
  461. `./lib/jquery/jquery-3.1.1.min.js`,
  462. `./lib/other/BinaryHeap.js`,
  463. `./lib/other/hls.js`,
  464. `./lib/tween/tween.min.js`,
  465. `./lib/plasio/js/laslaz.js`,
  466. `./lib/plasio/vendor/bluebird.js`,
  467. `./lib/plasio/workers/laz-loader-worker.js`,
  468. `./lib/plasio/workers/laz-perf.js`,
  469. `./lib/Cesium/Cesium.js`,
  470. `./lib/shapefile/shapefile.js`,
  471. ];
  472. await Promise.all(libs.map(loadLib));
  473. await loadLib(`./lib/potree/potree.js`);
  474. console.log(props);
  475. const localSdk = cover({
  476. ...props,
  477. dom: props.layout,
  478. isLocal: false,
  479. scenes: props.scenes,
  480. lonlat: props.lonlat,
  481. mapDom: null,
  482. } as any) as unknown as SDK;
  483. (window as any).sdk = sdk = localSdk;
  484. sdk.layout = props.layout;
  485. return sdk;
  486. };
  487. export default sdk!;