PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/filters/HueRotate.js

https://github.com/rovyoper/phaser
JavaScript | 79 lines | 52 code | 23 blank | 4 comment | 0 complexity | b7af8250169d1226badd5c1b80329ee4 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Original shader by Daniil (https://www.shadertoy.com/view/4sl3DH)
  3. * Tweaked, uniforms added and converted to Phaser/PIXI by Richard Davey
  4. */
  5. Phaser.Filter.HueRotate = function (game) {
  6. Phaser.Filter.call(this, game);
  7. this.uniforms.alpha = { type: '1f', value: 1.0 };
  8. this.uniforms.size = { type: '1f', value: 0.03 };
  9. this.uniforms.iChannel0 = { type: 'sampler2D', value: null, textureData: { repeat: true } };
  10. this.fragmentSrc = [
  11. "precision mediump float;",
  12. "uniform vec2 resolution;",
  13. "uniform float time;",
  14. "uniform float alpha;",
  15. "uniform sampler2D iChannel0;",
  16. "/* Simple hue rotation filter based on article:",
  17. "http://beesbuzz.biz/code/hsv_color_transforms.php",
  18. "*/",
  19. "#define SPEED 10.0",
  20. "void main(void)",
  21. "{",
  22. "vec2 uv = gl_FragCoord.xy / resolution.xy;",
  23. "float c = cos(time * SPEED);",
  24. "float s = sin(time * SPEED);",
  25. "mat4 hueRotation =",
  26. "mat4( 0.299, 0.587, 0.114, 0.0,",
  27. "0.299, 0.587, 0.114, 0.0,",
  28. "0.299, 0.587, 0.114, 0.0,",
  29. "0.000, 0.000, 0.000, 1.0) +",
  30. "mat4( 0.701, -0.587, -0.114, 0.0,",
  31. "-0.299, 0.413, -0.114, 0.0,",
  32. "-0.300, -0.588, 0.886, 0.0,",
  33. "0.000, 0.000, 0.000, 0.0) * c +",
  34. "mat4( 0.168, 0.330, -0.497, 0.0,",
  35. "-0.328, 0.035, 0.292, 0.0,",
  36. "1.250, -1.050, -0.203, 0.0,",
  37. "0.000, 0.000, 0.000, 0.0) * s;",
  38. "vec4 pixel = texture2D(iChannel0, uv);",
  39. "gl_FragColor = pixel * hueRotation;",
  40. "}"
  41. ];
  42. };
  43. Phaser.Filter.HueRotate.prototype = Object.create(Phaser.Filter.prototype);
  44. Phaser.Filter.HueRotate.prototype.constructor = Phaser.Filter.HueRotate;
  45. Phaser.Filter.HueRotate.prototype.init = function (width, height, texture) {
  46. this.setResolution(width, height);
  47. this.uniforms.iChannel0.value = texture;
  48. };
  49. Object.defineProperty(Phaser.Filter.HueRotate.prototype, 'alpha', {
  50. get: function() {
  51. return this.uniforms.alpha.value;
  52. },
  53. set: function(value) {
  54. this.uniforms.alpha.value = value;
  55. }
  56. });