weird shader problem

i am making a 2D topDown shooter and i want to make the enemy ragDolls fade out after awhile so i tried setting up a transparent shader a made a script but when i look at the material in the model it looks really weird. in some places you can see through it. here is a image Image

Rendering transparent objects (or to be more precise, any objects using a "Transparent ..." shader, even if their alpha is 1) requires depth-sorting all transparent objects and rendering them back-to-front. This is necessary, since an object that needs to be rendered "behind" an already rendered half-transparent object doesn't know about how much of it can be seen through that object (i.e., the alpha value of the already rendered in-front object). Rendering them back-to-front instead gives the occluding object full control over how much of the already rendered pixel value is being overwritten, according to the occluder's alpha value.

In addition, z-buffer writing (not reading) is usually disabled while rendering transparent objects, for the same reason - if a half-transparent object would write its depth value, and another half-transparent objects lying behind it is rendered afterwards (because depth sorting failed, see below), it would not be rendered at all due to the z-test.

Depth-sorting is far from perfect, since it is not done on a polygonal basis (which would be extremely expensive, and might involve constant material changes in the pipeline), but on a mesh basis. So it will fail if the mesh is non-convex, or if different meshes penetrate each other. Usually, a mesh's boundingbox center is used as the sort criteria.

I assume the head you rendered uses a single mesh wherever the transparent material is in use. That mesh is non-convex, and there is no depth-sorting performed within meshes. So depending on the view, some parts of the mesh will be rendered over parts of the mesh that are actually in front, and will generate rendering errors. The only way to fix this would be to break up that mesh into smaller, ideally convex and non-penetrating parts.

You could also experiment with an additive/multiplicative shader (in the Particles section), which will work for transparent objects using the same material (hence its usefulness for particles), but you'll have little control over the range of alpha values you can use (i.e., it will still be "transparent" even if the alpha is set to 1).

You could also try to modify the standard transparency shader to enable z-writing, and purely rely on depth-sorting. This will help you with your particular problem, but will introduce other rendering artifacts at other points/situations (e.g., the "missing objects in-behind", mentioned above). For example, in your case the bright part of the horn in the back will become fully invisible, even if your material's alpha is <1, so you'll never see that part of the horn through the half-transparent head.


Further possibilities are:

  1. renderQueue: if we are talking about different meshes (i.e., not within a single mesh), you can control the render order of these objects (or more precisely: of each material) using the shader/material property “renderQueue”, which can be set via script. This will group all objects with the same queue and render them from low to high. (for example, opaque objects use 2000 as default, transparent objects use 3000, and so on. Disadvantage: each unique renderQueue value means an additional render pass/drawcall.

  2. Z-pre-pass: you can modify your shader code to create an adittional first pass which does nothing except rendering the object(s) exclusively in the Z-buffer. See Advantage: transparent objects use z-Buffer information, and for complex meshes (which are unsortable) you can get smooth transitions especially when fading/switching between transparent and opaque. Disadvantages: You can no longer look “inside” semi-transparent objects, and other transparent objects behind the one you want to render will be invisible. Also, as it is an additional pass, it might be more expenstive (depending heavily on the shape of your objects).

See also here:

Probably the textures you are using have an alpha channel and its value is 0, so it gets rendered as transparent...

Try this shader