1 | /****************************************************************************** |
---|
2 | Copyright (c) W.J. van der Laan |
---|
3 | |
---|
4 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
---|
5 | this software and associated documentation files (the "Software"), to deal in |
---|
6 | the Software without restriction, including without limitation the rights to use, |
---|
7 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
---|
8 | Software, and to permit persons to whom the Software is furnished to do so, subject |
---|
9 | to the following conditions: |
---|
10 | |
---|
11 | The above copyright notice and this permission notice shall be included in all copies |
---|
12 | or substantial portions of the Software. |
---|
13 | |
---|
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
15 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
---|
16 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
---|
17 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
---|
18 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE |
---|
19 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
20 | ******************************************************************************/ |
---|
21 | /** Deferred shading framework |
---|
22 | // W.J. :wumpus: van der Laan 2005 // |
---|
23 | |
---|
24 | Post shader: Light geometry material |
---|
25 | */ |
---|
26 | sampler Tex0: register(s0); |
---|
27 | sampler Tex1: register(s1); |
---|
28 | |
---|
29 | float4x4 worldView; |
---|
30 | |
---|
31 | // Attributes of light |
---|
32 | float4 lightDiffuseColor; |
---|
33 | float4 lightSpecularColor; |
---|
34 | float4 lightFalloff; |
---|
35 | |
---|
36 | float4 main(float2 texCoord: TEXCOORD0, float3 projCoord: TEXCOORD1) : COLOR |
---|
37 | { |
---|
38 | float4 a0 = tex2D(Tex0, texCoord); // Attribute 0: Diffuse color+shininess |
---|
39 | float4 a1 = tex2D(Tex1, texCoord); // Attribute 1: Normal+depth |
---|
40 | |
---|
41 | // Attributes |
---|
42 | float3 colour = a0.rgb; |
---|
43 | float alpha = a0.a; // Specularity |
---|
44 | float distance = a1.w; // Distance from viewer (w) |
---|
45 | float3 normal = a1.xyz; |
---|
46 | |
---|
47 | // Calculate position of texel in view space |
---|
48 | float3 position = projCoord*distance; |
---|
49 | |
---|
50 | // Extract position in view space from worldView matrix |
---|
51 | float3 lightPos = float3(worldView[0][3],worldView[1][3],worldView[2][3]); |
---|
52 | |
---|
53 | // Calculate light direction and distance |
---|
54 | float3 lightVec = lightPos - position; |
---|
55 | float len_sq = dot(lightVec, lightVec); |
---|
56 | float len = sqrt(len_sq); |
---|
57 | float3 lightDir = lightVec/len; |
---|
58 | |
---|
59 | /// Calculate attenuation |
---|
60 | float attenuation = dot(lightFalloff, float3(1, len, len_sq)); |
---|
61 | |
---|
62 | /// Calculate diffuse colour |
---|
63 | float3 light_diffuse = max(0,dot(lightDir, normal)) * lightDiffuseColor; |
---|
64 | |
---|
65 | /// Calculate specular component |
---|
66 | float3 viewDir = -normalize(position); |
---|
67 | float3 h = normalize(viewDir + lightDir); |
---|
68 | float3 light_specular = pow(dot(normal, h),32) * lightSpecularColor; |
---|
69 | |
---|
70 | // Calcalate total lighting for this fragment |
---|
71 | float3 total_light_contrib; |
---|
72 | total_light_contrib = light_diffuse; |
---|
73 | // Uncomment next line if specular desired |
---|
74 | //total_light_contrib += alpha * light_specular; |
---|
75 | |
---|
76 | return float4(total_light_contrib*colour/attenuation, 0); |
---|
77 | } |
---|
78 | // Debugging only |
---|
79 | //return lightDiffuseColor; |
---|
80 | //return lightDiffuseColor/attenuation; |
---|
81 | //return float4(abs(position-position2),0); |
---|
82 | //return length(lightPos-position)/1000.0; |
---|
83 | //return float4(abs(lightPos-lightPos2),0); |
---|
84 | //return float4(lightPos-position,0); |
---|
85 | //return float4(-global.position/1000.0f,0); |
---|
86 | //return float4(global.position/1000.0,0); |
---|
87 | ///return float4(lightPos[0], lightPos[1], -lightPos[2], 0)/1000.0f; |
---|
88 | //return lightDiffuseColor*a0.xyzw/2; |
---|
89 | |
---|