fogBlack.frag 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /////////////////////////start Pd Header
  2. // 2015 Sofy Yuditskaya
  3. // put all the shadertoy constants here for ease of use
  4. uniform vec3 iResolution; // viewport resolution (in pixels)
  5. uniform float iTime; // shader playback time (in seconds)
  6. uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click
  7. uniform float SPHERE_DENSITY;
  8. // changed samplerXX to sampler2D so it would be recognized, you can also change it to samplerCube as needed
  9. // uniform sampler2D iChannel0; // input channel. XX = 2D/Cube
  10. // uniform sampler2D iChannel1; // input channel. XX = 2D/Cube
  11. // uniform sampler2D iChannel2; // input channel. XX = 2D/Cube
  12. // uniform sampler2D iChannel3; // input channel. XX = 2D/Cube
  13. // uniform vec4 iDate; // (year, month, day, time in seconds)
  14. // uniform float iSampleRate; // sound sample rate (i.e., 44100)
  15. void mainImage(out vec4 fragColor, in vec2 fragCoord);
  16. // changelog for Pd:
  17. //running mainImage() from main()
  18. // declared Shadertoy's usual variables above
  19. // declared iChannel0...3 variables individually
  20. void main( )
  21. {
  22. mainImage(gl_FragColor, gl_FragCoord.xy);
  23. }
  24. //paste ShaderToy code below
  25. /////////////////////////end Pd Header
  26. #define MARCH_DIST 2.5
  27. #define MARCH_COUNT 20
  28. #define SCATTER_DEPTH 10
  29. #define SCATTER_DIST 2.5
  30. #define SPHERE_RADIUS 2.
  31. // #define SPHERE_DENSITY 2.5
  32. //#define LIGHT_POS vec3(1.0 * cos(iTime), 10.0, 15.0)
  33. #define LIGHT_COLOR 1.7 * vec4(1.0, 1.0,1., 0.)
  34. #define ABSORPTION_COLOR 0.0028 * vec4(0.1, 0.1, 0.1, 0.5)
  35. #define VOLUME_COLOR 0.045 * vec4(0.5, 0.5, 0.5,0.)
  36. #define NOISE_SCALE 1.2
  37. //#define BG_COLOR 1.3 * vec4(0.05, 0.05, 0.05, 0.0)
  38. #define BG_COLOR 1.3 * vec4(0.0, 0.0, 0.0, 0.0)
  39. float hash(float n)
  40. {
  41. return fract(sin(n)*43758.5453);
  42. }
  43. float noise(in vec3 x)
  44. {
  45. vec3 p = floor(x);
  46. vec3 f = fract(x);
  47. f = f * f * (3.0 - 2.0 * f);
  48. float n = p.x + p.y * 57.0 + 113.0 * p.z;
  49. float res = mix(mix(mix( hash(n+ 0.0), hash(n+ 1.0),f.x),
  50. mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y),
  51. mix(mix( hash(n+113.0), hash(n+114.0),f.x),
  52. mix( hash(n+170.0), hash(n+171.0),f.x),f.y),f.z);
  53. return res;
  54. }
  55. float fbm( vec3 p )
  56. {
  57. float f;
  58. f = 0.5000*noise( p ); p = p*2.02;
  59. f += 0.2500*noise( p ); p = p*2.03;
  60. f += 0.1250*noise( p );
  61. return f;
  62. }
  63. float get_density_at_pos(vec3 p)
  64. {
  65. float d = (SPHERE_RADIUS - length(p)) / SPHERE_RADIUS;
  66. float noise_mult = fbm(NOISE_SCALE * (p + vec3( 0.*iTime, 0.*iTime, 40.*iTime)));
  67. return clamp(noise_mult * SPHERE_DENSITY * d, 0.0, 1.0);
  68. }
  69. //TODO: actual Rayleigh scattering
  70. vec4 get_scatter_color(vec3 p)
  71. {
  72. float absorption = 0.0;
  73. vec2 m = 2.0 * iMouse.xy / iResolution.xy - 1.0;
  74. vec3 light_pos = vec3(20.0 * m, 10.0);
  75. vec3 light_dir = normalize(light_pos - p);
  76. float t = 0.0;
  77. float rd = SCATTER_DIST / float(SCATTER_DEPTH);
  78. for(int i = 0; i < SCATTER_DEPTH; i++)
  79. {
  80. vec3 sp = p + t * light_dir;
  81. float d = get_density_at_pos(sp);
  82. absorption += d;
  83. t+= rd;
  84. }
  85. return clamp(LIGHT_COLOR * VOLUME_COLOR - absorption * ABSORPTION_COLOR, 0.0, 1.0);
  86. }
  87. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  88. {
  89. //1 : retrieve the fragment's coordinates
  90. vec2 uv = ( fragCoord.xy / iResolution.xy ) * 2.0 - 1.0;
  91. //preserve aspect ratio
  92. uv.x *= iResolution.x / iResolution.y;
  93. //2 : camera position and ray direction
  94. vec3 pos = vec3( 0.,0.,-3.);
  95. vec3 dir = normalize( vec3( uv, 1. ) );
  96. vec3 ip;
  97. float t = 0.0;
  98. float density = 0.0;
  99. vec4 march_color = BG_COLOR;
  100. float rd = MARCH_DIST / float(MARCH_COUNT);
  101. for(int i = 0; i < MARCH_COUNT; i++)
  102. {
  103. ip = pos + dir * t;
  104. float d = get_density_at_pos(ip);
  105. density += d;
  106. vec4 c = get_scatter_color(ip);
  107. march_color += density * c;
  108. t += rd;
  109. }
  110. //4 : apply color to this fragment
  111. fragColor = vec4( march_color);
  112. fragColor.a = fragColor.r ;
  113. }