@galacean/effects-plugin-orientation-transformer 2.0.0-alpha.4 → 2.0.0-alpha.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/dist/index.mjs CHANGED
@@ -3,160 +3,202 @@
3
3
  * Description: Galacean Effects player orientation transformer plugin
4
4
  * Author: Ant Group CO., Ltd.
5
5
  * Contributors: 燃然,意绮
6
- * Version: v2.0.0-alpha.4
6
+ * Version: v2.0.0-alpha.6
7
7
  */
8
8
 
9
- import { isObject, isArray, assertExist, AbstractPlugin, ItemBehaviour, VFXItem, registerPlugin, logger } from '@galacean/effects';
9
+ import { ItemBehaviour, VFXItem, isObject, isArray, assertExist, AbstractPlugin, registerPlugin, logger } from '@galacean/effects';
10
10
 
11
- /******************************************************************************
12
- Copyright (c) Microsoft Corporation.
13
-
14
- Permission to use, copy, modify, and/or distribute this software for any
15
- purpose with or without fee is hereby granted.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
18
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
19
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
20
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
22
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23
- PERFORMANCE OF THIS SOFTWARE.
24
- ***************************************************************************** */
25
- /* global Reflect, Promise, SuppressedError, Symbol */
26
-
27
- var extendStatics = function(d, b) {
28
- extendStatics = Object.setPrototypeOf ||
29
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
30
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
31
- return extendStatics(d, b);
32
- };
33
-
34
- function __extends(d, b) {
35
- if (typeof b !== "function" && b !== null)
36
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
37
- extendStatics(d, b);
38
- function __() { this.constructor = d; }
39
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
40
- }
41
-
42
- var __assign = function() {
43
- __assign = Object.assign || function __assign(t) {
44
- for (var s, i = 1, n = arguments.length; i < n; i++) {
45
- s = arguments[i];
46
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
47
- }
48
- return t;
49
- };
50
- return __assign.apply(this, arguments);
51
- };
52
-
53
- function __read(o, n) {
54
- var m = typeof Symbol === "function" && o[Symbol.iterator];
55
- if (!m) return o;
56
- var i = m.call(o), r, ar = [], e;
57
- try {
58
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
59
- }
60
- catch (error) { e = { error: error }; }
61
- finally {
62
- try {
63
- if (r && !r.done && (m = i["return"])) m.call(i);
64
- }
65
- finally { if (e) throw e.error; }
66
- }
67
- return ar;
68
- }
69
-
70
- function __spreadArray(to, from, pack) {
71
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
72
- if (ar || !(i in from)) {
73
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
74
- ar[i] = from[i];
75
- }
76
- }
77
- return to.concat(ar || Array.prototype.slice.call(from));
78
- }
79
-
80
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
81
- var e = new Error(message);
82
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
83
- };
11
+ function _set_prototype_of(o, p) {
12
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
13
+ o.__proto__ = p;
14
+ return o;
15
+ };
16
+ return _set_prototype_of(o, p);
17
+ }
18
+
19
+ function _inherits(subClass, superClass) {
20
+ if (typeof superClass !== "function" && superClass !== null) {
21
+ throw new TypeError("Super expression must either be null or a function");
22
+ }
23
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
24
+ constructor: {
25
+ value: subClass,
26
+ writable: true,
27
+ configurable: true
28
+ }
29
+ });
30
+ if (superClass) _set_prototype_of(subClass, superClass);
31
+ }
32
+
33
+ var OrientationComponent = /*#__PURE__*/ function(ItemBehaviour) {
34
+ _inherits(OrientationComponent, ItemBehaviour);
35
+ function OrientationComponent() {
36
+ return ItemBehaviour.apply(this, arguments);
37
+ }
38
+ var _proto = OrientationComponent.prototype;
39
+ _proto.fromData = function fromData(data) {
40
+ ItemBehaviour.prototype.fromData.call(this, data);
41
+ var targets = data.content.options.targets;
42
+ if (targets) {
43
+ this.targets = targets.map(function(t) {
44
+ return {
45
+ name: t.name,
46
+ xMin: +t.xMin || 0,
47
+ yMin: +t.yMin || 0,
48
+ xMax: +t.xMax || 0,
49
+ yMax: +t.yMax || 0,
50
+ vMin: +t.vMin || 0,
51
+ hMin: +t.hMin || 0,
52
+ vMax: +t.vMax || 0,
53
+ hMax: +t.hMax || 0
54
+ };
55
+ });
56
+ }
57
+ };
58
+ _proto.start = function start() {
59
+ var _this_item_composition;
60
+ var transformer = (_this_item_composition = this.item.composition) == null ? void 0 : _this_item_composition.loaderData.deviceTransformer;
61
+ if (transformer) {
62
+ var _this_targets;
63
+ (_this_targets = this.targets) == null ? void 0 : _this_targets.forEach(function(target) {
64
+ return transformer.addTarget(target);
65
+ });
66
+ transformer.initComposition();
67
+ }
68
+ };
69
+ _proto.update = function update(dt) {
70
+ var _this_item_composition;
71
+ var transformer = (_this_item_composition = this.item.composition) == null ? void 0 : _this_item_composition.loaderData.deviceTransformer;
72
+ if (transformer) {
73
+ transformer.updateOrientation();
74
+ }
75
+ };
76
+ _proto.onDestroy = function onDestroy() {
77
+ var _this = this;
78
+ var _this_targets;
79
+ (_this_targets = this.targets) == null ? void 0 : _this_targets.forEach(function(target) {
80
+ var _this_item_composition_loaderData_deviceTransformer, _this_item_composition;
81
+ return (_this_item_composition = _this.item.composition) == null ? void 0 : (_this_item_composition_loaderData_deviceTransformer = _this_item_composition.loaderData.deviceTransformer) == null ? void 0 : _this_item_composition_loaderData_deviceTransformer.removeTarget(target.name);
82
+ });
83
+ };
84
+ return OrientationComponent;
85
+ }(ItemBehaviour);
86
+
87
+ /**
88
+ * 水平旋转变化起始角(手机处于改角度时无变化)
89
+ */ var VERTICAL_INIT_DEGREE = 45;
90
+ var TransformVFXItem = /*#__PURE__*/ function(VFXItem) {
91
+ _inherits(TransformVFXItem, VFXItem);
92
+ function TransformVFXItem(engine, props) {
93
+ var _this;
94
+ _this = VFXItem.call(this, engine, props) || this;
95
+ var component = _this.addComponent(OrientationComponent);
96
+ component.fromData(props);
97
+ return _this;
98
+ }
99
+ return TransformVFXItem;
100
+ }(VFXItem);
84
101
 
85
- var CompositionTransformerAcceler = /** @class */ (function () {
102
+ var CompositionTransformerAcceler = /*#__PURE__*/ function() {
86
103
  function CompositionTransformerAcceler(composition) {
87
104
  this.composition = composition;
88
105
  this.targets = {};
89
106
  this.records = {};
90
- this.current = {};
91
- this.gammaRange = [-89, 89];
92
- this.betaRange = [-80, 80];
107
+ this.currentPos = {};
108
+ this.currentRot = {};
109
+ this.gammaRange = [
110
+ -89,
111
+ 89
112
+ ];
113
+ this.betaRange = [
114
+ -89,
115
+ 89
116
+ ];
93
117
  }
94
- CompositionTransformerAcceler.prototype.update = function (_a) {
95
- var x = _a.x, y = _a.y;
96
- this.currentEvent = { x: x, y: y };
118
+ var _proto = CompositionTransformerAcceler.prototype;
119
+ _proto.update = function update(param) {
120
+ var x = param.x, y = param.y;
121
+ this.currentEvent = {
122
+ x: x,
123
+ y: y
124
+ };
97
125
  };
98
- CompositionTransformerAcceler.prototype.updateOrientation = function () {
126
+ _proto.updateOrientation = function updateOrientation() {
99
127
  var _this = this;
100
128
  var event = this.currentEvent;
101
129
  if (event) {
102
- Object.getOwnPropertyNames(this.targets).forEach(function (name) {
130
+ Object.getOwnPropertyNames(this.targets).forEach(function(name) {
103
131
  _this.moveLayer(name, event);
104
132
  });
105
133
  }
106
134
  };
107
- CompositionTransformerAcceler.prototype.initComposition = function () {
135
+ _proto.initComposition = function initComposition() {
108
136
  var items = this.composition.items;
109
- for (var i = 0; i < items.length; i++) {
137
+ for(var i = 0; i < items.length; i++){
110
138
  var item = items[i];
111
139
  var target = this.targets[item.name];
112
140
  if (target) {
113
141
  var position = item.transform.position.toArray();
114
- var currentPosition = this.current[item.name];
142
+ var rotation = item.transform.rotation.toArray();
143
+ var currentPosition = this.currentPos[item.name];
144
+ var currentRot = this.currentRot[item.name];
115
145
  this.records[item.name] = {
116
146
  item: item,
117
147
  position: position,
148
+ rotation: rotation
118
149
  };
119
150
  if (currentPosition) {
120
- var _a = __read(currentPosition, 3), x = _a[0], y = _a[1], z = _a[2];
121
- item.transform.setPosition(x, y, z);
151
+ item.transform.setPosition(currentPosition[0], currentPosition[1], currentPosition[2]);
152
+ }
153
+ if (currentRot) {
154
+ item.transform.setRotation(currentRot[0], currentRot[1], currentRot[2]);
122
155
  }
123
156
  }
124
157
  }
125
158
  };
126
- CompositionTransformerAcceler.prototype.addTarget = function (target) {
159
+ _proto.addTarget = function addTarget(target) {
127
160
  this.targets[target.name] = target;
128
161
  };
129
- CompositionTransformerAcceler.prototype.removeTarget = function (name) {
162
+ _proto.removeTarget = function removeTarget(name) {
130
163
  delete this.targets[name];
131
164
  delete this.records[name];
132
165
  };
133
- CompositionTransformerAcceler.prototype.moveLayer = function (name, _a) {
134
- var _b;
135
- var _c;
136
- var x = _a.x, y = _a.y;
137
- var layer = (_c = this.records[name]) === null || _c === void 0 ? void 0 : _c.item;
166
+ _proto.moveLayer = function moveLayer(name, param) {
167
+ var x = param.x, y = param.y;
168
+ var _this_records_name;
169
+ var layer = (_this_records_name = this.records[name]) == null ? void 0 : _this_records_name.item;
138
170
  if (layer) {
139
171
  var initPosition = this.records[name].position;
172
+ var initRotation = this.records[name].rotation;
140
173
  var target = this.targets[name];
141
174
  var beta = clamp(x, this.betaRange), gamma = clamp(y, this.gammaRange);
142
175
  if (target) {
176
+ var _layer_transform, _layer_transform1;
143
177
  var position = [
144
178
  initPosition[0] + 1.2 * mapRange((beta - this.gammaRange[0]) / (this.gammaRange[1] - this.gammaRange[0]), target.xMin, target.xMax),
145
179
  initPosition[1] + 1.2 * mapRange((gamma - this.betaRange[0]) / (this.betaRange[1] - this.betaRange[0]), target.yMin, target.yMax),
146
- initPosition[2],
180
+ initPosition[2]
147
181
  ];
148
- (_b = layer.transform).setPosition.apply(_b, __spreadArray([], __read(position), false));
149
- this.current[name] = position.slice();
182
+ var br = (clamp(x * 2, this.betaRange) - this.betaRange[0]) / (this.betaRange[1] - this.betaRange[0]);
183
+ var gr = (clamp(y * 2 - VERTICAL_INIT_DEGREE, this.gammaRange) - this.gammaRange[0]) / (this.gammaRange[1] - this.gammaRange[0]);
184
+ var rotation = [
185
+ initRotation[0] + mapRange(gr, target.hMin, target.hMax),
186
+ initRotation[1] + mapRange(br, target.vMin, target.vMax),
187
+ initRotation[2]
188
+ ];
189
+ (_layer_transform = layer.transform).setPosition.apply(_layer_transform, [].concat(position));
190
+ (_layer_transform1 = layer.transform).setRotation.apply(_layer_transform1, [].concat(rotation));
191
+ this.currentPos[name] = position.slice();
192
+ this.currentRot[name] = rotation.slice();
150
193
  }
151
194
  }
152
195
  };
153
196
  return CompositionTransformerAcceler;
154
- }());
197
+ }();
155
198
  function clamp(x, range) {
156
199
  if (x < range[0]) {
157
200
  return range[0];
158
- }
159
- else if (x > range[1]) {
201
+ } else if (x > range[1]) {
160
202
  return range[1];
161
203
  }
162
204
  return x;
@@ -174,71 +216,75 @@ function getObjectType(obj) {
174
216
  * @internal
175
217
  * @param angleObj 角度数值对象
176
218
  * @param validRange 角度限制值
177
- */
178
- function angleLimit(angleObj, validRange) {
219
+ */ function angleLimit(angleObj, validRange) {
179
220
  var flagAlpha = true;
180
221
  var flagBeta = true;
181
222
  var flagGamma = true;
182
- if (getObjectType(validRange.alpha) === '[object Array]' && validRange.alpha.length >= 2) {
223
+ if (getObjectType(validRange.alpha) === "[object Array]" && validRange.alpha.length >= 2) {
183
224
  var referAlphaA = angleObj.alpha + validRange.alpha[0];
184
225
  var referAlphaB = angleObj.alpha + validRange.alpha[1];
185
226
  referAlphaA = referAlphaA < 0 ? referAlphaA + 360 : referAlphaA;
186
227
  referAlphaB = referAlphaB > 360 ? referAlphaB - 360 : referAlphaB;
187
228
  if (referAlphaA < referAlphaB) {
188
229
  flagAlpha = angleObj.alpha > referAlphaA && angleObj.alpha < referAlphaB;
189
- }
190
- else {
230
+ } else {
191
231
  flagAlpha = angleObj.alpha > referAlphaA || angleObj.alpha < referAlphaB;
192
232
  }
193
233
  }
194
- if (getObjectType(validRange.beta) === '[object Array]' && validRange.beta.length >= 2) {
234
+ if (getObjectType(validRange.beta) === "[object Array]" && validRange.beta.length >= 2) {
195
235
  flagBeta = angleObj.beta >= validRange.beta[0] && angleObj.beta <= validRange.beta[1];
196
236
  }
197
- if (getObjectType(validRange.gamma) === '[object Array]' && validRange.gamma.length >= 2) {
237
+ if (getObjectType(validRange.gamma) === "[object Array]" && validRange.gamma.length >= 2) {
198
238
  flagGamma = angleObj.gamma >= validRange.gamma[0] && angleObj.gamma <= validRange.gamma[1];
199
239
  }
200
240
  return flagAlpha && flagBeta && flagGamma;
201
241
  }
202
242
 
243
+ function _extends() {
244
+ _extends = Object.assign || function assign(target) {
245
+ for(var i = 1; i < arguments.length; i++){
246
+ var source = arguments[i];
247
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
248
+ }
249
+ return target;
250
+ };
251
+ return _extends.apply(this, arguments);
252
+ }
253
+
203
254
  function deepMerge(target, args) {
204
255
  if (!isObject(target) && !isArray(target)) {
205
256
  return target;
206
257
  }
207
- var result = isArray(target) ? __spreadArray([], __read(target), false) : __assign({}, target);
258
+ var result = isArray(target) ? [].concat(target) : _extends({}, target);
208
259
  var length = arguments.length;
209
- for (var index = 1; index < length; index++) {
260
+ for(var index = 1; index < length; index++){
210
261
  var source = arguments[index] || {};
211
262
  if (isObject(source)) {
212
263
  var prop = void 0;
213
- for (prop in source) {
264
+ for(prop in source){
214
265
  if (source.hasOwnProperty(prop)) {
215
266
  if (isObject(result[prop]) && isObject(source[prop])) {
216
267
  result[prop] = deepMerge(result[prop], source[prop]);
217
- }
218
- else if (isObject(source[prop])) {
268
+ } else if (isObject(source[prop])) {
219
269
  result[prop] = deepMerge(source[prop]);
220
- }
221
- else if (isArray(source[prop])) {
270
+ } else if (isArray(source[prop])) {
222
271
  result[prop] = deepMerge(source[prop]);
223
- }
224
- else {
272
+ } else {
225
273
  result[prop] = source[prop];
226
274
  }
227
275
  }
228
276
  }
229
- for (prop in result) {
230
- if (isObject(result[prop]) &&
231
- result.hasOwnProperty(prop) &&
232
- !source.hasOwnProperty(prop)) {
277
+ for(prop in result){
278
+ if (isObject(result[prop]) && result.hasOwnProperty(prop) && !source.hasOwnProperty(prop)) {
233
279
  result[prop] = deepMerge(result[prop]);
234
280
  }
235
281
  }
236
282
  }
237
283
  }
238
284
  if (length === 1) {
239
- for (var prop in result) {
240
- if (isObject(result[prop]) && result.hasOwnProperty(prop)) {
241
- result[prop] = deepMerge(result[prop]);
285
+ for(var prop1 in result){
286
+ if (isObject(result[prop1]) && result.hasOwnProperty(prop1)) {
287
+ result[prop1] = deepMerge(result[prop1]);
242
288
  }
243
289
  }
244
290
  }
@@ -253,38 +299,33 @@ function isIOS() {
253
299
 
254
300
  /**
255
301
  * 稳定区间
256
- */
257
- var STABLE_RANGE = 3;
302
+ */ var STABLE_RANGE = 3;
258
303
  /**
259
304
  * 每次增幅的最大值
260
- */
261
- var MAX_CHANGE = 5;
305
+ */ var MAX_CHANGE = 5;
262
306
  /**
263
307
  * 每次增幅的最小值
264
- */
265
- var MIN_CHANGE = 1;
308
+ */ var MIN_CHANGE = 1;
266
309
  /**
267
310
  * 数据缓存队列的长度
268
- */
269
- var VALUE_POOL_LENGTH = 5;
311
+ */ var VALUE_POOL_LENGTH = 5;
270
312
  /**
271
313
  * 滤波方法
272
- */
273
- var Filtering = /** @class */ (function () {
314
+ */ var Filtering = /*#__PURE__*/ function() {
274
315
  function Filtering(options) {
275
- if (options === void 0) { options = {}; }
316
+ if (options === void 0) options = {};
276
317
  this.options = options;
277
318
  this.lastValue = 0;
278
319
  this.valuePool = [];
279
320
  }
321
+ var _proto = Filtering.prototype;
280
322
  /**
281
- * 0deg 左右保持一定的稳定区间 STABLE_RANGE
282
- * - 小于 STABLE_RANGE 的算 0
283
- * - 大于 STABLE_RANGE 的减去 STABLE_RANGE
284
- * @param value - 输入值
285
- * @returns
286
- */
287
- Filtering.prototype.stableFix = function (value) {
323
+ * 0deg 左右保持一定的稳定区间 STABLE_RANGE
324
+ * - 小于 STABLE_RANGE 的算 0
325
+ * - 大于 STABLE_RANGE 的减去 STABLE_RANGE
326
+ * @param value - 输入值
327
+ * @returns
328
+ */ _proto.stableFix = function stableFix(value) {
288
329
  var stableRange = this.options.stableRange || STABLE_RANGE;
289
330
  if (Math.abs(value) < stableRange) {
290
331
  return 0;
@@ -292,13 +333,12 @@ var Filtering = /** @class */ (function () {
292
333
  return value > stableRange ? value - stableRange : value + stableRange;
293
334
  };
294
335
  /**
295
- * 变幅限制
296
- * - 最大变化 MAX_CHANGE
297
- * - 最小变化 MIN_CHANGE
298
- * @param value
299
- * @returns
300
- */
301
- Filtering.prototype.changeLimit = function (value) {
336
+ * 变幅限制
337
+ * - 最大变化 MAX_CHANGE
338
+ * - 最小变化 MIN_CHANGE
339
+ * @param value
340
+ * @returns
341
+ */ _proto.changeLimit = function changeLimit(value) {
302
342
  var lastValue = this.lastValue;
303
343
  var result = value;
304
344
  if (Math.abs(value - lastValue) > MAX_CHANGE) {
@@ -310,21 +350,20 @@ var Filtering = /** @class */ (function () {
310
350
  this.lastValue = result;
311
351
  return result;
312
352
  };
313
- Filtering.prototype.linearRegressionMedian = function (value) {
353
+ _proto.linearRegressionMedian = function linearRegressionMedian(value) {
314
354
  // 在队列中装载数据,达到队列阀值长度时开始计算
315
355
  this.valuePool.push(value);
316
356
  var result;
317
357
  if (this.valuePool.length >= VALUE_POOL_LENGTH) {
318
358
  result = getLinearRegressionFixedVal(this.valuePool);
319
359
  this.valuePool.shift();
320
- }
321
- else {
360
+ } else {
322
361
  result = this.lastValue;
323
362
  }
324
363
  return result;
325
364
  };
326
365
  return Filtering;
327
- }());
366
+ }();
328
367
  // 计算出线性回归参数
329
368
  function linearRegression(x, y) {
330
369
  var result = {};
@@ -334,30 +373,30 @@ function linearRegression(x, y) {
334
373
  var sumXY = 0;
335
374
  var sumXX = 0;
336
375
  var sumYY = 0;
337
- for (var i = 0; i < y.length; i++) {
376
+ for(var i = 0; i < y.length; i++){
338
377
  sumX += x[i];
339
378
  sumY += y[i];
340
- sumXY += (x[i] * y[i]);
341
- sumXX += (x[i] * x[i]);
342
- sumYY += (y[i] * y[i]);
379
+ sumXY += x[i] * y[i];
380
+ sumXX += x[i] * x[i];
381
+ sumYY += y[i] * y[i];
343
382
  }
344
383
  var avgX = sumX / n;
345
384
  var avgY = sumY / n;
346
- result['a'] = (sumXY - n * avgX * avgY) / (sumXX - n * avgX * avgX);
347
- result['b'] = avgY - result.a * avgX;
348
- result['miss'] = Math.pow((n * sumXY - sumX * sumY) / Math.sqrt((n * sumXX - sumX * sumX) * (n * sumYY - sumY * sumY)), 2);
385
+ result["a"] = (sumXY - n * avgX * avgY) / (sumXX - n * avgX * avgX);
386
+ result["b"] = avgY - result.a * avgX;
387
+ result["miss"] = Math.pow((n * sumXY - sumX * sumY) / Math.sqrt((n * sumXX - sumX * sumX) * (n * sumYY - sumY * sumY)), 2);
349
388
  return result;
350
389
  }
351
390
  // 生成线性回归函数 F(x) = ax + b
352
391
  function createLinearRegressionFx(values) {
353
392
  var x = [];
354
393
  var y = [];
355
- values.forEach(function (item, index) {
394
+ values.forEach(function(item, index) {
356
395
  x.push(index); // 数据是连续的,所以直接取数组下标为 x
357
396
  y.push(item);
358
397
  });
359
398
  var key = linearRegression(x, y);
360
- return function (x) {
399
+ return function(x) {
361
400
  return key.a * x + key.b;
362
401
  };
363
402
  }
@@ -365,14 +404,14 @@ function createLinearRegressionFx(values) {
365
404
  function getLinearRegressionFixedVal(valuePool) {
366
405
  var fx = createLinearRegressionFx(valuePool);
367
406
  // 这个迭代修正很重要
368
- valuePool = valuePool.map(function (item, index) {
407
+ valuePool = valuePool.map(function(item, index) {
369
408
  return fx(index);
370
409
  });
371
410
  return fx(valuePool.length / 2);
372
411
  }
373
412
 
374
- var useJSBridge = isIOS() && typeof window.AlipayJSBridge !== 'undefined';
375
- var useWindVane = typeof window.WindVane !== 'undefined';
413
+ var useJSBridge = isIOS() && typeof window.AlipayJSBridge !== "undefined";
414
+ var useWindVane = typeof window.WindVane !== "undefined";
376
415
  // 默认参数
377
416
  var defaultOptions = {
378
417
  X: true,
@@ -382,31 +421,45 @@ var defaultOptions = {
382
421
  relativeBeta: false,
383
422
  relativeGamma: false,
384
423
  validRange: {
385
- alpha: [-60, 60],
386
- beta: [-60, 180],
387
- gamma: [-89, 89],
424
+ alpha: [
425
+ -60,
426
+ 60
427
+ ],
428
+ beta: [
429
+ -60,
430
+ 180
431
+ ],
432
+ gamma: [
433
+ -89,
434
+ 89
435
+ ]
388
436
  },
389
- stableRange: 3,
437
+ stableRange: 3
390
438
  };
391
- var DeviceOrientation = /** @class */ (function () {
439
+ var DeviceOrientation = /*#__PURE__*/ function() {
392
440
  function DeviceOrientation(options) {
393
- if (options === void 0) { options = {}; }
441
+ if (options === void 0) options = {};
394
442
  this.lastX = 0;
395
443
  this.lastY = 0;
396
444
  this.prevAcceler = {
397
445
  x: 0,
398
446
  y: 0,
399
- z: 0,
447
+ z: 0
400
448
  };
401
449
  this.stoped = false;
402
450
  this.options = deepMerge(defaultOptions, options);
403
451
  var stableRange = this.options.stableRange;
404
- this.filterX = new Filtering({ stableRange: stableRange });
405
- this.filterY = new Filtering({ stableRange: stableRange });
452
+ this.filterX = new Filtering({
453
+ stableRange: stableRange
454
+ });
455
+ this.filterY = new Filtering({
456
+ stableRange: stableRange
457
+ });
406
458
  }
407
- DeviceOrientation.prototype.watch = function (callback) {
459
+ var _proto = DeviceOrientation.prototype;
460
+ _proto.watch = function watch(callback) {
408
461
  var _this = this;
409
- if (typeof callback !== 'function') {
462
+ if (typeof callback !== "function") {
410
463
  return;
411
464
  }
412
465
  // 目前只能绑定一个方法,此处可以扩展成一个方法队列
@@ -425,7 +478,7 @@ var DeviceOrientation = /** @class */ (function () {
425
478
  var isInValidDegRange;
426
479
  var x; // 最终输出的 x
427
480
  var y; // 最终输出的 y
428
- var handleWatch = function (e) {
481
+ var handleWatch = function(e) {
429
482
  if (_this.stoped) {
430
483
  return;
431
484
  }
@@ -442,7 +495,7 @@ var DeviceOrientation = /** @class */ (function () {
442
495
  isInValidDegRange = angleLimit.call(_this, {
443
496
  alpha: alpha,
444
497
  beta: beta,
445
- gamma: gamma,
498
+ gamma: gamma
446
499
  }, _this.options.validRange);
447
500
  if (isInValidDegRange) {
448
501
  // 最终结果值
@@ -452,8 +505,7 @@ var DeviceOrientation = /** @class */ (function () {
452
505
  x = _this.filterX.stableFix(x);
453
506
  x = _this.filterX.changeLimit(x);
454
507
  x = _this.filterX.linearRegressionMedian(+x);
455
- }
456
- else {
508
+ } else {
457
509
  x = 0;
458
510
  }
459
511
  if (_this.options.Y) {
@@ -463,18 +515,24 @@ var DeviceOrientation = /** @class */ (function () {
463
515
  y = _this.filterY.stableFix(y);
464
516
  y = _this.filterY.changeLimit(y);
465
517
  y = _this.filterY.linearRegressionMedian(+y);
466
- }
467
- else {
518
+ } else {
468
519
  y = 0;
469
520
  }
470
521
  if (_this.isValid(x, y)) {
471
522
  if (_this.options.useRequestAnimationFrame) {
472
- window.requestAnimationFrame(function () {
473
- callback(x, y, { alpha: alpha, beta: beta, gamma: gamma });
523
+ window.requestAnimationFrame(function() {
524
+ callback(x, y, {
525
+ alpha: alpha,
526
+ beta: beta,
527
+ gamma: gamma
528
+ });
529
+ });
530
+ } else {
531
+ callback(x, y, {
532
+ alpha: alpha,
533
+ beta: beta,
534
+ gamma: gamma
474
535
  });
475
- }
476
- else {
477
- callback(x, y, { alpha: alpha, beta: beta, gamma: gamma });
478
536
  }
479
537
  _this.lastX = x;
480
538
  _this.lastY = y;
@@ -485,51 +543,52 @@ var DeviceOrientation = /** @class */ (function () {
485
543
  this.stoped = false;
486
544
  if (useWindVane) {
487
545
  switchWindVane(true, this.options.interval);
488
- this.watchListener = function (e) {
489
- if ('param' in e) {
490
- var _a = e.param, x_1 = _a.x, y_1 = _a.y, z = _a.z;
546
+ this.watchListener = function(e) {
547
+ if ("param" in e) {
548
+ var _e_param = e.param, x = _e_param.x, y = _e_param.y, z = _e_param.z;
491
549
  // 即使手机没动,也会不停触发,所以这里加个判断,值相等则不调用回调
492
- if (x_1 === _this.prevAcceler.x && y_1 === _this.prevAcceler.y && z === _this.prevAcceler.z) {
550
+ if (x === _this.prevAcceler.x && y === _this.prevAcceler.y && z === _this.prevAcceler.z) {
493
551
  return;
494
552
  }
495
- _this.prevAcceler = { x: x_1, y: y_1, z: z };
553
+ _this.prevAcceler = {
554
+ x: x,
555
+ y: y,
556
+ z: z
557
+ };
496
558
  var evt = {
497
559
  alpha: +z * 180,
498
- beta: +y_1 * -90,
499
- gamma: +x_1 * 90,
560
+ beta: +y * -90,
561
+ gamma: +x * 90
500
562
  };
501
563
  handleWatch(evt);
502
564
  }
503
565
  };
504
- document.addEventListener('motion.gyro', this.watchListener, false);
505
- }
506
- else if (useJSBridge) {
566
+ document.addEventListener("motion.gyro", this.watchListener, false);
567
+ } else if (useJSBridge) {
507
568
  switchIOSEvent(true, this.options.interval / 1000);
508
- this.watchListener = function (e) {
509
- if (e && 'data' in e) {
569
+ this.watchListener = function(e) {
570
+ if (e && "data" in e) {
510
571
  /*
511
- data:
512
- {
513
- data: {
514
- x: 0.0, // gamma -1 ~ 1
515
- y: 0.0, // beta -1 ~ 1
516
- z: 0.0, // alpha
517
- }
518
- }
519
- */
520
- var _a = e.data, x_2 = _a.x, y_2 = _a.y, z = _a.z;
572
+ data:
573
+ {
574
+ data: {
575
+ x: 0.0, // gamma -1 ~ 1
576
+ y: 0.0, // beta -1 ~ 1
577
+ z: 0.0, // alpha
578
+ }
579
+ }
580
+ */ var _e_data = e.data, x = _e_data.x, y = _e_data.y, z = _e_data.z;
521
581
  var evt = {
522
582
  alpha: z * 180,
523
- beta: y_2 * -90,
524
- gamma: x_2 * 90,
583
+ beta: y * -90,
584
+ gamma: x * 90
525
585
  };
526
586
  handleWatch(evt);
527
587
  }
528
588
  };
529
- document.addEventListener('accelerometerChange', this.watchListener, false);
530
- }
531
- else {
532
- this.watchListener = function (e) {
589
+ document.addEventListener("accelerometerChange", this.watchListener, false);
590
+ } else {
591
+ this.watchListener = function(e) {
533
592
  nowts = new Date().getTime();
534
593
  // 控制时间片
535
594
  if (nowts - timefragment >= _this.options.interval) {
@@ -537,80 +596,87 @@ var DeviceOrientation = /** @class */ (function () {
537
596
  handleWatch(e);
538
597
  }
539
598
  };
540
- window.addEventListener('deviceorientation', this.watchListener, false);
599
+ window.addEventListener("deviceorientation", this.watchListener, false);
541
600
  }
542
601
  };
543
- DeviceOrientation.prototype.unWatch = function () {
602
+ _proto.unWatch = function unWatch() {
544
603
  assertExist(this.watchListener);
545
604
  if (useWindVane) {
546
605
  switchWindVane(false);
547
- document.removeEventListener('motion.gyro', this.watchListener);
548
- }
549
- else if (useJSBridge) {
606
+ document.removeEventListener("motion.gyro", this.watchListener);
607
+ } else if (useJSBridge) {
550
608
  switchIOSEvent(false);
551
- document.removeEventListener('accelerometerChange', this.watchListener);
552
- }
553
- else {
554
- window.removeEventListener('deviceorientation', this.watchListener);
609
+ document.removeEventListener("accelerometerChange", this.watchListener);
610
+ } else {
611
+ window.removeEventListener("deviceorientation", this.watchListener);
555
612
  }
556
613
  this.stoped = true;
557
614
  this.watchListener = undefined;
558
615
  };
559
- DeviceOrientation.prototype.pause = function () {
616
+ _proto.pause = function pause() {
560
617
  this.stoped = true;
561
618
  if (useJSBridge) {
562
619
  switchIOSEvent(false);
563
620
  }
564
621
  };
565
- DeviceOrientation.prototype.continue = function () {
622
+ _proto.continue = function _continue() {
566
623
  this.stoped = false;
567
624
  if (useJSBridge) {
568
625
  switchIOSEvent(true, this.options.interval / 1000);
569
626
  }
570
627
  };
571
- DeviceOrientation.prototype.isValid = function (x, y) {
628
+ _proto.isValid = function isValid(x, y) {
572
629
  // 判断是否和上次的值一样,检测有没有变
573
630
  return this.lastX !== x || this.lastY !== y;
574
631
  };
575
632
  return DeviceOrientation;
576
- }());
633
+ }();
577
634
  // 开启/关闭 iOS 的陀螺仪监听
578
635
  function switchIOSEvent(open, interval) {
579
- window.AlipayJSBridge.call('watchShake', {
636
+ window.AlipayJSBridge.call("watchShake", {
580
637
  monitorGyroscope: false,
581
638
  monitorAccelerometer: !!open,
582
- interval: !open ? 10 : parseFloat(interval.toFixed(3)),
583
- }, function () { });
639
+ interval: !open ? 10 : parseFloat(interval.toFixed(3))
640
+ }, function() {});
584
641
  }
585
642
  function switchWindVane(open, interval) {
586
643
  var params = {
587
644
  // 是开启还是关闭陀螺仪的监听
588
645
  on: open,
589
646
  // 陀螺仪事件的时间间隔
590
- frequency: interval,
647
+ frequency: interval
591
648
  };
592
- window.WindVane.call('WVMotion', 'listenGyro', params, function (e) {
593
- console.info('call WVMotion success');
594
- }, function (e) {
595
- console.error('call WVMotion failed:' + JSON.stringify(e));
649
+ window.WindVane.call("WVMotion", "listenGyro", params, function(e) {
650
+ console.info("call WVMotion success");
651
+ }, function(e) {
652
+ console.error("call WVMotion failed:" + JSON.stringify(e));
596
653
  });
597
654
  }
598
655
 
599
656
  /**
600
657
  *
601
- */
602
- var OrientationAdapterAcceler = /** @class */ (function () {
658
+ */ var OrientationAdapterAcceler = /*#__PURE__*/ function() {
603
659
  function OrientationAdapterAcceler() {
604
660
  this.transformers = [];
605
661
  this.connected = false;
606
662
  this.accelerMotion = null;
607
663
  this.validRange = {
608
- alpha: [-60, 60],
609
- beta: [-80, 80],
610
- gamma: [-89, 89], // 左右翻转
664
+ alpha: [
665
+ -60,
666
+ 60
667
+ ],
668
+ beta: [
669
+ -80,
670
+ 80
671
+ ],
672
+ gamma: [
673
+ -89,
674
+ 89
675
+ ]
611
676
  };
612
677
  }
613
- OrientationAdapterAcceler.prototype.connect = function () {
678
+ var _proto = OrientationAdapterAcceler.prototype;
679
+ _proto.connect = function connect() {
614
680
  var _this = this;
615
681
  if (!this.connected) {
616
682
  this.accelerMotion = new DeviceOrientation({
@@ -621,37 +687,38 @@ var OrientationAdapterAcceler = /** @class */ (function () {
621
687
  relativeBeta: false,
622
688
  relativeGamma: false,
623
689
  validRange: this.validRange,
624
- stableRange: 3, // 稳定区间
690
+ stableRange: 3
625
691
  });
626
- this.accelerMotion.watch(function (x, y) {
692
+ this.accelerMotion.watch(function(x, y) {
627
693
  var motion = {
628
694
  x: x,
629
- y: y,
695
+ y: y
630
696
  };
631
697
  _this.dispatchMotion(motion);
632
698
  });
633
699
  }
634
700
  this.connected = true;
635
701
  };
636
- OrientationAdapterAcceler.prototype.disconnect = function () {
702
+ _proto.disconnect = function disconnect() {
637
703
  if (this.accelerMotion) {
638
704
  this.accelerMotion.unWatch();
639
705
  }
640
706
  this.connected = false;
641
707
  };
642
708
  /**
643
- *
644
- * @param motion
645
- */
646
- OrientationAdapterAcceler.prototype.dispatchMotion = function (motion) {
647
- this.transformers.forEach(function (t) { return t.update(motion); });
648
- };
649
- OrientationAdapterAcceler.prototype.addTransformer = function (transformer) {
709
+ *
710
+ * @param motion
711
+ */ _proto.dispatchMotion = function dispatchMotion(motion) {
712
+ this.transformers.forEach(function(t) {
713
+ return t.update(motion);
714
+ });
715
+ };
716
+ _proto.addTransformer = function addTransformer(transformer) {
650
717
  if (!this.transformers.includes(transformer)) {
651
718
  this.transformers.push(transformer);
652
719
  }
653
720
  };
654
- OrientationAdapterAcceler.prototype.removeTransformer = function (transformer) {
721
+ _proto.removeTransformer = function removeTransformer(transformer) {
655
722
  var index = this.transformers.indexOf(transformer);
656
723
  if (index > -1) {
657
724
  this.transformers.splice(index, 1);
@@ -659,7 +726,7 @@ var OrientationAdapterAcceler = /** @class */ (function () {
659
726
  return this.transformers.length === 0;
660
727
  };
661
728
  return OrientationAdapterAcceler;
662
- }());
729
+ }();
663
730
  var adapter;
664
731
  function getAccelerAdapter() {
665
732
  if (!adapter) {
@@ -668,72 +735,74 @@ function getAccelerAdapter() {
668
735
  return adapter;
669
736
  }
670
737
 
671
- var OrientationPluginLoader = /** @class */ (function (_super) {
672
- __extends(OrientationPluginLoader, _super);
738
+ var OrientationPluginLoader = /*#__PURE__*/ function(AbstractPlugin) {
739
+ _inherits(OrientationPluginLoader, AbstractPlugin);
673
740
  function OrientationPluginLoader() {
674
- var _this = _super !== null && _super.apply(this, arguments) || this;
741
+ var _this;
742
+ _this = AbstractPlugin.apply(this, arguments) || this;
675
743
  _this.order = 90;
676
744
  _this.adapter = null;
677
- _this.handleConnect = function () {
678
- var _a;
745
+ _this.handleConnect = function() {
679
746
  if (!closeManually) {
680
- (_a = _this.adapter) === null || _a === void 0 ? void 0 : _a.connect();
747
+ var _this_adapter;
748
+ (_this_adapter = _this.adapter) == null ? void 0 : _this_adapter.connect();
681
749
  }
682
750
  };
683
- _this.handleDisconnect = function () {
684
- var _a;
685
- (_a = _this.adapter) === null || _a === void 0 ? void 0 : _a.disconnect();
751
+ _this.handleDisconnect = function() {
752
+ var _this_adapter;
753
+ (_this_adapter = _this.adapter) == null ? void 0 : _this_adapter.disconnect();
686
754
  };
687
755
  return _this;
688
756
  }
689
- OrientationPluginLoader.prototype.onCompositionConstructed = function () {
690
- var _a;
757
+ var _proto = OrientationPluginLoader.prototype;
758
+ _proto.onCompositionConstructed = function onCompositionConstructed() {
759
+ var _this_adapter;
691
760
  this.adapter = getAdapter();
692
- (_a = this.adapter) === null || _a === void 0 ? void 0 : _a.connect();
761
+ (_this_adapter = this.adapter) == null ? void 0 : _this_adapter.connect();
693
762
  this.bindAccelerDocumentEvent();
694
763
  };
695
- OrientationPluginLoader.prototype.onCompositionReset = function (composition) {
696
- var _a;
764
+ _proto.onCompositionReset = function onCompositionReset(composition) {
765
+ var _this_adapter;
697
766
  var transformer = composition.loaderData.deviceTransformer = new CompositionTransformerAcceler(composition);
698
- (_a = this.adapter) === null || _a === void 0 ? void 0 : _a.addTransformer(transformer);
767
+ (_this_adapter = this.adapter) == null ? void 0 : _this_adapter.addTransformer(transformer);
699
768
  };
700
- OrientationPluginLoader.prototype.onCompositionUpdate = function (composition) {
769
+ _proto.onCompositionUpdate = function onCompositionUpdate(composition) {
701
770
  var transformer = composition.loaderData.deviceTransformer;
702
771
  if (transformer) {
703
772
  transformer.updateOrientation();
704
773
  }
705
774
  };
706
- OrientationPluginLoader.prototype.onCompositionDestroyed = function (composition) {
707
- var _a, _b;
708
- var empty = (_a = this.adapter) === null || _a === void 0 ? void 0 : _a.removeTransformer(composition.loaderData.deviceTransformer);
775
+ _proto.onCompositionDestroyed = function onCompositionDestroyed(composition) {
776
+ var _this_adapter;
777
+ var empty = (_this_adapter = this.adapter) == null ? void 0 : _this_adapter.removeTransformer(composition.loaderData.deviceTransformer);
709
778
  if (empty) {
710
- (_b = this.adapter) === null || _b === void 0 ? void 0 : _b.disconnect();
779
+ var _this_adapter1;
780
+ (_this_adapter1 = this.adapter) == null ? void 0 : _this_adapter1.disconnect();
711
781
  }
712
782
  };
713
- OrientationPluginLoader.prototype.bindAccelerDocumentEvent = function () {
783
+ _proto.bindAccelerDocumentEvent = function bindAccelerDocumentEvent() {
714
784
  this.unbindAccelerDocumentEvent(); // 保证全局只监听一份
715
- document.addEventListener('resume', this.handleConnect, false);
716
- document.addEventListener('pause', this.handleDisconnect, false);
717
- document.addEventListener('back', this.handleDisconnect, false);
785
+ document.addEventListener("resume", this.handleConnect, false);
786
+ document.addEventListener("pause", this.handleDisconnect, false);
787
+ document.addEventListener("back", this.handleDisconnect, false);
718
788
  };
719
- OrientationPluginLoader.prototype.unbindAccelerDocumentEvent = function () {
720
- document.removeEventListener('resume', this.handleConnect);
721
- document.removeEventListener('pause', this.handleDisconnect);
722
- document.removeEventListener('back', this.handleDisconnect);
789
+ _proto.unbindAccelerDocumentEvent = function unbindAccelerDocumentEvent() {
790
+ document.removeEventListener("resume", this.handleConnect);
791
+ document.removeEventListener("pause", this.handleDisconnect);
792
+ document.removeEventListener("back", this.handleDisconnect);
723
793
  };
724
- OrientationPluginLoader.prototype.dispose = function () {
794
+ _proto.dispose = function dispose() {
725
795
  getAdapter().disconnect();
726
796
  this.unbindAccelerDocumentEvent();
727
797
  };
728
798
  return OrientationPluginLoader;
729
- }(AbstractPlugin));
799
+ }(AbstractPlugin);
730
800
  // 是否是手动关闭,如果是,那么 resume 时不要恢复
731
801
  var closeManually = false;
732
802
  /**
733
803
  *
734
804
  * @returns
735
- */
736
- function getAdapter() {
805
+ */ function getAdapter() {
737
806
  // 运动感应类型(默认使用加速度,Native 环境使用陀螺仪)
738
807
  return getAccelerAdapter();
739
808
  }
@@ -746,61 +815,9 @@ function openDeviceMotion() {
746
815
  getAdapter().connect();
747
816
  }
748
817
 
749
- var OrientationComponent = /** @class */ (function (_super) {
750
- __extends(OrientationComponent, _super);
751
- function OrientationComponent() {
752
- return _super !== null && _super.apply(this, arguments) || this;
753
- }
754
- OrientationComponent.prototype.fromData = function (data) {
755
- _super.prototype.fromData.call(this, data);
756
- var targets = data.content.options.targets;
757
- if (targets) {
758
- this.targets = targets.map(function (t) { return ({
759
- name: t.name,
760
- xMin: +t.xMin || 0,
761
- yMin: +t.yMin || 0,
762
- xMax: +t.xMax || 0,
763
- yMax: +t.yMax || 0,
764
- }); });
765
- }
766
- };
767
- OrientationComponent.prototype.start = function () {
768
- var _a, _b;
769
- var transformer = (_a = this.item.composition) === null || _a === void 0 ? void 0 : _a.loaderData.deviceTransformer;
770
- if (transformer) {
771
- (_b = this.targets) === null || _b === void 0 ? void 0 : _b.forEach(function (target) { return transformer.addTarget(target); });
772
- transformer.initComposition();
773
- }
774
- };
775
- OrientationComponent.prototype.update = function (dt) {
776
- var _a;
777
- var transformer = (_a = this.item.composition) === null || _a === void 0 ? void 0 : _a.loaderData.deviceTransformer;
778
- if (transformer) {
779
- transformer.updateOrientation();
780
- }
781
- };
782
- OrientationComponent.prototype.onDestroy = function () {
783
- var _this = this;
784
- var _a;
785
- (_a = this.targets) === null || _a === void 0 ? void 0 : _a.forEach(function (target) { var _a, _b; return (_b = (_a = _this.item.composition) === null || _a === void 0 ? void 0 : _a.loaderData.deviceTransformer) === null || _b === void 0 ? void 0 : _b.removeTarget(target.name); });
786
- };
787
- return OrientationComponent;
788
- }(ItemBehaviour));
789
-
790
- var TransformVFXItem = /** @class */ (function (_super) {
791
- __extends(TransformVFXItem, _super);
792
- function TransformVFXItem(engine, props) {
793
- var _this = _super.call(this, engine, props) || this;
794
- var component = _this.addComponent(OrientationComponent);
795
- component.fromData(props);
796
- return _this;
797
- }
798
- return TransformVFXItem;
799
- }(VFXItem));
800
-
801
- registerPlugin('orientation-transformer', OrientationPluginLoader, TransformVFXItem);
802
- var version = "2.0.0-alpha.4";
803
- logger.info('plugin orientation transformer version: ' + version);
818
+ registerPlugin("orientation-transformer", OrientationPluginLoader, TransformVFXItem);
819
+ var version = "2.0.0-alpha.6";
820
+ logger.info("plugin orientation transformer version: " + version);
804
821
 
805
822
  export { OrientationAdapterAcceler, closeDeviceMotion, getAdapter, openDeviceMotion, version };
806
823
  //# sourceMappingURL=index.mjs.map