Search Unity

Light Probes. "swaping to a different pre-baked one at runtime."

Discussion in 'Global Illumination' started by SuperUltraHyper, Mar 20, 2016.

  1. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    The Manual says (Light Probes) "can be swapped to a different pre-baked one at runtime."

    http://docs.unity3d.com/ScriptReference/LightmapSettings-lightProbes.html

    How is this done? gameObject.GetComponent<LightProbes>(); doesn't return the light probes of the attached gameObject. and making a public LightProbes and manually assigning it in the editor doesn't do it either. Any help?

    My current script:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4.  
    5. public class LightProbeTest : MonoBehaviour {
    6.  
    7.     LightProbes lPGroup1;
    8.     LightProbes lPGroup2;
    9.  
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.         SceneManager.LoadSceneAsync("LP_Test1", LoadSceneMode.Additive);
    14.         SceneManager.LoadSceneAsync("LP_Test2", LoadSceneMode.Additive);
    15.  
    16.         StartCoroutine(WaitForLoad());
    17.     }
    18.  
    19.     IEnumerator WaitForLoad()
    20.     {
    21.         yield return new WaitForSeconds(2);
    22.  
    23.         GameObject go1 = GameObject.Find("LP_1");
    24.         GameObject go2 = GameObject.Find("LP_2");
    25.         lPGroup1 = go1.GetComponent<LightProbes>();
    26.         lPGroup2 = go2.GetComponent<LightProbes>();
    27.  
    28.         Debug.Log("Light Probes GO = "+go1.name+", "+go2.name);
    29.         Debug.Log("Light Probes = " + lPGroup1.name+", "+ lPGroup2.name);
    30.  
    31.         StartCoroutine(SwapLightProbes());
    32.     }
    33.  
    34.     IEnumerator SwapLightProbes()
    35.     {
    36.         Debug.Log("Group1 " + lPGroup1.count +", "+ lPGroup1.bakedProbes);
    37.         LightmapSettings.lightProbes = lPGroup1;
    38.         Debug.Log("LightmapSettings = " + LightmapSettings.lightProbes);
    39.         yield return new WaitForSeconds(1);
    40.  
    41.         Debug.Log("Group2 " + lPGroup2.count + ", " + lPGroup2.bakedProbes);
    42.         LightmapSettings.lightProbes = lPGroup2;
    43.         Debug.Log("LightmapSettings = " + LightmapSettings.lightProbes);
    44.         yield return new WaitForSeconds(1);
    45.  
    46.         StartCoroutine(SwapLightProbes());
    47.  
    48.     }
    49. }
    50.  
     
  2. mholub

    mholub

    Joined:
    Oct 3, 2012
    Posts:
    123
    It is because LightProbes object is not component (doesn't inherit from http://docs.unity3d.com/ScriptReference/Component.html) so it is not attached to gameobject. It is scene data, not single object.
    I assume you should write editor script, which gets LightProbes from LightmapSettings.lightProbes getter in editor, than store it somewhere (array of LightProbes objects in SerializedObject instance) and then during playmode you could swap lightprobes with LightmapSettings.lightProbes setter
     
  3. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    Ah. Thanks for the tip.
     
  4. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    Another question then... Why does this code print every coefficient as 0? (I am trying to serialize the LightProbes but nothing is [Serializable] (LightProbes or ShericalHarmonicsL2) so I am attempting to do it manually)

    Code (CSharp):
    1.         for (int c = 0; c < LightmapSettings.lightProbes.count; c++)
    2.         {
    3.             for (int i = 0; i < 3; i++)
    4.             {
    5.                 for (int j = 0; j < 9; j++)
    6.                 {
    7.                     Debug.Log(c + ","+ i + "," + j + " = " + LightmapSettings.lightProbes.bakedProbes[c][i, j]);
    8.                 }
    9.             }
    10.         }