@@ -34,11 +34,17 @@ const DEFAULT_STYLE = createDefaultStyle();
34
34
35
35
/**
36
36
* @typedef {Object } AttributeDescription
37
- * @property {string } name Attribute name, as will be declared in the headers (including a_)
38
- * @property {string } type Attribute type, either `float`, `vec2`, `vec4`...
39
- * @property {string } varyingName Varying name, as will be declared in the fragment shader (including v_)
37
+ * @property {string } name Attribute name, as will be declared in the header of the vertex shader (including a_)
38
+ * @property {string } type Attribute GLSL type, either `float`, `vec2`, `vec4`...
39
+ * @property {string } varyingName Varying name, as will be declared in the header of both shaders (including v_)
40
40
* @property {string } varyingType Varying type, either `float`, `vec2`, `vec4`...
41
- * @property {string } varyingExpression GLSL expression to assign to the varying in the vertex shader
41
+ * @property {string } varyingExpression GLSL expression to assign to the varying in the vertex shader (e.g. `unpackColor(a_myAttr)`)
42
+ */
43
+
44
+ /**
45
+ * @typedef {Object } UniformDescription
46
+ * @property {string } name Uniform name, as will be declared in the header of the vertex shader (including u_)
47
+ * @property {string } type Uniform GLSL type, either `float`, `vec2`, `vec4`...
42
48
*/
43
49
44
50
/**
@@ -48,8 +54,8 @@ const DEFAULT_STYLE = createDefaultStyle();
48
54
*
49
55
* ```js
50
56
* const shader = new ShaderBuilder()
51
- * .addVarying('v_width ', 'float', 'a_width ')
52
- * .addUniform('u_time')
57
+ * .addAttribute('a_width ', 'float')
58
+ * .addUniform('u_time', 'float )
53
59
* .setColorExpression('...')
54
60
* .setSymbolSizeExpression('...')
55
61
* .getSymbolFragmentShader();
@@ -63,7 +69,7 @@ export class ShaderBuilder {
63
69
constructor ( ) {
64
70
/**
65
71
* Uniforms; these will be declared in the header (should include the type).
66
- * @type {Array<string > }
72
+ * @type {Array<UniformDescription > }
67
73
* @private
68
74
*/
69
75
this . uniforms_ = [ ] ;
@@ -202,33 +208,37 @@ export class ShaderBuilder {
202
208
/**
203
209
* Adds a uniform accessible in both fragment and vertex shaders.
204
210
* The given name should include a type, such as `sampler2D u_texture`.
205
- * @param {string } name Uniform name
211
+ * @param {string } name Uniform name, including the `u_` prefix
212
+ * @param {'float'|'vec2'|'vec3'|'vec4'|'sampler2D' } type GLSL type
206
213
* @return {ShaderBuilder } the builder object
207
214
*/
208
- addUniform ( name ) {
209
- this . uniforms_ . push ( name ) ;
215
+ addUniform ( name , type ) {
216
+ this . uniforms_ . push ( {
217
+ name,
218
+ type,
219
+ } ) ;
210
220
return this ;
211
221
}
212
222
213
223
/**
214
224
* Adds an attribute accessible in the vertex shader, read from the geometry buffer.
215
225
* The given name should include a type, such as `vec2 a_position`.
216
226
* Attributes will also be made available under the same name in fragment shaders.
217
- * @param {string } name Attribute name
218
- * @param {'float'|'vec2'|'vec3'|'vec4' } type Type
219
- * @param {string } [transform ] Expression which will be assigned to the varying in the vertex shader, and
227
+ * @param {string } name Attribute name, including the `a_` prefix
228
+ * @param {'float'|'vec2'|'vec3'|'vec4' } type GLSL type
229
+ * @param {string } [varyingExpression ] Expression which will be assigned to the varying in the vertex shader, and
220
230
* passed on to the fragment shader.
221
- * @param {'float'|'vec2'|'vec3'|'vec4' } [transformedType ] Type of the attribute after transformation;
231
+ * @param {'float'|'vec2'|'vec3'|'vec4' } [varyingType ] Type of the attribute after transformation;
222
232
* e.g. `vec4` after unpacking color components
223
233
* @return {ShaderBuilder } the builder object
224
234
*/
225
- addAttribute ( name , type , transform , transformedType ) {
235
+ addAttribute ( name , type , varyingExpression , varyingType ) {
226
236
this . attributes_ . push ( {
227
237
name,
228
238
type,
229
239
varyingName : name . replace ( / ^ a _ / , 'v_' ) ,
230
- varyingType : transformedType ?? type ,
231
- varyingExpression : transform ?? name ,
240
+ varyingType : varyingType ?? type ,
241
+ varyingExpression : varyingExpression ?? name ,
232
242
} ) ;
233
243
return this ;
234
244
}
@@ -463,7 +473,7 @@ export class ShaderBuilder {
463
473
}
464
474
465
475
return `${ COMMON_HEADER }
466
- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
476
+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
467
477
attribute vec2 a_position;
468
478
attribute float a_index;
469
479
attribute vec4 a_hitColor;
@@ -540,7 +550,7 @@ ${this.attributes_
540
550
}
541
551
542
552
return `${ COMMON_HEADER }
543
- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
553
+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
544
554
varying vec2 v_texCoord;
545
555
varying vec4 v_hitColor;
546
556
varying vec2 v_centerPx;
@@ -584,7 +594,7 @@ ${this.attributes_
584
594
}
585
595
586
596
return `${ COMMON_HEADER }
587
- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
597
+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
588
598
attribute vec2 a_segmentStart;
589
599
attribute vec2 a_segmentEnd;
590
600
attribute float a_measureStart;
@@ -704,7 +714,7 @@ ${this.attributes_
704
714
}
705
715
706
716
return `${ COMMON_HEADER }
707
- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
717
+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
708
718
varying vec2 v_segmentStart;
709
719
varying vec2 v_segmentEnd;
710
720
varying float v_angleStart;
@@ -884,7 +894,7 @@ ${this.attributes_
884
894
}
885
895
886
896
return `${ COMMON_HEADER }
887
- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
897
+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
888
898
attribute vec2 a_position;
889
899
attribute vec4 a_hitColor;
890
900
@@ -919,7 +929,7 @@ ${this.attributes_
919
929
}
920
930
921
931
return `${ COMMON_HEADER }
922
- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
932
+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
923
933
varying vec4 v_hitColor;
924
934
${ this . attributes_
925
935
. map (
0 commit comments