【发布时间】:2022-08-17 20:32:59
【问题描述】:
我正在尝试使用 Vulkan 加载包含透明像素的 2D 精灵,到目前为止,我已经能够加载精灵,但无法使透明度正常工作(透明像素应该与蓝色背景混合)。我不确定该怎么做才能让它正确。
颜色混合状态:
VkPipelineColorBlendAttachmentState colorBlendAttachment {};
colorBlendAttachment.colorWriteMask =
VK_COLOR_COMPONENT_R_BIT |
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT |
VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.blendEnable = VK_TRUE;
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
VkPipelineColorBlendStateCreateInfo colorBlendState {};
colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlendState.logicOpEnable = VK_FALSE;
colorBlendState.logicOp = VK_LOGIC_OP_COPY;
colorBlendState.attachmentCount = 1;
colorBlendState.pAttachments = &colorBlendAttachment;
colorBlendState.blendConstants[0] = 1.f;
colorBlendState.blendConstants[1] = 1.f;
colorBlendState.blendConstants[2] = 1.f;
colorBlendState.blendConstants[3] = 1.f;
深度模板状态:
VkPipelineDepthStencilStateCreateInfo info {};
光栅化状态:
VkPipelineRasterizationStateCreateInfo info {};
info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
info.depthClampEnable = VK_FALSE;
info.rasterizerDiscardEnable = VK_FALSE;
info.polygonMode = polygonMode;
info.lineWidth = 1.0f;
info.cullMode = VK_CULL_MODE_NONE;
info.frontFace = VK_FRONT_FACE_CLOCKWISE;
info.depthBiasEnable = VK_FALSE;
info.depthBiasConstantFactor = 0.0f;
info.depthBiasClamp = 0.0f;
info.depthBiasSlopeFactor = 0.0f;
顶点着色器:
#version 460
layout (location = 0) in vec2 vPosition;
layout (location = 1) in vec2 vTexCoord;
layout (location = 0) out vec2 texCoord;
void main()
{
gl_Position = vec4(vPosition, 0.0f, 1.0f);
texCoord = vTexCoord;
}
片段着色器:
#version 460
layout (location = 0) in vec2 texCoord;
layout (location = 0) out vec4 outFragColor;
layout(set = 0, binding = 0) uniform sampler2D tex;
void main()
{
vec3 color = texture(tex, texCoord).xyz;
outFragColor = vec4(color, 1.0f);
}
-
\"
vec4(color, 1.0f);\“ ... 什么确切地你认为这会做吗? -
将每个顶点颜色设置为 255 的 alpha 值?
-
这是从分段着色器。这怎么会影响“顶点颜色”?
-
哎呀...现在可以工作了,谢谢