【发布时间】:2014-10-30 06:49:18
【问题描述】:
我正在开发一个使用 SharpDX 和 SharpDX 工具包绘制简单 3D 形状的应用程序,Geometrics.Desktop 示例对入门非常有帮助。现在我正在尝试让一些形状透明,为了简单起见,我只是想让该示例中的茶壶模型看起来透明(也许半透明会更精确)。
对于那些不熟悉 Geometrics.Desktop 示例的人,它会在 3D 中绘制一些简单的原始几何形状。作为一个示例应用程序,它非常简单,因此它是一个很好的暂存器,可用于试验 SharpDX“工具包”库的 3D 功能。
我已将此添加到 LoadContent 方法(在启动时运行一次),以准备 Direct3D 进行混合:
var blendStateDescription = new BlendStateDescription();
blendStateDescription.AlphaToCoverageEnable = false;
blendStateDescription.RenderTarget[0].IsBlendEnabled = true;
blendStateDescription.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
blendStateDescription.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha;
blendStateDescription.RenderTarget[0].BlendOperation = BlendOperation.Add;
blendStateDescription.RenderTarget[0].SourceAlphaBlend = BlendOption.Zero;
blendStateDescription.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
blendStateDescription.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
blendStateDescription.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
var blendState = SharpDX.Toolkit.Graphics.BlendState.New(this.GraphicsDevice, blendStateDescription);
this.GraphicsDevice.SetBlendState(blendState);
我已将深度模板设置如下:
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.
var depthDisabledStencilState = SharpDX.Toolkit.Graphics.DepthStencilState.New(this.GraphicsDevice, depthDisabledStencilDesc);
//turn z-buffer off
this.GraphicsDevice.SetDepthStencilState(depthDisabledStencilState, 1);
在 Draw 方法中(以帧速率运行并迭代一小部分 3D 模型,绘制每个模型),我添加了下面的代码。它将茶壶移动到视口的中心并使其大到足以遮挡一些其他对象。茶壶是最后画的,所以其他模型已经渲染好了,所以我真的希望这会在它们上面画一个半透明的茶壶:
// Draw the primitive using BasicEffect
if (i == 6)
{
basicEffect.Alpha = 0.5f;
basicEffect.World *= Matrix.Translation(-x, -y, 0);
basicEffect.World *= Matrix.Scaling(3);
}
else
{
basicEffect.Alpha = 1;
}
primitive.Draw(basicEffect);
(注释和对 Draw 的最终调用是原始示例代码的一部分。)
但是很可惜,结果是在其他物体中出现了一个大不透明茶壶:
我需要怎么做才能让那个茶壶变得透明?
是我遗漏了什么,还是我错误地配置了混合状态?
为了完整起见,我还尝试使用 Alpha 通道为 50% 的图像对我的对象进行纹理处理,但这仍然会导致对象不透明。
提前感谢您的帮助!
【问题讨论】:
-
你使用的是direct3d11吗?因为没有primitive,只有context。
-
是的,我使用的是 D3D11,但我使用的是几何意义上的“原始”一词 - 立方体、球体、圆柱体等。 SharpDX 工具包中的类可以轻松绘制此类形状,并且易于使用。大多。让它们变得透明非常困难。
标签: c# direct3d sharpdx direct3d11