@@ -5,98 +5,84 @@ import {
5
5
PrimitiveType ,
6
6
} from '../../types' ;
7
7
8
- const cubeMapFaces = {
9
- 0 : 'TEXTURE_CUBE_MAP_POSITIVE_X' ,
10
- 1 : 'TEXTURE_CUBE_MAP_NEGATIVE_X' ,
11
- 2 : 'TEXTURE_CUBE_MAP_POSITIVE_Y' ,
12
- 3 : 'TEXTURE_CUBE_MAP_NEGATIVE_Y' ,
13
- 4 : 'TEXTURE_CUBE_MAP_POSITIVE_Z' ,
14
- 5 : 'TEXTURE_CUBE_MAP_NEGATIVE_Z' ,
15
- } as const ;
8
+ import { PickByType } from '../../../utils' ;
16
9
17
- const blendDestinations = {
18
- [ BlendMode . Opaque ] : 'ZERO' ,
19
- [ BlendMode . AlphaKey ] : 'ZERO' ,
20
- [ BlendMode . Alpha ] : 'ONE_MINUS_SRC_ALPHA' ,
21
- [ BlendMode . Add ] : 'ONE' ,
22
- [ BlendMode . Mod ] : 'ZERO' ,
23
- [ BlendMode . Mod2x ] : 'SRC_COLOR' ,
24
- [ BlendMode . ModAdd ] : 'ONE' ,
25
- [ BlendMode . InvSrcAlphaAdd ] : 'ONE' ,
26
- [ BlendMode . InvSrcAlphaOpaque ] : 'ZERO' ,
27
- [ BlendMode . SrcAlphaOpaque ] : 'ZERO' ,
28
- [ BlendMode . NoAlphaAdd ] : 'ONE' ,
29
- [ BlendMode . ConstantAlpha ] : 'ONE_MINUS_CONSTANT_ALPHA' ,
30
- } as const ;
10
+ export default ( gl : WebGL2RenderingContext ) => {
11
+ const resolve = ( prop : keyof PickByType < WebGL2RenderingContext , number > ) : number => {
12
+ const constant = gl [ prop ] ;
13
+ if ( constant === undefined ) {
14
+ throw new Error ( `Could not find WebGL2 constant: ${ prop } ` ) ;
15
+ }
16
+ return constant ;
17
+ } ;
31
18
32
- const blendSources = {
33
- [ BlendMode . Opaque ] : 'ONE' ,
34
- [ BlendMode . AlphaKey ] : 'ONE' ,
35
- [ BlendMode . Alpha ] : 'SRC_ALPHA' ,
36
- [ BlendMode . Add ] : 'SRC_ALPHA' ,
37
- [ BlendMode . Mod ] : 'DST_COLOR' ,
38
- [ BlendMode . Mod2x ] : 'DST_COLOR' ,
39
- [ BlendMode . ModAdd ] : 'DST_COLOR' ,
40
- [ BlendMode . InvSrcAlphaAdd ] : 'ONE_MINUS_SRC_ALPHA' ,
41
- [ BlendMode . InvSrcAlphaOpaque ] : 'ONE_MINUS_SRC_ALPHA' ,
42
- [ BlendMode . SrcAlphaOpaque ] : 'SRC_ALPHA' ,
43
- [ BlendMode . NoAlphaAdd ] : 'ONE' ,
44
- [ BlendMode . ConstantAlpha ] : 'CONSTANT_ALPHA' ,
45
- } as const ;
19
+ const constants = {
20
+ cubeMapFaces : {
21
+ 0 : resolve ( 'TEXTURE_CUBE_MAP_POSITIVE_X' ) ,
22
+ 1 : resolve ( 'TEXTURE_CUBE_MAP_NEGATIVE_X' ) ,
23
+ 2 : resolve ( 'TEXTURE_CUBE_MAP_POSITIVE_Y' ) ,
24
+ 3 : resolve ( 'TEXTURE_CUBE_MAP_NEGATIVE_Y' ) ,
25
+ 4 : resolve ( 'TEXTURE_CUBE_MAP_POSITIVE_Z' ) ,
26
+ 5 : resolve ( 'TEXTURE_CUBE_MAP_NEGATIVE_Z' ) ,
27
+ } ,
46
28
47
- // TODO: Texture format
29
+ blendDestinations : {
30
+ [ BlendMode . Opaque ] : resolve ( 'ZERO' ) ,
31
+ [ BlendMode . AlphaKey ] : resolve ( 'ZERO' ) ,
32
+ [ BlendMode . Alpha ] : resolve ( 'ONE_MINUS_SRC_ALPHA' ) ,
33
+ [ BlendMode . Add ] : resolve ( 'ONE' ) ,
34
+ [ BlendMode . Mod ] : resolve ( 'ZERO' ) ,
35
+ [ BlendMode . Mod2x ] : resolve ( 'SRC_COLOR' ) ,
36
+ [ BlendMode . ModAdd ] : resolve ( 'ONE' ) ,
37
+ [ BlendMode . InvSrcAlphaAdd ] : resolve ( 'ONE' ) ,
38
+ [ BlendMode . InvSrcAlphaOpaque ] : resolve ( 'ZERO' ) ,
39
+ [ BlendMode . SrcAlphaOpaque ] : resolve ( 'ZERO' ) ,
40
+ [ BlendMode . NoAlphaAdd ] : resolve ( 'ONE' ) ,
41
+ [ BlendMode . ConstantAlpha ] : resolve ( 'ONE_MINUS_CONSTANT_ALPHA' ) ,
42
+ } ,
48
43
49
- const bufferFormatByPoolTarget = {
50
- [ PoolTarget . Vertex ] : 'ZERO' ,
51
- [ PoolTarget . Index ] : 'UNSIGNED_SHORT' ,
52
- } as const ;
44
+ blendSources : {
45
+ [ BlendMode . Opaque ] : resolve ( 'ONE' ) ,
46
+ [ BlendMode . AlphaKey ] : resolve ( 'ONE' ) ,
47
+ [ BlendMode . Alpha ] : resolve ( 'SRC_ALPHA' ) ,
48
+ [ BlendMode . Add ] : resolve ( 'SRC_ALPHA' ) ,
49
+ [ BlendMode . Mod ] : resolve ( 'DST_COLOR' ) ,
50
+ [ BlendMode . Mod2x ] : resolve ( 'DST_COLOR' ) ,
51
+ [ BlendMode . ModAdd ] : resolve ( 'DST_COLOR' ) ,
52
+ [ BlendMode . InvSrcAlphaAdd ] : resolve ( 'ONE_MINUS_SRC_ALPHA' ) ,
53
+ [ BlendMode . InvSrcAlphaOpaque ] : resolve ( 'ONE_MINUS_SRC_ALPHA' ) ,
54
+ [ BlendMode . SrcAlphaOpaque ] : resolve ( 'SRC_ALPHA' ) ,
55
+ [ BlendMode . NoAlphaAdd ] : resolve ( 'ONE' ) ,
56
+ [ BlendMode . ConstantAlpha ] : resolve ( 'CONSTANT_ALPHA' ) ,
57
+ } ,
53
58
54
- const bufferTypeByPooltarget = {
55
- [ PoolTarget . Vertex ] : 'ARRAY_BUFFER' ,
56
- [ PoolTarget . Index ] : 'ELEMENT_ARRAY_BUFFER' ,
57
- } as const ;
59
+ // TODO: Texture format
58
60
59
- const bufferUsageByPoolTarget = {
60
- [ PoolUsage . Static ] : 'STATIC_DRAW' ,
61
- [ PoolUsage . Dynamic ] : 'DYNAMIC_DRAW' ,
62
- [ PoolUsage . Stream ] : 'DYNAMIC_DRAW' ,
63
- } as const ;
61
+ bufferFormatByPoolTarget : {
62
+ [ PoolTarget . Vertex ] : resolve ( 'ZERO' ) ,
63
+ [ PoolTarget . Index ] : resolve ( 'UNSIGNED_SHORT' ) ,
64
+ } ,
64
65
65
- const primitiveTypes = {
66
- [ PrimitiveType . Points ] : 'POINTS' ,
67
- [ PrimitiveType . Lines ] : 'LINES' ,
68
- [ PrimitiveType . LineStrip ] : 'LINE_STRIP' ,
69
- [ PrimitiveType . Triangles ] : 'TRIANGLES' ,
70
- [ PrimitiveType . TriangleStrip ] : 'TRIANGLE_STRIP' ,
71
- [ PrimitiveType . TriangleFan ] : 'TRIANGLE_FAN' ,
72
- } as const ;
66
+ bufferTypeByPooltarget : {
67
+ [ PoolTarget . Vertex ] : resolve ( 'ARRAY_BUFFER' ) ,
68
+ [ PoolTarget . Index ] : resolve ( 'ELEMENT_ARRAY_BUFFER' ) ,
69
+ } ,
73
70
74
- export default ( gl : WebGL2RenderingContext ) => {
75
- const constants = { } ;
71
+ bufferUsageByPoolTarget : {
72
+ [ PoolUsage . Static ] : resolve ( 'STATIC_DRAW' ) ,
73
+ [ PoolUsage . Dynamic ] : resolve ( 'DYNAMIC_DRAW' ) ,
74
+ [ PoolUsage . Stream ] : resolve ( 'DYNAMIC_DRAW' ) ,
75
+ } ,
76
76
77
- const categories = {
78
- cubeMapFaces,
79
- blendDestinations,
80
- blendSources,
81
- bufferFormatByPoolTarget,
82
- bufferTypeByPooltarget,
83
- bufferUsageByPoolTarget,
84
- primitiveTypes,
85
- } as const ;
86
-
87
- for ( const [ name , category ] of Object . entries ( categories ) ) {
88
- const entry = { } ;
89
- for ( const [ index , prop ] of Object . entries ( category ) ) {
90
- const constant = gl [ prop ] ;
91
- if ( constant === undefined ) {
92
- throw new Error ( `Could not find WebGL2 constant: ${ prop } ` ) ;
93
- }
94
- // @ts -expect-error: currently unused (and untyped)
95
- entry [ index ] = constant ;
96
- }
97
- // @ts -expect-error: currently unused (and untyped)
98
- constants [ name ] = entry ;
99
- }
77
+ primitiveTypes : {
78
+ [ PrimitiveType . Points ] : resolve ( 'POINTS' ) ,
79
+ [ PrimitiveType . Lines ] : resolve ( 'LINES' ) ,
80
+ [ PrimitiveType . LineStrip ] : resolve ( 'LINE_STRIP' ) ,
81
+ [ PrimitiveType . Triangles ] : resolve ( 'TRIANGLES' ) ,
82
+ [ PrimitiveType . TriangleStrip ] : resolve ( 'TRIANGLE_STRIP' ) ,
83
+ [ PrimitiveType . TriangleFan ] : resolve ( 'TRIANGLE_FAN' ) ,
84
+ } ,
85
+ } ;
100
86
101
87
return constants ;
102
88
} ;
0 commit comments