float4x4 viewProj : register(c31); float3 eye : register(c2); float3 g_vLightDir : register(c1); float4x4 view : register(c35); float softness : register(c20); float lightTransferAmmount : register(c21); float specularFactor : register(c22); float specularPower : register(c23); float translucencyAmmount : register(c24); float textureScale : register(c25); float surfaceTension : register(c26); float refractAmmount : register(c27); float depthDifferenceBlur : register(c28); float2 ddx : register(c29); sampler samplerPosition : register(s0); sampler samplerNormal : register(s1); sampler samplerDiffuse : register(s2); //WRAP sampler samplerBackground : register(s3); samplerCUBE envSampler : register(s6); struct QuadOutput { float4 p:position; float2 t:texcoord0; float4 q:texcoord1; }; struct IndexedQuadOutput { float4 q:position; float3 n:normal; float4 p:texcoord0; float4 k:texcoord1; float2 t:texcoord2; float size:texcoord3; }; struct BlurResult { float4 position : color0; float4 normal : color1; }; struct ColorDepth { float4 color : color0; float depth : depth; }; float CalcDepth(float4 pos, float difference) { float3 eyeVector = viewProj._m02_m12_m22; pos.xyz -= eyeVector * difference; float4 finalDepth = mul(pos, viewProj); return finalDepth.z / finalDepth.w; } float4 psDepth(IndexedQuadOutput i):color { float l = length(i.t.xy * 2 - 1.0); if (l > 1.0) discard; float dist = cos(asin(l)); float3 eyeVector = viewProj._m02_m12_m22; float3 pos = i.p.xyz; //pos -= eyeVector * dist; return distance(eye, pos); } ColorDepth psPosition(IndexedQuadOutput i) { float l = length(i.t.xy * 2 - 1.0); if (l > 1.0) discard; float dist = cos(asin(l)); ColorDepth result; float3 eyeVector = viewProj._m02_m12_m22; float3 pos = i.p.xyz; pos -= eyeVector * dist; result.color = float4(pos, i.k.w); result.depth = CalcDepth(i.p, dist); return result; } ColorDepth psNormal(IndexedQuadOutput i) { float l = length(i.t.xy * 2 - 1.0); if (l > 1.0) discard; float dist = cos(asin(l)); ColorDepth result; float3 normal = float3(i.t.xy * 2 - 1.0, 0.0); normal.z = 1.0 - length(normal.xy); normal = normalize(normal); normal = mul(normal, (float3x3)view); result.color = float4(normal, 1.0); result.depth = CalcDepth(i.p, dist); return result; } BlurResult psBlur(QuadOutput input) { const int strength = 8; float2 SamplePos = input.t; float pi4 = 3.141 * 2.0 / strength; float4 resultPosition = tex2D(samplerPosition, SamplePos); // distance dependend blur float dist = length(resultPosition.xyz - eye); float finalSoftness = softness * (10 / dist) * 3; float3 centerPos = resultPosition.xyz; float4 resultNormal = tex2D(samplerNormal, SamplePos); int cnt = 1; int normalCnt = 1; for (int i = 0; i < strength; i++) { SamplePos = input.t + ddx * finalSoftness * float2(sin(pi4 * i), cos(pi4 * i)); float4 sampledPos = tex2D(samplerPosition, SamplePos); if (distance(sampledPos.xyz, centerPos) > depthDifferenceBlur) continue; resultPosition += sampledPos; float4 sampledNormal = tex2D(samplerNormal, SamplePos); if (sampledNormal.a >= 0.0 || resultNormal.a == 0.0) { resultNormal += sampledNormal; normalCnt ++; } cnt++; } BlurResult result; resultNormal /= normalCnt; resultNormal.xyz = normalize(resultNormal.xyz); result.normal = resultNormal; result.position = resultPosition / cnt; return result; } float4 psCompose(QuadOutput i):color0 { float4 normal = tex2D(samplerNormal, i.t); float4 simpleBackgroundColor = tex2D(samplerBackground, i.t); if (normal.a < 0.9) return simpleBackgroundColor; float3 viewDir = viewProj._m02_m12_m22; float4 position = tex2D(samplerPosition, i.t); float intensity = position.a; normal.xyz *= lerp(tex3D(samplerDiffuse, normal.xyz*0.25).xyz, float3(1.0, 1.0, 1.0), intensity); normal.xyz = normalize(normal.xyz); float4 lightColor = float4(0.925f, 1.0f, 0.75f, 1.f); float3 sssNormal = normalize(normal.xyz + g_vLightDir * lightTransferAmmount); float fDiffuse = dot(sssNormal, g_vLightDir); float3 h = normalize( normalize(viewDir) + g_vLightDir ); float fSpecular = dot(normal.xyz, h); fSpecular = fSpecular > 0 ? pow( fSpecular, specularPower) * specularFactor : 0.0; float4 diffuse = lightColor * fDiffuse * float4(0.8, 1.0, 1.0, 1.0); float d = saturate(pow(1.2*(1.0 - dot(normal.xyz, viewDir)), 10.0))*0.5; float f = dot(viewDir,normal.xyz);// * 2 - 1; float2 texDuDv = mul((float3x3)view, normal.xyz).yx * -refractAmmount * (f * 2 - 1); float4 background = 1.0; background.x = tex2D(samplerBackground, i.t + texDuDv * 1.1).x; background.y = tex2D(samplerBackground, i.t + texDuDv * 1.05).y; background.z = tex2D(samplerBackground, i.t + texDuDv * 1.0 ).z; return lerp(diffuse, background, min(1.0, translucencyAmmount + intensity)) + texCUBE(envSampler, normal.xyz) * (1-f) * specularFactor * 0.5 + fSpecular * lightColor; }