Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to play a movie texture in a lightprojector cookie.

Discussion in 'General Graphics' started by van-der-Heijden, Aug 17, 2015.

  1. van-der-Heijden

    van-der-Heijden

    Joined:
    Jun 12, 2015
    Posts:
    3
    I'm trying to play a movie file using a spotlight with a projector/light shader attached.
    I'm also playing a video on some mesh which works great. I modified that script a bit to make the light play the movie but I can't seem to find a way to make the "cookie" play it. The scripts is trying to play the mainTexture which is in this case just a plain color. I'm a total noob in Unity so forgive my ignorance ;)

    This is the script

    void Start () {

    MovieTexture mt = (MovieTexture)GetComponent<Projector>().material.mainTexture;

    mt.loop = true;
    mt.Play();
    }

    }
     
  2. van-der-Heijden

    van-der-Heijden

    Joined:
    Jun 12, 2015
    Posts:
    3
    I found the solution myself. Starting a movie is seems a global thing so I made and dummy object with everything disabled except the script that makes the movie play.
     
    theANMATOR2b likes this.
  3. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Just a note for you moving forward: movie textures are expensive, so if you get a big FPS hit, you might want to consider blending between multiple textures instead of using a movie texture.

    Every time you go to sample from a movie texture (which is likely every frame), Unity has to make a texture out of a frame of the video based on how much time has passed, then sample from that texture.
     
  4. Scrabo

    Scrabo

    Joined:
    Dec 6, 2018
    Posts:
    3
    Hi, i see it was years ago- I just started unity some weeks ago. I want to achive the same thing, using videos or image sequences as light cookies. I want to use 60-120 frame long animations. I tried to do this with emissioning videos from an object to another, it looks cool, but thats not i searching for. Blending between 5-8 cookies will do the trick, but I just can't do it right now, even don't know where to start. Do you ever created a working script, or tutorial for this?
     
  5. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Now there is a Unity Video API that allows you to create a "Video Player" component. There are different Render Modes you can use, one of them allows you to output the video to a texture, which you can then use as a cookie.
    upload_2019-10-17_10-0-34.png
     
  6. Julian-Palacios

    Julian-Palacios

    Joined:
    Oct 5, 2015
    Posts:
    3
    Not the first time that I read this solution, but I can't seem to make it work.
    The light just projects a white square cookie with the same ratio of the video.
    Tried both RenderTexture and CustomRenderTexture without success.
    Is it working for you?
     
  7. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I haven't tried it until just now. I've got it working, but there are some extra steps.

    Cookies use the alpha channel of the texture to determine how much light to block. The VideoPlayer component draws fully opaque textures, which means the entire part of the texture covered in video will allow light to pass through. There may be other solutions, but this is what I did to fix this.

    I have 2 RenderTextures. I render the video to the first texture as described previously. From there, I have a simple script on my camera that uses
    Graphics.Blit()
    to copy the first texture to the second texture using a custom material any time that camera is about to render. (I think) it is important that this script is attached to the camera.

    Code (CSharp):
    1. public class Vid2Cookie : MonoBehaviour
    2. {
    3.  
    4.     [SerializeField] RenderTexture videoTexture = null;
    5.     [SerializeField] RenderTexture cookieTexture = null;
    6.     [SerializeField] Material cookieMaterial = null;
    7.  
    8.     void OnPreRender()
    9.     {
    10.         Graphics.Blit(videoTexture, cookieTexture, cookieMaterial);
    11.     }
    12.  
    13. }
    My custom material uses a custom shader that converts RGB color values to an alpha value. This is a basic unlit shader with an added line in the fragment shader portion.

    Code (CSharp):
    1. fixed4 frag (v2f i) : SV_Target
    2. {
    3.     fixed4 col = tex2D(_MainTex, i.uv);
    4.     col.a = (col.r + col.g + col.b) / 3;
    5.     return col;
    6. }
    After that, my Cookie Texture is ready to use.

    upload_2019-10-24_13-52-49.png