学习ios Metal(2)—尺寸可调的点的绘制

        metal的基础知识入门,首推Metal By Example系列:http://metalbyexample.com/。博主的相关文章,主要给出工程实际遇到的典型问题及其解决方案。

       

       本节源码:https://github.com/sjy234sjy234/Learn-Metal/tree/master/MetalPoint。metal图形渲染引擎,内置是支持绘制尺寸可调的点的,绘制出来是一个方形点,首先,metal的drawPrimitives方法,一共支持5种MTLPrimitiveType类型:

typedef NS_ENUM(NSUInteger, MTLPrimitiveType) {

    MTLPrimitiveTypePoint = 0,

    MTLPrimitiveTypeLine = 1,

    MTLPrimitiveTypeLineStrip = 2,

    MTLPrimitiveTypeTriangle = 3,

    MTLPrimitiveTypeTriangleStrip = 4,

} NS_ENUM_AVAILABLE(10_11, 8_0);

        先在Objective-C中给出支持颜色的Vertex的定义:

typedef struct
{
    vector_float4 position;
    vector_float4 color;
} Vertex;

        在PointRenderer类中给出需要绘制的点的缓存分配:

//buffer definition
@property (nonatomic, strong) id<MTLBuffer> pointVertexBuffer;

//buffer setup
- (void)buildResources
{
    static const Vertex pointVertices[] =
    {
        { .position = { -0.5, -0.5, 0, 1 }, .color = { 1, 0, 0, 1 } },
        { .position = { -0.5,  0.5, 0, 1 }, .color = { 0, 1, 0, 1 } },
        { .position = {  0.5, -0.5, 0, 1 }, .color = { 0, 0, 1, 1 } },
        { .position = {  0.5,  0.5, 0, 1 }, .color = { 0, 1, 0, 1 } }
    };
    _pointVertexBuffer = [_metalContext.device newBufferWithBytes:pointVertices
                                                              length:sizeof(pointVertices)
                                                             options:MTLResourceOptionCPUCacheModeDefault];
}

        最后在draw函数中调用drawPrimitives函数绘制MTLPrimitiveTypePoint即可:

[commandEncoder drawPrimitives: MTLPrimitiveTypePoint vertexStart: 0 vertexCount: 4];

        控制点的尺寸是在shader里面进行的,下面是point.metal的shader文件里面的代码,实现了点的尺寸和颜色的设置:

#include <metal_stdlib>
using namespace metal;

struct Vertex
{
    float4 position [[position]];
    float4 color;
};

struct PVertex{
    float4 position [[position]];
    float4 color;
    float size[[point_size]];
};

vertex PVertex point_vertex_main(device Vertex *vertices [[buffer(0)]],
                           uint vid [[vertex_id]])
{
    PVertex outVertex;
    outVertex.position = vertices[vid].position;
    outVertex.color = vertices[vid].color;
    outVertex.size = 30;
    
    return outVertex;
}

fragment float4 point_fragment_main(PVertex inVertex [[stage_in]])
{
    return inVertex.color;
}

        其中的关键是在vertex_main传给fragment_main的变量PVertex有一个声明内置变量float size[[point_size]],修改size的值就可以调整绘制点的尺寸。

猜你喜欢

转载自blog.csdn.net/sjy234sjy234/article/details/81837178
今日推荐