Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing an Object's Emissive Color and Intensity (HDRP) in C#

Discussion in 'Scripting' started by kayronjm, Mar 31, 2020.

  1. kayronjm

    kayronjm

    Joined:
    Jun 15, 2019
    Posts:
    34
    Hi all, I've spent hours and hours trying to do something that I initially assumed was very simple and turned out not to be. I've got an object that has an emissive color map assigned to it. If I change the HDR color and intensity of the material itself in the inspector, the object changes accordingly. In code however, it's impossible to get working.

    In order to access the object's materials, I had to first assign a Mesh Renderer component to the object and place the materials it uses in the slots. If I click Play, I can still make changes to the HDR color and intensity in the inspector, whether I select the material itself or the material assigned in the Mesh Renderer. However, to interact with this material in code, to a script attached to the object, on Start(), I added:

    Code (CSharp):
    1. weaponTurretMainMaterial = this.transform.GetComponent<Renderer>().materials[1];
    Adding this line of code alone (with nothing else in the script) already breaks the link between the material in the Mesh Renderer and the object itself. No changes made to the material in the inspector within the object make any difference. I have to make the changes to the base material in the folder itself. This was already weird to me. Anything I tried in code, did absolutely nothing at this point.

    I've tried enabling the keyword:

    Code (CSharp):
    1. weaponTurretMainMaterial.EnableKeyword("_EMISSION");
    I've tried setting a color like this:

    Code (CSharp):
    1. weaponTurretMainMaterial.SetVector("_EmissionColor", Color.green * 10.0f);
    Also like this:

    Code (CSharp):
    1. weaponTurretMainMaterial.SetColor("_EmissionColor", new Vector4(0.0f, 1.0f, 0.0f, 1.0f) * 10.0f);
    I've tried using _EmissiveColor and _EMISSIVE as opposed to _EmissionColor and _EMISSION. I've tried combinations of the above, etc. Hours and nothing achieved.

    The paradigm is that as soon as I use the line for GetComponent<Renderer>().materials[1], it basically disassociates itself from the object and the only way to change anything is to change the base material itself in the folder via the inspector. It's incredibly frustrating and any help would be greatly appreciated. Thank you!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Did you try assigning it back into the renderer? I think when you get the .material property it is a copy, so your code is only changing the copy.

    Also, to assign an array of Materials, you need to actually make an array, copy all the other materials over to it, change the material you want, then reassign the entire array back to the .materials property. You cannot assign a single entry in that array.
     
  3. kayronjm

    kayronjm

    Joined:
    Jun 15, 2019
    Posts:
    34
    Thanks for the tip! Since there's only one material that has an emission channel, I've only set this one. Indeed as soon as I make any change via SetColor, the material in the object has (Instance) next to it. I have however managed to get it to change colour and intensity from my script, with simply:

    Code (CSharp):
    1. weaponTurretRenderer = this.transform.GetComponent<Renderer>();
    2. emisColor = Color.green * 1000.0f;      
    3. weaponTurretRenderer.material.SetColor("_EmissiveColor", emisColor);
    The only issue is now that the changes aren't physically displaying. I can make these changes from the inspector while not running the game and it's all good. Do it via the script however and the emission colour and intensity values change but the emission surface remains the same.
     
    Last edited: Apr 1, 2020
  4. kayronjm

    kayronjm

    Joined:
    Jun 15, 2019
    Posts:
    34
    I tried the above code on a random cube with a different, test material and it worked just fine. It seems to just be an issue with the emissive colour map in my weapon turret's material texture. It doesn't matter - I got around the issue by cutting holes into the model and creating a group of planes to cover these holes. These planes are then textured with a new Unity material that has a black base colour and is enabled for emission, whose colour and intensity is controlled by the above code. All working as expected.
     
    Kurt-Dekker likes this.
  5. lucidtripper

    lucidtripper

    Joined:
    Aug 3, 2017
    Posts:
    22
    how about for URP ? trying to switch emission on a "urp lit" shader from black to green depending on a bool