c# - SharpDX/DirectDX : Transparency, 法线和渲染顺序

标签 c# directx alphablending sharpdx geometry-shader

我正在渲染几个用几何着色器创建的立方体(我只加载位置和颜色),每个立方体都有不同的 alpha 值。问题是,在启用 alpha 混合后,立方体的可见性会随着我观察它们的角度而变化。 alpha >0.5 的物体看起来非常坚固,当我稍微旋转它们时几乎消失了。

红色立方体的 alpha = 255,但我们可以看到它下面的内容。我试过启用和禁用模板缓冲区,但它没有做出任何改变,所以我猜它要么是我的着色器,要么是一些简单的东西,比如错误的标志值。

http://tinypic.com/r/10e589v/8

阿尔法混合

            var depthDisabledStencilDesc = new DepthStencilStateDescription()
        {
            IsDepthEnabled = false,
            DepthWriteMask = DepthWriteMask.All,
            DepthComparison = Comparison.Less,
            IsStencilEnabled = true,
            StencilReadMask = 0xFF,
            StencilWriteMask = 0xFF,
            // Stencil operation if pixel front-facing.
            FrontFace = new DepthStencilOperationDescription()
            {
                FailOperation = StencilOperation.Keep,
                DepthFailOperation = StencilOperation.Increment,
                PassOperation = StencilOperation.Keep,
                Comparison = Comparison.Always
            },
            // Stencil operation if pixel is back-facing.
            BackFace = new DepthStencilOperationDescription()
            {
                FailOperation = StencilOperation.Keep,
                DepthFailOperation = StencilOperation.Decrement,
                PassOperation = StencilOperation.Keep,
                Comparison = Comparison.Always
            }
        };

        // Create the depth stencil state.
        DepthDisabledStencilState = new DepthStencilState(Device, depthDisabledStencilDesc);
        //turn z-buffer off
        Device.ImmediateContext.OutputMerger.SetDepthStencilState(DepthDisabledStencilState, 1);

        #region Initialize Blending
        BlendStateDescription blendDesc = new BlendStateDescription();

        blendDesc.RenderTarget[0].IsBlendEnabled = true;
        blendDesc.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
        blendDesc.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha;
        blendDesc.RenderTarget[0].BlendOperation = BlendOperation.Add;
        blendDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.One;
        blendDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.InverseSourceAlpha;
        blendDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
        blendDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

        AlphaEnableBlendingState = new BlendState(Device, blendDesc);

        // Setup the blend factor.
        var blendFactor = new Color4(0, 0, 0, 0);

        Device.ImmediateContext.OutputMerger.SetBlendState(AlphaEnableBlendingState, blendFactor, -1);

几何着色器

    float4 v1 = input[0].Pos/2 + float4(-scale * cube_size, -scale * cube_size, -scale * cube_size, 0) + offset;
float4 v2 = input[0].Pos/2 + float4(-scale * cube_size, scale * cube_size, -scale * cube_size, 0) + offset;
float4 v3 = input[0].Pos/2 + float4(scale * cube_size, scale * cube_size, -scale * cube_size, 0) + offset;
float4 v4 = input[0].Pos/2 + float4(scale * cube_size, -scale * cube_size, -scale * cube_size, 0) + offset;

float4 v5 = input[0].Pos/2 + float4(-scale * cube_size, -scale * cube_size, scale * cube_size, 0) + offset;
float4 v6 = input[0].Pos/2 + float4(-scale * cube_size, scale * cube_size, scale * cube_size, 0) + offset;
float4 v7 = input[0].Pos/2 + float4(scale * cube_size, scale * cube_size, scale * cube_size, 0) + offset;
float4 v8 = input[0].Pos/2 + float4(scale * cube_size, -scale * cube_size, scale * cube_size, 0) + offset;

v1 = mul(v1, World);
v1 = mul(v1, View);
v1 = mul(v1, Projection);

v2 = mul(v2, World);
v2 = mul(v2, View);
v2 = mul(v2, Projection);

v3 = mul(v3, World);
v3 = mul(v3, View);
v3 = mul(v3, Projection);

//front
float4 edge1 = v3-v2;
float4 edge2 = v1-v3;
output.Normal = cross(edge1, edge2);

output.Pos = v3;    
OutputStream.Append(output);            
output.Pos = v2;
OutputStream.Append(output);            
output.Pos = v1;
OutputStream.Append(output);

output.Pos = v3;
OutputStream.Append(output);        
output.Pos = v4;
OutputStream.Append(output);        
output.Pos = v1;
OutputStream.Append(output);
OutputStream.RestartStrip();

其他人脸以同样的方式创建。

最佳答案

要与背景图元混合,必须首先渲染相同的背景图元,以便与现有像素颜色执行所需的混合操作。

Blending Stage

如果顺序颠倒,第二个图元将被第一个图元遮挡,您最终只会得到最后一个图元像素颜色。

希望它能帮助您更接近您的目标。

干杯

关于c# - SharpDX/DirectDX : Transparency, 法线和渲染顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22487749/

相关文章:

c# - 在第 1 类中制作一个 2d 列表,在第 2 类中填写并在第 3 类中使用

javascript - CefSharp - 获取 AJAX 请求的结果

c# - 在 ASP.NET 中创建 default.aspx.designer.cs 文件

c++ - DirectX大型平面 segmentation

c# - 叠加两个或多个位图以在图片框中显示 (C#)

c# - NHibernate关于多对多关系的一些属性的搜索

c# - 在 DirectX 中渲染环境

wpf - WPF d3d9.dll崩溃

c++ - 如何在 SDL 中进行硬件加速 Alpha 混合?

c# - 在 XNA 中对网格进行排序