LineDraw.hlsl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //--------------------------------------------------------------------------------------
  2. // LineDraw.hlsl - Used to solid lines for GameCode4 (D3D11 renderer)
  3. //
  4. // Part of the GameCode4 Application
  5. //
  6. // GameCode4 is the sample application that encapsulates much of the source code
  7. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  8. // "Rez" Graham, published by Charles River Media.
  9. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  10. //
  11. // If this source code has found it's way to you, and you think it has helped you
  12. // in any way, do the authors a favor and buy a new copy of the book - there are
  13. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  14. // by clicking here:
  15. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  16. //
  17. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  18. //
  19. // The source code is managed and maintained through Google Code:
  20. // http://code.google.com/p/gamecode4/
  21. //
  22. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  23. //
  24. // This program is free software; you can redistribute it and/or
  25. // modify it under the terms of the GNU Lesser GPL v3
  26. // as published by the Free Software Foundation.
  27. //
  28. // This program is distributed in the hope that it will be useful,
  29. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  31. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  32. //
  33. // You should have received a copy of the GNU Lesser GPL v3
  34. // along with this program; if not, write to the Free Software
  35. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  36. //
  37. //--------------------------------------------------------------------------------------
  38. // This shader was originally posted here: http://www.gamedev.net/topic/609031-hlsl-line-geometry-shader/
  39. Texture2D LineTexture;
  40. //-----------------------------------------------------------------------------
  41. // Constant Buffers
  42. //-----------------------------------------------------------------------------
  43. cbuffer cbRenderTarget
  44. {
  45. float4 RenderTargetSize; // x,y are the only ones used.
  46. }
  47. cbuffer cbChangePerFrame
  48. {
  49. matrix WorldViewProjection;
  50. }
  51. cbuffer cbDiffuseColor
  52. {
  53. float4 DiffuseColor;
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Shader Input / Output Structures
  57. //-----------------------------------------------------------------------------
  58. struct VS_INPUT
  59. {
  60. float4 Position : POSITION0;
  61. };
  62. struct GEO_IN
  63. {
  64. float4 Position : POSITION0;
  65. };
  66. struct GEO_OUT
  67. {
  68. float4 Position : SV_POSITION;
  69. float2 TexCoord : TEXCOORD;
  70. };
  71. struct PS_OUTPUT
  72. {
  73. float4 Color : SV_Target0;
  74. };
  75. GEO_IN VS_LineV2(in VS_INPUT input)
  76. {
  77. GEO_IN output;
  78. output.Position = mul(input.Position, WorldViewProjection);
  79. //output.Position = input.Position;
  80. return output;
  81. }
  82. [maxvertexcount(6)]
  83. void GS(line GEO_IN points[2], inout TriangleStream<GEO_OUT> output)
  84. {
  85. float4 p0 = points[0].Position;
  86. float4 p1 = points[1].Position;
  87. float w0 = p0.w;
  88. float w1 = p1.w;
  89. p0.xyz /= p0.w;
  90. p1.xyz /= p1.w;
  91. float3 line01 = p1 - p0;
  92. float3 dir = normalize(line01);
  93. // scale to correct window aspect ratio
  94. float3 ratio = float3(RenderTargetSize.y, RenderTargetSize.x, 0);
  95. ratio = normalize(ratio);
  96. float3 unit_z = normalize(float3(0, 0, -1));
  97. float3 normal = normalize(cross(unit_z, dir) * ratio);
  98. float width = 0.005;
  99. GEO_OUT v[4];
  100. float3 dir_offset = dir * ratio * width;
  101. float3 normal_scaled = normal * ratio * width;
  102. float3 p0_ex = p0 - dir_offset;
  103. float3 p1_ex = p1 + dir_offset;
  104. v[0].Position = float4(p0_ex - normal_scaled, 1) * w0;
  105. v[0].TexCoord = float2(0,0);
  106. v[1].Position = float4(p0_ex + normal_scaled, 1) * w0;
  107. v[1].TexCoord = float2(0,0);
  108. v[2].Position = float4(p1_ex + normal_scaled, 1) * w1;
  109. v[2].TexCoord = float2(0,0);
  110. v[3].Position = float4(p1_ex - normal_scaled, 1) * w1;
  111. v[3].TexCoord = float2(0,0);
  112. output.Append(v[2]);
  113. output.Append(v[1]);
  114. output.Append(v[0]);
  115. output.RestartStrip();
  116. output.Append(v[3]);
  117. output.Append(v[2]);
  118. output.Append(v[0]);
  119. output.RestartStrip();
  120. }
  121. PS_OUTPUT PS_LineV2(GEO_OUT input)
  122. {
  123. PS_OUTPUT output;
  124. //output.Color = LineTexture.Sample(LinearSampler, input.TexCoord);
  125. //output.Color = float4(input.TexCoord.xy, 0, 1);
  126. output.Color = DiffuseColor;
  127. return output;
  128. }
  129. technique10 LineV2Technique
  130. {
  131. pass P0
  132. {
  133. SetVertexShader(CompileShader(vs_4_0, VS_LineV2()));
  134. SetGeometryShader(CompileShader(gs_4_0, GS()));
  135. SetPixelShader(CompileShader(ps_4_0, PS_LineV2()));
  136. }
  137. }