@@ -19,15 +19,15 @@ class Screen extends EventEmitter {
19
19
if ( ! opts . camera ) {
20
20
const { fov, near, far, z } = opts ;
21
21
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 ,
25
25
near ?? - 10 , far ?? 10 ,
26
26
) ;
27
27
this . _camera . position . z = z ?? 5 ;
28
28
} 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 ,
31
31
) ;
32
32
this . _camera . position . z = z ?? 10 ;
33
33
}
@@ -36,40 +36,37 @@ class Screen extends EventEmitter {
36
36
}
37
37
38
38
if ( ! opts . scene ) {
39
- this . _scene = new this . three . Scene ( ) ;
39
+ this . _scene = new this . _three . Scene ( ) ;
40
40
} else {
41
41
this . _scene = opts . scene ;
42
42
}
43
43
44
- if ( ! opts . renderer ) {
45
- this . _reinitRenderer ( ) ;
46
- } else {
44
+ if ( opts . renderer ) {
47
45
this . _autoRenderer = false ;
48
46
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
52
47
}
48
+ this . _reinitRenderer ( ) ;
53
49
54
- this . renderer . setSize ( this . _doc . width , this . _doc . height , false ) ;
50
+ this . _renderer . setSize ( this . _doc . width , this . _doc . height , false ) ;
55
51
56
- this . document . on ( 'mode' , ( ) => {
52
+ this . _doc . on ( 'mode' , ( e ) => {
57
53
this . _reinitRenderer ( ) ;
54
+ this . emit ( 'mode' , e ) ;
58
55
} ) ;
59
56
60
- this . document . on ( 'resize' , ( { width, height } ) => {
57
+ this . _doc . on ( 'resize' , ( { width, height } ) => {
61
58
width = width || 16 ;
62
59
height = height || 16 ;
63
60
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 ) ;
67
64
68
65
this . emit ( 'resize' , { width, height } ) ;
69
66
} ) ;
70
67
71
68
[ '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 ) )
73
70
) ;
74
71
75
72
this . draw ( ) ;
@@ -87,9 +84,9 @@ class Screen extends EventEmitter {
87
84
88
85
get width ( ) { return this . _doc . width ; }
89
86
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 ) ; }
93
90
94
91
get title ( ) { return this . _doc . title ; }
95
92
set title ( v ) { this . _doc . title = v || 'Untitled' ; }
@@ -103,50 +100,82 @@ class Screen extends EventEmitter {
103
100
this . _camera . updateProjectionMatrix ( ) ;
104
101
}
105
102
106
- get mode ( ) { return this . _doc . _mode ; }
103
+ get mode ( ) { return this . _doc . mode ; }
107
104
set mode ( v ) { this . _doc . mode = v ; }
108
105
109
106
draw ( ) {
110
- this . _renderer . render ( this . scene , this . camera ) ;
107
+ this . _renderer . render ( this . _scene , this . _camera ) ;
111
108
}
112
109
113
110
114
111
snapshot ( name = `${ Date . now ( ) } .jpg` ) {
115
112
const memSize = this . w * this . h * 4 ; // estimated number of bytes
116
113
const storage = { data : Buffer . allocUnsafeSlow ( memSize ) } ;
117
114
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 ,
120
117
) ;
121
118
122
119
const img = this . _Image . fromPixels ( this . w , this . h , 32 , storage . data ) ;
123
120
img . save ( name ) ;
124
121
}
125
122
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
+
126
133
// When switching from fullscreen and back, reset renderer to update VAO/FBO objects
127
134
_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
+ } ;
128
158
if ( this . _autoRenderer ) {
129
- this . _renderer . dispose ( ) ;
159
+ old . dispose ( ) ;
130
160
}
131
161
132
162
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 ,
136
165
canvas : this . canvas ,
137
- alpha : true ,
138
-
139
- premultipliedAlpha : true ,
140
- preserveDrawingBuffer : true ,
141
- logarithmicDepthBuffer : true ,
142
166
} ) ;
143
167
144
- this . _camera . aspect = this . width / this . height ;
168
+ this . _camera . aspect = this . w / this . h ;
145
169
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
150
179
}
151
180
}
152
181
0 commit comments