2014-10-30 214 views
5

我正在使用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); 

(注释和最终呼叫画出了一部分原来的示例代码)

但很可惜,结果是其他物体之间的大不透明茶壶:

enter image description here

什么我需要做的,使该TE apot透明?

我错过了什么,或者我错误地配置了混合状态?

为了完整起见,我还尝试使用具有50%alpha通道的图像纹理我的对象,但仍然会产生不透明的对象。

在此先感谢您的帮助!

+0

您使用的是direct3d11吗?因为没有*原始*只是*上下文*。 – 2014-11-04 20:49:34

+0

是的,我使用的是D3D11,但是我在几何意义上使用了单词“primitive” - 立方体,球体,圆柱体等等。 SharpDX工具包的类可以很容易地绘制这种形状,并且它们很容易处理。大多。让它们透明是非常困难的。 – NSFW 2014-11-05 05:12:40

回答

2

我有SourceAlphaBlend和DestinationAlphaBlend设置为零,它的工作原理应该如此。除了那些状态描述对我来说看起来是正确的。此外,请确保AlphaToCoverage在blendstate中处于禁用状态,否则您将会遇到其他异常情况。

另外请确保您在绘制每个框架之前设置BlendState。

+0

您确定这些是您所做的唯一更改吗?我只是对我的测试应用进行了这些更改,而茶壶仍然不透明。尽管你有alpha工作,但给我很大的希望! – NSFW 2014-11-07 05:19:32

+1

您是否使用Visual Studio?如果是这样,请使用“调试 - >图形 - >启动诊断”工具来调试D3D11设备并在运行时进行声明。也许一个国家没有正确地接受约束。 – 2014-11-07 16:29:33

+0

我有一个透明的茶壶!出现在诊断工具中的混合状态与我在代码中配置的混合状态不同。我将其移入渲染循环并且透明度现在可用。当然,我想将它从渲染循环中移出,但我会继续研究它。 – NSFW 2014-11-08 02:22:15