fog.frag 4.3 KB

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