deeptwins-engine-3d 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deeptwins-engine-3d",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "map for 3d",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/esm/index.d.ts",
@@ -1,103 +0,0 @@
1
- import type { Viewer } from 'deeptwins-cesium';
2
- /**
3
- * 位置信息
4
- */
5
- interface Position {
6
- lng: number;
7
- lat: number;
8
- alt: number;
9
- }
10
- export default class ModelRoamUpDate {
11
- readonly _map: any;
12
- modelOptions: any;
13
- polylineOptions: any;
14
- private follow;
15
- flyTo: boolean;
16
- private readonly _onPositionUpdate;
17
- modelLayer: any;
18
- polylineLayer: any;
19
- private positions;
20
- private positionsLineC3;
21
- private positionInterC3;
22
- private _eventPreUpdate;
23
- private modelMatrix;
24
- private hpRoll;
25
- private _time;
26
- status: 'stop' | 'running' | 'destroy';
27
- private currentPathIndex;
28
- private maxPathIndex;
29
- private targetHeading;
30
- private currentHeading;
31
- private rotationSpeed;
32
- private firstCameraSettings;
33
- private thirdPersonSettings;
34
- constructor(map: Viewer, options: any);
35
- /**
36
- * 实时添加点位
37
- * @param newPosition 新位置点
38
- * @throws 如果新位置与上一个位置完全相同
39
- */
40
- updatePosition(newPosition: Position): void;
41
- /**
42
- * 初始化模型
43
- * 加载模型
44
- * @param initialPosition 初始位置
45
- */
46
- private initModel;
47
- /**
48
- * 渲染回调函数
49
- * 更新模型位置、朝向和路径线
50
- */
51
- private updateModel;
52
- /**
53
- * 计算模型朝向
54
- * @param start 起始点
55
- * @param end 终点
56
- */
57
- private getHeading;
58
- /**
59
- * 更新模型朝向
60
- */
61
- private updateHeading;
62
- /**
63
- * 更新相机视角
64
- * 根据当前模式更新相机位置和朝向
65
- */
66
- private updateCameraView;
67
- /**
68
- * 更新第一人称视角
69
- * @param position 模型当前位置
70
- */
71
- private updateFirstPersonView;
72
- /**
73
- * 更新第三人称视角
74
- * @param position 模型当前位置
75
- */
76
- private updateThirdPersonView;
77
- /**
78
- * 设置第一人称视角
79
- */
80
- setFollowFirst(options: any): void;
81
- /**
82
- * 设置第三人称视角
83
- */
84
- setFollowThird(options: any): void;
85
- /**
86
- * 取消视角跟随
87
- */
88
- cancelFollow(): void;
89
- /**
90
- * 飞行定位到模型
91
- */
92
- private _flyToModel;
93
- /**
94
- * 监听
95
- */
96
- on(type: any, event: any): any;
97
- /**
98
- * 销毁实例
99
- * 清理所有资源
100
- */
101
- destroy(): void;
102
- }
103
- export {};
@@ -1,433 +0,0 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
- function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
4
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
5
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
6
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
7
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
9
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
10
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
11
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
12
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
13
- import * as Cesium from 'deeptwins-cesium';
14
- import { cloneDeep, merge } from 'lodash';
15
- import { v4 as uuidv4 } from 'uuid';
16
- import * as constant from "../constant";
17
- import { colorString } from "../tool/utils";
18
- /**
19
- * 位置信息
20
- */
21
-
22
- /**
23
- * 相机设置
24
- */
25
- var ModelRoamUpDate = /*#__PURE__*/function () {
26
- function ModelRoamUpDate(map, options) {
27
- var _this = this,
28
- _options$model;
29
- _classCallCheck(this, ModelRoamUpDate);
30
- //传入的参数
31
- _defineProperty(this, "_map", void 0);
32
- // 模型参数
33
- _defineProperty(this, "modelOptions", void 0);
34
- // 轨迹参数
35
- _defineProperty(this, "polylineOptions", void 0);
36
- // 视角
37
- _defineProperty(this, "follow", null);
38
- // 是否定位到模型位置
39
- _defineProperty(this, "flyTo", false);
40
- // 位置更新触发回调
41
- _defineProperty(this, "_onPositionUpdate", null);
42
- // 模型layer
43
- _defineProperty(this, "modelLayer", void 0);
44
- // 轨迹layer
45
- _defineProperty(this, "polylineLayer", void 0);
46
- // 所有位置信息点
47
- _defineProperty(this, "positions", []);
48
- // 所有轨迹位置信息点 Cartesian3
49
- _defineProperty(this, "positionsLineC3", []);
50
- // 插值后的位置信息点 Cartesian3
51
- _defineProperty(this, "positionInterC3", []);
52
- // 帧率更新的事件
53
- _defineProperty(this, "_eventPreUpdate", void 0);
54
- // 模型参数
55
- // 模型变换矩阵
56
- _defineProperty(this, "modelMatrix", new Cesium.Matrix4());
57
- // 模型姿态
58
- _defineProperty(this, "hpRoll", new Cesium.HeadingPitchRoll());
59
- // 漫游相关
60
- // 上个点位的时间
61
- _defineProperty(this, "_time", new Date().getTime());
62
- // 状态
63
- _defineProperty(this, "status", 'stop');
64
- // 当前漫游路径点索引
65
- _defineProperty(this, "currentPathIndex", 0);
66
- // 最大路径点索引
67
- _defineProperty(this, "maxPathIndex", 0);
68
- // 目标朝向角度
69
- _defineProperty(this, "targetHeading", 0);
70
- // 当前朝向角度
71
- _defineProperty(this, "currentHeading", 0);
72
- // 转向速度(弧度/帧)
73
- _defineProperty(this, "rotationSpeed", 0.05);
74
- // 第一人称视角设置
75
- _defineProperty(this, "firstCameraSettings", {
76
- distance: 0,
77
- pitch: -45
78
- });
79
- // 第三人称视角设置
80
- _defineProperty(this, "thirdPersonSettings", {
81
- distance: 500,
82
- pitch: -45
83
- });
84
- /**
85
- * 渲染回调函数
86
- * 更新模型位置、朝向和路径线
87
- */
88
- _defineProperty(this, "updateModel", function () {
89
- if (_this.currentPathIndex >= _this.maxPathIndex) {
90
- return;
91
- }
92
- if (_this.currentPathIndex > 0 && _this.currentPathIndex < _this.positionInterC3.length - 1 && !_this.positionInterC3[_this.currentPathIndex - 1].equals(_this.positionInterC3[_this.currentPathIndex])) {
93
- _this.getHeading(_this.positionInterC3[_this.currentPathIndex - 1], _this.positionInterC3[_this.currentPathIndex]);
94
- }
95
-
96
- // 更新朝向
97
- _this.updateHeading();
98
- var currentPos = _this.positionInterC3[_this.currentPathIndex];
99
- if (!_this.positionsLineC3.some(function (pos) {
100
- return pos.equals(currentPos);
101
- })) {
102
- _this.positionsLineC3.push(currentPos);
103
- if (_this.positions.length > 1) {
104
- // 更新轨迹
105
- _this.polylineLayer && _this._map.scene.primitives.remove(_this.polylineLayer);
106
- var trailGeometry = new Cesium.GeometryInstance({
107
- id: '_modelRoamUpDatePolyline',
108
- geometry: new Cesium.PolylineGeometry({
109
- positions: _this.positionsLineC3,
110
- width: _this.polylineOptions.width,
111
- vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT
112
- })
113
- });
114
- _this.polylineLayer = _this._map.scene.primitives.add(new Cesium.Primitive({
115
- geometryInstances: trailGeometry,
116
- appearance: _this.polylineLayer ? _this.polylineLayer.appearance : new Cesium.PolylineMaterialAppearance({
117
- material: new Cesium.Material({
118
- fabric: {
119
- type: 'Color',
120
- uniforms: {
121
- color: colorString(_this.polylineOptions.color)
122
- }
123
- }
124
- })
125
- }),
126
- asynchronous: false
127
- }));
128
- }
129
- }
130
- if (_this.modelLayer && _this.currentPathIndex < _this.positionInterC3.length) {
131
- // 更新模型位置
132
- var _position = _this.positionInterC3[_this.currentPathIndex];
133
- _this.modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(_position, _this.hpRoll, Cesium.Ellipsoid.WGS84, Cesium.Transforms.localFrameToFixedFrameGenerator('north', 'west'));
134
- _this.modelLayer.modelMatrix = _this.modelMatrix;
135
-
136
- // 调用位置更新回调
137
- if (_this._onPositionUpdate) {
138
- var cartographic = Cesium.Cartographic.fromCartesian(_position);
139
- _this._onPositionUpdate({
140
- lon: Cesium.Math.toDegrees(cartographic.longitude),
141
- lat: Cesium.Math.toDegrees(cartographic.latitude),
142
- height: cartographic.height
143
- });
144
- }
145
- if (_this.follow) {
146
- _this.updateCameraView();
147
- }
148
- }
149
- _this.currentPathIndex++;
150
- });
151
- var _ref = options || {},
152
- model = _ref.model,
153
- polyline = _ref.polyline,
154
- onPositionUpdate = _ref.onPositionUpdate,
155
- flyTo = _ref.flyTo;
156
- if (!map) {
157
- throw new Error('undefined map');
158
- }
159
- if (!options.model && !((_options$model = options.model) !== null && _options$model !== void 0 && _options$model.url)) {
160
- throw new Error('undefined model');
161
- }
162
- this._map = map;
163
- this.modelOptions = model;
164
- this.polylineOptions = merge(constant.DEFAULT_POLYLINE_PRIMITIVE_OPTIONS(), cloneDeep(polyline || {}));
165
- this.flyTo = flyTo || false;
166
- this._onPositionUpdate = onPositionUpdate;
167
- }
168
- /**
169
- * 实时添加点位
170
- * @param newPosition 新位置点
171
- * @throws 如果新位置与上一个位置完全相同
172
- */
173
- _createClass(ModelRoamUpDate, [{
174
- key: "updatePosition",
175
- value: function updatePosition(newPosition) {
176
- if (this.status === 'destroy') return;
177
- if (!this.positions.length) {
178
- this.initModel(newPosition);
179
- return;
180
- }
181
-
182
- // 获取最后一个目标点的位置
183
- var lastTargetPos = this.positions[this.positions.length - 1];
184
- if (lastTargetPos) {
185
- if (newPosition.lng === lastTargetPos.lng && newPosition.lat === lastTargetPos.lat && Math.abs(newPosition.alt - lastTargetPos.alt) < 0.1) {
186
- return;
187
- }
188
- }
189
-
190
- // 将新位置添加到目标点数组
191
- this.positions.push(newPosition);
192
- var newPos = Cesium.Cartesian3.fromDegrees(newPosition.lng, newPosition.lat, newPosition.alt);
193
- if (this.status === 'running') {
194
- this.positionInterC3.push(newPos);
195
- this.maxPathIndex = this.positionInterC3.length;
196
- } else {
197
- this.currentPathIndex = 0;
198
- }
199
- }
200
- /**
201
- * 初始化模型
202
- * 加载模型
203
- * @param initialPosition 初始位置
204
- */
205
- }, {
206
- key: "initModel",
207
- value: (function () {
208
- var _initModel = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(initialPosition) {
209
- var _this2 = this;
210
- var pos, model;
211
- return _regeneratorRuntime().wrap(function _callee$(_context) {
212
- while (1) switch (_context.prev = _context.next) {
213
- case 0:
214
- // 添加初始位置到目标点数组
215
- this.positions.push(initialPosition);
216
- pos = Cesium.Cartesian3.fromDegrees(initialPosition.lng, initialPosition.lat, initialPosition.alt);
217
- this.positionsLineC3 = [pos];
218
- // 创建模型矩阵
219
- this.modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(pos, this.hpRoll, Cesium.Ellipsoid.WGS84, Cesium.Transforms.localFrameToFixedFrameGenerator('north', 'west'));
220
- _context.next = 6;
221
- return Cesium.Model.fromGltfAsync(_objectSpread({
222
- id: uuidv4(),
223
- modelMatrix: this.modelMatrix
224
- }, this.modelOptions));
225
- case 6:
226
- model = _context.sent;
227
- this.modelLayer = this._map.scene.primitives.add(model);
228
- // 开启动画
229
- this.modelLayer.readyEvent.addEventListener(function () {
230
- if (_this2.flyTo) {
231
- _this2._flyToModel();
232
- }
233
- _this2.modelLayer.activeAnimations.addAll({
234
- loop: Cesium.ModelAnimationLoop.REPEAT,
235
- multiplier: 2
236
- });
237
- if (_this2.status === 'stop') {
238
- _this2.status = 'running';
239
- // 监听帧率
240
- _this2._eventPreUpdate = _this2._map.on(constant.SCENE_EVENT_TYPE.PRE_UPDATE, _this2.updateModel);
241
- }
242
- });
243
- case 9:
244
- case "end":
245
- return _context.stop();
246
- }
247
- }, _callee, this);
248
- }));
249
- function initModel(_x) {
250
- return _initModel.apply(this, arguments);
251
- }
252
- return initModel;
253
- }())
254
- }, {
255
- key: "getHeading",
256
- value:
257
- /**
258
- * 计算模型朝向
259
- * @param start 起始点
260
- * @param end 终点
261
- */
262
- function getHeading(start, end) {
263
- var startCartographic = Cesium.Cartographic.fromCartesian(start);
264
- var endCartographic = Cesium.Cartographic.fromCartesian(end);
265
- var startLonLat = [Cesium.Math.toDegrees(startCartographic.longitude), Cesium.Math.toDegrees(startCartographic.latitude)];
266
- var endLonLat = [Cesium.Math.toDegrees(endCartographic.longitude), Cesium.Math.toDegrees(endCartographic.latitude)];
267
- var horizontalDistance = Cesium.Cartesian3.distance(Cesium.Cartesian3.fromDegrees(startLonLat[0], startLonLat[1], startCartographic.height), Cesium.Cartesian3.fromDegrees(endLonLat[0], endLonLat[1], startCartographic.height));
268
- if (horizontalDistance < 0.1) {
269
- return;
270
- }
271
- var direction = Cesium.Cartesian3.subtract(end, start, new Cesium.Cartesian3());
272
- Cesium.Cartesian3.normalize(direction, direction);
273
- var transform = Cesium.Transforms.eastNorthUpToFixedFrame(start);
274
- var localDirection = Cesium.Matrix4.multiplyByPointAsVector(Cesium.Matrix4.inverse(transform, new Cesium.Matrix4()), direction, new Cesium.Cartesian3());
275
- this.targetHeading = Math.atan2(localDirection.x, localDirection.y);
276
- }
277
-
278
- /**
279
- * 更新模型朝向
280
- */
281
- }, {
282
- key: "updateHeading",
283
- value: function updateHeading() {
284
- // 计算需要转向的最短角度
285
- var deltaHeading = this.targetHeading - this.currentHeading;
286
-
287
- // 确保角度在 -π 到 π 之间
288
- while (deltaHeading > Math.PI) deltaHeading -= 2 * Math.PI;
289
- while (deltaHeading < -Math.PI) deltaHeading += 2 * Math.PI;
290
-
291
- // 平滑插值
292
- if (Math.abs(deltaHeading) > this.rotationSpeed) {
293
- this.currentHeading += Math.sign(deltaHeading) * this.rotationSpeed;
294
- } else {
295
- this.currentHeading = this.targetHeading;
296
- }
297
-
298
- // 确保角度在 0 到 2π 之间
299
- while (this.currentHeading < 0) this.currentHeading += 2 * Math.PI;
300
- while (this.currentHeading > 2 * Math.PI) this.currentHeading -= 2 * Math.PI;
301
-
302
- // 更新模型朝向
303
- this.hpRoll = new Cesium.HeadingPitchRoll(this.currentHeading, 0.0, 0.0);
304
- }
305
-
306
- /**
307
- * 更新相机视角
308
- * 根据当前模式更新相机位置和朝向
309
- */
310
- }, {
311
- key: "updateCameraView",
312
- value: function updateCameraView() {
313
- if (!this.modelLayer || !this.positionInterC3) {
314
- return;
315
- }
316
- var position = this.currentPathIndex >= this.positionInterC3.length ? this.positionInterC3[this.positionInterC3.length - 1] : this.positionInterC3[this.currentPathIndex];
317
- if (this.follow === 'first') {
318
- this.updateFirstPersonView(position);
319
- } else if (this.follow === 'third') {
320
- this.updateThirdPersonView(position);
321
- }
322
- }
323
-
324
- /**
325
- * 更新第一人称视角
326
- * @param position 模型当前位置
327
- */
328
- }, {
329
- key: "updateFirstPersonView",
330
- value: function updateFirstPersonView(position) {
331
- // 设置相机位置和朝向
332
- this._map.camera.setView({
333
- destination: position,
334
- orientation: {
335
- heading: this.hpRoll.heading,
336
- pitch: Cesium.Math.toRadians(this.firstCameraSettings.pitch),
337
- roll: 0.0
338
- }
339
- });
340
- }
341
-
342
- /**
343
- * 更新第三人称视角
344
- * @param position 模型当前位置
345
- */
346
- }, {
347
- key: "updateThirdPersonView",
348
- value: function updateThirdPersonView(position) {
349
- this._map.camera.lookAt(position, new Cesium.HeadingPitchRange(this.hpRoll.heading, Cesium.Math.toRadians(this.thirdPersonSettings.pitch), this.thirdPersonSettings.distance));
350
- }
351
-
352
- /**
353
- * 设置第一人称视角
354
- */
355
- }, {
356
- key: "setFollowFirst",
357
- value: function setFollowFirst(options) {
358
- if (this.status === 'destroy') return;
359
- this.firstCameraSettings = _objectSpread(_objectSpread({}, this.firstCameraSettings), options);
360
- this.follow = 'first';
361
- this.updateCameraView();
362
- }
363
-
364
- /**
365
- * 设置第三人称视角
366
- */
367
- }, {
368
- key: "setFollowThird",
369
- value: function setFollowThird(options) {
370
- if (this.status === 'destroy') return;
371
- this.thirdPersonSettings = _objectSpread(_objectSpread({}, this.thirdPersonSettings), options);
372
- this.follow = 'third';
373
- this.updateCameraView();
374
- }
375
-
376
- /**
377
- * 取消视角跟随
378
- */
379
- }, {
380
- key: "cancelFollow",
381
- value: function cancelFollow() {
382
- if (this.status === 'destroy') return;
383
- this.follow = null;
384
- }
385
-
386
- /**
387
- * 飞行定位到模型
388
- */
389
- }, {
390
- key: "_flyToModel",
391
- value: function _flyToModel() {
392
- var boundingSphere = this.modelLayer.boundingSphere;
393
- boundingSphere.radius = Math.max(boundingSphere.radius || 100);
394
- this._map.camera.flyToBoundingSphere(boundingSphere);
395
- }
396
-
397
- /**
398
- * 监听
399
- */
400
- }, {
401
- key: "on",
402
- value: function on(type, event) {
403
- return this._map._event.listenToModelRoam(type, event, this);
404
- }
405
-
406
- /**
407
- * 销毁实例
408
- * 清理所有资源
409
- */
410
- }, {
411
- key: "destroy",
412
- value: function destroy() {
413
- if (this.status === 'destroy') return;
414
- this.status = 'destroy';
415
- if (this.modelLayer) {
416
- this._eventPreUpdate.off();
417
- this._map.scene.primitives.remove(this.modelLayer);
418
- this.modelLayer = null;
419
- }
420
- if (this.polylineLayer) {
421
- this._map.scene.primitives.remove(this.polylineLayer);
422
- this.polylineLayer = null;
423
- }
424
- this.positions = [];
425
- this.positionsLineC3 = [];
426
- this.positionInterC3 = [];
427
- this.currentPathIndex = 0;
428
- this.maxPathIndex = 0;
429
- }
430
- }]);
431
- return ModelRoamUpDate;
432
- }();
433
- export { ModelRoamUpDate as default };