在sharpdx中实现透明对象的最简单方法是什么?

问题描述 投票:0回答:1

我目前正试图在锐利的dx中实现半透明的多边形。

目前我使用GraphicsDevice和BasicEffect来绘制对象。

// Setup the vertices
game.GraphicsDevice.SetVertexBuffer(myModel.vertices);
game.GraphicsDevice.SetVertexInputLayout(myModel.inputLayout);

// Apply the basic effect technique and draw the object
basicEffect.CurrentTechnique.Passes[0].Apply();
game.GraphicsDevice.Draw(PrimitiveType.TriangleList, myModel.vertices.ElementCount);

这对于普通的对象来说是很好的,但是我想让一些对象部分透明。我已经将这些对象的颜色的alpha值设置为50,但是它们仍然被渲染为不透明。我需要怎么做才能达到这个效果?

graphics transparency sharpdx
1个回答
0
投票

在Sharpdx中,透明度要求浮动颜色的alpha混合值为0...1。上面Nico Schertler提供的评论解决了这个问题,可以看作是答案。

在没有Alpha模式的情况下,有两个选项可以在HLSL着色器文件中使用。

  • 在Pixel着色器中,使用clip()函数,取决于输入颜色。你可以定义你的透明黑色和 不显示 任何黑色三角形。像这样。

    float4 PS( PS_IN input ) : SV_Target { clip(input.color[3] < 0.1f ? -1:1 ); return input.color; }

参考: https:/docs.microsoft.comen-uswindowswin32direct3dhlsldx-graphics-hlsl-clip。

查看效果。

enter image description here

  • 修改顶点着色器,将这些顶点投射到(0,0,0),取决于输入颜色。你可以定义你的透明黑色和 不显示 任何黑色三角形。像这样。

    PS_IN VS( VS_IN input) { PS_IN output = (PS_IN)0; if ((input.color[0]!=0)||(input.color[1]!=0)||(input.color[2]!=0)) { output.position = mul(worldViewProj,input.position); } output.color = input.color; return output; }

请看下面我的HeightField网格边缘的效果 左边是没有变化的版本... ...

https://i.ibb.co/GC7wwSp/Cheap-Transparent.jpg

: 后一种解决方案可以使边缘更清晰,但只有当(0,0,0)在物体后面时才有效。

© www.soinside.com 2019 - 2024. All rights reserved.