Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Cg
- #define NUM_SAMPLES 1
- const float RADIUS = 0.0005;
- const float OFFSET = 18.0;
- const float FALLOFF = 0.000002;
- const float STRENGTH = 0.07;
- const float TOTAL_STRENGTH = 1.38;
- const float INVERTED_SAMPLES = 1 / float(NUM_SAMPLES);
- float rand(vec2 co)
- {
- return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
- }
- int randInt(int start, int end)
- {
- return int(fract(sin(dot(vec2(start, end),vec2(12.9898,78.233))) * 43758.5453));
- }
- void vshader(float4 vtx_position : POSITION,
- out float4 l_position : POSITION,
- out float2 l_texcoord : TEXCOORD0,
- out float2 l_texcoordB : TEXCOORD1,
- uniform float4 texpad_buffer,
- uniform float4x4 mat_modelproj)
- {
- l_position = mul(mat_modelproj, vtx_position);
- l_texcoord = vtx_position.xz;
- l_texcoordB = (vtx_position.xz * texpad_buffer.xy) + texpad_buffer.xy;
- }
- void fshader(out float4 o_color : COLOR,
- float2 l_texcoord : TEXCOORD0,
- float2 l_texcoordB : TEXCOORD1,
- uniform sampler2D k_random : TEXUNIT0,
- uniform sampler2D k_buffer : TEXUNIT1)
- {
- float3 random_point[16] = vec3[](vec3(0.53812504, 0.18565957, -0.43192),vec3(0.13790712, 0.24864247, 0.44301823),vec3(0.33715037, 0.56794053, -0.005789503),vec3(-0.6999805, -0.04511441, -0.0019965635),vec3(0.06896307, -0.15983082, -0.85477847),vec3(0.056099437, 0.006954967, -0.1843352),vec3(-0.014653638, 0.14027752, 0.0762037),vec3(0.010019933, -0.1924225, -0.034443386),vec3(-0.35775623, -0.5301969, -0.43581226),vec3(-0.3169221, 0.106360726, 0.015860917),vec3(0.010350345, -0.58698344, 0.0046293875),vec3(-0.08972908, -0.49408212, 0.3287904),vec3(0.7119986, -0.0154690035, -0.09183723),vec3(-0.053382345, 0.059675813, -0.5411899),vec3(0.035267662, -0.063188605, 0.54602677),vec3(-0.47761092, 0.2847911, -0.0271716));
- float3 fres = normalize((tex2D(k_buffer, l_texcoord * OFFSET).xyz * 2.0) - float3(1.0));
- float4 current_pixel = tex2D(k_buffer, l_texcoord);
- float current_pixel_depth = current_pixel.a;
- float3 frag_coords = float3(l_texcoord.xy, current_pixel_depth);
- float3 current_pixel_normal = current_pixel.xyz;
- float occlusion = 0.0;
- float scaled_radius = RADIUS / current_pixel_depth;
- float3 ray;
- float3 occluder_pos;
- float3 occluder_normal;
- float depth_difference;
- float normal_difference;
- for(int sample = 0; sample < NUM_SAMPLES; ++sample)
- {
- ray = scaled_radius * reflect(random_point[sample], fres);
- occluder_pos = frag_coords + sign(dot(ray, current_pixel_normal)) * ray;
- float4 occluder_fragment = tex2D(k_buffer, occluder_pos);
- occluder_normal = occluder_fragment.xyz;
- depth_difference = current_pixel_depth - occluder_fragment.a;
- normal_difference = (1.0 - dot(occluder_normal, current_pixel_normal));
- //normal_difference = 1.0;
- occlusion += step(FALLOFF, depth_difference) * normal_difference * (1.0 - smoothstep(FALLOFF, STRENGTH, depth_difference));
- }
- float ao = 1.0 - TOTAL_STRENGTH * occlusion * INVERTED_SAMPLES;
- o_color.r = ao;
- }
Advertisement
Add Comment
Please, Sign In to add comment