How to Make Part of Object Between Camera and Player Transparent

I’m trying to figure out how to make a large single object (my office building mesh) transparent when a part (like a wall for example) of the object is between the camera and the player… but only the area of the object between camera and player. I’ve found out how to make the entire object transparent when a part is between camera and player, but this isn’t what I’m looking for.

Is there a way to make just portions of a single object transparent? I’d rather not have to break up my building mesh into a bunch of separate objects if possible.

Here’s the code I’ve been using so far, which makes the entire object transparent.


/* Fades out any objects between the player and this transform.
   The renderers shader is first changed to be an Alpha/Diffuse, then alpha is faded out to fadedOutAlpha.
   Only objects 
   In order to catch all occluders, 5 rays are casted. occlusionRadius is the distance between them.
*/
var layerMask : LayerMask = 2;
var target : Transform;
var fadeSpeed = 1.0;
var occlusionRadius = .3;
var fadedOutAlpha = 0.3;

private var fadedOutObjects = Array ();

class FadeoutLOSInfo
{
	var renderer : Renderer;
	var originalMaterials : Material[];
	var alphaMaterials : Material[];
	var needFadeOut = true;
}

function FindLosInfo (r : Renderer) : FadeoutLOSInfo
{
	for (var fade : FadeoutLOSInfo in fadedOutObjects)
	{
		if (r == fade.renderer)
			return fade;
	}
	return null;
}

function LateUpdate () {
	var from = transform.position;
	var to = target.position;
	var castDistance = Vector3.Distance(to, from);

	// Mark all objects as not needing fade out
	for (var fade : FadeoutLOSInfo in fadedOutObjects)
	{
		fade.needFadeOut = false;
	}

	var offsets = [Vector3(0, 0, 0), Vector3(0, occlusionRadius, 0), Vector3(0, -occlusionRadius, 0), Vector3(occlusionRadius, 0, 0), Vector3(-occlusionRadius, 0, 0)];

	// We cast 5 rays to really make sure even occluders that are partly occluding the player are faded out
	for (var offset in offsets)
	{
		var relativeOffset = transform.TransformDirection(offset);
		// Find all blocking objects which we want to hide
		var hits : RaycastHit[] = Physics.RaycastAll(from + relativeOffset, to - from, castDistance, layerMask.value);
		for (var hit : RaycastHit in hits)
		{
			// Make sure we have a renderer
			var hitRenderer : Renderer = hit.collider.renderer;		
			if (hitRenderer == null || !hitRenderer.enabled)
				continue;

			var info = FindLosInfo(hitRenderer);

			// We are not fading this renderer already, so insert into faded objects map
			if (info == null)
			{
				info = new FadeoutLOSInfo ();
				info.originalMaterials = hitRenderer.sharedMaterials;
				info.alphaMaterials = new Material[info.originalMaterials.length];
				info.renderer = hitRenderer;
				for (var i=0;i<info.originalMaterials.length;i++)
				{
					var newMaterial = new Material (Shader.Find("Alpha/Diffuse"));
					newMaterial.mainTexture = info.originalMaterials*.mainTexture;	*
_					newMaterial.color = info.originalMaterials*.color;*_
_*					newMaterial.color.a = 1.0;*_
<em>_					info.alphaMaterials *= newMaterial;*_</em>
<em>_*				}*_</em>
<em>_*				hitRenderer.sharedMaterials = info.alphaMaterials;*_</em>
<em>_*				fadedOutObjects.Add(info);*_</em>
<em>_*			}*_</em>
<em>_*			// Just mark the renderer as needing fade out*_</em>
<em>_*			else*_</em>
<em>_*			{*_</em>
<em>_*				info.needFadeOut = true;*_</em>
<em>_*			}*_</em>
<em>_*		}*_</em>
<em>_*	}*_</em>
<em>_*	// Now go over all renderers and do the actual fading!*_</em>
<em><em>_	var fadeDelta = fadeSpeed * Time.deltaTime;_</em></em>
<em>_*	for (i=0;i<fadedOutObjects.Count;i++)*_</em>
<em>_*	{*_</em>
<em><em>_		var fade = fadedOutObjects*;*_</em></em>
<em><em>_*		// Fade out up to minimum alpha value*_</em></em>
<em><em>_*		if (fade.needFadeOut)*_</em></em>
<em><em>_*		{*_</em></em>
<em><em>_*			for (var alphaMaterial : Material in fade.alphaMaterials)*_</em></em>
<em><em>_*			{*_</em></em>
<em><em>_*				var alpha = alphaMaterial.color.a;*_</em></em>
<em><em>_*				alpha -= fadeDelta;*_</em></em>
<em><em>_*				alpha = Mathf.Max(alpha, fadedOutAlpha);*_</em></em>
<em><em>_*				alphaMaterial.color.a = alpha;*_</em></em>
<em><em>_*			}*_</em></em>
<em><em>_*		}*_</em></em>
<em><em>_*		// Fade back in*_</em></em>
<em><em>_*		else*_</em></em>
<em><em>_*		{*_</em></em>
<em><em>_*			var totallyFadedIn = 0;*_</em></em>
<em><em>_*			for (var alphaMaterial : Material in fade.alphaMaterials)*_</em></em>
<em><em>_*			{*_</em></em>
<em><em>_*				alpha = alphaMaterial.color.a;*_</em></em>
<em><em>_*				alpha += fadeDelta;*_</em></em>
<em><em>_*				alpha = Mathf.Min(alpha, 1.0);*_</em></em>
<em><em>_*				alphaMaterial.color.a = alpha;*_</em></em>
<em><em>_*				if (alpha >= 0.99)*_</em></em>
<em><em>_*					totallyFadedIn++;*_</em></em>
<em><em>_*			}*_</em></em>
<em><em>_*			// All alpha materials are faded back to 100%*_</em></em>
<em><em>_*			// Thus we can switch back to the original materials*_</em></em>
<em><em>_*			if (totallyFadedIn == fade.alphaMaterials.length)*_</em></em>
<em><em>_*			{*_</em></em>
<em><em>_*				if (fade.renderer)*_</em></em>
<em><em>_*					fade.renderer.sharedMaterials = fade.originalMaterials;*_</em></em>
<em><em>_*				for (var newMaterial in fade.alphaMaterials)*_</em></em>
<em><em>_*					Destroy(newMaterial);*_</em></em>
<em><em>_*				fadedOutObjects.RemoveAt(i);*_</em></em>
<em><em>_*				i--;*_</em></em>
<em><em>_*			}*_</em></em>
<em><em>_*		}*_</em></em>
<em><em>_*	}*_</em></em>
<em><em>_*}*_</em></em>
<em><em>_*@script AddComponentMenu ("Third Person Camera/Fadeout Line of Sight")*_</em></em>
<em><em>_*```*_</em></em>
<em><em>_*Thanks for any help.*_</em></em>

There’s a solution here on the forums:

https://forum.unity.com/threads/projecting-a-hole.35808/

They use a projector to project a transparent image/shader onto a surface. I’m new to Unity myself, so I can’t really help anymore than to just give you the link!

Thanks or the replies, guys.

I had considered changing the texture on the fly, but decided not to pursue it as I think it would have been terribly messy, gave poor results, and was a bit of a hack to be honest.

The idea of using a shader and the viewport coords sounds possible, and I had thought of it. But I’ll be honest, I really have no idea how to make that sort of thing happen in scripting… or if I would also need to write my own shader.