Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Textures
- Texture2D shadowMap : register(t0);
- //Samplers
- SamplerComparisonState SCompare : register(s0);
- //Constant buffers
- cbuffer SPerLightCB : register(b0)
- {
- float4 ambientColour;
- float4 diffuseColour;
- float3 lightPos;
- float padding;
- };
- //Input structure
- struct PixelIn
- {
- float4 Pos : SV_POSITION;
- float4 WPos : TEXCOORD0;
- float4 LPos : TEXCOORD1;
- float3 Normal : NORMAL;
- float2 Tex : TEXCOORD2;
- };
- //Constants
- static const float SMAP_SIZE = 1024.0f; //!< Shadow map size
- static const float SMAP_DM = 1.0f / SMAP_SIZE;
- float4 main(PixelIn pin) : SV_Target
- {
- float4 colour = ambientColour;
- //if position is not visible to the light - dont illuminate it
- //results in hard light frustum
- if (pin.LPos.x < -1.0f || pin.LPos.x > 1.0f ||
- pin.LPos.y < -1.0f || pin.LPos.y > 1.0f ||
- pin.LPos.z < 0.0f || pin.LPos.z > 1.0f)
- {
- return colour;
- }
- //transform clip space coords to texture space coords (-1:1 to 0:1)
- pin.LPos.x = pin.LPos.x / 2.0f + 0.5f;
- pin.LPos.y = pin.LPos.y / -2.0f + 0.5f;
- float3 L = normalize(lightPos - pin.WPos.xyz);
- float ndotl = dot(normalize(pin.Normal), L);
- float shadowFactor = 0.0f;
- if (ndotl > 0.0f)
- {
- //PCF Sampling
- float sum = 0.0f;
- const float dm = SMAP_DM;
- const float2 offsets[9] =
- {
- float2(-dm, -dm), float2(0.0f, -dm), float2(dm, -dm),
- float2(-dm, 0.0f), float2(0.0f, 0.0f), float2(dm, 0.0f),
- float2(-dm, +dm), float2(0.0f, +dm), float2(dm, +dm)
- };
- [unroll]
- for (int i = 0; i < 9; ++i)
- {
- sum += shadowMap.SampleCmpLevelZero(SCompare, pin.LPos.xy + offsets[i], pin.LPos.z).r;
- }
- shadowFactor = sum / 9.0f;
- }
- //calculate ilumination
- return colour += shadowFactor * diffuseColour * ndotl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement