All files / engine/Source/Renderer ShaderFunction.js

100% Statements 14/14
100% Branches 6/6
100% Functions 3/3
100% Lines 13/13

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 57 58 59 60 61 62 63 64 65 66 67                                                      21762x 21762x             1x   13604x 2x         13602x     13602x 13599x 13599x 20630x       3x               1x 21587x        
import DeveloperError from "../Core/DeveloperError.js";
 
/**
 * A utility for dynamically-generating a GLSL function
 *
 * @alias ShaderFunction
 * @constructor
 *
 * @see {@link ShaderBuilder}
 * @param {string} signature The full signature of the function as it will appear in the shader. Do not include the curly braces.
 * @example
 * // generate the following function
 * //
 * // void assignVaryings(vec3 position)
 * // {
 * //    v_positionEC = (czm_modelView * vec4(a_position, 1.0)).xyz;
 * //    v_texCoord = a_texCoord;
 * // }
 * const signature = "void assignVaryings(vec3 position)";
 * const func = new ShaderFunction(signature);
 * func.addLine("v_positionEC = (czm_modelView * vec4(a_position, 1.0)).xyz;");
 * func.addLine("v_texCoord = a_texCoord;");
 * const generatedLines = func.generateGlslLines();
 *
 * @private
 */
function ShaderFunction(signature) {
  this.signature = signature;
  this.body = [];
}
 
/**
 * Adds one or more lines to the body of the function
 * @param {string|string[]} lines One or more lines of GLSL code to add to the function body. Do not include any preceding or ending whitespace, but do include the semicolon for each line.
 */
ShaderFunction.prototype.addLines = function (lines) {
  //>>includeStart('debug', pragmas.debug);
  if (typeof lines !== "string" && !Array.isArray(lines)) {
    throw new DeveloperError(
      `Expected lines to be a string or an array of strings, actual value was ${lines}`,
    );
  }
  //>>includeEnd('debug');
  const body = this.body;
 
  // Indent the body of the function by 4 spaces
  if (Array.isArray(lines)) {
    const length = lines.length;
    for (let i = 0; i < length; i++) {
      body.push(`    ${lines[i]}`);
    }
  } else {
    // Single string case
    body.push(`    ${lines}`);
  }
};
 
/**
 * Generate lines of GLSL code for use with {@link ShaderBuilder}
 * @return {string[]}
 */
ShaderFunction.prototype.generateGlslLines = function () {
  return [].concat(this.signature, "{", this.body, "}");
};
 
export default ShaderFunction;