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 23 Character Animation / SkinnedMesh / Shaders / DrawNormals.hlsl < prev    next >
Encoding:
Text File  |  2016-03-02  |  3.5 KB  |  117 lines

  1. //***************************************************************************************
  2. // DrawNormals.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 0
  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 TangentL : TANGENT;
  27. #ifdef SKINNED
  28.     float3 BoneWeights : WEIGHTS;
  29.     uint4 BoneIndices  : BONEINDICES;
  30. #endif
  31. };
  32.  
  33. struct VertexOut
  34. {
  35.     float4 PosH     : SV_POSITION;
  36.     float3 NormalW  : NORMAL;
  37.     float3 TangentW : TANGENT;
  38.     float2 TexC     : TEXCOORD;
  39. };
  40.  
  41. VertexOut VS(VertexIn vin)
  42. {
  43.     VertexOut vout = (VertexOut)0.0f;
  44.  
  45.     // Fetch the material data.
  46.     MaterialData matData = gMaterialData[gMaterialIndex];
  47.     
  48. #ifdef SKINNED
  49.     float weights[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  50.     weights[0] = vin.BoneWeights.x;
  51.     weights[1] = vin.BoneWeights.y;
  52.     weights[2] = vin.BoneWeights.z;
  53.     weights[3] = 1.0f - weights[0] - weights[1] - weights[2];
  54.  
  55.     float3 posL = float3(0.0f, 0.0f, 0.0f);
  56.     float3 normalL = float3(0.0f, 0.0f, 0.0f);
  57.     float3 tangentL = float3(0.0f, 0.0f, 0.0f);
  58.     for(int i = 0; i < 4; ++i)
  59.     {
  60.         // Assume no nonuniform scaling when transforming normals, so 
  61.         // that we do not have to use the inverse-transpose.
  62.  
  63.         posL += weights[i] * mul(float4(vin.PosL, 1.0f), gBoneTransforms[vin.BoneIndices[i]]).xyz;
  64.         normalL += weights[i] * mul(vin.NormalL, (float3x3)gBoneTransforms[vin.BoneIndices[i]]);
  65.         tangentL += weights[i] * mul(vin.TangentL.xyz, (float3x3)gBoneTransforms[vin.BoneIndices[i]]);
  66.     }
  67.  
  68.     vin.PosL = posL;
  69.     vin.NormalL = normalL;
  70.     vin.TangentL.xyz = tangentL;
  71. #endif
  72.  
  73.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  74.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  75.     vout.TangentW = mul(vin.TangentL, (float3x3)gWorld);
  76.  
  77.     // Transform to homogeneous clip space.
  78.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  79.     vout.PosH = mul(posW, gViewProj);
  80.     
  81.     // Output vertex attributes for interpolation across triangle.
  82.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), gTexTransform);
  83.     vout.TexC = mul(texC, matData.MatTransform).xy;
  84.     
  85.     return vout;
  86. }
  87.  
  88. float4 PS(VertexOut pin) : SV_Target
  89. {
  90.     // Fetch the material data.
  91.     MaterialData matData = gMaterialData[gMaterialIndex];
  92.     float4 diffuseAlbedo = matData.DiffuseAlbedo;
  93.     uint diffuseMapIndex = matData.DiffuseMapIndex;
  94.     uint normalMapIndex = matData.NormalMapIndex;
  95.     
  96.     // Dynamically look up the texture in the array.
  97.     diffuseAlbedo *= gTextureMaps[diffuseMapIndex].Sample(gsamAnisotropicWrap, pin.TexC);
  98.  
  99. #ifdef ALPHA_TEST
  100.     // Discard pixel if texture alpha < 0.1.  We do this test as soon 
  101.     // as possible in the shader so that we can potentially exit the
  102.     // shader early, thereby skipping the rest of the shader code.
  103.     clip(diffuseAlbedo.a - 0.1f);
  104. #endif
  105.  
  106.     // Interpolating normal can unnormalize it, so renormalize it.
  107.     pin.NormalW = normalize(pin.NormalW);
  108.     
  109.     // NOTE: We use interpolated vertex normal for SSAO.
  110.  
  111.     // Write normal in view space coordinates
  112.     float3 normalV = mul(pin.NormalW, (float3x3)gView);
  113.     return float4(normalV, 0.0f);
  114. }
  115.  
  116.  
  117.