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 8 Lighting / LitColumns / Shaders / Default.hlsl next >
Encoding:
Text File  |  2016-03-02  |  3.0 KB  |  120 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. // Constant data that varies per frame.
  24.  
  25. cbuffer cbPerObject : register(b0)
  26. {
  27.     float4x4 gWorld;
  28. };
  29.  
  30. cbuffer cbMaterial : register(b1)
  31. {
  32.     float4 gDiffuseAlbedo;
  33.     float3 gFresnelR0;
  34.     float  gRoughness;
  35.     float4x4 gMatTransform;
  36. };
  37.  
  38. // Constant data that varies per material.
  39. cbuffer cbPass : register(b2)
  40. {
  41.     float4x4 gView;
  42.     float4x4 gInvView;
  43.     float4x4 gProj;
  44.     float4x4 gInvProj;
  45.     float4x4 gViewProj;
  46.     float4x4 gInvViewProj;
  47.     float3 gEyePosW;
  48.     float cbPerObjectPad1;
  49.     float2 gRenderTargetSize;
  50.     float2 gInvRenderTargetSize;
  51.     float gNearZ;
  52.     float gFarZ;
  53.     float gTotalTime;
  54.     float gDeltaTime;
  55.     float4 gAmbientLight;
  56.  
  57.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  58.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  59.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  60.     // are spot lights for a maximum of MaxLights per object.
  61.     Light gLights[MaxLights];
  62. };
  63.  
  64. struct VertexIn
  65. {
  66.     float3 PosL    : POSITION;
  67.     float3 NormalL : NORMAL;
  68. };
  69.  
  70. struct VertexOut
  71. {
  72.     float4 PosH    : SV_POSITION;
  73.     float3 PosW    : POSITION;
  74.     float3 NormalW : NORMAL;
  75. };
  76.  
  77. VertexOut VS(VertexIn vin)
  78. {
  79.     VertexOut vout = (VertexOut)0.0f;
  80.     
  81.     // Transform to world space.
  82.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  83.     vout.PosW = posW.xyz;
  84.  
  85.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  86.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  87.  
  88.     // Transform to homogeneous clip space.
  89.     vout.PosH = mul(posW, gViewProj);
  90.  
  91.     return vout;
  92. }
  93.  
  94. float4 PS(VertexOut pin) : SV_Target
  95. {
  96.     // Interpolating normal can unnormalize it, so renormalize it.
  97.     pin.NormalW = normalize(pin.NormalW);
  98.  
  99.     // Vector from point being lit to eye. 
  100.     float3 toEyeW = normalize(gEyePosW - pin.PosW);
  101.  
  102.     // Indirect lighting.
  103.     float4 ambient = gAmbientLight*gDiffuseAlbedo;
  104.  
  105.     const float shininess = 1.0f - gRoughness;
  106.     Material mat = { gDiffuseAlbedo, gFresnelR0, shininess };
  107.     float3 shadowFactor = 1.0f;
  108.     float4 directLight = ComputeLighting(gLights, mat, pin.PosW, 
  109.         pin.NormalW, toEyeW, shadowFactor);
  110.  
  111.     float4 litColor = ambient + directLight;
  112.  
  113.     // Common convention to take alpha from diffuse material.
  114.     litColor.a = gDiffuseAlbedo.a;
  115.  
  116.     return litColor;
  117. }
  118.  
  119.  
  120.