Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

168 řádky
3.8 KiB

  1. define([
  2. 'js/rendering/renderer'/*,
  3. 'picture'*/
  4. ], function (
  5. renderer/*,
  6. picture*/
  7. ) {
  8. const frag = `
  9. varying vec2 vTextureCoord;
  10. uniform sampler2D uSampler;
  11. uniform vec4 targetColor;
  12. void main(void)
  13. {
  14. vec4 source = texture2D(uSampler, vTextureCoord);
  15. //reverse hardlight
  16. if (source.a == 0.0) {
  17. gl_FragColor = vec4(0, 0, 0, 0);
  18. return;
  19. }
  20. //yeah, premultiplied
  21. vec3 Cb = source.rgb/source.a, Cs;
  22. if (targetColor.a > 0.0) {
  23. Cs = targetColor.rgb / targetColor.a;
  24. }
  25. vec3 multiply = Cb * Cs * 2.0;
  26. vec3 Cb2 = Cb * 2.0 - 1.0;
  27. vec3 screen = Cb2 + Cs - Cb2 * Cs;
  28. vec3 B;
  29. if (Cs.r <= 0.5) {
  30. B.r = 0.0;
  31. } else {
  32. B.r = screen.r;
  33. }
  34. if (Cs.g <= 0.5) {
  35. B.g = 0.0;
  36. } else {
  37. B.g = screen.g;
  38. }
  39. if (Cs.b <= 0.5) {
  40. B.b = 0.0;
  41. } else {
  42. B.b = screen.b;
  43. }
  44. vec4 res;
  45. res.xyz = (1.0 - source.a) * Cs + source.a * B;
  46. res.a = source.a + targetColor.a * (1.0-source.a);
  47. gl_FragColor = vec4(res.xyz * res.a, res.a);
  48. }`;
  49. return {
  50. type: 'lightPatch',
  51. color: 'f7ffb2',
  52. patches: [],
  53. rays: [],
  54. init: function (blueprint) {
  55. this.blueprint = this.blueprint || {};
  56. let obj = this.obj;
  57. let x = obj.x;
  58. let y = obj.y;
  59. let maxDistance = Math.sqrt(Math.pow(obj.width / 2, 2) + Math.pow(obj.height / 2, 2));
  60. for (let i = 0; i < obj.width; i++) {
  61. for (let j = 0; j < obj.height; j++) {
  62. let distance = maxDistance - Math.sqrt(Math.pow((obj.width / 2) - i, 2) + Math.pow((obj.width / 2) - i, 2));
  63. let alpha = distance / maxDistance;
  64. let sprite = renderer.buildObject({
  65. x: (x + i),
  66. y: (y + j),
  67. sheetName: 'white',
  68. cell: 0,
  69. layerName: 'lightPatches'
  70. });
  71. sprite.alpha = (0.2 + (Math.random() * 1)) * alpha;
  72. //sprite.tint = '0x' + this.color;
  73. //We assume that target alpha is 1.0 always
  74. let overlayFilter = new PIXI.Filter(undefined, frag, {
  75. targetColor: [0.0, 0.0, 0.0, 1.0]
  76. });
  77. //assign the color to rgb array
  78. //sprite.blendMode = PIXI.BLEND_MODES.OVERLAY;
  79. //sprite.pluginName = 'picture';
  80. sprite.filters = [overlayFilter];
  81. this.patches.push(sprite);
  82. }
  83. }
  84. let rCount = ((obj.width * obj.height) / 10) + ~~(Math.random() + 2);
  85. for (let i = 0; i < rCount; i++) {
  86. let nx = x + 3 + ~~(Math.random() * (obj.width - 1));
  87. let ny = y - 4 + ~~(Math.random() * (obj.height));
  88. let w = 1 + ~~(Math.random() * 2);
  89. let h = 6 + ~~(Math.random() * 13);
  90. let hm = 2;
  91. let rContainer = renderer.buildContainer({
  92. layerName: 'lightBeams'
  93. });
  94. this.rays.push(rContainer);
  95. for (let j = 0; j < h; j++) {
  96. let ray = renderer.buildObject({
  97. x: nx,
  98. y: ny,
  99. cell: 0,
  100. sheetName: 'white',
  101. parent: rContainer
  102. });
  103. ray.x = ~~((nx * scale) - (scaleMult * j));
  104. ray.y = (ny * scale) + (scaleMult * j * hm);
  105. ray.alpha = ((1.0 - (j / h)) * 0.4);
  106. ray.width = w * scaleMult;
  107. ray.height = scaleMult * hm;
  108. ray.tint = 0xffeb38;
  109. ray.blendMode = PIXI.BLEND_MODES.ADD;
  110. }
  111. }
  112. },
  113. update: function () {
  114. let rays = this.rays;
  115. let rLen = rays.length;
  116. for (let i = 0; i < rLen; i++) {
  117. let r = rays[i];
  118. r.alpha += (Math.random() * 0.03) - 0.015;
  119. if (r.alpha < 0.3)
  120. r.alpha = 0.3;
  121. else if (r.alpha > 1)
  122. r.alpha = 1;
  123. }
  124. },
  125. setVisible: function (visible) {
  126. this.patches.forEach(function (p) {
  127. p.visible = visible;
  128. });
  129. this.rays.forEach(function (r) {
  130. r.visible = visible;
  131. });
  132. },
  133. destroy: function () {
  134. this.patches.forEach(function (p) {
  135. p.parent.removeChild(p);
  136. });
  137. this.patches = [];
  138. this.rays.forEach(function (r) {
  139. r.parent.removeChild(r);
  140. });
  141. this.rays = [];
  142. }
  143. };
  144. });