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 / Common.hlsl next >
Encoding:
Text File  |  2016-03-02  |  3.3 KB  |  109 lines

  1. //***************************************************************************************
  2. // Common.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 structures and functions for lighting.
  19. #include "LightingUtil.hlsl"
  20.  
  21. struct MaterialData
  22. {
  23.     float4   DiffuseAlbedo;
  24.     float3   FresnelR0;
  25.     float    Roughness;
  26.     float4x4 MatTransform;
  27.     uint     DiffuseMapIndex;
  28.     uint     NormalMapIndex;
  29.     uint     MatPad1;
  30.     uint     MatPad2;
  31. };
  32.  
  33. TextureCube gCubeMap : register(t0);
  34.  
  35. // An array of textures, which is only supported in shader model 5.1+.  Unlike Texture2DArray, the textures
  36. // in this array can be different sizes and formats, making it more flexible than texture arrays.
  37. Texture2D gTextureMaps[10] : register(t1);
  38.  
  39. // Put in space1, so the texture array does not overlap with these resources.  
  40. // The texture array will occupy registers t0, t1, ..., t3 in space0. 
  41. StructuredBuffer<MaterialData> gMaterialData : register(t0, space1);
  42.  
  43.  
  44. SamplerState gsamPointWrap        : register(s0);
  45. SamplerState gsamPointClamp       : register(s1);
  46. SamplerState gsamLinearWrap       : register(s2);
  47. SamplerState gsamLinearClamp      : register(s3);
  48. SamplerState gsamAnisotropicWrap  : register(s4);
  49. SamplerState gsamAnisotropicClamp : register(s5);
  50.  
  51. // Constant data that varies per frame.
  52. cbuffer cbPerObject : register(b0)
  53. {
  54.     float4x4 gWorld;
  55.     float4x4 gTexTransform;
  56.     uint gMaterialIndex;
  57.     uint gObjPad0;
  58.     uint gObjPad1;
  59.     uint gObjPad2;
  60. };
  61.  
  62. // Constant data that varies per material.
  63. cbuffer cbPass : register(b1)
  64. {
  65.     float4x4 gView;
  66.     float4x4 gInvView;
  67.     float4x4 gProj;
  68.     float4x4 gInvProj;
  69.     float4x4 gViewProj;
  70.     float4x4 gInvViewProj;
  71.     float3 gEyePosW;
  72.     float cbPerObjectPad1;
  73.     float2 gRenderTargetSize;
  74.     float2 gInvRenderTargetSize;
  75.     float gNearZ;
  76.     float gFarZ;
  77.     float gTotalTime;
  78.     float gDeltaTime;
  79.     float4 gAmbientLight;
  80.  
  81.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  82.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  83.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  84.     // are spot lights for a maximum of MaxLights per object.
  85.     Light gLights[MaxLights];
  86. };
  87.  
  88. //---------------------------------------------------------------------------------------
  89. // Transforms a normal map sample to world space.
  90. //---------------------------------------------------------------------------------------
  91. float3 NormalSampleToWorldSpace(float3 normalMapSample, float3 unitNormalW, float3 tangentW)
  92. {
  93.     // Uncompress each component from [0,1] to [-1,1].
  94.     float3 normalT = 2.0f*normalMapSample - 1.0f;
  95.  
  96.     // Build orthonormal basis.
  97.     float3 N = unitNormalW;
  98.     float3 T = normalize(tangentW - dot(tangentW, N)*N);
  99.     float3 B = cross(N, T);
  100.  
  101.     float3x3 TBN = float3x3(T, B, N);
  102.  
  103.     // Transform from tangent space to world space.
  104.     float3 bumpedNormalW = mul(normalT, TBN);
  105.  
  106.     return bumpedNormalW;
  107. }
  108.  
  109.