- Home /
Why do my tree leafs(transparent diffuse textures) take over the color of the background?
Hello,
I made some trees in blender using a basic mesh for the tree trunk and planes with transparent/diffuse materials on them for the leaves. the image is saved in .png .
but when it is getting evening in my game the sunset starts to come(I have made that by using a big orange sphere with 70% transparency, it is not that the leaves start to get behind it because the trunk is still visible ) and the background changes to orange but my leaves also start to become orange when they are far away. when I walk to them they switch back to their original color.
how do i solve this? here is an image: http://tinypic.com/r/54jeau/5
You can see in the background on the right a tree with this problem
From my experience, transparency in unity is a little bit funky. Sometimes it seems like a transparent object shows up in front of everything, regardless of depth. $$anonymous$$y guess is that's what you're running into
Never$$anonymous$$d, I just tested it and that's not a problem anymore. I don't know why I thought that...
Answer by gameplay4all · Dec 26, 2014 at 01:28 PM
I don't know if people are still visiting this question, but I'm cleaning up my profile so I'm answering my own questions.
The problem just lies with the build-in transparent shader, so I used the cut-out shader and all was looking fine :)
Answer by Bunny83 · Jun 02, 2019 at 06:15 PM
Since this question already got bumped I'll post an actual explanation what happens. The issue is the rendering order of objects and that the leaves are drawn "behind" your orange sphere.
The way geometry is rendered has to be seperated into two main categories:
opaque geometry
transparent geometry
Unity first draws all opaque geometry and usually sorts the objects front-to-back. The front to back ordering minimizes overdraw since a big object close to the camera most likely covers a large portion of the screen. So things drawn further away will fail quickly thanks to the depth buffer.
Transparent geometry is where it gets really tricky. This OpenGL wiki article is always a good start. We need to draw transparent objects back-to-front in order to correctly blend the colors. For this purpose Unity sorts all transparent objects back to front. However it does not sort individual triangles of the objects but always the objects as a whole. The depth sorting is done based on the objects pivot point. So an object that has a pivot further away from the camera is drawn first and those objects which are closer to the camera are drawn last.
As you can read in the article there is no general solution for the sorting issue since even when you would sort individual triangles, 3 triangles could overlap each other in a circular fashion so it's impossible to sort them in a meaningful order. The only (correct) solution would be to split at least one of the triangles into two parts. So the now 4 parts can be sorted correctly as they are no longer circular dependent.
What happened in your case specifically is that your orange sky dome sphere has its pivot more or less in the center of your scene. So if you (the camera) is close to the center, the sphere is "closer" to the camera than the leaves of the tree, even when all the triangles of the sphere are actually much further away. Therefore the leaves are drawn before your sphere and therefore appear behind the sphere. As soon as you get closer to the trees (and further away from the sphere center) the order will switch around and the leaves are drawn correctly after the sphere since now the sphere is further away than the leaves.
Cutout shaders are not transparent shaders. They are actually opaque shaders and are rendered during the opaque pass. Since cutout shaders do not need any kind of blending it's perfectly fine to draw them front to back as the areas that are "cut out" simply aren't written at all into the render buffers.
If you need actual transparency when using such a sky dome, you just have to make sure the sky dome is always rendered before your other transparent objects. This can be done by adjusting the render queue, either of the material of your sphere directly or inside the used shader. If you do
Tags { "Queue" = "Transparent-1" }
in the sphere's shader it will always be rendered before all other transparent shaders and therefore your leaves would appear on top of it.
If you want to set the materials renderqueue from outside the shader, you should know that the transparent queue is equivalent to a value of 3000. So assigning 2999 to the sphere material renderqueue would have the same effect and ensures it's rendered before any other transparent shaders (at least as long as they don't force their own render queue to be even lower).
Your answer
Follow this Question
Related Questions
Adjust the scale of leaves at Runtime 0 Answers
How can i get background tranparency ? 2 Answers
Linear color space give tree leaves a white shine. 0 Answers
White trees in a dark scene 3 Answers
Tree Creator Shader will not fade 0 Answers