melonJS icon indicating copy to clipboard operation
melonJS copied to clipboard

[Feature request] Ellipse / line / rect stroke thickness

Open diigits opened this issue 5 years ago • 2 comments

When drawing the stroke of an ellipse (and strokeRect, StrokeLine, etc.) it would be nice to be able to add a thickness to the stroke. On rectangles and ellipses an option to have the stroke be either within the bounds or on the bounds would be great as well. This option would ideally act like the CSS property box-sizing: border-box; and would make it so that your shape would automatically be made smaller so that the thicker line could fit inside the originally specified width and height.

I imagine it as an option on the renderable which is what I illustrated here.

circle

diigits avatar May 27 '20 02:05 diigits

👍 related to #994, should somehow work for now at least in the fitStrokeToBoundingBox style when false, and if you have friendly GPU implementing a lineWidth > 1.

obiot avatar May 27 '20 06:05 obiot

I was toying with chatGPT this morning, and asked him/her to write me a fragment shader that draws a line, including with a thickness parameter, here it is:

#version 100

uniform vec2 u_resolution;
uniform vec2 u_start;
uniform vec2 u_end;
uniform float u_thickness;

void main() {
    vec2 st = gl_FragCoord.xy/u_resolution;
    float y = smoothstep(u_start.x, u_end.x, st.x);
    float d = abs(st.y - y);
    if (d < u_thickness/2.0) {
        gl_FragColor = vec4(1.0);
    } else {
        gl_FragColor = vec4(0.0);
    }
}

Pretty awesome !

It initially gave me a WebGL 2.0 version, and without any variable, but as I explained what I needed, we came to this one after a couple of iterations, Is it not amazing ?

obiot avatar Dec 15 '22 02:12 obiot