1 | float4 readCubeMap(samplerCUBE cm, float3 coord)
|
---|
2 | {
|
---|
3 | float4 color = texCUBE( cm, float3(coord.xy, - coord.z) );
|
---|
4 | return color;
|
---|
5 | }
|
---|
6 |
|
---|
7 | float readDistanceCubeMap(samplerCUBE dcm, float3 coord)
|
---|
8 | {
|
---|
9 | float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).r;
|
---|
10 | if(dist == 0) dist = 40; ///sky
|
---|
11 | return dist;
|
---|
12 | }
|
---|
13 |
|
---|
14 | // This function is called several times.
|
---|
15 | float3 Hit( float3 x, float3 R, samplerCUBE mp )
|
---|
16 | {
|
---|
17 | float rl = readDistanceCubeMap( mp, R); // |r|
|
---|
18 |
|
---|
19 | float ppp = length( x ) / readDistanceCubeMap( mp, x); // |p|/|p|
|
---|
20 | float dun = 0, pun = ppp, dov = 0, pov = 0;
|
---|
21 | float dl = rl * ( 1 - ppp ); // eq. 2
|
---|
22 | float3 l = x + R * dl; // ray equation
|
---|
23 |
|
---|
24 | // iteration
|
---|
25 | for( int i = 0; i < 2; i++ ) // 2 !!!!!!!!!!!!!!!!!!!!!!!
|
---|
26 | {
|
---|
27 | float llp = length( l ) / readDistanceCubeMap( mp, l); // |l|/|l|
|
---|
28 | if ( llp < 0.999f ) // undershooting
|
---|
29 | {
|
---|
30 | dun = dl; pun = llp; // last undershooting
|
---|
31 | dl += ( dov == 0 ) ? rl * ( 1 - llp ) : // eq. 2
|
---|
32 | ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3
|
---|
33 | } else if ( llp > 1.001f ) // overshooting
|
---|
34 | {
|
---|
35 | dov = dl; pov = llp; // last overshooting
|
---|
36 | dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
|
---|
37 | }
|
---|
38 | l = x + R * dl; // ray equation
|
---|
39 | }
|
---|
40 | return l; // computed hit point
|
---|
41 | }
|
---|
42 |
|
---|
43 | void BottleVS(float4 position : POSITION,
|
---|
44 | out float3 wPos : TEXCOORD1,
|
---|
45 | float2 texCoord : TEXCOORD0,
|
---|
46 | out float2 otexCoord : TEXCOORD0,
|
---|
47 | float3 normal : NORMAL,
|
---|
48 | out float3 mNormal : TEXCOORD2,
|
---|
49 | out float4 hPos : POSITION,
|
---|
50 | uniform float4x4 worldViewProj,
|
---|
51 | uniform float4x4 world,
|
---|
52 | uniform float4x4 worldIT)
|
---|
53 | {
|
---|
54 |
|
---|
55 | hPos = mul(worldViewProj, position);
|
---|
56 | wPos = mul(world, position).xyz;
|
---|
57 | mNormal = mul(normal, worldIT);
|
---|
58 | // mNormal = normal;
|
---|
59 | otexCoord = texCoord;
|
---|
60 | }
|
---|
61 |
|
---|
62 | //////////////
|
---|
63 | //Localized reflection
|
---|
64 | //////////////
|
---|
65 | void BottlePS( float2 texCoord : TEXCOORD0,
|
---|
66 | float3 wPos : TEXCOORD1,
|
---|
67 | float3 mNormal : TEXCOORD2,
|
---|
68 | uniform float3 cameraPos,
|
---|
69 | uniform samplerCUBE CubeMap : register(s0),
|
---|
70 | uniform samplerCUBE DistanceMap : register(s1),
|
---|
71 | uniform float3 lastCenter,
|
---|
72 | uniform float3 lightPosition,
|
---|
73 | uniform float3 color,
|
---|
74 | out float4 Color :COLOR0)
|
---|
75 | {
|
---|
76 |
|
---|
77 | Color = float4(1,1,1,1);
|
---|
78 |
|
---|
79 | mNormal = normalize(mNormal);
|
---|
80 | float3 RR, TT;
|
---|
81 | float3 mPos = wPos - lastCenter;
|
---|
82 | float3 V = normalize(wPos - cameraPos);
|
---|
83 | float3 R = normalize(reflect( V, mNormal));
|
---|
84 |
|
---|
85 | float3 T = normalize(refract(V, mNormal, 0.9));
|
---|
86 |
|
---|
87 | RR = R; TT = T;
|
---|
88 |
|
---|
89 | RR = Hit(mPos, R, DistanceMap);
|
---|
90 | TT = Hit(mPos, T, DistanceMap);
|
---|
91 |
|
---|
92 | float4 reflectcolor = readCubeMap(CubeMap, RR );
|
---|
93 | float4 refractcolor = readCubeMap(CubeMap, TT );
|
---|
94 |
|
---|
95 | float cos_theta = -dot(V, mNormal);
|
---|
96 | float sFresnel = 0.2;
|
---|
97 | float F = (sFresnel + pow(1-cos_theta, 5.0f) * (1-sFresnel));
|
---|
98 |
|
---|
99 | Color = (F * reflectcolor + (1-F) * refractcolor * float4(color,1)) ;
|
---|
100 |
|
---|
101 | half3 light = lightPosition;
|
---|
102 | V = -V;
|
---|
103 | half3 L = normalize(light);
|
---|
104 | half3 H = normalize(L+V);
|
---|
105 | half4 lighting = lit(dot(mNormal, L),dot(mNormal, H), 100);
|
---|
106 |
|
---|
107 | Color = (0.7 + lighting.y) * Color + lighting.z;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | float4 BottlePhotonMapPS( float2 texCoord : TEXCOORD0,
|
---|
112 | float3 wPos : TEXCOORD1,
|
---|
113 | float3 mNormal : TEXCOORD2,
|
---|
114 | uniform float3 cameraPos,
|
---|
115 | uniform samplerCUBE DistanceMap : register(s0),
|
---|
116 | uniform float3 lastCenter):COLOR0
|
---|
117 | {
|
---|
118 |
|
---|
119 | float4 Color = float4(1,1,1,1);
|
---|
120 |
|
---|
121 | mNormal = normalize(mNormal);
|
---|
122 | float3 newTexCoord;
|
---|
123 | float3 mPos = wPos - lastCenter;
|
---|
124 | float3 V = normalize(wPos - cameraPos);
|
---|
125 | float3 R = normalize(refract( V, mNormal, 0.9 ));
|
---|
126 |
|
---|
127 | newTexCoord = R;
|
---|
128 |
|
---|
129 | newTexCoord = Hit(mPos, R, DistanceMap);
|
---|
130 |
|
---|
131 | Color = float4(normalize(newTexCoord),1);
|
---|
132 |
|
---|
133 | //if(dot(V,mNormal)>0)
|
---|
134 | //{
|
---|
135 | // Color = float4(1,0,0,0);
|
---|
136 | //}
|
---|
137 | return Color;
|
---|
138 | } |
---|