shader_include.hlsli 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //--------------------------------------------------------------------------------------
  2. // File: shader_include.hlsl
  3. //
  4. // Include file for common shader definitions and functions.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8. //--------------------------------------------------------------------------------------
  9. // Defines
  10. //--------------------------------------------------------------------------------------
  11. #define ADD_SPECULAR 0
  12. //--------------------------------------------------------------------------------------
  13. // Textures
  14. //--------------------------------------------------------------------------------------
  15. Texture2D g_baseTexture : register( t0 ); // Base color texture
  16. Texture2D g_nmhTexture : register( t1 ); // Normal map and height map texture pair
  17. //--------------------------------------------------------------------------------------
  18. // Samplers
  19. //--------------------------------------------------------------------------------------
  20. SamplerState g_samLinear : register( s0 );
  21. SamplerState g_samPoint : register( s1 );
  22. //--------------------------------------------------------------------------------------
  23. // Constant Buffers
  24. //--------------------------------------------------------------------------------------
  25. cbuffer cbMain : register( b0 )
  26. {
  27. matrix g_mWorld; // World matrix
  28. matrix g_mView; // View matrix
  29. matrix g_mProjection; // Projection matrix
  30. matrix g_mWorldViewProjection; // WVP matrix
  31. matrix g_mViewProjection; // VP matrix
  32. matrix g_mInvView; // Inverse of view matrix
  33. float4 g_vScreenResolution; // Screen resolution
  34. float4 g_vMeshColor; // Mesh color
  35. float4 g_vTessellationFactor; // Edge, inside, minimum tessellation factor and 1/desired triangle size
  36. float4 g_vDetailTessellationHeightScale; // Height scale for detail tessellation of grid surface
  37. float4 g_vGridSize; // Grid size
  38. float4 g_vDebugColorMultiply; // Debug colors
  39. float4 g_vDebugColorAdd; // Debug colors
  40. float4 g_vFrustumPlaneEquation[4]; // View frustum plane equations
  41. };
  42. cbuffer cbMaterial : register( b1 )
  43. {
  44. float4 g_materialAmbientColor; // Material's ambient color
  45. float4 g_materialDiffuseColor; // Material's diffuse color
  46. float4 g_materialSpecularColor; // Material's specular color
  47. float4 g_fSpecularExponent; // Material's specular exponent
  48. float4 g_LightPosition; // Light's position in world space
  49. float4 g_LightDiffuse; // Light's diffuse color
  50. float4 g_LightAmbient; // Light's ambient color
  51. float4 g_vEye; // Camera's location
  52. float4 g_fBaseTextureRepeat; // The tiling factor for base and normal map textures
  53. float4 g_fPOMHeightMapScale; // Describes the useful range of values for the height field
  54. float4 g_fShadowSoftening; // Blurring factor for the soft shadows computation
  55. int g_nMinSamples; // The minimum number of samples for sampling the height field profile
  56. int g_nMaxSamples; // The maximum number of samples for sampling the height field profile
  57. };
  58. //--------------------------------------------------------------------------------------
  59. // Function: ComputeIllumination
  60. //
  61. // Description: Computes phong illumination for the given pixel using its attribute
  62. // textures and a light vector.
  63. //--------------------------------------------------------------------------------------
  64. float4 ComputeIllumination( float2 texCoord, float3 vLightTS, float3 vViewTS )
  65. {
  66. // Sample the normal from the normal map for the given texture sample:
  67. float3 vNormalTS = normalize( g_nmhTexture.Sample(g_samLinear, texCoord) * 2.0 - 1.0 );
  68. // Sample base map
  69. float4 cBaseColor = g_baseTexture.Sample( g_samLinear, texCoord );
  70. // Compute diffuse color component:
  71. float4 cDiffuse = saturate( dot( vNormalTS, vLightTS ) ) * g_materialDiffuseColor;
  72. // Compute the specular component if desired:
  73. float4 cSpecular = 0;
  74. #if ADD_SPECULAR==1
  75. float3 vReflectionTS = normalize( 2 * dot( vViewTS, vNormalTS ) * vNormalTS - vViewTS );
  76. float fRdotL = saturate( dot( vReflectionTS, vLightTS ) );
  77. cSpecular = pow( fRdotL, g_fSpecularExponent.x ) * g_materialSpecularColor;
  78. #endif
  79. // Composite the final color:
  80. float4 cFinalColor = ( g_materialAmbientColor + cDiffuse ) * cBaseColor + cSpecular;
  81. return cFinalColor;
  82. }