- Home /
Object alpha blending
I want to fade trees out when close to the camera, but the transparency shaders have problems with sorting and don't cash shadows. I don't want to display the faces "that are only visible because of transparency".
Is there a way (preferably higher level than shader) that allows to "hide" the object using an alpha value instead of making it transparent?
To illustrate - a normal tree with bumped diffuse shader (obviously no sorting problems):
The same tree with shader changed to Transparent/Bumped Diffuse. Alpha set to 1, but there are some weird sorting issues. It is important, because I would like to make the transparency gradual, but change of shaders is really noticable and weird.
This is how I would imagine it - it is the exact same as normal Bumped Diffuse shader, but in the end the object is simply "overlaid" with some alpha value, so that the background is visible. The shadows and all should remain as if the object was 100% opaque.
I guess the problems would arise when there are several trees overlapping, but is this at all possible?
Answer by FairGamesProductions · Nov 06, 2014 at 10:02 PM
I suggest you look into Occlusion if you have Unity Pro. If you don't, I think it may be better to do via script. Have a script attached to the tree that checks the distance to the player. If the distance is lover than,whatever you need, then turn on the renderer. Example JS:
var VisualDistance = 10.0; //the distance where the tree is no longer visible
private var Player : GameObject;
function Start ()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
function Update ()
{
if (Vector3.Distance(Player.transform.position, gameObject.transform.position) < VisualDistance && !gameObject.renderer.enabled)
{
gameObject.renderer.enabled = true;
}
else if (Vector3.Distance(Player.transform.position, gameObject.transform.position) > VisualDistance && gameObject.renderer.enabled)
{
gameObject.renderer.enabled = false;
}
}
The reason to do the conditions like that, is so that the renderer.enabled only changes once when the distance threshold is passed in either direction, and not every frame. If your trees have colliders, make sure you also enable/disable them too.
I wish the trees to be transparent, not dissapear. It is a topdown camera and the trees get in the way sometimes.
But the transparency shaders sort polygons in an odd way (see picture 2).
That makes it a little more complex. I recommend you switch out the material when needed:
var Trans$$anonymous$$aterial : $$anonymous$$aterial;
var FadeSpeed = 1.0;
private var normal$$anonymous$$aterial : $$anonymous$$aterial;
private var LerpAplhaDown = false;
private var counter = 0.0;
function Start ()
{
normal$$anonymous$$aterial = gameObject.renderer.material;
}
function Update ()
{
if (Vector3.Distance($$anonymous$$ainCamera.transform.position, gameObject.transform.position) < 1.0 && gameObject.renderer.material != Trans$$anonymous$$aterial)
{
gameObject.renderer.material = Trans$$anonymous$$aterial;
LerpAplhaDown = true;
` ` counter = 0.0;
}
else if (Vector3.Distance($$anonymous$$ainCamera.transform.position, gameObject.transform.position) > 1.0 && gameObject.renderer.material == Trans$$anonymous$$aterial)
{
gameObject.renderer.material = normal$$anonymous$$aterial;
}
//------FADE----------------------
if (LerpAplhaDown && counter < 1.0)
{
gameObject.renderer.material.color.a = $$anonymous$$athf.Lerp(1.0, 0.5, counter);
counter += Time.deltaTime * FadeSpeed;
}
else if (counter >= 1.0)
{
LerpAplhaDown = false;
}
}
You can switch out the conditions of the if statements to whatever you may need. $$anonymous$$aybe mouse hover Over, or something like that. And the lerp is so that the material fades out. You can add the other direction too, just switch the 1.0 and the 0.5 in the Lerp. With this method the polygon order will be less noticeable.
Alternatively, you can split the tree mesh into a lower part and top, and set a higher render queue to the top part so the top is always rendered over the bottom(CS):
using UnityEngine;
[AddComponent$$anonymous$$enu("Rendering/SetRenderQueue")]
public class SetRenderQueue : $$anonymous$$onoBehaviour {
[SerializeField]
protected int[] m_queues = new int[]{3000};
protected void Awake() {
$$anonymous$$aterial[] materials = renderer.materials;
for (int i = 0; i < materials.Length && i < m_queues.Length; ++i) {
materials[i].renderQueue = m_queues[i];
}
}
}
The issue was that the built in transparency shaders act odd, but thanks for the effort. I solved it by just getting a nicer transparency shader from asset store. Still not what I had in $$anonymous$$d, but atleast looks less weird...
Your answer
Follow this Question
Related Questions
Mesh transparency halfway cutoff script 0 Answers
Need help with opacity error 0 Answers
Transparent brush overlap 'dotting' effect problem 1 Answer
Transparency with correct culling 2 Answers
Change blend mode from outside shader? 3 Answers