/////////////////////////////////////////////////////////////////////////////// // // ## ###### // ###### ### // ## ############### Shark 3D Engine (www.shark3d.com) // ########## # # # // ######## Copyright (c) 1996-2006 Spinor GmbH. // ######### # # # All rights reserved. // ## ########## // ## // /////////////////////////////////////////////////////////////////////////////// #define NITERATION 4 struct PS_INPUT { float3 eye : TEXCOORD0; float3 normal : TEXCOORD1; float3 posView : TEXCOORD2; }; sampler colorSamp: register(s0); sampler distanceSamp: register(s1); float readDistanceCubeMap(samplerCUBE m, float3 r) { float dist = texCUBE(m, r).r; if(dist == 0) dist = 10; return dist; } half3 Hit(samplerCUBE tex, half3 R, half3 p0) { float rl = texCUBE(tex, R).r; float dp = rl - dot(p0, R); float3 p = p0 + R * dp; float ppp = length(p) / texCUBE(tex, p).r; float dun = 0, dov = 0, pun = ppp, pov = ppp; if (ppp < 1) dun = dp; else dov = dp; float dl = max( dp + rl * (1 - ppp), 0); float3 RR = p0 + R * dl; for(int i = 0; i < NITERATION; i++) { float ddl; float llp = length(RR) / readDistanceCubeMap(tex, RR); if (llp < 1) { dun = dl; pun = llp; ddl = (dov == 0) ? rl * (1 - llp) : (dl - dov) * (1 - llp)/(llp - pov); } else { dov = dl; pov = llp; ddl = (dun == 0) ? rl * (1 - llp) : (dl - dun) * (1 - llp)/(llp - pun); } dl = max(dl + ddl, 0); RR = p0 + R * dl; } return RR; } /////////////////////////////////////////////////////////////////////////////// // Pixelshader // Profile: 3x0 // reflective float4 main(PS_INPUT input): COLOR0 { // return texCUBE(colorSamp, input.normal); //return float4(input.normal, 1); const float g_FresnelCoeff = 1.0; const float g_Transparency = 0.0; const float g_RefractFactor = 1.0f; float3 normal = normalize(input.normal); float3 eye = normalize(input.eye); float3 r1 = reflect(eye, normal); float3 RR1 = Hit(distanceSamp, r1, input.posView); // fresnel float3 fresnel = saturate(g_FresnelCoeff + (1.0f - g_FresnelCoeff) * pow(1 - dot(normal, -eye), 5)); return texCUBE(colorSamp,RR1) * float4(fresnel, 1); } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // Pixelshader // Profile: 3x0 // refractive float4 refract(PS_INPUT input): COLOR0 { // return texCUBE(colorSamp, input.normal); //return float4(input.normal, 1); const float3 g_FresnelCoeff = 0.0; const float g_Transparency = 0.3; const float g_RefractFactor = 0.8f; float3 normal = normalize(input.normal); float3 eye = normalize(input.eye); float3 r1 = reflect(eye, normal); float3 r2 = refract(eye, normal, g_RefractFactor); float3 RR1 = Hit(distanceSamp, r1, input.posView); float3 RR2 = Hit(distanceSamp, r2, input.posView); // fresnel float3 fresnel = saturate(g_FresnelCoeff + (1.0f - g_FresnelCoeff) * pow(1 - dot(normal, -eye), 5)); //return 1; return texCUBE(colorSamp,RR2)*(1- g_Transparency) + texCUBE(colorSamp, RR1) * g_Transparency; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // Pixelshader // Profile: 3x0 // metal float4 Fresnel; float4 metal(PS_INPUT input): COLOR0 { //const float3 g_FresnelCoeff = 0.7; const float g_Transparency = 0.0; const float g_RefractFactor = 1.0f; float3 normal = normalize(input.normal); float3 eye = normalize(input.eye); float3 r1 = reflect(eye, normal); float3 RR1 = Hit(distanceSamp, r1, input.posView); // fresnel float4 fresnel = saturate(Fresnel + (1.0f - Fresnel) * pow(1 - dot(normal, -eye), 5)); // fresnel = float4(0.95, 0.63, 0.54,1); return texCUBE(colorSamp,RR1) * fresnel; } ///////////////////////////////////////////////////////////////////////////////