Home Reference Source

cables_dev/cables/src/core/cgl/cgl_shader_lib.js

  1. import { Uniform } from "./cgl_shader_uniform.js";
  2. import { Texture } from "./cgl_texture.js";
  3. import { TextureEffect } from "./cgl_textureeffect.js";
  4.  
  5. const ShaderLibMods = {
  6. "CGL.BLENDMODES": function ()
  7. {
  8. this.name = "blendmodes";
  9. this.srcHeadFrag = TextureEffect.getBlendCode();
  10. },
  11. "CGL.BLENDMODES3": function ()
  12. {
  13. this.name = "blendmodes3";
  14. this.srcHeadFrag = TextureEffect.getBlendCode(3);
  15. },
  16.  
  17. "CGL.LUMINANCE": function ()
  18. {
  19. this.name = "luminance";
  20. this.srcHeadFrag = "".endl()
  21. + "float cgl_luminance(vec3 c)".endl()
  22. + "{".endl()
  23. + " return dot(vec3(0.2126,0.7152,0.0722),c);".endl()
  24. + "}".endl();
  25. },
  26.  
  27.  
  28. // quite good random numbers, but somehow don't work in ANGLE
  29. "CGL.RANDOM_OLD": function ()
  30. {
  31. this.name = "randomNumber";
  32. this.srcHeadFrag = "".endl()
  33. + "float cgl_random(vec2 co)".endl()
  34. + "{".endl()
  35. + " return fract(sin(dot(co.xy ,vec2(12.9898,4.1414))) * 432758.5453);".endl()
  36. + "}".endl()
  37. + "vec3 cgl_random3(vec2 co)".endl()
  38. + "{".endl()
  39. + " return vec3( cgl_random(co),cgl_random(co+0.5711),cgl_random(co+1.5711));".endl()
  40. + "}";
  41. },
  42.  
  43.  
  44. // low quality generative ranodm numbers
  45. "CGL.RANDOM_LOW": function ()
  46. {
  47. this.name = "randomNumber";
  48. this.srcHeadFrag = "".endl()
  49. + "float cgl_random(vec2 co)".endl()
  50. + "{".endl()
  51. + " return fract(sin(dot(co.xy ,vec2(12.9898,4.1414))) * 358.5453);".endl()
  52. + "}".endl()
  53. + "vec3 cgl_random3(vec2 co)".endl()
  54. + "{".endl()
  55. + " return vec3( cgl_random(co),cgl_random(co+0.5711),cgl_random(co+1.5711));".endl()
  56. + "}";
  57. },
  58.  
  59. // texture based random numbers
  60. "CGL.RANDOM_TEX": function ()
  61. {
  62. this.name = "randomNumbertex";
  63. this.srcHeadFrag = "".endl()
  64. + "UNI sampler2D CGLRNDTEX;".endl()
  65. + "float cgl_random(vec2 co)".endl()
  66. + "{".endl()
  67. + " return texture(CGLRNDTEX,co*5711.0).r;".endl()
  68. + "}".endl()
  69. + "vec3 cgl_random3(vec2 co)".endl()
  70. + "{".endl()
  71. + " return texture(CGLRNDTEX,co*5711.0).rgb;".endl()
  72. + "}";
  73.  
  74. this.initUniforms = function (shader)
  75. {
  76. return [new Uniform(shader, "t", "CGLRNDTEX", 7)];
  77. };
  78.  
  79. this.onBind = function (cgl, shader)
  80. {
  81. Texture.getRandomTexture(cgl);
  82. cgl.setTexture(7, Texture.getRandomTexture(cgl).tex);
  83. };
  84. },
  85. };
  86.  
  87. export { ShaderLibMods };