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
0
Question by PorkMuncher · Nov 06, 2014 at 09:21 PM · transparencyalphablendingopacity

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):

alt text

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. alt text

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. alt text

I guess the problems would arise when there are several trees overlapping, but is this at all possible?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · 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 PorkMuncher · Nov 06, 2014 at 10:10 PM 0
Share

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).

avatar image FairGamesProductions · Nov 06, 2014 at 10:51 PM 0
Share

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];
         }
     }
 }

avatar image PorkMuncher · Nov 07, 2014 at 10:39 AM 0
Share

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

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

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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