Core/Uniform.js

  1. /**
  2. * A class representing a uniform.
  3. *
  4. * @property {String} name The name of this uniform. Also the variable name in the shader.
  5. * @property {Number} value The value of this uniform.
  6. * @property {String} type The type of this uniform. Available types: int, int_vec2, int_vec3, int_vec4, int_array, float, float_vec2, float_vec3, float_vec4, float_array, float_mat2, float_mat3, float_mat4.
  7. * @property {Boolean} stale A boolean indicating whether or not this uniform is stale and needs to be updated.
  8. */
  9. Lore.Uniform = class Uniform {
  10. /**
  11. * Creates an instance of Uniform.
  12. * @param {String} name The name of this uniform. Also the variable name in the shader.
  13. * @param {Number} value The value of this uniform.
  14. * @param {String} type The type of this uniform. Available types: int, int_vec2, int_vec3, int_vec4, int_array, float, float_vec2, float_vec3, float_vec4, float_array, float_mat2, float_mat3, float_mat4.
  15. */
  16. constructor(name, value, type) {
  17. this.name = name;
  18. this.value = value;
  19. this.type = type;
  20. this.stale = true;
  21. }
  22. /**
  23. * Set the value of this uniform.
  24. *
  25. * @param {Number} value A number which is valid for the specified type.
  26. */
  27. setValue(value) {
  28. this.value = value;
  29. this.stale = true;
  30. }
  31. /**
  32. * Pushes the uniform to the GPU.
  33. *
  34. * @param {WebGLRenderingContext} gl A WebGL rendering context.
  35. * @param {WebGLUniformLocation} program
  36. * @param {Lore.Uniform} uniform
  37. */
  38. static Set(gl, program, uniform) {
  39. let location = gl.getUniformLocation(program, uniform.name);
  40. if (uniform.type === 'int') {
  41. gl.uniform1i(location, uniform.value);
  42. } else if (uniform.type === 'int_vec2') {
  43. gl.uniform2iv(location, uniform.value);
  44. } else if (uniform.type === 'int_vec3') {
  45. gl.uniform3iv(location, uniform.value);
  46. } else if (uniform.type === 'int_vec4') {
  47. gl.uniform4iv(location, uniform.value);
  48. } else if (uniform.type === 'int_array') {
  49. gl.uniform1iv(location, uniform.value);
  50. } else if (uniform.type === 'float') {
  51. gl.uniform1f(location, uniform.value);
  52. } else if (uniform.type === 'float_vec2') {
  53. gl.uniform2fv(location, uniform.value);
  54. } else if (uniform.type === 'float_vec3') {
  55. gl.uniform3fv(location, uniform.value);
  56. } else if (uniform.type === 'float_vec4') {
  57. gl.uniform4fv(location, uniform.value);
  58. } else if (uniform.type === 'float_array') {
  59. gl.uniform1fv(location, uniform.value);
  60. } else if (uniform.type === 'float_mat2') {
  61. // false, see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/uniformMatrix
  62. gl.uniformMatrix2fv(location, false, uniform.value);
  63. } else if (uniform.type === 'float_mat3') {
  64. gl.uniformMatrix3fv(location, false, uniform.value);
  65. } else if (uniform.type === 'float_mat4') {
  66. gl.uniformMatrix4fv(location, false, uniform.value);
  67. }
  68. // TODO: Add SAMPLER_2D and SAMPLER_CUBE
  69. // Had to set this to true because point sizes did not update...
  70. uniform.stale = true;
  71. }
  72. }