[1720] | 1 | float3 F0;
|
---|
| 2 |
|
---|
| 3 | float4 readCubeMap(samplerCUBE cm, float3 coord)
|
---|
| 4 | {
|
---|
[1735] | 5 | float4 color = texCUBElod( cm, float4(coord.xy, - coord.z,0) );
|
---|
[1720] | 6 | return color;
|
---|
| 7 | }
|
---|
| 8 |
|
---|
| 9 | float readDistanceCubeMap(samplerCUBE dcm, float3 coord)
|
---|
| 10 | {
|
---|
[1735] | 11 | float dist = texCUBElod(dcm, float4(coord.xy, - coord.z,0)).a;
|
---|
[1720] | 12 | return dist;
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 |
|
---|
[1831] | 17 | #define MAX_LIN_ITERATIONCOUNT 20
|
---|
| 18 | #define MIN_LIN_ITERATIONCOUNT 6
|
---|
| 19 | #define SECANT_ITERATIONCOUNT 1
|
---|
[1724] | 20 |
|
---|
| 21 | float Hit(float3 x, float3 R, samplerCUBE mp, out float3 newDir)
|
---|
[1720] | 22 | {
|
---|
| 23 | R = normalize(R);
|
---|
| 24 |
|
---|
| 25 | float3 Ra = abs(R);
|
---|
| 26 | float3 xa = abs(x);
|
---|
| 27 |
|
---|
[1735] | 28 | float xm = max(max(xa.x,xa.y),xa.z);
|
---|
| 29 | float Rm = max(max(Ra.x,Ra.y),Ra.z);
|
---|
| 30 |
|
---|
| 31 | float a = xm / Rm;
|
---|
| 32 |
|
---|
| 33 |
|
---|
[1831] | 34 | float dt = length(x / xm - R / Rm) * MAX_LIN_ITERATIONCOUNT;
|
---|
| 35 | dt = max(dt, MIN_LIN_ITERATIONCOUNT);
|
---|
[1735] | 36 | dt = 1.0 / dt;
|
---|
| 37 |
|
---|
| 38 |
|
---|
[1859] | 39 | bool overshoot = false, undershoot = false;
|
---|
[1720] | 40 | float dp, dl = 0, ppp, llp;
|
---|
| 41 | float lR = readDistanceCubeMap(mp, R);
|
---|
[1724] | 42 | //float3 p = R;
|
---|
| 43 | float3 p = 0;
|
---|
[1831] | 44 |
|
---|
[1720] | 45 | //linear iteration
|
---|
[1831] | 46 | float t;
|
---|
| 47 | float dist = 0;
|
---|
| 48 | /*
|
---|
| 49 | t = 0.9999999999999;
|
---|
[1735] | 50 | dp = a * t / (1 - t);
|
---|
| 51 | p = x + R * dp;
|
---|
[1831] | 52 | dist = readDistanceCubeMap(mp, p);
|
---|
[1735] | 53 | ppp = length(p) / dist;
|
---|
[1831] | 54 | */
|
---|
[1859] | 55 | t = dt;
|
---|
[1735] | 56 |
|
---|
[1859] | 57 | while(t <= 1.0 && !overshoot)
|
---|
[1720] | 58 | {
|
---|
[1724] | 59 | dp = a * t / (1 - t);
|
---|
| 60 | p = x + R * dp;
|
---|
| 61 | float dist = readDistanceCubeMap(mp, p);
|
---|
| 62 | if(dist > 0)
|
---|
| 63 | {
|
---|
| 64 | ppp = length(p) / dist;
|
---|
| 65 | if(ppp < 1)
|
---|
[1720] | 66 | {
|
---|
[1724] | 67 | dl = dp;
|
---|
| 68 | llp = ppp;
|
---|
| 69 | undershoot = true;
|
---|
[1720] | 70 | }
|
---|
[1724] | 71 | else
|
---|
| 72 | {
|
---|
| 73 | if (undershoot)
|
---|
| 74 | overshoot = true;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
[1859] | 77 | else
|
---|
[1724] | 78 | undershoot = false;
|
---|
| 79 |
|
---|
[1720] | 80 | t += dt;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[1859] | 83 | //if(t >= 1.0 && undershoot && dist)
|
---|
| 84 | // overshoot = true;
|
---|
[1730] | 85 |
|
---|
[1720] | 86 | if(overshoot)
|
---|
| 87 | {
|
---|
| 88 | float dnew;
|
---|
| 89 | for(int i = 0; i < SECANT_ITERATIONCOUNT; i++ )
|
---|
| 90 | {
|
---|
| 91 | dnew = dl + (dp - dl) * (1 - llp) / (ppp - llp);
|
---|
| 92 |
|
---|
| 93 | p = x + R * dnew;
|
---|
| 94 | half pppnew = length(p) / readDistanceCubeMap(mp, p);
|
---|
| 95 | if(pppnew < 1)
|
---|
| 96 | {
|
---|
| 97 | llp = pppnew;
|
---|
| 98 | dl = dnew;
|
---|
| 99 | }
|
---|
| 100 | else
|
---|
| 101 | {
|
---|
| 102 | ppp = pppnew;
|
---|
| 103 | dp = dnew;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | else
|
---|
| 108 | p = float3(0,0,0);
|
---|
[1724] | 109 |
|
---|
| 110 | newDir = p;
|
---|
| 111 | return dp;
|
---|
[1720] | 112 | }
|
---|
[1724] | 113 |
|
---|
[1853] | 114 |
|
---|
| 115 | float HitOld( float3 x, float3 R, samplerCUBE mp, out float3 newDir )
|
---|
[1720] | 116 | {
|
---|
[1724] | 117 | //return R + 0.00000000001 * x;
|
---|
[1720] | 118 | float rl = readDistanceCubeMap( mp, R); // |r|
|
---|
| 119 |
|
---|
| 120 | float ppp = length( x ) / readDistanceCubeMap( mp, x); // |p|/|p|
|
---|
| 121 | float dun = 0, pun = ppp, dov = 0, pov = 0;
|
---|
| 122 | float dl = rl * ( 1 - ppp ); // eq. 2
|
---|
| 123 | float3 l = x + R * dl; // ray equation
|
---|
| 124 |
|
---|
| 125 | // iteration
|
---|
[1724] | 126 | for( int i = 0; i < SECANT_ITERATIONCOUNT; i++ )
|
---|
[1720] | 127 | {
|
---|
| 128 | float llp = length( l ) / readDistanceCubeMap( mp, l); // |l|/|l|
|
---|
| 129 | if ( llp < 0.999f ) // undershooting
|
---|
| 130 | {
|
---|
| 131 | dun = dl; pun = llp; // last undershooting
|
---|
| 132 | dl += ( dov == 0 ) ? rl * ( 1 - llp ) : // eq. 2
|
---|
| 133 | ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3
|
---|
| 134 | } else if ( llp > 1.001f ) // overshooting
|
---|
| 135 | {
|
---|
| 136 | dov = dl; pov = llp; // last overshooting
|
---|
| 137 | dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
|
---|
| 138 | }
|
---|
| 139 | l = x + R * dl; // ray equation
|
---|
| 140 | }
|
---|
[1735] | 141 | newDir = l; // computed hit point
|
---|
| 142 |
|
---|
| 143 | return dl;
|
---|
[1720] | 144 | }
|
---|
| 145 |
|
---|
[1853] | 146 |
|
---|
[1724] | 147 | struct VertOut
|
---|
[1720] | 148 | {
|
---|
[1724] | 149 | float3 wPos : TEXCOORD1;
|
---|
| 150 | float3 cPos : TEXCOORD3;
|
---|
| 151 | float2 texCoord : TEXCOORD0;
|
---|
| 152 | float3 mNormal : TEXCOORD2;
|
---|
| 153 | float4 hPos : POSITION;
|
---|
| 154 | };
|
---|
[1720] | 155 |
|
---|
[1724] | 156 | VertOut DefaultVS(float4 position : POSITION,
|
---|
| 157 | float2 texCoord : TEXCOORD0,
|
---|
| 158 | float3 normal : NORMAL,
|
---|
| 159 | uniform float4x4 worldViewProj,
|
---|
| 160 | uniform float4x4 world,
|
---|
| 161 | uniform float4x4 worldview,
|
---|
| 162 | uniform float4x4 worldI )
|
---|
[1720] | 163 | {
|
---|
[1724] | 164 | VertOut OUT;
|
---|
| 165 | OUT.hPos = mul(worldViewProj, position);
|
---|
| 166 | OUT.wPos = mul(world, position).xyz;
|
---|
| 167 | OUT.cPos = mul(worldview, position).xyz;
|
---|
| 168 | OUT.mNormal = mul(normal, worldI);
|
---|
| 169 | //OUT.mNormal = normal;
|
---|
| 170 | OUT.texCoord = texCoord;
|
---|
| 171 | return OUT;
|
---|
[1720] | 172 | }
|
---|
| 173 |
|
---|
| 174 | //////////////
|
---|
| 175 | //Metal
|
---|
| 176 | //////////////
|
---|
[1854] | 177 | float4 Metal1BouncePS( VertOut IN,
|
---|
[1720] | 178 | uniform float3 cameraPos,
|
---|
| 179 | uniform samplerCUBE CubeMap : register(s0),
|
---|
| 180 | uniform samplerCUBE DistanceMap : register(s1),
|
---|
[1854] | 181 | uniform float3 lastCenter):COLOR0
|
---|
[1720] | 182 | {
|
---|
| 183 |
|
---|
[1854] | 184 | float4 Color = float4(1,1,1,1);
|
---|
[1720] | 185 |
|
---|
[1724] | 186 | IN.mNormal = normalize(IN.mNormal);
|
---|
[1720] | 187 | float3 newTexCoord;
|
---|
[1724] | 188 | float3 mPos = IN.wPos - lastCenter;
|
---|
| 189 | float3 V = (IN.wPos - cameraPos);
|
---|
[1720] | 190 | float cameraDistace = length(V);
|
---|
| 191 | V = normalize(V);
|
---|
[1724] | 192 | float3 R = normalize(reflect( V, IN.mNormal));
|
---|
[1720] | 193 |
|
---|
[1730] | 194 | newTexCoord = R;
|
---|
[1853] | 195 | HitOld(mPos, R, DistanceMap, newTexCoord);
|
---|
[1724] | 196 | Color = readCubeMap(CubeMap, newTexCoord );
|
---|
[1720] | 197 |
|
---|
[1724] | 198 | float ctheta_in = dot(IN.mNormal,R);
|
---|
| 199 | float ctheta_out = dot(IN.mNormal,-V);
|
---|
[1720] | 200 |
|
---|
| 201 | float3 F = 0;
|
---|
| 202 |
|
---|
| 203 | // F,P,G számítása
|
---|
| 204 | if ( ctheta_in > 0 && ctheta_out > 0 )
|
---|
| 205 | {
|
---|
| 206 | float3 H = normalize(R - V); // felezõvektor
|
---|
| 207 | float cbeta = dot(H,R);
|
---|
| 208 | F = F0 + (1-F0)*pow(1-cbeta,5);
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[1854] | 211 | Color = Color * float4(F,1);
|
---|
| 212 |
|
---|
| 213 | return Color;
|
---|
[1720] | 214 | }
|
---|
[1724] | 215 |
|
---|
| 216 |
|
---|
| 217 | void MetalMultipleBouncePS( VertOut IN,
|
---|
[1720] | 218 | uniform float3 cameraPos,
|
---|
[1724] | 219 | uniform samplerCUBE CubeMap : register(s0),
|
---|
[1720] | 220 | uniform samplerCUBE DistanceMap : register(s1),
|
---|
[1724] | 221 | uniform samplerCUBE NormDistMap1 : register(s2),
|
---|
| 222 | uniform samplerCUBE NormDistMap2 : register(s3),
|
---|
[1720] | 223 | uniform float3 lastCenter,
|
---|
[1854] | 224 | uniform float SingleBounce,
|
---|
[1720] | 225 | out float4 Color :COLOR0)
|
---|
| 226 | {
|
---|
[1854] | 227 | if(SingleBounce == 1)
|
---|
| 228 | Color = Metal1BouncePS(IN,cameraPos,CubeMap,DistanceMap,lastCenter);
|
---|
[1724] | 229 | else
|
---|
[1854] | 230 | {
|
---|
| 231 | Color = float4(1,1,1,1);
|
---|
[1730] | 232 |
|
---|
[1854] | 233 | IN.mNormal = normalize(IN.mNormal);
|
---|
| 234 | float3 newTexCoord;
|
---|
| 235 | float3 mPos = IN.wPos - lastCenter;
|
---|
| 236 | float3 V = (IN.wPos - cameraPos);
|
---|
| 237 | float cameraDistace = length(V);
|
---|
| 238 | V = normalize(V);
|
---|
| 239 | float3 R = normalize(reflect( V, IN.mNormal));
|
---|
| 240 | //float3 R = normalize(refract( V, IN.mNormal, 0.98));
|
---|
| 241 |
|
---|
| 242 | newTexCoord = R;
|
---|
[1730] | 243 |
|
---|
[1854] | 244 | //Color = readCubeMap(NormDistMap1, mPos );
|
---|
| 245 | //return;
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | float3 newDir1;
|
---|
| 249 | float d1 = Hit(mPos, R, NormDistMap1, newDir1);
|
---|
| 250 | float3 normal1 = readCubeMap(NormDistMap1, newDir1);
|
---|
[1859] | 251 | bool valid1 = /*dot(normal1, R) < 0 &&*/ dot(newDir1,newDir1) != 0;
|
---|
[1854] | 252 | if(valid1)
|
---|
| 253 | newDir1 = 0;
|
---|
| 254 | float3 newDir2;
|
---|
| 255 | float d2 = Hit(mPos, R, NormDistMap2, newDir2);
|
---|
| 256 | float3 normal2 = readCubeMap(NormDistMap2, newDir2);
|
---|
[1859] | 257 | bool valid2 = /*dot(normal2, R) < 0 &&*/ dot(newDir2,newDir2) != 0;
|
---|
[1854] | 258 | if(valid2)
|
---|
| 259 | newDir2 = 0;
|
---|
| 260 |
|
---|
| 261 | if( !valid1 && !valid2)
|
---|
[1724] | 262 | {
|
---|
[1854] | 263 | Hit(mPos, R, DistanceMap, newTexCoord);
|
---|
| 264 | Color = readCubeMap(CubeMap, newTexCoord );
|
---|
| 265 | //Color = 1;
|
---|
[1724] | 266 | }
|
---|
[1854] | 267 | else
|
---|
| 268 | {
|
---|
| 269 | float d;
|
---|
| 270 | float3 newN;
|
---|
| 271 |
|
---|
| 272 | d = d2;
|
---|
| 273 | newN = normal2;
|
---|
| 274 |
|
---|
| 275 | Color = float4(0,0,1,1);
|
---|
| 276 | if( !valid2 || (valid1 && d1 < d2) )
|
---|
| 277 | {
|
---|
| 278 | d = d1;
|
---|
| 279 | newN = normal1;
|
---|
| 280 | Color = float4(0,1,0,1);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | float3 newV = R;
|
---|
| 284 | float3 newX = mPos + R * d;
|
---|
| 285 | float3 newR = normalize(reflect( newV, newN));
|
---|
| 286 | // float3 newR = normalize(refract( newV, newN, 0.98));
|
---|
[1720] | 287 |
|
---|
[1854] | 288 | Hit(newX, newR, DistanceMap, newTexCoord);
|
---|
| 289 | Color = readCubeMap(CubeMap, newTexCoord );
|
---|
| 290 | //Color = float4(newR,1);
|
---|
| 291 | //Color = float4(1,0,0,1);
|
---|
| 292 | }
|
---|
[1831] | 293 |
|
---|
[1854] | 294 | /*
|
---|
| 295 | return;
|
---|
| 296 |
|
---|
| 297 | float ctheta_in = dot(IN.mNormal,R);
|
---|
| 298 | float ctheta_out = dot(IN.mNormal,-V);
|
---|
[1720] | 299 |
|
---|
[1854] | 300 | float3 F = 0;
|
---|
| 301 |
|
---|
| 302 | // F,P,G számítása
|
---|
| 303 | if ( ctheta_in > 0 && ctheta_out > 0 )
|
---|
| 304 | {
|
---|
| 305 | float3 H = normalize(R - V); // felezõvektor
|
---|
| 306 | float cbeta = dot(H,R);
|
---|
| 307 | F = F0 + (1-F0)*pow(1-cbeta,5);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | Color = Color * float4(F,1); */
|
---|
| 311 | }
|
---|
[1720] | 312 | }
|
---|
| 313 |
|
---|
[1724] | 314 | float4 NormalDistancePS( VertOut IN):COLOR
|
---|
[1720] | 315 | {
|
---|
[1724] | 316 |
|
---|
| 317 | float4 Color = float4(0, 0, 0, 0);
|
---|
| 318 | //return Color;
|
---|
| 319 | Color = float4(normalize(IN.mNormal), length(IN.cPos));
|
---|
| 320 | return Color;
|
---|
[1720] | 321 | } |
---|