DX11CustomVertexShader.hlsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 light : TEXCOORD2;
  46. };
  47. ////////////////////////////////////////////////////////////////////////////////
  48. // Vertex Shader
  49. ////////////////////////////////////////////////////////////////////////////////
  50. PixelInputType TextureVertexShader(VertexInputType input)
  51. {
  52. // return input;
  53. PixelInputType output;
  54. output.normal
  55. = normalize(mul(input.normal, (float3x3)knochenMatrix[input.knochen]));
  56. // Change the position vector to be 4 units for proper matrix calculations.
  57. input.position.w = 1.0f;
  58. // Store the texture coordinates for the pixel shader.
  59. output.tex = input.tex;
  60. // Calculate the position of the vertex against the world, view, and
  61. // projection matrices.
  62. output.worldPos = mul(input.position, knochenMatrix[input.knochen]);
  63. output.position = mul(output.worldPos, view);
  64. output.position = mul(output.position, projection);
  65. output.light = float4(1.0f, 1.0f, 1.0f, 0.f);
  66. uint dayLight;
  67. uint dynamicLight;
  68. if (lightLength == 1)
  69. {
  70. dayLight = lightBuffer[0].dayLight1;
  71. dynamicLight = lightBuffer[0].dynamicLight1;
  72. }
  73. else if (input.id < lightLength)
  74. {
  75. if (input.id % 2 == 0)
  76. {
  77. dayLight = lightBuffer[input.id / 2].dayLight1;
  78. dynamicLight = lightBuffer[input.id / 2].dynamicLight1;
  79. }
  80. else
  81. {
  82. dayLight = lightBuffer[input.id / 2].dayLight2;
  83. dynamicLight = lightBuffer[input.id / 2].dynamicLight2;
  84. }
  85. }
  86. if (lightLength > 0)
  87. {
  88. output.light.x = (((dayLight >> 24) & 0xFF) / 255.f) * dayLightFactor;
  89. output.light.y = (((dayLight >> 16) & 0xFF) / 255.f) * dayLightFactor;
  90. output.light.z = (((dayLight >> 8) & 0xFF) / 255.f) * dayLightFactor;
  91. float3 dynamicLightResult;
  92. dynamicLightResult.x = ((dynamicLight >> 24) & 0xFF) / 255.f;
  93. dynamicLightResult.y = ((dynamicLight >> 16) & 0xFF) / 255.f;
  94. dynamicLightResult.z = ((dynamicLight >> 8) & 0xFF) / 255.f;
  95. if (dynamicLightResult.x > output.light.x)
  96. {
  97. output.light.x = dynamicLightResult.x;
  98. }
  99. if (dynamicLightResult.y > output.light.y)
  100. {
  101. output.light.y = dynamicLightResult.y;
  102. }
  103. if (dynamicLightResult.z > output.light.z)
  104. {
  105. output.light.z = dynamicLightResult.z;
  106. }
  107. output.light.w = 1.f;
  108. }
  109. return output;
  110. }