我在金属中绘制2个不同的顶点缓冲区,一个带有纹理(忽略顶点颜色数据),另一个没有纹理(纯粹绘制顶点颜色数据):
let commandBuffer = self.commandQueue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd)
//render first buffer with texture
commandEncoder.setRenderPipelineState(self.rps)
commandEncoder.setVertexBuffer(self.vertexBuffer1, offset: 0, at: 0)
commandEncoder.setVertexBuffer(self.uniformBuffer, offset: 0, at: 1)
commandEncoder.setFragmentTexture(self.texture, at: 0)
commandEncoder.setFragmentSamplerState(self.samplerState, at: 0)
commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: count1, instanceCount: 1)
//render second buffer without texture
commandEncoder.setRenderPipelineState(self.rps)
commandEncoder.setVertexBuffer(self.vertexBuffer2, offset: 0, at: 0)
commandEncoder.setVertexBuffer(self.uniformBuffer, offset: 0, at: 1)
commandEncoder.setFragmentTexture(nil, at: 0)
commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: count2, instanceCount: 1)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
着色器看起来像这样:
#include <metal_stdlib>
using namespace metal;
struct Vertex {
float4 position [[position]];
float4 color;
float4 texCoord;
};
struct Uniforms {
float4x4 modelMatrix;
};
vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]],
constant Uniforms &uniforms [[buffer(1)]],
uint vid [[vertex_id]])
{
float4x4 matrix = uniforms.modelMatrix;
Vertex in = vertices[vid];
Vertex out;
out.position = matrix * float4(in.position);
out.color = in.color;
out.texCoord = in.texCoord;
return out;
}
fragment float4 fragment_func(Vertex vert [[stage_in]],
texture2d<float> tex2D [[ texture(0) ]],
sampler sampler2D [[ sampler(0) ]]) {
if (vert.color[0] == 0 && vert.color[1] == 0 && vert.color[2] == 0) {
//texture color
return tex2D.sample(sampler2D, float2(vert.texCoord[0],vert.texCoord[1]));
}
else {
//color color
return vert.color;
}
}
这有更好的方法吗?我想使用纹理的任何顶点我将设置为黑色,着色器检查颜色是否为黑色,如果是,则使用纹理,否则使用颜色。
另外,如果它们在屏幕上重叠,有没有办法使用乘法函数将彩色多边形和纹理多边形混合在一起?似乎MTLBlendOperation只有加/减/最小/最大的选项,没有乘法?
答案 0 :(得分:1)
另一种方法是使用两个不同的片段函数,一个渲染纹理片段,另一个处理彩色顶点。
首先,您需要在加载时创建两个不同的MTLRenderPipelineState
:
let desc = MTLRenderPipelineDescriptor()
/* ...load all other settings in the descriptor... */
// Load the common vertex function.
desc.vertexFunction = library.makeFunction(name: "vertex_func")
// First create the one associated to the textured fragment function.
desc.fragmentFunction = library.makeFunction(name: "fragment_func_textured")
let texturedRPS = try! device.makeRenderPipelineState(descriptor: desc)
// Then modify the descriptor to create the state associated with the untextured fragment function.
desc.fragmentFunction = library.makeFunction(name: "fragment_func_untextured")
let untexturedRPS = try! device.makeRenderPipelineState(descriptor: desc)
然后在渲染时,在编码纹理对象的绘制命令之前,设置纹理状态,在编码无纹理对象的绘制命令之前,切换到无纹理对象。像这样:
//render first buffer with texture
commandEncoder.setRenderPipelineState(texturedRPS) // Set the textured state
commandEncoder.setVertexBuffer(self.vertexBuffer1, offset: 0, at: 0)
commandEncoder.setVertexBuffer(self.uniformBuffer, offset: 0, at: 1)
commandEncoder.setFragmentTexture(self.texture, at: 0)
commandEncoder.setFragmentSamplerState(self.samplerState, at: 0)
commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: count1, instanceCount: 1)
//render second buffer without texture
commandEncoder.setRenderPipelineState(untexturedRPS) // Set the untextured state
commandEncoder.setVertexBuffer(self.vertexBuffer2, offset: 0, at: 0)
commandEncoder.setVertexBuffer(self.uniformBuffer, offset: 0, at: 1)
// No need to set the fragment texture as we don't need it in the fragment function.
// commandEncoder.setFragmentTexture(nil, at: 0)
commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: count2, instanceCount: 1)
顶点函数无需更改。您需要将片段函数拆分为两个:
fragment float4 fragment_func_textured(Vertex vert [[stage_in]],
texture2d<float> tex2D [[ texture(0) ]],
sampler sampler2D [[ sampler(0) ]]) {
//texture color
return tex2D.sample(sampler2D, float2(vert.texCoord[0],vert.texCoord[1]));
}
fragment float4 fragment_func_untextured(Vertex vert [[stage_in]]) {
//color color
return vert.color;
}
你甚至可以继续使用两个不同的顶点函数来输出两个不同的顶点结构,以节省几个字节。实际上,纹理化片段函数只需要texCoord
字段而不是color
,而无纹理函数则是另一种方式。
编辑:您可以使用此片段功能同时使用纹理颜色和顶点颜色:
fragment float4 fragment_func_blended(Vertex vert [[stage_in]],
texture2d<float> tex2D [[ texture(0) ]],
sampler sampler2D [[ sampler(0) ]]) {
// texture color
float4 texture_sample = tex2D.sample(sampler2D, float2(vert.texCoord[0],vert.texCoord[1]));
// vertex color
float4 vertex_sample = vert.color;
// Blend the two together
float4 blended = texture_sample * vertex_sample;
// Or use another blending operation.
// float4 blended = mix(texture_sample, vertex_sample, mix_factor);
// Where mix_factor is in the range 0.0 to 1.0.
return blended;
}