我正在渲染几个用几何着色器创建的立方体(我只加载位置和颜色),每个立方体都有不同的 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();
其他面以相同的方式创建。