1 | float4x4 world_IT;
|
---|
2 | int REDUCED_CUBEMAP_SIZE = 8;
|
---|
3 |
|
---|
4 |
|
---|
5 | float4 readCubeMap(samplerCUBE cm, float3 coord)
|
---|
6 | {
|
---|
7 | float4 color = texCUBE( cm, float3(coord.xy, - coord.z) );
|
---|
8 | return color;
|
---|
9 | }
|
---|
10 |
|
---|
11 | float readDistanceCubeMap(samplerCUBE dcm, float3 coord)
|
---|
12 | {
|
---|
13 | float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).r;
|
---|
14 | if(dist == 0) dist = 4000; ///sky
|
---|
15 | return dist;
|
---|
16 | }
|
---|
17 |
|
---|
18 | // This function is called several times.
|
---|
19 | float3 Hit( float3 x, float3 R, samplerCUBE mp )
|
---|
20 | {
|
---|
21 | float rl = readDistanceCubeMap( mp, R); // |r|
|
---|
22 |
|
---|
23 | float ppp = length( x ) / readDistanceCubeMap( mp, x); // |p|/|p|
|
---|
24 | float dun = 0, pun = ppp, dov = 0, pov = 0;
|
---|
25 | float dl = rl * ( 1 - ppp ); // eq. 2
|
---|
26 | float3 l = x + R * dl; // ray equation
|
---|
27 |
|
---|
28 | // iteration
|
---|
29 | for( int i = 0; i < 2; i++ ) // 2 !!!!!!!!!!!!!!!!!!!!!!!
|
---|
30 | {
|
---|
31 | float llp = length( l ) / readDistanceCubeMap( mp, l); // |l|/|l|
|
---|
32 | if ( llp < 0.999f ) // undershooting
|
---|
33 | {
|
---|
34 | dun = dl; pun = llp; // last undershooting
|
---|
35 | dl += ( dov == 0 ) ? rl * ( 1 - llp ) : // eq. 2
|
---|
36 | ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3
|
---|
37 | } else if ( llp > 1.001f ) // overshooting
|
---|
38 | {
|
---|
39 | dov = dl; pov = llp; // last overshooting
|
---|
40 | dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
|
---|
41 | }
|
---|
42 | l = x + R * dl; // ray equation
|
---|
43 | }
|
---|
44 | return l; // computed hit point
|
---|
45 | }
|
---|
46 |
|
---|
47 | float4 GetContibution(float3 L, float3 pos, float3 N, float3 V, samplerCUBE SmallEnvMapSampler, samplerCUBE DistanceEnvMapSampler) // Phong-Blinn
|
---|
48 | // L: a hossza lényeges (az egységkocka faláig ér)
|
---|
49 | {
|
---|
50 | REDUCED_CUBEMAP_SIZE = 4;
|
---|
51 | float mindist = 1.0;
|
---|
52 |
|
---|
53 | float kd = 0.3; // 0.3
|
---|
54 | float ks = 0; // 0.5
|
---|
55 | float shininess = 10;
|
---|
56 |
|
---|
57 | float l = length(L);
|
---|
58 | L = normalize(L);
|
---|
59 |
|
---|
60 | //dw
|
---|
61 | float dw = 4 / (REDUCED_CUBEMAP_SIZE*REDUCED_CUBEMAP_SIZE*l*l*l + 4/3.1416f);
|
---|
62 | //Lin
|
---|
63 | float4 Lin = readCubeMap(SmallEnvMapSampler, L);
|
---|
64 | //r
|
---|
65 | float doy = readDistanceCubeMap(DistanceEnvMapSampler, L);
|
---|
66 | float dxy = length(L * doy - pos);
|
---|
67 |
|
---|
68 | dxy = max(mindist, dxy);
|
---|
69 |
|
---|
70 |
|
---|
71 | //dws
|
---|
72 | float dws = (doy*doy * dw) / (dxy*dxy*(1 - dw/3.1416f) + doy*doy*dw/3.1416f); // localization:
|
---|
73 | //float dws = dw;
|
---|
74 |
|
---|
75 | //L = L * doy - pos; // L: x->y, az objektumtól induljon, ne a középpontból
|
---|
76 | L = normalize(L);
|
---|
77 | float3 H = normalize(L + V); // felezõvektor
|
---|
78 |
|
---|
79 | float a = kd * max(dot(N,L),0) +
|
---|
80 | ks * pow(max(dot(N,H),0), shininess); // diffuse + specular
|
---|
81 |
|
---|
82 | // 1.: eddigi
|
---|
83 | //return Lin * a * dws;
|
---|
84 |
|
---|
85 | float ctheta_in = dot(N,L);
|
---|
86 | float ctheta_out = dot(N,V);
|
---|
87 |
|
---|
88 | return Lin * a * dws;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void DiffuseVS(float4 position : POSITION,
|
---|
92 | float3 normal : NORMAL,
|
---|
93 | half3 tangent : TEXCOORD1,
|
---|
94 | float2 texCoord : TEXCOORD0,
|
---|
95 | out float2 otexCoord : TEXCOORD0,
|
---|
96 | out float3 wPos : TEXCOORD1,
|
---|
97 | out float3 mNormal : TEXCOORD2,
|
---|
98 | out half3 Tangent : TEXCOORD3, |
---|
99 | out half3 Binormal : TEXCOORD4,
|
---|
100 | out float4 hPos : POSITION,
|
---|
101 | uniform float4x4 worldViewProj,
|
---|
102 | uniform float4x4 world)
|
---|
103 | {
|
---|
104 |
|
---|
105 | hPos = mul(worldViewProj, position);
|
---|
106 | wPos = mul(world, position).xyz;
|
---|
107 | //mNormal = mul(normal, world_IT);
|
---|
108 | mNormal = normal;
|
---|
109 | otexCoord = texCoord;
|
---|
110 | Tangent = tangent;
|
---|
111 | Binormal = cross(tangent, normal);
|
---|
112 | }
|
---|
113 |
|
---|
114 | float4 DiffusePS( float2 Tex : TEXCOORD0,
|
---|
115 | float3 pos : TEXCOORD1,
|
---|
116 | float3 N : TEXCOORD2,
|
---|
117 | half3 Tangent : TEXCOORD3, |
---|
118 | half3 Binormal : TEXCOORD4,
|
---|
119 | uniform float3 cameraPos,
|
---|
120 | uniform float3 lastCenter,
|
---|
121 | uniform half3 lightPosition,
|
---|
122 | uniform samplerCUBE SmallEnvMapSampler : register(s0),
|
---|
123 | uniform samplerCUBE DistanceEnvMapSampler : register(s1),
|
---|
124 | uniform sampler2D ColorTexture : register(s2),
|
---|
125 | uniform sampler2D NormalMap : register(s3)
|
---|
126 | ) : COLOR0
|
---|
127 | {
|
---|
128 | REDUCED_CUBEMAP_SIZE = 4;
|
---|
129 |
|
---|
130 | //V = /*-*/normalize( V );
|
---|
131 | float3 V = normalize(pos - cameraPos); //
|
---|
132 |
|
---|
133 | float3x3 ModelToTangent = float3x3(Tangent, Binormal, N);
|
---|
134 | half3 tNormal = tex2D(NormalMap, Tex).rgb; |
---|
135 |
|
---|
136 | N =mul(tNormal, ModelToTangent );
|
---|
137 | N = mul( N, world_IT );
|
---|
138 | N = normalize(N);
|
---|
139 |
|
---|
140 | float3 R = reflect(V, N);
|
---|
141 | pos -= lastCenter;
|
---|
142 |
|
---|
143 | float4 intens = 0;
|
---|
144 |
|
---|
145 |
|
---|
146 | for (int x = 0; x < REDUCED_CUBEMAP_SIZE; x++) // az envmap minden texelére
|
---|
147 | for (int y = 0; y < REDUCED_CUBEMAP_SIZE; y++)
|
---|
148 | {
|
---|
149 | // intenzitás kiolvasása az adott texelbõl
|
---|
150 |
|
---|
151 | float2 p, tpos;
|
---|
152 | tpos.x = x/(float)REDUCED_CUBEMAP_SIZE; // 0..1
|
---|
153 | tpos.y = y/(float)REDUCED_CUBEMAP_SIZE; // 0..1
|
---|
154 | tpos.xy += float2(0.5/REDUCED_CUBEMAP_SIZE, 0.5/REDUCED_CUBEMAP_SIZE); // az adott texel középpont uv koordinátái
|
---|
155 |
|
---|
156 | p.x = tpos.x;
|
---|
157 | p.y = 1-tpos.y;
|
---|
158 | p.xy = 2*p.xy - 1; // -1..1 // az adott texel középpont pozíciója
|
---|
159 |
|
---|
160 | float3 L;
|
---|
161 |
|
---|
162 | L = float3(p.x, p.y, 1);
|
---|
163 | intens += GetContibution( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
164 |
|
---|
165 | L = float3(p.x, p.y, -1);
|
---|
166 | intens += GetContibution( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
167 |
|
---|
168 | L = float3(p.x, 1, p.y);
|
---|
169 | intens += GetContibution( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
170 |
|
---|
171 | L = float3(p.x, -1, p.y);
|
---|
172 | intens += GetContibution( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
173 |
|
---|
174 | L = float3(1, p.x, p.y);
|
---|
175 | intens += GetContibution( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
176 |
|
---|
177 | L = float3(-1, p.x, p.y);
|
---|
178 | intens += GetContibution( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
179 | }
|
---|
180 |
|
---|
181 | half3 light = lightPosition;
|
---|
182 | half3 L = normalize(light);
|
---|
183 | half3 H = normalize(L + V);
|
---|
184 | half4 lighting = lit(dot(N, L),dot(N, H), 20);
|
---|
185 |
|
---|
186 | float4 retColor = intens;
|
---|
187 | float4 texColor = tex2D(ColorTexture, Tex);
|
---|
188 | retColor = intens * texColor * 1.5
|
---|
189 | + lighting.y * texColor * 0.15
|
---|
190 | + lighting.z * 0.15
|
---|
191 | ;
|
---|
192 | return retColor;
|
---|
193 | } |
---|