Source: shaders/point_shader.js

  1. /*
  2. * File: point_shader.js
  3. * for particle engine
  4. */
  5. "use strict";
  6. import * as glSys from "../core/gl.js";
  7. import * as vertexBuffer from "../core/vertex_buffer.js";
  8. import SimpleShader from "./simple_shader.js";
  9. class PointShader extends SimpleShader {
  10. /**
  11. * @constructor PointShader
  12. * @param {string} vertexShaderPath - Path to the vertex shader
  13. * @param {string} fragmentShaderPath - Path to the fragment shader
  14. */
  15. constructor(vertexShaderPath, fragmentShaderPath) {
  16. super(vertexShaderPath, fragmentShaderPath);
  17. let gl = glSys.get();
  18. this.mPointSizeRef = null; // reference to the PointSize uniform
  19. // point size uniform
  20. this.mPointSizeRef = gl.getUniformLocation(this.mCompiledShader, "uPointSize");
  21. this.mPointSize = 1;
  22. }
  23. /**
  24. * @function activate() - Activates the shader for rendering
  25. * @param {vec4} pixelColor - The color that is referenced in the fragment shader
  26. * @param {mat4} trsMatrix - The transform matrix
  27. * @param {mat4} cameraMatrix - The camera matrix
  28. */
  29. // Activate the shader for rendering
  30. activate(pixelColor, trsMatrix, cameraMatrix) {
  31. // first call the super class' activate
  32. super.activate(pixelColor, trsMatrix, cameraMatrix);
  33. // now our own functionality: load the pixel size
  34. let gl = glSys.get();
  35. gl.uniform1f(this.mPointSizeRef, this.mPointSize);
  36. // re-bind the vertex position attribute to the pixel's buffer
  37. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.getPointVertexBuffer());
  38. gl.vertexAttribPointer(this.mVertexPositionRef, // this is defined in SimpleShader
  39. 3, // each element is a 3-float (x,y.z)
  40. gl.FLOAT, // data type is FLOAT
  41. false, // if the content is normalized vectors
  42. 0, // number of bytes to skip in between elements
  43. 0);
  44. gl.enableVertexAttribArray(this.mVertexPositionRef);
  45. }
  46. /**
  47. * @function setPointSize() - sets the pixel size to allow resizing
  48. * @param {float} w - the new pixel size
  49. */
  50. setPointSize(w) {
  51. this.mPointSize = w;
  52. }
  53. }
  54. export default PointShader;