Search Unity

Animated cookie textures

Discussion in 'Editor & General Support' started by DerWoDaSo, Aug 25, 2009.

  1. DerWoDaSo

    DerWoDaSo

    Joined:
    May 25, 2009
    Posts:
    131
    Just wondering... is there any way to create animated cookie textures?

    Cheers,

    Jan
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Not directly, instead you might try creating a script routine that "flipped" cookie textures to create that effect.
     
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Light.cookie is of type Texture, so MovieTextures should work there.
     
  4. DerWoDaSo

    DerWoDaSo

    Joined:
    May 25, 2009
    Posts:
    131
    But if I create a movie texture and apply it to the Cookie property in the Inspector, that would be the same, right? ...because this doesn't work for me. :(
     
  5. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    That was my point, I don't believe Movie Textures can be applied as light cookies. Hence the suggestion for you to look at manually flipping through a series of textures yourself. Give it a go and see if it works!
     
  6. DerWoDaSo

    DerWoDaSo

    Joined:
    May 25, 2009
    Posts:
    131
    Yepp, I will try that... thanks!
     
  7. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Is there a reason for this? Usually when properties are Texture rather than just Texture2D, we can use MovieTextures, etc. in them.
     
  8. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Light cookie textures have specific requirements IIRC, thus the limitation that I think is there. :)
     
  9. DerWoDaSo

    DerWoDaSo

    Joined:
    May 25, 2009
    Posts:
    131
    If any one needs... I wrote a litte script that lets you "animate" a light cookie.

    Just put it onto a light, put in a series of different cookieTextures, choose framerate and playback mode and here you go.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AnimateCookieTexture : MonoBehaviour {
    5.  
    6.     public enum AnimMode
    7.     {
    8.         forwards,
    9.         backwards,
    10.         random
    11.     }
    12.  
    13.     public Texture2D[] textures;
    14.     public float fps = 15;
    15.  
    16.     public AnimMode animMode = AnimMode.forwards;
    17.  
    18.     private int frameNr = 0;
    19.     private Light cLight;
    20.  
    21.     void Start () {
    22.         cLight = GetComponent(typeof(Light)) as Light;
    23.         if (cLight == null)
    24.         {
    25.             Debug.LogWarning("AnimateCookieTexture: No light found on this gameObject", this);
    26.             enabled = false;
    27.         }
    28.  
    29.         StartCoroutine("switchCookie");
    30.     }
    31.  
    32.  
    33.  
    34.     IEnumerator switchCookie()
    35.     {
    36.         while (true)
    37.         {
    38.             cLight.cookie = textures[frameNr];
    39.  
    40.             yield return new WaitForSeconds(1.0f / fps);
    41.             switch (animMode)
    42.             {
    43.                 case AnimMode.forwards: frameNr++; if (frameNr >= textures.Length) frameNr = 0; break;
    44.                 case AnimMode.backwards: frameNr--; if (frameNr < 0) frameNr = textures.Length - 1; break;
    45.                 case AnimMode.random: frameNr = Random.RandomRange(0, textures.Length); break;
    46.             }
    47.         }
    48.     }
    49.    
    50. }
    51.  
     
    Karleen, Alanmco and 265lutab like this.
  10. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I'm still curious as to what the specific requirements are. If it's just that they have to be Texture2Ds, it would be nice if the property had the specific type.
     
  11. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    The docs (Light Cookie) are but a short reach away and they state:

    1. Enable Border Mipmaps

    2. Enable Build Alpha From Grayscale (this way you can make a grayscale cookie and Unity converts it to an alpha map automatically)

    3. Set the Texture Format to Alpha 8 Bit


    Movie textures don't support those options.
     
  12. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Hey can you help me a little. I am trying to get that script to work but i get that error in Unity: Insert a semicolon at the end.
     
  13. salex1

    salex1

    Joined:
    Jul 9, 2015
    Posts:
    29
    Great script DerWoDaSo! I use it for 2.5D game project where you need to animate the shadows on windows :)
     
    DerWoDaSo likes this.
  14. NeatWolf

    NeatWolf

    Joined:
    Sep 27, 2013
    Posts:
    924
    Hi there!

    9 years after, is there any native way or workaround to render even a simple shader-based cookie for a light - with the specific intent to mimic the irregularities of the flame of a torch?
     
  15. DerWoDaSo

    DerWoDaSo

    Joined:
    May 25, 2009
    Posts:
    131
    9 years after ... and I stumble upon my own posts again.

    You can setup a RenderTexture and use that as cookie which is updated every frame or when ever you need it. I do that for cloud shadows at the moment.
     
    NeatWolf likes this.
  16. PxoGeorgSochurek

    PxoGeorgSochurek

    Joined:
    Jan 22, 2019
    Posts:
    7
    @DerWoDaSo , I'm currently trying to do the same (RenderTexture as cookie), but it won't update accordingly, just once when I delete the assigned cookie and re-assign the RenderTexture. When I use the RT in any other way (e.g. in a Material), it works fine.
    I tried all the RT's settings, but nothing changed that (tried in 2018.2 with "old" Render Workflow, and in 2018.3 with HDRP).
    Do you have any advice? Thanks a lot!
     
  17. Kareeem

    Kareeem

    Joined:
    Mar 1, 2013
    Posts:
    37
    This script is awsome and it still works! I wasnt able to get the Render to texture thing working...

    THANKS!
     
    Karleen likes this.
  18. Karleen

    Karleen

    Joined:
    Apr 15, 2019
    Posts:
    1
    Thanks DerWoDaSo !
    The script works perfectly !
     
  19. smetzzz

    smetzzz

    Joined:
    Mar 24, 2014
    Posts:
    145
    Gah, I had a render texture animated as a cookie working beautifully a few versions of Unity ago. Now it doesn't :(
     
  20. Panther202

    Panther202

    Joined:
    Nov 21, 2017
    Posts:
    7
    Hey,
    I do have the same problem. On objects the animated texture works fine but as a Spotlight Cookie it doesn't move at all. Is there any solution? Working in HDRP 2019.2.1