1
+ declare const Promise : any ;
1
2
import Kernel from './Kernel' ;
2
3
import Utils from './Utils' ;
3
4
import { EventEmitter } from './Events' ;
@@ -65,8 +66,14 @@ class Camera extends Object3D {
65
66
private readonly animationDuration : number = 200 ; //层级变化的动画周期,毫秒
66
67
private readonly nearFactor : number = 0.6 ;
67
68
private readonly maxPitch : number = 40 ;
68
- private readonly resolutionFactor1 : number = Math . pow ( 2 , 0.3752950 ) ;
69
- private readonly resolutionFactor2 : number = Math . pow ( 2 , 1.3752950 ) ;
69
+ //resolutionFactor1的值为1时量算出的分辨率就是实际分辨率,但是图片不是256大小显示
70
+ //为了确保图片以256显示,需要将resolutionFactor1设置为Math.pow(2, 0.3752950)
71
+ //getResolution()和getResolutionInWorld()方法用于让其他类调用获取实际的分辨率,需要除以resolutionFactor1以便获取真实值
72
+ // private resolutionFactor1: number = Math.pow(2, 0.3752950);
73
+ private resolutionFactor1 : number ;
74
+ //resolutionFactor2用于矫正计算出的分辨率与实际分辨率之间的差别
75
+ // private resolutionFactor2: number = this.resolutionFactor1 * 2;
76
+ private resolutionFactor2 : number ;
70
77
71
78
//旋转的时候,绕着视线与地球交点进行旋转
72
79
//定义抬头时,旋转角为正值
@@ -106,8 +113,22 @@ class Camera extends Object3D {
106
113
//this.far可以动态计算
107
114
//this.aspect在Viewport改变后重新计算
108
115
//this.fov可以调整以实现缩放效果
109
- constructor ( private canvas : HTMLCanvasElement , private fov : number = 45 , private aspect : number = 1 , private near : number = 1 , private far : number = 100 , level : number = 3 , lonlat : number [ ] = [ 0 , 0 ] ) {
116
+ constructor (
117
+ private canvas : HTMLCanvasElement ,
118
+ private fov : number = 45 ,
119
+ private aspect : number = 1 ,
120
+ private near : number = 1 ,
121
+ private far : number = 100 ,
122
+ level : number = 3 ,
123
+ lonlat : number [ ] = [ 0 , 0 ] ,
124
+ resolutionFactor : number = Math . pow ( 2 , 0.3752950 ) ) {
125
+
110
126
super ( ) ;
127
+ if ( ! ( resolutionFactor > 0 ) ) {
128
+ resolutionFactor = Math . pow ( 2 , 0.3752950 ) ;
129
+ }
130
+ this . resolutionFactor1 = resolutionFactor ;
131
+ this . resolutionFactor2 = this . resolutionFactor1 * 2 ;
111
132
this . eventEmitter = new EventEmitter ( ) ;
112
133
this . lonlatsOfBoundary = [ ] ;
113
134
this . initFov = this . fov ;
@@ -446,8 +467,34 @@ class Camera extends Object3D {
446
467
return newFov ;
447
468
}
448
469
470
+ //返回x和y综合的平均分辨率
471
+ //for public use
472
+ getResolution ( ) : number {
473
+ const {
474
+ resolutionX,
475
+ bestDisplayLevelFloatX,
476
+ resolutionY,
477
+ bestDisplayLevelFloatY
478
+ } = this . measureXYResolutionAndBestDisplayLevel ( ) ;
479
+ return ( resolutionX + resolutionY ) / 2 / this . resolutionFactor1 ;
480
+ }
481
+
482
+ //for public use
483
+ getResolutionInWorld ( ) : number {
484
+ return this . getResolution ( ) / Kernel . SCALE_FACTOR ;
485
+ }
486
+
487
+ //屏幕1px在实际世界中的距离,for test
488
+ private getResolutionInWorld2 ( ) : number {
489
+ if ( realResolutionCache . hasOwnProperty ( this . level ) ) {
490
+ return realResolutionCache [ this . level ] ;
491
+ } else {
492
+ return Kernel . MAX_REAL_RESOLUTION / Math . pow ( 2 , this . level ) ;
493
+ }
494
+ }
495
+
449
496
//resolution,level
450
- measureXYResolutionAndBestDisplayLevel ( ) : any {
497
+ private measureXYResolutionAndBestDisplayLevel ( ) : any {
451
498
//计算resolution
452
499
var p = this . matrix . getPosition ( ) ;
453
500
var dir = Vector . fromVertice ( p ) ;
@@ -486,7 +533,7 @@ class Camera extends Object3D {
486
533
}
487
534
488
535
//[resolution,level]
489
- calculateCurrentResolutionAndBestDisplayLevel ( ) {
536
+ private calculateCurrentResolutionAndBestDisplayLevel ( ) {
490
537
var distance2EarthOrigin = this . getDistance2EarthOrigin ( ) ;
491
538
return this . _calculateResolutionAndBestDisplayLevelByDistance2EarthOrigin ( distance2EarthOrigin ) ;
492
539
}
@@ -540,15 +587,6 @@ class Camera extends Object3D {
540
587
return Kernel . MAX_RESOLUTION / Math . pow ( 2 , level ) ;
541
588
}
542
589
543
- //屏幕1px在实际世界中的距离
544
- getResolutionInWorld ( ) : number {
545
- if ( realResolutionCache . hasOwnProperty ( this . level ) ) {
546
- return realResolutionCache [ this . level ] ;
547
- } else {
548
- return Kernel . MAX_REAL_RESOLUTION / Math . pow ( 2 , this . level ) ;
549
- }
550
- }
551
-
552
590
getVertice ( ) {
553
591
const origin2PositionVector = Vector . fromVertice ( this . getPosition ( ) ) ;
554
592
origin2PositionVector . setLength ( Kernel . EARTH_RADIUS ) ;
@@ -584,7 +622,7 @@ class Camera extends Object3D {
584
622
this . level = level ;
585
623
this . floatLevel = level ;
586
624
}
587
- if ( levelChanged ) {
625
+ if ( levelChanged ) {
588
626
Utils . publish ( 'level-change' , {
589
627
oldLevel : oldLevel ,
590
628
newLevel : this . level
@@ -702,9 +740,48 @@ class Camera extends Object3D {
702
740
return pitch ;
703
741
}
704
742
705
- //计算拾取射线与地球的交点,以笛卡尔空间直角坐标系坐标数组的形式返回
706
- //该方法需要projViewMatrixForDraw系列矩阵进行计算
707
- getPickCartesianCoordInEarthByCanvas ( canvasX : number , canvasY : number ) : Vertice [ ] {
743
+ // _doWorkByMatrixForDraw(cb: any) {
744
+ // this._updateCore();
745
+
746
+ // //暂存projViewMatrix系列矩阵
747
+ // var matrix = this.matrix;
748
+ // var viewMatrix = this.viewMatrix;
749
+ // var projMatrix = this.projMatrix;
750
+ // var projViewMatrix = this.projViewMatrix;
751
+
752
+ // //将projViewMatrix系列矩阵赋值为projViewMatrixForDraw系列矩阵
753
+ // this.matrix = this.matrixForDraw;
754
+ // this.viewMatrix = this.viewMatrixForDraw;
755
+ // this.projMatrix = this.projMatrixForDraw;
756
+ // this.projViewMatrix = this.projViewMatrixForDraw;
757
+
758
+ // //基于projViewMatrixForDraw系列矩阵进行计算,应该没有误差
759
+ // // var pickDirection = this._getPickDirectionByCanvas(canvasX, canvasY);
760
+ // // var p = this.getPosition();
761
+ // // var line = new Line(p, pickDirection);
762
+ // // var result = this._getPickCartesianCoordInEarthByLine(line);
763
+ // cb();
764
+
765
+ // //还原projViewMatrix系列矩阵
766
+ // this.matrix = matrix;
767
+ // this.viewMatrix = viewMatrix;
768
+ // this.projMatrix = projMatrix;
769
+ // this.projViewMatrix = projViewMatrix;
770
+ // }
771
+
772
+ /**
773
+ * 该方法需要projViewMatrixForDraw系列矩阵进行计算
774
+ * 返回拾取的射线相关信息,result.line表示拾取的直线,result.vertices表示拾取的点,即射线与地球的交点
775
+ * @param canvasX
776
+ * @param canvasY
777
+ * @param verticesResult 如果为true,那么会计算result.vertices
778
+ * return {line, vertices}
779
+ */
780
+ getPickInfoByCanvas ( canvasX : number , canvasY : number , verticesResult : boolean = false ) :any {
781
+ const result : any = {
782
+ line : null ,
783
+ vertices : [ ]
784
+ } ;
708
785
this . _updateCore ( ) ;
709
786
710
787
//暂存projViewMatrix系列矩阵
@@ -722,8 +799,10 @@ class Camera extends Object3D {
722
799
//基于projViewMatrixForDraw系列矩阵进行计算,应该没有误差
723
800
var pickDirection = this . _getPickDirectionByCanvas ( canvasX , canvasY ) ;
724
801
var p = this . getPosition ( ) ;
725
- var line = new Line ( p , pickDirection ) ;
726
- var result = this . _getPickCartesianCoordInEarthByLine ( line ) ;
802
+ result . line = new Line ( p , pickDirection ) ;
803
+ if ( verticesResult ) {
804
+ result . vertices = this . _getPickCartesianCoordInEarthByLine ( result . line ) ;
805
+ }
727
806
728
807
//还原projViewMatrix系列矩阵
729
808
this . matrix = matrix ;
@@ -734,6 +813,13 @@ class Camera extends Object3D {
734
813
return result ;
735
814
}
736
815
816
+ //计算拾取射线与地球的交点,以笛卡尔空间直角坐标系坐标数组的形式返回
817
+ //该方法需要projViewMatrixForDraw系列矩阵进行计算
818
+ getPickCartesianCoordInEarthByCanvas ( canvasX : number , canvasY : number ) : Vertice [ ] {
819
+ const pickInfo = this . getPickInfoByCanvas ( canvasX , canvasY , true ) ;
820
+ return pickInfo . vertices ;
821
+ }
822
+
737
823
getLightDirection ( ) : Vector {
738
824
var dirVertice = this . matrix . getVectorZ ( ) ;
739
825
var direction = new Vector ( - dirVertice . x , - dirVertice . y , - dirVertice . z ) ;
@@ -768,7 +854,7 @@ class Camera extends Object3D {
768
854
}
769
855
770
856
animateTo ( newLon : number , newLat : number , newLevel : number = this . getLevel ( ) , duration : number = 1000 ) {
771
- const promise = new Promise ( ( resolve , reject ) => {
857
+ const promise = new Promise ( ( resolve : any , reject : any ) => {
772
858
if ( this . isAnimating ( ) ) {
773
859
reject ( "be animating" ) ;
774
860
return ;
@@ -860,14 +946,14 @@ class Camera extends Object3D {
860
946
requestAnimationFrame ( callback ) ;
861
947
}
862
948
863
- setExtent ( extent : Extent ) {
864
- if ( extent ) {
949
+ setExtent ( extent : Extent ) {
950
+ if ( extent ) {
865
951
const [ lon , lat , level ] = this . _calculateLonLatLevelByExtent ( extent ) ;
866
- this . centerTo ( lon , lat , level ) ;
952
+ this . centerTo ( lon , lat , level ) ;
867
953
}
868
954
}
869
955
870
- animateToExtent ( extent : Extent , duration : number = 1000 ) {
956
+ animateToExtent ( extent : Extent , duration : number = 1000 ) {
871
957
const [ lon , lat , level ] = this . _calculateLonLatLevelByExtent ( extent ) ;
872
958
return this . animateTo ( lon , lat , level , duration ) ;
873
959
}
@@ -916,16 +1002,16 @@ class Camera extends Object3D {
916
1002
this . setPosition ( newPosition ) ;
917
1003
}
918
1004
919
- private _safelyGetValidLevel ( level : number ) {
920
- if ( level > Kernel . MAX_LEVEL ) {
1005
+ private _safelyGetValidLevel ( level : number ) {
1006
+ if ( level > Kernel . MAX_LEVEL ) {
921
1007
level = Kernel . MAX_LEVEL ;
922
- } else if ( level < Kernel . MIN_LEVEL ) {
1008
+ } else if ( level < Kernel . MIN_LEVEL ) {
923
1009
level = Kernel . MIN_LEVEL ;
924
1010
}
925
1011
return level ;
926
1012
}
927
1013
928
- private _calculateLonLatLevelByExtent ( extent : Extent ) {
1014
+ private _calculateLonLatLevelByExtent ( extent : Extent ) {
929
1015
const centerLon = ( extent . getMinLon ( ) + extent . getMaxLon ( ) ) / 2 ;
930
1016
const centerLat = ( extent . getMinLat ( ) + extent . getMaxLat ( ) ) / 2 ;
931
1017
const deltaLon = extent . getMaxLon ( ) - extent . getMinLon ( ) ;
0 commit comments