VertexLighting.fx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. float4x4 world;
  2. float4x4 view;
  3. float4x4 projection;
  4. //////////////////////////////////////////////////////////////
  5. // Example 3.1 //
  6. // //
  7. // These variables are necessary to define a single //
  8. // directional light. The light has only a direction and a //
  9. // color. //
  10. //////////////////////////////////////////////////////////////
  11. float4 lightColor;
  12. float3 lightDirection;
  13. //////////////////////////////////////////////////////////////
  14. // Example 3.2 //
  15. // //
  16. // We are also including an "ambient" lighting color. This //
  17. // represents a color / intensity of scattered light in a //
  18. // scene. It is not directional, so we only need one //
  19. // variable to represent it. //
  20. //////////////////////////////////////////////////////////////
  21. float4 ambientColor;
  22. //////////////////////////////////////////////////////////////
  23. // Example 3.3 //
  24. // //
  25. // For this shader, we need to return multiple fields from //
  26. // the vertex shader. To facilitate this, we define a //
  27. // struct to use as the return type. Notice the semantics //
  28. // are "baked in" to the structure fields. This helps //
  29. // clean up the function defintions. //
  30. //////////////////////////////////////////////////////////////
  31. struct VertexShaderOutput
  32. {
  33. float4 Position : POSITION;
  34. float4 Color : COLOR0;
  35. };
  36. //////////////////////////////////////////////////////////////
  37. // Example 3.4 //
  38. // //
  39. // You can also use stucts for inputs. This structure //
  40. // represents the input to the pixel shader. //
  41. //////////////////////////////////////////////////////////////
  42. struct PixelShaderInput
  43. {
  44. float4 Color: COLOR0;
  45. };
  46. //////////////////////////////////////////////////////////////
  47. // Example 3.5 //
  48. // //
  49. // This simple vertex shader sets a vertex color by //
  50. // calculating the sum of the directional and ambient light //
  51. // at each vertex. //
  52. //////////////////////////////////////////////////////////////
  53. VertexShaderOutput DiffuseLighting(
  54. float3 position : POSITION,
  55. float3 normal : NORMAL )
  56. {
  57. VertexShaderOutput output;
  58. //generate the world-view-proj matrix
  59. float4x4 wvp = mul(mul(world, view), projection);
  60. //transform the input position to the output
  61. output.Position = mul(float4(position, 1.0), wvp);
  62. //////////////////////////////////////////////////////////////
  63. // Example 3.6 //
  64. // //
  65. // In this shader, we expect the light to be defined in //
  66. // terms of World Space. We need to transform the model //
  67. // normals to World Space as well to make them useful in //
  68. // the lighting caculation. //
  69. //////////////////////////////////////////////////////////////
  70. float3 worldNormal = mul(normal, world);
  71. //////////////////////////////////////////////////////////////
  72. // Example 3.7 //
  73. // //
  74. // The actual lighting calculation for diffuse light is //
  75. // normal-dot-lightDirection. The intrinsic function //
  76. // used is called "dot" and takes two vectors as parameters //
  77. // and returns a scalar value. //
  78. // //
  79. // The lambertian lighting calculation uses the direction //
  80. // from the vertex to the light. Therefore the light //
  81. // direction vector is reversed (negated) as lightDirection //
  82. // indicates the direction of the light is pointing. //
  83. //////////////////////////////////////////////////////////////
  84. float diffuseIntensity = saturate( dot(-lightDirection, worldNormal));
  85. //////////////////////////////////////////////////////////////
  86. // Example 3.8 //
  87. // //
  88. // The diffuse intensity is then mutiplied by the light //
  89. // color to get the diffuse color at the vertex. //
  90. //////////////////////////////////////////////////////////////
  91. float4 diffuseColor = lightColor * diffuseIntensity;
  92. //////////////////////////////////////////////////////////////
  93. // Example 3.9 //
  94. // //
  95. // The final vertex color is the sum of the diffuse //
  96. // lighting at the vertex and the global ambient light. //
  97. // //
  98. // This simplistic shader will unintentionally modify the //
  99. // alpha (opacity) of the vertex color, so the alpha //
  100. // component of the color is set fixed at 1.0 (totally //
  101. // opaque) for the purposes of this sample. //
  102. //////////////////////////////////////////////////////////////
  103. output.Color = diffuseColor + ambientColor;
  104. diffuseColor.a = 1.0;
  105. //return the output structure
  106. return output;
  107. }
  108. //////////////////////////////////////////////////////////////
  109. // Example 3.10 //
  110. // //
  111. // This pixel shader returns the color provided to it from //
  112. // the interpolator. This means that the color input is //
  113. // interpolated from the vertex colors output. This //
  114. // causes the smooth shaded effect known as Gouraud Shading //
  115. //////////////////////////////////////////////////////////////
  116. float4 SimplePixelShader(PixelShaderInput input) : COLOR
  117. {
  118. return input.Color;
  119. }
  120. //////////////////////////////////////////////////////////////
  121. // Example 3.11 //
  122. // //
  123. // This simple technique is called "VertexLighting" and //
  124. // draws polygons lit by a single directional light and //
  125. // ambient light //
  126. //////////////////////////////////////////////////////////////
  127. technique VertexLighting
  128. {
  129. pass P0
  130. {
  131. //set the VertexShader state to the vertex shader function
  132. VertexShader = compile vs_2_0 DiffuseLighting();
  133. //set the PixelShader state to the pixel shader function
  134. PixelShader = compile ps_2_0 SimplePixelShader();
  135. }
  136. }