@soonspacejs/plugin-navigation 2.13.6 → 2.13.7
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/README.md +113 -113
- package/dist/index.esm.js +1168 -1
- package/dist/index.esm.js.map +1 -0
- package/package.json +3 -3
- package/dist/azimuth/index.d.ts +0 -4
- package/dist/camera/index.d.ts +0 -5
- package/dist/camera/map-camera.d.ts +0 -14
- package/dist/camera/navigate-camera.d.ts +0 -202
- package/dist/camera/ssp-camera.d.ts +0 -13
- package/dist/camera/types.d.ts +0 -26
- package/dist/camera/utils.d.ts +0 -11
- package/dist/index.d.ts +0 -4
- package/dist/navigator/index.d.ts +0 -68
- package/dist/navigator/navigator-nodes.d.ts +0 -26
- package/dist/navigator/types.d.ts +0 -23
- package/dist/navigator/utils.d.ts +0 -12
- package/dist/utils/gyro.d.ts +0 -26
- package/dist/utils/index.d.ts +0 -1
package/dist/index.esm.js
CHANGED
|
@@ -1 +1,1168 @@
|
|
|
1
|
-
import{OrthographicCamera as t,Matrix4 as e,Quaternion as i,Euler as o,Vector3 as n,PerspectiveCamera as s,Object3D as r,MathUtils as a,Matrix3 as h}from"three";class l{constructor(t,e){this.ssp=t,this.nativeCamera=e,this._isActive=!1,this.ssp.signals.cameraObjectChange.add((()=>{const t=this.ssp.viewport.cameraManager.currentCamera;this._isActive&&t===this.nativeCamera||(t===this.nativeCamera?(this._onBeforeEnter(),this.onEnter()):this._isActive&&t!==this.nativeCamera&&(this._onBeforeQuit(),this.onQuit()))}))}active(){this.ssp.viewport.cameraManager.setCurrentCamera(this.nativeCamera)}_onBeforeEnter(){this._isActive=!0}_onBeforeQuit(){this._isActive=!1}onEnter(){}onQuit(){}}class u extends l{get zoom(){return this._zoom}set zoom(t){this._zoom=t,this.resizeCamera()}constructor(e){const i=new t;super(e,i),this.ssp=e,this.resizeObserver=new ResizeObserver((()=>{this.resizeCamera()})),this._zoom=.01,i.far=2e5}resizeCamera(){if(this._isActive){const{clientWidth:t,clientHeight:e}=this.ssp.viewport.renderer.domElement,i=t*this.zoom/2,o=e*this.zoom/2;this.nativeCamera.left=-i,this.nativeCamera.right=i,this.nativeCamera.top=o,this.nativeCamera.bottom=-o,this.nativeCamera.updateProjectionMatrix()}}onEnter(){console.log("map camera enter"),this.ssp.domElement&&(this.resizeObserver.observe(this.ssp.domElement),this.resizeCamera())}onQuit(){this.ssp.domElement&&(this.resizeObserver.unobserve(this.ssp.domElement),console.log("map camera quit"))}}class c{constructor(t,e,i,o){this.gyroEvent=null,this.onChange=()=>{},this._isActive=!1,this.defaultBeta=null,this.defaultAlpha=null,this.defaultGamma=null,this.cache=null,this.beta=null,this.alpha=null,this.gamma=null,this.gyroX=null==t||t,this.gyroY=null==e||e,this.gyroZ=null==i||i,this.absolute=null==o||o}requestPermisson(){return new Promise(((t,e)=>{var i;window.DeviceOrientationEvent.requestPermission||t(!0),null===(i=window.DeviceOrientationEvent)||void 0===i||i.requestPermission().then((i=>{"granted"===i?t(!0):e("用户拒绝使用陀螺仪权限")}))}))}async active(){await this.requestPermisson(),this._isActive=!0,this.gyroEvent&&this.inActive(),this.gyroEvent=t=>{const{defaultAlpha:e,defaultBeta:i,defaultGamma:o,gyroX:n,gyroY:s,gyroZ:r}=this,{alpha:a,beta:h,gamma:l}=t,u=s&&null!=a?a:e,c=n&&null!=h?h:i,p=r&&null!=l?l:o;if(this.absolute)this.alpha=u,this.beta=c,this.gamma=p;else if(this.cache){const{alpha:t,beta:e,gamma:i}=this.cache;this.alpha=null===u?null:null===t?u:u-t,this.beta=null===c?null:null===e?c:c-e,this.gamma=null===p?null:null===i?p:p-i}else this.cache={alpha:u,beta:c,gamma:p};this.onChange(this.alpha,this.beta,this.gamma)},window.addEventListener("deviceorientation",this.gyroEvent)}inActive(){this._isActive=!1,this.gyroEvent&&(window.removeEventListener("deviceorientation",this.gyroEvent),this.gyroEvent=null)}}var p,g;function d(t){const s=new e,r=new i,a=new o;return s.lookAt(new n,t,new n(0,1,0)),r.setFromRotationMatrix(s),a.setFromQuaternion(r),a}function m(t){return new n(0,0,-1).applyQuaternion((new i).setFromEuler(t))}function v(t){return m(t).normalize()}function y(t,e){return"function"==typeof t?t(e):t}function T(t,e){return"function"==typeof t?t(e):t}!function(t){t.FIRST_VISION="first",t.THIRD_VISION="third",t.UP_VISION="up",t.LEFT_VISION="left"}(p||(p={})),function(t){t.RELATIVE_ORIENTATION="relative",t.FIXED_ORIENTATION="fixed",t.GYRO_ORIENTATION="gyro"}(g||(g={}));const f=Object.freeze({disabledAnimate:!1,fixedOrientation:!1,autoRestoreOrientation:1500,oppositeCamera:!1,oppositeType:{x:!1,y:!0,z:!1},orientationTarget:"main",vision:p.THIRD_VISION,orientationType:g.RELATIVE_ORIENTATION,rotationToTarget:new n(0,4,-10),distanceToTarget:1,targetRotationFix:[0,0,0,"XYZ"],isFixRotationRelativeTarget:!0,targetPositionFix:[0,2,0],isFixPositionRelativeTarget:!1,enableGyro:!1,gyroX:!1,gyroY:!0,gyroZ:!1,gyroAbsolute:!0,onControlStart:null,onControlRender:null,onControlEnd:null});function _(t){const e=(new i).setFromEuler((new o).fromArray(t));this.quaternion.multiply(e)}class w extends l{get position(){return this.nativeCamera.position}get rotation(){return this.nativeCamera.rotation}get quaternion(){return this.nativeCamera.quaternion}_lookAt(...t){this.nativeCamera.lookAt(...t),this.isFixRotationRelativeTarget||_.call(this.nativeCamera,this.targetRotationAfterFix)}get enableGyro(){return this._enableGyro}set enableGyro(t){this._enableGyro=t,t?this._gyroListener.active():this._gyroListener.inActive()}get gyroX(){return this._gyroListener.gyroX}set gyroX(t){this._gyroListener.gyroX=t}get gyroY(){return this._gyroListener.gyroY}set gyroY(t){this._gyroListener.gyroY=t}get gyroZ(){return this._gyroListener.gyroZ}set gyroZ(t){this._gyroListener.gyroZ=t}get gyroAbsolute(){return this._gyroListener.absolute}set gyroAbsolute(t){this._gyroListener.absolute=t}get targetRotationAfterFix(){return T(this.targetRotationFix)}get targetPositionAfterFix(){return y(this.targetPositionFix)}get realDistanceToTarget(){return"first"===this.vision?1:this.distanceToTarget}get realRotationToTarget(){switch(this.vision){case"first":return new n(0,0,-1);case"up":return new n(0,1,-.01);case"left":return new n(1,0,0);default:return this.rotationToTarget}}get orientationType(){return this._orientationType}set orientationType(t){this._orientationType=t,t===g.GYRO_ORIENTATION?this._gyroListener.active():this._gyroListener.inActive(),t===g.FIXED_ORIENTATION&&this._fixedOrientationRotation.copy(this.followRotation),this.restoreOrientation()}get orientationTarget(){return this._orientationTarget}set orientationTarget(t){this._orientationTarget=t,this.restoreOrientation()}get vision(){return this._vision}set vision(t){this._vision=t,this.followTarget.visible=t!==p.FIRST_VISION,this.restoreOrientation()}get followTarget(){return this._followTarget}set followTarget(t){this._followTarget=t,this.restoreOrientation()}get followPosition(){const t=new n;return t.fromArray(this.targetPositionAfterFix),this.isFixPositionRelativeTarget&&t.applyEuler(this.followRotation),this.followTarget.position.clone().add(t)}get followRotation(){const t=this.followTarget.rotation;if(this.isFixRotationRelativeTarget){const e=(new o).copy(t),n=(new i).setFromEuler(e),s=(new i).setFromEuler((new o).fromArray(this.targetRotationAfterFix));return n.multiply(s),(new o).setFromQuaternion(n)}return this.followTarget.rotation}get fixedOrientation(){return this._fixedOrientation}set fixedOrientation(t){this._fixedOrientation=t,t||this.restoreOrientation()}constructor(t,e,i){const n=null!=e?e:new s(80,1,.1,100);super(t,n),this.ssp=t,this.initOptions=f,this.cameraKey="navigatorCamera"+~~(1e4*Math.random()),this._events={onRender:()=>{},onMouseDown:t=>{}},this._restoreTimer=void 0,this._positionTween=null,this.autoFollowRender=!0,this._cameraPlaceholder=new r,this.disabledAnimate=f.disabledAnimate,this.distanceToTarget=f.distanceToTarget,this.rotationToTarget=f.rotationToTarget,this.oppositeCamera=f.oppositeCamera,this.oppositeType=f.oppositeType,this.isFixRotationRelativeTarget=f.isFixRotationRelativeTarget,this.targetRotationFix=f.targetRotationFix,this.isFixPositionRelativeTarget=f.isFixPositionRelativeTarget,this.targetPositionFix=f.targetPositionFix,this.autoRestoreOrientation=f.autoRestoreOrientation,this.onControlStart=null,this.onControlRender=null,this.onControlEnd=null,this._enableGyro=!1,this._gyroRotation=new o,this._orientationType=f.orientationType,this._fixedOrientationRotation=new o,this._orientationTarget=f.orientationTarget,this._vision=f.vision,this._followTarget=new r,this._fixedOrientation=f.fixedOrientation,this.initGyroListener();const a=Object.freeze(Object.assign(Object.assign({},f),null!=i?i:{}));this.initOptions=a,this.setOptions(a),this.setCamera(n)}initGyroListener(){const t=this.initOptions;this._gyroListener=new c(t.gyroX,t.gyroY,t.gyroZ,t.gyroAbsolute),this._gyroListener.onChange=(t,e,i)=>{const n=null===t?0:a.degToRad(t),s=null===e?0:a.degToRad(e),r=null===i?0:a.degToRad(i);this._gyroRotation=new o(s,n,r)}}setCameraOrientationOnControl(){const{rotation:t}=this.ssp.viewport.cameraManager.mainCamera,{realDistanceToTarget:e,followPosition:i}=this,o=v(t);this.position.copy(i.clone().add(o.multiplyScalar(-e))),this._lookAt(this.followPosition)}updateCameraPlaceholder(){const t=this.getRenderCameraPosition();this._cameraPlaceholder.position.copy(t)}setCameraOrientationOnRender(){this.position.copy(this._cameraPlaceholder.position),this._lookAt(this.followPosition)}getRenderCameraOrientation(){const{_fixedOrientationRotation:t,_orientationType:e,_orientationTarget:i}=this,s=new o;switch(e===g.GYRO_ORIENTATION&&s.copy(this._gyroRotation),!0){case"main"===i:s.copy(e===g.FIXED_ORIENTATION?t:this.followRotation);break;case i instanceof n:{const t=this.orientationTarget.clone().sub(this.followPosition);s.copy(d(t));break}case i instanceof r:{const t=i;switch(e){case g.RELATIVE_ORIENTATION:s.copy(t.rotation);break;case g.FIXED_ORIENTATION:s.copy(d(t.position.clone().sub(this.followPosition)))}break}case i instanceof o:s.copy(i)}return s}getRenderCameraPosition(){const t=new n,e=this.getRenderCameraOrientation(),{realDistanceToTarget:i,realRotationToTarget:o}=this;if(t.copy(this.followPosition.clone().add(o.clone().applyEuler(e).normalize().multiplyScalar(i))),this.oppositeCamera){const e=this.followPosition.clone().sub(t).multiplyScalar(2),{x:i,y:o,z:n}=this.oppositeType;[i,o,n].includes(!1)&&[i,o,n].includes(!0)?(i&&(e.x=0),o&&(e.y=0),n&&(e.z=0),t.add(e)):i&&t.add(e)}return t}onRender(){this.followTarget&&(this.updateCameraPlaceholder(),this._positionTween||(this.autoFollowRender||this.fixedOrientation?this.setCameraOrientationOnRender():this.onControlRender?this.onControlRender(this.nativeCamera,this.followTarget):this.setCameraOrientationOnControl()))}initEvents(){var t;const e=this.ssp.viewport.renderer.domElement,n=this.ssp.viewport.cameraManager.mainCamera,s=this.onRender.bind(this),r=t=>{var e,s;const r=!1===this.autoFollowRender;if(this.autoFollowRender=!1,null===(e=this._positionTween)||void 0===e||e.stop(),window.clearTimeout(this._restoreTimer),document.removeEventListener("mouseup",a),document.removeEventListener("touchend",a),document.addEventListener("mouseup",a,!0),document.addEventListener("touchend",a,!0),!r){if(this.isFixRotationRelativeTarget)n.quaternion.copy(this.quaternion);else{const t=(new i).setFromEuler((new o).fromArray(this.targetRotationAfterFix)).invert();n.quaternion.copy((new i).copy(this.quaternion).multiply(t))}null===(s=this.onControlStart)||void 0===s||s.call(this,this.nativeCamera,this.followTarget,t)}},a=t=>{const e=()=>{var e;this.restoreOrientation(),this.autoFollowRender=!0,null===(e=this.onControlEnd)||void 0===e||e.call(this,this.nativeCamera,this.followTarget,t),document.removeEventListener("mouseup",a,!0),document.removeEventListener("touchend",a,!0)};"number"==typeof this.autoRestoreOrientation?(window.clearTimeout(this._restoreTimer),this._restoreTimer=setTimeout(e,this.autoRestoreOrientation)):this.autoRestoreOrientation&&e()};this._events.onRender=s,this._events.onMouseDown=r;const h=this.ssp.signals;h.beforeRender.add(s),h.mouseDown.add(r),null===(t=e.parentElement)||void 0===t||t.addEventListener("touchstart",r,!0)}clearEvents(){var t;const{onRender:e,onMouseDown:i}=this._events,o=this.ssp.signals,n=this.ssp.viewport.renderer.domElement;o.beforeRender.remove(e),o.mouseDown.remove(i),null===(t=n.parentElement)||void 0===t||t.removeEventListener("touchstart",i,!0)}onEnter(){console.log("onenter"),this.initEvents()}onQuit(){console.log("onquit"),this.clearEvents(),this._gyroListener.inActive()}resetOptions(){this.setOptions(Object.assign({},this.initOptions))}setOptions(t){const e=this.disabledAnimate;this.disabledAnimate=!0,void 0!==t.isFixPositionRelativeTarget&&(this.isFixPositionRelativeTarget=t.isFixPositionRelativeTarget),void 0!==t.targetPositionFix&&(this.targetPositionFix=t.targetPositionFix),void 0!==t.isFixRotationRelativeTarget&&(this.isFixRotationRelativeTarget=t.isFixRotationRelativeTarget),void 0!==t.targetRotationFix&&(this.targetRotationFix=t.targetRotationFix),void 0!==t.rotationToTarget&&(this.rotationToTarget=t.rotationToTarget),void 0!==t.distanceToTarget&&(this.distanceToTarget=t.distanceToTarget),void 0!==t.fixedOrientation&&(this.fixedOrientation=t.fixedOrientation),void 0!==t.oppositeCamera&&(this.oppositeCamera=t.oppositeCamera),void 0!==t.oppositeType&&(this.oppositeType=t.oppositeType),void 0!==t.orientationTarget&&(this.orientationTarget=t.orientationTarget),void 0!==t.orientationType&&(this.orientationType=t.orientationType),void 0!==t.vision&&(this.vision=t.vision),void 0!==t.autoRestoreOrientation&&(this.autoRestoreOrientation=t.autoRestoreOrientation),void 0!==t.gyroX&&(this.gyroX=t.gyroX),void 0!==t.gyroY&&(this.gyroY=t.gyroY),void 0!==t.gyroZ&&(this.gyroZ=t.gyroZ),void 0!==t.gyroAbsolute&&(this.gyroAbsolute=t.gyroAbsolute),void 0!==t.enableGyro&&(this.enableGyro=t.enableGyro),void 0!==t.onControlStart&&(this.onControlStart=t.onControlStart),void 0!==t.onControlRender&&(this.onControlRender=t.onControlRender),void 0!==t.onControlEnd&&(this.onControlEnd=t.onControlEnd),this.disabledAnimate=e,void 0!==t.disabledAnimate&&(this.disabledAnimate=t.disabledAnimate)}setCamera(t){return this._isActive&&this.ssp.viewport.cameraManager.removeCamera(this.cameraKey),this.nativeCamera=t,this.ssp.viewport.cameraManager.cameras[this.cameraKey]=t,this._isActive&&this.ssp.viewport.cameraManager.setCurrentCamera(t),this}restoreOrientation(){if(this.followTarget&&!this.fixedOrientation){if(this._positionTween&&(this._positionTween.stop(),this._positionTween=null),this.disabledAnimate)return this.position.copy(this._cameraPlaceholder.position),void this._lookAt(this.followPosition);this.ssp.animation(this.position,this._cameraPlaceholder.position,{duration:1e3},(t=>{this._lookAt(this.followPosition)}),(t=>this._positionTween=t)).finally((()=>{this._positionTween=null}))}}}var O;!function(t){t.PENDING="pending",t.STOP="stop",t.PLAYING="playing",t.PAUSE="pause",t.FINISHED="finished"}(O||(O={}));var R=Object.defineProperty,A=(t,e,i)=>(((t,e,i)=>{e in t?R(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[e]=i})(t,"symbol"!=typeof e?e+"":e,i),i);function E(t,e,i){let o=function(t,e){const i=Math.sqrt(t.lengthSq()*e.lengthSq());if(0===i)return 0;let o=t.dot(e)/i;return o=Math.max(-1,Math.min(1,o)),Math.acos(o)}(t,e);return 0===o?o:t.clone().cross(e).dot(i)<0?-o:o}const C=180/Math.PI,b={yaw:[{name:"前",range:[-15,15]},{name:"左",range:[15,165]},{name:"右",range:[-165,-15]},{name:"后",range:[-180.1,-165]},{name:"后",range:[165,180.1]}],pitch:[{name:"前",range:[-15,15]},{name:"上",range:[15,165]},{name:"下",range:[-165,-15]},{name:"后",range:[-180.1,-165]},{name:"后",range:[165,180.1]}],roll:[{name:"上",range:[-15,15]},{name:"左",range:[15,165]},{name:"右",range:[-165,-15]},{name:"下",range:[-180.1,-165]},{name:"下",range:[165,180.1]}]};const x={degrees:!0,map:b,front:{x:0,y:0,z:1},up:{x:0,y:1,z:0}};class P{constructor(t){A(this,"_options"),A(this,"_listMap"),A(this,"_front"),A(this,"_up"),t&&(this.options=t)}static get options(){return this._options??(this.options=x)}static set options(t){this._options=Object.assign({},structuredClone(b),t)}get defaultOptions(){return this.constructor.options}get options(){return this._options??(this.options=this.defaultOptions)}set options(t){this._options=Object.assign({},structuredClone(this.defaultOptions),t),this._listMap=null,this._front=null,this._up=null}get degrees(){return this.options.degrees??(this.options.degrees=this.defaultOptions.degrees??!0)}set degrees(t){this.options.degrees=t}get map(){return this.options.map||(this.map=this.defaultOptions.map),this.options.map}set map(t){const e=structuredClone(this.defaultOptions.map),i=structuredClone(b);t=t?{yaw:t.yaw??e.yaw??i.yaw,pitch:t.pitch??e.pitch??i.pitch,roll:t.roll??e.roll??i.roll}:e,this.options.map=t,this._listMap=null}get listMap(){return this._listMap??(this._listMap=function(t){const e={};for(const[i,o]of Object.entries(t))e[i]=Array.isArray(o)?o:Object.entries(o).map((([t,e])=>({name:t,range:e})));return e}(this.map??{}))}get front(){return this._front||(this.front=this.options.front??this.defaultOptions.front??x.front),this._front}set front(t){this._front=(new n).copy(t)}get up(){return this._up||(this.up=this.options.up??this.defaultOptions.up??x.up),this._up}set up(t){this._up=(new n).copy(t)}computeAzimuth(t,e,i){const o=(new n).copy(t),s=(new n).copy(e??this.front),r=(new n).copy(i??this.up);let{yaw:a,pitch:h,roll:l}=function(t,e,i){const o=e.clone().negate(),n=i.clone().projectOnPlane(e),s=E(e,t.clone().projectOnPlane(n),n),r=e.clone().cross(n);return{yaw:s,pitch:E(e,t.clone().projectOnPlane(r),r),roll:E(n,t.clone().projectOnPlane(o),o)}}(o,s,r);return this.degrees&&(a*=C,h*=C,l*=C),{yaw:{angle:a,name:this.findAzimuthNames("yaw",a)},pitch:{angle:h,name:this.findAzimuthNames("pitch",h)},roll:{angle:l,name:this.findAzimuthNames("roll",l)}}}findAzimuthNames(t,e){const i=[],o=this.listMap[t];if(!o)return i;for(const{name:t,range:[n,s]}of o)(e>=n&&e<s||e<=n&&e>s)&&i.push(t);return i}}A(P,"_options");const F=new P;function I(t,e){F.map={yaw:{left:[20,170],right:[-170,-20]},pitch:{up:[20,170],down:[-170,-20]}};const{yaw:i,pitch:o}=F.computeAzimuth(t,e),n=i.name,s=o.name;return n&&s?s?n?`${n}-${s}`:s:n:"straight"}function N(t,e){return e.reverse().map((e=>{t.removeObjectById(e.sid);const i=e.nodes.reverse().map((t=>({id:t.sid,position:t.position,graphs:t.graphs})));return t.createTopology(Object.assign(Object.assign({},e.info),{nodes:i}))}))}function S(t,e,i){if(!e||!i)return"straight";return I(e.position.clone().sub(t.position),i.position.clone().sub(e.position))}function L(t){const e=t.nodes,i=t.getLength();let o=0,n=0;return e.reduce(((t,s,r)=>{const a=e[r+1],h=e[r+2],l=a?a.position.clone().distanceTo(s.position):0,u=S(s,a,h),c=o+l,p=c/i,g={progressArea:[n,p],distanceArea:[o,c],node:s,nextNode:a,distanceToStart:o,distanceToEnd:i-o,distanceToNext:l,frontDirection:u};return o=c,n=p,t.push(g),t}),[])}function z(t,e){return 0===t?e[0]:100===t?e[e.length-1]:e.find((e=>{const[i,o]=e.progressArea;return t>=i&&t<o}))}function M(t,e){if(!e)return{distanceToStartNode:0,distanceToEndNode:0,distanceToStart:0,distanceToEnd:0};const[i,o]=e.progressArea,{distanceToNext:n,distanceToEnd:s,distanceToStart:r}=e,a=(t-i)/(o-i),h=n*a,l=n*(1-a);return{distanceToStartNode:h,distanceToEndNode:l,distanceToStart:h+r,distanceToEnd:s-l}}new h;class D{get model(){return this._model}set model(t){this._model=t}get speed(){return this._speed}set speed(t){this._speed=t}get pathProgress(){return this._pathProgress}set pathProgress(t){this._pathProgress=t}get totalProgress(){return this._pathProgress}set totalProgress(t){this._pathProgress=t}get currentPath(){return this.paths[this.currentPathIndex]}get currentPathIndex(){return this._currentPathIndex}get currentRotationVector(){return this._nextNode&&this._currentNode?this._nextNode.clone().sub(this._currentNode):new n}constructor(t,e){this.ssp=t,this.paths=e,this.onPlay=()=>{},this.onStatusChange=()=>{},this.loop=!1,this.autoNext=!0,this.playAfterStart=!1,this.flyToModelAfterStart=!0,this.backStartPointWhenStop=!0,this._model=new r,this._speed=1,this._pathProgress=0,this._totalProgress=0,this._currentPathIndex=0,this._currentNode=null,this._nextNode=null,this._playStatus=O.PENDING}changeStatus(t){this._playStatus=t,this.onStatusChange(t)}start(){}restart(){this.stop(),this.start()}play(t=0,e){this.changeStatus(O.PLAYING),this.paths[t]}pause(){this.changeStatus(O.PAUSE)}stop(){this.changeStatus(O.STOP)}flip(){}over(){this.changeStatus(O.PENDING)}}const G=new P;export{P as Azimuth,c as GyroListener,u as MapCamera,g as NAVIGATE_ORIENTATION_TYPE,p as NAVIGATE_VISION_TYPE,w as NavigateCamera,D as Navigator,O as PLAY_STATUS,l as SoonSpaceCamera,d as Vector3ToEuler,G as azimuth,m as eulerToVector3,I as getDirection,S as getDirectionByNodes,M as getDistanceInfoByProgress,T as getFixEuler,y as getFixVector,z as getNavigatorNodeInfoByProgress,L as getNavigatorNodeInfosByTopology,v as normalizeEuler,N as reverseTopologies};
|
|
1
|
+
import { OrthographicCamera, Matrix4, Quaternion, Euler, Vector3, PerspectiveCamera, Object3D, MathUtils, Matrix3 } from 'three';
|
|
2
|
+
|
|
3
|
+
class SoonSpaceCamera {
|
|
4
|
+
constructor(ssp, nativeCamera) {
|
|
5
|
+
this.ssp = ssp;
|
|
6
|
+
this.nativeCamera = nativeCamera;
|
|
7
|
+
this._isActive = false;
|
|
8
|
+
this.ssp.signals.cameraObjectChange.add(() => {
|
|
9
|
+
const currentCamera = this.ssp.viewport.cameraManager
|
|
10
|
+
.currentCamera;
|
|
11
|
+
if (this._isActive && currentCamera === this.nativeCamera)
|
|
12
|
+
return;
|
|
13
|
+
else if (currentCamera === this.nativeCamera) {
|
|
14
|
+
this._onBeforeEnter();
|
|
15
|
+
this.onEnter();
|
|
16
|
+
}
|
|
17
|
+
else if (this._isActive && currentCamera !== this.nativeCamera) {
|
|
18
|
+
this._onBeforeQuit();
|
|
19
|
+
this.onQuit();
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
active() {
|
|
24
|
+
this.ssp.viewport.cameraManager.setCurrentCamera(this.nativeCamera);
|
|
25
|
+
}
|
|
26
|
+
_onBeforeEnter() {
|
|
27
|
+
this._isActive = true;
|
|
28
|
+
}
|
|
29
|
+
_onBeforeQuit() {
|
|
30
|
+
this._isActive = false;
|
|
31
|
+
}
|
|
32
|
+
onEnter() { }
|
|
33
|
+
onQuit() { }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class MapCamera extends SoonSpaceCamera {
|
|
37
|
+
get zoom() {
|
|
38
|
+
return this._zoom;
|
|
39
|
+
}
|
|
40
|
+
set zoom(value) {
|
|
41
|
+
this._zoom = value;
|
|
42
|
+
this.resizeCamera();
|
|
43
|
+
}
|
|
44
|
+
constructor(ssp) {
|
|
45
|
+
const camera = new OrthographicCamera();
|
|
46
|
+
super(ssp, camera);
|
|
47
|
+
this.ssp = ssp;
|
|
48
|
+
this.resizeObserver = new ResizeObserver(() => {
|
|
49
|
+
this.resizeCamera();
|
|
50
|
+
});
|
|
51
|
+
this._zoom = 0.01;
|
|
52
|
+
camera.far = 200000;
|
|
53
|
+
}
|
|
54
|
+
resizeCamera() {
|
|
55
|
+
if (this._isActive) {
|
|
56
|
+
const { clientWidth: width, clientHeight: height, } = this.ssp.viewport.renderer.domElement;
|
|
57
|
+
const halfWidth = (width * this.zoom) / 2;
|
|
58
|
+
const halfHeight = (height * this.zoom) / 2;
|
|
59
|
+
this.nativeCamera.left = -halfWidth;
|
|
60
|
+
this.nativeCamera.right = halfWidth;
|
|
61
|
+
this.nativeCamera.top = halfHeight;
|
|
62
|
+
this.nativeCamera.bottom = -halfHeight;
|
|
63
|
+
this.nativeCamera.updateProjectionMatrix();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
onEnter() {
|
|
67
|
+
console.log('map camera enter');
|
|
68
|
+
if (!this.ssp.domElement)
|
|
69
|
+
return;
|
|
70
|
+
this.resizeObserver.observe(this.ssp.domElement);
|
|
71
|
+
this.resizeCamera();
|
|
72
|
+
}
|
|
73
|
+
onQuit() {
|
|
74
|
+
if (!this.ssp.domElement)
|
|
75
|
+
return;
|
|
76
|
+
this.resizeObserver.unobserve(this.ssp.domElement);
|
|
77
|
+
console.log('map camera quit');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class GyroListener {
|
|
82
|
+
constructor(x, y, z, absolute) {
|
|
83
|
+
this.gyroEvent = null;
|
|
84
|
+
this.onChange = () => { };
|
|
85
|
+
this._isActive = false;
|
|
86
|
+
this.defaultBeta = null;
|
|
87
|
+
this.defaultAlpha = null;
|
|
88
|
+
this.defaultGamma = null;
|
|
89
|
+
this.cache = null;
|
|
90
|
+
this.beta = null;
|
|
91
|
+
this.alpha = null;
|
|
92
|
+
this.gamma = null;
|
|
93
|
+
this.gyroX = x !== null && x !== void 0 ? x : true;
|
|
94
|
+
this.gyroY = y !== null && y !== void 0 ? y : true;
|
|
95
|
+
this.gyroZ = z !== null && z !== void 0 ? z : true;
|
|
96
|
+
this.absolute = absolute !== null && absolute !== void 0 ? absolute : true;
|
|
97
|
+
}
|
|
98
|
+
requestPermisson() {
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
var _a;
|
|
101
|
+
if (!window.DeviceOrientationEvent.requestPermission) {
|
|
102
|
+
resolve(true);
|
|
103
|
+
}
|
|
104
|
+
(_a = window.DeviceOrientationEvent) === null || _a === void 0 ? void 0 : _a.requestPermission().then((state) => {
|
|
105
|
+
if (state === 'granted') {
|
|
106
|
+
//允许
|
|
107
|
+
resolve(true);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
//拒绝
|
|
111
|
+
reject('用户拒绝使用陀螺仪权限');
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/** 监听陀螺仪事件 */
|
|
117
|
+
async active() {
|
|
118
|
+
await this.requestPermisson();
|
|
119
|
+
this._isActive = true;
|
|
120
|
+
if (this.gyroEvent) {
|
|
121
|
+
this.inActive();
|
|
122
|
+
}
|
|
123
|
+
this.gyroEvent = (event) => {
|
|
124
|
+
const { defaultAlpha, defaultBeta, defaultGamma, gyroX, gyroY, gyroZ, } = this;
|
|
125
|
+
const { alpha, beta, gamma, } = event;
|
|
126
|
+
const resultAlpha = gyroY ? alpha !== null && alpha !== void 0 ? alpha : defaultAlpha : defaultAlpha;
|
|
127
|
+
const resultBeta = gyroX ? beta !== null && beta !== void 0 ? beta : defaultBeta : defaultBeta;
|
|
128
|
+
const resultGamma = gyroZ ? gamma !== null && gamma !== void 0 ? gamma : defaultGamma : defaultGamma;
|
|
129
|
+
if (this.absolute) {
|
|
130
|
+
this.alpha = resultAlpha;
|
|
131
|
+
this.beta = resultBeta;
|
|
132
|
+
this.gamma = resultGamma;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
// 计算差值
|
|
136
|
+
if (!this.cache) {
|
|
137
|
+
this.cache = {
|
|
138
|
+
alpha: resultAlpha,
|
|
139
|
+
beta: resultBeta,
|
|
140
|
+
gamma: resultGamma,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
const { alpha, beta, gamma, } = this.cache;
|
|
145
|
+
this.alpha = resultAlpha === null ? null : alpha === null ? resultAlpha : resultAlpha - alpha;
|
|
146
|
+
this.beta = resultBeta === null ? null : beta === null ? resultBeta : resultBeta - beta;
|
|
147
|
+
this.gamma = resultGamma === null ? null : gamma === null ? resultGamma : resultGamma - gamma;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
this.onChange(this.alpha, this.beta, this.gamma);
|
|
151
|
+
};
|
|
152
|
+
window.addEventListener('deviceorientation', this.gyroEvent);
|
|
153
|
+
}
|
|
154
|
+
/** 销毁陀螺仪事件 */
|
|
155
|
+
inActive() {
|
|
156
|
+
this._isActive = false;
|
|
157
|
+
if (!this.gyroEvent)
|
|
158
|
+
return;
|
|
159
|
+
window.removeEventListener('deviceorientation', this.gyroEvent);
|
|
160
|
+
this.gyroEvent = null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
var NAVIGATE_VISION_TYPE;
|
|
165
|
+
(function (NAVIGATE_VISION_TYPE) {
|
|
166
|
+
NAVIGATE_VISION_TYPE["FIRST_VISION"] = "first";
|
|
167
|
+
NAVIGATE_VISION_TYPE["THIRD_VISION"] = "third";
|
|
168
|
+
NAVIGATE_VISION_TYPE["UP_VISION"] = "up";
|
|
169
|
+
NAVIGATE_VISION_TYPE["LEFT_VISION"] = "left";
|
|
170
|
+
})(NAVIGATE_VISION_TYPE || (NAVIGATE_VISION_TYPE = {}));
|
|
171
|
+
var NAVIGATE_ORIENTATION_TYPE;
|
|
172
|
+
(function (NAVIGATE_ORIENTATION_TYPE) {
|
|
173
|
+
NAVIGATE_ORIENTATION_TYPE["RELATIVE_ORIENTATION"] = "relative";
|
|
174
|
+
NAVIGATE_ORIENTATION_TYPE["FIXED_ORIENTATION"] = "fixed";
|
|
175
|
+
NAVIGATE_ORIENTATION_TYPE["GYRO_ORIENTATION"] = "gyro";
|
|
176
|
+
})(NAVIGATE_ORIENTATION_TYPE || (NAVIGATE_ORIENTATION_TYPE = {}));
|
|
177
|
+
|
|
178
|
+
function Vector3ToEuler(v) {
|
|
179
|
+
const matrix = new Matrix4();
|
|
180
|
+
const quaternion = new Quaternion();
|
|
181
|
+
const euler = new Euler();
|
|
182
|
+
// 将方向向量转换为旋转矩阵
|
|
183
|
+
matrix.lookAt(new Vector3(), v, new Vector3(0, 1, 0));
|
|
184
|
+
// 将旋转矩阵转换为四元数
|
|
185
|
+
quaternion.setFromRotationMatrix(matrix);
|
|
186
|
+
// 将四元数转换为欧拉角
|
|
187
|
+
euler.setFromQuaternion(quaternion);
|
|
188
|
+
return euler;
|
|
189
|
+
}
|
|
190
|
+
function eulerToVector3(e) {
|
|
191
|
+
return new Vector3(0, 0, -1).applyQuaternion(new Quaternion().setFromEuler(e));
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* 欧拉角归一化
|
|
195
|
+
* @param e 欧拉角
|
|
196
|
+
*/
|
|
197
|
+
function normalizeEuler(e) {
|
|
198
|
+
return eulerToVector3(e).normalize();
|
|
199
|
+
}
|
|
200
|
+
function getFixVector(v, target) {
|
|
201
|
+
return typeof v === 'function' ? v(target) : v;
|
|
202
|
+
}
|
|
203
|
+
function getFixEuler(e, target) {
|
|
204
|
+
return typeof e === 'function' ? e(target) : e;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const DEFAULT_NAVIGATE_CAMERA_OPTIONS = Object.freeze({
|
|
208
|
+
disabledAnimate: false,
|
|
209
|
+
fixedOrientation: false,
|
|
210
|
+
autoRestoreOrientation: 1500,
|
|
211
|
+
oppositeCamera: false,
|
|
212
|
+
oppositeType: {
|
|
213
|
+
x: false,
|
|
214
|
+
y: true,
|
|
215
|
+
z: false,
|
|
216
|
+
},
|
|
217
|
+
orientationTarget: 'main',
|
|
218
|
+
vision: NAVIGATE_VISION_TYPE.THIRD_VISION,
|
|
219
|
+
orientationType: NAVIGATE_ORIENTATION_TYPE.RELATIVE_ORIENTATION,
|
|
220
|
+
rotationToTarget: new Vector3(0, 4, -10),
|
|
221
|
+
distanceToTarget: 1,
|
|
222
|
+
targetRotationFix: [0, 0, 0, 'XYZ'],
|
|
223
|
+
isFixRotationRelativeTarget: true,
|
|
224
|
+
targetPositionFix: [0, 2, 0],
|
|
225
|
+
isFixPositionRelativeTarget: false,
|
|
226
|
+
enableGyro: false,
|
|
227
|
+
gyroX: false,
|
|
228
|
+
gyroY: true,
|
|
229
|
+
gyroZ: false,
|
|
230
|
+
gyroAbsolute: true,
|
|
231
|
+
onControlStart: null,
|
|
232
|
+
onControlRender: null,
|
|
233
|
+
onControlEnd: null,
|
|
234
|
+
});
|
|
235
|
+
function _applyEulerFix(fix) {
|
|
236
|
+
const quaternion = new Quaternion().setFromEuler(new Euler().fromArray(fix));
|
|
237
|
+
this.quaternion.multiply(quaternion);
|
|
238
|
+
}
|
|
239
|
+
/** 导航相机 */
|
|
240
|
+
class NavigateCamera extends SoonSpaceCamera {
|
|
241
|
+
get position() {
|
|
242
|
+
return this.nativeCamera.position;
|
|
243
|
+
}
|
|
244
|
+
get rotation() {
|
|
245
|
+
return this.nativeCamera.rotation;
|
|
246
|
+
}
|
|
247
|
+
get quaternion() {
|
|
248
|
+
return this.nativeCamera.quaternion;
|
|
249
|
+
}
|
|
250
|
+
_lookAt(...args) {
|
|
251
|
+
this.nativeCamera.lookAt(...args);
|
|
252
|
+
if (!this.isFixRotationRelativeTarget) {
|
|
253
|
+
_applyEulerFix.call(this.nativeCamera, this.targetRotationAfterFix);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/** 开启 */
|
|
257
|
+
get enableGyro() {
|
|
258
|
+
return this._enableGyro;
|
|
259
|
+
}
|
|
260
|
+
set enableGyro(value) {
|
|
261
|
+
this._enableGyro = value;
|
|
262
|
+
value ? this._gyroListener.active() : this._gyroListener.inActive();
|
|
263
|
+
}
|
|
264
|
+
/** 是否应用陀螺仪 X 轴,默认不启动 */
|
|
265
|
+
get gyroX() {
|
|
266
|
+
return this._gyroListener.gyroX;
|
|
267
|
+
}
|
|
268
|
+
set gyroX(value) {
|
|
269
|
+
this._gyroListener.gyroX = value;
|
|
270
|
+
}
|
|
271
|
+
/** 是否应用陀螺仪 Y 轴,默认启动 */
|
|
272
|
+
get gyroY() {
|
|
273
|
+
return this._gyroListener.gyroY;
|
|
274
|
+
}
|
|
275
|
+
set gyroY(value) {
|
|
276
|
+
this._gyroListener.gyroY = value;
|
|
277
|
+
}
|
|
278
|
+
/** 是否应用陀螺仪 Z 轴,默认不启动 */
|
|
279
|
+
get gyroZ() {
|
|
280
|
+
return this._gyroListener.gyroZ;
|
|
281
|
+
}
|
|
282
|
+
set gyroZ(value) {
|
|
283
|
+
this._gyroListener.gyroZ = value;
|
|
284
|
+
}
|
|
285
|
+
/** 陀螺仪是否使用绝对值,默认使用 */
|
|
286
|
+
get gyroAbsolute() {
|
|
287
|
+
return this._gyroListener.absolute;
|
|
288
|
+
}
|
|
289
|
+
set gyroAbsolute(value) {
|
|
290
|
+
this._gyroListener.absolute = value;
|
|
291
|
+
}
|
|
292
|
+
get targetRotationAfterFix() {
|
|
293
|
+
return getFixEuler(this.targetRotationFix);
|
|
294
|
+
}
|
|
295
|
+
get targetPositionAfterFix() {
|
|
296
|
+
return getFixVector(this.targetPositionFix);
|
|
297
|
+
}
|
|
298
|
+
get realDistanceToTarget() {
|
|
299
|
+
if (this.vision === 'first')
|
|
300
|
+
return 1;
|
|
301
|
+
else
|
|
302
|
+
return this.distanceToTarget;
|
|
303
|
+
}
|
|
304
|
+
get realRotationToTarget() {
|
|
305
|
+
switch (this.vision) {
|
|
306
|
+
case 'first':
|
|
307
|
+
return new Vector3(0, 0, -1);
|
|
308
|
+
case 'up':
|
|
309
|
+
return new Vector3(0, 1, -0.01);
|
|
310
|
+
case 'left':
|
|
311
|
+
return new Vector3(1, 0, 0);
|
|
312
|
+
default:
|
|
313
|
+
return this.rotationToTarget;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* 相机朝向类型
|
|
318
|
+
*
|
|
319
|
+
* NAVIGATE_ORIENTATION_TYPE.RELATIVE_ORIENTATION 相对朝向
|
|
320
|
+
*
|
|
321
|
+
* NAVIGATE_ORIENTATION_TYPE.FIXED_ORIENTATION 绝对朝向
|
|
322
|
+
*
|
|
323
|
+
* NAVIGATE_ORIENTATION_TYPE.GYRO_ORIENTATION 陀螺仪朝向
|
|
324
|
+
*/
|
|
325
|
+
get orientationType() {
|
|
326
|
+
return this._orientationType;
|
|
327
|
+
}
|
|
328
|
+
set orientationType(value) {
|
|
329
|
+
this._orientationType = value;
|
|
330
|
+
if (value === NAVIGATE_ORIENTATION_TYPE.GYRO_ORIENTATION) {
|
|
331
|
+
this._gyroListener.active();
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
this._gyroListener.inActive();
|
|
335
|
+
}
|
|
336
|
+
if (value === NAVIGATE_ORIENTATION_TYPE.FIXED_ORIENTATION) {
|
|
337
|
+
this._fixedOrientationRotation.copy(this.followRotation);
|
|
338
|
+
}
|
|
339
|
+
this.restoreOrientation();
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* 相机朝向目标
|
|
343
|
+
*
|
|
344
|
+
* “main” 朝向跟踪目标
|
|
345
|
+
*
|
|
346
|
+
* Vector3 朝向点
|
|
347
|
+
*
|
|
348
|
+
* Object3D 朝向目标对象
|
|
349
|
+
*
|
|
350
|
+
* Euler 固定朝向
|
|
351
|
+
*/
|
|
352
|
+
get orientationTarget() {
|
|
353
|
+
return this._orientationTarget;
|
|
354
|
+
}
|
|
355
|
+
set orientationTarget(value) {
|
|
356
|
+
this._orientationTarget = value;
|
|
357
|
+
this.restoreOrientation();
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* 视角配置
|
|
361
|
+
*
|
|
362
|
+
* NAVIGATE_VISION_TYPE.FIRST_VISION 第一人称
|
|
363
|
+
*
|
|
364
|
+
* NAVIGATE_VISION_TYPE.THIRD_VISION 第三人称
|
|
365
|
+
*
|
|
366
|
+
* NAVIGATE_VISION_TYPE.UP_VISION 俯视图
|
|
367
|
+
*
|
|
368
|
+
* NAVIGATE_VISION_TYPE.LEFT_VISION 左视图
|
|
369
|
+
*
|
|
370
|
+
* 默认为第三人称
|
|
371
|
+
*/
|
|
372
|
+
get vision() {
|
|
373
|
+
return this._vision;
|
|
374
|
+
}
|
|
375
|
+
set vision(value) {
|
|
376
|
+
this._vision = value;
|
|
377
|
+
// 第一视角时,隐藏跟随目标
|
|
378
|
+
this.followTarget.visible = value !== NAVIGATE_VISION_TYPE.FIRST_VISION;
|
|
379
|
+
this.restoreOrientation();
|
|
380
|
+
}
|
|
381
|
+
/** 相机跟随目标 */
|
|
382
|
+
get followTarget() {
|
|
383
|
+
return this._followTarget;
|
|
384
|
+
}
|
|
385
|
+
set followTarget(value) {
|
|
386
|
+
this._followTarget = value;
|
|
387
|
+
this.restoreOrientation();
|
|
388
|
+
}
|
|
389
|
+
/** 相机跟踪点(相对跟随目标以及偏移参数获取的点) */
|
|
390
|
+
get followPosition() {
|
|
391
|
+
const fixVector = new Vector3();
|
|
392
|
+
fixVector.fromArray(this.targetPositionAfterFix);
|
|
393
|
+
if (this.isFixPositionRelativeTarget) {
|
|
394
|
+
fixVector.applyEuler(this.followRotation);
|
|
395
|
+
}
|
|
396
|
+
return this.followTarget.position.clone().add(fixVector);
|
|
397
|
+
}
|
|
398
|
+
/** 跟踪目标的方向(相对跟随目标以及偏移参数获取的朝向) */
|
|
399
|
+
get followRotation() {
|
|
400
|
+
const rotation = this.followTarget.rotation;
|
|
401
|
+
if (this.isFixRotationRelativeTarget) {
|
|
402
|
+
const euler = new Euler().copy(rotation);
|
|
403
|
+
const q1 = new Quaternion().setFromEuler(euler);
|
|
404
|
+
const q2 = new Quaternion().setFromEuler(new Euler().fromArray(this.targetRotationAfterFix));
|
|
405
|
+
q1.multiply(q2);
|
|
406
|
+
return new Euler().setFromQuaternion(q1);
|
|
407
|
+
}
|
|
408
|
+
return this.followTarget.rotation;
|
|
409
|
+
}
|
|
410
|
+
/** 固定朝向,朝向不受控制器影响 */
|
|
411
|
+
get fixedOrientation() {
|
|
412
|
+
return this._fixedOrientation;
|
|
413
|
+
}
|
|
414
|
+
set fixedOrientation(value) {
|
|
415
|
+
this._fixedOrientation = value;
|
|
416
|
+
// 恢复正常朝向
|
|
417
|
+
if (!value) {
|
|
418
|
+
this.restoreOrientation();
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
constructor(ssp, camera, options) {
|
|
422
|
+
const thisCamera = camera !== null && camera !== void 0 ? camera : new PerspectiveCamera(80, 1, 0.1, 100);
|
|
423
|
+
super(ssp, thisCamera);
|
|
424
|
+
this.ssp = ssp;
|
|
425
|
+
this.initOptions = DEFAULT_NAVIGATE_CAMERA_OPTIONS;
|
|
426
|
+
this.cameraKey = 'navigatorCamera' + ~~(Math.random() * 10000);
|
|
427
|
+
this._events = {
|
|
428
|
+
onRender: () => { },
|
|
429
|
+
onMouseDown: (event) => { },
|
|
430
|
+
};
|
|
431
|
+
this._restoreTimer = void 0;
|
|
432
|
+
this._positionTween = null;
|
|
433
|
+
this.autoFollowRender = true;
|
|
434
|
+
/** 相机位置记录用的对象,用于恢复相机位置和朝向使用 */
|
|
435
|
+
this._cameraPlaceholder = new Object3D();
|
|
436
|
+
/** 取消飞向动画 */
|
|
437
|
+
this.disabledAnimate = DEFAULT_NAVIGATE_CAMERA_OPTIONS.disabledAnimate;
|
|
438
|
+
/** 第三人称相机距离跟踪目标距离 */
|
|
439
|
+
this.distanceToTarget = DEFAULT_NAVIGATE_CAMERA_OPTIONS.distanceToTarget;
|
|
440
|
+
/** 第三人称相机相对模型视角 */
|
|
441
|
+
this.rotationToTarget = DEFAULT_NAVIGATE_CAMERA_OPTIONS.rotationToTarget;
|
|
442
|
+
/** 相机反转 */
|
|
443
|
+
this.oppositeCamera = DEFAULT_NAVIGATE_CAMERA_OPTIONS.oppositeCamera;
|
|
444
|
+
/** 相机反转类型, 默认为 Y 轴对称 */
|
|
445
|
+
this.oppositeType = DEFAULT_NAVIGATE_CAMERA_OPTIONS.oppositeType;
|
|
446
|
+
/** 是否相对模型修正角度 */
|
|
447
|
+
this.isFixRotationRelativeTarget = DEFAULT_NAVIGATE_CAMERA_OPTIONS.isFixRotationRelativeTarget;
|
|
448
|
+
/**
|
|
449
|
+
* 目标角度修正
|
|
450
|
+
*
|
|
451
|
+
* 支持传入 getter 函数
|
|
452
|
+
*/
|
|
453
|
+
this.targetRotationFix = DEFAULT_NAVIGATE_CAMERA_OPTIONS.targetRotationFix;
|
|
454
|
+
/** 是否相对模型修正偏移 */
|
|
455
|
+
this.isFixPositionRelativeTarget = DEFAULT_NAVIGATE_CAMERA_OPTIONS.isFixPositionRelativeTarget;
|
|
456
|
+
/**
|
|
457
|
+
* 目标位置修正
|
|
458
|
+
*
|
|
459
|
+
* 支持传入 getter 函数
|
|
460
|
+
*/
|
|
461
|
+
this.targetPositionFix = DEFAULT_NAVIGATE_CAMERA_OPTIONS.targetPositionFix;
|
|
462
|
+
/**
|
|
463
|
+
* 自动恢复朝向
|
|
464
|
+
*
|
|
465
|
+
* boolean 是否恢复朝向
|
|
466
|
+
*
|
|
467
|
+
* number 间隔多少秒后恢复朝向
|
|
468
|
+
*/
|
|
469
|
+
this.autoRestoreOrientation = DEFAULT_NAVIGATE_CAMERA_OPTIONS.autoRestoreOrientation;
|
|
470
|
+
/** 当控制器控制相机时允许外部自定义控制,控制开始时触发 */
|
|
471
|
+
this.onControlStart = null;
|
|
472
|
+
/** 当控制器控制相机时允许外部自定义控制,控制进行时回调 */
|
|
473
|
+
this.onControlRender = null;
|
|
474
|
+
/** 当控制器控制相机时允许外部自定义控制,控制结束时回调 */
|
|
475
|
+
this.onControlEnd = null;
|
|
476
|
+
this._enableGyro = false;
|
|
477
|
+
/** 陀螺仪的旋转欧拉角 */
|
|
478
|
+
this._gyroRotation = new Euler();
|
|
479
|
+
this._orientationType = DEFAULT_NAVIGATE_CAMERA_OPTIONS.orientationType;
|
|
480
|
+
/** 绝对朝向的缓存值 */
|
|
481
|
+
this._fixedOrientationRotation = new Euler();
|
|
482
|
+
this._orientationTarget = DEFAULT_NAVIGATE_CAMERA_OPTIONS.orientationTarget;
|
|
483
|
+
this._vision = DEFAULT_NAVIGATE_CAMERA_OPTIONS.vision;
|
|
484
|
+
this._followTarget = new Object3D();
|
|
485
|
+
this._fixedOrientation = DEFAULT_NAVIGATE_CAMERA_OPTIONS.fixedOrientation;
|
|
486
|
+
this.initGyroListener();
|
|
487
|
+
const initOptions = Object.freeze(Object.assign(Object.assign({}, DEFAULT_NAVIGATE_CAMERA_OPTIONS), (options !== null && options !== void 0 ? options : {})));
|
|
488
|
+
this.initOptions = initOptions;
|
|
489
|
+
this.setOptions(initOptions);
|
|
490
|
+
this.setCamera(thisCamera);
|
|
491
|
+
}
|
|
492
|
+
initGyroListener() {
|
|
493
|
+
const initOptions = this.initOptions;
|
|
494
|
+
this._gyroListener = new GyroListener(initOptions.gyroX, initOptions.gyroY, initOptions.gyroZ, initOptions.gyroAbsolute);
|
|
495
|
+
this._gyroListener.onChange = (a, b, g) => {
|
|
496
|
+
const adjustedAlpha = a === null ? 0 : MathUtils.degToRad(a);
|
|
497
|
+
const adjustedBeta = b === null ? 0 : MathUtils.degToRad(b);
|
|
498
|
+
const adjustedGamma = g === null ? 0 : MathUtils.degToRad(g);
|
|
499
|
+
this._gyroRotation = new Euler(adjustedBeta, adjustedAlpha, adjustedGamma);
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
/** 控制器操作时处理 */
|
|
503
|
+
setCameraOrientationOnControl() {
|
|
504
|
+
const { rotation, } = this.ssp.viewport.cameraManager.mainCamera;
|
|
505
|
+
const { realDistanceToTarget, followPosition, } = this;
|
|
506
|
+
// 旋转后方向不变,位置变
|
|
507
|
+
// 欧拉角转方向向量归一化
|
|
508
|
+
const normallizeRotation = normalizeEuler(rotation);
|
|
509
|
+
this.position.copy(followPosition
|
|
510
|
+
.clone()
|
|
511
|
+
.add(normallizeRotation.multiplyScalar(-realDistanceToTarget)));
|
|
512
|
+
this._lookAt(this.followPosition);
|
|
513
|
+
}
|
|
514
|
+
/** 更新相机占位符的方位 */
|
|
515
|
+
updateCameraPlaceholder() {
|
|
516
|
+
const position = this.getRenderCameraPosition();
|
|
517
|
+
this._cameraPlaceholder.position.copy(position);
|
|
518
|
+
}
|
|
519
|
+
/** 设置相机的方位 */
|
|
520
|
+
setCameraOrientationOnRender() {
|
|
521
|
+
this.position.copy(this._cameraPlaceholder.position);
|
|
522
|
+
this._lookAt(this.followPosition);
|
|
523
|
+
}
|
|
524
|
+
/** 获取相机点相对跟踪点的欧拉角 */
|
|
525
|
+
getRenderCameraOrientation() {
|
|
526
|
+
const { _fixedOrientationRotation, _orientationType, _orientationTarget, } = this;
|
|
527
|
+
const euler = new Euler();
|
|
528
|
+
// 陀螺仪朝向只有一种朝向
|
|
529
|
+
if (_orientationType === NAVIGATE_ORIENTATION_TYPE.GYRO_ORIENTATION) {
|
|
530
|
+
euler.copy(this._gyroRotation);
|
|
531
|
+
}
|
|
532
|
+
switch (true) {
|
|
533
|
+
case _orientationTarget === 'main': {
|
|
534
|
+
euler.copy(_orientationType === NAVIGATE_ORIENTATION_TYPE.FIXED_ORIENTATION ?
|
|
535
|
+
_fixedOrientationRotation :
|
|
536
|
+
this.followRotation);
|
|
537
|
+
break;
|
|
538
|
+
}
|
|
539
|
+
case _orientationTarget instanceof Vector3: {
|
|
540
|
+
const rotation = this.orientationTarget
|
|
541
|
+
.clone()
|
|
542
|
+
.sub(this.followPosition);
|
|
543
|
+
euler.copy(Vector3ToEuler(rotation));
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
case _orientationTarget instanceof Object3D: {
|
|
547
|
+
const obj = _orientationTarget;
|
|
548
|
+
switch (_orientationType) {
|
|
549
|
+
case NAVIGATE_ORIENTATION_TYPE.RELATIVE_ORIENTATION:
|
|
550
|
+
euler.copy(obj.rotation);
|
|
551
|
+
break;
|
|
552
|
+
case NAVIGATE_ORIENTATION_TYPE.FIXED_ORIENTATION:
|
|
553
|
+
euler.copy(Vector3ToEuler(obj.position.clone().sub(this.followPosition)));
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
case _orientationTarget instanceof Euler: {
|
|
559
|
+
// 相对朝向方向和固定朝向方向效果是相同的
|
|
560
|
+
euler.copy(_orientationTarget);
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return euler;
|
|
565
|
+
}
|
|
566
|
+
/** 获取相机落点(相对跟踪点的方向归一化 * 距离) */
|
|
567
|
+
getRenderCameraPosition() {
|
|
568
|
+
const position = new Vector3();
|
|
569
|
+
const rotation = this.getRenderCameraOrientation();
|
|
570
|
+
const { realDistanceToTarget, realRotationToTarget, } = this;
|
|
571
|
+
position.copy(this.followPosition
|
|
572
|
+
.clone()
|
|
573
|
+
.add(realRotationToTarget
|
|
574
|
+
.clone()
|
|
575
|
+
.applyEuler(rotation)
|
|
576
|
+
.normalize()
|
|
577
|
+
.multiplyScalar(realDistanceToTarget)));
|
|
578
|
+
if (this.oppositeCamera) {
|
|
579
|
+
const addVector = this.followPosition
|
|
580
|
+
.clone()
|
|
581
|
+
.sub(position)
|
|
582
|
+
.multiplyScalar(2);
|
|
583
|
+
const { x, y, z, } = this.oppositeType;
|
|
584
|
+
if ([x, y, z].includes(false) && [x, y, z].includes(true)) {
|
|
585
|
+
if (x) {
|
|
586
|
+
addVector.x = 0;
|
|
587
|
+
}
|
|
588
|
+
if (y) {
|
|
589
|
+
addVector.y = 0;
|
|
590
|
+
}
|
|
591
|
+
if (z) {
|
|
592
|
+
addVector.z = 0;
|
|
593
|
+
}
|
|
594
|
+
position.add(addVector);
|
|
595
|
+
}
|
|
596
|
+
else {
|
|
597
|
+
x && position.add(addVector);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
return position;
|
|
601
|
+
}
|
|
602
|
+
onRender() {
|
|
603
|
+
if (!this.followTarget)
|
|
604
|
+
return;
|
|
605
|
+
this.updateCameraPlaceholder();
|
|
606
|
+
if (this._positionTween)
|
|
607
|
+
return;
|
|
608
|
+
if (this.autoFollowRender || this.fixedOrientation) {
|
|
609
|
+
this.setCameraOrientationOnRender();
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
if (this.onControlRender) {
|
|
613
|
+
this.onControlRender(this.nativeCamera, this.followTarget);
|
|
614
|
+
}
|
|
615
|
+
else {
|
|
616
|
+
this.setCameraOrientationOnControl();
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
/** 初始化事件 */
|
|
620
|
+
initEvents() {
|
|
621
|
+
var _a;
|
|
622
|
+
const dom = this.ssp.viewport.renderer.domElement;
|
|
623
|
+
const mainCamera = this.ssp.viewport.cameraManager.mainCamera;
|
|
624
|
+
const onRender = this.onRender.bind(this);
|
|
625
|
+
const onMouseDown = (event) => {
|
|
626
|
+
var _a, _b;
|
|
627
|
+
const isControlling = this.autoFollowRender === false;
|
|
628
|
+
this.autoFollowRender = false;
|
|
629
|
+
(_a = this._positionTween) === null || _a === void 0 ? void 0 : _a.stop();
|
|
630
|
+
window.clearTimeout(this._restoreTimer);
|
|
631
|
+
document.removeEventListener('mouseup', onMouseUp);
|
|
632
|
+
document.removeEventListener('touchend', onMouseUp);
|
|
633
|
+
document.addEventListener('mouseup', onMouseUp, true);
|
|
634
|
+
document.addEventListener('touchend', onMouseUp, true);
|
|
635
|
+
if (isControlling)
|
|
636
|
+
return;
|
|
637
|
+
if (!this.isFixRotationRelativeTarget) {
|
|
638
|
+
const invertQuaternion = new Quaternion()
|
|
639
|
+
.setFromEuler(new Euler().fromArray(this.targetRotationAfterFix))
|
|
640
|
+
.invert();
|
|
641
|
+
mainCamera.quaternion.copy(new Quaternion().copy(this.quaternion).multiply(invertQuaternion));
|
|
642
|
+
}
|
|
643
|
+
else {
|
|
644
|
+
mainCamera.quaternion.copy(this.quaternion);
|
|
645
|
+
}
|
|
646
|
+
(_b = this.onControlStart) === null || _b === void 0 ? void 0 : _b.call(this, this.nativeCamera, this.followTarget, event);
|
|
647
|
+
};
|
|
648
|
+
const onMouseUp = (event) => {
|
|
649
|
+
const exec = () => {
|
|
650
|
+
var _a;
|
|
651
|
+
this.restoreOrientation();
|
|
652
|
+
this.autoFollowRender = true;
|
|
653
|
+
(_a = this.onControlEnd) === null || _a === void 0 ? void 0 : _a.call(this, this.nativeCamera, this.followTarget, event);
|
|
654
|
+
document.removeEventListener('mouseup', onMouseUp, true);
|
|
655
|
+
document.removeEventListener('touchend', onMouseUp, true);
|
|
656
|
+
};
|
|
657
|
+
if (typeof this.autoRestoreOrientation === 'number') {
|
|
658
|
+
window.clearTimeout(this._restoreTimer);
|
|
659
|
+
this._restoreTimer = setTimeout(exec, this.autoRestoreOrientation);
|
|
660
|
+
}
|
|
661
|
+
else if (this.autoRestoreOrientation) {
|
|
662
|
+
exec();
|
|
663
|
+
}
|
|
664
|
+
};
|
|
665
|
+
this._events.onRender = onRender;
|
|
666
|
+
this._events.onMouseDown = onMouseDown;
|
|
667
|
+
const signals = this.ssp.signals;
|
|
668
|
+
signals.beforeRender.add(onRender);
|
|
669
|
+
signals.mouseDown.add(onMouseDown);
|
|
670
|
+
// TODO dom 上的 touchstart 被 ssp 内部 preventDefault 了,临时只能取父节点注册事件
|
|
671
|
+
(_a = dom.parentElement) === null || _a === void 0 ? void 0 : _a.addEventListener('touchstart', onMouseDown, true);
|
|
672
|
+
}
|
|
673
|
+
/** 清除事件 */
|
|
674
|
+
clearEvents() {
|
|
675
|
+
var _a;
|
|
676
|
+
const { onRender, onMouseDown, } = this._events;
|
|
677
|
+
const signals = this.ssp.signals;
|
|
678
|
+
const dom = this.ssp.viewport.renderer.domElement;
|
|
679
|
+
signals.beforeRender.remove(onRender);
|
|
680
|
+
signals.mouseDown.remove(onMouseDown);
|
|
681
|
+
(_a = dom.parentElement) === null || _a === void 0 ? void 0 : _a.removeEventListener('touchstart', onMouseDown, true);
|
|
682
|
+
}
|
|
683
|
+
onEnter() {
|
|
684
|
+
console.log('onenter');
|
|
685
|
+
this.initEvents();
|
|
686
|
+
}
|
|
687
|
+
onQuit() {
|
|
688
|
+
console.log('onquit');
|
|
689
|
+
this.clearEvents();
|
|
690
|
+
this._gyroListener.inActive();
|
|
691
|
+
}
|
|
692
|
+
/** 参数重置 */
|
|
693
|
+
resetOptions() {
|
|
694
|
+
this.setOptions(Object.assign({}, this.initOptions));
|
|
695
|
+
}
|
|
696
|
+
/** 设置参数 */
|
|
697
|
+
setOptions(options) {
|
|
698
|
+
const animate = this.disabledAnimate;
|
|
699
|
+
this.disabledAnimate = true;
|
|
700
|
+
if (options.isFixPositionRelativeTarget !== void 0)
|
|
701
|
+
this.isFixPositionRelativeTarget = options.isFixPositionRelativeTarget;
|
|
702
|
+
if (options.targetPositionFix !== void 0)
|
|
703
|
+
this.targetPositionFix = options.targetPositionFix;
|
|
704
|
+
if (options.isFixRotationRelativeTarget !== void 0)
|
|
705
|
+
this.isFixRotationRelativeTarget = options.isFixRotationRelativeTarget;
|
|
706
|
+
if (options.targetRotationFix !== void 0)
|
|
707
|
+
this.targetRotationFix = options.targetRotationFix;
|
|
708
|
+
if (options.rotationToTarget !== void 0)
|
|
709
|
+
this.rotationToTarget = options.rotationToTarget;
|
|
710
|
+
if (options.distanceToTarget !== void 0)
|
|
711
|
+
this.distanceToTarget = options.distanceToTarget;
|
|
712
|
+
if (options.fixedOrientation !== void 0)
|
|
713
|
+
this.fixedOrientation = options.fixedOrientation;
|
|
714
|
+
if (options.oppositeCamera !== void 0)
|
|
715
|
+
this.oppositeCamera = options.oppositeCamera;
|
|
716
|
+
if (options.oppositeType !== void 0)
|
|
717
|
+
this.oppositeType = options.oppositeType;
|
|
718
|
+
if (options.orientationTarget !== void 0)
|
|
719
|
+
this.orientationTarget = options.orientationTarget;
|
|
720
|
+
if (options.orientationType !== void 0)
|
|
721
|
+
this.orientationType = options.orientationType;
|
|
722
|
+
if (options.vision !== void 0)
|
|
723
|
+
this.vision = options.vision;
|
|
724
|
+
if (options.autoRestoreOrientation !== void 0)
|
|
725
|
+
this.autoRestoreOrientation = options.autoRestoreOrientation;
|
|
726
|
+
if (options.gyroX !== void 0)
|
|
727
|
+
this.gyroX = options.gyroX;
|
|
728
|
+
if (options.gyroY !== void 0)
|
|
729
|
+
this.gyroY = options.gyroY;
|
|
730
|
+
if (options.gyroZ !== void 0)
|
|
731
|
+
this.gyroZ = options.gyroZ;
|
|
732
|
+
if (options.gyroAbsolute !== void 0)
|
|
733
|
+
this.gyroAbsolute = options.gyroAbsolute;
|
|
734
|
+
if (options.enableGyro !== void 0)
|
|
735
|
+
this.enableGyro = options.enableGyro;
|
|
736
|
+
if (options.onControlStart !== void 0)
|
|
737
|
+
this.onControlStart = options.onControlStart;
|
|
738
|
+
if (options.onControlRender !== void 0)
|
|
739
|
+
this.onControlRender = options.onControlRender;
|
|
740
|
+
if (options.onControlEnd !== void 0)
|
|
741
|
+
this.onControlEnd = options.onControlEnd;
|
|
742
|
+
this.disabledAnimate = animate;
|
|
743
|
+
if (options.disabledAnimate !== void 0)
|
|
744
|
+
this.disabledAnimate = options.disabledAnimate;
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* 设置相机,若导航相机正在使用中,则会立即切换为设置的相机
|
|
748
|
+
*/
|
|
749
|
+
setCamera(camera) {
|
|
750
|
+
if (this._isActive) {
|
|
751
|
+
this.ssp.viewport.cameraManager.removeCamera(this.cameraKey);
|
|
752
|
+
}
|
|
753
|
+
this.nativeCamera = camera;
|
|
754
|
+
this.ssp.viewport.cameraManager.cameras[this.cameraKey] = camera;
|
|
755
|
+
if (this._isActive) {
|
|
756
|
+
this.ssp.viewport.cameraManager.setCurrentCamera(camera);
|
|
757
|
+
}
|
|
758
|
+
return this;
|
|
759
|
+
}
|
|
760
|
+
/** 重置相机坐标 */
|
|
761
|
+
restoreOrientation() {
|
|
762
|
+
if (!this.followTarget)
|
|
763
|
+
return;
|
|
764
|
+
if (this.fixedOrientation)
|
|
765
|
+
return;
|
|
766
|
+
if (this._positionTween) {
|
|
767
|
+
this._positionTween.stop();
|
|
768
|
+
this._positionTween = null;
|
|
769
|
+
}
|
|
770
|
+
if (this.disabledAnimate) {
|
|
771
|
+
this.position.copy(this._cameraPlaceholder.position);
|
|
772
|
+
this._lookAt(this.followPosition);
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
this.ssp
|
|
776
|
+
.animation(this.position, this._cameraPlaceholder.position, { duration: 1000, }, (tp) => {
|
|
777
|
+
this._lookAt(this.followPosition);
|
|
778
|
+
}, (tween) => (this._positionTween = tween))
|
|
779
|
+
.finally(() => {
|
|
780
|
+
this._positionTween = null;
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
var PLAY_STATUS;
|
|
786
|
+
(function (PLAY_STATUS) {
|
|
787
|
+
PLAY_STATUS["PENDING"] = "pending";
|
|
788
|
+
PLAY_STATUS["STOP"] = "stop";
|
|
789
|
+
PLAY_STATUS["PLAYING"] = "playing";
|
|
790
|
+
PLAY_STATUS["PAUSE"] = "pause";
|
|
791
|
+
PLAY_STATUS["FINISHED"] = "finished";
|
|
792
|
+
})(PLAY_STATUS || (PLAY_STATUS = {}));
|
|
793
|
+
|
|
794
|
+
var zt = Object.defineProperty;
|
|
795
|
+
var Ut = (t, n, e) => n in t ? zt(t, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[n] = e;
|
|
796
|
+
var j = (t, n, e) => (Ut(t, typeof n != "symbol" ? n + "" : n, e), e);
|
|
797
|
+
function an(t, n) {
|
|
798
|
+
const e = Math.sqrt(t.lengthSq() * n.lengthSq());
|
|
799
|
+
if (e === 0)
|
|
800
|
+
return 0;
|
|
801
|
+
let o = t.dot(n) / e;
|
|
802
|
+
return o = Math.max(-1, Math.min(1, o)), Math.acos(o);
|
|
803
|
+
}
|
|
804
|
+
function J(t, n, e) {
|
|
805
|
+
let o = an(t, n);
|
|
806
|
+
return o === 0 ? o : t.clone().cross(n).dot(e) < 0 ? -o : o;
|
|
807
|
+
}
|
|
808
|
+
function fn(t, n, e) {
|
|
809
|
+
const o = n.clone().negate(), s = e.clone().projectOnPlane(n), c = t.clone().projectOnPlane(s), r = J(n, c, s), i = n.clone().cross(s), l = t.clone().projectOnPlane(i), u = J(n, l, i), a = t.clone().projectOnPlane(o), f = J(s, a, o);
|
|
810
|
+
return { yaw: r, pitch: u, roll: f };
|
|
811
|
+
}
|
|
812
|
+
const it = 180 / Math.PI, ut = {
|
|
813
|
+
yaw: [
|
|
814
|
+
{ name: "前", range: [-15, 15] },
|
|
815
|
+
{ name: "左", range: [15, 165] },
|
|
816
|
+
{ name: "右", range: [-165, -15] },
|
|
817
|
+
{ name: "后", range: [-180.1, -165] },
|
|
818
|
+
{ name: "后", range: [165, 180.1] }
|
|
819
|
+
],
|
|
820
|
+
pitch: [
|
|
821
|
+
{ name: "前", range: [-15, 15] },
|
|
822
|
+
{ name: "上", range: [15, 165] },
|
|
823
|
+
{ name: "下", range: [-165, -15] },
|
|
824
|
+
{ name: "后", range: [-180.1, -165] },
|
|
825
|
+
{ name: "后", range: [165, 180.1] }
|
|
826
|
+
],
|
|
827
|
+
roll: [
|
|
828
|
+
{ name: "上", range: [-15, 15] },
|
|
829
|
+
{ name: "左", range: [15, 165] },
|
|
830
|
+
{ name: "右", range: [-165, -15] },
|
|
831
|
+
{ name: "下", range: [-180.1, -165] },
|
|
832
|
+
{ name: "下", range: [165, 180.1] }
|
|
833
|
+
]
|
|
834
|
+
};
|
|
835
|
+
function pn(t) {
|
|
836
|
+
const n = {};
|
|
837
|
+
for (const [e, o] of Object.entries(t))
|
|
838
|
+
n[e] = Array.isArray(o) ? o : Object.entries(o).map(([s, c]) => ({ name: s, range: c }));
|
|
839
|
+
return n;
|
|
840
|
+
}
|
|
841
|
+
const lt = {
|
|
842
|
+
degrees: !0,
|
|
843
|
+
map: ut,
|
|
844
|
+
front: { x: 0, y: 0, z: 1 },
|
|
845
|
+
up: { x: 0, y: 1, z: 0 }
|
|
846
|
+
};
|
|
847
|
+
class It {
|
|
848
|
+
constructor(n) {
|
|
849
|
+
j(this, "_options");
|
|
850
|
+
j(this, "_listMap");
|
|
851
|
+
j(this, "_front");
|
|
852
|
+
j(this, "_up");
|
|
853
|
+
n && (this.options = n);
|
|
854
|
+
}
|
|
855
|
+
static get options() {
|
|
856
|
+
return this._options ?? (this.options = lt);
|
|
857
|
+
}
|
|
858
|
+
static set options(n) {
|
|
859
|
+
this._options = Object.assign({}, structuredClone(ut), n);
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* 默认选项
|
|
863
|
+
*/
|
|
864
|
+
get defaultOptions() {
|
|
865
|
+
return this.constructor.options;
|
|
866
|
+
}
|
|
867
|
+
get options() {
|
|
868
|
+
return this._options ?? (this.options = this.defaultOptions);
|
|
869
|
+
}
|
|
870
|
+
set options(n) {
|
|
871
|
+
this._options = Object.assign({}, structuredClone(this.defaultOptions), n), this._listMap = null, this._front = null, this._up = null;
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* 是否使用角度,而非弧度
|
|
875
|
+
*/
|
|
876
|
+
get degrees() {
|
|
877
|
+
return this.options.degrees ?? (this.options.degrees = this.defaultOptions.degrees ?? !0);
|
|
878
|
+
}
|
|
879
|
+
set degrees(n) {
|
|
880
|
+
this.options.degrees = n;
|
|
881
|
+
}
|
|
882
|
+
get map() {
|
|
883
|
+
return this.options.map || (this.map = this.defaultOptions.map), this.options.map;
|
|
884
|
+
}
|
|
885
|
+
set map(n) {
|
|
886
|
+
const e = structuredClone(this.defaultOptions.map), o = structuredClone(ut);
|
|
887
|
+
n = n ? {
|
|
888
|
+
yaw: n.yaw ?? e.yaw ?? o.yaw,
|
|
889
|
+
pitch: n.pitch ?? e.pitch ?? o.pitch,
|
|
890
|
+
roll: n.roll ?? e.roll ?? o.roll
|
|
891
|
+
} : e, this.options.map = n, this._listMap = null;
|
|
892
|
+
}
|
|
893
|
+
get listMap() {
|
|
894
|
+
return this._listMap ?? (this._listMap = pn(this.map ?? {}));
|
|
895
|
+
}
|
|
896
|
+
get front() {
|
|
897
|
+
return this._front || (this.front = this.options.front ?? this.defaultOptions.front ?? lt.front), this._front;
|
|
898
|
+
}
|
|
899
|
+
set front(n) {
|
|
900
|
+
this._front = new Vector3().copy(n);
|
|
901
|
+
}
|
|
902
|
+
get up() {
|
|
903
|
+
return this._up || (this.up = this.options.up ?? this.defaultOptions.up ?? lt.up), this._up;
|
|
904
|
+
}
|
|
905
|
+
set up(n) {
|
|
906
|
+
this._up = new Vector3().copy(n);
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* 计算方位
|
|
910
|
+
* @param target
|
|
911
|
+
* @param front
|
|
912
|
+
* @param up
|
|
913
|
+
*/
|
|
914
|
+
computeAzimuth(n, e, o) {
|
|
915
|
+
const s = new Vector3().copy(n), c = new Vector3().copy(e ?? this.front), r = new Vector3().copy(o ?? this.up);
|
|
916
|
+
let { yaw: i, pitch: l, roll: u } = fn(s, c, r);
|
|
917
|
+
return this.degrees && (i *= it, l *= it, u *= it), {
|
|
918
|
+
yaw: {
|
|
919
|
+
angle: i,
|
|
920
|
+
name: this.findAzimuthNames("yaw", i)
|
|
921
|
+
},
|
|
922
|
+
pitch: {
|
|
923
|
+
angle: l,
|
|
924
|
+
name: this.findAzimuthNames("pitch", l)
|
|
925
|
+
},
|
|
926
|
+
roll: {
|
|
927
|
+
angle: u,
|
|
928
|
+
name: this.findAzimuthNames("roll", u)
|
|
929
|
+
}
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* 查找匹配的方位名字
|
|
934
|
+
* @param type - 类型
|
|
935
|
+
* @param angle - 角度
|
|
936
|
+
* @returns 匹配的名字列表
|
|
937
|
+
*/
|
|
938
|
+
findAzimuthNames(n, e) {
|
|
939
|
+
const o = [], s = this.listMap[n];
|
|
940
|
+
if (!s)
|
|
941
|
+
return o;
|
|
942
|
+
for (const { name: c, range: [r, i] } of s)
|
|
943
|
+
(e >= r && e < i || e <= r && e > i) && o.push(c);
|
|
944
|
+
return o;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
j(It, "_options");
|
|
948
|
+
const Xn = new It();
|
|
949
|
+
new Matrix3();
|
|
950
|
+
|
|
951
|
+
function getDirection(target, front) {
|
|
952
|
+
Xn.map = {
|
|
953
|
+
yaw: {
|
|
954
|
+
left: [20, 170],
|
|
955
|
+
right: [-170, -20],
|
|
956
|
+
},
|
|
957
|
+
pitch: {
|
|
958
|
+
up: [20, 170],
|
|
959
|
+
down: [-170, -20],
|
|
960
|
+
},
|
|
961
|
+
};
|
|
962
|
+
const { yaw, pitch, } = Xn.computeAzimuth(target, front);
|
|
963
|
+
const yawName = yaw.name;
|
|
964
|
+
const pitchName = pitch.name;
|
|
965
|
+
if (!yawName || !pitchName)
|
|
966
|
+
return 'straight';
|
|
967
|
+
if (!pitchName)
|
|
968
|
+
return yawName;
|
|
969
|
+
if (!yawName)
|
|
970
|
+
return pitchName;
|
|
971
|
+
return `${yawName}-${pitchName}`;
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* 路径反转
|
|
975
|
+
* @param ssp
|
|
976
|
+
* @param topologies 路径数组
|
|
977
|
+
* @returns
|
|
978
|
+
*/
|
|
979
|
+
function reverseTopologies(ssp, topologies) {
|
|
980
|
+
return topologies.reverse().map((item) => {
|
|
981
|
+
ssp.removeObjectById(item.sid);
|
|
982
|
+
const topologyNodes = item.nodes.reverse().map((node) => ({
|
|
983
|
+
id: node.sid,
|
|
984
|
+
position: node.position,
|
|
985
|
+
graphs: node.graphs,
|
|
986
|
+
}));
|
|
987
|
+
return ssp.createTopology(Object.assign(Object.assign({}, item.info), { nodes: topologyNodes }));
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
function getDirectionByNodes(p1, p2, p3) {
|
|
992
|
+
if (!p2 || !p3) {
|
|
993
|
+
return 'straight';
|
|
994
|
+
}
|
|
995
|
+
const target = p2.position.clone().sub(p1.position);
|
|
996
|
+
const front = p3.position.clone().sub(p2.position);
|
|
997
|
+
return getDirection(target, front);
|
|
998
|
+
}
|
|
999
|
+
/** 分析并返回所有节点信息 */
|
|
1000
|
+
function getNavigatorNodeInfosByTopology(topology) {
|
|
1001
|
+
const nodes = topology.nodes;
|
|
1002
|
+
const totalLength = topology.getLength();
|
|
1003
|
+
let countLength = 0;
|
|
1004
|
+
let countProgress = 0;
|
|
1005
|
+
return nodes.reduce((acc, cur, index) => {
|
|
1006
|
+
const secondNode = nodes[index + 1];
|
|
1007
|
+
const thirdNode = nodes[index + 2];
|
|
1008
|
+
const distanceToNext = secondNode ?
|
|
1009
|
+
secondNode.position.clone().distanceTo(cur.position) :
|
|
1010
|
+
0;
|
|
1011
|
+
const frontDirection = getDirectionByNodes(cur, secondNode, thirdNode);
|
|
1012
|
+
const distanceEnd = countLength + distanceToNext;
|
|
1013
|
+
const progressEnd = distanceEnd / totalLength;
|
|
1014
|
+
const result = {
|
|
1015
|
+
progressArea: [countProgress, progressEnd],
|
|
1016
|
+
distanceArea: [countLength, distanceEnd],
|
|
1017
|
+
node: cur,
|
|
1018
|
+
nextNode: secondNode,
|
|
1019
|
+
distanceToStart: countLength,
|
|
1020
|
+
distanceToEnd: totalLength - countLength,
|
|
1021
|
+
distanceToNext,
|
|
1022
|
+
frontDirection,
|
|
1023
|
+
};
|
|
1024
|
+
countLength = distanceEnd;
|
|
1025
|
+
countProgress = progressEnd;
|
|
1026
|
+
acc.push(result);
|
|
1027
|
+
return acc;
|
|
1028
|
+
}, []);
|
|
1029
|
+
}
|
|
1030
|
+
/** 根据进度获取当前节点信息 */
|
|
1031
|
+
function getNavigatorNodeInfoByProgress(progress, infos) {
|
|
1032
|
+
if (progress === 0)
|
|
1033
|
+
return infos[0];
|
|
1034
|
+
if (progress === 100)
|
|
1035
|
+
return infos[infos.length - 1];
|
|
1036
|
+
return infos.find((info) => {
|
|
1037
|
+
const [start, end] = info.progressArea;
|
|
1038
|
+
return progress >= start && progress < end;
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
/** 获取当前位置距离信息 */
|
|
1042
|
+
function getDistanceInfoByProgress(progress, info) {
|
|
1043
|
+
if (!info)
|
|
1044
|
+
return {
|
|
1045
|
+
distanceToStartNode: 0,
|
|
1046
|
+
distanceToEndNode: 0,
|
|
1047
|
+
distanceToStart: 0,
|
|
1048
|
+
distanceToEnd: 0,
|
|
1049
|
+
};
|
|
1050
|
+
const [startProgress, endProgress] = info.progressArea;
|
|
1051
|
+
const { distanceToNext, distanceToEnd, distanceToStart, } = info;
|
|
1052
|
+
const progressInArea = (progress - startProgress) / (endProgress - startProgress);
|
|
1053
|
+
const distanceToStartNode = distanceToNext * progressInArea;
|
|
1054
|
+
const distanceToEndNode = distanceToNext * (1 - progressInArea);
|
|
1055
|
+
return {
|
|
1056
|
+
distanceToStartNode,
|
|
1057
|
+
distanceToEndNode,
|
|
1058
|
+
distanceToStart: distanceToStartNode + distanceToStart,
|
|
1059
|
+
distanceToEnd: distanceToEnd - distanceToEndNode,
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
class Navigator {
|
|
1064
|
+
get model() {
|
|
1065
|
+
return this._model;
|
|
1066
|
+
}
|
|
1067
|
+
set model(obj) {
|
|
1068
|
+
this._model = obj;
|
|
1069
|
+
}
|
|
1070
|
+
/** 播放速度 */
|
|
1071
|
+
get speed() {
|
|
1072
|
+
return this._speed;
|
|
1073
|
+
}
|
|
1074
|
+
set speed(value) {
|
|
1075
|
+
this._speed = value;
|
|
1076
|
+
}
|
|
1077
|
+
/** 当前段进度 */
|
|
1078
|
+
get pathProgress() {
|
|
1079
|
+
return this._pathProgress;
|
|
1080
|
+
}
|
|
1081
|
+
set pathProgress(value) {
|
|
1082
|
+
this._pathProgress = value;
|
|
1083
|
+
}
|
|
1084
|
+
/** 总进度 */
|
|
1085
|
+
get totalProgress() {
|
|
1086
|
+
return this._pathProgress;
|
|
1087
|
+
}
|
|
1088
|
+
set totalProgress(value) {
|
|
1089
|
+
this._pathProgress = value;
|
|
1090
|
+
}
|
|
1091
|
+
get currentPath() {
|
|
1092
|
+
return this.paths[this.currentPathIndex];
|
|
1093
|
+
}
|
|
1094
|
+
/** 对外只读 当前路径索引 */
|
|
1095
|
+
get currentPathIndex() {
|
|
1096
|
+
return this._currentPathIndex;
|
|
1097
|
+
}
|
|
1098
|
+
/** 对外只读 当前朝向方向向量 */
|
|
1099
|
+
get currentRotationVector() {
|
|
1100
|
+
if (!this._nextNode || !this._currentNode)
|
|
1101
|
+
return new Vector3();
|
|
1102
|
+
return this._nextNode.clone().sub(this._currentNode);
|
|
1103
|
+
}
|
|
1104
|
+
constructor(ssp, paths) {
|
|
1105
|
+
this.ssp = ssp;
|
|
1106
|
+
this.paths = paths;
|
|
1107
|
+
this.onPlay = () => { };
|
|
1108
|
+
this.onStatusChange = () => { };
|
|
1109
|
+
/** 是否循环播放,默认为否 */
|
|
1110
|
+
this.loop = false;
|
|
1111
|
+
/** 是否自动开始下一段路径播放,默认为是 */
|
|
1112
|
+
this.autoNext = true;
|
|
1113
|
+
/** 开始或者重新开始后自动播放路径动画,默认为否 */
|
|
1114
|
+
this.playAfterStart = false;
|
|
1115
|
+
/** 开始时飞向模型,默认为是 */
|
|
1116
|
+
this.flyToModelAfterStart = true;
|
|
1117
|
+
/** 停止时是否返回起点,默认为是 */
|
|
1118
|
+
this.backStartPointWhenStop = true;
|
|
1119
|
+
/** 路径动画主体模型 */
|
|
1120
|
+
this._model = new Object3D();
|
|
1121
|
+
this._speed = 1;
|
|
1122
|
+
this._pathProgress = 0;
|
|
1123
|
+
this._totalProgress = 0;
|
|
1124
|
+
/** 当前播放的动画路径索引 */
|
|
1125
|
+
this._currentPathIndex = 0;
|
|
1126
|
+
/** 当前节点 */
|
|
1127
|
+
this._currentNode = null;
|
|
1128
|
+
/** 下个路径节点 */
|
|
1129
|
+
this._nextNode = null;
|
|
1130
|
+
/** 当前状态 */
|
|
1131
|
+
this._playStatus = PLAY_STATUS.PENDING;
|
|
1132
|
+
}
|
|
1133
|
+
changeStatus(status) {
|
|
1134
|
+
this._playStatus = status;
|
|
1135
|
+
this.onStatusChange(status);
|
|
1136
|
+
}
|
|
1137
|
+
/** 开始导航 */
|
|
1138
|
+
start() { }
|
|
1139
|
+
/** 重新开始导航 */
|
|
1140
|
+
restart() {
|
|
1141
|
+
this.stop();
|
|
1142
|
+
this.start();
|
|
1143
|
+
}
|
|
1144
|
+
/** 播放动画 */
|
|
1145
|
+
play(index = 0, node) {
|
|
1146
|
+
this.changeStatus(PLAY_STATUS.PLAYING);
|
|
1147
|
+
this.paths[index];
|
|
1148
|
+
}
|
|
1149
|
+
/** 暂停动画 */
|
|
1150
|
+
pause() {
|
|
1151
|
+
this.changeStatus(PLAY_STATUS.PAUSE);
|
|
1152
|
+
}
|
|
1153
|
+
/** 停止动画 */
|
|
1154
|
+
stop() {
|
|
1155
|
+
this.changeStatus(PLAY_STATUS.STOP);
|
|
1156
|
+
}
|
|
1157
|
+
/** 路径反转 */
|
|
1158
|
+
flip() { }
|
|
1159
|
+
/** 结束导航 */
|
|
1160
|
+
over() {
|
|
1161
|
+
this.changeStatus(PLAY_STATUS.PENDING);
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
const azimuth = new It();
|
|
1166
|
+
|
|
1167
|
+
export { It as Azimuth, GyroListener, MapCamera, NAVIGATE_ORIENTATION_TYPE, NAVIGATE_VISION_TYPE, NavigateCamera, Navigator, PLAY_STATUS, SoonSpaceCamera, Vector3ToEuler, azimuth, eulerToVector3, getDirection, getDirectionByNodes, getDistanceInfoByProgress, getFixEuler, getFixVector, getNavigatorNodeInfoByProgress, getNavigatorNodeInfosByTopology, normalizeEuler, reverseTopologies };
|
|
1168
|
+
//# sourceMappingURL=index.esm.js.map
|