Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by cassius · Dec 03, 2011 at 04:55 AM · cameraobjecttransparencyfade

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[i].mainTexture;    
                 newMaterial.color = info.originalMaterials[i].color;
                 newMaterial.color.a = 1.0;
                 info.alphaMaterials[i] = newMaterial;
             }

             hitRenderer.sharedMaterials = info.alphaMaterials;
             fadedOutObjects.Add(info);
         }
         // Just mark the renderer as needing fade out
         else
         {
             info.needFadeOut = true;
         }
     }
 }

 // Now go over all renderers and do the actual fading!
 var fadeDelta = fadeSpeed * Time.deltaTime;
 for (i=0;i<fadedOutObjects.Count;i++)
 {
     var fade = fadedOutObjects[i];
     // Fade out up to minimum alpha value
     if (fade.needFadeOut)
     {
         for (var alphaMaterial : Material in fade.alphaMaterials)
         {
             var alpha = alphaMaterial.color.a;
             alpha -= fadeDelta;
             alpha = Mathf.Max(alpha, fadedOutAlpha);
             alphaMaterial.color.a = alpha;
         }
     }

     // Fade back in
     else
     {
         var totallyFadedIn = 0;
         for (var alphaMaterial : Material in fade.alphaMaterials)
         {
             alpha = alphaMaterial.color.a;
             alpha += fadeDelta;
             alpha = Mathf.Min(alpha, 1.0);
             alphaMaterial.color.a = alpha;
             if (alpha >= 0.99)
                 totallyFadedIn++;
         }


         // All alpha materials are faded back to 100%
         // Thus we can switch back to the original materials
         if (totallyFadedIn == fade.alphaMaterials.length)
         {
             if (fade.renderer)
                 fade.renderer.sharedMaterials = fade.originalMaterials;

             for (var newMaterial in fade.alphaMaterials)
                 Destroy(newMaterial);
             fadedOutObjects.RemoveAt(i);
             i--;
         }
     }
 }

}

@script AddComponentMenu ("Third Person Camera/Fadeout Line of Sight")

Thanks for any help.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Geko_X · Dec 15, 2011 at 06:44 AM

There's a solution here on the forums. 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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Dec 15, 2011 at 07:07 AM 0
Share

Great tip, Geko ! Cheers

avatar image Geko_X · Dec 15, 2011 at 07:15 AM 0
Share

No problem!

avatar image
0

Answer by cassius · Dec 03, 2011 at 06:23 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Transparency cone between object and camera 1 Answer

Fading grass? 0 Answers

How to make an object always in front of the OVRPlayerController camera? 1 Answer

Click a cube to move player over to that position & rotate player to face fixed point 1 Answer

How to get distance between camera and object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges