【发布时间】:2021-07-06 16:58:51
【问题描述】:
我正在使用 Metal 创建动画。我使用 MTKViewDelegate 方法在黑色 MTKView 上绘制动画。我的目的是模拟汽油并使其像这样半透明:
现在看起来像这样:
当我在片段着色器中设置 alpha 时,没有任何反应,尽管着色效果很好:
fragment half4 fragment_shader(VertexOut fragData [[stage_in]], float2 pointCoord [[point_coord]]) {
float dist = length(pointCoord - float2(0.6));
float4 color = fragData.color;
// Marking particles round shaped
color.a = 1.0 - smoothstep(0.4, 0.4, dist);
// Setting yellow color with 0.5 alpha
color.b = (0.6, 0.2, 0.7, 0.5);
return half4(color);
}
我也有附件描述符,但它仍然无法使 alpha 工作:
let attachmentDesciptor: MTLRenderPipelineColorAttachmentDescriptor = pipelineDescriptor.colorAttachments[0] attachmentDesciptor.isBlendingEnabled = true attachmentDesciptor.rgbBlendOperation = MTLBlendOperation.add attachmentDesciptor.alphaBlendOperation = MTLBlendOperation.add attachmentDesciptor.sourceRGBBlendFactor = .sourceAlpha attachmentDesciptor.sourceAlphaBlendFactor = .sourceAlpha attachmentDesciptor.destinationRGBBlendFactor = MTLBlendFactor.oneMinusSourceAlpha attachmentDesciptor.destinationAlphaBlendFactor = MTLBlendFactor.oneMinusSourceAlpha
已编辑:
我也尝试将metalView.isOpaque 设置为false,但它只是改变了背景:
Same animation with different background
任何帮助将不胜感激!
【问题讨论】:
-
您是否尝试在
MTKView上将opaque设置为NO? -
是的,它使背景变白,但不改变动画