Source: primitives/PostEffect.js

  1. /**
  2. * @function createPostEffect
  3. * @memberof SQR.Primitives
  4. *
  5. * @description Creates a post-processing effect (such as SAO or depth-of-field). It creates
  6. * an instance of SQR.Transform with a full screen quad buffer and the shader build from the provided source.
  7. * Please read the {@tutorial post-effects} tutorial to see how it works.
  8. *
  9. * @param {string} shaderSource - the source of the shader for this post effect
  10. * @param {Object=} shaderOptions - options for the shader. Same as in the {@link SQR.Shader} constructor
  11. *
  12. * @returns {SQR.Transform} a transform representing this post effect
  13. */
  14. SQR.Primitives.createPostEffect = function(shaderSource, shaderOptions, fullScreenQuad) {
  15. // SQR.fullScreenQuad = SQR.fullScreenQuad || SQR.Buffer()
  16. fullScreenQuad = fullScreenQuad || SQR.Buffer()
  17. .layout(SQR.v2u2(), 6)
  18. .data('aPosition', -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1)
  19. .data('aUV', 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0)
  20. .update();
  21. var pe = new SQR.Transform('post-effect');
  22. // pe.buffer = SQR.fullScreenQuad;
  23. pe.buffer = fullScreenQuad;
  24. if(shaderSource) pe.shader = SQR.Shader(shaderSource, shaderOptions);
  25. return pe;
  26. }
  27. // SQR.Primitives.createImage = function(img, mode, shaderSource, shaderOptions) {
  28. // if(!shaderSource && !SQR.GLSL) throw '> SQR.Primitives.createImage > sqr-glsl.js package is required to use this feature.';
  29. // shaderSource = shaderSource || SQR.GLSL['post/image.glsl'];
  30. // var pe = new SQR.Transform();
  31. // pe.buffer = SQR.Buffer()
  32. // .layout(SQR.v2u2(), 6)
  33. // .data('aPosition', -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1)
  34. // .data('aUV', 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0)
  35. // .update();
  36. // var image = img;
  37. // var texture = SQR.Texture(image);
  38. // pe.shader = SQR.Shader(shaderSource, shaderOptions);
  39. // pe.setImage = function(img) {
  40. // image = img;
  41. // texture.setSource(img).update();
  42. // pe.shader.use().setUniform('uTexture', texture);
  43. // }
  44. // pe.setImage(image);
  45. // pe.size = function(w, h) {
  46. // var xl = -1, yt = 1, xr = 1, yb = -1;
  47. // var iw = image.width, ih = image.height;
  48. // var fw = iw / ih * h;
  49. // var fh = ih / iw * w;
  50. // if(mode == 'fit') {
  51. // if(fw > w) {
  52. // yb = -(fh / h);
  53. // yt = (fh / h);
  54. // }
  55. // if(fh > h) {
  56. // xl = -(fw / w);
  57. // xr = (fw / w);
  58. // }
  59. // } else if(mode == 'cover') {
  60. // if(fw > w) {
  61. // xl = -(fw / w);
  62. // xr = (fw / w);
  63. // }
  64. // if(fh > h) {
  65. // yb = -(fh / h);
  66. // yt = (fh / h);
  67. // }
  68. // }
  69. // pe.buffer.data('aPosition',
  70. // xl, yt,
  71. // xr, yt,
  72. // xr, yb,
  73. // xl, yt,
  74. // xr, yb,
  75. // xl, yb
  76. // ).update();
  77. // return pe;
  78. // }
  79. // return pe;
  80. // }