All files / engine/Source/Renderer ShaderStruct.js

100% Statements 10/10
100% Branches 2/2
100% Functions 3/3
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                    20043x 20043x               1x 20821x 20821x             1x 21677x 21677x   13018x     21677x        
/**
 * A utility for dynamically-generating a GLSL struct.
 *
 * @alias ShaderStruct
 * @constructor
 *
 * @see {@link ShaderBuilder}
 * @param {string} name The name of the struct as it will appear in the shader.
 * @example
 * // Generate the struct:
 * //
 * // struct Attributes
 * // {
 * //     vec3 position;
 * //     vec3 normal;
 * //     vec2 texCoord;
 * // };
 * const struct = new ShaderStruct("Attributes");
 * struct.addField("vec3", "position");
 * struct.addField("vec3", "normal");
 * struct.addField("vec2", "texCoord");
 * const generatedLines = struct.generateGlslLines();
 *
 * @private
 */
function ShaderStruct(name) {
  this.name = name;
  this.fields = [];
}
 
/**
 * Add a field to the struct
 * @param {string} type The type of the struct field
 * @param {string} identifier The identifier of the struct field
 */
ShaderStruct.prototype.addField = function (type, identifier) {
  const field = `    ${type} ${identifier};`;
  this.fields.push(field);
};
 
/**
 * Generate a list of lines of GLSL code for use with {@link ShaderBuilder}
 * @return {string[]} The generated GLSL code.
 */
ShaderStruct.prototype.generateGlslLines = function () {
  let fields = this.fields;
  if (fields.length === 0) {
    // GLSL requires structs to have at least one field
    fields = ["    float _empty;"];
  }
 
  return [].concat(`struct ${this.name}`, "{", fields, "};");
};
 
export default ShaderStruct;