@soonspacejs/plugin-patrol-controls 2.6.0 → 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 +25 -1
- package/dist/index.esm.js +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,11 @@ import type { CameraViewpointData, CameraTargetViewData, Position } from 'soonsp
|
|
|
4
4
|
import type { Node } from 'soonspacejs/types/Library';
|
|
5
5
|
import type { Topology } from 'soonspacejs/types/Library';
|
|
6
6
|
import type { Tween } from '@tweenjs/tween.js';
|
|
7
|
+
export type ProgressParams = {
|
|
8
|
+
patrolled: number;
|
|
9
|
+
total: number;
|
|
10
|
+
percent: number;
|
|
11
|
+
};
|
|
7
12
|
export interface DefaultOptions {
|
|
8
13
|
eyeHeight: number;
|
|
9
14
|
/**
|
|
@@ -22,6 +27,10 @@ export interface DefaultOptions {
|
|
|
22
27
|
* 更新回调
|
|
23
28
|
*/
|
|
24
29
|
onUpdate: (realTimePosition: Position, realTimeRotation: Euler, nextNode: Node, toNextNodeDistance: number) => void;
|
|
30
|
+
/**
|
|
31
|
+
* 巡检进度
|
|
32
|
+
*/
|
|
33
|
+
onProgress: (params: ProgressParams) => void;
|
|
25
34
|
/**
|
|
26
35
|
* 结束回调
|
|
27
36
|
*/
|
|
@@ -30,7 +39,7 @@ export interface DefaultOptions {
|
|
|
30
39
|
export type StartOptions = Partial<DefaultOptions>;
|
|
31
40
|
export type ResetOptions = Pick<StartOptions, 'eyeHeight' | 'naviSpeed' | 'rotateSpeed'>;
|
|
32
41
|
export default class PatrolControlsPlugin {
|
|
33
|
-
ssp: SoonSpace;
|
|
42
|
+
readonly ssp: SoonSpace;
|
|
34
43
|
camera: PerspectiveCamera;
|
|
35
44
|
options: DefaultOptions;
|
|
36
45
|
states: {
|
|
@@ -39,6 +48,16 @@ export default class PatrolControlsPlugin {
|
|
|
39
48
|
};
|
|
40
49
|
nodes: Node[];
|
|
41
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;
|
|
42
61
|
isPaused: boolean;
|
|
43
62
|
isStoped: boolean;
|
|
44
63
|
_positionTween: Tween<Vector3> | null;
|
|
@@ -56,6 +75,11 @@ export default class PatrolControlsPlugin {
|
|
|
56
75
|
* @param options
|
|
57
76
|
*/
|
|
58
77
|
start(path: Topology, options: StartOptions): void;
|
|
78
|
+
/**
|
|
79
|
+
* 设置巡检进度
|
|
80
|
+
* @param percent 0 - 1(不包含 1)
|
|
81
|
+
*/
|
|
82
|
+
setProgress(percent: number): void;
|
|
59
83
|
/**
|
|
60
84
|
* 设置巡检参数
|
|
61
85
|
* @param options
|
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
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.6.
|
|
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.6.
|
|
18
|
+
"soonspacejs": "2.6.1"
|
|
19
19
|
}
|
|
20
20
|
}
|