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

Ambient Light(UNITY_LIGHTMODEL_AMBIENT) removed in Unity 5.0?

Discussion in 'Shaders' started by vadithiyan, Jan 6, 2015.

  1. vadithiyan

    vadithiyan

    Joined:
    Sep 3, 2013
    Posts:
    6
    I was following the tutorial on CGCookie on writing shaders in Unity. And when i wrote the code to add ambient light to the shader, it just did not work. I tried the same code in Unity 4.6 and it works. Am i doing anything wrong??
    Code (CSharp):
    1.     Properties
    2.     {
    3.         _Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
    4.     }
    5.     SubShader
    6.     {
    7.         Pass
    8.         {
    9.         Tags {"LightMode" = "ForwardBase"}
    10.             CGPROGRAM
    11.             #pragma vertex vert
    12.             #pragma fragment frag
    13.          
    14.             //user defined
    15.             uniform float4 _Color;
    16.          
    17.             //unity defined
    18.             uniform float4 _LightColor0;
    19.          
    20.             ///base structs
    21.             struct vertexInput
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float3 normal : NORMAL;
    25.             };
    26.             struct vertexOutput
    27.             {
    28.                 float4 pos : SV_POSITION;
    29.                 float4 col : COLOR;  
    30.             };
    31.          
    32.             //vertex function
    33.             vertexOutput vert(vertexInput v)
    34.             {
    35.              
    36.                 vertexOutput o;
    37.                 float3 normalDirection = normalize(mul(float4(v.normal, 0.0), _World2Object).xyz);
    38.                 float3 lightDirection;
    39.                 float atten = 1.0;
    40.              
    41.                 lightDirection = normalize( _WorldSpaceLightPos0.xyz);
    42.                 float3 diffuseReflection = max(0.0, (atten * _LightColor0.xyz * dot(normalDirection , lightDirection)));
    43.                 float3 finalLight =  2 *(diffuseReflection + UNITY_LIGHTMODEL_AMBIENT.xyz);
    44.              
    45.                 o.col = float4(finalLight * _Color.rgb , 1.0);
    46.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    47.                 return o;
    48.             }
    49.          
    50.             //fragment shader
    51.             float4 frag(vertexOutput i) : COLOR
    52.             {
    53.                 return i.col;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }
    i tried to change this line
    float3 finalLight = 2 *(diffuseReflection + UNITY_LIGHTMODEL_AMBIENT.xyz);
    to
    float3 finalLight = 2 *( UNITY_LIGHTMODEL_AMBIENT.xyz);
    just to check if it is working and the mesh is completely black after this change.
    Is this a bug or has anything been changed?
     
  2. DFugas

    DFugas

    Joined:
    May 5, 2015
    Posts:
    1
    It works in Unity 5.
    I use 5.3.1 ver, My problem was - I wrote '*' instead of '+' UNITY_LIGHTMODEL_AMBIENT.
    You can use my code (from GCCookie).

    Code (CSharp):
    1. //vertext func
    2.             vertexOutput vert(vertexInput v) {
    3.                 vertexOutput o;
    4.  
    5.                 float3 normalDirection = normalize( mul(float4(v.normal, 0.0), _World2Object).xyz );
    6.                 float lightDirection;
    7.                 float atten = 1.0;
    8.  
    9.                 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    10.  
    11.                 float3 diffuseReflection = atten * _LightColor0.xyz * max( 0.0, dot(lightDirection, normalDirection) );
    12.                 float3 lightFinal = diffuseReflection + UNITY_LIGHTMODEL_AMBIENT.xyz;
    13.  
    14.                 o.col = float4(lightFinal * _Color.rgb, 1.0);
    15.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    16.                 return o;
    17.             }
     
  3. vadithiyan

    vadithiyan

    Joined:
    Sep 3, 2013
    Posts:
    6
    This was a problem in during the beta stage of Unity 5.0, this was before Unity 5 was officially released. I sent Unity a bug report, they acknowledged that it was a bug and fixed the bug in the next beta build. Should have updated this thread with the information, thanks though.