Advertisement
Guest User

Untitled

a guest
Mar 6th, 2023
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. let shader = `
  2. // Feel free to steal this :^)
  3. // Consider it MIT licensed, you can link to this page if you want to.
  4. uniform float iTime;
  5. vec2 iResolution = vec2(1000.0); //<---------- Kinda the graininess/rng seed PLAY WITH THIS
  6.  
  7. #define SHOW_NOISE 0
  8. #define SRGB 0
  9. // 0: Addition, 1: Screen, 2: Overlay, 3: Soft Light, 4: Lighten-Only
  10. #define BLEND_MODE 0
  11.  
  12. //0 to 1
  13. #define SPEED 0.1
  14.  
  15. #define INTENSITY 0.05
  16. // What gray level noise should tend to.
  17. #define MEAN 0.0
  18.  
  19. // Controls the contrast/variance of noise.
  20. #define VARIANCE 0.5
  21.  
  22. vec3 channel_mix(vec3 a, vec3 b, vec3 w) {
  23. return vec3(mix(a.r, b.r, w.r), mix(a.g, b.g, w.g), mix(a.b, b.b, w.b));
  24. }
  25.  
  26. float gaussian(float z, float u, float o) {
  27. return (1.0 / (o * sqrt(2.0 * 3.1415))) * exp(-(((z - u) * (z - u)) / (2.0 * (o * o))));
  28. }
  29.  
  30. vec3 madd(vec3 a, vec3 b, float w) {
  31. return a + a * b * w;
  32. }
  33.  
  34. vec3 screen(vec3 a, vec3 b, float w) {
  35. return mix(a, vec3(1.0) - (vec3(1.0) - a) * (vec3(1.0) - b), w);
  36. }
  37.  
  38. vec3 overlay(vec3 a, vec3 b, float w) {
  39. return mix(a, channel_mix(
  40. 2.0 * a * b,
  41. vec3(1.0) - 2.0 * (vec3(1.0) - a) * (vec3(1.0) - b),
  42. step(vec3(0.5), a)
  43. ), w);
  44. }
  45.  
  46. vec3 soft_light(vec3 a, vec3 b, float w) {
  47. return mix(a, pow(a, pow(vec3(2.0), 2.0 * (vec3(0.5) - b))), w);
  48. }
  49.  
  50. void main() {
  51. vec2 coord = gl_FragCoord.xy;
  52. vec2 ps = vec2(1.0) / iResolution.xy;
  53. vec2 uv = coord * ps;
  54. vec4 color = vec4(0.);//texture(iChannel0, uv);
  55. #if SRGB
  56. color = pow(color, vec4(2.2));
  57. #endif
  58.  
  59. float t = iTime * float(SPEED);
  60. float seed = dot(uv, vec2(12.9898, 78.233));
  61. float noise = fract(sin(seed) * 43758.5453 + t);
  62. noise = gaussian(noise, float(MEAN), float(VARIANCE) * float(VARIANCE));
  63.  
  64. #if SHOW_NOISE
  65. color = vec4(noise);
  66. #else
  67. // Ignore these mouse stuff if you're porting this
  68. // and just use an arbitrary intensity value.
  69. float w = float(INTENSITY);
  70.  
  71. vec3 grain = vec3(noise) * (1.0 - color.rgb);
  72.  
  73.  
  74.  
  75. #if BLEND_MODE == 0
  76. color.rgb += grain * w;
  77. #elif BLEND_MODE == 1
  78. color.rgb = screen(color.rgb, grain, w);
  79. #elif BLEND_MODE == 2
  80. color.rgb = overlay(color.rgb, grain, w);
  81. #elif BLEND_MODE == 3
  82. color.rgb = soft_light(color.rgb, grain, w);
  83. #elif BLEND_MODE == 4
  84. color.rgb = max(color.rgb, grain * w);
  85. #endif
  86.  
  87. #if SRGB
  88. color = pow(color, vec4(1.0 / 2.2));
  89. #endif
  90. #endif
  91. gl_FragColor = vec4(1.,1.,1.,color.r); //<---------- Kinda like tint PLAY WITH THIS
  92. }
  93. `;
  94.  
  95. export default class GrainShader {
  96. constructor(THREE) {
  97. this.grainShader = new THREE.ShaderMaterial({
  98. fragmentShader: shader,
  99. transparent: true, //<---------- Toggling this does Weird. PLAY WITH THIS disable/enable etc.
  100. depthTest:false,
  101. depthWrite:false,
  102. blending:THREE.AdditiveBlending, //<---------- Blend mode PLAY WITH THIS disable/enable etc.
  103. uniforms:{
  104. iTime:{value:0}
  105. }
  106. }
  107. );
  108. this.update=()=>{
  109. this.grainShader.uniforms.iTime.value = performance.now()/1000.; //<---------- Time scale PLAY WITH THIS
  110. }
  111. }
  112. }
  113.  
  114. export function makeItGrain( THREE, camera, bsz = 2.5 ) {
  115. let gs = new GrainShader(THREE);
  116. //gs.grainShader.depthFunc = THREE.GreaterEqualDepth;
  117. gs.grainShader.depthTest = false;
  118. gs.grainShader.side = THREE.DoubleSide;//BackSide;
  119. let m = new THREE.Mesh(new THREE.PlaneGeometry(1,1),gs.grainShader);
  120. m.position.z = camera.near * -1.001;
  121. //m.rotation.x=Math.PI*.5
  122. m.onBeforeRender = function(){
  123. gs.grainShader.uniforms.iTime.value = performance.now()*.001;
  124. }
  125. camera.add(m)
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement