I’m trying to apply rounded corners to a texture, while keeping the texture’s size to be exactly half that of the ThreeJS plane it is being drawn on. As well as keeping the aspect ratio of the image, and not stretching the rounded corners. I’ve gotten pretty close to what I want, but as you can see in the screenshot here, the texture is not quite filling the black area. It should not have the extra black space on the left and right sides of the texture.
In this screenshot, the texture is filling the area the way I’d like, but the rounded corners are being stretched:
Here’s my fragment shader and a shadertoy: Shader - Shadertoy BETA
float roundedRectangle (vec2 uv, vec2 pos, vec2 size, float radius, float thickness)
{
float d = length(max(abs(uv - pos),size) - size) - radius;
return smoothstep(0.66, 0.33, d / thickness * 5.0);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord.xy / iResolution.xy;
float aspect = iResolution.x / iResolution.y;
vec2 center = vec2(.5);
vec2 texCoord = (uv - .25) * 2.;//scale texture to be half the size of three.js plane
//This doesn't quite fill the area correctly, but the rounded corners don't get stretched:
float rounded = roundedRectangle(vec2(uv.x * aspect, uv.y), vec2(center.x * aspect, center.y), vec2(.2 * aspect,.2), 0.05, 0.05);
//This fills the area correctly, but the rounded corners get stretched:
//float rounded = roundedRectangle(vec2(uv.x, uv.y), vec2(center.x, center.y), vec2(.2,.2), 0.05, 0.05);
vec4 col = texture(iChannel0, texCoord) * rounded;
if (uv.x > .25 && uv.x < .75 && uv.y > .25 && uv.y < .75) {
fragColor = col;
} else {
//notice the black space left around the texture
fragColor = vec4(1., 0., 0., 0.);
}
}
If context is helpful:
In the end, I should be able to apply this as a ShaderMaterial to dom image elements with various sizes and aspect ratios. I’m creating a ThreeJS plane for each dom image element that is exactly twice as big as the image element so that I can do some other stuff “outside” of the image element with the shader. That’s why I’m scaling the texture down to half size and placing it in the middle of the material.
Any help would be appreciated! Thanks!