DangerOnTheRanger

Untitled

Oct 27th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1. //Cg
  2.  
  3.  
  4. #define NUM_SAMPLES 1
  5.  
  6. const float RADIUS = 0.0005;
  7. const float OFFSET = 18.0;
  8. const float FALLOFF = 0.000002;
  9. const float STRENGTH = 0.07;
  10. const float TOTAL_STRENGTH = 1.38;
  11. const float INVERTED_SAMPLES = 1 / float(NUM_SAMPLES);
  12.  
  13.  
  14. float rand(vec2 co)
  15. {
  16.     return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
  17. }
  18.  
  19.  
  20. int randInt(int start, int end)
  21. {
  22.     return int(fract(sin(dot(vec2(start, end),vec2(12.9898,78.233))) * 43758.5453));
  23. }
  24.  
  25.  
  26. void vshader(float4 vtx_position : POSITION,
  27.              out float4 l_position : POSITION,
  28.              out float2 l_texcoord : TEXCOORD0,
  29.              out float2 l_texcoordB : TEXCOORD1,
  30.              uniform float4 texpad_buffer,
  31.              uniform float4x4 mat_modelproj)
  32. {
  33.    l_position = mul(mat_modelproj, vtx_position);
  34.    l_texcoord = vtx_position.xz;
  35.    l_texcoordB = (vtx_position.xz * texpad_buffer.xy) + texpad_buffer.xy;
  36. }
  37.  
  38.  
  39. void fshader(out float4 o_color : COLOR,
  40.              float2 l_texcoord : TEXCOORD0,
  41.              float2 l_texcoordB : TEXCOORD1,
  42.              uniform sampler2D k_random : TEXUNIT0,
  43.              uniform sampler2D k_buffer : TEXUNIT1)
  44. {
  45.    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));
  46.    float3 fres = normalize((tex2D(k_buffer, l_texcoord * OFFSET).xyz * 2.0) - float3(1.0));
  47.  
  48.    float4 current_pixel = tex2D(k_buffer, l_texcoord);
  49.    float current_pixel_depth = current_pixel.a;
  50.    float3 frag_coords = float3(l_texcoord.xy, current_pixel_depth);
  51.    float3 current_pixel_normal = current_pixel.xyz;
  52.  
  53.    float occlusion = 0.0;
  54.    float scaled_radius = RADIUS / current_pixel_depth;
  55.  
  56.    float3 ray;
  57.    float3 occluder_pos;
  58.    float3 occluder_normal;
  59.    float depth_difference;
  60.    float normal_difference;
  61.  
  62.    for(int sample = 0; sample < NUM_SAMPLES; ++sample)
  63.    {
  64.       ray = scaled_radius * reflect(random_point[sample], fres);
  65.       occluder_pos = frag_coords + sign(dot(ray, current_pixel_normal)) * ray;
  66.  
  67.       float4 occluder_fragment = tex2D(k_buffer, occluder_pos);
  68.       occluder_normal = occluder_fragment.xyz;
  69.  
  70.       depth_difference = current_pixel_depth - occluder_fragment.a;
  71.       normal_difference = (1.0 - dot(occluder_normal, current_pixel_normal));
  72.       //normal_difference = 1.0;
  73.      
  74.       occlusion += step(FALLOFF, depth_difference) * normal_difference * (1.0 - smoothstep(FALLOFF, STRENGTH, depth_difference));
  75.    }
  76.  
  77.    float ao = 1.0 - TOTAL_STRENGTH * occlusion * INVERTED_SAMPLES;
  78.    o_color.r = ao;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment