• 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 janidsa · Aug 17, 2015 at 05:44 PM · alphabug-perhapscutoutcutoff

Adjusting alpha cutoff during run time doesn't work

Hello, I am trying to change the render mode of my material on a standard shader to cutout mode with the following code:

 mesh_renderer.materials[0].SetFloat("_Mode", 1);
 mesh_renderer.materials[0].SetFloat("_Cutoff", 1f);

alt text

The code works just fine and the alpha cutoff is set correctly in the inspector, however it doesn't work on the mesh until I drag it to any other value than 1f, and then back to 1f in the inspector, even if I ran the code through update.

Is there a way it would apply the changes to the material from the code, without requiring me to manually do that?

Thanks in advance!

Comment
Add comment · Show 1
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 jmgek · Aug 17, 2015 at 05:51 PM 0
Share

Try setting cutoff to 0 then 1

1 Reply

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

Answer by Thomas-Mountainborn · Aug 17, 2015 at 06:09 PM

A material created at runtime does not have all the necessary standard shader keywords enabled. You need to set a bunch of other ones before you have it set as transparent - by dragging the value in the inspector, the editor does this for you, which is why that works.

 material.SetOverrideTag("RenderType", "TransparentCutout");
 material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
 material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
 material.SetInt("_ZWrite", 1);
 material.EnableKeyword("_ALPHATEST_ON");
 material.DisableKeyword("_ALPHABLEND_ON");
 material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
 material.renderQueue = 2450;

This post by Farfarer provides a handy method to make your life easier in regards to this issue.

Comment
Add comment · Show 1 · 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 janidsa · Aug 17, 2015 at 06:58 PM 1
Share

Thank you! This solved the issue.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is there a way to cast (maybe recieve) sprite shadows without discarding alpha pixels with cutout shader? 0 Answers

Separate Alpha and Color Textures for CutOut Shader 1 Answer

Merging (alpha) Unlit shader with Cutout shader 3 Answers

An Outline like the Editor does in game, with transparency 1 Answer

Texture Alpha cutoff + Photoshop 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges