1 | float cutNearness2;// 100.0
|
---|
2 |
|
---|
3 | struct vsInputBushToAtlas
|
---|
4 | {
|
---|
5 | float4 pos : POSITION;
|
---|
6 | float2 tex : TEXCOORD0;
|
---|
7 | float3 normal : NORMAL;
|
---|
8 | };
|
---|
9 |
|
---|
10 |
|
---|
11 | struct vsOutputBushToAtlas
|
---|
12 | {
|
---|
13 | float4 pos : POSITION;
|
---|
14 | float2 tex : TEXCOORD0;
|
---|
15 | float4 worldPos : TEXCOORD1;
|
---|
16 | float3 normal : TEXCOORD2;
|
---|
17 | float4 occProjPos : TEXCOORD3;
|
---|
18 | };
|
---|
19 |
|
---|
20 | vsOutputBushToAtlas
|
---|
21 | vsRenderBushToAtlas(vsInputBushToAtlas input)
|
---|
22 | {
|
---|
23 | vsOutputBushToAtlas output = (vsOutputBushToAtlas)0;
|
---|
24 |
|
---|
25 | output.pos.xy=float2(2,-2)*input.tex-float2(1,1);
|
---|
26 | output.pos.y=-1*output.pos.y; //Texture space to screen space.
|
---|
27 | output.pos.z=0;
|
---|
28 | output.pos.w=1;
|
---|
29 |
|
---|
30 | output.worldPos = mul(input.pos, modelToWorldMatrix);
|
---|
31 | output.normal = mul(inverseTransposedModelToWorldMatrix, float4(input.normal, 0));
|
---|
32 | output.occProjPos = mul(output.worldPos, occWorldToProjMatrix);
|
---|
33 | output.tex = input.tex;
|
---|
34 |
|
---|
35 | return output;
|
---|
36 | }
|
---|
37 |
|
---|
38 | float4
|
---|
39 | psRenderBushToAtlas(vsOutputBushToAtlas input) : COLOR0
|
---|
40 | {
|
---|
41 | input.occProjPos /= input.occProjPos.w;
|
---|
42 | float2 occTexPos = mul(input.occProjPos.xyw, occProjToTexMatrix);
|
---|
43 |
|
---|
44 | input.normal = normalize(input.normal);
|
---|
45 | float3 diff = lightPos - input.worldPos.xyz;
|
---|
46 | float4 retVal = float4(0, 0, 0, 1);
|
---|
47 | float3 diffDir = normalize(diff);
|
---|
48 | float cosa = dot(diffDir, input.normal);
|
---|
49 | float cosb = -dot(diffDir, normalize(lightDir));
|
---|
50 | float dist2 = dot(diff, diff);
|
---|
51 |
|
---|
52 | float visibility = tex2Dlod(depthMapSampler, float4(occTexPos, input.occProjPos.z, 0) );
|
---|
53 | float formFactor;
|
---|
54 | if( cosa > 0.0)
|
---|
55 | {
|
---|
56 | if( cosb < 0.01 && cosb > -0.1)
|
---|
57 | cosb = 0.01;
|
---|
58 | float nearness = cosb * cosb * dist2;
|
---|
59 | if( nearness < cutNearness2)
|
---|
60 | {
|
---|
61 | float e2 = dist2 * (1.0 - cosb * cosb);
|
---|
62 | float dist2dash = e2 + cutNearness2;
|
---|
63 | formFactor = cosa * sqrt(cutNearness2 / dist2dash ) / dist2dash;
|
---|
64 | }
|
---|
65 | else
|
---|
66 | formFactor = cosa * cosb / dist2;
|
---|
67 |
|
---|
68 | if( cosb > 0.0)
|
---|
69 | {
|
---|
70 | retVal = float4(lightPower * formFactor * visibility, 1);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | return retVal;// + float4(0.3, 0, 0, 1);
|
---|
74 | }
|
---|
75 |
|
---|
76 | technique BushToAtlas{
|
---|
77 | pass P0
|
---|
78 | {
|
---|
79 | VertexShader = compile vs_3_0 vsRenderBushToAtlas();
|
---|
80 | PixelShader = compile ps_3_0 psRenderBushToAtlas();
|
---|
81 | }
|
---|
82 | }
|
---|