xzw 2 anos atrás
pai
commit
41c6f28634
2 arquivos alterados com 121 adições e 51 exclusões
  1. 45 28
      src/custom/viewer/ViewerNew.js
  2. 76 23
      src/viewer/ExtendView.js

+ 45 - 28
src/custom/viewer/ViewerNew.js

@@ -3622,12 +3622,12 @@ export class Viewer extends ViewerBase{
             position = new THREE.Vector3, //相机最终位置
             dis;                          //相机距离目标
         duration = duration == void 0 ? 1200 : duration;      
-        let camera = viewer.scene.getActiveCamera()
+        let camera = o.endCamera || viewer.scene.getActiveCamera()
         let cameraPos = camera.position.clone()
-         
-        if(camera.type == 'OrthographicCamera'){
+        let boundSize   
+        /* if(camera.type == 'OrthographicCamera'){
             return console.error('focusOnObject暂不支持OrthographicCamera。因情况复杂,请视情况使用splitScreenTool.viewportFitBound')
-        }
+        } */
         
         let getPosWithFullBound = (points, boundingBox, target, cameraPos  )=>{//使boundingBox差不多占满屏幕时的相机到target的距离
             // points 和 boundingBox 至少有一个
@@ -3656,7 +3656,7 @@ export class Viewer extends ViewerBase{
                 bound = boundingBox.applyMatrix4(inv);
                 scale = 0.9; 
             }
-            let boundSize = bound.getSize(new THREE.Vector3)
+            boundSize = bound.getSize(new THREE.Vector3)
             
             
             {
@@ -3667,14 +3667,16 @@ export class Viewer extends ViewerBase{
                 boundSize.x = Math.max(min, boundSize.x)
                 boundSize.y = Math.max(min, boundSize.y)
             }
-            
-            
-            let aspect = boundSize.x / boundSize.y
-            if(camera.aspect > aspect){//视野更宽则用bound的纵向来决定
-                dis = boundSize.y/2/ Math.tan(THREE.Math.degToRad(camera.fov / 2)) + boundSize.z/2 
-            }else{
-                let hfov = cameraLight.getHFOVForCamera(camera, true);
-                dis = boundSize.x/2 / Math.tan(hfov / 2) + boundSize.z/2
+            if(camera.type == 'OrthographicCamera'){
+                dis = boundSize.length() / 2
+            }else{     
+                let aspect = boundSize.x / boundSize.y
+                if(camera.aspect > aspect){//视野更宽则用bound的纵向来决定
+                    dis = boundSize.y/2/ Math.tan(THREE.Math.degToRad(camera.fov / 2)) + boundSize.z/2 
+                }else{
+                    let hfov = cameraLight.getHFOVForCamera(camera, true);
+                    dis = boundSize.x/2 / Math.tan(hfov / 2) + boundSize.z/2
+                }
             }
             dis = Math.max(0.1,dis)
             
@@ -3961,7 +3963,10 @@ export class Viewer extends ViewerBase{
             }
         }else if(object.boundingBox && type == 'boundingBox'){//使屏幕刚好看全boundingBox
             target = object.boundingBox.getCenter(new THREE.Vector3)
-            position = getPosWithFullBound(object.points, object.boundingBox, target, cameraPos  )
+            if(o.dir){ //指定方向
+                cameraPos.copy(target).sub(o.dir)
+            }  
+            position = getPosWithFullBound(object.points, object.boundingBox.clone(), target, cameraPos  )
             if(Potree.settings.displayMode == 'showPanos'){//全景 (比较难校准)
                 let pano = viewer.images360.fitPanoTowardPoint({ 
                     point : position,
@@ -3990,21 +3995,33 @@ export class Viewer extends ViewerBase{
 
 
  
-         
-            
-        /*} else if(dimension == 2){//线
-            
-        }else if(dimension == 3){//面
-            
-        }else{//立体
-            
-        } */
-
-        viewer.scene.view.setView({position, target, duration, callback:()=>{
-                //console.log('focusOnObjectSuccess: '+object.name,  type)
-                deferred.resolve()
+        if(o.startCamera && o.endCamera){
+            viewer.scene.view.tranCamera(this.mainViewport,  { endPosition:position, target ,
+                boundSize, 
+                callback:()=>{
+                    //console.log('focusOnObjectSuccess: '+object.name,  type)
+                    deferred.resolve()
+                }, startCamera:o.startCamera, endCamera:o.endCamera, midCamera:this.scene.cameraBasic,
+                endYaw:o.endYaw,  endPitch:o.endPitch
+            }, duration) 
+        }else if(camera.type == "OrthographicCamera"){   
+            viewer.scene.view.moveOrthoCamera(this.mainViewport,  { endPosition:position, target ,
+                boundSize, 
+                endYaw:o.endYaw,  endPitch:o.endPitch,
+                callback:()=>{
+                    //console.log('focusOnObjectSuccess: '+object.name,  type)
+                    deferred.resolve()
+                }, 
+            }, duration)
+        }else{ 
+            viewer.scene.view.setView({position, target, duration,
+                endYaw:o.endYaw,  endPitch:o.endPitch,
+                callback:()=>{
+                    //console.log('focusOnObjectSuccess: '+object.name,  type)
+                    deferred.resolve()
+                }
             }
-        })
+        )}
          
         this.dispatchEvent({type:'focusOnObject', CamTarget:target, position}) //给controls发送信息
         

+ 76 - 23
src/viewer/ExtendView.js

@@ -50,7 +50,7 @@ class ExtendView extends View {
     }
     
     set quaternion(q){
-        this.direction = new THREE.Vector3(0,0,-1).applyQuaternion(q)
+        this.direction = new THREE.Vector3(0,0,-1).applyQuaternion(q) //注意如果得到的dir.x==dir.y==0,  yaw不会变为0, 导致算的quaternion和q不一致
     }
     
     copy(a){
@@ -180,7 +180,7 @@ class ExtendView extends View {
     setView( info = {}){
         // position, target, duration = 0, callback = null, onUpdate = null, Easing='', cancelFun
         this.cancelFlying()
-        let posWaitDone,  rotWaitDone 
+        let posWaitDone,  rotWaitDone , dir
         
         let posDone = ()=>{
             rotWaitDone || done()
@@ -188,10 +188,14 @@ class ExtendView extends View {
         }
         let rotDone = ()=>{
             if(endTarget){
-                this.lookAt(endTarget); //compute radius for orbitcontrol
+                this.lookAt(endTarget); //compute radius for orbitcontrol 
             }else if(endQuaternion){
                 this.rotation = new THREE.Euler().setFromQuaternion(endQuaternion)
+            }else if(endYaw != void 0){
+                this.yaw = endYaw,  this.pitch = endPitch
             }
+            //if(dir.x == 0 && dir.y == 0)this.yaw = 0 //统一一下 朝上的话是正的。朝下的一般不是0,会保留一个接近0的小数所以不用管
+           
             posWaitDone || done()
             rotWaitDone = false 
         }
@@ -212,24 +216,27 @@ class ExtendView extends View {
         
         let endPosition = new THREE.Vector3().copy(info.position)
         let startPosition = this.position.clone();
-		let startQuaternion, endQuaternion, endTarget = null ;
+		let startQuaternion, endQuaternion, endTarget = null,  
+            endYaw = info.endYaw, startYaw, endPitch = info.endPitch,  startPitch ;
+        
+        
         this.restrictPos(endPosition)
          
-		if(info.target ){
+         
+        if(endYaw != void 0) {
+            startYaw = this.yaw  
+            startPitch = this.pitch  
+		}else if(info.target ){
 			endTarget = new THREE.Vector3().copy(info.target)  
-            endQuaternion = math.getQuaFromPosAim(endPosition,endTarget)  
-            let dir = new THREE.Vector3().subVectors(endTarget, endPosition).normalize()
-           
-            let view = this.clone();
-            view.direction = dir;
-              
+            endQuaternion = math.getQuaFromPosAim(endPosition,endTarget) //若为垂直,会自动偏向x负的方向
+            dir = new THREE.Vector3().subVectors(endTarget, endPosition).normalize()
+            //console.log(dir, this.direction)   
 		}else if(info.quaternion){
             endQuaternion = info.quaternion.clone()
         }
          
         if(endQuaternion){ 
-            startQuaternion = this.quaternion//new THREE.Quaternion().setFromEuler(this.rotation)
-           
+            startQuaternion = this.quaternion 
         }
         
         
@@ -264,16 +271,19 @@ class ExtendView extends View {
                 }, info.ignoreFirstFrame);  
             } 
             
-            if(endQuaternion){
+            if(endQuaternion || endYaw != void 0){
                 rotWaitDone = true 
                 transitions.start( (progress, delta )=>{
-                   
-                    let quaternion = (new THREE.Quaternion()).copy(startQuaternion) 
-                    lerp.quaternion(quaternion, endQuaternion)(progress)  //在垂直的视角下的角度突变的厉害,这时候可能渐变yaw比较好
-             
-                    //this.rotation = new THREE.Euler().setFromQuaternion(quaternion)
-                    this.quaternion = quaternion
-                     
+                    if(endYaw != void 0){
+                        this.yaw = startYaw * (1-progress) + endYaw * progress
+                        this.pitch = startPitch * (1-progress) + endPitch * progress
+                    }else{ 
+                        let quaternion = (new THREE.Quaternion()).copy(startQuaternion) 
+                        lerp.quaternion(quaternion, endQuaternion)(progress)  //在垂直的视角下的角度突变的厉害,这时候可能渐变yaw比较好
+                        //console.log(quaternion)
+                        //this.rotation = new THREE.Euler().setFromQuaternion(quaternion)
+                        this.quaternion = quaternion
+                    }
                     posChange || info.onUpdate && info.onUpdate(progress, delta)  
                     
                 }, info.duration, rotDone , 0, info.Easing ? easing[info.Easing] : easing.easeInOutSine ,null, this.LookTransition, ()=>{
@@ -308,13 +318,16 @@ class ExtendView extends View {
     
     //平移Ortho相机
     moveOrthoCamera(viewport,  info, duration,  easeName){//boundSize优先于endZoom。
-        let camera = viewport.camera
+        let camera = info.camera || viewport.camera
         
         let startZoom = camera.zoom 
         let endPosition = info.endPosition 
         let boundSize = info.boundSize
         let endZoom = info.endZoom
         let margin = info.margin || {x:0,y:0}/* 200 */ //像素
+        let onUpdate = info.onUpdate 
+        
+        
         
         if(info.bound){//需要修改boundSize以适应相机的旋转,当相机不在xy水平面上朝向z时
             endPosition = endPosition || info.bound.getCenter(new THREE.Vector3())
@@ -329,9 +342,12 @@ class ExtendView extends View {
             boundSize.set(1,1)  //避免infinity
         }
         
+        
+        
+        
         this.setView( Object.assign(info,  { position:endPosition,  duration, 
             
-            onUpdate:(progress)=>{ 
+            onUpdate:(progress, delta)=>{ 
                 if(boundSize || endZoom){ 
                     if(boundSize){
                         let aspect = boundSize.x / boundSize.y
@@ -349,6 +365,7 @@ class ExtendView extends View {
                     
                     camera.zoom = endZoom * progress + startZoom * (1 - progress)
                     camera.updateProjectionMatrix() 
+                    onUpdate && onUpdate(progress, delta)
                 } 
             },
             
@@ -391,6 +408,42 @@ class ExtendView extends View {
     
         
         
+    } 
+    
+    
+    tranCamera(viewport,  info, duration,  easeName){
+        viewport.camera = info.midCamera
+        //viewport.camera.matrixWorld = info.endCamera.matrixWorld
+        
+        
+        //viewer.setCameraMode(CameraMode.ORTHOGRAPHIC) 
+        info.midCamera.projectionMatrix.copy(info.startCamera.projectionMatrix)
+        
+        let onUpdate = info.onUpdate
+        info.onUpdate = (progress, delta)=>{ 
+            lerp.matrix4(info.midCamera.projectionMatrix, info.endCamera.projectionMatrix)(progress) 
+            
+            /* console.log('matrixWorld', viewport.camera.position.toArray(), viewport.camera.matrixWorld.toArray())
+            console.log('projectionMatrix', info.endCamera.zoom,  viewport.camera.projectionMatrix.toArray())
+             */
+            onUpdate && onUpdate(progress, delta)
+        }
+        
+        let callback = info.callback
+        info.callback = ()=>{ 
+            viewport.camera = info.endCamera 
+            callback && callback()
+        } 
+        
+        
+        info.camera = info.endCamera
+        
+        if(info.camera.type == "OrthographicCamera"){
+            this.moveOrthoCamera(viewport,  info, duration,  easeName)
+        }else{
+            this.setView( Object.assign(info,  { duration}) )
+        }
+        
     }