DX11CustomVertexShader.hlsl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /////////////
  2. // GLOBALS //
  3. /////////////
  4. cbuffer MatrixBuffer : register(b0)
  5. {
  6. matrix knochenMatrix[128];
  7. };
  8. // The projection and view matrix
  9. cbuffer Kamera : register(b1)
  10. {
  11. matrix view;
  12. matrix projection;
  13. }
  14. cbuffer LightController : register(b2)
  15. {
  16. uint lightLength;
  17. float dayLightFactor;
  18. }
  19. // stores the light for two verticies
  20. struct VertexLight
  21. {
  22. uint dayLight1;
  23. uint dynamicLight1;
  24. uint dayLight2;
  25. uint dynamicLight2;
  26. };
  27. StructuredBuffer<VertexLight> lightBuffer : register(t0);
  28. //////////////
  29. // TYPEDEFS //
  30. //////////////
  31. struct VertexInputType
  32. {
  33. float4 position : POSITION;
  34. float2 tex : TEXCOORD0;
  35. float3 normal : NORMAL;
  36. uint knochen : KNOCHEN_ID0;
  37. uint id : VERTEX_ID0;
  38. };
  39. struct PixelInputType
  40. {
  41. float4 worldPos : POSITION;
  42. float4 position : SV_POSITION;
  43. float2 tex : TEXCOORD0;
  44. float3 normal : TEXCOORD1;
  45. float4 light1 : TEXCOORD2;
  46. float4 light2 : TEXCOORD3;
  47. };
  48. ////////////////////////////////////////////////////////////////////////////////
  49. // Vertex Shader
  50. ////////////////////////////////////////////////////////////////////////////////
  51. PixelInputType TextureVertexShader(VertexInputType input)
  52. {
  53. // return input;
  54. PixelInputType output;
  55. output.normal
  56. = normalize(mul(input.normal, (float3x3)knochenMatrix[input.knochen]));
  57. // Change the position vector to be 4 units for proper matrix calculations.
  58. input.position.w = 1.0f;
  59. // Store the texture coordinates for the pixel shader.
  60. output.tex = input.tex;
  61. // Calculate the position of the vertex against the world, view, and
  62. // projection matrices.
  63. output.worldPos = mul(input.position, knochenMatrix[input.knochen]);
  64. output.position = mul(output.worldPos, view);
  65. output.position = mul(output.position, projection);
  66. output.light1 = float4(1.0f, 1.0f, 1.0f, 0.f);
  67. output.light2 = float4(0.0f, 0.0f, 0.0f, 0.f);
  68. uint dayLight = 0;
  69. uint dynamicLight = 0;
  70. if (lightLength == 1)
  71. {
  72. dayLight = lightBuffer[0].dayLight1;
  73. dynamicLight = lightBuffer[0].dynamicLight1;
  74. }
  75. else if (input.id < lightLength)
  76. {
  77. if (input.id % 2 == 0)
  78. {
  79. dayLight = lightBuffer[input.id / 2].dayLight1;
  80. dynamicLight = lightBuffer[input.id / 2].dynamicLight1;
  81. }
  82. else
  83. {
  84. dayLight = lightBuffer[input.id / 2].dayLight2;
  85. dynamicLight = lightBuffer[input.id / 2].dynamicLight2;
  86. }
  87. }
  88. if (lightLength > 0 && input.id < lightLength)
  89. {
  90. output.light1.x = (((dayLight >> 24) & 0xFF) / 255.f) * dayLightFactor;
  91. output.light1.y = (((dayLight >> 16) & 0xFF) / 255.f) * dayLightFactor;
  92. output.light1.z = (((dayLight >> 8) & 0xFF) / 255.f) * dayLightFactor;
  93. output.light2.x = ((dynamicLight >> 24) & 0xFF) / 255.f;
  94. output.light2.y = ((dynamicLight >> 16) & 0xFF) / 255.f;
  95. output.light2.z = ((dynamicLight >> 8) & 0xFF) / 255.f;
  96. output.light1.w = 1.f;
  97. }
  98. return output;
  99. }