• 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 awplays49 · May 17, 2015 at 12:13 AM · transparent

Material that can fade from opaque to transparent?

How can i make a shader that supports both opaque color and transparent color? I tried a lot of things such as:

Legacy shaders (makes it only transparent)

Transparent setting (makes it only transparent)

Fade setting (same as transparent

Comment
Add comment · Show 2
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 awplays49 · May 17, 2015 at 01:46 AM 0
Share

I'll respond in the morning if I get anything. It's really late here.

avatar image IsaiahKelly · May 17, 2015 at 06:15 AM 0
Share

I'm not exactly sure what you want here. Transparency/alpha is separate from color value. So as long as the shader supports the alpha channel, you can make any color transparent or opaque depending on the alpha value you give it.

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by IsaiahKelly · May 17, 2015 at 07:13 AM

Any shader that supports the color alpha value can be either opaque or transparent. It just depends on the alpha value set. Here are two ways to fade a Material. The first method "AlphaFade()" just fades out the original color's alpha. While the "ColorFade()" method changes the entire color (including alpha), so you need to make sure the fade color's alpha is set to low or zero for a fade to happen with it.

Note: Material must support the alpha channel for this to work! e.g. Unity 5 standard shader must be set to Fade/Transparent. Legacy shader must be set to "Transparent".

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour 
 {
     [Range (0.1f, 1.0f)]
     public float fadeSpeed = 1f;    // How fast alpha value decreases.
     public Color fadeColor = new Color (0, 0, 0, 0);
 
     private Material m_Material;    // Used to store material reference.
     private Color m_Color;            // Used to store color reference.
 
 
     void Start ()
     {
         // Get reference to object's material.
         m_Material = GetComponent <Renderer> ().material;
 
         // Get material's starting color value.
         m_Color = m_Material.color;
 
         // Must use "StartCoroutine()" to execute 
         // methods with return type of "IEnumerator".
         StartCoroutine (ColorFade ());
     }
 
     // This method fades only the alpha.
     IEnumerator AlphaFade ()
     {
         // Alpha start value.
         float alpha = 1.0f;
 
         // Loop until aplha is below zero (completely invisalbe)
         while (alpha > 0.0f)
         {
             // Reduce alpha by fadeSpeed amount.
             alpha -= fadeSpeed * Time.deltaTime;
 
             // Create a new color using original color RGB values combined
             // with new alpha value. We have to do this because we can't 
             // change the alpha value of the original color directly.
             m_Material.color = new Color (m_Color.r, m_Color.g, m_Color.b, alpha);
 
             yield return null;
         }
     }
 
 
     // This method fades from original color to "fadeColor"
     IEnumerator ColorFade ()
     {
         // Lerp start value.
         float change = 0.0f;
         
         // Loop until lerp value is 1 (fully changed)
         while (change < 1.0f)
         {
             // Reduce change value by fadeSpeed amount.
             change += fadeSpeed * Time.deltaTime;
 
             m_Material.color = Color.Lerp (m_Color, fadeColor, change);
             
             yield return null;
         }
     }
 }
Comment
Add comment · Show 10 · 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 awplays49 · May 17, 2015 at 11:29 AM 0
Share

But what shader type can be both solid and transparent?

avatar image awplays49 · May 17, 2015 at 11:31 AM 0
Share

Why is my fade shader still transparent when the alpha is set to 255?

avatar image IsaiahKelly · May 17, 2015 at 08:40 PM 0
Share

You're title says "$$anonymous$$aterial that can fade from opaque to transparent?" and that's exactly what the code above does. I don't understand what you mean by "both solid and transparent". Are you trying to just make a portion of the material transparent?

Please let me know which version of Unity and shader you are using for this. Do you have any textures on it too?

avatar image awplays49 · May 17, 2015 at 09:40 PM 0
Share

The material isn't responding. I see what your code is, and it's pretty much the same as what I have. I am using standard shader, opaque and just plain grey.

avatar image awplays49 · May 17, 2015 at 09:41 PM 0
Share

I'm using unity 5

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Don't want game object seen when it goes past a wall 1 Answer

GUI.DrawTexture - Slightly Transparent? 1 Answer

Transparent game objects 4 Answers

Opaque specular on transparent object? (i.e. glass sphere) 0 Answers

water transparent 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