float4 readCubeMap(samplerCUBE cm, float3 coord) { float4 color = texCUBE( cm, float3(coord.xy, - coord.z) ); color.a = 1; return color; } float readDistanceCubeMap(samplerCUBE dcm, float3 coord) { float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).a; if(dist == 0) dist = 1000; ///sky return dist; } #define SECANT_ITERATIONCOUNT 20 float3 Hit( float3 x, float3 R, samplerCUBE mp ) { float rl = readDistanceCubeMap( mp, R); // |r| float ppp = length( x ) / readDistanceCubeMap( mp, x); // |p|/|p’| float dun = 0, pun = ppp, dov = 0, pov = 0; float dl = rl * ( 1 - ppp ); // eq. 2 float3 l = x + R * dl; // ray equation // iteration for( int i = 0; i < SECANT_ITERATIONCOUNT ; i++ ) { float llp = length( l ) / readDistanceCubeMap( mp, l); // |l|/|l’| if ( llp < 0.999f ) // undershooting { dun = dl; pun = llp; // last undershooting dl += ( dov == 0 ) ? rl * ( 1 - llp ) : // eq. 2 ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3 } else if ( llp > 1.001f ) // overshooting { dov = dl; pov = llp; // last overshooting dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3 } l = x + R * dl; // ray equation } return l; // computed hit point } struct Shaded_OUT { float4 vPos : POSITION; float4 wNormal : TEXCOORD0; float4 wPos : TEXCOORD1; }; float4 EnvMap_Default_PS(Shaded_OUT IN, uniform samplerCUBE CubeMap : register(s0), uniform float3 cameraPos) : COLOR0 { float3 N = normalize(IN.wNormal.xyz); float3 V = normalize(IN.wPos.xyz - cameraPos); float3 R = reflect( V, N); return readCubeMap(CubeMap, R ); } float4 EnvMap_Refract_Default_PS (Shaded_OUT IN, uniform samplerCUBE CubeMap : register(s0), uniform float3 cameraPos, uniform float sRefraction, uniform float transparency ) : COLOR0 { float3 N = normalize(IN.wNormal.xyz); float3 V = normalize(IN.wPos.xyz - cameraPos); float3 R = reflect( V, N); float3 R2 = refract( V, N, sRefraction); return 0.5;//(1.0 - transparency) * readCubeMap(CubeMap, R ) + transparency * readCubeMap(CubeMap, R2); } float4 EnvMap_Localized_Reflection_PS( Shaded_OUT IN, uniform samplerCUBE CubeMap : register(s0), uniform samplerCUBE DistanceMap : register(s1), uniform float3 cameraPos, uniform float3 lastCenter, uniform float sFresnel, uniform float sRefraction ) :COLOR0 { float3 N = normalize(IN.wNormal.xyz); float3 RR; float3 V = normalize(IN.wPos.xyz - cameraPos); float3 cubePos = IN.wPos.xyz - lastCenter; float3 R = reflect( V, N); RR = R; RR = Hit(cubePos, R, DistanceMap); return readCubeMap(CubeMap, RR ); } float4 EnvMap_Localized_Refraction_PS( Shaded_OUT IN, uniform samplerCUBE CubeMap : register(s0), uniform samplerCUBE DistanceMap : register(s1), uniform float3 cameraPos, uniform float3 lastCenter, uniform float4 sFresnel, uniform float sRefraction ) :COLOR0 { float4 Color = 0; float3 N = normalize(IN.wNormal.xyz); float3 RR, TT; float3 V = normalize(IN.wPos.xyz - cameraPos); float3 cubePos = IN.wPos.xyz - lastCenter; float3 R = reflect( V, N); float3 T = refract(V, N, sRefraction); RR = R; TT = T; RR = Hit(cubePos, R, DistanceMap); float4 reflectcolor = readCubeMap(CubeMap, RR ); if(dot(T,T)!=0) { TT = Hit(cubePos, T, DistanceMap); float4 refractcolor = readCubeMap(CubeMap, TT ); float cos_theta = -dot(V, N); float F = (sFresnel + pow(1 - cos_theta, 5.0f) * (1 - sFresnel)); F = saturate(F); Color = (F * reflectcolor + (1 - F) * refractcolor); } else { Color = reflectcolor; } return Color; } float4 EnvMap_LocalizedMetal_PS( Shaded_OUT IN, uniform samplerCUBE CubeMap : register(s0), uniform samplerCUBE DistanceMap : register(s1), uniform float3 cameraPos, uniform float3 lastCenter, uniform float3 F0 ):COLOR0 { float4 Color; float3 N = normalize(IN.wNormal.xyz); float3 RR; float3 V = normalize(IN.wPos.xyz - cameraPos); float3 cubePos = IN.wPos.xyz - lastCenter; float3 R = reflect( V, N); RR = R; RR = Hit(cubePos, R, DistanceMap); Color = readCubeMap(CubeMap, RR); //Color = readDistanceCubeMap(DistanceMap, cubePos)/300.0; //return Color; float ctheta_in = dot(N, R); float ctheta_out = dot(N, -V); float3 F = 0; // compute F,P,G //if ( ctheta_in > 0 && ctheta_out > 0 ) { float3 H = normalize(R - V); // felezővektor float cbeta = dot(H,R); //F = ( (n-1)*(n-1) + pow(1-cbeta,5) * 4*n + k*k) / ( (n+1)*(n+1) + k*k ); //float3 F0 = ((n-1)*(n-1) + k*k) / ( (n+1)*(n+1) + k*k ); //float3 F1 = float3(1.0f,1.0f,1.0f) - F0; F = F0 + (1 - F0) * pow(1 - cbeta, 5); } return Color * float4(F,1); }