@@ -786,6 +786,9 @@ class SceneModel extends _GanyDOMWidgetModel {
786
786
background_color : 'white' ,
787
787
background_opacity : 1. ,
788
788
children : [ ] ,
789
+ camera_position : null ,
790
+ camera_up : [ 0 , 1 , 0 ] ,
791
+ camera_target : null ,
789
792
} ;
790
793
}
791
794
@@ -794,6 +797,10 @@ class SceneModel extends _GanyDOMWidgetModel {
794
797
795
798
this . scene = new Scene ( ) ;
796
799
800
+ this . _scale = new THREE . Vector3 ( 1. , 1. , 1. ) ;
801
+ this . _translation = new THREE . Vector3 ( 0. , 0. , 0. ) ;
802
+ this . updateMatrix ( ) ;
803
+
797
804
this . updateChildren ( ) ;
798
805
this . on ( 'change:children' , this . updateChildren . bind ( this ) ) ;
799
806
}
@@ -806,6 +813,42 @@ class SceneModel extends _GanyDOMWidgetModel {
806
813
return this . get ( 'background_opacity' )
807
814
}
808
815
816
+ get cameraPosition ( ) : THREE . Vector3 | null {
817
+ const position = this . get ( 'camera_position' ) ;
818
+
819
+ if ( position === null ) {
820
+ return null ;
821
+ }
822
+
823
+ return this . scale ( new THREE . Vector3 ( position [ 0 ] , position [ 1 ] , position [ 2 ] ) ) ;
824
+ }
825
+
826
+ get cameraUp ( ) : THREE . Vector3 {
827
+ const position = this . get ( 'camera_up' ) ;
828
+ return new THREE . Vector3 ( position [ 0 ] , position [ 1 ] , position [ 2 ] ) ;
829
+ }
830
+
831
+ get cameraTarget ( ) : THREE . Vector3 | null {
832
+ const target = this . get ( 'camera_target' ) ;
833
+
834
+ if ( target === null ) {
835
+ return null ;
836
+ }
837
+
838
+ return this . scale ( new THREE . Vector3 ( target [ 0 ] , target [ 1 ] , target [ 2 ] ) ) ;
839
+ }
840
+
841
+ private scale ( vector : THREE . Vector3 ) {
842
+ return new THREE . Vector3 ( ) . copy ( vector ) . applyMatrix4 ( this . _matrix ) ;
843
+ }
844
+
845
+ private updateMatrix ( ) {
846
+ const scaleMatrix = new THREE . Matrix4 ( ) . makeScale ( this . _scale . x , this . _scale . y , this . _scale . z ) ;
847
+ const positionMatrix = new THREE . Matrix4 ( ) . makeTranslation ( this . _translation . x , this . _translation . y , this . _translation . z ) ;
848
+
849
+ this . _matrix = new THREE . Matrix4 ( ) . multiplyMatrices ( scaleMatrix , positionMatrix ) ;
850
+ }
851
+
809
852
private updateChildren ( ) {
810
853
// TODO: Remove old children
811
854
@@ -816,20 +859,32 @@ class SceneModel extends _GanyDOMWidgetModel {
816
859
}
817
860
818
861
const boundingSphereRadius = Math . max ( ...blocks . map ( ( block : Block ) => block . boundingSphere . radius ) ) ;
819
- const scale = new THREE . Vector3 ( 1 / boundingSphereRadius , 1 / boundingSphereRadius , 1 / boundingSphereRadius ) ;
862
+
863
+ const scale = 1 / boundingSphereRadius ;
864
+ this . _scale . x = scale ;
865
+ this . _scale . y = scale ;
866
+ this . _scale . z = scale ;
820
867
821
868
const position = blocks [ 0 ] . boundingSphere . center ;
822
869
870
+ this . _translation = new THREE . Vector3 ( - position . x , - position . y , - position . z ) ;
871
+
872
+ this . updateMatrix ( ) ;
873
+
823
874
for ( const block of blocks ) {
824
- block . scale = scale ;
825
- block . position = new THREE . Vector3 ( - position . x , - position . y , - position . z ) ;
875
+ block . scale = this . _scale ;
876
+ block . position = this . _translation ;
826
877
827
878
this . scene . addBlock ( block ) ;
828
879
}
829
880
}
830
881
831
882
scene : Scene ;
832
883
884
+ private _scale : THREE . Vector3 ;
885
+ private _translation : THREE . Vector3 ;
886
+ private _matrix : THREE . Matrix4 ;
887
+
833
888
static serializers : ISerializers = {
834
889
..._GanyDOMWidgetModel . serializers ,
835
890
children : { deserialize : ( unpack_models as any ) } ,
@@ -854,6 +909,10 @@ class SceneView extends DOMWidgetView {
854
909
this . renderer . backgroundColor = this . model . backgroundColor
855
910
this . renderer . backgroundOpacity = this . model . backgroundOpacity ;
856
911
912
+ this . updateCameraUp ( ) ;
913
+ this . updateCameraPosition ( ) ;
914
+ this . updateCameraTarget ( ) ;
915
+
857
916
this . initEventListeners ( ) ;
858
917
} ) ;
859
918
}
@@ -863,6 +922,34 @@ class SceneView extends DOMWidgetView {
863
922
864
923
this . model . on ( 'change:background_color' , ( ) => { this . renderer . backgroundColor = this . model . backgroundColor ; } ) ;
865
924
this . model . on ( 'change:background_opacity' , ( ) => { this . renderer . backgroundOpacity = this . model . backgroundOpacity ; } ) ;
925
+
926
+ this . model . on ( 'change:camera_position' , this . updateCameraPosition . bind ( this ) ) ;
927
+ this . model . on ( 'change:camera_target' , this . updateCameraTarget . bind ( this ) ) ;
928
+ this . model . on ( 'change:camera_up' , this . updateCameraUp . bind ( this ) ) ;
929
+ }
930
+
931
+ updateCameraPosition ( ) {
932
+ const newPosition = this . model . cameraPosition ;
933
+
934
+ if ( newPosition === null ) {
935
+ return ;
936
+ }
937
+
938
+ this . renderer . cameraPosition = newPosition ;
939
+ }
940
+
941
+ updateCameraTarget ( ) {
942
+ const newTarget = this . model . cameraTarget ;
943
+
944
+ if ( newTarget === null ) {
945
+ return ;
946
+ }
947
+
948
+ this . renderer . cameraTarget = newTarget ;
949
+ }
950
+
951
+ updateCameraUp ( ) {
952
+ this . renderer . cameraUp = this . model . cameraUp ;
866
953
}
867
954
868
955
processPhosphorMessage ( msg : Message ) {
0 commit comments