testgpurender_msdf.frag.hlsl 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. cbuffer Context : register(b0, space3) {
  2. float distance_field_range;
  3. float2 texture_size;
  4. };
  5. Texture2D u_texture : register(t0, space2);
  6. SamplerState u_sampler : register(s0, space2);
  7. struct PSInput {
  8. float4 v_color : COLOR0;
  9. float2 v_uv : TEXCOORD0;
  10. };
  11. struct PSOutput {
  12. float4 o_color : SV_Target;
  13. };
  14. float median(float r, float g, float b) {
  15. return max(min(r, g), min(max(r, g), b));
  16. }
  17. float screenPxRange(float2 texCoord) {
  18. float2 unitRange = float2(distance_field_range, distance_field_range)/texture_size;
  19. float2 screenTexSize = float2(1.0, 1.0)/fwidth(texCoord);
  20. return max(0.5*dot(unitRange, screenTexSize), 1.0);
  21. }
  22. PSOutput main(PSInput input) {
  23. PSOutput output;
  24. float3 msd = u_texture.Sample(u_sampler, input.v_uv).rgb;
  25. float sd = median(msd.r, msd.g, msd.b);
  26. float screenPxDistance = screenPxRange(input.v_uv)*(sd - 0.5);
  27. float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0);
  28. output.o_color.rgb = input.v_color.rgb;
  29. output.o_color.a = (input.v_color.a * opacity);
  30. return output;
  31. }