@@ -34,11 +34,17 @@ const DEFAULT_STYLE = createDefaultStyle();
3434
3535/**
3636 * @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_)
4040 * @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`...
4248 */
4349
4450/**
@@ -48,8 +54,8 @@ const DEFAULT_STYLE = createDefaultStyle();
4854 *
4955 * ```js
5056 * 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 )
5359 * .setColorExpression('...')
5460 * .setSymbolSizeExpression('...')
5561 * .getSymbolFragmentShader();
@@ -63,7 +69,7 @@ export class ShaderBuilder {
6369 constructor ( ) {
6470 /**
6571 * Uniforms; these will be declared in the header (should include the type).
66- * @type {Array<string > }
72+ * @type {Array<UniformDescription > }
6773 * @private
6874 */
6975 this . uniforms_ = [ ] ;
@@ -202,33 +208,37 @@ export class ShaderBuilder {
202208 /**
203209 * Adds a uniform accessible in both fragment and vertex shaders.
204210 * 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
206213 * @return {ShaderBuilder } the builder object
207214 */
208- addUniform ( name ) {
209- this . uniforms_ . push ( name ) ;
215+ addUniform ( name , type ) {
216+ this . uniforms_ . push ( {
217+ name,
218+ type,
219+ } ) ;
210220 return this ;
211221 }
212222
213223 /**
214224 * Adds an attribute accessible in the vertex shader, read from the geometry buffer.
215225 * The given name should include a type, such as `vec2 a_position`.
216226 * 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
220230 * 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;
222232 * e.g. `vec4` after unpacking color components
223233 * @return {ShaderBuilder } the builder object
224234 */
225- addAttribute ( name , type , transform , transformedType ) {
235+ addAttribute ( name , type , varyingExpression , varyingType ) {
226236 this . attributes_ . push ( {
227237 name,
228238 type,
229239 varyingName : name . replace ( / ^ a _ / , 'v_' ) ,
230- varyingType : transformedType ?? type ,
231- varyingExpression : transform ?? name ,
240+ varyingType : varyingType ?? type ,
241+ varyingExpression : varyingExpression ?? name ,
232242 } ) ;
233243 return this ;
234244 }
@@ -463,7 +473,7 @@ export class ShaderBuilder {
463473 }
464474
465475 return `${ COMMON_HEADER }
466- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
476+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
467477attribute vec2 a_position;
468478attribute float a_index;
469479attribute vec4 a_hitColor;
@@ -540,7 +550,7 @@ ${this.attributes_
540550 }
541551
542552 return `${ COMMON_HEADER }
543- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
553+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
544554varying vec2 v_texCoord;
545555varying vec4 v_hitColor;
546556varying vec2 v_centerPx;
@@ -584,7 +594,7 @@ ${this.attributes_
584594 }
585595
586596 return `${ COMMON_HEADER }
587- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
597+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
588598attribute vec2 a_segmentStart;
589599attribute vec2 a_segmentEnd;
590600attribute float a_measureStart;
@@ -704,7 +714,7 @@ ${this.attributes_
704714 }
705715
706716 return `${ COMMON_HEADER }
707- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
717+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
708718varying vec2 v_segmentStart;
709719varying vec2 v_segmentEnd;
710720varying float v_angleStart;
@@ -884,7 +894,7 @@ ${this.attributes_
884894 }
885895
886896 return `${ COMMON_HEADER }
887- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
897+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
888898attribute vec2 a_position;
889899attribute vec4 a_hitColor;
890900
@@ -919,7 +929,7 @@ ${this.attributes_
919929 }
920930
921931 return `${ COMMON_HEADER }
922- ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform } ;` ) . join ( '\n' ) }
932+ ${ this . uniforms_ . map ( ( uniform ) => `uniform ${ uniform . type } ${ uniform . name } ;` ) . join ( '\n' ) }
923933varying vec4 v_hitColor;
924934${ this . attributes_
925935 . map (
0 commit comments