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 10 Blending / BlendDemo / Shaders / Default.hlsl next >
Encoding:
Text File  |  2016-03-02  |  4.4 KB  |  159 lines

  1. //***************************************************************************************
  2. // Default.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //
  4. // Default shader, currently supports lighting.
  5. //***************************************************************************************
  6.  
  7. // Defaults for number of lights.
  8. #ifndef NUM_DIR_LIGHTS
  9.     #define NUM_DIR_LIGHTS 3
  10. #endif
  11.  
  12. #ifndef NUM_POINT_LIGHTS
  13.     #define NUM_POINT_LIGHTS 0
  14. #endif
  15.  
  16. #ifndef NUM_SPOT_LIGHTS
  17.     #define NUM_SPOT_LIGHTS 0
  18. #endif
  19.  
  20. // Include structures and functions for lighting.
  21. #include "LightingUtil.hlsl"
  22.  
  23. Texture2D    gDiffuseMap : register(t0);
  24.  
  25.  
  26. SamplerState gsamPointWrap        : register(s0);
  27. SamplerState gsamPointClamp       : register(s1);
  28. SamplerState gsamLinearWrap       : register(s2);
  29. SamplerState gsamLinearClamp      : register(s3);
  30. SamplerState gsamAnisotropicWrap  : register(s4);
  31. SamplerState gsamAnisotropicClamp : register(s5);
  32.  
  33. // Constant data that varies per frame.
  34. cbuffer cbPerObject : register(b0)
  35. {
  36.     float4x4 gWorld;
  37.     float4x4 gTexTransform;
  38. };
  39.  
  40. // Constant data that varies per pass.
  41. cbuffer cbPass : register(b1)
  42. {
  43.     float4x4 gView;
  44.     float4x4 gInvView;
  45.     float4x4 gProj;
  46.     float4x4 gInvProj;
  47.     float4x4 gViewProj;
  48.     float4x4 gInvViewProj;
  49.     float3 gEyePosW;
  50.     float cbPerObjectPad1;
  51.     float2 gRenderTargetSize;
  52.     float2 gInvRenderTargetSize;
  53.     float gNearZ;
  54.     float gFarZ;
  55.     float gTotalTime;
  56.     float gDeltaTime;
  57.     float4 gAmbientLight;
  58.  
  59.     // Allow application to change fog parameters once per frame.
  60.     // For example, we may only use fog for certain times of day.
  61.     float4 gFogColor;
  62.     float gFogStart;
  63.     float gFogRange;
  64.     float2 cbPerObjectPad2;
  65.  
  66.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  67.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  68.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  69.     // are spot lights for a maximum of MaxLights per object.
  70.     Light gLights[MaxLights];
  71. };
  72.  
  73. cbuffer cbMaterial : register(b2)
  74. {
  75.     float4   gDiffuseAlbedo;
  76.     float3   gFresnelR0;
  77.     float    gRoughness;
  78.     float4x4 gMatTransform;
  79. };
  80.  
  81. struct VertexIn
  82. {
  83.     float3 PosL    : POSITION;
  84.     float3 NormalL : NORMAL;
  85.     float2 TexC    : TEXCOORD;
  86. };
  87.  
  88. struct VertexOut
  89. {
  90.     float4 PosH    : SV_POSITION;
  91.     float3 PosW    : POSITION;
  92.     float3 NormalW : NORMAL;
  93.     float2 TexC    : TEXCOORD;
  94. };
  95.  
  96. VertexOut VS(VertexIn vin)
  97. {
  98.     VertexOut vout = (VertexOut)0.0f;
  99.     
  100.     // Transform to world space.
  101.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  102.     vout.PosW = posW.xyz;
  103.  
  104.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  105.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  106.  
  107.     // Transform to homogeneous clip space.
  108.     vout.PosH = mul(posW, gViewProj);
  109.     
  110.     // Output vertex attributes for interpolation across triangle.
  111.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), gTexTransform);
  112.     vout.TexC = mul(texC, gMatTransform).xy;
  113.  
  114.     return vout;
  115. }
  116.  
  117. float4 PS(VertexOut pin) : SV_Target
  118. {
  119.     float4 diffuseAlbedo = gDiffuseMap.Sample(gsamAnisotropicWrap, pin.TexC) * gDiffuseAlbedo;
  120.     
  121. #ifdef ALPHA_TEST
  122.     // Discard pixel if texture alpha < 0.1.  We do this test as soon 
  123.     // as possible in the shader so that we can potentially exit the
  124.     // shader early, thereby skipping the rest of the shader code.
  125.     clip(diffuseAlbedo.a - 0.1f);
  126. #endif
  127.  
  128.     // Interpolating normal can unnormalize it, so renormalize it.
  129.     pin.NormalW = normalize(pin.NormalW);
  130.  
  131.     // Vector from point being lit to eye. 
  132.     float3 toEyeW = gEyePosW - pin.PosW;
  133.     float distToEye = length(toEyeW);
  134.     toEyeW /= distToEye; // normalize
  135.  
  136.     // Light terms.
  137.     float4 ambient = gAmbientLight*diffuseAlbedo;
  138.  
  139.     const float shininess = 1.0f - gRoughness;
  140.     Material mat = { diffuseAlbedo, gFresnelR0, shininess };
  141.     float3 shadowFactor = 1.0f;
  142.     float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
  143.         pin.NormalW, toEyeW, shadowFactor);
  144.  
  145.     float4 litColor = ambient + directLight;
  146.  
  147. #ifdef FOG
  148.     float fogAmount = saturate((distToEye - gFogStart) / gFogRange);
  149.     litColor = lerp(litColor, gFogColor, fogAmount);
  150. #endif
  151.  
  152.     // Common convention to take alpha from diffuse albedo.
  153.     litColor.a = diffuseAlbedo.a;
  154.  
  155.     return litColor;
  156. }
  157.  
  158.  
  159.