1 | float4 readCubeMap(samplerCUBE cm, float3 coord)
|
---|
2 | {
|
---|
3 | float4 color = texCUBE( cm, float3(coord.xy, - coord.z) );
|
---|
4 | color.a = 1;
|
---|
5 | return color;
|
---|
6 | }
|
---|
7 |
|
---|
8 | float readDistanceCubeMap(samplerCUBE dcm, float3 coord)
|
---|
9 | {
|
---|
10 | float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).r;
|
---|
11 | if(dist == 0) dist = 1000; ///sky
|
---|
12 | return dist;
|
---|
13 | }
|
---|
14 |
|
---|
15 | #define SECANT_ITERATIONCOUNT 2
|
---|
16 |
|
---|
17 | float3 Hit( float3 x, float3 R, samplerCUBE mp )
|
---|
18 | {
|
---|
19 | float rl = readDistanceCubeMap( mp, R); // |r|
|
---|
20 |
|
---|
21 | float ppp = length( x ) / readDistanceCubeMap( mp, x); // |p|/|p|
|
---|
22 | float dun = 0, pun = ppp, dov = 0, pov = 0;
|
---|
23 | float dl = rl * ( 1 - ppp ); // eq. 2
|
---|
24 | float3 l = x + R * dl; // ray equation
|
---|
25 |
|
---|
26 | // iteration
|
---|
27 | for( int i = 0; i < SECANT_ITERATIONCOUNT ; i++ )
|
---|
28 | {
|
---|
29 | float llp = length( l ) / readDistanceCubeMap( mp, l); // |l|/|l|
|
---|
30 | if ( llp < 0.999f ) // undershooting
|
---|
31 | {
|
---|
32 | dun = dl; pun = llp; // last undershooting
|
---|
33 | dl += ( dov == 0 ) ? rl * ( 1 - llp ) : // eq. 2
|
---|
34 | ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3
|
---|
35 | } else if ( llp > 1.001f ) // overshooting
|
---|
36 | {
|
---|
37 | dov = dl; pov = llp; // last overshooting
|
---|
38 | dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
|
---|
39 | }
|
---|
40 | l = x + R * dl; // ray equation
|
---|
41 | }
|
---|
42 | return l; // computed hit point
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | struct Shaded_OUT
|
---|
47 | {
|
---|
48 | float4 vPos : POSITION;
|
---|
49 | float4 wNormal : TEXCOORD0;
|
---|
50 | float4 wPos : TEXCOORD1;
|
---|
51 | };
|
---|
52 |
|
---|
53 |
|
---|
54 | float4 EnvMap_Default_PS(Shaded_OUT IN,
|
---|
55 | uniform samplerCUBE CubeMap : register(s0),
|
---|
56 | uniform float3 cameraPos) : COLOR0
|
---|
57 | {
|
---|
58 | float3 N = normalize(IN.wNormal.xyz);
|
---|
59 | float3 V = normalize(IN.wPos.xyz - cameraPos);
|
---|
60 | float3 R = reflect( V, N);
|
---|
61 |
|
---|
62 | return readCubeMap(CubeMap, R );
|
---|
63 | }
|
---|
64 |
|
---|
65 | float4 EnvMap_Localized_Reflection_PS( Shaded_OUT IN,
|
---|
66 | uniform samplerCUBE CubeMap : register(s0),
|
---|
67 | uniform samplerCUBE DistanceMap : register(s1),
|
---|
68 | uniform float3 cameraPos,
|
---|
69 | uniform float3 lastCenter,
|
---|
70 | uniform float sFresnel,
|
---|
71 | uniform float sRefraction ) :COLOR0
|
---|
72 | {
|
---|
73 | float3 N = normalize(IN.wNormal.xyz);
|
---|
74 | float3 RR;
|
---|
75 | float3 V = normalize(IN.wPos.xyz - cameraPos);
|
---|
76 | float3 cubePos = IN.wPos.xyz - lastCenter;
|
---|
77 | float3 R = reflect( V, N);
|
---|
78 |
|
---|
79 | RR = R;
|
---|
80 | RR = Hit(cubePos, R, DistanceMap);
|
---|
81 |
|
---|
82 | return readCubeMap(CubeMap, RR );
|
---|
83 | }
|
---|
84 |
|
---|
85 | float4 EnvMap_Localized_Refraction_PS( Shaded_OUT IN,
|
---|
86 | uniform samplerCUBE CubeMap : register(s0),
|
---|
87 | uniform samplerCUBE DistanceMap : register(s1),
|
---|
88 | uniform float3 cameraPos,
|
---|
89 | uniform float3 lastCenter,
|
---|
90 | uniform float sFresnel,
|
---|
91 | uniform float sRefraction ) :COLOR0
|
---|
92 | {
|
---|
93 | float3 N = normalize(IN.wNormal.xyz);
|
---|
94 | float3 RR, TT;
|
---|
95 | float3 V = normalize(IN.wPos.xyz - cameraPos);
|
---|
96 | float3 cubePos = IN.wPos.xyz - lastCenter;
|
---|
97 | float3 R = reflect( V, N);
|
---|
98 | float3 T = refract(V, N, sRefraction);
|
---|
99 |
|
---|
100 | RR = R; TT = T;
|
---|
101 | RR = Hit(cubePos, R, DistanceMap);
|
---|
102 | TT = Hit(cubePos, T, DistanceMap);
|
---|
103 |
|
---|
104 | float4 reflectcolor = readCubeMap(CubeMap, RR );
|
---|
105 | float4 refractcolor = readCubeMap(CubeMap, TT );
|
---|
106 |
|
---|
107 | float cos_theta = -dot(V, N);
|
---|
108 | float F = (sFresnel + pow(1 - cos_theta, 5.0f) * (1 - sFresnel));
|
---|
109 |
|
---|
110 | return (F * reflectcolor + (1 - F) * refractcolor);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | float4 EnvMap_LocalizedMetal_PS( Shaded_OUT IN,
|
---|
115 | uniform samplerCUBE CubeMap : register(s0),
|
---|
116 | uniform samplerCUBE DistanceMap : register(s1),
|
---|
117 | uniform float3 cameraPos,
|
---|
118 | uniform float3 lastCenter,
|
---|
119 | uniform float3 F0 ):COLOR0
|
---|
120 | {
|
---|
121 | float4 Color;
|
---|
122 | float3 N = normalize(IN.wNormal.xyz);
|
---|
123 | float3 RR;
|
---|
124 | float3 V = normalize(IN.wPos.xyz - cameraPos);
|
---|
125 | float3 cubePos = IN.wPos.xyz - lastCenter;
|
---|
126 | float3 R = reflect( V, N);
|
---|
127 |
|
---|
128 | RR = R;
|
---|
129 | RR = Hit(cubePos, R, DistanceMap);
|
---|
130 | Color = readCubeMap(CubeMap, RR );
|
---|
131 |
|
---|
132 | float ctheta_in = dot(N, R);
|
---|
133 | float ctheta_out = dot(N, -V);
|
---|
134 |
|
---|
135 | float3 F = 0;
|
---|
136 |
|
---|
137 | // compute F,P,G
|
---|
138 | //if ( ctheta_in > 0 && ctheta_out > 0 )
|
---|
139 | {
|
---|
140 | float3 H = normalize(R - V); // felezõvektor
|
---|
141 | float cbeta = dot(H,R);
|
---|
142 | //F = ( (n-1)*(n-1) + pow(1-cbeta,5) * 4*n + k*k) / ( (n+1)*(n+1) + k*k );
|
---|
143 | //float3 F0 = ((n-1)*(n-1) + k*k) / ( (n+1)*(n+1) + k*k );
|
---|
144 | //float3 F1 = float3(1.0f,1.0f,1.0f) - F0;
|
---|
145 | F = F0 + (1 - F0) * pow(1 - cbeta, 5);
|
---|
146 | }
|
---|
147 |
|
---|
148 | return Color * float4(F,1);
|
---|
149 | }
|
---|