@@ -19,15 +19,15 @@ class Screen extends EventEmitter {
1919 if ( ! opts . camera ) {
2020 const { fov, near, far, z } = opts ;
2121 if ( fov === 0 ) {
22- this . _camera = new this . three . OrthographicCamera (
23- - this . width * 0.5 , this . width * 0.5 ,
24- this . height * 0.5 , - this . height * 0.5 ,
22+ this . _camera = new this . _three . OrthographicCamera (
23+ - this . w * 0.5 , this . w * 0.5 ,
24+ this . h * 0.5 , - this . h * 0.5 ,
2525 near ?? - 10 , far ?? 10 ,
2626 ) ;
2727 this . _camera . position . z = z ?? 5 ;
2828 } else {
29- this . _camera = new this . three . PerspectiveCamera (
30- fov ?? 90 , this . width / this . height , near ?? 0.1 , far ?? 200 ,
29+ this . _camera = new this . _three . PerspectiveCamera (
30+ fov ?? 90 , this . w / this . h , near ?? 0.1 , far ?? 200 ,
3131 ) ;
3232 this . _camera . position . z = z ?? 10 ;
3333 }
@@ -36,40 +36,37 @@ class Screen extends EventEmitter {
3636 }
3737
3838 if ( ! opts . scene ) {
39- this . _scene = new this . three . Scene ( ) ;
39+ this . _scene = new this . _three . Scene ( ) ;
4040 } else {
4141 this . _scene = opts . scene ;
4242 }
4343
44- if ( ! opts . renderer ) {
45- this . _reinitRenderer ( ) ;
46- } else {
44+ if ( opts . renderer ) {
4745 this . _autoRenderer = false ;
4846 this . _renderer = opts . renderer ;
49- this . context . enable ( 0x8861 ) ; // GL_POINT_SPRITE 0x8861
50- this . context . enable ( 0x8642 ) ; // GL_VERTEX_PROGRAM_POINT_SIZE
51- this . context . enable ( 0x8862 ) ; // GL_COORD_REPLACE
5247 }
48+ this . _reinitRenderer ( ) ;
5349
54- this . renderer . setSize ( this . _doc . width , this . _doc . height , false ) ;
50+ this . _renderer . setSize ( this . _doc . width , this . _doc . height , false ) ;
5551
56- this . document . on ( 'mode' , ( ) => {
52+ this . _doc . on ( 'mode' , ( e ) => {
5753 this . _reinitRenderer ( ) ;
54+ this . emit ( 'mode' , e ) ;
5855 } ) ;
5956
60- this . document . on ( 'resize' , ( { width, height } ) => {
57+ this . _doc . on ( 'resize' , ( { width, height } ) => {
6158 width = width || 16 ;
6259 height = height || 16 ;
6360
64- this . camera . aspect = width / height ;
65- this . camera . updateProjectionMatrix ( ) ;
66- this . renderer . setSize ( width , height , false ) ;
61+ this . _camera . aspect = width / height ;
62+ this . _camera . updateProjectionMatrix ( ) ;
63+ this . _renderer . setSize ( width , height , false ) ;
6764
6865 this . emit ( 'resize' , { width, height } ) ;
6966 } ) ;
7067
7168 [ 'keydown' , 'keyup' , 'mousedown' , 'mouseup' , 'mousemove' , 'mousewheel' ] . forEach (
72- ( type ) => this . document . on ( type , ( e ) => this . emit ( type , e ) )
69+ ( type ) => this . _doc . on ( type , ( e ) => this . emit ( type , e ) )
7370 ) ;
7471
7572 this . draw ( ) ;
@@ -87,9 +84,9 @@ class Screen extends EventEmitter {
8784
8885 get width ( ) { return this . _doc . width ; }
8986 get height ( ) { return this . _doc . height ; }
90- get w ( ) { return this . width ; }
91- get h ( ) { return this . height ; }
92- get size ( ) { return new this . three . Vector2 ( this . width , this . height ) ; }
87+ get w ( ) { return this . _doc . width ; }
88+ get h ( ) { return this . _doc . height ; }
89+ get size ( ) { return new this . _three . Vector2 ( this . w , this . h ) ; }
9390
9491 get title ( ) { return this . _doc . title ; }
9592 set title ( v ) { this . _doc . title = v || 'Untitled' ; }
@@ -103,50 +100,82 @@ class Screen extends EventEmitter {
103100 this . _camera . updateProjectionMatrix ( ) ;
104101 }
105102
106- get mode ( ) { return this . _doc . _mode ; }
103+ get mode ( ) { return this . _doc . mode ; }
107104 set mode ( v ) { this . _doc . mode = v ; }
108105
109106 draw ( ) {
110- this . _renderer . render ( this . scene , this . camera ) ;
107+ this . _renderer . render ( this . _scene , this . _camera ) ;
111108 }
112109
113110
114111 snapshot ( name = `${ Date . now ( ) } .jpg` ) {
115112 const memSize = this . w * this . h * 4 ; // estimated number of bytes
116113 const storage = { data : Buffer . allocUnsafeSlow ( memSize ) } ;
117114
118- this . context . readPixels (
119- 0 , 0 , this . w , this . h , this . context . RGBA , this . context . UNSIGNED_BYTE , storage ,
115+ this . _gl . readPixels (
116+ 0 , 0 , this . w , this . h , this . _gl . RGBA , this . _gl . UNSIGNED_BYTE , storage ,
120117 ) ;
121118
122119 const img = this . _Image . fromPixels ( this . w , this . h , 32 , storage . data ) ;
123120 img . save ( name ) ;
124121 }
125122
123+ static _deepAssign ( src , dest ) {
124+ Object . entries ( src ) . forEach ( ( [ k , v ] ) => {
125+ if ( v && typeof v === 'object' ) {
126+ Screen . _deepAssign ( v , dest [ k ] ) ;
127+ return ;
128+ }
129+ dest [ k ] = v ;
130+ } ) ;
131+ }
132+
126133 // When switching from fullscreen and back, reset renderer to update VAO/FBO objects
127134 _reinitRenderer ( ) {
135+ const old = this . _renderer ;
136+
137+ // Migrate renderer props
138+ const renderProps = ! old ? null : {
139+ shadowMap : {
140+ enabled : old . shadowMap . enabled ,
141+ type : old . shadowMap . type ,
142+ } ,
143+ debug : {
144+ checkShaderErrors : old . debug_checkShaderErrors ,
145+ onShaderError : old . debug_onShaderError ,
146+ } ,
147+ autoClear : old . autoClear ,
148+ autoClearColor : old . autoClearColor ,
149+ autoClearDepth : old . autoClearDepth ,
150+ autoClearStencil : old . autoClearStencil ,
151+ clippingPlanes : old . clippingPlanes ,
152+ outputColorSpace : old . outputColorSpace ,
153+ sortObjects : old . sortObjects ,
154+ toneMapping : old . toneMapping ,
155+ toneMappingExposure : old . toneMappingExposure ,
156+ transmissionResolutionScale : old . transmissionResolutionScale ,
157+ } ;
128158 if ( this . _autoRenderer ) {
129- this . _renderer . dispose ( ) ;
159+ old . dispose ( ) ;
130160 }
131161
132162 this . _autoRenderer = true ;
133- this . _renderer = new this . three . WebGLRenderer ( {
134- context : this . context ,
135- antialias : true ,
163+ this . _renderer = new this . _three . WebGLRenderer ( {
164+ context : this . _gl ,
136165 canvas : this . canvas ,
137- alpha : true ,
138-
139- premultipliedAlpha : true ,
140- preserveDrawingBuffer : true ,
141- logarithmicDepthBuffer : true ,
142166 } ) ;
143167
144- this . _camera . aspect = this . width / this . height ;
168+ this . _camera . aspect = this . w / this . h ;
145169 this . _camera . updateProjectionMatrix ( ) ;
146- this . _renderer . setSize ( this . width , this . height , false ) ;
147- this . context . enable ( 0x8861 ) ; // GL_POINT_SPRITE 0x8861
148- this . context . enable ( 0x8642 ) ; // GL_VERTEX_PROGRAM_POINT_SIZE
149- this . context . enable ( 0x8862 ) ; // GL_COORD_REPLACE
170+ this . _renderer . setSize ( this . w , this . h , false ) ;
171+
172+ if ( renderProps ) {
173+ Screen . _deepAssign ( renderProps , this . _renderer ) ;
174+ }
175+
176+ this . _gl . enable ( 0x8861 ) ; // GL_POINT_SPRITE 0x8861
177+ this . _gl . enable ( 0x8642 ) ; // GL_VERTEX_PROGRAM_POINT_SIZE
178+ this . _gl . enable ( 0x8862 ) ; // GL_COORD_REPLACE
150179 }
151180}
152181
0 commit comments