Search Unity

Render Camera view to UI

Discussion in 'Immediate Mode GUI (IMGUI)' started by auzette, Dec 23, 2009.

  1. auzette

    auzette

    Joined:
    Mar 23, 2009
    Posts:
    74
    Hi. I have a task before me to be able to put a camera in the world, put a character model in front of it, render it to a texture, then display that texture using GUI.DrawTexture().

    I haven't had much luck getting all the pieces put together to make this happen. My latest attempt crashes the editor. :(.

    So I have a camera in the world, and a model in front of it. That's no biggie. I added a GUITexture to the scene, but it won't allow itself to be dropped onto the cameras target texture.

    So, I create a rendertexture programmatically, and assigned it to the camera.

    Now, how do I display it? From what I've read, the ReadPixels takes whatever is the current camera. How can I tell which camera is the one I'm interested in (I have a main camera that renders the full view as well).

    Ultimately, the point of this is to create a paperdoll view, where i load a model in the world, and render it to a texture that is displayed in the UI (with buttons to rotate the model, etc.)

    Am I on the right track? Is there a better way?

    Thanks for any insight.
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    First off I'd like to ask why this needs to be rendered to a texture, can't you simply use a second camera that's limited to the desired display region? That way you can do all the rotation and whatnot without the use of a texture.

    If you must use a texture and want to use a GUITexture:

    1. Create a render texture (Assets > Create > Render Texture...)

    2. Assign that render texture to the camera in question (select camera, drag assign the render texture as the camera's target texture)

    3. Assign the render texture as the texture to be displayed by your GUITexture (select GUITexture, drag render texture to the 'texture' slot).


    You need to keep in mind that a "GUITexture" is _not_ a texture itself, think of it as a container for showing a texture and that's why you can't assign it directly as the target of the camera.


    Or, if you're going to use GUI.DrawTexture (in which case no GUITexture should be used at all) then just do items #1 and #2 above then when you draw the texture draw the render texture and you should be good to go.
     
    sumit47 likes this.
  3. burtonposey

    burtonposey

    Joined:
    Sep 8, 2009
    Posts:
    62
    Hey HiggyB,

    I think my issue is closely related to the strategy you're suggesting. I'm trying to develop a GUI system where I render out my GUI elements to a render texture and then get them up front an center in Unity. I thought the way to approach this was to use GUITexture. I can see from your reply that there might be a better way to do this.

    I've got the render texture working, but in my current implementation my main camera doesn't render the render texture when I change a default GUITexture to my Render Texture.

    I could definitely use some guidance. I'm trying to figure out the most efficient way to render a GUI to the screen. I've already got a good strategy for detecting region clicks on a single texture (my GUI).

    Thanks
     
  4. burtonposey

    burtonposey

    Joined:
    Sep 8, 2009
    Posts:
    62
    I figured out what the problem was. The render texture wasn't working with transparency when assigning it to a GUITexture. When I changed it back to diffuse it has transparency on the GUITexture. Not sure why that would work, but I accept that it does, :).
     
  5. sendel76

    sendel76

    Joined:
    Mar 17, 2010
    Posts:
    36
    I need it just the other way round. I want to render the GUI into a RenderTexture. Is that possible?
     
    twhittaker likes this.