Search Unity

FIXED Changing Light.cookieSize at runtime no working?

Discussion in 'Scripting' started by strongbox3d, Apr 4, 2020.

  1. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Hello guys,

    I am trying to change the my direction light cookieSize property at runtime but it is not working! I searched in Google and here without any success.

    Could someone shine a light on this? Here is my script attached to the light:

    Code (CSharp):
    1. public class SunRaysToDepth : MonoBehaviour
    2. {
    3.     Light lightSun;
    4.     float minCaustic = 1.0f;
    5.     float maxCaustic = 20.0f;
    6.     float minDepth = 0.3f;
    7.     float maxDepth = 40.0f;
    8.  
    9.     Transform playerTrans;
    10.  
    11.     private void Start()
    12.     {
    13.         lightSun = GetComponent<Light>();
    14.         playerTrans = GameObject.FindWithTag("Player").GetComponent<Transform>();
    15.         lightSun.cookieSize = minCaustic;
    16.         //if (playerTrans != null)
    17.         //{
    18.             InvokeRepeating("UpdateCausticSize", 0.0f, 1.0f);
    19.         //}
    20.     }
    21.  
    22.     void UpdateCausticSize()
    23.     {
    24.         float tempSize = UtilityScript.MapValues(minCaustic, maxCaustic, minDepth, maxDepth, Mathf.Abs(playerTrans.position.y));
    25.         lightSun.cookieSize = tempSize;
    26.     }
    27. }
    Regards,
    Carlos
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Do you get any errors in the console?
    Does the script run?
    What does UtilityScript.MapValues do, and can you verify that it returns different values? Perhaps it returns the same value for some reason?
     
  3. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Hello Suddoha,

    No, as a matter of fact, when I Log using Debug.Log, everything shows the script working but the actual light doesn't change the cookie size even though the log tells me that the cookie has the correct value.

    UtilityScript.MapValue is a method that maps values between min and max outcomes from a min and max income and a current value.

    The use of the script is so as my player move from a min position y to a max position y the cookie size increases from a min size to a max size. Here is the Method:

    Code (CSharp):
    1. public static float MapValues(float minOut, float maxOut, float minIn, float maxIn, float currentIn)//Fish: 5 pound, 20 pounds, 10, 100, 15 ?
    2.     {
    3.         if (currentIn < minIn)
    4.         {
    5.             currentIn = minIn;
    6.         }
    7.         if (currentIn > maxIn)
    8.         {
    9.             currentIn = maxIn;
    10.         }
    11.         return (maxOut - minOut) * ((currentIn - minIn) / (maxIn - minIn)) + minOut;
    12.     }
    Regards,
    Carlos
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I'm seeing this exact thing using a Spotlight. It works if I set the light to Directional, but not for Spotlight.

    I made sure the cookie texture is set up properly (type is cookie, alpha from brightness, etc.), and my cookie shape looks perfect. It just does not respond in any way to changes in the .cookieSize property when the light is a Spotlight.

    Just in case I tried both forward and deferred rendering paths but no difference. I also jacked the quality settings up as high as they go, still no change.

    Can someone who has used light cookies with spotlights weigh in? We must be missing something super-simple here...
     
  5. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Kurt,

    How does "Light.cookieSize" work for you if you set the light to Directional? Do you mean that if you use something like:

    void Start()
    {
    Light.cookieSize = 2.0f;
    }

    works for you if your light is set to Directional? Because that is what is not working for my Directional light! Could you expand on this?

    Regards,
    Carlos
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I can do better than that! Here's my testbed. Give it a whirl. The light is a spotlight in scene, but the code converts it to a directional light, otherwise .cookieSize seems to have no effect.
     

    Attached Files:

  7. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Kurt,

    Thanks for the test. This doesn't work neither. I am working on HDRP, perhaps that is the problem, I see that in HDRP cookies are just default textures no "cookie" when importing the texture to use as cookie. But there most be a way to be able to control the size of the light cookie through scripting. I hope someone can help on this, I really need it.

    Regards,
    Carlos
     
  8. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    I really need to solve this or at least get an answer if this is something that cannot be done. I can't believe that no one have dealt with this problem before! It shouldn't be that difficult to do something as simple as this! "Light.cookieSize" is exposed but un-usable?

    Could someone please help?

    Regards,
    Carlos
     
  9. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Well I figured it out. it happens that in HDRP everything related to lights is different.

    Here is the code that is able to change light properties at runtime when using HDRP:

    Code (CSharp):
    1. using UnityEngine.Rendering.HighDefinition;
    2. using UnityEngine;
    3. using System.ComponentModel;
    4.  
    5. public class SunRaysToDepth : MonoBehaviour
    6. {
    7.     HDAdditionalLightData sunLight;
    8.     public Texture cook;
    9.  
    10.     private void Start()
    11.     {
    12.         sunLight = GetComponent<HDAdditionalLightData>();//Get Reference to the class holding HDRP light properties
    13.         sunLight.intensity = 2.0f;//Change the intensity of the light
    14.         sunLight.SetCookie(cook, new Vector2(1.0f, 1.0f));//Change size of the light cookie
    15.     }
    16. }
    I hope this helps someone.

    Regards,
    Carlos
     
  10. ThomasTheUX

    ThomasTheUX

    Joined:
    Oct 9, 2018
    Posts:
    1
    Awesome thanks! This was just the right hint to solve my issue working on (directional light) cookies with URP.
    If there's someone using URP and light cookies, you can try this solution:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.Universal;
    3.  
    4. public class CookieTest : MonoBehaviour
    5. {
    6.  
    7.     public UniversalAdditionalLightData URPLightData;
    8.     private float size = 10.0f;
    9.  
    10.  
    11.     private void Update()
    12.     {
    13.         size -= Time.deltaTime;
    14.         URPLightData.lightCookieSize = new Vector2(size, size);
    15.     }
    16.  
    17. }

    Additionally, add the Universal Additional Light Data Script (which should automatically be attached to your light) to this script in the inspector.
     
    Pecokis and Radivarig like this.