Advertisement
Guest User

Untitled

a guest
May 30th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.27 KB | None | 0 0
  1. float4x4 World;
  2. float4x4 View;
  3. float4x4 Projection;
  4.  
  5. float SpecularPower = 16;
  6.  
  7. float3 LightDirection;
  8. float4x4 LightView;
  9. float4x4 LightProjection;
  10. float Bias;
  11. float ShadowMapSize = 2048;
  12.  
  13. Texture2D ShadowMap;
  14. sampler ShadowMapSampler = sampler_state {
  15.     texture = <ShadowMap>;
  16.     magfilter = POINT;
  17.     minfilter = POINT;
  18.     mipfilter = POINT;
  19.     AddressU = CLAMP;
  20.     AddressV = CLAMP;
  21. };
  22.  
  23. // The input for the VertexShader
  24. struct VertexShaderInput
  25. {
  26.     float4 Position : POSITION;
  27.     float3 Normal : NORMAL;
  28. };
  29.  
  30. // The output from the vertex shader, used for later processing
  31. struct VertexShaderOutput
  32. {
  33.     float4 Position : SV_POSITION;
  34.     float3 Normal : TEXCOORD0;
  35.     float4 LightViewPosition : TEXCOORD1;
  36.     float4 LightDirection : TEXCOORD2;
  37. };
  38.  
  39. struct PixelShaderOutput
  40. {
  41.     float4 Color : SV_TARGET0;
  42. };
  43.  
  44. //between 3 and 7 for decent results?
  45. float CalcShadowTermSoftPCF(float fLightDepth, float ndotl, float2 vTexCoord, int iSqrtSamples)
  46. {
  47.     float fShadowTerm = 0.0f;
  48.  
  49.     float variableBias = clamp(0.0005 * tan(acos(ndotl)), 0.00001, Bias);
  50.  
  51.     float shadowMapSize = ShadowMapSize.x;
  52.  
  53.     float fRadius = iSqrtSamples - 1; //mad(iSqrtSamples, 0.5, -0.5);//(iSqrtSamples - 1.0f) / 2;
  54.  
  55.     [unroll]
  56.     for (float y = -fRadius; y <= fRadius; y++)
  57.     {
  58.         [unroll]
  59.         for (float x = -fRadius; x <= fRadius; x++)
  60.         {
  61.             float2 vOffset = 0;
  62.             vOffset = float2(x, y);
  63.             vOffset /= shadowMapSize;
  64.             //vOffset *= 2;
  65.             //vOffset /= variableBias*200;
  66.             float2 vSamplePoint = vTexCoord + vOffset;
  67.             float fDepth = ShadowMap.Sample(ShadowMapSampler, vSamplePoint).r;
  68.             float fSample = (fLightDepth <= fDepth + Bias);
  69.  
  70.             // Edge tap smoothing
  71.             float xWeight = 1;
  72.             float yWeight = 1;
  73.  
  74.             if (x == -fRadius)
  75.                 xWeight = 1 - frac(vTexCoord.x * shadowMapSize);
  76.             else if (x == fRadius)
  77.                 xWeight = frac(vTexCoord.x * shadowMapSize);
  78.  
  79.             if (y == -fRadius)
  80.                 yWeight = 1 - frac(vTexCoord.y * shadowMapSize);
  81.             else if (y == fRadius)
  82.                 yWeight = frac(vTexCoord.y * shadowMapSize);
  83.  
  84.             fShadowTerm += fSample * xWeight * yWeight;
  85.         }
  86.     }
  87.  
  88.     fShadowTerm /= (fRadius*fRadius*4);
  89.  
  90.     return fShadowTerm;
  91. }
  92.  
  93. // The VertexShader.
  94. VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
  95. {
  96.     VertexShaderOutput output = (VertexShaderOutput)0;
  97.  
  98.     float4 worldPosition = mul(input.Position, World);
  99.     float4 viewPosition = mul(worldPosition, View);
  100.     output.Position = mul(viewPosition, Projection);
  101.    
  102.     output.Normal = (mul(normalize(input.Normal), World));
  103.  
  104.     output.LightDirection.xyz = -LightDirection;
  105.     output.LightDirection.w = 1;
  106.    
  107.     output.LightViewPosition = mul(input.Position, World);
  108.     output.LightViewPosition = mul(output.LightViewPosition, LightView);
  109.     output.LightViewPosition = mul(output.LightViewPosition, LightProjection);
  110.     return output;
  111. }
  112.  
  113. float CalcShadowTermPCF(float light_space_depth, float ndotl, float2 shadow_coord)
  114. {
  115.     float shadow_term = 0;
  116.  
  117.     //float2 v_lerps = frac(ShadowMapSize * shadow_coord);
  118.  
  119.     float variableBias = clamp(0.001 * tan(acos(ndotl)), 0, Bias);
  120.  
  121.     //safe to assume it's a square
  122.     float size = 1 / ShadowMapSize.x;
  123.        
  124.     float samples[4];
  125.     samples[0] = (light_space_depth - variableBias < ShadowMap.Sample(ShadowMapSampler, shadow_coord).r);
  126.     samples[1] = (light_space_depth - variableBias < ShadowMap.Sample(ShadowMapSampler, shadow_coord + float2(size, 0)).r);
  127.     samples[2] = (light_space_depth - variableBias < ShadowMap.Sample(ShadowMapSampler, shadow_coord + float2(0, size)).r);
  128.     samples[3] = (light_space_depth - variableBias < ShadowMap.Sample(ShadowMapSampler, shadow_coord + float2(size, size)).r);
  129.  
  130.     shadow_term = (samples[0] + samples[1] + samples[2] + samples[3]) / 4.0;
  131.     //shadow_term = lerp(lerp(samples[0],samples[1],v_lerps.x),lerp(samples[2],samples[3],v_lerps.x),v_lerps.y);
  132.  
  133.     return shadow_term;
  134. }
  135.  
  136.  
  137. // The Pixel Shader
  138. PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
  139. {
  140.     PixelShaderOutput Output = (PixelShaderOutput)0;
  141.     float3 reflectionVector = -reflect(input.LightDirection, input.Normal);
  142.     float specular = normalize(reflectionVector);
  143.     float4 diffuse = saturate(dot(input.LightDirection, input.Normal));
  144.     specular = pow(abs(specular), SpecularPower) * diffuse;
  145.  
  146.     float NdL = saturate(dot(input.Normal, input.LightDirection));
  147.     float ourDepth = (input.LightViewPosition.z / input.LightViewPosition.w);
  148.     float2 ShadowTexCoord = mad(0.5f , input.LightViewPosition.xy / input.LightViewPosition.w , float2(0.5f, 0.5f));
  149.     ShadowTexCoord.y = 1.0f - ShadowTexCoord.y;
  150.     float shadowContribution = 0;
  151.     shadowContribution = CalcShadowTermSoftPCF(ourDepth, NdL, ShadowTexCoord, 4);
  152.  
  153.     Output.Color = float4(0, 0.5, 0.8, 1) * (diffuse + specular * 1) * shadowContribution; ;
  154.     return Output;
  155. }
  156.  
  157. // Our Techinique
  158. technique Technique1
  159. {
  160.     pass Pass0
  161.     {
  162.         VertexShader = compile vs_5_0 VertexShaderFunction();
  163.         PixelShader = compile ps_5_0 PixelShaderFunction();
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement