float3 F0; float4 readCubeMap(samplerCUBE cm, float3 coord) { float4 color = texCUBElod( cm, float4(coord.xy, - coord.z,0) ); return color; } float readDistanceCubeMap(samplerCUBE dcm, float3 coord) { float dist = texCUBElod(dcm, float4(coord.xy, - coord.z,0)).a; return dist; } #define MAX_LIN_ITERATIONCOUNT 80 //80 #define MIN_LIN_ITERATIONCOUNT 60 //60 #define SECANT_ITERATIONCOUNT 2 #define MAX_RAY_DEPTH 2 /* void linearSearch( float3 x, float3 R, float3 N, samplerCUBE mp, out float3 p, out float dl, out float dp, out float llp, out float ppp) { float3 Ra = abs(R), xa = abs(x); float xm = max(max(xa.x,xa.y),xa.z); float Rm = max(max(Ra.x,Ra.y),Ra.z); float a = xm / Rm; bool undershoot = false, overshoot = false; float dt = length(x / xm - R / Rm) * MAX_LIN_ITERATIONCOUNT; dt = max(dt, MIN_LIN_ITERATIONCOUNT); dt = 1.0 / dt; float t = 0.01;//dt; float pa; bool first = true; //Linear iteration while(t <= 1.0 && !overshoot) { dp = a * t / (1 - t); p = x + R * dp; pa = readDistanceCubeMap(mp, p); if(pa > 0) { ppp = length(p) / pa; if(ppp < 1) { dl = dp; llp = ppp; undershoot = true; } else if(undershoot) overshoot = true; } else undershoot = false; t += dt; } if(!overshoot) p = float3(0,0,0); } */ void linearSearch( float3 x, float3 R, float3 N, samplerCUBE mp, float min, out float3 p, out float dl, out float dp, out float llp, out float ppp) { float3 Ra = abs(R), xa = abs(x); float xm = max(max(xa.x,xa.y),xa.z); float Rm = max(max(Ra.x,Ra.y),Ra.z); float a = xm / Rm; int shootL = 0, shootP = 0; bool found = false; float dt = length(x / xm - R / Rm) * MAX_LIN_ITERATIONCOUNT; dt = max(dt, MIN_LIN_ITERATIONCOUNT); dt = 1.0 / dt; float t = 0.01; if(min > length(x)) { float a = 1; float b = 2 * dot(R, x); float c = dot(x,x) - min * min; float dis = b*b - 4*a*c; float t1 = (-b + sqrt(dis))/ 2.0 * a; float t2 = (-b - sqrt(dis))/ 2.0 * a; if(t1 > 0 && (t2 < 0 || t1 < t2)) t = 1.0;//t1 / ( a + t1); else if(t2 > 0) t = 1.0;//t2 / ( a + t2); //t = 0.011; } float pa; //Linear iteration while(t <= 1.0 && !found) { dp = a * t / (1 - t); p = x + R * dp; pa = readDistanceCubeMap(mp, p); if(pa > 0) { ppp = length(p) / pa; if(ppp < 1) shootP = -1; else shootP = 1; if(shootL * shootP == -1) found = true; else { shootL = shootP; dl = dp; llp = ppp; } } else shootL = 0; t += dt; } if(!found) p = float3(0,0,0); } void secantSearch(float3 x, float3 R, samplerCUBE mp, float dl, float dp, float llp, float ppp, out float3 p) { for(int i= 0; i < SECANT_ITERATIONCOUNT; i++) { float dnew; dnew = dl + (dp - dl) * (1 - llp) / (ppp - llp); p = x + R * dnew; half pppnew = length(p) / readDistanceCubeMap(mp, p); if(pppnew < 1) { llp = pppnew; dl = dnew; } else { ppp = pppnew; dp = dnew; } } } float3 Hit(float3 x, float3 R, float3 N, samplerCUBE mp1, samplerCUBE mp2, samplerCUBE mp3Color, samplerCUBE mp3Dist, float min, float min1, float min2, out float4 Il, out float3 Nl) { float dl1 = 0, dp1, llp1, ppp1; float3 p1; linearSearch(x, R, N, mp1, min1, p1, dl1, dp1, llp1, ppp1); float dl2 = 0, dp2, llp2, ppp2; float3 p2; linearSearch(x, R, N, mp2, min2, p2, dl2, dp2, llp2, ppp2); bool valid1 = dot(p1,p1) != 0; bool valid2 = dot(p2,p2) != 0; float dl, dp, llp, ppp; float3 p; if(!valid1 && ! valid2) { linearSearch(x, R, N, mp3Dist, min, p, dl, dp, llp, ppp); Il.a = 1; secantSearch(x, R, mp3Dist, dl, dp, llp, ppp, p); Il.rgb = Nl.rgb = readCubeMap(mp3Color, p).rgb; } else { if( !valid2 || (valid1 && dp1 < dp2)) { secantSearch(x, R, mp1, dl1, dp1, llp1, ppp1, p1); Il.rgb = Nl.rgb = readCubeMap(mp1, p1).rgb; p = p1; } else { secantSearch(x, R, mp2, dl2, dp2, llp2, ppp2, p2); Il.rgb = Nl.rgb = readCubeMap(mp2, p2).rgb; p = p2; } Il.a = 0; } return p; } struct VertOut { float3 wPos : TEXCOORD1; float3 cPos : TEXCOORD3; float2 texCoord : TEXCOORD0; float3 mNormal : TEXCOORD2; float4 hPos : POSITION; }; VertOut DefaultVS(float4 position : POSITION, float2 texCoord : TEXCOORD0, float3 normal : NORMAL, uniform float4x4 worldViewProj, uniform float4x4 world, uniform float4x4 worldview, uniform float4x4 worldI ) { VertOut OUT; OUT.hPos = mul(worldViewProj, position); OUT.wPos = mul(world, position).xyz; OUT.cPos = mul(worldview, position).xyz; OUT.mNormal = mul(normal, worldI); //OUT.mNormal = normal; OUT.texCoord = texCoord; return OUT; } float4 SingleReflectionPS(VertOut IN, uniform float3 cameraPos, uniform samplerCUBE CubeMap : register(s0), uniform samplerCUBE DistanceMap : register(s1), uniform samplerCUBE NormDistMap1 : register(s2), uniform samplerCUBE NormDistMap2 : register(s3), uniform float3 lastCenter, uniform float refIndex, float min, float min1, float min2 ) { float4 Color = float4(1,1,1,1); float3 N = normalize(IN.mNormal); float3 newTexCoord; float3 x = IN.wPos - lastCenter; float3 V = (IN.wPos - cameraPos); V = normalize(V); float3 R; //if(refIndex > 100.0) R = normalize(reflect( V, N)); //else //{ // refIndex = 1.0 / refIndex; // R = normalize(refract( V, N, refIndex)); //} float3 Nl; float4 Il; float3 l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, min, min1, min2, Il, Nl); if(Il.a == 0) Il = readCubeMap(CubeMap, l); return Il; } #define REFRACTION 0 float4 MultipleReflectionPS(VertOut IN, uniform float3 cameraPos, uniform samplerCUBE CubeMap : register(s0), uniform samplerCUBE DistanceMap : register(s1), uniform samplerCUBE NormDistMap1 : register(s2), uniform samplerCUBE NormDistMap2 : register(s3), uniform float3 lastCenter, uniform float refIndex, float min, float min1, float min2) { float4 I = float4(0,0,0,0); float3 N = normalize(IN.mNormal); float3 x = IN.wPos - lastCenter; float3 V = (IN.wPos - cameraPos); float Fp = 0.04; float F = Fp + pow(1 - dot(N, -V), 5) * (1 - Fp); V = normalize(V); float3 l; float4 Irefl; int depth = 0; while(depth < MAX_RAY_DEPTH) { float3 R; R = normalize(reflect( V, N)); float3 Nl; float4 Il; l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, min, min1, min2, Il, Nl); if(Il.a == 0) { depth += 1; } else { I = Il; depth = MAX_RAY_DEPTH; } x = l; N = Nl; V = R; } /* if(I.a == 0) I = readCubeMap(CubeMap, l);*/ Irefl = I; if(REFRACTION) { float4 Irefr; N = normalize(IN.mNormal); x = IN.wPos - lastCenter; V = (IN.wPos - cameraPos); depth = 0; while(depth < MAX_RAY_DEPTH) { float3 R; //R = normalize(reflect( V, N)); float ri = refIndex; if(dot(V,N) > 0) { ri = 1.0 / ri; N = -N; } R = refract( V, N, ri); if(dot(R,R) == 0) R = reflect( V, N); float3 Nl; float4 Il; l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, min, min1, min2, Il, Nl); if(Il.a == 0) { depth += 1; } else { I = Il; depth = MAX_RAY_DEPTH; } x = l; N = normalize(Nl); V = R; } if(I.a == 0) I = readCubeMap(CubeMap, l); Irefr = I; I = Irefl * F + (1.0 - F) * Irefr; } return I; } float4 mainPS(VertOut IN, uniform float3 cameraPos, uniform samplerCUBE CubeMap : register(s0), uniform samplerCUBE DistanceMap : register(s1), uniform samplerCUBE NormDistMap1 : register(s2), uniform samplerCUBE NormDistMap2 : register(s3), uniform float3 lastCenter, uniform float SingleBounce, uniform float refIndex, uniform float4 min, uniform float4 min1, uniform float4 min2 ):COLOR { // min1.x = 1.0 / min1.x; // min2.x = 1.0 / min2.x; float4 Color = 1.0; if(SingleBounce == 1) Color = SingleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, min.w, min1.w, min2.w); else Color = MultipleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, min.w, min1.w, min2.w); return Color; }