@soonspacejs/plugin-patrol-controls 2.5.59 → 2.6.1
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.d.ts +33 -8
- package/dist/index.esm.js +1 -15
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import SoonSpace, { CameraViewpointData, CameraTargetViewData, Position } from 'soonspacejs';
|
|
2
|
-
import { Node } from 'soonspacejs/types/Library';
|
|
3
|
-
import { Topology } from 'soonspacejs/types/Library';
|
|
4
|
-
import { Tween } from '@tweenjs/tween.js';
|
|
5
1
|
import { PerspectiveCamera, Vector3, Euler } from 'three';
|
|
2
|
+
import SoonSpace from 'soonspacejs';
|
|
3
|
+
import type { CameraViewpointData, CameraTargetViewData, Position } from 'soonspacejs';
|
|
4
|
+
import type { Node } from 'soonspacejs/types/Library';
|
|
5
|
+
import type { Topology } from 'soonspacejs/types/Library';
|
|
6
|
+
import type { Tween } from '@tweenjs/tween.js';
|
|
7
|
+
export type ProgressParams = {
|
|
8
|
+
patrolled: number;
|
|
9
|
+
total: number;
|
|
10
|
+
percent: number;
|
|
11
|
+
};
|
|
6
12
|
export interface DefaultOptions {
|
|
7
13
|
eyeHeight: number;
|
|
8
14
|
/**
|
|
@@ -21,15 +27,19 @@ export interface DefaultOptions {
|
|
|
21
27
|
* 更新回调
|
|
22
28
|
*/
|
|
23
29
|
onUpdate: (realTimePosition: Position, realTimeRotation: Euler, nextNode: Node, toNextNodeDistance: number) => void;
|
|
30
|
+
/**
|
|
31
|
+
* 巡检进度
|
|
32
|
+
*/
|
|
33
|
+
onProgress: (params: ProgressParams) => void;
|
|
24
34
|
/**
|
|
25
35
|
* 结束回调
|
|
26
36
|
*/
|
|
27
37
|
onEnd: (endPosition: Position) => void;
|
|
28
38
|
}
|
|
29
|
-
export
|
|
30
|
-
export
|
|
39
|
+
export type StartOptions = Partial<DefaultOptions>;
|
|
40
|
+
export type ResetOptions = Pick<StartOptions, 'eyeHeight' | 'naviSpeed' | 'rotateSpeed'>;
|
|
31
41
|
export default class PatrolControlsPlugin {
|
|
32
|
-
ssp: SoonSpace;
|
|
42
|
+
readonly ssp: SoonSpace;
|
|
33
43
|
camera: PerspectiveCamera;
|
|
34
44
|
options: DefaultOptions;
|
|
35
45
|
states: {
|
|
@@ -38,6 +48,16 @@ export default class PatrolControlsPlugin {
|
|
|
38
48
|
};
|
|
39
49
|
nodes: Node[];
|
|
40
50
|
nextPointIndex: number;
|
|
51
|
+
/**
|
|
52
|
+
* y 轴加上 eyeHeight 的节点之间距离
|
|
53
|
+
*/
|
|
54
|
+
_nodeDistances: number[];
|
|
55
|
+
_totalDistance: number;
|
|
56
|
+
/**
|
|
57
|
+
* 0 - 1
|
|
58
|
+
*/
|
|
59
|
+
_updatePercent: number;
|
|
60
|
+
_needsUpdateProgress: boolean;
|
|
41
61
|
isPaused: boolean;
|
|
42
62
|
isStoped: boolean;
|
|
43
63
|
_positionTween: Tween<Vector3> | null;
|
|
@@ -55,6 +75,11 @@ export default class PatrolControlsPlugin {
|
|
|
55
75
|
* @param options
|
|
56
76
|
*/
|
|
57
77
|
start(path: Topology, options: StartOptions): void;
|
|
78
|
+
/**
|
|
79
|
+
* 设置巡检进度
|
|
80
|
+
* @param percent 0 - 1(不包含 1)
|
|
81
|
+
*/
|
|
82
|
+
setProgress(percent: number): void;
|
|
58
83
|
/**
|
|
59
84
|
* 设置巡检参数
|
|
60
85
|
* @param options
|
|
@@ -86,7 +111,7 @@ export default class PatrolControlsPlugin {
|
|
|
86
111
|
* 计算下一弧度
|
|
87
112
|
* @returns
|
|
88
113
|
*/
|
|
89
|
-
private
|
|
114
|
+
private computeNextRotation;
|
|
90
115
|
/**
|
|
91
116
|
* 开始巡检
|
|
92
117
|
* @returns
|
package/dist/index.esm.js
CHANGED
|
@@ -1,15 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Copyright (c) Microsoft Corporation.
|
|
3
|
-
|
|
4
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
-
purpose with or without fee is hereby granted.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
-
***************************************************************************** */
|
|
15
|
-
function t(t,i,e,s){return new(e||(e=Promise))((function(o,n){function a(t){try{h(s.next(t))}catch(t){n(t)}}function r(t){try{h(s.throw(t))}catch(t){n(t)}}function h(t){var i;t.done?o(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(a,r)}h((s=s.apply(t,i||[])).next())}))}class i{constructor(t){this._mainCameraViewpointData=null,this.ssp=t,this.camera=t.viewport.camera,this.options={naviSpeed:1,rotateSpeed:1,eyeHeight:150,flyToStartPoint:!0,onUpdate:()=>{},onEnd:()=>{}},this.states={moveDuration:0,rotateDuration:0},this.nodes=[],this.nextPointIndex=0,this.isPaused=!1,this.isStoped=!1,this._positionTween=null,this._rotationTween=null,this._cameraViewpointData=null}start(t,i){this.initOptions(i),this.init(t),this.patrolStart()}setOptions(t){if(this.isStoped)return;const{naviSpeed:i,rotateSpeed:e}=t;if(i&&this._positionTween){const t=this.states.moveDuration*this.options.naviSpeed/i;this._positionTween.duration(t),this.states.moveDuration=t}if(e&&this._rotationTween){const t=this.states.rotateDuration*this.options.rotateSpeed/e;this._rotationTween.duration(t),this.states.rotateDuration=t}this.initOptions(t)}pause(){var t,i;if(this.isPaused)return;this._cameraViewpointData=this.ssp.getCameraViewpoint();const{cameraManager:e}=this.ssp.viewport,s=e.getMainCamera();e.setCurrentCamera(s),this._cameraViewpointData&&this.ssp.setCameraViewpoint(this._cameraViewpointData),this.ssp.viewport.controls.currentControls.enabled=!1,this.isPaused=!0,null===(t=this._positionTween)||void 0===t||t.pause(),null===(i=this._rotationTween)||void 0===i||i.pause()}resume(){var t,i;if(this.isPaused){if(this._cameraViewpointData){const{cameraManager:t}=this.ssp.viewport;t.setCurrentCamera(this.camera),this.ssp.setCameraViewpoint(this._cameraViewpointData)}this.isPaused=!1,null===(t=this._positionTween)||void 0===t||t.resume(),null===(i=this._rotationTween)||void 0===i||i.resume()}}stop(){this.patrolStop();const{cameraManager:t}=this.ssp.viewport,i=t.getMainCamera();t.setCurrentCamera(i),this._mainCameraViewpointData&&this.ssp.setCameraViewpoint(this._mainCameraViewpointData),this.ssp.viewport.controls.currentControls.enabled=!0}init(t){const{cameraManager:i}=this.ssp.viewport;this.isPaused=!1,this.isStoped=!1,this.nextPointIndex=0,this._mainCameraViewpointData=this.ssp.getCameraViewpoint(),this.camera=i.cameras.patrolControls||i.createCamera("patrolControls"),i.setCurrentCamera(this.camera),this.ssp.setCameraViewpoint(this._mainCameraViewpointData),this.nodes=[...t.nodes]}initOptions(t){const{eyeHeight:i,naviSpeed:e,rotateSpeed:s,flyToStartPoint:o=!0,onUpdate:n,onEnd:a}=t;i&&(this.options.eyeHeight=i),e&&(this.options.naviSpeed=e),s&&(this.options.rotateSpeed=s),this.options.flyToStartPoint=o,n&&(this.options.onUpdate=n),a&&(this.options.onEnd=a)}computedRotation(t,i){const e=new this.ssp.THREE.Matrix4;e.lookAt(t,i,this.camera.up);const s=(new this.ssp.THREE.Euler).setFromRotationMatrix(e,"YXZ");return s.copy(this.ssp.utils.rotationAFix(this.camera.rotation,s.clone())),0===s.y&&(0===s.z?s.x=0:0===s.x&&(s.z=0)),s}computedNextRotation(){var t;const i=this.nodes[this.nextPointIndex].getWorldPosition(new this.ssp.THREE.Vector3),e=null===(t=this.nodes[this.nextPointIndex+1])||void 0===t?void 0:t.getWorldPosition(new this.ssp.THREE.Vector3);return e?this.computedRotation(i,e):new this.ssp.THREE.Euler}patrolStart(){return t(this,void 0,void 0,(function*(){return new Promise((i=>t(this,void 0,void 0,(function*(){const{onUpdate:t,onEnd:e}=this.options;if(this.nextPointIndex===this.nodes.length)null==e||e(this.camera.position.clone()),i(!0),this.isStoped=!0;else{const i=this.camera.position.clone(),e=this.computedNextRotation(),s=this.nodes[this.nextPointIndex],o=s.getWorldPosition(new this.ssp.THREE.Vector3);o.setY(o.y+this.options.eyeHeight);const n=i.distanceTo(o);if(this.states.moveDuration=0===this.nextPointIndex?1e3:n/this.options.naviSpeed/.6,0!==this.nextPointIndex||this.options.flyToStartPoint?yield this.ssp.animation(this.camera.position,o,{duration:this.states.moveDuration},((i,n)=>{var a;this.isPaused&&(null===(a=this._positionTween)||void 0===a||a.pause()),null==t||t(i,e,s,this.camera.position.clone().distanceTo(o))}),(t=>{this._positionTween=t})):this.camera.position.copy(o),this.nextPointIndex<this.nodes.length-1){const t=this.camera.rotation.clone(),i=this.computedNextRotation(),e=Math.abs(t.x-i.x)+Math.abs(t.y-i.y)+Math.abs(t.y-i.y);this.states.rotateDuration=0===this.nextPointIndex?1e3:100*e/this.options.rotateSpeed,0!==this.nextPointIndex||this.options.flyToStartPoint?yield this.ssp.animation(this.camera.rotation,i,{duration:this.states.rotateDuration},((t,i)=>{var e;this.isPaused&&(null===(e=this._rotationTween)||void 0===e||e.pause())}),(t=>{this._rotationTween=t})):this.camera.rotation.copy(i)}this.nextPointIndex++,yield this.patrolStart()}}))))}))}patrolStop(){var t,i;this.options={naviSpeed:1,rotateSpeed:1,eyeHeight:150,flyToStartPoint:!0,onUpdate:()=>{},onEnd:()=>{}},this.nodes=[],this.nextPointIndex=0,this.isPaused=!1,this.isStoped=!0,null===(t=this._positionTween)||void 0===t||t.stop(),null===(i=this._rotationTween)||void 0===i||i.stop(),this._positionTween=null,this._rotationTween=null}}export{i as default};
|
|
1
|
+
import{Vector3 as t,Matrix4 as i,Euler as e}from"three";function s(t,i,e,s){return new(e||(e=Promise))((function(o,n){function a(t){try{h(s.next(t))}catch(t){n(t)}}function r(t){try{h(s.throw(t))}catch(t){n(t)}}function h(t){var i;t.done?o(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(a,r)}h((s=s.apply(t,i||[])).next())}))}const o="patrolControls";class n{constructor(t){this.ssp=t,this.options={naviSpeed:1,rotateSpeed:1,eyeHeight:150,flyToStartPoint:!0,onUpdate:()=>{},onProgress:()=>{},onEnd:()=>{}},this.states={moveDuration:0,rotateDuration:0},this.nodes=[],this.nextPointIndex=0,this._nodeDistances=[],this._totalDistance=0,this._updatePercent=0,this._needsUpdateProgress=!1,this.isPaused=!1,this.isStoped=!0,this._positionTween=null,this._rotationTween=null,this._cameraViewpointData=null,this._mainCameraViewpointData=null,this.camera=this.ssp.viewport.camera}start(t,i){this.isStoped?(this.init(t),this.initOptions(i),this.patrolStart()):this.ssp.utils.warn("巡检已经开始!")}setProgress(t){var i,e;this._updatePercent=Math.min(Math.max(t,0),1),this._needsUpdateProgress=!0,null===(i=this._positionTween)||void 0===i||i.stop(),null===(e=this._rotationTween)||void 0===e||e.stop()}setOptions(t){if(this.isStoped)return;const{naviSpeed:i,rotateSpeed:e}=t;if(i&&this._positionTween){const t=this.states.moveDuration*this.options.naviSpeed/i;this._positionTween.duration(t),this.states.moveDuration=t}if(e&&this._rotationTween){const t=this.states.rotateDuration*this.options.rotateSpeed/e;this._rotationTween.duration(t),this.states.rotateDuration=t}this.initOptions(t)}pause(){var t,i;if(this.isPaused||this.isStoped)return;this._cameraViewpointData=this.ssp.getCameraViewpoint();const{cameraManager:e}=this.ssp.viewport,s=e.getMainCamera();e.setCurrentCamera(s),this._cameraViewpointData&&this.ssp.setCameraViewpoint(this._cameraViewpointData),this.ssp.viewport.controls.currentControls.enabled=!1,this.isPaused=!0,null===(t=this._positionTween)||void 0===t||t.pause(),null===(i=this._rotationTween)||void 0===i||i.pause()}resume(){var t,i;if(this.isPaused&&!this.isStoped){if(this._cameraViewpointData){const{cameraManager:t}=this.ssp.viewport;t.setCurrentCamera(this.camera),this.ssp.setCameraViewpoint(this._cameraViewpointData)}this.isPaused=!1,null===(t=this._positionTween)||void 0===t||t.resume(),null===(i=this._rotationTween)||void 0===i||i.resume()}}stop(){if(this.isStoped)return;this.patrolStop();const{cameraManager:t}=this.ssp.viewport,i=t.getMainCamera();t.setCurrentCamera(i),this._mainCameraViewpointData&&this.ssp.setCameraViewpoint(this._mainCameraViewpointData),this.ssp.viewport.controls.currentControls.enabled=!0}init(t){const{cameraManager:i}=this.ssp.viewport;this.isPaused=!1,this.isStoped=!1,this.nextPointIndex=0,this._mainCameraViewpointData=this.ssp.getCameraViewpoint(),this.camera=i.cameras[o]||i.createCamera(o),i.setCurrentCamera(this.camera),this.ssp.setCameraViewpoint(this._mainCameraViewpointData),this.nodes=[...t.nodes]}initOptions(i){const{eyeHeight:e,naviSpeed:s,rotateSpeed:o,flyToStartPoint:n=!0,onUpdate:a,onProgress:r,onEnd:h}=i;e&&(this.options.eyeHeight=e),s&&(this.options.naviSpeed=s),o&&(this.options.rotateSpeed=o),this.options.flyToStartPoint=n,a&&(this.options.onUpdate=a),r&&(this.options.onProgress=r),h&&(this.options.onEnd=h),this._nodeDistances.length=0,this._nodeDistances.push(0),this._totalDistance=0;for(let i=0,e=this.nodes.length;i<e-1;i++){const e=this.nodes[i],s=this.nodes[i+1];if(e&&s){const i=e.getWorldPosition(new t),o=s.getWorldPosition(new t);i.y+=this.options.eyeHeight,o.y+=this.options.eyeHeight;const n=i.distanceTo(o);this._nodeDistances.push(n),this._totalDistance+=n}}}computedRotation(t,s){const o=new i;o.lookAt(t,s,this.camera.up);const n=(new e).setFromRotationMatrix(o,"YXZ");return n.copy(this.ssp.utils.rotationAFix(this.camera.rotation,n.clone())),0===n.y&&(0===n.z?n.x=0:0===n.x&&(n.z=0)),n}computeNextRotation(){let i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.nextPointIndex;var s,o;const n=null===(s=this.nodes[i])||void 0===s?void 0:s.getWorldPosition(new t),a=null===(o=this.nodes[i+1])||void 0===o?void 0:o.getWorldPosition(new t);return n&&a?this.computedRotation(n,a):new e}patrolStart(){return s(this,void 0,void 0,(function*(){return new Promise((i=>s(this,void 0,void 0,(function*(){const{onUpdate:e,onProgress:s,onEnd:o}=this.options;if(this.nextPointIndex>=this.nodes.length)null==o||o(this.camera.position.clone()),i(!0);else{if(this._needsUpdateProgress){let i=this._totalDistance*this._updatePercent;for(let e=0,s=this._nodeDistances.length;e<s;e++)if(i-=this._nodeDistances[e],i<0){this.nextPointIndex=e,i+=this._nodeDistances[e];const s=i/this._nodeDistances[e]||0,o=this.nodes[e-1],n=this.nodes[e],a=new t,r=o.getWorldPosition(new t),h=n.getWorldPosition(new t);r.y+=this.options.eyeHeight,h.y+=this.options.eyeHeight;const p=(new t).subVectors(h,r).multiplyScalar(s);a.copy(r).add(p),this.camera.position.copy(a),this.camera.rotation.copy(this.computeNextRotation(e-1));break}this._needsUpdateProgress=!1}const i=this.camera.position.clone(),o=this.computeNextRotation(),n=this.nodes[this.nextPointIndex],a=n.getWorldPosition(new t);a.setY(a.y+this.options.eyeHeight);const r=i.distanceTo(a);if(this.states.moveDuration=0===this.nextPointIndex?1e3:r/this.options.naviSpeed/.6,0!==this.nextPointIndex||this.options.flyToStartPoint)try{yield this.ssp.animation(this.camera.position,a,{duration:this.states.moveDuration},(t=>{var i;this.isPaused&&(null===(i=this._positionTween)||void 0===i||i.pause());const r=this.camera.position.distanceTo(a);if(null==e||e(t,o,n,r),this._nodeDistances[this.nextPointIndex]>0){let t=this._nodeDistances.slice(0,this.nextPointIndex+1).reduce(((t,i)=>t+i),0);t-=r,null==s||s({patrolled:t,total:this._totalDistance,percent:t/this._totalDistance})}}),(t=>{this._positionTween=t}))}catch(t){}else this.camera.position.copy(a);if(this.nextPointIndex<this.nodes.length-1){const t=this.camera.rotation.clone(),i=this.computeNextRotation(),e=Math.abs(t.x-i.x)+Math.abs(t.y-i.y)+Math.abs(t.y-i.y);if(this.states.rotateDuration=0===this.nextPointIndex?1e3:100*e/this.options.rotateSpeed,0!==this.nextPointIndex||this.options.flyToStartPoint)try{yield this.ssp.animation(this.camera.rotation,i,{duration:this.states.rotateDuration},((t,i)=>{var e;this.isPaused&&(null===(e=this._rotationTween)||void 0===e||e.pause())}),(t=>{this._rotationTween=t}))}catch(t){}else this.camera.rotation.copy(i)}this._needsUpdateProgress||this.nextPointIndex++,this.isStoped||(yield this.patrolStart())}}))))}))}patrolStop(){var t,i;this.options={naviSpeed:1,rotateSpeed:1,eyeHeight:150,flyToStartPoint:!0,onUpdate:()=>{},onProgress:()=>{},onEnd:()=>{}},this.nodes=[],this.nextPointIndex=0,this.isPaused=!1,this.isStoped=!0,this._needsUpdateProgress=!1,null===(t=this._positionTween)||void 0===t||t.stop(),null===(i=this._rotationTween)||void 0===i||i.stop(),this._positionTween=null,this._rotationTween=null}}export{n as default};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soonspacejs/plugin-patrol-controls",
|
|
3
3
|
"pluginName": "PatrolControlsPlugin",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.6.1",
|
|
5
5
|
"description": "Patrol-controls plugin for SoonSpace.js",
|
|
6
6
|
"main": "dist/index.esm.js",
|
|
7
7
|
"module": "dist/index.esm.js",
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
],
|
|
14
14
|
"author": "xuek",
|
|
15
15
|
"license": "UNLICENSED",
|
|
16
|
-
"gitHead": "
|
|
16
|
+
"gitHead": "7c3d5b68b469b40bce705edf9fc5b25aa23d972c",
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"soonspacejs": "2.
|
|
18
|
+
"soonspacejs": "2.6.1"
|
|
19
19
|
}
|
|
20
20
|
}
|