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 19 Normal Mapping / NormalMap / Shaders / Default.hlsl < prev    next >
Encoding:
Text File  |  2016-03-02  |  3.4 KB  |  113 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.     float3 PosW    : POSITION;
  33.     float3 NormalW : NORMAL;
  34.     float3 TangentW : TANGENT;
  35.     float2 TexC    : TEXCOORD;
  36. };
  37.  
  38. VertexOut VS(VertexIn vin)
  39. {
  40.     VertexOut vout = (VertexOut)0.0f;
  41.  
  42.     // Fetch the material data.
  43.     MaterialData matData = gMaterialData[gMaterialIndex];
  44.     
  45.     // Transform to world space.
  46.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  47.     vout.PosW = posW.xyz;
  48.  
  49.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  50.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  51.     
  52.     vout.TangentW = mul(vin.TangentU, (float3x3)gWorld);
  53.  
  54.     // Transform to homogeneous clip space.
  55.     vout.PosH = mul(posW, gViewProj);
  56.     
  57.     // Output vertex attributes for interpolation across triangle.
  58.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), gTexTransform);
  59.     vout.TexC = mul(texC, matData.MatTransform).xy;
  60.     
  61.     return vout;
  62. }
  63.  
  64. float4 PS(VertexOut pin) : SV_Target
  65. {
  66.     // Fetch the material data.
  67.     MaterialData matData = gMaterialData[gMaterialIndex];
  68.     float4 diffuseAlbedo = matData.DiffuseAlbedo;
  69.     float3 fresnelR0 = matData.FresnelR0;
  70.     float  roughness = matData.Roughness;
  71.     uint diffuseMapIndex = matData.DiffuseMapIndex;
  72.     uint normalMapIndex = matData.NormalMapIndex;
  73.     
  74.     // Interpolating normal can unnormalize it, so renormalize it.
  75.     pin.NormalW = normalize(pin.NormalW);
  76.     
  77.     float4 normalMapSample = gTextureMaps[normalMapIndex].Sample(gsamAnisotropicWrap, pin.TexC);
  78.     float3 bumpedNormalW = NormalSampleToWorldSpace(normalMapSample.rgb, pin.NormalW, pin.TangentW);
  79.  
  80.     // Uncomment to turn off normal mapping.
  81.     //bumpedNormalW = pin.NormalW;
  82.  
  83.     // Dynamically look up the texture in the array.
  84.     diffuseAlbedo *= gTextureMaps[diffuseMapIndex].Sample(gsamAnisotropicWrap, pin.TexC);
  85.  
  86.     // Vector from point being lit to eye. 
  87.     float3 toEyeW = normalize(gEyePosW - pin.PosW);
  88.  
  89.     // Light terms.
  90.     float4 ambient = gAmbientLight*diffuseAlbedo;
  91.  
  92.     const float shininess = (1.0f - roughness) * normalMapSample.a;
  93.     Material mat = { diffuseAlbedo, fresnelR0, shininess };
  94.     float3 shadowFactor = 1.0f;
  95.     float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
  96.         bumpedNormalW, toEyeW, shadowFactor);
  97.  
  98.     float4 litColor = ambient + directLight;
  99.  
  100.     // Add in specular reflections.
  101.     float3 r = reflect(-toEyeW, bumpedNormalW);
  102.     float4 reflectionColor = gCubeMap.Sample(gsamLinearWrap, r);
  103.     float3 fresnelFactor = SchlickFresnel(fresnelR0, bumpedNormalW, r);
  104.     litColor.rgb += shininess * fresnelFactor * reflectionColor.rgb;
  105.     
  106.     // Common convention to take alpha from diffuse albedo.
  107.     litColor.a = diffuseAlbedo.a;
  108.  
  109.     return litColor;
  110. }
  111.  
  112.  
  113.