Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question altering materials alpha value through code makes character look weird.

Discussion in 'Shaders' started by Stixxy, Jul 21, 2022.

  1. Stixxy

    Stixxy

    Joined:
    Feb 26, 2019
    Posts:
    37
    Hey, thanks for stopping by. I have a problem where I'm trying to make it so that when the camera zooms in to the player, they start to go transparent, but when they start to turn transparent, their material gets all messed up. Here's the code for the script:

    Code (CSharp):
    1.  
    2.     [Header("References")]
    3.     private GameObject raph;
    4.     private SkinnedMeshRenderer player;
    5.     private SkinnedMeshRenderer playerHat;
    6.     private Camera mainCam;
    7.  
    8.     [Header("Materials")]
    9.     public Material opaque;
    10.     public Material transparent;
    11.     private Color transparency;
    12.  
    13.  
    14.     [Header("Values")]
    15.     public float distance;
    16.     private float transparencyRate;
    17.  
    18.  
    19.     // Start is called before the first frame update
    20.     void Awake()
    21.     {
    22.         raph = GameObject.FindGameObjectWithTag("Player");
    23.         player = GameObject.Find("Farmer").GetComponent<SkinnedMeshRenderer>();
    24.         playerHat = GameObject.Find("Farmer Hat").GetComponent<SkinnedMeshRenderer>();
    25.         mainCam = Camera.main;
    26.  
    27.         transparency = transparent.color;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         distance = Vector3.Distance(mainCam.transform.position, raph.transform.position);
    34.  
    35.         if (distance <= 5f)
    36.         {
    37.             transparencyRate = (distance / 5f) * 255f;
    38.  
    39.             transparency.a = transparencyRate;
    40.  
    41.             player.material = transparent;
    42.             player.material.color = transparency;
    43.             playerHat.material = transparent;
    44.             playerHat.material.color = transparency;
    45.         }
    46.  
    47.         else if (distance > 5)
    48.         {
    49.             player.material = opaque;
    50.             playerHat.material = opaque;
    51.         }
    52.     }
    Here's some pictures with the materials and the character themself:

    https://imgur.com/gallery/IoSsleH

    if anyone knows what is going wrong, I'd really appreciate that considering my knowledge when it comes to shaders is practically 0! Thanks for reading my post :)
     
  2. victor_apihtin

    victor_apihtin

    Unity Technologies

    Joined:
    Mar 2, 2021
    Posts:
    28
    hi @Stixxy

    The idea is nobel, but you wont be able to render the objects properly even if you didnt have the artifacts, due to that transparent objects sort per their pivot position and not per pixel depth value. It is going to look weird anyhow.
    The way people usually do it is by using Dithering algorithm. The material in this case is always opaque, but the shader is removing pixels in a pattern so that the closer the camera is to the object, more and more pixels are being removed.

    So depending how you are going to implement your shaders (shader graph or by code) you should be looking for made solutions for "Dither". For example there is a dithering node in Shader graph

    As for the artifacts, I am not entirely sure, but it seems that depth buffer is empty, as if the geometry behind the character has not been written prior the character. Maybe "Fade" is not an appropriate mode, or the shader for grass is not set up properly
     
  3. Stixxy

    Stixxy

    Joined:
    Feb 26, 2019
    Posts:
    37
    Thanks for the reply! But I actually managed to solve it by making the value a number between 0 and 1 instead of 0 and 255. Turns out I just needed to stop multiplying the transparencyrate * 255. Thanks for the response anyways though!.
     
    victor_apihtin likes this.