Advertisement
TheRarebit

Boned Mesh Shader hlsl

May 16th, 2017
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. //------------------------------------------------------------------------
  2. // Light settings
  3. //------------------------------------------------------------------------
  4. #define MAX_LIGHTS 8
  5.  
  6. #define LIGHT_DIR 0
  7. #define LIGHT_PNT 1
  8. #define LIGHT_SPT 2
  9.  
  10. struct Light
  11. {
  12. int m_type;
  13. float3 m_pos;
  14. float4 m_ambient;
  15. float4 m_diffuse;
  16. float4 m_specular;
  17. float m_radius;
  18. };
  19.  
  20. struct Material
  21. {
  22. float4 m_ambient;
  23. float4 m_diffuse;
  24. float4 m_emissive;
  25. float4 m_specular;
  26. float m_specularPower;
  27. };
  28.  
  29. //------------------------------------------------------------------------
  30. // Globals
  31. //------------------------------------------------------------------------
  32. uniform extern matrix g_world;
  33. uniform extern matrix g_worldInvTrans;
  34. uniform extern matrix g_view;
  35. uniform extern matrix g_proj;
  36.  
  37. uniform extern float3 g_cameraPos;
  38. uniform extern float4 g_globalAmbient;
  39. uniform extern int g_numLights;
  40.  
  41. uniform extern matrix g_boneMatrices[40];
  42.  
  43. uniform extern Light g_lights[MAX_LIGHTS];
  44. uniform extern Material g_material;
  45.  
  46. //------------------------------------------------------------------------
  47. // Textures
  48. //------------------------------------------------------------------------
  49. uniform extern texture g_tex;
  50. sampler g_texSampler = sampler_state
  51. {
  52. Texture = <g_tex>;
  53. MinFilter = Anisotropic;
  54. MagFilter = Linear;
  55. MipFilter = Linear;
  56. MaxAnisotropy = 8;
  57. };
  58.  
  59. //------------------------------------------------------------------------
  60. // Vertex shader
  61. //------------------------------------------------------------------------
  62. struct InputVS
  63. {
  64. float3 m_pos : POSITION0;
  65. int4 m_boneIndices : BLENDINDICES0;
  66. float4 m_weights : BLENDWEIGHT0;
  67. float3 m_normal : NORMAL;
  68. float2 m_tex0 : TEXCOORD0;
  69. };
  70.  
  71. struct OutputVS
  72. {
  73. float4 m_posH : POSITION0;
  74. float3 m_posW : POSITION1;
  75. float3 m_normalW : NORMAL;
  76. float2 m_tex0 : TEXCOORD0;
  77. };
  78.  
  79. float4 blendWeights(float4 t_pos, float4 t_weights, int4 t_bones)
  80. {
  81. float4 l_result = float4(0.0f, 0.0f, 0.0f, 0.0f);
  82.  
  83. [unroll]
  84. for (int i = 0; i < 4; ++i) {
  85. if (t_weights[i] != 0.0f) {
  86. l_result += t_weights[i] * mul(t_pos, g_boneMatrices[t_bones[i]]);
  87. }
  88. }
  89.  
  90. return l_result;
  91. }
  92.  
  93. //------------------------------------------------------------------------
  94. // Vertex Shader
  95. //------------------------------------------------------------------------
  96. OutputVS VS(InputVS t_input) {
  97. OutputVS l_result;
  98.  
  99. float4 l_pos = blendWeights(float4(t_input.m_pos, 1.0f), t_input.m_weights, t_input.m_boneIndices);
  100. l_pos.w = 1.0f;
  101.  
  102. float4x4 l_wvp = mul(g_world, mul(g_view, g_proj));
  103. l_result.m_posH = mul(l_pos, l_wvp);
  104. l_result.m_posW = mul(l_pos, g_world).xyz;
  105. l_result.m_tex0 = t_input.m_tex0;
  106.  
  107. float4 l_normal = blendWeights(float4(t_input.m_normal, 1.0f), t_input.m_weights, t_input.m_boneIndices);
  108. l_normal.w = 0.0f;
  109.  
  110. l_result.m_normalW = mul(l_normal, (float3x3)g_worldInvTrans).xyz;
  111.  
  112. return l_result;
  113. }
  114.  
  115. //------------------------------------------------------------------------
  116. // Pixel Shader
  117. //------------------------------------------------------------------------
  118. float4 PS(OutputVS l_input) : COLOR
  119. {
  120. float3 l_n = normalize(l_input.m_normalW);
  121.  
  122. float3 l_v = normalize(g_cameraPos - l_input.m_posW);
  123. float3 l_lightDir = float3(0.0f, 0.0f, 0.0f);
  124. float3 l_h = float3(0.0f, 0.0f, 0.0f);
  125.  
  126. float l_atten = 0.0f;
  127. float l_nDotl = 0.0f;
  128. float l_nDoth = 0.0f;
  129. float l_power = 0.0f;
  130.  
  131. float4 l_colour = float4(0.0f, 0.0f, 0.0f, 0.0f);
  132.  
  133. [unroll]
  134. for (int i = 0; i < g_numLights; ++i) {
  135. l_lightDir = (g_lights[i].m_pos - l_input.m_posW) / g_lights[i].m_radius;
  136. l_atten = saturate(1.0f - dot(l_lightDir, l_lightDir));
  137.  
  138. l_lightDir = normalize(l_lightDir);
  139. l_h = normalize(l_lightDir + l_v);
  140.  
  141. l_nDotl = saturate(dot(l_n, l_lightDir));
  142. l_nDoth = saturate(dot(l_n, l_h));
  143. l_power = (l_nDotl == 0.0f) ? 0.0f : pow(l_nDoth, g_material.m_specularPower);
  144.  
  145. l_colour += (g_material.m_ambient * (g_globalAmbient + (l_atten * g_lights[i].m_ambient))) +
  146. (g_material.m_diffuse * g_lights[i].m_diffuse * l_nDotl * l_atten) +
  147. (g_material.m_specular * g_lights[i].m_specular * l_power * l_atten);
  148. }
  149.  
  150. return l_colour * tex2D(g_texSampler, l_input.m_tex0);
  151. }
  152.  
  153. //------------------------------------------------------------------------
  154. // Techniques
  155. //------------------------------------------------------------------------
  156. technique draw
  157. {
  158. pass P0
  159. {
  160. vertexShader = compile vs_3_0 VS();
  161. pixelShader = compile ps_3_0 PS();
  162.  
  163. ZEnable = 1;
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement