【问题标题】:wgpu doesn't render with alpha channelswgpu 不使用 Alpha 通道进行渲染
【发布时间】:2022-06-20 13:18:47
【问题描述】:

我正在尝试使用 wgpu 渲染半透明的 png 图像。我有一个问题,一些不应该显示的像素正在显示。例如,在我的图像左侧有一个灰色 (128, 128, 128) 块,其 alpha 值为 0,这意味着它应该是不可见的,对吗?我已将纹理格式设置为 Rgba8UnormSrgb:

let texture = device.create_texture(
    &wgpu::TextureDescriptor {
        label,
        size,
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format: wgpu::TextureFormat::Rgba8UnormSrgb,
        usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::RENDER_ATTACHMENT,
    }
);

这是 png 图像和等效的 jpg 图像,这是 wgpu 正在渲染的内容。

https://user-images.githubusercontent.com/99501993/169669616-f3386235-73d3-45b8-9415-003c480f686f.png
https://user-images.githubusercontent.com/99501993/169669627-cdabd091-6886-43cd-b6f5-2c110d7b44d1.png

【问题讨论】:

    标签: rust wgpu-rs


    【解决方案1】:

    得到了答案。这是由于 BlendState 未设置为 ALPHA_BLENDING

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
    【解决方案2】:

    更改片段着色器的混合模式为我解决了这个问题。

    根据docs,实现了3种混合状态:

    1. 替换
    2. ALPHA_BLENDING
    3. PREMULTIPLIED_ALPHA_BLENDING

    这是一个例子:

    fragment: Some(wgpu::FragmentState {
            module: &draw_signal_shader,
            entry_point: "fs_main",
            targets: &[
                wgpu::ColorTargetState{
                    format: surface.get_preferred_format(&adapter).unwrap().into(),
    
                    blend: Some(wgpu::BlendState{
                        color: wgpu::BlendComponent{
                            src_factor: wgpu::BlendFactor::SrcAlpha,
                            dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
                            operation: wgpu::BlendOperation::Add,},
                        alpha: wgpu::BlendComponent::OVER
                    }),
    
                    write_mask: wgpu::ColorWrites::ALL,
                }
            ],
        }),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2013-12-17
      • 2019-07-04
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多