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

How to Change the Emissive Color of the Material

Discussion in 'Scripting' started by Alexander21, Oct 26, 2017.

  1. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Hi All

    I am trying to change the Emissive Color of the Materail . I am using the below code.


    Code (CSharp):
    1. public material  mat;
    2.  
    3. public void Start()
    4. {
    5. mat.EnableKeyword("_EMISSION");
    6. mat.SetColor("_EmisColor",,Color.red);
    7. }
    The shader of the material is Legacy Shader/transparent/vertextlit.

    but the emissive color is not changed. Could any one Help me....
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    N00MKRAD likes this.
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    This is that shader from the unity built-in shaders:
    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Legacy Shaders/Transparent/VertexLit" {
    4.     Properties {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _SpecColor ("Spec Color", Color) = (1,1,1,0)
    7.         _Emission ("Emissive Color", Color) = (0,0,0,0)
    8.         _Shininess ("Shininess", Range (0.1, 1)) = 0.7
    9.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    10.     }
    11.  
    12.     SubShader {
    13.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    14.         LOD 100
    15.  
    16.         ZWrite Off
    17.         Blend SrcAlpha OneMinusSrcAlpha
    18.         ColorMask RGB
    19.  
    20.         // Non-lightmapped
    21.         Pass {
    22.             Tags { "LightMode" = "Vertex" }
    23.             Material {
    24.                 Diffuse [_Color]
    25.                 Ambient [_Color]
    26.                 Shininess [_Shininess]
    27.                 Specular [_SpecColor]
    28.                 Emission [_Emission]
    29.             }
    30.             Lighting On
    31.             SeparateSpecular On
    32.             SetTexture [_MainTex] {
    33.                 Combine texture * primary DOUBLE, texture * primary
    34.             }
    35.         }
    36.  
    37.         // Lightmapped, encoded as dLDR
    38.         Pass {
    39.             Tags { "LightMode" = "VertexLM" }
    40.  
    41.             BindChannels {
    42.                 Bind "Vertex", vertex
    43.                 Bind "normal", normal
    44.                 Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    45.                 Bind "texcoord", texcoord1 // main uses 1st uv
    46.             }
    47.             SetTexture [unity_Lightmap] {
    48.                 matrix [unity_LightmapMatrix]
    49.                 constantColor [_Color]
    50.                 combine texture * constant
    51.             }
    52.             SetTexture [_MainTex] {
    53.                 combine texture * previous DOUBLE, texture * primary
    54.             }
    55.         }
    56.  
    57.         // Lightmapped, encoded as RGBM
    58.         Pass {
    59.             Tags { "LightMode" = "VertexLMRGBM" }
    60.  
    61.             BindChannels {
    62.                 Bind "Vertex", vertex
    63.                 Bind "normal", normal
    64.                 Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    65.                 Bind "texcoord1", texcoord1 // unused
    66.                 Bind "texcoord", texcoord2 // main uses 1st uv
    67.             }
    68.  
    69.             SetTexture [unity_Lightmap] {
    70.                 matrix [unity_LightmapMatrix]
    71.                 combine texture * texture alpha DOUBLE
    72.             }
    73.             SetTexture [unity_Lightmap] {
    74.                 constantColor [_Color]
    75.                 combine previous * constant
    76.             }
    77.             SetTexture [_MainTex] {
    78.                 combine texture * previous QUAD, texture * primary
    79.             }
    80.         }
    81.     }
    82. }
    As you can see, the property name is "_Emission".
     
  4. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Thanks fire7Side & jeffreyschoch for your post...... The propery name _Emission is Correct........
     
    N00MKRAD likes this.
  5. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    I was getting this kind of error while trying to get color:

    emission_error.png

    This kind of code, I have used:
    Code (CSharp):
    1.  Color startCol = myMaterial.GetColor("_Emission");
    2.  
    3. myMaterial.SetColor("_Emission", startCol * new Color(1f, 1f, 1f, 1f - i));
     
  6. azevedco

    azevedco

    Joined:
    Mar 2, 2014
    Posts:
    34
    As i'm working in HDRP, not sure if it differs per pipeline, I use "_EmissiveColor" when changing the emissive color of a material.