本文介绍了透明着色器允许以下对象显示在顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的场景中,将笑脸(带有png图像的四边形)放置在Y:0处,将点(带有3X3拼贴的四边形)放置在Y:-0.25处.我需要用于笑脸的着色器是透明-漫反射",因为我正在使用圆形png图像.

但是我在下面使用的圆点显示在笑脸上方.使用其他任何着色器(例如漫反射")可以解决此问题,但笑脸会变成正方形.

截屏:

如果您需要更多说明,请随时询问.

我已附加了检查器面板中的笑脸和点的着色器详细信息.链接: http://postimg.org/image/cvws1os7d/

我发现问题应该出在MainCamera,尤其是距离& 视野".我需要使用透视"作为投影类型,并使用140作为视场".

如果我将投影类型更改为正交",则此问题已完全解决.

下面的屏幕截图显示了距离和视野如何控制笑脸上点的外观.

屏幕截图1:Y位置:8.48视场角:30链接: http://postimg.org/image/s31tttrkp/

屏幕截图2:Y位置:9.7视场角:30链接: http://postimg.org/image/f71sq0y4b/

屏幕截图3:Y位置:11.41视场角:30链接: http://postimg.org/image/3uk4az3d3/

截屏4:Y位置:1视场:140链接: http://postimg.org/image/bul9zwg7z/

这是一个线索吗?

解决方案

仅提供一些有关通常如何实现透明性的信息(不仅限于Unity).

同时,可以按任何顺序绘制不透明对象(即使以从前到后的顺序对其进行排序,最终也可以依靠早期的z-cull来改善某些GPU的性能).可以使用存储在 z缓冲区中的深度值来推断哪些像素可见./p>

您不能依靠z-buffer来提高透明度.为了渲染半透明的对象,典型的方法是在所有不透明的对象之后绘制它们,并以从后到前的顺序对其进行排序(首先绘制距离摄像机更远的透明对象).

现在的问题是:如何对对象进行排序?使用透视相机和通用形状的网格,解决方案并不明显.

对于平行于地形摄影机视图平面定向的四边形网格,z顺序隐式正确(这就是为什么它总是对您有用的原因).您还会注意到摄影机位置会影响绘图顺序,因为对于透视摄影机,该顺序是根据对象位置与摄影机之间的距离计算的.

那么在您的特定用例场景中,您可以使用Unity3d做什么?一些技巧:

  • 明确设置材料的渲染队列
  • 在着色器中明确设置渲染顺序(与上述类似,但等于具有相同着色器的每个对象)
  • 使用 Offset 将深度伪装到着色器中(在您的着色器中没有用案例,但值得一听)

希望这会有所帮助


编辑

我不知道,相机透明度排序模式似乎可以是可定制的.因此,这是另一种解决方案,如果您想使用透视相机,这可能是最适合您的情况.

In my scene, the smileys(Quad with png image) are placed at Y:0 and the dots(Quad with tiling 3X3) are placed at Y: -0.25.The shader I need to use for the smileys is Transparent-Diffuse as I am using a circle png image.

But the dots I use below are showing up above the smiley. Using any other shader like Diffuse solves the issue but the smiley becomes a square.

Screenshot:

If you need any more clarifications please dont hesitate to ask.

Edit:I have attached the shader details of both the smiley and the dots from the inspector panel.link: http://postimg.org/image/cvws1os7d/

Edit 2:I have found that the issue should be with the MainCamera and especially with distance & "Field Of View".I need to use "Perspective" as projection type and 140 as Field of View.

If I change the projection type to Orthographic the issue is completely fixed.

The below screenshots show how the distance and Field of View controls the appearance of the dots over the smiley.

Screenshot 1:Y position: 8.48Field of View: 30link: http://postimg.org/image/s31tttrkp/

Screenshot 2:Y position: 9.7Field of View: 30link: http://postimg.org/image/f71sq0y4b/

Screenshot 3:Y position: 11.41Field of View: 30link: http://postimg.org/image/3uk4az3d3/

Screenshot 4:Y position: 1Field of View: 140link: http://postimg.org/image/bul9zwg7z/

Can this be a clue?

解决方案

Just a couple of info, on how transparency is typically implemented (not only by Unity).

Meanwhile opaque objects can be drawn in any order (even if sorting them in front-to-back order can eventually improve some GPU performances relying on an early z-cull). Which pixels are visible can be deduced using the depth value stored into the z-buffer.

You can't rely on z-buffer for transparency.For rendering translucent objects a typical approach is to draw them after all opaque objects, and sorting them in back-to-front order (transparent objects more distant from the camera are drawn first).

Now the question is: how do you sort objects? with a perspective camera and meshes of a generic shape, the solution is not obvious.

For quad meshes oriented parallel to a ortographic camera view plane, the z order is implicitly correct (that's why it always works for you).You can also notice that camera position influences the drawing order, because with perspective camera the order is calculated as distance between object position and camera.

So what can you do with Unity3d, in your specific use case scenario?A couple of tricks:

  • Explicitly set the render queue of the material
  • Explicitly set the render order inside the shader (similar of the above, but equals to every object with the same shader)
  • Fake the depth using Offset into the shader (not that useful in your case but worth to be known)

hope this helps


EDIT

I didn't know that, the camera transparency sorting mode appears to be customizable. So this is another solution, maybe the best for your case if you want to use a perspective camera.

这篇关于透明着色器允许以下对象显示在顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:51
查看更多