Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Changing lightweight shader "surface type "in c# ?

Discussion in 'Graphics Experimental Previews' started by staticbl, Oct 31, 2018.

  1. staticbl

    staticbl

    Joined:
    Jul 1, 2016
    Posts:
    12
    Hi there,

    I'm struggling with the following, could someone please help me with it ?

    I'm using the LightWeight Pipeline and my material is using the "LightWeightPipeline/Standard (Simple Lighting)" shader. I'm trying to make my object transparent in certain conditions during runtime (and then back to non-transparent), but I don't know how to change the "surface type" from opaque to transparent at runtime. How would I do that ?

    I know I could keep the surface type transparent at all times, but that doesn't look good as it changes the look of my objects.

    Thanks !

    Jonathan
     
  2. Ehredt

    Ehredt

    Joined:
    Jun 20, 2018
    Posts:
    79
    To do something similar, I just switch between two materials, one opaque and one transparent. This has been working fine for me, but if there's a better way to do it, I'd be interested as well.
     
  3. BigGameCompany

    BigGameCompany

    Joined:
    Sep 29, 2016
    Posts:
    112
    I'm trying to do this as well. Having a separate transparent material isn't really an option for us - there are just too many. Anyone have any other solutions?
    Thanks
     
  4. BigGameCompany

    BigGameCompany

    Joined:
    Sep 29, 2016
    Posts:
    112
    I am switching to the Unlit / Transparent shader when I need transparency at the moment. It's not ideal but works ok for my requirements.
     
  5. JonathanW28

    JonathanW28

    Joined:
    Mar 24, 2017
    Posts:
    1
    I managed to do this with the following:
    Code (CSharp):
    1.  
    2.        private const int MATERIAL_OPAQUE = 0;
    3.        private const int MATERIAL_TRANSPARENT = 1;
    4.  
    5. private void SetMaterialTransparent(Material material, bool enabled)
    6.         {
    7.             material.SetFloat("_Surface", enabled ? MATERIAL_TRANSPARENT : MATERIAL_OPAQUE);
    8.             material.SetShaderPassEnabled("SHADOWCASTER", !enabled);
    9.             material.renderQueue = enabled ? 3000 : 2000;
    10.             material.SetFloat("_DstBlend", enabled ? 10 : 0);
    11.             material.SetFloat("_SrcBlend", enabled ? 5 : 1);
    12.             material.SetFloat("_ZWrite", enabled ? 0 : 1);
    13.         }
     
    Last edited: Aug 23, 2021
    CameronLewis likes this.