Light.vs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. cbuffer MatrixBuffer
  2. {
  3. matrix worldMatrix;
  4. matrix viewMatrix;
  5. matrix projectionMatrix;
  6. };
  7. cbuffer CameraBuffer
  8. {
  9. float3 cameraPosition;
  10. float padding;
  11. };
  12. struct VertexInputType
  13. {
  14. float4 position : POSITION;
  15. float2 tex : TEXCOORD0;
  16. float3 normal : NORMAL;
  17. };
  18. struct PixelInputType
  19. {
  20. float4 position : SV_POSITION;
  21. float2 tex : TEXCOORD0;
  22. float3 normal : NORMAL;
  23. float3 viewDirection : TEXCOORD1;
  24. };
  25. PixelInputType LightVertexShader(VertexInputType input)
  26. {
  27. PixelInputType output;
  28. float4 worldPosition;
  29. // Change the position vector to be 4 units for proper matrix calculations
  30. input.position.w = 1.0f;
  31. // Calculate the position of the vertex against the world, view, and projection matracies
  32. output.position = mul(input.position, worldMatrix);
  33. output.position = mul(output.position, viewMatrix);
  34. output.position = mul(output.position, projectionMatrix);
  35. // Store the input color for the pixel shader to use.
  36. output.tex = input.tex;
  37. // Calculate the normal vector against the world matrix only
  38. output.normal = mul(input.normal, (float3x3)worldMatrix);
  39. // Normalize the normal vector
  40. output.normal = normalize(output.normal);
  41. // Calculate the position of the vertex in the world
  42. worldPosition = mul(input.position, worldMatrix);
  43. // Determine the viewing direction
  44. output.viewDirection = cameraPosition.xyz - worldPosition.xyz;
  45. // Normalize the viewing direction vector
  46. output.viewDirection = normalize(output.viewDirection);
  47. return output;
  48. }