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 | |
---|
22 | // Simple PCF |
---|
23 | // Number of samples in one dimension (square for total samples) |
---|
24 | #define NUM_SHADOW_SAMPLES_1D 2.0 |
---|
25 | #define SHADOW_FILTER_SCALE 1 |
---|
26 | |
---|
27 | #define SHADOW_SAMPLES NUM_SHADOW_SAMPLES_1D*NUM_SHADOW_SAMPLES_1D |
---|
28 | |
---|
29 | float4 offsetSample(float4 uv, float2 offset, float invMapSize) |
---|
30 | { |
---|
31 | return float4(uv.xy + offset * invMapSize * uv.w, uv.z, uv.w); |
---|
32 | } |
---|
33 | |
---|
34 | float calcDepthShadow(sampler2D shadowMap, float4 uv, float invShadowMapSize) |
---|
35 | { |
---|
36 | // 4-sample PCF |
---|
37 | |
---|
38 | float shadow = 0.0; |
---|
39 | float offset = (NUM_SHADOW_SAMPLES_1D/2 - 0.5) * SHADOW_FILTER_SCALE; |
---|
40 | for (float y = -offset; y <= offset; y += SHADOW_FILTER_SCALE) |
---|
41 | for (float x = -offset; x <= offset; x += SHADOW_FILTER_SCALE) |
---|
42 | { |
---|
43 | float depth = tex2Dproj(shadowMap, offsetSample(uv, float2(x, y), invShadowMapSize)).x; |
---|
44 | if (depth >= 1 || depth >= uv.z) |
---|
45 | shadow += 1.0; |
---|
46 | } |
---|
47 | |
---|
48 | shadow /= SHADOW_SAMPLES; |
---|
49 | |
---|
50 | return shadow; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | float calcSimpleShadow(sampler2D shadowMap, float4 shadowMapPos) |
---|
55 | { |
---|
56 | return tex2Dproj(shadowMap, shadowMapPos).x; |
---|
57 | } |
---|
58 | |
---|
59 | float calcPSSMDepthShadow(sampler2D shadowMap0, sampler2D shadowMap1, sampler2D shadowMap2, |
---|
60 | float4 lsPos0, float4 lsPos1, float4 lsPos2, |
---|
61 | float invShadowmapSize0, float invShadowmapSize1, float invShadowmapSize2, |
---|
62 | float4 pssmSplitPoints, float camDepth) |
---|
63 | { |
---|
64 | |
---|
65 | float shadow; |
---|
66 | float4 splitColour; |
---|
67 | // calculate shadow |
---|
68 | if (camDepth <= pssmSplitPoints.y) |
---|
69 | { |
---|
70 | splitColour = float4(0.3, 0.0, 0, 0); |
---|
71 | shadow = calcDepthShadow(shadowMap0, lsPos0, invShadowmapSize0); |
---|
72 | } |
---|
73 | else if (camDepth <= pssmSplitPoints.z) |
---|
74 | { |
---|
75 | splitColour = float4(0, 0.3, 0, 0); |
---|
76 | shadow = calcDepthShadow(shadowMap1, lsPos1, invShadowmapSize1); |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | splitColour = float4(0.0, 0.0, 0.3, 0); |
---|
81 | shadow = calcDepthShadow(shadowMap2, lsPos2, invShadowmapSize2); |
---|
82 | } |
---|
83 | |
---|
84 | return shadow; |
---|
85 | } |
---|
86 | |
---|
87 | float calcPSSMSimpleShadow(sampler2D shadowMap0, sampler2D shadowMap1, sampler2D shadowMap2, |
---|
88 | float4 lsPos0, float4 lsPos1, float4 lsPos2, |
---|
89 | float4 pssmSplitPoints, float camDepth) |
---|
90 | { |
---|
91 | |
---|
92 | float shadow; |
---|
93 | float4 splitColour; |
---|
94 | // calculate shadow |
---|
95 | if (camDepth <= pssmSplitPoints.y) |
---|
96 | { |
---|
97 | splitColour = float4(0.3, 0.0, 0, 0); |
---|
98 | shadow = calcSimpleShadow(shadowMap0, lsPos0); |
---|
99 | } |
---|
100 | else if (camDepth <= pssmSplitPoints.z) |
---|
101 | { |
---|
102 | splitColour = float4(0, 0.3, 0, 0); |
---|
103 | shadow = calcSimpleShadow(shadowMap1, lsPos1); |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | splitColour = float4(0.0, 0.0, 0.3, 0); |
---|
108 | shadow = calcSimpleShadow(shadowMap2, lsPos2); |
---|
109 | } |
---|
110 | |
---|
111 | return shadow; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | float3 calcPSSMDebugShadow(sampler2D shadowMap0, sampler2D shadowMap1, sampler2D shadowMap2, |
---|
117 | float4 lsPos0, float4 lsPos1, float4 lsPos2, |
---|
118 | float invShadowmapSize0, float invShadowmapSize1, float invShadowmapSize2, |
---|
119 | float4 pssmSplitPoints, float camDepth) |
---|
120 | { |
---|
121 | |
---|
122 | float4 splitColour; |
---|
123 | // calculate shadow |
---|
124 | if (camDepth <= pssmSplitPoints.y) |
---|
125 | { |
---|
126 | //splitColour = float4(0.3, 0.0, 0, 0); |
---|
127 | //splitColour = lsPos0 / lsPos0.w; |
---|
128 | splitColour.rgb = tex2Dproj(shadowMap0, lsPos0).x; |
---|
129 | } |
---|
130 | else if (camDepth <= pssmSplitPoints.z) |
---|
131 | { |
---|
132 | //splitColour = float4(0, 0.3, 0, 0); |
---|
133 | //splitColour = lsPos1 / lsPos1.w; |
---|
134 | splitColour.rgb = tex2Dproj(shadowMap1, lsPos1).x; |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | //splitColour = float4(0.0, 0.0, 0.3, 0); |
---|
139 | //splitColour = lsPos2 / lsPos2.w; |
---|
140 | splitColour.rgb = tex2Dproj(shadowMap2, lsPos2).x; |
---|
141 | } |
---|
142 | |
---|
143 | return splitColour.rgb; |
---|
144 | } |
---|