home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 20 Shadow Mapping / Shadows / Shaders / Default.hlsl < prev    next >
Encoding:
Text File  |  2016-03-02  |  3.9 KB  |  127 lines

  1. //***************************************************************************************
  2. // Default.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. // Defaults for number of lights.
  6. #ifndef NUM_DIR_LIGHTS
  7.     #define NUM_DIR_LIGHTS 3
  8. #endif
  9.  
  10. #ifndef NUM_POINT_LIGHTS
  11.     #define NUM_POINT_LIGHTS 0
  12. #endif
  13.  
  14. #ifndef NUM_SPOT_LIGHTS
  15.     #define NUM_SPOT_LIGHTS 0
  16. #endif
  17.  
  18. // Include common HLSL code.
  19. #include "Common.hlsl"
  20.  
  21. struct VertexIn
  22. {
  23.     float3 PosL    : POSITION;
  24.     float3 NormalL : NORMAL;
  25.     float2 TexC    : TEXCOORD;
  26.     float3 TangentU : TANGENT;
  27. };
  28.  
  29. struct VertexOut
  30. {
  31.     float4 PosH    : SV_POSITION;
  32.     float4 ShadowPosH : POSITION0;
  33.     float3 PosW    : POSITION1;
  34.     float3 NormalW : NORMAL;
  35.     float3 TangentW : TANGENT;
  36.     float2 TexC    : TEXCOORD;
  37. };
  38.  
  39. VertexOut VS(VertexIn vin)
  40. {
  41.     VertexOut vout = (VertexOut)0.0f;
  42.  
  43.     // Fetch the material data.
  44.     MaterialData matData = gMaterialData[gMaterialIndex];
  45.     
  46.     // Transform to world space.
  47.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  48.     vout.PosW = posW.xyz;
  49.  
  50.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  51.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  52.     
  53.     vout.TangentW = mul(vin.TangentU, (float3x3)gWorld);
  54.  
  55.     // Transform to homogeneous clip space.
  56.     vout.PosH = mul(posW, gViewProj);
  57.     
  58.     // Output vertex attributes for interpolation across triangle.
  59.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), gTexTransform);
  60.     vout.TexC = mul(texC, matData.MatTransform).xy;
  61.  
  62.     // Generate projective tex-coords to project shadow map onto scene.
  63.     vout.ShadowPosH = mul(posW, gShadowTransform);
  64.     
  65.     return vout;
  66. }
  67.  
  68. float4 PS(VertexOut pin) : SV_Target
  69. {
  70.     // Fetch the material data.
  71.     MaterialData matData = gMaterialData[gMaterialIndex];
  72.     float4 diffuseAlbedo = matData.DiffuseAlbedo;
  73.     float3 fresnelR0 = matData.FresnelR0;
  74.     float  roughness = matData.Roughness;
  75.     uint diffuseMapIndex = matData.DiffuseMapIndex;
  76.     uint normalMapIndex = matData.NormalMapIndex;
  77.     
  78.     // Dynamically look up the texture in the array.
  79.     diffuseAlbedo *= gTextureMaps[diffuseMapIndex].Sample(gsamAnisotropicWrap, pin.TexC);
  80.  
  81. #ifdef ALPHA_TEST
  82.     // Discard pixel if texture alpha < 0.1.  We do this test as soon 
  83.     // as possible in the shader so that we can potentially exit the
  84.     // shader early, thereby skipping the rest of the shader code.
  85.     clip(diffuseAlbedo.a - 0.1f);
  86. #endif
  87.  
  88.     // Interpolating normal can unnormalize it, so renormalize it.
  89.     pin.NormalW = normalize(pin.NormalW);
  90.     
  91.     float4 normalMapSample = gTextureMaps[normalMapIndex].Sample(gsamAnisotropicWrap, pin.TexC);
  92.     float3 bumpedNormalW = NormalSampleToWorldSpace(normalMapSample.rgb, pin.NormalW, pin.TangentW);
  93.  
  94.     // Uncomment to turn off normal mapping.
  95.     //bumpedNormalW = pin.NormalW;
  96.  
  97.     // Vector from point being lit to eye. 
  98.     float3 toEyeW = normalize(gEyePosW - pin.PosW);
  99.  
  100.     // Light terms.
  101.     float4 ambient = gAmbientLight*diffuseAlbedo;
  102.  
  103.     // Only the first light casts a shadow.
  104.     float3 shadowFactor = float3(1.0f, 1.0f, 1.0f);
  105.     shadowFactor[0] = CalcShadowFactor(pin.ShadowPosH);
  106.  
  107.     const float shininess = (1.0f - roughness) * normalMapSample.a;
  108.     Material mat = { diffuseAlbedo, fresnelR0, shininess };
  109.     float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
  110.         bumpedNormalW, toEyeW, shadowFactor);
  111.  
  112.     float4 litColor = ambient + directLight;
  113.  
  114.     // Add in specular reflections.
  115.     float3 r = reflect(-toEyeW, bumpedNormalW);
  116.     float4 reflectionColor = gCubeMap.Sample(gsamLinearWrap, r);
  117.     float3 fresnelFactor = SchlickFresnel(fresnelR0, bumpedNormalW, r);
  118.     litColor.rgb += shininess * fresnelFactor * reflectionColor.rgb;
  119.     
  120.     // Common convention to take alpha from diffuse albedo.
  121.     litColor.a = diffuseAlbedo.a;
  122.  
  123.     return litColor;
  124. }
  125.  
  126.  
  127.