Search Unity

About render to texture feature

Discussion in 'Unity Everyplay' started by omerfarukz, Dec 19, 2015.

  1. omerfarukz

    omerfarukz

    Joined:
    Jun 28, 2013
    Posts:
    5
    Hi,
    I am trying to render facecam into a sprite or image(2d) object. I read documentation and described codes applied. But if i start faceacam it shown a normal position instead of sprite. Can you share any code sample to me?

    My code is below

    Texture2Dtexture=newTexture2D(128,128,TextureFormat.BGRA32,false);
    texture.wrapMode=TextureWrapMode.Repeat;

    Everyplay.FaceCamSetTargetTexture(texture);

    MySprite.GetComponent<SpriteRenderer>().material.mainTexture=texture;

    Everyplay.FaceCamStartSession();​

    Thanks
     
  2. ullatussimo

    ullatussimo

    Unity Technologies

    Joined:
    Jun 16, 2015
    Posts:
    108
    Hello @omerfarukz,

    Thanks for reaching out!

    I would recommend you to use quads for this.
    Simply create a material, assign it to a quad and then in code create a new Texture2D (just as you have done already) and assign that texture to the quad's material.

    Runtime conversion from Texture2D = > Sprite would not work, as you would have to explicitly convert every single frame, leading to bad performance.

    If you instead wish to use the Unity's UI objects for this, I suggest you go with Raw Image, as it accepts Texture2D instead of Sprite.

    Your code looks mostly correct for both cases, but for the RawImage approach you need to access the texture through the RawImage script:
    MyRawImage.GetComponent<RawImage>().texture = texture;

    For the quad approach, your code should work if you simply do the following change:
    MyQuad.GetComponent<Renderer>().material.mainTexture=texture;

    Hope this helps!

    Cheers,
    Simo