Daevin
Daevin

Reputation: 901

Can I make all the contents of a GameObject transparent? (Unity3D 2017.3)

I'm making an ARCore app (that shouldn't matter, should it?) that has the model you're placing visible at the center of the screen before you place it. These models can be changed by the user.

The effect I'm trying to get is to have the model that you see before you place it 'ghosted'/translucent. What I would ideally like is having an empty GameObject (right now named GhostWrapper) that is a parent of the Ghost, and have everything inside GhostWrapper be translucent. This is because the Ghost model can change with user input, and the Ghost is then instantiated on the plane when the users wants to place it:

public void SetPlaceableModel(GameObject newModel)
{
    PlaceableModel = newModel;
    var pos = (Ghost != null) ? Ghost.transform.position : FirstPersonCamera.ScreenToWorldPoint(new Vector3(Screen.width / 2f, Screen.height / 2f, -100f));
    var rot = (Ghost != null) ? Ghost.transform.rotation : Quaternion.identity;
    Destroy(Ghost);
    Ghost = Instantiate(PlaceableModel, pos, rot, GhostWrapper.transform);
}

I haven't been able to get the GhostWrapper to work, so the below is what I've tried to do directly to the Ghost, but I would prefer if I can have, essentially, the GhostWrapper cause everything inside it to appear to be translucent (it doesn't have to force its children to be transparent, but make them look like it).

I have Google'd it quite a bit, and have tried the ways suggested in the top results but almost all seem to be a variation of "get the MeshRenderer and set its .material.color to a transparent", and that doesn't seem to be working. I've also tried setting the material on my model to use different shaders and setting the colour alpha way down, but none of that seems to be working (all the answers I've seen appear to be from 2009-2014, so maybe they're just out-of-date?). I've also tried what the Unity documentation says to no avail.

I've thought about having the GhostWrapper be an actual box model that contains the Ghost and manipulating it to make the items inside appear transparent, but since the models can be vastly different shapes/sizes, and the user can move close to them, they could just move inside the box and it would lose the effect.

Can anyone help me with this?

Note: the models I'm using have the Unit/Texture shader. I am very open to changing that if I can get it to work, but I have changed it to several others, including using Standard and changing the Render Mode to Fade and Transparent. Still no luck.

Upvotes: 0

Views: 2697

Answers (1)

Programmer
Programmer

Reputation: 125285

You cannot modify the transparency of the Unit/Texture shader because there is no property in that shader that allows you to do so. One way to do this with the Unit/Texture shader is to get the base texture property, modify it with one of the Texture2D.SetPixelXXX functions and set the alpha to whatever you want. This is slow and unnecessary. Go this route only if you must use the Unit/Texture shader.

Use the Standard shader for this. Below are the steps when using the Standard shader.

1.Change the material Shader to Standard

2.Change the material's Rendering Mode to Fade

You can also do this from code with:

void changeMaterialModeToFadeMode(Renderer rd)
{
    rd.material.SetFloat("_Mode", 2);
    rd.material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    rd.material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    rd.material.SetInt("_ZWrite", 0);
    rd.material.DisableKeyword("_ALPHATEST_ON");
    rd.material.EnableKeyword("_ALPHABLEND_ON");
    rd.material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    rd.material.renderQueue = 3000;
}

3.Finally, this is how to change the alpha:

GameObject ghost = ....;
Renderer ghostRenderer = ghost.GetComponent<Renderer>();

//Get current Color
Color meshColor = ghostRenderer.material.color;

//Set Alpha
const float alpha = 0.5f;
meshColor.a = alpha;

//Apply the new color to the material
ghostRenderer.material.color = meshColor;

Upvotes: 5

Related Questions