我正在尝试在片段Shader中绘制一个圆圈从CPU端,我将接触点的位置传递给Shader。
以下是阴影的输入/输出结构
struct smudgeOut {
float4 pos[[position]];
float pointSize [[point_size]];
float2 textureCoordinates;
float2 drawPoint;
};
struct SmudgeIn
{
float2 drawPoint;
float size;
float4 color;
};
在顶点着色器中,我正在绕过的点周围创建一个正方形
vertex smudgeOut vertex_shader_smudge(constant SmudgeIn *allParams[[buffer(0)]],
uint vertexId [[vertex_id]],
uint instanceId [[instance_id]])
{
SmudgeIn params = allParams[instanceId];
smudgeOut vo;
half2 pos;
ushort x = (vertexId % 4) ;
switch (x) {
case 1:
pos.x = params.drawPoint.x + params.size * 3.0/4.0 ;
pos.y = params.drawPoint.y + params.size ;
vo.textureCoordinates = float2(1,0);;
break;
case 2:
pos.x = params.drawPoint.x - params.size * 3.0/4.0 ;
pos.y = params.drawPoint.y - params.size ;
vo.textureCoordinates = float2(0,1);
break;
case 3:
pos.x = params.drawPoint.x + params.size * 3.0/4.0 ;
pos.y = params.drawPoint.y - params.size ;
vo.textureCoordinates = float2(1,1);
break;
default:
pos.x = params.drawPoint.x - params.size * 3.0/4.0 ;
pos.y = params.drawPoint.y + params.size ;
vo.textureCoordinates = float2(0,0);
break;
}
vo.pos.xy = float2(pos);
vo.drawPoint = params.drawPoint;
vo.pointSize = 60;
vo.pos.zw = float2(0, 1);
return vo;
}
这是我创建的正方形内的片段着色器,我想在其中有一个圆,使正方形的其余部分变为无色
fragment float4 fragment_shader_smudge(smudgeOut params[[stage_in]],
float2 uv[[point_coord]],
texture2d<float , access::sample>texture [[texture(0)]],
texture2d<float , access::sample>texture1 [[texture(1)]])
{
float2 uvPos = uv;
float2 centerpoint = params.drawPoint;
float2 diff = uvPos - centerpoint;
float dist = length(diff);
float4 color;
if(dist < 0.05){
float4 color = float4(1.0f, 0.0f, 1.0f, 1.0f);
}else{
discard_fragment();
}
return color;
}
我无法绕过我所经过的点,所以它也应该在Square内。建议我这里有什么问题