Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Volumetric SSAO
- // Implemented by Tomerk
- // Optimized by Ethatron
- // ---------------------------------------
- // TWEAKABLE VARIABLES.
- #undef TEST_MODE
- // testMode. set to 1, you can see the raw ssao
- #define LUMINANCE_CONSIDERATION
- float luminosity_threshold = 0.3;
- // comment this line to not take pixel brightness into account
- #define N_SAMPLES 9
- // number of samples, currently do not change.
- float aoRadiusMultiplier = 4;
- // Linearly multiplies the radius of the AO Sampling
- float aoStrengthMultiplier = 1.0;
- // Linearly multiplies the strength of the AO
- float aoClamp = 0.0;
- // The maximum strength of the AO, 0 is max strength, 1 is weakest
- float ThicknessModel = 100;
- // units in space the AO assumes objects' thicknesses are
- // END OF TWEAKABLE VARIABLES.
- // ---------------------------------------
- texture2D noise;
- sampler2D PassSamplerL34 = sampler_state
- {
- texture = <obge_LastRendertarget0_EFFECTPASS>;
- AddressU = CLAMP;
- AddressV = CLAMP;
- MINFILTER = LINEAR;
- MAGFILTER = LINEAR;
- MIPFILTER = NONE;
- };
- struct VSOUTb
- {
- float4 vertPos : POSITION;
- float2 UVCoord : TEXCOORD0;
- };
- struct VSINb
- {
- float4 vertPos : POSITION0;
- float2 UVCoord : TEXCOORD0;
- };
- VSOUTb FrameVSm(VSIN IN)
- {
- VSOUT OUT = (VSOUT)0.0f; // initialize to zero, avoid complaints.
- OUT.vertPos = IN.vertPos;
- OUT.UVCoord = IN.UVCoord;
- return OUT;
- }
- static const float2 sample_offsetm[N_SAMPLES] =
- {
- //#if N_SAMPLES >= 9
- float2(-0.1376476f, 0.2842022f ),
- float2(-0.626618f , 0.4594115f ),
- float2(-0.8903138f, -0.05865424f),
- float2( 0.2871419f, 0.8511679f ),
- float2(-0.1525251f, -0.3870117f ),
- float2( 0.6978705f, -0.2176773f ),
- float2( 0.7343006f, 0.3774331f ),
- float2( 0.1408805f, -0.88915f ),
- float2(-0.6642616f, -0.543601f )
- //#endif
- };
- static const float sample_radiusm[N_SAMPLES] =
- {
- //#if N_SAMPLES >= 9
- 0.948832,
- 0.629516,
- 0.451554,
- 0.439389,
- 0.909372,
- 0.682344,
- 0.5642,
- 0.4353,
- 0.5130
- //#endif
- };
- float4 Occlusionb(VSOUTb IN) : COLOR0 {
- float3 sample = tex2D(PassSamplerL, IN.UVCoord).rgb;
- float depth = (IN.UVCoord);
- [branch]
- if (depth >= 0.99)
- return float4(sample, 1.0);
- float3 pos = (IN.UVCoord, depth);
- float3 dx = ddx(pos);
- float3 dy = ddy(pos);
- float3 norm = normalize(cross(dx, dy));
- norm.y *= -1;
- float sample_depth;
- float ao = 0;
- float s = 0.0;
- float2 rand_vec = (IN.UVCoord);
- float2 sample_vec_divisor = g_InvFocalLen * depth * 2.0 / (aoRadiusMultiplier * 5000 * rcpres);
- float2 sample_center = IN.UVCoord + norm.xy / sample_vec_divisor * float2(1, aspect);
- float sample_center_depth = depth * 1.0 + norm.z * aoRadiusMultiplier * 10;
- [unroll]
- for (int i = 0; i < N_SAMPLES; i++) {
- float2 sample_vec = reflect(sample_offset[i], rand_vec) / sample_vec_divisor;
- float2 sample_coords = sample_center + sample_vec * float2(1, aspect);
- float curr_sample_radius = sample_radius[i] * aoRadiusMultiplier * 10;
- float curr_sample_depth = 3.0 * (sample_coords);
- ao += clamp(0, curr_sample_radius + sample_center_depth - curr_sample_depth , 2 * curr_sample_radius);
- ao -= clamp(0, curr_sample_radius + sample_center_depth - curr_sample_depth - ThicknessModel, 2 * curr_sample_radius);
- s += 2 * curr_sample_radius;
- }
- ao /= s;
- ao *= (depth);
- ao = 1.0 - ao * aoStrengthMultiplier;
- return float4(sample, ao);
- }
- float4 BlurNCombine(VSOUT IN) : COLOR0 {
- float4 sample = tex2D(PassSamplerL, IN.UVCoord).rgba;
- float3 color = sample.rgb;
- float ao = sample.a * 4;
- ao += tex2D(PassSamplerL, IN.UVCoord + float2( rcpres.x, 0)).a * 2;
- ao += tex2D(PassSamplerL, IN.UVCoord + float2(-rcpres.x, 0)).a * 2;
- ao += tex2D(PassSamplerL, IN.UVCoord + float2(0, rcpres.y)).a * 2;
- ao += tex2D(PassSamplerL, IN.UVCoord + float2(0, -rcpres.y)).a * 2;
- ao += tex2D(PassSamplerL, IN.UVCoord + rcpres ).a;
- ao += tex2D(PassSamplerL, IN.UVCoord - rcpres ).a;
- ao += tex2D(PassSamplerL, IN.UVCoord + rcpres * float2(1, -1)).a;
- ao += tex2D(PassSamplerL, IN.UVCoord - rcpres * float2(1, -1)).a;
- ao /= 16;
- #ifdef LUMINANCE_CONSIDERATION
- float luminance = (color);
- float white = 1.0;
- float black = 0;
- luminance = clamp(
- max(black, luminance - luminosity_threshold) +
- max(black, luminance - luminosity_threshold) +
- max(black, luminance - luminosity_threshold), 0.0, 1.0);
- ao = lerp(ao, white, luminance);
- #endif
- #ifdef TEST_MODE
- return ao;
- #endif
- return float4(color * ao, 1);
- }
- technique t065
- <
- int group = 1.0;
- int fxclass = 2.0;
- int conditions = 2.0;
- >
- {
- pass {
- VertexShader = compile vs_3_0 FrameVS();
- PixelShader = compile ps_3_0 Occlusion();
- }
- pass {
- VertexShader = compile vs_3_0 FrameVS();
- PixelShader = compile ps_3_0 BlurNCombine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement