1、并集
float sdCircle(vec2 uv,float r){return length(uv)-r;
}
float sdRect(vec2 uv,float r){return max(abs(uv.x),abs(uv.y))-r;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{float w = 1.0/iResolution.y;// 计算纹理坐标vec2 uv = fragCoord/iResolution.xy;// 将纹理坐标系原点移动到屏幕中央uv-=0.5;// 调整 x 轴比例,使得坐标系纵横比一致uv.x*=iResolution.x/iResolution.y;vec3 col = vec3(0.0);// `sdRect(uv - vec2(0.1, 0.1), 0.2)`计算当前 uv 坐标到中心在 (0.2, 0.2)、半径为 0.2 的矩形的距离float r= sdRect(uv-vec2(0.1,0.1),0.2);// 大于0代表像素点不在矩形范围内r = r > 0.0 ? 0.0:1.0;float c = sdCircle(uv-vec2(-0.1,-0.1),0.2);c = c > 0.0 ? 0.0:1.0;// 并集就是像素属于圆或者矩形都可以着色,即r=1 or c=1float result = max(r,c);//r和c只能为0或1,取最大值即有一个为1结果就是1col = mix(col,vec3(1.0,0.0,1.0),result);fragColor = vec4(col,1.0);
}
2、差集
float sdCircle(vec2 uv,float r){return length(uv)-r;
}
float sdRect(vec2 uv,float r){return max(abs(uv.x),abs(uv.y))-r;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{float w = 1.0/iResolution.y;// 计算纹理坐标vec2 uv = fragCoord/iResolution.xy;// 将纹理坐标系原点移动到屏幕中央uv-=0.5;// 调整 x 轴比例,使得坐标系纵横比一致uv.x*=iResolution.x/iResolution.y;vec3 col = vec3(0.0);// `sdRect(uv - vec2(0.1, 0.1), 0.2)`计算当前 uv 坐标到中心在 (0.2, 0.2)、半径为 0.2 的矩形的距离float r= sdRect(uv-vec2(0.1,0.1),0.2);// 大于0代表像素点不在矩形范围内r = r > 0.0 ? 0.0:1.0;float c = sdCircle(uv-vec2(-0.1,-0.1),0.2);c = c > 0.0 ? 0.0:1.0;// 差集就是像素只属于圆或者只属于矩形才能着色,即(r=1 and c=0)&&(r=0 and c=1)float result = abs(r-c);col = mix(col,vec3(1.0,0.0,1.0),result);fragColor = vec4(col,1.0);
}
3、交集
float sdCircle(vec2 uv,float r){return length(uv)-r;
}
float sdRect(vec2 uv,float r){return max(abs(uv.x),abs(uv.y))-r;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{float w = 1.0/iResolution.y;// 计算纹理坐标vec2 uv = fragCoord/iResolution.xy;// 将纹理坐标系原点移动到屏幕中央uv-=0.5;// 调整 x 轴比例,使得坐标系纵横比一致uv.x*=iResolution.x/iResolution.y;vec3 col = vec3(0.0);// `sdRect(uv - vec2(0.1, 0.1), 0.2)`计算当前 uv 坐标到中心在 (0.2, 0.2)、半径为 0.2 的矩形的距离float r= sdRect(uv-vec2(0.1,0.1),0.2);// 大于0代表像素点不在矩形范围内r = r > 0.0 ? 0.0:1.0;float c = sdCircle(uv-vec2(-0.1,-0.1),0.2);c = c > 0.0 ? 0.0:1.0;// 交集就是像素既属于圆又属于矩形才能着色,即(r=1 and c=1)&&(r=1 and c=1)float result = r*c;col = mix(col,vec3(1.0,0.0,1.0),result);fragColor = vec4(col,1.0);
}