SpriteLine.bsl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. Parameters =
  2. {
  3. mat4x4 worldTransform;
  4. float invViewportWidth;
  5. float invViewportHeight;
  6. StructBuffer linePoints;
  7. float lineWidth;
  8. float4 tint;
  9. };
  10. Technique =
  11. {
  12. Language = "HLSL11";
  13. Pass =
  14. {
  15. Target =
  16. {
  17. Blend = true;
  18. Color = { SRCA, SRCIA, ADD };
  19. WriteMask = RGB;
  20. };
  21. DepthRead = false;
  22. DepthWrite = false;
  23. Common =
  24. {
  25. struct VStoFS
  26. {
  27. float4 position : SV_POSITION;
  28. float2 screenPos : TEXCOORD1;
  29. uint lineIdx : LINEIDX;
  30. };
  31. };
  32. Vertex =
  33. {
  34. float invViewportWidth;
  35. float invViewportHeight;
  36. float4x4 worldTransform;
  37. struct VertexInput
  38. {
  39. float2 position : POSITION;
  40. uint lineIdx : BLENDINDICES0;
  41. };
  42. VStoFS main(VertexInput input)
  43. {
  44. float4 tfrmdPos = mul(worldTransform, float4(input.position, 0, 1));
  45. float tfrmdX = -1.0f + (tfrmdPos.x * invViewportWidth);
  46. float tfrmdY = 1.0f - (tfrmdPos.y * invViewportHeight);
  47. VStoFS output;
  48. output.position = float4(tfrmdX, tfrmdY, 0, 1);
  49. output.screenPos = tfrmdPos.xy;
  50. output.lineIdx = input.lineIdx;
  51. return output;
  52. }
  53. };
  54. Fragment =
  55. {
  56. StructuredBuffer<float2> linePoints;
  57. float lineWidth;
  58. float4 tint;
  59. float getSquaredDistanceToLine(float2 a, float2 b, float2 pt)
  60. {
  61. float2 lineDiff = b - a;
  62. float sqrdLineLength = dot(lineDiff, lineDiff);
  63. float2 pointDiff = pt - a;
  64. if(sqrdLineLength < 0.0001f) // a == b
  65. return dot(pointDiff, pointDiff);
  66. else
  67. {
  68. float t = clamp(dot(pointDiff, lineDiff / sqrdLineLength), 0, 1);
  69. float2 projPoint = a + lineDiff * t;
  70. float2 toLineDiff = projPoint - pt;
  71. return dot(toLineDiff, toLineDiff);
  72. }
  73. }
  74. float4 main(VStoFS input) : SV_Target
  75. {
  76. // Get number of lines
  77. uint numPoints;
  78. uint dummy;
  79. linePoints.GetDimensions(numPoints, dummy);
  80. uint numLines = numPoints - 1;
  81. // Find nearest line
  82. //// Distance to current line
  83. int lineIdx = (int)input.lineIdx;
  84. float2 a = linePoints.Load(lineIdx + 0);
  85. float2 b = linePoints.Load(lineIdx + 1);
  86. float sqrdDist = getSquaredDistanceToLine(a, b, input.screenPos);
  87. //// Distance to previous line
  88. int prevLineIdx = max(0, lineIdx - 1);
  89. a = linePoints.Load(prevLineIdx + 0);
  90. b = linePoints.Load(prevLineIdx + 1);
  91. sqrdDist = min(sqrdDist, getSquaredDistanceToLine(a, b, input.screenPos));
  92. //// Distance to next line
  93. int nextLineIdx = min((int)numLines - 1, lineIdx + 1);
  94. a = linePoints.Load(nextLineIdx + 0);
  95. b = linePoints.Load(nextLineIdx + 1);
  96. sqrdDist = min(sqrdDist, getSquaredDistanceToLine(a, b, input.screenPos));
  97. float sqrdLineWidth = lineWidth * lineWidth;
  98. sqrdDist = max(sqrdDist - sqrdLineWidth, 0.0f);
  99. float featherWidth = 1.0f; // When changing this change the width of the line quads as well
  100. float filtered = sqrdDist / (featherWidth * featherWidth); // TODO - Use a different filter like Gaussian
  101. float weight = 1.0f - clamp(filtered, 0, 1);
  102. return float4(tint.rgb, tint.a * weight);
  103. }
  104. };
  105. };
  106. };
  107. Technique =
  108. {
  109. Language = "GLSL";
  110. Pass =
  111. {
  112. Target =
  113. {
  114. Blend = true;
  115. Color = { SRCA, SRCIA, ADD };
  116. WriteMask = RGB;
  117. };
  118. DepthRead = false;
  119. DepthWrite = false;
  120. Vertex =
  121. {
  122. uniform float invViewportWidth;
  123. uniform float invViewportHeight;
  124. uniform mat4 worldTransform;
  125. in vec2 bs_position;
  126. in int bs_blendindices;
  127. out vec2 screenPos;
  128. out flat int lineIdx;
  129. out gl_PerVertex
  130. {
  131. vec4 gl_Position;
  132. };
  133. void main()
  134. {
  135. vec4 tfrmdPos = worldTransform * vec4(bs_position, 0, 1);
  136. float tfrmdX = -1.0f + (tfrmdPos.x * invViewportWidth);
  137. float tfrmdY = 1.0f - (tfrmdPos.y * invViewportHeight);
  138. gl_Position = vec4(tfrmdX, tfrmdY, 0, 1);
  139. screenPos = tfrmdPos.xy;
  140. lineIdx = bs_blendindices;
  141. }
  142. };
  143. Fragment =
  144. {
  145. uniform samplerBuffer linePoints;
  146. uniform float lineWidth;
  147. uniform vec4 tint;
  148. in vec2 screenPos;
  149. in flat int lineIdx;
  150. out vec4 fragColor;
  151. float getSquaredDistanceToLine(vec2 a, vec2 b, vec2 pt)
  152. {
  153. vec2 lineDiff = b - a;
  154. float sqrdLineLength = dot(lineDiff, lineDiff);
  155. vec2 pointDiff = pt - a;
  156. if(sqrdLineLength < 0.0001f) // a == b
  157. return dot(pointDiff, pointDiff);
  158. else
  159. {
  160. float t = clamp(dot(pointDiff, lineDiff / sqrdLineLength), 0, 1);
  161. vec2 projPoint = a + lineDiff * t;
  162. vec2 toLineDiff = projPoint - pt;
  163. return dot(toLineDiff, toLineDiff);
  164. }
  165. }
  166. void main()
  167. {
  168. // Get number of lines
  169. int numPoints = textureSize(linePoints);
  170. int numLines = numPoints - 1;
  171. // Find nearest line
  172. //// Distance to current line
  173. vec2 a = texelFetch(linePoints, lineIdx + 0).xy;
  174. vec2 b = texelFetch(linePoints, lineIdx + 1).xy;
  175. float sqrdDist = getSquaredDistanceToLine(a, b, screenPos);
  176. //// Distance to previous line
  177. int prevLineIdx = max(0, lineIdx - 1);
  178. a = texelFetch(linePoints, prevLineIdx + 0).xy;
  179. b = texelFetch(linePoints, prevLineIdx + 1).xy;
  180. sqrdDist = min(sqrdDist, getSquaredDistanceToLine(a, b, screenPos));
  181. //// Distance to next line
  182. int nextLineIdx = min(numLines - 1, lineIdx + 1);
  183. a = texelFetch(linePoints, nextLineIdx + 0).xy;
  184. b = texelFetch(linePoints, nextLineIdx + 1).xy;
  185. sqrdDist = min(sqrdDist, getSquaredDistanceToLine(a, b, screenPos));
  186. float sqrdLineWidth = lineWidth * lineWidth;
  187. sqrdDist = max(sqrdDist - sqrdLineWidth, 0.0f);
  188. float featherWidth = 1.0f; // When changing this change the width of the line quads as well
  189. float filtered = sqrdDist / (featherWidth * featherWidth); // TODO - Use a different filter like Gaussian
  190. float weight = 1.0f - clamp(filtered, 0, 1);
  191. fragColor = vec4(tint.rgb, tint.a * weight);
  192. }
  193. };
  194. };
  195. };