我正在使用SharpDX和SharpDX Toolkit绘制简单的3D形状的应用程序,而Geometrics.Desktop示例在入门中非常有用。现在,我正在尝试使某些形状透明,并且保持简单,我只是试图使该示例中的茶壶模型显得透明(可能半透明更精确)。如何使用SharpDX工具包绘制透明的3D对象?
对于那些不熟悉Geometrics.Desktop示例的人来说,它会在3D中绘制几个简单的原始几何图形。作为一个示例应用程序,它非常简单,因此它是一个相当不错的便签本,用于试验SharpDX“Toolkit”库的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);
(注释和最终呼叫画出了一部分原来的示例代码)
但很可惜,结果是其他物体之间的大不透明茶壶:
什么我需要做的,使该TE apot透明?
我错过了什么,或者我错误地配置了混合状态?
为了完整起见,我还尝试使用具有50%alpha通道的图像纹理我的对象,但仍然会产生不透明的对象。
在此先感谢您的帮助!
您使用的是direct3d11吗?因为没有*原始*只是*上下文*。 – 2014-11-04 20:49:34
是的,我使用的是D3D11,但是我在几何意义上使用了单词“primitive” - 立方体,球体,圆柱体等等。 SharpDX工具包的类可以很容易地绘制这种形状,并且它们很容易处理。大多。让它们透明是非常困难的。 – NSFW 2014-11-05 05:12:40