Search Unity

Pulsing Emission value causing stuttering

Discussion in 'Image Effects' started by TheMonkeysaur, Dec 1, 2017.

  1. TheMonkeysaur

    TheMonkeysaur

    Joined:
    May 2, 2017
    Posts:
    52
    I'm using a simple script to pulse the emission parts of my material. It works nicely as I want it to, but whenever it's active it's causing my game to stutter when my player moves around.

    This is the code I am using:

    Code (csharp):
    1.  
    2. public class emissionPulse : MonoBehaviour {
    3.  
    4.         public float frequency = 1f;
    5.         public Material material;
    6.         public Color emissionColor;
    7.  
    8.         // Use this for initialization
    9.         void Start()
    10.         {
    11.         emissionColor = material.GetColor("_EmissionColor");
    12.         }
    13.  
    14.         // Update is called once per frame
    15.         void Update()
    16.         {
    17.             float glow = (2 + Mathf.Cos(Time.time * frequency));
    18.             material.SetColor("_EmissionColor", emissionColor * glow);
    19.         }
    20. }
    21.  
    Anyone know why this is happening or another way to achieve this effect?

    Thanks.
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Put all that math inside your shader. Throw away your script. It'll be much faster.
     
  3. TheMonkeysaur

    TheMonkeysaur

    Joined:
    May 2, 2017
    Posts:
    52
    Using a shader is probably a better idea, thanks.
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I don't see how the performance would change at all unless the OP is missing out a ton of info like he's spawned 100s of these.
     
  5. TheMonkeysaur

    TheMonkeysaur

    Joined:
    May 2, 2017
    Posts:
    52
    Nope there is only one object.