Advertisement
Guest User

ssao2

a guest
Jul 21st, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. // Volumetric SSAO
  2. // Implemented by Tomerk
  3. // Optimized by Ethatron
  4.  
  5. // ---------------------------------------
  6. // TWEAKABLE VARIABLES.
  7.  
  8.  
  9. #undef TEST_MODE
  10. // testMode. set to 1, you can see the raw ssao
  11.  
  12. #define LUMINANCE_CONSIDERATION
  13. float luminosity_threshold = 0.3;
  14. // comment this line to not take pixel brightness into account
  15.  
  16. #define N_SAMPLES 9
  17. // number of samples, currently do not change.
  18.  
  19. float aoRadiusMultiplier = 4;
  20. // Linearly multiplies the radius of the AO Sampling
  21.  
  22. float aoStrengthMultiplier = 1.0;
  23. // Linearly multiplies the strength of the AO
  24.  
  25. float aoClamp = 0.0;
  26. // The maximum strength of the AO, 0 is max strength, 1 is weakest
  27.  
  28. float ThicknessModel = 100;
  29. // units in space the AO assumes objects' thicknesses are
  30.  
  31.  
  32. // END OF TWEAKABLE VARIABLES.
  33. // ---------------------------------------
  34.  
  35. texture2D noise;
  36. sampler2D PassSamplerL34 = sampler_state
  37. {
  38. texture = <obge_LastRendertarget0_EFFECTPASS>;
  39.  
  40. AddressU = CLAMP;
  41. AddressV = CLAMP;
  42.  
  43. MINFILTER = LINEAR;
  44. MAGFILTER = LINEAR;
  45. MIPFILTER = NONE;
  46. };
  47.  
  48. struct VSOUTb
  49. {
  50. float4 vertPos : POSITION;
  51. float2 UVCoord : TEXCOORD0;
  52. };
  53.  
  54. struct VSINb
  55. {
  56. float4 vertPos : POSITION0;
  57. float2 UVCoord : TEXCOORD0;
  58. };
  59.  
  60. VSOUTb FrameVSm(VSIN IN)
  61. {
  62. VSOUT OUT = (VSOUT)0.0f; // initialize to zero, avoid complaints.
  63.  
  64. OUT.vertPos = IN.vertPos;
  65. OUT.UVCoord = IN.UVCoord;
  66.  
  67. return OUT;
  68. }
  69.  
  70. static const float2 sample_offsetm[N_SAMPLES] =
  71. {
  72. //#if N_SAMPLES >= 9
  73. float2(-0.1376476f, 0.2842022f ),
  74. float2(-0.626618f , 0.4594115f ),
  75. float2(-0.8903138f, -0.05865424f),
  76. float2( 0.2871419f, 0.8511679f ),
  77. float2(-0.1525251f, -0.3870117f ),
  78. float2( 0.6978705f, -0.2176773f ),
  79. float2( 0.7343006f, 0.3774331f ),
  80. float2( 0.1408805f, -0.88915f ),
  81. float2(-0.6642616f, -0.543601f )
  82. //#endif
  83. };
  84.  
  85. static const float sample_radiusm[N_SAMPLES] =
  86. {
  87. //#if N_SAMPLES >= 9
  88. 0.948832,
  89. 0.629516,
  90. 0.451554,
  91. 0.439389,
  92. 0.909372,
  93. 0.682344,
  94. 0.5642,
  95. 0.4353,
  96. 0.5130
  97. //#endif
  98. };
  99.  
  100. float4 Occlusionb(VSOUTb IN) : COLOR0 {
  101. float3 sample = tex2D(PassSamplerL, IN.UVCoord).rgb;
  102. float depth = (IN.UVCoord);
  103.  
  104. [branch]
  105. if (depth >= 0.99)
  106. return float4(sample, 1.0);
  107.  
  108. float3 pos = (IN.UVCoord, depth);
  109. float3 dx = ddx(pos);
  110. float3 dy = ddy(pos);
  111. float3 norm = normalize(cross(dx, dy));
  112. norm.y *= -1;
  113.  
  114. float sample_depth;
  115.  
  116. float ao = 0;
  117. float s = 0.0;
  118.  
  119. float2 rand_vec = (IN.UVCoord);
  120. float2 sample_vec_divisor = g_InvFocalLen * depth * 2.0 / (aoRadiusMultiplier * 5000 * rcpres);
  121. float2 sample_center = IN.UVCoord + norm.xy / sample_vec_divisor * float2(1, aspect);
  122. float sample_center_depth = depth * 1.0 + norm.z * aoRadiusMultiplier * 10;
  123.  
  124. [unroll]
  125. for (int i = 0; i < N_SAMPLES; i++) {
  126. float2 sample_vec = reflect(sample_offset[i], rand_vec) / sample_vec_divisor;
  127. float2 sample_coords = sample_center + sample_vec * float2(1, aspect);
  128.  
  129. float curr_sample_radius = sample_radius[i] * aoRadiusMultiplier * 10;
  130. float curr_sample_depth = 3.0 * (sample_coords);
  131.  
  132. ao += clamp(0, curr_sample_radius + sample_center_depth - curr_sample_depth , 2 * curr_sample_radius);
  133. ao -= clamp(0, curr_sample_radius + sample_center_depth - curr_sample_depth - ThicknessModel, 2 * curr_sample_radius);
  134.  
  135. s += 2 * curr_sample_radius;
  136. }
  137.  
  138. ao /= s;
  139. ao *= (depth);
  140. ao = 1.0 - ao * aoStrengthMultiplier;
  141.  
  142. return float4(sample, ao);
  143. }
  144.  
  145. float4 BlurNCombine(VSOUT IN) : COLOR0 {
  146. float4 sample = tex2D(PassSamplerL, IN.UVCoord).rgba;
  147. float3 color = sample.rgb;
  148. float ao = sample.a * 4;
  149.  
  150. ao += tex2D(PassSamplerL, IN.UVCoord + float2( rcpres.x, 0)).a * 2;
  151. ao += tex2D(PassSamplerL, IN.UVCoord + float2(-rcpres.x, 0)).a * 2;
  152. ao += tex2D(PassSamplerL, IN.UVCoord + float2(0, rcpres.y)).a * 2;
  153. ao += tex2D(PassSamplerL, IN.UVCoord + float2(0, -rcpres.y)).a * 2;
  154.  
  155. ao += tex2D(PassSamplerL, IN.UVCoord + rcpres ).a;
  156. ao += tex2D(PassSamplerL, IN.UVCoord - rcpres ).a;
  157. ao += tex2D(PassSamplerL, IN.UVCoord + rcpres * float2(1, -1)).a;
  158. ao += tex2D(PassSamplerL, IN.UVCoord - rcpres * float2(1, -1)).a;
  159.  
  160. ao /= 16;
  161.  
  162. #ifdef LUMINANCE_CONSIDERATION
  163. float luminance = (color);
  164. float white = 1.0;
  165. float black = 0;
  166.  
  167. luminance = clamp(
  168. max(black, luminance - luminosity_threshold) +
  169. max(black, luminance - luminosity_threshold) +
  170. max(black, luminance - luminosity_threshold), 0.0, 1.0);
  171.  
  172. ao = lerp(ao, white, luminance);
  173. #endif
  174.  
  175. #ifdef TEST_MODE
  176. return ao;
  177. #endif
  178.  
  179. return float4(color * ao, 1);
  180. }
  181.  
  182. technique t065
  183. <
  184. int group = 1.0;
  185. int fxclass = 2.0;
  186. int conditions = 2.0;
  187. >
  188. {
  189. pass {
  190. VertexShader = compile vs_3_0 FrameVS();
  191. PixelShader = compile ps_3_0 Occlusion();
  192. }
  193.  
  194. pass {
  195. VertexShader = compile vs_3_0 FrameVS();
  196. PixelShader = compile ps_3_0 BlurNCombine();
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement