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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How can I use StartCoroutine to fade also the rendering mode of the character material ?

Discussion in 'Editor & General Support' started by barshalev, Jun 11, 2020.

  1. barshalev

    barshalev

    Joined:
    Jun 10, 2020
    Posts:
    7
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class Teleporting : MonoBehaviour
    8. {
    9.     public List<GameObject> teleporters = new List<GameObject>();
    10.     public GameObject objectToTeleportMaterial;
    11.     public float fadeDuration = 5;
    12.     public float fadeInTargetOpacity = 0;
    13.     public float fadeOutTargetOpacity = 1;
    14.     public static bool teleportedIn = false;
    15.  
    16.     private List<Vector3> teleportersPositions = new List<Vector3>();
    17.     private bool teleported = false;
    18.     private Material material;
    19.     private GameObject myother;
    20.  
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.        
    26.     }
    27.  
    28.     private void OnTriggerEnter(Collider other)
    29.     {
    30.         if (other.name == "vanguard_t_choonyung@T-Pose (1)")
    31.         {
    32.             teleported = false;
    33.             myother = other.gameObject;
    34.             material = objectToTeleportMaterial.GetComponent<Renderer>().material;
    35.             Teleport(material, fadeInTargetOpacity, fadeDuration);
    36.         }
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.         if (teleported == true)
    43.         {
    44.             myother.transform.position = teleporters[1].transform.position;
    45.             Teleport(material, fadeOutTargetOpacity, fadeDuration);
    46.             teleported = false;
    47.         }
    48.     }
    49.  
    50.     private void Teleport(Material material, float fadeTargetOpacity, float fadeDuration)
    51.     {
    52.         if (fadeTargetOpacity == 1)
    53.         {
    54.             MaterialExtensions.ToOpaqueMode(material);
    55.         }
    56.         else
    57.         {
    58.             MaterialExtensions.ToTransparentMode(material);
    59.         }
    60.         StartCoroutine(FadeTo(material, fadeTargetOpacity, fadeDuration));
    61.     }
    62.  
    63.     // Define an enumerator to perform our fading.
    64.     // Pass it the material to fade, the opacity to fade to (0 = transparent, 1 = opaque),
    65.     // and the number of seconds to fade over.
    66.     IEnumerator FadeTo(Material material, float targetOpacity, float duration)
    67.     {
    68.  
    69.         // Cache the current color of the material, and its initiql opacity.
    70.         Color color = material.color;
    71.         float startOpacity = color.a;
    72.  
    73.         // Track how many seconds we've been fading.
    74.         float t = 0;
    75.  
    76.         while (t < duration)
    77.         {
    78.             // Step the fade forward one frame.
    79.             t += Time.deltaTime;
    80.             // Turn the time into an interpolation factor between 0 and 1.
    81.             float blend = Mathf.Clamp01(t / duration);
    82.  
    83.             // Blend to the corresponding opacity between start & target.
    84.             color.a = Mathf.Lerp(startOpacity, targetOpacity, blend);
    85.  
    86.             // Apply the resulting color to the material.
    87.             material.color = color;
    88.  
    89.             // Wait one frame, and repeat.
    90.             yield return null;
    91.         }
    92.  
    93.         if (targetOpacity == 1)
    94.         {
    95.             //MaterialExtensions.ToOpaqueMode(material);
    96.             teleportedIn = true;
    97.         }
    98.  
    99.         if (targetOpacity == 0)
    100.         {
    101.             teleported = true;
    102.         }
    103.     }
    104. }
    105.  
    And the class :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public static class MaterialExtensions
    7. {
    8.     public static void ToOpaqueMode(this Material material)
    9.     {
    10.         material.SetOverrideTag("RenderType", "");
    11.         material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    12.         material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
    13.         material.SetInt("_ZWrite", 1);
    14.         material.DisableKeyword("_ALPHATEST_ON");
    15.         material.DisableKeyword("_ALPHABLEND_ON");
    16.         material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    17.         material.renderQueue = -1;
    18.     }
    19.  
    20.     public static void ToTransparentMode(this Material material)
    21.     {
    22.         material.SetOverrideTag("RenderType", "Transparent");
    23.         material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    24.         material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    25.         material.SetInt("_ZWrite", 0);
    26.         material.DisableKeyword("_ALPHATEST_ON");
    27.         material.EnableKeyword("_ALPHABLEND_ON");
    28.         material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    29.         material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
    30.     }
    31. }
    32.  
    The problem is when using the functions ToOpaqueMode and ToTransparentMode it's changing the rendering mode too fast at once and not part of the FadeTo function so it's not looks so realistic. The character does fade in out by changing the alpha color to 0 and 1 but the rendering mode is not changing by time using a Coroutine so the rendering mode changes don't look like a part of the teleporting it's not looks like natural part of the teleporting.

    Is there a way to use this ToOpaqueMode and ToTransparentMode with the FadeTo ?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Why not just always use a transparent rendering mode, and just start with 100% opacity?

    I'm not sure how you could interpolate a boolean value, and it seems you are changing a bunch of booleans when you switch rendering modes.

    By the way, you're missing out on the full power of your extension methods. Since they are extension methods, you can call them directly on the object, you don't need to statically invoke the method. So instead of
    Code (CSharp):
    1. MaterialExtensions.ToOpaqueMode(material);
    you could just do
    Code (CSharp):
    1. material.ToOpaqueMode();
     
    barshalev likes this.
  3. barshalev

    barshalev

    Joined:
    Jun 10, 2020
    Posts:
    7

    I added this line to the Start

    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         material.ToTransparentMode();
    5.     }
    6.  
    And I moved all the code with the materials from the Teleport function so now it's :

    Code (csharp):
    1.  
    2. private void Teleport(Material material, float fadeTargetOpacity, float fadeDuration)
    3.     {
    4.         StartCoroutine(FadeTo(material, fadeTargetOpacity, fadeDuration));
    5.     }
    6.  
    So now that I'm using only the transparent rendering mode, how to start with 100% opacity ? and what should I change later to use with the opacity ?
     
  4. barshalev

    barshalev

    Joined:
    Jun 10, 2020
    Posts:
    7

    You can see in this link I added screenshot to describe the problem I had and after solved this problem why I wanted to make the rendering modes to be also interpolate a boolean value like changing the alpha color.

    The problem is that the rendering modes changing is made by once at once and not like fading the alpha color slowly.

    I didn't understand how to do it with my code.
     
  5. barshalev

    barshalev

    Joined:
    Jun 10, 2020
    Posts:
    7
    https://www.reddit.com/r/Unity3D/comments/h0ko7j/why_the_character_is_looks_transparent_after/