- Home /
Make a complex object semi transparent without changing it
I have a race car that, at the end of the race, must become a ghost and be semi transparent. The object of the car is not determined, indeed the cars can change, can be made of many objects and meshes, textures and shaders. Looking for a solution for this problem I only found suggestions and scripts regarding changing the shaders of the object, but I think that this is quite complex, I was wondering if there is a way to set some filter to a camera or maybe make this object son of another object and inherit its transparency, some how. In other words, determine some sort of condition, under which ANY normal object acquires half transparency. Thank you.
Answer by JonS · Aug 19, 2015 at 09:26 PM
There's no simple way to apply transparency in the way you seem to want. Transparency is handled entirely by the shader on the materials that the model is using. If the material doesn't have and apply an Alpha value it can't be done.
That said, it's not too hard to loop through all the materials on a model and adjust the alpha at runtime. I typically use the following:
public class ModelFader : MonoBehaviour {
private Renderer[] mRenderers;
void Start() {
mRenderers = GetComponentsInChildren<Renderer>();
}
public void SetRendererAlphas(float alpha) {
for(int i = 0; i < mRenderers.Length; i++) {
for(int j = 0; j < mRenderers[i].materials.Length; j++) {
Color matColor = mRenderers[i].materials[j].color;
matColor.a = alpha;
mRenderers[i].materials[j].color = matColor;
}
}
}
}
One thing though is that this will generate an error if any of the materials on the model don't have a color value to change. You'll have to do a check to see if matColor is null.
Thank you. I believe I already tried something similar, but this won't work if the shader is set to Standard/Opaque, that unofrtunately is the standard, am I wrong? As soon as you are targeting shaders maybe it's possible to change the shader type runtime?
Correct, the Standard shader defaults to Opaque which completely ignores alpha and can't do transparency. If you're using the new 5.0 Standard shader there's a dropdown at the top for "Rendering $$anonymous$$ode". Setting that to "Fade" will allow you to fade out the material using the alpha value. There's also the "Transparent" setting but that's intended for materials like plastic or glass that still show reflections and lighting highlights even when fully transparent.
The easiest way to swap shaders at runtime if needed is to set up 2 different materials that use the shader setup you want for opaque and transparent. Then just swap the material on the renderer at runtime using the material property
Is there a way to set a camera to see only part of the objects and another camera to see some others, like layers for the collisions? This would be a very nice workaround... One camera would be dedicated to semi transparent objects that are invisible to the main camera... Then the second camera could be redirected to a raw image and the job is done.
Answer by TTTao · May 19, 2017 at 02:04 PM
one more thing, if the car use the build-in standard transparent shader, the car will be X-Ray like transparent ,which is very ugly.
To fix the xray effect, you'll need a 2-pass shader.. first pass renders just the z-buffer of the object once, with no color. Second pass renders the object transparent as you desire... but as it checks the z-buffer... only the front size is rendered, nothing on the back or behind the z-buffer is rendered. So you'll need to switch materials... when object is going to fade out, as mentioned above.
like this.... http://wiki.unity3d.com/index.php?title=AlphaVertexLitZ