首页 > 解决方案 > 金属三角测量

问题描述

在此处输入图像描述

如图所示。这些数字是按顺序排列在屏幕上的接触点。我只需要在彩色区域内绘制训练。我们可以用它的算法在金属上做吗?或者有什么建议吗?

var temp1:VertexInFillBrus
temp1 = VertexInFillBrush(a:drawPoints[0] , b:drawPoints[drawPoints.count - 1] , c: drawPoints[drawPoints.count - 2]))
self.bezierCalculatedCoordinates.append(temp1)
self.buildBuffers(device: device)

在绘图命令中

commandEncoder.setRenderPipelineState(renderPipelineState!)
commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
commandEncoder.setFragmentTexture(texture, index: 0)
commandEncoder.drawIndexedPrimitives(type: .triangle, indexCount: 3 , indexType: .uint16, indexBuffer: indexBuffer!, indexBufferOffset: 0 ,instanceCount:bezierCalculatedCoordinates.count)

在着色器中

struct VertexOutFillBrush {
    float4 pos[[position]];
    float2 currentTextureCoordinates;
};
struct VertexInFillBrush
{
    float2 a;
    float2 b;
    float2 c;
};

顶点着色器

 vertex VertexOutFillBrush fillBrushVertex(constant VertexInFillBrush *allParams[[buffer(0)]],
                                            uint vertexId [[vertex_id]],
                                            uint instanceId [[instance_id]])
    {

        VertexInFillBrush params = allParams[instanceId];
        VertexOutFillBrush vo;
        half2 pos;
        ushort x = (vertexId % 3) ;
        switch (x) {

            case 0:
                pos.x = params.a.x ;//+   0.06 * sinpi(params.angle)  * 3.0/4.0  ;
                pos.y = params.a.y ;//-    0.06 * cospi(params.angle);
                vo.currentTextureCoordinates =  float2(1 * pos.x/2.0 + 0.5 , -1 * pos.y/2.0 + 0.5);
                break;
            case 1:
                pos.x = params.b.x ;//+   0.06 * sinpi(params.angle)  * 3.0/4.0  ;
                pos.y = params.b.y ;//-
                 vo.currentTextureCoordinates =  float2(1 * pos.x/2.0 + 0.5 , -1 * pos.y/2.0 + 0.5);
                break;

         default:
                pos.x = params.c.x ;
                pos.y = params.c.y  ;
                vo.currentTextureCoordinates =  float2(1 * pos.x/2.0 + 0.5 , -1 * pos.y/2.0 + 0.5);
                break;
        }
        vo.pos.xy = float2(pos);
        vo.pos.zw = float2(0, 1);

        return vo;
    }

在片段着色器中

fragment float4 fillBrushFragment(VertexOutFillBrush params[[stage_in]],
                                    texture2d<float , access::sample>texture [[texture(0)]])
{

    constexpr sampler defaultSampler;
    float4 color = float4(texture.sample(defaultSampler, float2(params.currentTextureCoordinates))) ;
    if(color.w > 0.02){
        return float4(0.0,0.0,0.0,0.0);
    }
    return float4(1.0,1.0,0.0,0.5);

}

标签: iosmetalmetalkit

解决方案


推荐阅读