1 | /* Copyright Torus Knot Software Ltd 2000-2014 |
---|
2 | |
---|
3 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
4 | of this software and associated documentation files (the "Software"), to deal |
---|
5 | in the Software without restriction, including without limitation the rights |
---|
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
7 | copies of the Software, and to permit persons to whom the Software is |
---|
8 | furnished to do so, subject to the following conditions: |
---|
9 | |
---|
10 | The above copyright notice and this permission notice shall be included in |
---|
11 | all copies or substantial portions of the Software. |
---|
12 | |
---|
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
19 | THE SOFTWARE. |
---|
20 | */ |
---|
21 | #include "shadows.cg" |
---|
22 | |
---|
23 | #define BIAS 0 |
---|
24 | |
---|
25 | struct VS_OUT |
---|
26 | { |
---|
27 | float4 pos : POSITION; |
---|
28 | float3 diffuseUV : TEXCOORD0; // xy = UV, z = fog/depth |
---|
29 | #if !SHADOWCASTER |
---|
30 | float3 col : COLOR; |
---|
31 | #endif |
---|
32 | #if DEPTH_SHADOWCASTER |
---|
33 | float depth : TEXCOORD1; |
---|
34 | #endif |
---|
35 | #if DEPTH_SHADOWRECEIVER |
---|
36 | float4 lightSpacePos0 : TEXCOORD2; |
---|
37 | float4 lightSpacePos1 : TEXCOORD3; |
---|
38 | float4 lightSpacePos2 : TEXCOORD4; |
---|
39 | #endif |
---|
40 | }; |
---|
41 | |
---|
42 | VS_OUT main_vp( |
---|
43 | float4 pos : POSITION, |
---|
44 | float3 normal : NORMAL, |
---|
45 | float4 uv0 : TEXCOORD0, |
---|
46 | |
---|
47 | uniform float4x4 worldViewProj, |
---|
48 | uniform float4 lightPosition, |
---|
49 | uniform float3 lightDiffuse |
---|
50 | #if FOG |
---|
51 | , uniform float2 fogParams // x = fog start, y = fog distance |
---|
52 | #endif |
---|
53 | #if DEPTH_SHADOWCASTER |
---|
54 | , uniform float4 depthRange // x = min, y = max, z = range, w = 1/range |
---|
55 | #elif DEPTH_SHADOWRECEIVER |
---|
56 | , uniform float4 depthRange0 // x = min, y = max, z = range, w = 1/range |
---|
57 | , uniform float4 depthRange1 // x = min, y = max, z = range, w = 1/range |
---|
58 | , uniform float4 depthRange2 // x = min, y = max, z = range, w = 1/range |
---|
59 | #endif |
---|
60 | #if DEPTH_SHADOWRECEIVER |
---|
61 | , uniform float4x4 texWorldViewProjMatrix0 |
---|
62 | , uniform float4x4 texWorldViewProjMatrix1 |
---|
63 | , uniform float4x4 texWorldViewProjMatrix2 |
---|
64 | #endif |
---|
65 | |
---|
66 | ) |
---|
67 | { |
---|
68 | VS_OUT outp; |
---|
69 | |
---|
70 | // project position to the screen |
---|
71 | outp.pos = mul(worldViewProj, pos); |
---|
72 | |
---|
73 | #if !SHADOWCASTER |
---|
74 | // Get object space light direction |
---|
75 | float3 lightDir = normalize(lightPosition.xyz - (pos.xyz * lightPosition.w).xyz); |
---|
76 | outp.col = lightDiffuse.xyz * max(dot(lightDir, normal.xyz), 0.0); |
---|
77 | # if FOG |
---|
78 | outp.diffuseUV.z = linearFog(outp.pos.z, fogParams.x, fogParams.y); |
---|
79 | # endif |
---|
80 | |
---|
81 | #endif |
---|
82 | |
---|
83 | // pass through other texcoords exactly as they were received |
---|
84 | outp.diffuseUV.xy = uv0.xy; |
---|
85 | |
---|
86 | |
---|
87 | #if DEPTH_SHADOWCASTER |
---|
88 | outp.depth = (BIAS + outp.pos.z - depthRange.x) * depthRange.w; |
---|
89 | #endif |
---|
90 | |
---|
91 | #if DEPTH_SHADOWRECEIVER |
---|
92 | // Calculate the position of vertex in light space |
---|
93 | outp.lightSpacePos0 = mul(texWorldViewProjMatrix0, pos); |
---|
94 | outp.lightSpacePos1 = mul(texWorldViewProjMatrix1, pos); |
---|
95 | outp.lightSpacePos2 = mul(texWorldViewProjMatrix2, pos); |
---|
96 | |
---|
97 | // make linear |
---|
98 | outp.lightSpacePos0.z = (outp.lightSpacePos0.z - depthRange0.x) * depthRange0.w; |
---|
99 | outp.lightSpacePos1.z = (outp.lightSpacePos1.z - depthRange1.x) * depthRange1.w; |
---|
100 | outp.lightSpacePos2.z = (outp.lightSpacePos2.z - depthRange2.x) * depthRange2.w; |
---|
101 | |
---|
102 | // pass cam depth |
---|
103 | outp.diffuseUV.z = outp.pos.z; |
---|
104 | |
---|
105 | #endif |
---|
106 | |
---|
107 | return outp; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | float4 main_fp( |
---|
112 | VS_OUT In, |
---|
113 | |
---|
114 | uniform sampler2D diffuseMap : register(s0), |
---|
115 | #if DEPTH_SHADOWRECEIVER |
---|
116 | uniform sampler2D shadowMap0 : register(s1), |
---|
117 | uniform sampler2D shadowMap1 : register(s2), |
---|
118 | uniform sampler2D shadowMap2 : register(s3), |
---|
119 | #endif |
---|
120 | |
---|
121 | uniform float3 materialAmbient |
---|
122 | |
---|
123 | #if SHADOWCASTER |
---|
124 | , uniform float3 shadowColour |
---|
125 | #endif |
---|
126 | #if FOG |
---|
127 | , uniform float3 fogColour |
---|
128 | #endif |
---|
129 | #if DEPTH_SHADOWRECEIVER |
---|
130 | , uniform float inverseShadowmapSize0 |
---|
131 | , uniform float inverseShadowmapSize1 |
---|
132 | , uniform float inverseShadowmapSize2 |
---|
133 | , uniform float4 pssmSplitPoints |
---|
134 | #endif |
---|
135 | |
---|
136 | ) : COLOR |
---|
137 | { |
---|
138 | |
---|
139 | // look up the diffuse map layer |
---|
140 | float4 texDiffuse = tex2D(diffuseMap, In.diffuseUV.xy); |
---|
141 | |
---|
142 | #if SHADOWCASTER |
---|
143 | # if DEPTH_SHADOWCASTER |
---|
144 | // early-out with depth (we still include alpha for those cards that support it) |
---|
145 | return float4(In.depth.xxx, 1); |
---|
146 | # else |
---|
147 | return float4(shadowColour.xyz, texDiffuse.a); |
---|
148 | # endif |
---|
149 | |
---|
150 | #else |
---|
151 | // compute the ambient contribution (pulled from the diffuse map) |
---|
152 | float3 vAmbient = texDiffuse.xyz * materialAmbient.xyz; |
---|
153 | float3 vColor3 = texDiffuse.rgb * In.col.rgb; |
---|
154 | |
---|
155 | # if DEPTH_SHADOWRECEIVER |
---|
156 | float camDepth = In.diffuseUV.z; |
---|
157 | float shadow = calcPSSMDepthShadow(shadowMap0, shadowMap1, shadowMap2, |
---|
158 | In.lightSpacePos0, In.lightSpacePos1, In.lightSpacePos2, |
---|
159 | inverseShadowmapSize0, inverseShadowmapSize1, inverseShadowmapSize2, |
---|
160 | pssmSplitPoints, camDepth); |
---|
161 | vColor3 *= shadow; |
---|
162 | /* |
---|
163 | vAmbient = float3(0,0,0); |
---|
164 | vColor3 = calcPSSMDebugShadow(shadowMap0, shadowMap1, shadowMap2, |
---|
165 | In.lightSpacePos0, In.lightSpacePos1, In.lightSpacePos2, |
---|
166 | inverseShadowmapSize0, inverseShadowmapSize1, inverseShadowmapSize2, |
---|
167 | pssmSplitPoints, camDepth); |
---|
168 | */ |
---|
169 | # endif |
---|
170 | |
---|
171 | float4 vColor; |
---|
172 | vColor = float4(vColor3 + vAmbient, texDiffuse.a); |
---|
173 | |
---|
174 | # if FOG |
---|
175 | // if fog is active, interpolate between the unfogged color and the fog color |
---|
176 | // based on vertex shader fog value |
---|
177 | vColor.rgb = lerp(vColor.rgb, fogColour, In.diffuseUV.z).rgb; |
---|
178 | # endif |
---|
179 | |
---|
180 | return vColor; |
---|
181 | #endif |
---|
182 | |
---|
183 | } |
---|
184 | |
---|
185 | |
---|