123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /////////////////////////start Pd Header
- // 2015 Sofy Yuditskaya
- // put all the shadertoy constants here for ease of use
- uniform vec3 iResolution; // viewport resolution (in pixels)
- uniform float iTime; // shader playback time (in seconds)
- uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click
- uniform float SPHERE_DENSITY;
- // changed samplerXX to sampler2D so it would be recognized, you can also change it to samplerCube as needed
- // uniform sampler2D iChannel0; // input channel. XX = 2D/Cube
- // uniform sampler2D iChannel1; // input channel. XX = 2D/Cube
- // uniform sampler2D iChannel2; // input channel. XX = 2D/Cube
- // uniform sampler2D iChannel3; // input channel. XX = 2D/Cube
- // uniform vec4 iDate; // (year, month, day, time in seconds)
- // uniform float iSampleRate; // sound sample rate (i.e., 44100)
- void mainImage(out vec4 fragColor, in vec2 fragCoord);
- // changelog for Pd:
- //running mainImage() from main()
- // declared Shadertoy's usual variables above
- // declared iChannel0...3 variables individually
- void main( )
- {
- mainImage(gl_FragColor, gl_FragCoord.xy);
- }
- //paste ShaderToy code below
- /////////////////////////end Pd Header
- #define MARCH_DIST 2.5
- #define MARCH_COUNT 20
- #define SCATTER_DEPTH 10
- #define SCATTER_DIST 2.5
- #define SPHERE_RADIUS 2.
- // #define SPHERE_DENSITY 2.5
- //#define LIGHT_POS vec3(1.0 * cos(iTime), 10.0, 15.0)
- #define LIGHT_COLOR 1.7 * vec4(1.0, 1.0,1., 0.)
- #define ABSORPTION_COLOR 0.0028 * vec4(0.1, 0.1, 0.1, 0.5)
- #define VOLUME_COLOR 0.045 * vec4(0.5, 0.5, 0.5,0.)
- #define NOISE_SCALE 1.2
- //#define BG_COLOR 1.3 * vec4(0.05, 0.05, 0.05, 0.0)
- #define BG_COLOR 1.3 * vec4(0.0, 0.0, 0.0, 0.0)
- float hash(float n)
- {
- return fract(sin(n)*43758.5453);
- }
- float noise(in vec3 x)
- {
- vec3 p = floor(x);
- vec3 f = fract(x);
- f = f * f * (3.0 - 2.0 * f);
-
- float n = p.x + p.y * 57.0 + 113.0 * p.z;
- float res = mix(mix(mix( hash(n+ 0.0), hash(n+ 1.0),f.x),
- mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y),
- mix(mix( hash(n+113.0), hash(n+114.0),f.x),
- mix( hash(n+170.0), hash(n+171.0),f.x),f.y),f.z);
- return res;
- }
- float fbm( vec3 p )
- {
- float f;
- f = 0.5000*noise( p ); p = p*2.02;
- f += 0.2500*noise( p ); p = p*2.03;
- f += 0.1250*noise( p );
- return f;
- }
- float get_density_at_pos(vec3 p)
- {
- float d = (SPHERE_RADIUS - length(p)) / SPHERE_RADIUS;
- float noise_mult = fbm(NOISE_SCALE * (p + vec3( 0.*iTime, 0.*iTime, 40.*iTime)));
- return clamp(noise_mult * SPHERE_DENSITY * d, 0.0, 1.0);
- }
- //TODO: actual Rayleigh scattering
- vec4 get_scatter_color(vec3 p)
- {
- float absorption = 0.0;
- vec2 m = 2.0 * iMouse.xy / iResolution.xy - 1.0;
- vec3 light_pos = vec3(20.0 * m, 10.0);
- vec3 light_dir = normalize(light_pos - p);
-
- float t = 0.0;
- float rd = SCATTER_DIST / float(SCATTER_DEPTH);
-
- for(int i = 0; i < SCATTER_DEPTH; i++)
- {
- vec3 sp = p + t * light_dir;
- float d = get_density_at_pos(sp);
- absorption += d;
- t+= rd;
- }
-
-
- return clamp(LIGHT_COLOR * VOLUME_COLOR - absorption * ABSORPTION_COLOR, 0.0, 1.0);
- }
- void mainImage( out vec4 fragColor, in vec2 fragCoord )
- {
- //1 : retrieve the fragment's coordinates
- vec2 uv = ( fragCoord.xy / iResolution.xy ) * 2.0 - 1.0;
- //preserve aspect ratio
- uv.x *= iResolution.x / iResolution.y;
- //2 : camera position and ray direction
- vec3 pos = vec3( 0.,0.,-3.);
- vec3 dir = normalize( vec3( uv, 1. ) );
- vec3 ip;
- float t = 0.0;
- float density = 0.0;
- vec4 march_color = BG_COLOR;
- float rd = MARCH_DIST / float(MARCH_COUNT);
- for(int i = 0; i < MARCH_COUNT; i++)
- {
- ip = pos + dir * t;
- float d = get_density_at_pos(ip);
- density += d;
- vec4 c = get_scatter_color(ip);
- march_color += density * c;
- t += rd;
- }
-
- //4 : apply color to this fragment
- fragColor = vec4( march_color);
- fragColor.a = fragColor.r ;
- }
|