Author Topic: Using NGUI on RenderTextures for ingame UI panels  (Read 8273 times)

snlehton

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 6
    • View Profile
Using NGUI on RenderTextures for ingame UI panels
« on: April 30, 2014, 04:18:52 AM »
Heya,

I was investigating of using NGUI with RenderTextures, and while I got it working quite well (UIRoot with FixedSize), this doesn't work unless the rendertexture size matches the actual ingame camera pixel size.

Further more, if you want to have ingame panels such as terminals in FPS's or similar done with NGUI, it won't work at all.

This is all due to the fact that NGUI assumes that the input (pixels in the ingame camera) can be directly translated to the world space using the UICamera. Please correct me if I'm wrong.

What I was able to do with minor modifications is a small proof-of-concept how to get NGUI to render to a RenderTexture and display that on a quad in the world, and be able to access the panel using mouse. I only needed to modify the way UICamera does raycasting, and replace that with a RaycastConversion delegate that does the conversion from screen space coordinates to viewport coordinates for that particular UICamera. Then I implemented a special delegate that does the conversion using a transform placed in the world (quad with the rendertexture). I was even able to add obstruction checks to make it so that the panel doesn't get input if the mouse is obstructed by something.

Is this done before, or is this something that could/should be implemented directly into NGUI?

The funny thing is that I didn't even need the ingame panels, I just though they would be nice thing to implement out of curiosity :) What I really needed was a rendertarget which is not the same size as the game viewport.

I can supply my modifications or even an youtube video if needed...

Cheers,
Sampsa / Draconus

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using NGUI on RenderTextures for ingame UI panels
« Reply #1 on: April 30, 2014, 05:13:24 PM »
Quote
doesn't work unless the rendertexture size matches the actual ingame camera pixel size
Sure it does. It just won't necessarily be crisp. Not much can be done about that, it's the same with any texture that you stretch or shrink.

Input won't work with render textures, because when you draw a texture in NGUI you do it using a UITexture, which is a single widget. If you want to make it possible to click on something inside a rendered texture, then you need to create logic for that. Add some script to your UITexture object that will listen for events you want -- such as click, and will then do a raycast using the render texture's camera, hitting the actual button.

FelixSchl

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Using NGUI on RenderTextures for ingame UI panels
« Reply #2 on: May 08, 2014, 02:46:51 PM »
snlehton, I would be very interested to see your modifications to achieve this. A simple pastebin dump should suffice, no need for a youtube video.

snlehton

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 6
    • View Profile
Re: Using NGUI on RenderTextures for ingame UI panels
« Reply #3 on: May 13, 2014, 08:16:19 AM »
UICamera.cs changes: http://pastebin.com/Lg1DW6Q1
RenderTextureTest.cs: http://pastebin.com/qfUfJBc5

Put RenderTextureTest.cs in the UIRoot object and setup accordingly:
screen: A Unity quad containing material which has a render texture as its texture (I used 512x256 rendertexture asset, but it can be made runtime too)
ingameCamera: The camera which is used to render the ingame view (= the camera that render the quad above on the screen)
check obstruction: use this to do raychecks from the ingame camera to the quad before allowing the input forward.

Web player demo: http://sampsy.com/blog/NGUIRenderTexture.html

Use WASD and mouse aim... cursor can get offscreen which is annoying but it's just a quick demo.

Hope this helps.