1 | float4x4 modelToWorldMatrix;
|
---|
2 | float4x4 inverseTransposedModelToWorldMatrix;
|
---|
3 | float4x4 modelToProjMatrix;
|
---|
4 |
|
---|
5 | float4x4 occWorldToProjMatrix;
|
---|
6 | float3x3 occProjToTexMatrix;
|
---|
7 |
|
---|
8 | int2 prmAtlasTiles;
|
---|
9 | float2 atlasHalfPixel;
|
---|
10 |
|
---|
11 | texture brdfMap; //material texture sampler
|
---|
12 | sampler2D brdfMapSampler = sampler_state{
|
---|
13 | texture = < brdfMap >;
|
---|
14 | MinFilter = LINEAR;
|
---|
15 | MagFilter = LINEAR;
|
---|
16 | MipFilter = None;
|
---|
17 | AddressU = Wrap;
|
---|
18 | AddressV = Wrap;
|
---|
19 | };
|
---|
20 |
|
---|
21 | texture bumpMap; //material texture sampler
|
---|
22 | sampler2D bumpMapSampler = sampler_state{
|
---|
23 | texture = < bumpMap >;
|
---|
24 | MinFilter = LINEAR;
|
---|
25 | MagFilter = LINEAR;
|
---|
26 | MipFilter = None;
|
---|
27 | AddressU = Wrap;
|
---|
28 | AddressV = Wrap;
|
---|
29 | };
|
---|
30 |
|
---|
31 | texture normalMap; //material texture sampler
|
---|
32 | sampler2D normalMapSampler = sampler_state{
|
---|
33 | texture = < normalMap >;
|
---|
34 | MinFilter = LINEAR;
|
---|
35 | MagFilter = LINEAR;
|
---|
36 | MipFilter = None;
|
---|
37 | AddressU = Wrap;
|
---|
38 | AddressV = Wrap;
|
---|
39 | };
|
---|
40 |
|
---|
41 | texture filteredAtlas;
|
---|
42 | sampler filteredAtlasSampler = sampler_state
|
---|
43 | {
|
---|
44 | Texture = <filteredAtlas>;
|
---|
45 | MinFilter = LINEAR;
|
---|
46 | MagFilter = LINEAR;
|
---|
47 | MipFilter = None;
|
---|
48 | AddressU = Wrap;
|
---|
49 | AddressV = Wrap;
|
---|
50 | };
|
---|
51 |
|
---|
52 | texture depthMap;
|
---|
53 | sampler2D depthMapSampler = sampler_state{
|
---|
54 | texture = < depthMap >;
|
---|
55 | MinFilter = Linear;
|
---|
56 | MagFilter = Linear;
|
---|
57 | MipFilter = None;
|
---|
58 | AddressU = Clamp;
|
---|
59 | AddressV = Clamp;
|
---|
60 | // AddressU = Border;
|
---|
61 | // AddressV = Border;
|
---|
62 | // BorderColor = float4(65535, 65535, 65535, 65535);
|
---|
63 | };
|
---|
64 |
|
---|
65 | float3 lightPos;
|
---|
66 | float3 lightDir;
|
---|
67 | float3 lightPower;
|
---|
68 |
|
---|
69 | float4 weightsa[8];
|
---|
70 |
|
---|
71 | struct vsInputWalk
|
---|
72 | {
|
---|
73 | float4 pos : POSITION;
|
---|
74 | float3 normal : NORMAL;
|
---|
75 | float2 tex : TEXCOORD0;
|
---|
76 | float2 texAtlas : TEXCOORD1;
|
---|
77 | float3 tangent : TEXCOORD2;
|
---|
78 | float3 binormal : TEXCOORD3;
|
---|
79 | };
|
---|
80 |
|
---|
81 | struct vsOutputWalk
|
---|
82 | {
|
---|
83 | float4 pos : POSITION;
|
---|
84 | float3 normal : NORMAL;
|
---|
85 | float3 tangent : TEXCOORD4;
|
---|
86 | float3 binormal : TEXCOORD5;
|
---|
87 |
|
---|
88 | float2 tex : TEXCOORD0;
|
---|
89 | float2 texAtlas : TEXCOORD1;
|
---|
90 | float4 worldPos : TEXCOORD2;
|
---|
91 | float4 occProjPos : TEXCOORD3;
|
---|
92 | };
|
---|
93 |
|
---|
94 |
|
---|
95 | vsOutputWalk
|
---|
96 | vsWalk(vsInputWalk input)
|
---|
97 | {
|
---|
98 | vsOutputWalk output = (vsOutputWalk)0;
|
---|
99 | output.pos = mul(input.pos, modelToProjMatrix);
|
---|
100 |
|
---|
101 | output.worldPos = mul(input.pos, modelToWorldMatrix);
|
---|
102 | output.occProjPos = mul(output.worldPos, occWorldToProjMatrix);
|
---|
103 |
|
---|
104 | output.normal = mul(inverseTransposedModelToWorldMatrix, float4(input.normal.xyz, 0.0));
|
---|
105 | output.tangent = mul(inverseTransposedModelToWorldMatrix, float4(input.tangent.xyz, 0.0));
|
---|
106 | output.binormal = mul(inverseTransposedModelToWorldMatrix, float4(input.binormal.xyz, 0.0));
|
---|
107 | output.tex = input.tex;
|
---|
108 | output.texAtlas = input.texAtlas;
|
---|
109 |
|
---|
110 | return output;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | float4
|
---|
115 | psWalk(vsOutputWalk input) : COLOR
|
---|
116 | {
|
---|
117 | float3 col = 0;
|
---|
118 | for(int iCluster=0; iCluster<32; iCluster++)
|
---|
119 | {
|
---|
120 | float2 prmTexPos = float2(
|
---|
121 | (input.texAtlas.x + (iCluster % prmAtlasTiles.x)) / prmAtlasTiles.x,
|
---|
122 | 1.0 - (input.texAtlas.y + (iCluster / prmAtlasTiles.x)) / prmAtlasTiles.y) + atlasHalfPixel;
|
---|
123 | float4 weight = weightsa[iCluster/4];
|
---|
124 | //weight = 0.065;
|
---|
125 | float3 val = tex2D(filteredAtlasSampler, prmTexPos);
|
---|
126 | col += val.xyz * weight.x;
|
---|
127 | iCluster++;
|
---|
128 |
|
---|
129 | prmTexPos.x += 1.0 / prmAtlasTiles.x;
|
---|
130 | val = tex2D(filteredAtlasSampler, prmTexPos);
|
---|
131 | col += val.xyz * weight.y;
|
---|
132 | iCluster++;
|
---|
133 |
|
---|
134 | prmTexPos.x += 1.0 / prmAtlasTiles.x;
|
---|
135 | val = tex2D(filteredAtlasSampler, prmTexPos);
|
---|
136 | col += val.xyz * weight.z;
|
---|
137 | iCluster++;
|
---|
138 |
|
---|
139 | prmTexPos.x += 1.0 / prmAtlasTiles.x;
|
---|
140 | val = tex2D(filteredAtlasSampler, prmTexPos);
|
---|
141 | col += val.xyz * weight.w;
|
---|
142 | }
|
---|
143 | // col *= 1000.01;
|
---|
144 | // col *= 0.8;
|
---|
145 | // col = 0;
|
---|
146 |
|
---|
147 | // get tangent space normal vector |
---|
148 | /* float3 tNormal = tex2D(normalMapSampler, input.tex).rgb; |
---|
149 | // which is the transpose of ModelToTangent |
---|
150 | float3 mNormal = normalize( input.tangent * tNormal.x + input.binormal * tNormal.y + input.normal * tNormal.z );
|
---|
151 |
|
---|
152 | input.normal = mNormal;
|
---|
153 | */
|
---|
154 | // return float4(input.normal, 1);
|
---|
155 |
|
---|
156 | input.occProjPos /= input.occProjPos.w;
|
---|
157 | float2 occTexPos = mul(input.occProjPos.xyw, occProjToTexMatrix);
|
---|
158 | float3 toLight = lightPos - input.worldPos;
|
---|
159 | float3 toLightDir = normalize(lightPos - input.worldPos);
|
---|
160 | float cosa = dot(input.normal, toLightDir);
|
---|
161 | float cosb = dot(lightDir, -toLightDir);
|
---|
162 |
|
---|
163 | float visibility = tex2Dproj(depthMapSampler, float4(occTexPos, input.occProjPos.z, 1));
|
---|
164 | if(cosa > 0 && cosb > 0.0)
|
---|
165 | col += cosa.xxx * pow(cosb, 9) * visibility / dot(toLight, toLight);
|
---|
166 |
|
---|
167 | // return float4(input.tangent,1);
|
---|
168 | // return tex2D(filteredAtlasSampler, float2(input.texAtlas.x / 32.0, 1.0 - input.texAtlas.y));
|
---|
169 | return float4( lightPower * col * tex2D(brdfMapSampler, input.tex) , 1);
|
---|
170 | // return float4(lightPower * col * tex2D(bumpMapSampler, input.tex), 1);
|
---|
171 | }
|
---|
172 |
|
---|
173 | float4
|
---|
174 | psWalkIndirect(vsOutputWalk input) : COLOR
|
---|
175 | {
|
---|
176 | float3 col = 0;
|
---|
177 | for(int iCluster=0; iCluster<32; iCluster++)
|
---|
178 | {
|
---|
179 | float2 prmTexPos = float2(
|
---|
180 | (input.texAtlas.x + (iCluster % prmAtlasTiles.x)) / prmAtlasTiles.x,
|
---|
181 | 1.0 - (input.texAtlas.y + (iCluster / prmAtlasTiles.x)) / prmAtlasTiles.y) + atlasHalfPixel;
|
---|
182 | float4 weight = weightsa[iCluster/4];
|
---|
183 | // weight = 0.065;
|
---|
184 | float3 val = tex2D(filteredAtlasSampler, prmTexPos);
|
---|
185 | col += val.xyz * weight.x;
|
---|
186 | iCluster++;
|
---|
187 |
|
---|
188 | prmTexPos.x += 1.0 / prmAtlasTiles.x;
|
---|
189 | val = tex2D(filteredAtlasSampler, prmTexPos);
|
---|
190 | col += val.xyz * weight.y;
|
---|
191 | iCluster++;
|
---|
192 |
|
---|
193 | prmTexPos.x += 1.0 / prmAtlasTiles.x;
|
---|
194 | val = tex2D(filteredAtlasSampler, prmTexPos);
|
---|
195 | col += val.xyz * weight.z;
|
---|
196 | iCluster++;
|
---|
197 |
|
---|
198 | prmTexPos.x += 1.0 / prmAtlasTiles.x;
|
---|
199 | val = tex2D(filteredAtlasSampler, prmTexPos);
|
---|
200 | col += val.xyz * weight.w;
|
---|
201 | }
|
---|
202 |
|
---|
203 | return float4( lightPower * col * tex2D(brdfMapSampler, input.tex) , 1);
|
---|
204 | // return float4( lightPower * col, 1);
|
---|
205 | }
|
---|
206 |
|
---|
207 | float4
|
---|
208 | psWalkDirect(vsOutputWalk input) : COLOR
|
---|
209 | {
|
---|
210 | float3 col = 0;
|
---|
211 |
|
---|
212 | input.occProjPos /= input.occProjPos.w;
|
---|
213 | float2 occTexPos = mul(input.occProjPos.xyw, occProjToTexMatrix);
|
---|
214 | float3 toLight = lightPos - input.worldPos;
|
---|
215 | float3 toLightDir = normalize(lightPos - input.worldPos);
|
---|
216 | float cosa = dot(input.normal, toLightDir);
|
---|
217 | float cosb = dot(lightDir, -toLightDir);
|
---|
218 |
|
---|
219 | float visibility = tex2Dproj(depthMapSampler, float4(occTexPos, input.occProjPos.z, 1));
|
---|
220 | if(cosa > 0 && cosb > 0.0)
|
---|
221 | col += cosa.xxx * pow(cosb, 9) * visibility / dot(toLight, toLight);
|
---|
222 |
|
---|
223 | return float4( lightPower * col * tex2D(brdfMapSampler, input.tex) , 1);
|
---|
224 | }
|
---|
225 |
|
---|
226 | float4
|
---|
227 | psWalkAmbient(vsOutputWalk input) : COLOR
|
---|
228 | {
|
---|
229 | float3 col = 0.0001;
|
---|
230 |
|
---|
231 | input.occProjPos /= input.occProjPos.w;
|
---|
232 | float2 occTexPos = mul(input.occProjPos.xyw, occProjToTexMatrix);
|
---|
233 | float3 toLight = lightPos - input.worldPos;
|
---|
234 | float3 toLightDir = normalize(lightPos - input.worldPos);
|
---|
235 | float cosa = dot(input.normal, toLightDir);
|
---|
236 | float cosb = dot(lightDir, -toLightDir);
|
---|
237 |
|
---|
238 | float visibility = tex2Dproj(depthMapSampler, float4(occTexPos, input.occProjPos.z, 1));
|
---|
239 | if(cosa > 0 && cosb > 0.0)
|
---|
240 | col += cosa.xxx * pow(cosb, 9) * visibility / dot(toLight, toLight);
|
---|
241 |
|
---|
242 | return float4( lightPower * col * tex2D(brdfMapSampler, input.tex) , 1);
|
---|
243 | }
|
---|
244 |
|
---|
245 | technique walk{
|
---|
246 | pass P0
|
---|
247 | {
|
---|
248 | VertexShader = compile vs_3_0 vsWalk();
|
---|
249 | PixelShader = compile ps_3_0 psWalk();
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | technique walkIndirect{
|
---|
254 | pass P0
|
---|
255 | {
|
---|
256 | VertexShader = compile vs_3_0 vsWalk();
|
---|
257 | PixelShader = compile ps_3_0 psWalkIndirect();
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | technique walkAmbient{
|
---|
262 | pass P0
|
---|
263 | {
|
---|
264 | VertexShader = compile vs_3_0 vsWalk();
|
---|
265 | PixelShader = compile ps_3_0 psWalkAmbient();
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | technique walkDirect{
|
---|
270 | pass P0
|
---|
271 | {
|
---|
272 | VertexShader = compile vs_3_0 vsWalk();
|
---|
273 | PixelShader = compile ps_3_0 psWalkDirect();
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | #include "bushToAtlas.fx"
|
---|
278 | #include "normalize.fx"
|
---|
279 | #include "showTex.fx"
|
---|
280 | #include "computeWeights.fx"
|
---|
281 | #include "depthMap.fx"
|
---|
282 | #include "dots.fx"
|
---|
283 | #include "torch.tx" |
---|