Triangulation in Metal

时间秒杀一切 提交于 2019-12-11 17:19:46

问题


As Shown in the picture. The numbers are touchpoints in the screen in order. I need to draw the traingulation only inside Colored area. Can we do it in metal with its algorithm or Any suggestions please

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)

In Draw Command

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)

In shader

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

Vertex Shader

 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;
    }

In fragment Shader

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);

}

来源:https://stackoverflow.com/questions/54903777/triangulation-in-metal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!