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 15 First Person Camera and Dynamic Indexing / CameraAndDynamicIndexing / Shaders / Default.hlsl next >
Encoding:
Text File  |  2016-03-02  |  4.6 KB  |  162 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 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     MatPad0;
  29.     uint     MatPad1;
  30.     uint     MatPad2;
  31. };
  32.  
  33.  
  34. // An array of textures, which is only supported in shader model 5.1+.  Unlike Texture2DArray, the textures
  35. // in this array can be different sizes and formats, making it more flexible than texture arrays.
  36. Texture2D gDiffuseMap[4] : register(t0);
  37.  
  38. // Put in space1, so the texture array does not overlap with these resources.  
  39. // The texture array will occupy registers t0, t1, ..., t3 in space0. 
  40. StructuredBuffer<MaterialData> gMaterialData : register(t0, space1);
  41.  
  42.  
  43. SamplerState gsamPointWrap        : register(s0);
  44. SamplerState gsamPointClamp       : register(s1);
  45. SamplerState gsamLinearWrap       : register(s2);
  46. SamplerState gsamLinearClamp      : register(s3);
  47. SamplerState gsamAnisotropicWrap  : register(s4);
  48. SamplerState gsamAnisotropicClamp : register(s5);
  49.  
  50. // Constant data that varies per frame.
  51. cbuffer cbPerObject : register(b0)
  52. {
  53.     float4x4 gWorld;
  54.     float4x4 gTexTransform;
  55.     uint gMaterialIndex;
  56.     uint gObjPad0;
  57.     uint gObjPad1;
  58.     uint gObjPad2;
  59. };
  60.  
  61. // Constant data that varies per material.
  62. cbuffer cbPass : register(b1)
  63. {
  64.     float4x4 gView;
  65.     float4x4 gInvView;
  66.     float4x4 gProj;
  67.     float4x4 gInvProj;
  68.     float4x4 gViewProj;
  69.     float4x4 gInvViewProj;
  70.     float3 gEyePosW;
  71.     float cbPerObjectPad1;
  72.     float2 gRenderTargetSize;
  73.     float2 gInvRenderTargetSize;
  74.     float gNearZ;
  75.     float gFarZ;
  76.     float gTotalTime;
  77.     float gDeltaTime;
  78.     float4 gAmbientLight;
  79.  
  80.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  81.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  82.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  83.     // are spot lights for a maximum of MaxLights per object.
  84.     Light gLights[MaxLights];
  85. };
  86.  
  87. struct VertexIn
  88. {
  89.     float3 PosL    : POSITION;
  90.     float3 NormalL : NORMAL;
  91.     float2 TexC    : TEXCOORD;
  92. };
  93.  
  94. struct VertexOut
  95. {
  96.     float4 PosH    : SV_POSITION;
  97.     float3 PosW    : POSITION;
  98.     float3 NormalW : NORMAL;
  99.     float2 TexC    : TEXCOORD;
  100. };
  101.  
  102. VertexOut VS(VertexIn vin)
  103. {
  104.     VertexOut vout = (VertexOut)0.0f;
  105.  
  106.     // Fetch the material data.
  107.     MaterialData matData = gMaterialData[gMaterialIndex];
  108.     
  109.     // Transform to world space.
  110.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  111.     vout.PosW = posW.xyz;
  112.  
  113.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  114.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  115.  
  116.     // Transform to homogeneous clip space.
  117.     vout.PosH = mul(posW, gViewProj);
  118.     
  119.     // Output vertex attributes for interpolation across triangle.
  120.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), gTexTransform);
  121.     vout.TexC = mul(texC, matData.MatTransform).xy;
  122.     
  123.     return vout;
  124. }
  125.  
  126. float4 PS(VertexOut pin) : SV_Target
  127. {
  128.     // Fetch the material data.
  129.     MaterialData matData = gMaterialData[gMaterialIndex];
  130.     float4 diffuseAlbedo = matData.DiffuseAlbedo;
  131.     float3 fresnelR0 = matData.FresnelR0;
  132.     float  roughness = matData.Roughness;
  133.     uint diffuseTexIndex = matData.DiffuseMapIndex;
  134.  
  135.     // Dynamically look up the texture in the array.
  136.     diffuseAlbedo *= gDiffuseMap[diffuseTexIndex].Sample(gsamLinearWrap, pin.TexC);
  137.     
  138.     // Interpolating normal can unnormalize it, so renormalize it.
  139.     pin.NormalW = normalize(pin.NormalW);
  140.  
  141.     // Vector from point being lit to eye. 
  142.     float3 toEyeW = normalize(gEyePosW - pin.PosW);
  143.  
  144.     // Light terms.
  145.     float4 ambient = gAmbientLight*diffuseAlbedo;
  146.  
  147.     const float shininess = 1.0f - roughness;
  148.     Material mat = { diffuseAlbedo, fresnelR0, shininess };
  149.     float3 shadowFactor = 1.0f;
  150.     float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
  151.         pin.NormalW, toEyeW, shadowFactor);
  152.  
  153.     float4 litColor = ambient + directLight;
  154.  
  155.     // Common convention to take alpha from diffuse albedo.
  156.     litColor.a = diffuseAlbedo.a;
  157.  
  158.     return litColor;
  159. }
  160.  
  161.  
  162.