Chlumsky / msdfgen

Multi-channel signed distance field generator
MIT License
3.97k stars 412 forks source link

[Question] Direct MSDF implementation #132

Closed Elzianor closed 3 years ago

Elzianor commented 3 years ago

Hi!

I'm trying to implement your direct MSDF algorithm using the "Shape Decomposition for Multi-channel Distance Fields" paper, p. 4.4.

I've generated the following MSDF 64x64 pixels texture:

image

Also I'm using the following HLSL shader to reconstruct glyph from MSDF texture:

float median(float a, float b, float c)
{
    return max(min(a,b), min(max(a,b), c));
}

float4 MSDF_PS(PS_OUTPUT_BASIC input) : SV_TARGET
{
    float3 dist = shaderTexture.Sample(sampleType, input.uv).rgb;

    float d = median(dist.r, dist.g, dist.b) - 0.5;

    float w = clamp(d/fwidth(d) + 0.5, 0.0, 1.0);

    float4 outside = float4(0, 0, 0, 0);
    float4 inside = float4(0, 0, 0, 1);
    float4 color = lerp(outside, inside, w);

    return color;
}

I'm just changed here mix to lerp because it is the analog from HLSL.

The result is the following:

image

As you probably can see the corners are not very sharp (and it is 64x64, not even 32x32 pixels texture).

I understand that without code review it may be hard to tell, but couldn't you probably point me where have I made mistake(s)?

Thanks in advance.