Round Box SDF scaling issue

I am using the Inigo Quilez SDF function to generate a round box shape. The aim is to be able to control the shape scale, smoothness and roundness. I've achieved a pretty good result but I am facing an issue when I change the scale. Here is my current code.

fixed3 position = // World space
fixed3 size = // Local scale
fixed radius = // From 0 to 1
fixed smoothness = // From 0 to 1

radius = lerp(0, 1 - smoothness, radius);

radius *= size;
smoothness *= size;

size = size - radius;
size = size - smoothness;

fixed box = length(max(abs(position) - size, 0.0)) - radius;
box = smoothstep(0, smoothness, box);


As I said it works well but when I scale my shape in the X axis the result is not correct. Here is a illustration of the issue.

It seems that the issue comes from the scaling part of the radius and the smoothness and the length call. But I don't see any way to fix it. If you also have some suggestions about some ways to improve the code I would be glad to hear you.

Thanks !

• Please don't cross post. If necessary, request migration instead. – Pikalek Aug 4 '17 at 22:28