Search Unity

How Can I Save Realtime Light Calculation When I Don't Have Realtime Light In Forward Base Pass

Discussion in 'Shaders' started by lichunHZ, Jun 13, 2017.

  1. lichunHZ

    lichunHZ

    Joined:
    Jun 13, 2017
    Posts:
    3
    Hi, I wrote a vertex/fragment shader to deal with lights, we know that in forward base pass, a realtime directional light is calculated. I need my shader to deal with this light, but if no realtime lights in my scene(all baked), I hope the shader can save this calculation. I don't know how to control it, lightmaps can be toggled by LIGHTMAP_ON, and light probes can be controlled by UNITY_SHOULD_SAMPLE_SH, how can i toggle real time lighting in my forward base pass shader?

    Even in the standard shader, i didn't see such macro. If no real time lighting, forward base pass shader still calculates real time lighting is pixel shader, just the light color is black.
     
  2. LukasCh

    LukasCh

    Unity Technologies

    Joined:
    Mar 9, 2015
    Posts:
    102
    What exactly do you mean by "save this calculation", because baked lights are already stored in texture and if no realtime light is present there is no point storing them too (Even though realtime light not intended to be stored). Maybe you want to skip forward base pass if not directional light is present?
     
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    He means skip the calculation (of the main directional light if it is not present.)

    I also don't see it.in the preprocessor macros. It would be a fairly good addition to set a macro if there is no main directional light. Then you can skip the calculation in the shader. And it doesn't interfere with the default approach of calculating it with a light intensity of 0.

    Code (csharp):
    1.  
    2. #ifndef UNITY_NO_MAIN_DIRECTIONAL
    3. // Calculation of main directional light
    4. #endif
    5.  
     
  4. LukasCh

    LukasCh

    Unity Technologies

    Joined:
    Mar 9, 2015
    Posts:
    102
    K, so currently we don't have this kind of keyword - that indicates if directional light is present in the scene or not. Reason is pretty simple - it is not a common scenario in standard shader to not use directional light. In addition keep in mind that adding new keywords is not free (Keyword limit https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html, also if both variants are used ~2x memory consumption). So in the end this could be more harmful for the rest of users as it only very few would used it.

    In future we plan to make local keyword system in this way we could easily get rid of global keyword limit.

    Any way, you can easily implement this on your project, lets call this keyword UNITY_NO_MAIN_DIRECTIONAL as jv3dc suggested:
    - Add script that tracks how many directional lights in the scene, according it enable/disable your keyword UNITY_NO_MAIN_DIRECTIONAL (https://docs.unity3d.com/ScriptReference/Shader.EnableKeyword.html)
    - In you custom shader or modified standard shader, just #ifdef directional light code
     
  5. lichunHZ

    lichunHZ

    Joined:
    Jun 13, 2017
    Posts:
    3
    Maybe this is the best solution so far. I have to add a keyword by my own and enable or disable it in the script, based on the scene.
     
  6. LukasCh

    LukasCh

    Unity Technologies

    Joined:
    Mar 9, 2015
    Posts:
    102
    Glad that it helps u! If you have any further question related to this forum topic, don't hesitate to ask here.