| 1 | #version 150 |
|---|
| 2 | /* Copyright Torus Knot Software Ltd 2000-2014 |
|---|
| 3 | |
|---|
| 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 5 | of this software and associated documentation files (the "Software"), to deal |
|---|
| 6 | in the Software without restriction, including without limitation the rights |
|---|
| 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 8 | copies of the Software, and to permit persons to whom the Software is |
|---|
| 9 | furnished to do so, subject to the following conditions: |
|---|
| 10 | |
|---|
| 11 | The above copyright notice and this permission notice shall be included in |
|---|
| 12 | all copies or substantial portions of the Software. |
|---|
| 13 | |
|---|
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 20 | THE SOFTWARE. |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | in vec3 diffuseUV; |
|---|
| 24 | uniform sampler2D diffuseMap; |
|---|
| 25 | uniform vec3 materialAmbient; |
|---|
| 26 | |
|---|
| 27 | #if !SHADOWCASTER |
|---|
| 28 | in vec3 col; |
|---|
| 29 | #endif |
|---|
| 30 | #if DEPTH_SHADOWCASTER |
|---|
| 31 | in float depth; |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | #if DEPTH_SHADOWRECEIVER |
|---|
| 35 | in vec4 lightSpacePos0; |
|---|
| 36 | in vec4 lightSpacePos1; |
|---|
| 37 | in vec4 lightSpacePos2; |
|---|
| 38 | uniform sampler2D shadowMap0; |
|---|
| 39 | uniform sampler2D shadowMap1; |
|---|
| 40 | uniform sampler2D shadowMap2; |
|---|
| 41 | uniform float inverseShadowmapSize0; |
|---|
| 42 | uniform float inverseShadowmapSize1; |
|---|
| 43 | uniform float inverseShadowmapSize2; |
|---|
| 44 | uniform vec4 pssmSplitPoints; |
|---|
| 45 | #endif |
|---|
| 46 | |
|---|
| 47 | #if SHADOWCASTER |
|---|
| 48 | uniform vec3 shadowColour; |
|---|
| 49 | #endif |
|---|
| 50 | #if FOG |
|---|
| 51 | uniform vec3 fogColour; |
|---|
| 52 | #endif |
|---|
| 53 | |
|---|
| 54 | out vec4 fragColour; |
|---|
| 55 | |
|---|
| 56 | // Number of samples in one dimension (square for total samples) |
|---|
| 57 | #define NUM_SHADOW_SAMPLES_1D 2.0 |
|---|
| 58 | #define SHADOW_FILTER_SCALE 1 |
|---|
| 59 | |
|---|
| 60 | #define SHADOW_SAMPLES NUM_SHADOW_SAMPLES_1D*NUM_SHADOW_SAMPLES_1D |
|---|
| 61 | |
|---|
| 62 | vec4 offsetSample(vec4 uv, vec2 offset, float invMapSize) |
|---|
| 63 | { |
|---|
| 64 | return vec4(uv.xy + offset * invMapSize * uv.w, uv.z, uv.w); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | float calcDepthShadow(sampler2D shadowMap, vec4 uv, float invShadowMapSize) |
|---|
| 68 | { |
|---|
| 69 | // 4-sample PCF |
|---|
| 70 | |
|---|
| 71 | float shadow = 0.0; |
|---|
| 72 | float offset = (NUM_SHADOW_SAMPLES_1D/2 - 0.5) * SHADOW_FILTER_SCALE; |
|---|
| 73 | for (float y = -offset; y <= offset; y += SHADOW_FILTER_SCALE) |
|---|
| 74 | for (float x = -offset; x <= offset; x += SHADOW_FILTER_SCALE) |
|---|
| 75 | { |
|---|
| 76 | float depth = textureProj(shadowMap, offsetSample(uv, vec2(x, y), invShadowMapSize)).x; |
|---|
| 77 | if (depth >= 1 || depth >= uv.z) |
|---|
| 78 | shadow += 1.0; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | shadow /= SHADOW_SAMPLES; |
|---|
| 82 | |
|---|
| 83 | return shadow; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | float calcPSSMDepthShadow(sampler2D shadowMap0, sampler2D shadowMap1, sampler2D shadowMap2, |
|---|
| 87 | vec4 lsPos0, vec4 lsPos1, vec4 lsPos2, |
|---|
| 88 | float invShadowmapSize0, float invShadowmapSize1, float invShadowmapSize2, |
|---|
| 89 | vec4 pssmSplitPoints, float camDepth) |
|---|
| 90 | { |
|---|
| 91 | |
|---|
| 92 | float shadow; |
|---|
| 93 | vec4 splitColour; |
|---|
| 94 | // calculate shadow |
|---|
| 95 | if (camDepth <= pssmSplitPoints.y) |
|---|
| 96 | { |
|---|
| 97 | splitColour = vec4(0.3, 0.0, 0, 0); |
|---|
| 98 | shadow = calcDepthShadow(shadowMap0, lsPos0, invShadowmapSize0); |
|---|
| 99 | } |
|---|
| 100 | else if (camDepth <= pssmSplitPoints.z) |
|---|
| 101 | { |
|---|
| 102 | splitColour = vec4(0, 0.3, 0, 0); |
|---|
| 103 | shadow = calcDepthShadow(shadowMap1, lsPos1, invShadowmapSize1); |
|---|
| 104 | } |
|---|
| 105 | else |
|---|
| 106 | { |
|---|
| 107 | splitColour = vec4(0.0, 0.0, 0.3, 0); |
|---|
| 108 | shadow = calcDepthShadow(shadowMap2, lsPos2, invShadowmapSize2); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | return shadow; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | void main() |
|---|
| 115 | { |
|---|
| 116 | // look up the diffuse map layer |
|---|
| 117 | vec4 texDiffuse = texture(diffuseMap, diffuseUV.xy); |
|---|
| 118 | |
|---|
| 119 | #if SHADOWCASTER |
|---|
| 120 | # if DEPTH_SHADOWCASTER |
|---|
| 121 | // early-out with depth (we still include alpha for those cards that support it) |
|---|
| 122 | fragColour = vec4(depth, depth, depth, 1); |
|---|
| 123 | # else |
|---|
| 124 | fragColour = vec4(shadowColour.xyz, texDiffuse.a); |
|---|
| 125 | # endif |
|---|
| 126 | |
|---|
| 127 | #else |
|---|
| 128 | // compute the ambient contribution (pulled from the diffuse map) |
|---|
| 129 | vec3 vAmbient = texDiffuse.xyz * materialAmbient.xyz; |
|---|
| 130 | vec3 vColor3 = texDiffuse.rgb * col.rgb; |
|---|
| 131 | |
|---|
| 132 | # if DEPTH_SHADOWRECEIVER |
|---|
| 133 | float camDepth = diffuseUV.z; |
|---|
| 134 | float shadow = calcPSSMDepthShadow(shadowMap0, shadowMap1, shadowMap2, |
|---|
| 135 | lightSpacePos0, lightSpacePos1, lightSpacePos2, |
|---|
| 136 | inverseShadowmapSize0, inverseShadowmapSize1, inverseShadowmapSize2, |
|---|
| 137 | pssmSplitPoints, camDepth); |
|---|
| 138 | vColor3 *= shadow; |
|---|
| 139 | # endif |
|---|
| 140 | |
|---|
| 141 | fragColour = vec4(vColor3 + vAmbient, texDiffuse.a); |
|---|
| 142 | |
|---|
| 143 | # if FOG |
|---|
| 144 | // if fog is active, interpolate between the unfogged color and the fog color |
|---|
| 145 | // based on vertex shader fog value |
|---|
| 146 | fragColour.rgb = mix(vColor.rgb, fogColour, diffuseUV.z).rgb; |
|---|
| 147 | # endif |
|---|
| 148 | |
|---|
| 149 | #endif |
|---|
| 150 | } |
|---|