From 326593eb4fffe38279c1b428bc6e328eccd8146e Mon Sep 17 00:00:00 2001 From: Xiang Silian Date: Fri, 26 Sep 2025 18:21:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=80=8F=E6=98=8E=E7=9A=84?= =?UTF-8?q?=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- and_proj/app/build.gradle | 6 +- .../api/texture_loading/texture_loading.cpp | 57 ++++++++++++++---- shaders/texture_loading/glsl/texture.frag | 6 +- shaders/texture_loading/glsl/texture.frag.spv | Bin 2076 -> 2056 bytes 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/and_proj/app/build.gradle b/and_proj/app/build.gradle index 2aacf93..1ffa43b 100644 --- a/and_proj/app/build.gradle +++ b/and_proj/app/build.gradle @@ -21,9 +21,9 @@ android { externalNativeBuild { cmake { abiFilters.addAll( 'arm64-v8a') - //arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_static', '-DVKB_VALIDATION_LAYERS=OFF' - arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_static', '-DVKB_VALIDATION_LAYERS=OFF', "-DCMAKE_BUILD_TYPE=Release" - cppFlags "-DRELEASE_MODE" + arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_static', '-DVKB_VALIDATION_LAYERS=OFF' + //arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_static', '-DVKB_VALIDATION_LAYERS=OFF', "-DCMAKE_BUILD_TYPE=Release" + //cppFlags "-DRELEASE_MODE" } } } diff --git a/samples/api/texture_loading/texture_loading.cpp b/samples/api/texture_loading/texture_loading.cpp index 079cb69..c8cc4dc 100644 --- a/samples/api/texture_loading/texture_loading.cpp +++ b/samples/api/texture_loading/texture_loading.cpp @@ -568,6 +568,12 @@ void TextureLoading::build_command_buffers() vkCmdDraw(draw_cmd_buffers[i], 6, 1, 0, 0); + + + draw_point_cloud(draw_cmd_buffers[i]); + //draw_point_cloud_line(draw_cmd_buffers[i]); + + vkCmdBindDescriptorSets(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout, 0, 1, &descriptor_set, 0, NULL); vkCmdBindPipeline(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.solid); @@ -576,9 +582,6 @@ void TextureLoading::build_command_buffers() vkCmdBindIndexBuffer(draw_cmd_buffers[i], index_buffer->get_handle(), 0, VK_INDEX_TYPE_UINT32); vkCmdDrawIndexed(draw_cmd_buffers[i], index_count, 1, 0, 0, 0); - draw_point_cloud(draw_cmd_buffers[i]); - //draw_point_cloud_line(draw_cmd_buffers[i]); - //draw_ui(draw_cmd_buffers[i]); vkCmdEndRenderPass(draw_cmd_buffers[i]); @@ -1095,15 +1098,37 @@ void TextureLoading::prepare_pipelines() VK_FRONT_FACE_COUNTER_CLOCKWISE, 0); - VkPipelineColorBlendAttachmentState blend_attachment_state = - vkb::initializers::pipeline_color_blend_attachment_state( - 0xf, - VK_FALSE); + //VkPipelineColorBlendAttachmentState blend_attachment_state = + // vkb::initializers::pipeline_color_blend_attachment_state( + // 0xf, + // VK_FALSE); - VkPipelineColorBlendStateCreateInfo color_blend_state = - vkb::initializers::pipeline_color_blend_state_create_info( - 1, - &blend_attachment_state); + VkPipelineColorBlendAttachmentState colorBlendAttachment{}; + colorBlendAttachment.blendEnable = VK_TRUE; + colorBlendAttachment.colorWriteMask = + VK_COLOR_COMPONENT_R_BIT | + VK_COLOR_COMPONENT_G_BIT | + VK_COLOR_COMPONENT_B_BIT | + VK_COLOR_COMPONENT_A_BIT; + + // 常用的Alpha混合公式 + 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_ONE; + colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; + colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; + + //VkPipelineColorBlendStateCreateInfo color_blend_state = + // vkb::initializers::pipeline_color_blend_state_create_info( + // 1, + // &blend_attachment_state); + + VkPipelineColorBlendStateCreateInfo colorBlending{}; + colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; + colorBlending.logicOpEnable = VK_FALSE; + colorBlending.attachmentCount = 1; + colorBlending.pAttachments = &colorBlendAttachment; // Note: Using reversed depth-buffer for increased precision, so Greater depth values are kept VkPipelineDepthStencilStateCreateInfo depth_stencil_state = @@ -1160,7 +1185,7 @@ void TextureLoading::prepare_pipelines() pipeline_create_info.pVertexInputState = &vertex_input_state; pipeline_create_info.pInputAssemblyState = &input_assembly_state; pipeline_create_info.pRasterizationState = &rasterization_state; - pipeline_create_info.pColorBlendState = &color_blend_state; + pipeline_create_info.pColorBlendState = &colorBlending; //&color_blend_state; pipeline_create_info.pMultisampleState = &multisample_state; pipeline_create_info.pViewportState = &viewport_state; pipeline_create_info.pDepthStencilState = &depth_stencil_state; @@ -1479,7 +1504,13 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options) dataSize = lineImage.size(); processWithVulkan(lineImage.data(), width, height, rowStride, dataSize, texture_point_line); - load_texture(); + const char* filename_test = "assets/test_trans.png"; + std::vector image_test; + unsigned w_t, h_t; + unsigned error_t = lodepng::decode(image_test, w_t, h_t, filename_test, LCT_RGBA, 8); + processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), texture); + //load_texture(); + generate_quad(); prepare_uniform_buffers(); diff --git a/shaders/texture_loading/glsl/texture.frag b/shaders/texture_loading/glsl/texture.frag index ccdb2fd..193cdac 100644 --- a/shaders/texture_loading/glsl/texture.frag +++ b/shaders/texture_loading/glsl/texture.frag @@ -37,5 +37,9 @@ void main() vec3 diffuse = max(dot(N, L), 0.0) * vec3(1.0); float specular = pow(max(dot(R, V), 0.0), 16.0) * color.a; - outFragColor = vec4(diffuse * color.rgb + specular, 1.0); + vec4 textureColor = color; + if (textureColor.a < 0.1) { + discard; + } + outFragColor = textureColor; } \ No newline at end of file diff --git a/shaders/texture_loading/glsl/texture.frag.spv b/shaders/texture_loading/glsl/texture.frag.spv index e0bc54e6d3f68f7021ab59468a846e8ea641c36c..aa31f6ce4746fe309927250b7d7cb2525814487c 100644 GIT binary patch delta 284 zcmbOu&>=7(gVASWW-z0qBLf3NNoqw&X;G?keolT7kjuczz{cP+c`su-qt9knro)Ve z+6*i}AqEClAU=EMjI9U*6N4L&tpF7F2GYzx%nroXP`=M(MV85&ARY%$)N%7#mU53Z{|i)T0mJ|jV