Search Unity

Overriding ambient light.

Discussion in 'Global Illumination' started by DamonJager, Aug 16, 2016.

  1. DamonJager

    DamonJager

    Joined:
    Jul 16, 2015
    Posts:
    56
    Where should I look into? Maybe the standard shader code? I want to do something on a per-renderer or material basis.
     
    Last edited: Aug 16, 2016
  2. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    Vertex Lit is what you need to use I think. There are better expert than me around though.
     
  3. DamonJager

    DamonJager

    Joined:
    Jul 16, 2015
    Posts:
    56
    I'm also learning. I think that vertex lit stuff fits better the old tech I'm considering to use since LP won't let me do dynamic stuff.
     
    Last edited: Aug 16, 2016
  4. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182

    Well which Unity 5.4 Release you may be right. Something has changed with VertexLit handling.

    I had to chose some 'Mobile/Particles/VertexLitBlended' for my scene previous Unity 5.4 release and they had worked in the past, it was to get some models foilage to work in my scene but now that I have adjusted my scene to use Mixed lighting and then baked that has caused everything with VertexLit Blender shaders to become completely transparent.

    So I will have to choose another shader or another set of models...which is a hassle considering all the time I spent placing those trees.
     
  5. kemalakay

    kemalakay

    Unity Technologies

    Joined:
    Jul 12, 2016
    Posts:
    224
    Hi @goat,

    I was just checking the issue you mentioned about VertexLit Blended shaders and I would like to ask for some details. You said VertexLit Blended shaders were working before the version 5.4, but I tried it on 5.15f1, 5.3.5p8 and when I assign a VertexLit Blended shader to a game object, make it static and bake the scene, the game object itself always becomes transparent. The behaviour is consistently the same in 5.x versions. May I ask if you are doing something different?

    Making them dynamic would technically solve your problem, since they become visible in that case. However, it won’t cast shadows. Is that what you want, have static game objects that cast shadow with VertexLit Blended shader assigned?

    Thanks
     
  6. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    253
    Hello from 2023.

    Is it possible now to override Ambient Light per-renderer?

    - Unity 2020.3.18f1
    - Built-in render pipeline

    There is
    https://docs.unity3d.com/ScriptReference/MaterialPropertyBlock.CopySHCoefficientArraysFrom.html

    with
    https://docs.unity3d.com/ScriptReference/Rendering.SphericalHarmonicsL2.html

    that work's fine with MeshRenderer, you can enable it this way:

    Code (CSharp):
    1. var _harmonics = new[] {new SphericalHarmonicsL2()};
    2. SphericalHarmonicsL2 sh1 = new SphericalHarmonicsL2();
    3. sh1.AddAmbientLight(SHcustomAmbientColor);
    4. if (SHuseCustomDirectionalLight)
    5. {
    6.     sh1.AddDirectionalLight(SHcustomLightDirection, SHcustomLightColor, SHcustomLightIntensity);
    7. }
    8.  
    9. _harmonics[0] = sh1;
    10. MaterialPropertyBlock prop = new MaterialPropertyBlock();
    11. renderer.GetPropertyBlock(prop);
    12. prop.CopySHCoefficientArraysFrom(_harmonics);
    13. renderer.lightProbeUsage = LightProbeUsage.CustomProvided;
    14. renderer.SetPropertyBlock(prop);
    15.  
    And disable with:
    renderer.lightProbeUsage = LightProbeUsage.BlendProbes;


    But with SkinnedMeshRenderer Enable code work fine, but to disable you need also:
    Code (CSharp):
    1. MaterialPropertyBlock block = new MaterialPropertyBlock();
    2. renderer.GetPropertyBlock(block);
    3. block.Clear();
    4. renderer.SetPropertyBlock(block);
    And re-enabling this override on SkinnedMeshRenderer after MaterialPropertyBlock.Clear() on it will be lit twice, by LightingSettings.ambientLight and by overriden SphericalHarmonicsL2[].

    May be there is more correctly working way to override Ambient Light on per-renderer bases, including SkinnedMeshRenderer?
     
    Last edited: Mar 16, 2023
  7. Pema-Malling

    Pema-Malling

    Unity Technologies

    Joined:
    Jul 3, 2020
    Posts:
    328
    The doubled lighting sounds like a bug to me. I suggest you file a bug report.