Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question MaterialPropertyBlock Problem

Discussion in 'Scripting' started by DanielRiches, Aug 16, 2020.

  1. DanielRiches

    DanielRiches

    Joined:
    Nov 23, 2019
    Posts:
    167
    Just started getting my toes wet with MaterialPropertyBlocks, I have the code below that lerps emission color, but when the index shifts the lerp starts from black, i'm setting the MPB's _EmissionColor through the lerp, then when I shift Index I set the start of the lerp with : theEmission = theMaterial.GetColor("_EmissiveColor");

    The problem is it seems to be reading the color as it's original starting color, not it's CURRENT color.

    Can anyone offer any insight? i'm betting it's something simple but like I said i'm new to this way of doing things :)

    MaterialPropertyBlocks are some great stuff though!

    Video of the result:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ReactToMusic : MonoBehaviour
    6. {
    7.     public GameObject theGameManager;
    8.     public MusicReactMode musicReactMode;
    9.  
    10.     private Renderer myMeshRenderer;
    11.     private MaterialPropertyBlock theMaterial;
    12.  
    13.     #region Variables
    14.     [HideInInspector] public bool goingSlow = true;
    15.     [HideInInspector] public bool goingFast = false;
    16.     float theSpeed;
    17.     private float slowSpeed = 0.8f;
    18.     private float fastSpeed = 0.2f;
    19.  
    20.     // Emission Lerp
    21.     private float lerpEmissionValue = 0.4f;  // Brightness of emission lerp
    22.     float meshELerpDuration;
    23.     float meshELerpTimer;
    24.     float meshELerpElapsedTime = 0f;
    25.  
    26.     // Color Lerp
    27.     float meshCLerpDuration;
    28.     float meshCLerpTimer;
    29.     float meshCLerpElapsedTime = 0f;
    30.     #endregion
    31.  
    32.     #region Lerp Bools
    33.     [HideInInspector] public bool active;
    34.     [HideInInspector] public bool isLerping;
    35.  
    36.     bool disco;
    37.  
    38.     bool chosenDisco;
    39.     #endregion
    40.  
    41.     #region Lerp Color Arrays
    42.     [HideInInspector] public Color[] discoColors;
    43.  
    44.     private Color[] tmpEmissionColorArray;
    45.     int tmpEmissionColorArrayIndex = 0;
    46.  
    47.     #endregion
    48.  
    49.     [Header("- Material -")]
    50.     public int materialElement;
    51.     [SerializeField] private Color myStartingMeshColor;
    52.     [SerializeField] private Color theColor;
    53.     [SerializeField] private Color myStartingEmissionColor;
    54.     [SerializeField] private Color theEmission;
    55.  
    56.     [Header("- Settings -")]
    57.     public bool lerpMe;
    58.     public bool lerpEmission;
    59.  
    60.     private void Start()
    61.     {
    62.         theMaterial = new MaterialPropertyBlock();
    63.  
    64.         theGameManager = GameObject.FindWithTag("GameManager");
    65.  
    66.         musicReactMode = theGameManager.gameObject.GetComponent<MusicReactMode>();
    67.  
    68.         myMeshRenderer = GetComponent<Renderer>();
    69.  
    70.         // Get chosen property block from the renderer and assign it to theMaterial
    71.         myMeshRenderer.GetPropertyBlock(theMaterial, materialElement);      
    72.  
    73.         if (materialElement > myMeshRenderer.materials.Length - 1)
    74.         {
    75.             Debug.Log("Selected a non-existant material on " + this.gameObject.name + ", Element 0 selected as default");
    76.             materialElement = 0;
    77.         }
    78.  
    79.         myStartingMeshColor = theMaterial.GetColor("_BaseColor");
    80.         myStartingEmissionColor = theMaterial.GetColor("_EmissiveColor");
    81.  
    82.  
    83.         theColor = theMaterial.GetColor("_BaseColor");
    84.         theEmission = theMaterial.GetColor("_EmissiveColor");
    85.     }
    86.  
    87.     void Update()
    88.     {
    89.         // Get the current value of the material properties in the renderer.
    90.         myMeshRenderer.GetPropertyBlock(theMaterial);
    91.  
    92.  
    93.         // Assign our new value.
    94.         if (myMeshRenderer != null)
    95.         {
    96.             if (this.gameObject.activeInHierarchy)
    97.             {
    98.                 #region Emission Lerp
    99.                 if (musicReactMode.isLerping)
    100.                 {
    101.                     if (musicReactMode.disco && !chosenDisco)
    102.                     {
    103.                         Debug.Log("Chosen Disco...");
    104.  
    105.                         tmpEmissionColorArray = discoColors;                    
    106.  
    107.                         tmpEmissionColorArrayIndex = 0;                    
    108.  
    109.                         meshELerpElapsedTime = 0f;
    110.  
    111.                         theEmission = theMaterial.GetColor("_EmissiveColor");
    112.  
    113.                         disco = true;
    114.                         chosenDisco = true;
    115.  
    116.                     }
    117.  
    118.                     if (!active)
    119.                     {
    120.                         Debug.Log("Activating...");
    121.  
    122.                         active = true;
    123.                         isLerping = true;
    124.                     }
    125.                  
    126.  
    127.                     if (lerpMe)
    128.                     {
    129.                         if (lerpEmission)
    130.                         {
    131.                             meshELerpElapsedTime += Time.deltaTime;
    132.  
    133.                             if (meshELerpElapsedTime >= meshELerpDuration)
    134.                             {
    135.                                 theEmission = theMaterial.GetColor("_EmissiveColor");                            
    136.  
    137.                                 tmpEmissionColorArrayIndex = (tmpEmissionColorArrayIndex + 1 + tmpEmissionColorArray.Length) % tmpEmissionColorArray.Length;                            
    138.  
    139.                                 meshELerpElapsedTime = 0f;
    140.                             }
    141.  
    142.                             meshELerpTimer = meshELerpElapsedTime / meshELerpDuration;
    143.  
    144.                             Debug.Log(theEmission);
    145.  
    146.                             theMaterial.SetColor("_EmissiveColor", Color.Lerp(theEmission, tmpEmissionColorArray[tmpEmissionColorArrayIndex] * lerpEmissionValue, meshELerpTimer));
    147.                         }
    148.                     }
    149.                 }
    150.                 #endregion
    151.  
    152.                 // Apply the edited values to the renderer.
    153.                 myMeshRenderer.SetPropertyBlock(theMaterial, materialElement);
    154.             }
    155.         }
    156.     }
    157. }
    EDIT:

    I got around the problem by using:
    Code (CSharp):
    1. theEmission = tmpEmissionColorArray[tmpEmissionColorArrayIndex] * lerpEmissionValue;
    instead when the Index shifts, but i'm unsure if this is just another way around the problem, not sure if it's the usual way you would do it...
     
    Last edited: Aug 17, 2020