Advertisement
Guest User

ssao3

a guest
Jul 21st, 2012
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. // By Christian Jakobsen
  2. // Ported by Ethatron
  3.  
  4. // ---------------------------------------
  5. // TWEAKABLE VARIABLES.
  6.  
  7.  
  8. #undef TEST_MODE
  9. // testMode. set to 1, you can see the raw ssao
  10.  
  11. float distance = 0.0025;
  12.  
  13.  
  14. // END OF TWEAKABLE VARIABLES.
  15. // ---------------------------------------
  16.  
  17. // shader codes begin here
  18. texture2D s24;
  19. sampler2D PassSamplerL3 = sampler_state
  20. {
  21. texture = <obge_LastRendertarget0_EFFECTPASS>;
  22.  
  23. AddressU = CLAMP;
  24. AddressV = CLAMP;
  25.  
  26. MINFILTER = LINEAR;
  27. MAGFILTER = LINEAR;
  28. MIPFILTER = LINEAR;
  29. };
  30.  
  31. texture2D cust_NoiseTexture < string filename = "Noise.dds"; >;
  32. sampler2D NoiseSampler = sampler_state {
  33. texture = <cust_NoiseTexture>;
  34.  
  35. AddressU = WRAP;
  36. AddressV = WRAP;
  37.  
  38. MINFILTER = LINEAR;
  39. MAGFILTER = LINEAR;
  40. MIPFILTER = LINEAR;
  41. };
  42.  
  43. struct VSINv
  44. {
  45. float4 vertPos : POSITION0;
  46. float2 UVCoord : TEXCOORD0;
  47. };
  48.  
  49. struct VSOUTv
  50. {
  51. float4 vertPos : POSITION;
  52. float2 UVCoord : TEXCOORD0;
  53. };
  54.  
  55. VSOUT FrameVSb(VSIN IN) {
  56. VSOUT OUT = (VSOUT)0.0f; // initialize to zero, avoid complaints.
  57.  
  58. OUT.vertPos = IN.vertPos;
  59. OUT.UVCoord = IN.UVCoord;
  60.  
  61. return OUT;
  62. }
  63.  
  64. float4 Occlusion(VSOUT IN) : COLOR0 {
  65. float3 sample = tex2D(PassSamplerL, IN.UVCoord).rgb;
  66.  
  67. #define screenTC IN.UVCoord
  68. #define screenSize (IN.UVCoord)
  69. #define sSceneDepthSampler DpthSampler
  70. #define sRandVectSampler NoiseSampler
  71.  
  72. //Tile the texture coordinates
  73. float2 rotationTC = screenTC * screenSize / 4.0f;
  74. //Sample a random vector and transform it into [-1, 1] range
  75. float3 vRotation = 2.0f * tex2Dlod(sRandVectSampler, float4(rotationTC, 0, 0)).rgb - 1.0f;
  76. //Create rotation matrix
  77. float3x3 matRotate;
  78.  
  79. float offsetScale = distance;
  80.  
  81. float h = 1.0f / (1.0f + vRotation.z);
  82.  
  83. matRotate._m00 = h * vRotation.y * vRotation.y + vRotation.z;
  84. matRotate._m01 = -h * vRotation.y * vRotation.x;
  85. matRotate._m02 = -vRotation.x;
  86. matRotate._m10 = -h * vRotation.y * vRotation.x;
  87. matRotate._m11 = h * vRotation.x * vRotation.x + vRotation.z;
  88. matRotate._m12 = -vRotation.y;
  89. matRotate._m20 = vRotation.x;
  90. matRotate._m21 = vRotation.y;
  91. matRotate._m22 = vRotation.z;
  92.  
  93. //Specify number of samples
  94. const int nSamplesNum = 24;
  95. //Sample the depth at the current pixel
  96. float fSceneDepthP = (screenTC);
  97. //Set the offset scale step
  98. float fOffsetScaleStep = 1.0f + 2.4f / nSamplesNum;
  99. //Initialize the accessibility factor to zero
  100. float fAccessibility = 0;
  101.  
  102. //Sample area around current pixel and accumulate the accessibility factor
  103. [loop]
  104. for (int i = 0 ; i < (nSamplesNum / 8) ; i++)
  105. for (int x = -1 ; x <= 1 ; x += 2)
  106. for (int y = -1 ; y <= 1 ; y += 2)
  107. for (int z = -1 ; z <= 1 ; z += 2) {
  108. //Create offset vector
  109. float3 vOffset = normalize(float3(x, y, z)) * (offsetScale *= fOffsetScaleStep);
  110. //Rotate the offset vector
  111. float3 vRotatedOffset = mul(vOffset, matRotate);
  112. //Center pixel's coordinates in screen space
  113. float3 vSamplePos = float3(screenTC, fSceneDepthP);
  114.  
  115. //Offset sample point
  116. vSamplePos += float3(vRotatedOffset.xy, vRotatedOffset.z * fSceneDepthP);
  117.  
  118. //Read sample point depth
  119. float fSceneDepthS = (vSamplePos.xy);
  120. //Discard if depth equals max
  121. if (fSceneDepthS >= 1.0f)
  122. fAccessibility += 1.0f;
  123. else {
  124. //Compute accessibility factor
  125. float fRangeIsInvalid = saturate(fSceneDepthP - fSceneDepthS);
  126. fAccessibility += lerp(fSceneDepthS > vSamplePos.z, 0.5f, fRangeIsInvalid);
  127. }
  128. }
  129.  
  130. //Compute average accessibility
  131. fAccessibility = fAccessibility / nSamplesNum;
  132.  
  133. //Amplify and return the ambient occlusion coefficient
  134. return float4(sample, saturate(fAccessibility * fAccessibility + fAccessibility) / (fSceneDepthP));
  135. }
  136.  
  137. float4 Blur2(VSOUT IN) : COLOR0 {
  138. float4 sample = tex2D(PassSamplerL, IN.UVCoord).rgba;
  139.  
  140. #if 0
  141. float ao = sample.a * 4;
  142.  
  143. ao += tex2D(PassSamplerL, IN.UVCoord + float2( rcpres.x, 0)).a * 2;
  144. ao += tex2D(PassSamplerL, IN.UVCoord + float2(-rcpres.x, 0)).a * 2;
  145. ao += tex2D(PassSamplerL, IN.UVCoord + float2(0, rcpres.y)).a * 2;
  146. ao += tex2D(PassSamplerL, IN.UVCoord + float2(0, -rcpres.y)).a * 2;
  147.  
  148. ao += tex2D(PassSamplerL, IN.UVCoord + rcpres ).a;
  149. ao += tex2D(PassSamplerL, IN.UVCoord - rcpres ).a;
  150. ao += tex2D(PassSamplerL, IN.UVCoord + rcpres * float2(1, -1)).a;
  151. ao += tex2D(PassSamplerL, IN.UVCoord - rcpres * float2(1, -1)).a;
  152.  
  153. ao /= 16;
  154. #else
  155. float ao = sample.a;
  156.  
  157. ao += 0.67 * tex2Dlod(PassSamplerL, float4(IN.UVCoord + float2( rcpres.x, rcpres.y), 0, 1)).a;
  158. ao += 0.57 * tex2Dlod(PassSamplerL, float4(IN.UVCoord + float2(-rcpres.x, -rcpres.y), 0, 1)).a;
  159. ao += 0.43 * tex2Dlod(PassSamplerL, float4(IN.UVCoord + float2( rcpres.x, -rcpres.y), 0, 2)).a;
  160. ao += 0.33 * tex2Dlod(PassSamplerL, float4(IN.UVCoord + float2(-rcpres.x, rcpres.y), 0, 2)).a;
  161. ao *= 0.333f;
  162. #endif
  163.  
  164. ao = saturate(pow(saturate(ao * 1.25 - 0.25), 0.25));
  165.  
  166. #ifdef TEST_MODE
  167. return ao;
  168. #endif
  169.  
  170. return float4(sample.rgb * ao, 1);
  171. }
  172.  
  173. technique t00
  174. <
  175. int group = 1;
  176. int fxclass = 2;
  177. int conditions = 3;
  178. >
  179. {
  180. pass p0 {
  181. VertexShader = compile vs_3_0 FrameVS();
  182. PixelShader = compile ps_3_0 Occlusion();
  183. }
  184.  
  185. pass p1 {
  186. VertexShader = compile vs_3_0 FrameVS();
  187. PixelShader = compile ps_3_0 Blur();
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement