1
1
import * as maptalks from 'maptalks' ;
2
2
import { defined } from './common/Util' ;
3
- import { createREGL , mat4 , vec2 , reshader , MaskRendererMixin } from '@maptalks/gl' ;
3
+ import { mat4 , vec2 , reshader , MaskRendererMixin } from '@maptalks/gl' ;
4
4
import { intersectsBox } from 'frustum-intersects' ;
5
5
import SHADER_MAP from './common/ShaderMap' ;
6
- import pickingVert from './common/glsl/picking.vert' ;
7
6
import sceneVert from './common/glsl/sceneVert.vert' ;
8
7
import extentFrag from './common/glsl/extent.frag' ;
9
8
import GLTFWorkerConnection from './common/GLTFWorkerConnection' ;
10
9
import { loadGLTF } from './worker/' ;
11
10
import MultiGLTFMarker from './MultiGLTFMarker' ;
12
11
12
+ const pickingVert = reshader . ShaderLib . get ( 'mesh_picking_vert' ) ;
13
+ const pickingWGSLVert = reshader . WgslShaderLib . get ( 'mesh_picking' ) . vert ;
14
+
13
15
const uniformDeclares = [ ] , tempBBox = [ ] ;
14
16
const pointLineModes = [ 'points' , 'lines' , 'line strip' , 'line loop' ] ;
15
- class GLTFLayerRenderer extends MaskRendererMixin ( maptalks . renderer . OverlayLayerCanvasRenderer ) {
17
+ class GLTFLayerRenderer extends MaskRendererMixin ( maptalks . renderer . OverlayLayerGLRenderer ) {
16
18
17
19
constructor ( layer ) {
18
20
super ( layer ) ;
@@ -149,7 +151,7 @@ class GLTFLayerRenderer extends MaskRendererMixin(maptalks.renderer.OverlayLayer
149
151
}
150
152
151
153
_setRenderMeshes ( marker , timestamp ) {
152
- const meshes = marker . getMeshes ( this . _gltfManager , this . regl , timestamp ) ;
154
+ const meshes = marker . getMeshes ( this . _gltfManager , this . regl || this . device , timestamp ) ;
153
155
const shader = marker . getShader ( ) ;
154
156
if ( meshes . length ) {
155
157
this . _renderMarkerList . push ( marker ) ;
@@ -278,67 +280,51 @@ class GLTFLayerRenderer extends MaskRendererMixin(maptalks.renderer.OverlayLayer
278
280
return false ;
279
281
}
280
282
281
- createContext ( ) {
282
- const inGroup = this . canvas . gl && this . canvas . gl . wrap ;
283
- if ( inGroup ) {
284
- this . gl = this . canvas . gl . wrap ( ) ;
285
- this . regl = this . canvas . gl . regl ;
286
- } else {
287
- const layer = this . layer ;
288
- const attributes = layer . options . glOptions || {
289
- alpha : true ,
290
- depth : true ,
291
- //antialias: true,
292
- stencil : true
293
- } ;
294
- this . glOptions = attributes ;
295
- this . gl = this . gl || this . _createGLContext ( this . canvas , attributes ) ;
296
- this . regl = createREGL ( {
297
- gl : this . gl ,
298
- optionalExtensions : [
299
- 'ANGLE_instanced_arrays' ,
300
- 'OES_element_index_uint' ,
301
- 'OES_standard_derivatives' ,
302
- 'OES_vertex_array_object' ,
303
- 'OES_texture_half_float' , 'OES_texture_half_float_linear' ,
304
- 'OES_texture_float' , 'OES_texture_float_linear' ,
305
- 'WEBGL_depth_texture' , 'EXT_shader_texture_lod' ,
306
- 'WEBGL_compressed_texture_s3tc'
307
- ]
308
- } ) ;
309
- }
310
- if ( inGroup ) {
311
- this . canvas . pickingFBO = this . canvas . pickingFBO || this . regl . framebuffer ( this . canvas . width , this . canvas . height ) ;
312
- }
313
- this . pickingFBO = this . canvas . pickingFBO || this . regl . framebuffer ( this . canvas . width , this . canvas . height ) ;
314
- this . _gltfManager = this . regl . gltfManager = this . regl . gltfManager || this . _createGLTFManager ( ) ;
283
+ initContext ( ) {
284
+ super . initContext ( ) ;
285
+ const { regl, device, reglGL } = this . context ;
286
+ const graphics = regl || device ;
287
+ this . regl = regl ;
288
+ this . gl = reglGL ;
289
+ this . device = device ;
290
+
291
+ const isWebGPU = ! ! device ;
292
+ const fboOptions = {
293
+ colorFormat : isWebGPU ? 'bgra8unorm' : 'rgba' ,
294
+ depthStencil : true ,
295
+ width : this . canvas . width ,
296
+ height : this . canvas . height
297
+ } ;
298
+ this . canvas . pickingFBO = this . canvas . pickingFBO || graphics . framebuffer ( fboOptions ) ;
299
+ this . pickingFBO = this . canvas . pickingFBO ;
300
+ this . _gltfManager = graphics . gltfManager = graphics . gltfManager || this . _createGLTFManager ( ) ;
315
301
// this._loginMarkerList();
316
302
this . _initRenderer ( ) ;
317
303
//检查是否有mesh、geometry未generate buffer过
318
304
if ( this . _noBuffersMeshes ) {
319
305
this . _noBuffersMeshes . forEach ( mesh => {
320
- mesh . generateInstancedBuffers ( this . regl ) ;
306
+ mesh . generateInstancedBuffers ( this . regl || this . device ) ;
321
307
} ) ;
322
308
}
323
309
if ( this . _noBuffersGeometries ) {
324
310
this . _noBuffersGeometries . forEach ( geometry => {
325
- geometry . generateBuffers ( this . regl ) ;
311
+ geometry . generateBuffers ( this . regl || this . device ) ;
326
312
} ) ;
327
313
}
328
- this . layer . fire ( 'contextcreate' , { regl : this . regl } ) ;
314
+ this . layer . fire ( 'contextcreate' , { regl : this . regl , device : this . device } ) ;
329
315
}
330
316
331
317
getGLTFManager ( ) {
332
318
return this . _gltfManager ;
333
319
}
334
320
335
321
_createGLTFManager ( ) {
336
- return new reshader . GLTFManager ( this . regl ) ;
322
+ return new reshader . GLTFManager ( this . regl || this . device ) ;
337
323
}
338
324
339
325
_initRenderer ( ) {
340
326
const map = this . layer . getMap ( ) ;
341
- const renderer = new reshader . Renderer ( this . regl ) ;
327
+ const renderer = new reshader . Renderer ( this . regl || this . device ) ;
342
328
this . renderer = renderer ;
343
329
this . _uniforms = {
344
330
'projMatrix' : map . projMatrix ,
@@ -348,18 +334,28 @@ class GLTFLayerRenderer extends MaskRendererMixin(maptalks.renderer.OverlayLayer
348
334
'cameraPosition' : map . cameraPosition ,
349
335
'altitudeScale' : 1
350
336
} ;
351
- reshader . pbr . PBRUtils . loginIBLResOnCanvas ( this . canvas , this . regl , map ) ;
352
-
337
+ reshader . pbr . PBRUtils . loginIBLResOnCanvas ( this . canvas , this . regl || this . device , map ) ;
338
+ const projViewModelMatrix = [ ] ;
339
+ const modelViewMatrix = [ ] ;
353
340
this . _picking = new reshader . FBORayPicking (
354
341
renderer ,
355
342
{
343
+ name : 'gltf-picking' ,
356
344
vert : pickingVert ,
345
+ wgslVert : pickingWGSLVert ,
357
346
uniforms : [
347
+ {
348
+ name : 'projViewModelMatrix' ,
349
+ type : 'function' ,
350
+ fn : function ( context , props ) {
351
+ return mat4 . multiply ( projViewModelMatrix , props [ 'projViewMatrix' ] , props [ 'modelMatrix' ] ) ;
352
+ }
353
+ } ,
358
354
{
359
355
name : 'modelViewMatrix' ,
360
356
type : 'function' ,
361
357
fn : function ( context , props ) {
362
- return mat4 . multiply ( [ ] , props [ 'viewMatrix' ] , props [ 'modelMatrix' ] ) ;
358
+ return mat4 . multiply ( modelViewMatrix , props [ 'viewMatrix' ] , props [ 'modelMatrix' ] ) ;
363
359
}
364
360
}
365
361
]
0 commit comments