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