1.将矩形旋转45度(PI/4)
#define PI 3.1415926
float Box(vec2 uv,vec2 bottomLeft,vec2 topRight,vec2 blur)
{
vec2 c = smoothstep(bottomLeft,bottomLeft + blur,uv);
vec2 co2 = smoothstep(topRight,topRight - blur ,uv);
c *= co2;
return c.x * c.y;
}
mat2 rotate2d(float angle)
{
mat2 m = mat2(cos(angle),-sin(angle),sin(angle),cos(angle));
return m;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
uv.x *= iResolution.x / iResolution.y;
uv *= 4.0;
uv = fract(uv);
uv = uv * rotate2d(PI / 4.0);
float c1 = Box(uv,vec2(-0.0,-0.0),vec2(1.5,1.5),vec2(0.01,0.01));
fragColor = vec4(c1 * vec3(1.0,1.0,0.0),1.0);
}
效果:
2.分段处理
#define PI 3.1415926
float Box(vec2 uv,vec2 bottomLeft,vec2 topRight,vec2 blur)
{
vec2 c = smoothstep(bottomLeft,bottomLeft + blur,uv);
vec2 co2 = smoothstep(topRight,topRight - blur ,uv);
c *= co2;
return c.x * c.y;
}
mat2 rotate2d(float angle)
{
mat2 m = mat2(cos(angle),-sin(angle),sin(angle),cos(angle));
return m;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;
uv.x *= iResolution.x / iResolution.y;
uv *= 4.0;
float id = mod(uv.x,2.0);
float idy = mod(uv.y,2.0);
int idd = int(floor(id));
int iddy = int(floor(idy));
uv = fract(uv);
if(iddy == 0){
//左右翻转
uv.x = 1.0 - uv.x;
}
if(idd == 0){
uv.y = 1.0 - uv.y;
uv = uv * rotate2d(PI / 4.0);
}else if(idd == 1){
uv = uv * rotate2d(PI / 4.0);
}else if(idd == 2){
uv.x = 1.0 - uv.x;
uv = uv * rotate2d(-PI / 4.0);
}
float c1 = Box(uv,vec2(-0.0,-0.0),vec2(1.5,1.5),vec2(0.01,0.01));
fragColor = vec4(c1 * vec3(1.0,1.0,0.0),1.0);
}
效果: