Author Topic: Render single sprite or even widget to RenderTexture  (Read 7621 times)

artfabrique

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 45
    • View Profile
Render single sprite or even widget to RenderTexture
« on: November 23, 2015, 10:31:06 AM »
Good day!
How can i render widget with all children to RenderTexture with alpha?
What is the right approach? Icame with this two but i'm not sure which will work:
1) Create second UI camera and temporary change layer for widget (here comes the problem that layer name bouces back to UIpanels' layer name)
2) Move this widget on coordinates x1000000y1000000 and try to render region

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Render single sprite or even widget to RenderTexture
« Reply #1 on: November 25, 2015, 05:33:07 PM »
NGUI's Prefab Toolbar does this, at edit time even. I recommend checking the code within -- but the idea is simple: create a temporary panel, camera set to use a render texture, instantiate whatever you want rendered into a texture, render it, then destroy the temporary panel, camera and objects.

artfabrique

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 45
    • View Profile
Re: Render single sprite or even widget to RenderTexture
« Reply #2 on: November 25, 2015, 06:26:24 PM »
NGUI's Prefab Toolbar does this, at edit time even. I recommend checking the code within -- but the idea is simple: create a temporary panel, camera set to use a render texture, instantiate whatever you want rendered into a texture, render it, then destroy the temporary panel, camera and objects.
Yep! Doing that right now. But faced a problem on Android/iOS. We are making a component for complex masks created using UISprite etc. wich are calculated once at start (so it's quite eficient) Think of 9slice masks from atlas  sprites etc.

Problem is that masked UITexture with custom shader supports only one UV set (you build a temporary mesh that way and we dont want to hack it).
Resulting rendertexture of mask equals screensize (not square & not power of 2)
Masked UITexture _MainTex & _AlphaTex(from shader) size has to be square and to be power of two on device
So if we dont want to do bilinear\trilinear interpolation on CPU (interpolate rendered mask-widget rect to square pow2 texture2d) we have to change shader and make _AlphaTex to use UV2 so we could manipulate _Alphatex offset&tiling separately from _MainTex.
But.. no UV2 on UITexture and we understand why: small business value + a lot of work.

Maybe you have any other idea? (without NGUI patching/hacking but still elegant?)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Render single sprite or even widget to RenderTexture
« Reply #3 on: November 25, 2015, 06:42:48 PM »
The prefab toolbar creates square preview textures, so I don't quite understand the first part of your post... The only trouble I see is positioning the camera so that it's in the center of the widget, and then adjusting its size so that it fits the widget within, but again the prefab toolbar does this for you so you can use its code as an example.

artfabrique

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 45
    • View Profile
Re: Render single sprite or even widget to RenderTexture
« Reply #4 on: November 25, 2015, 07:45:20 PM »
The prefab toolbar creates square preview textures, so I don't quite understand the first part of your post... The only trouble I see is positioning the camera so that it's in the center of the widget, and then adjusting its size so that it fits the widget within, but again the prefab toolbar does this for you so you can use its code as an example.
Here:

artfabrique

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 45
    • View Profile
Re: Render single sprite or even widget to RenderTexture
« Reply #5 on: November 25, 2015, 07:51:00 PM »
By the way here is teaser of one of 50 ships  :P

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Render single sprite or even widget to RenderTexture
« Reply #6 on: December 01, 2015, 06:27:50 AM »
Whatever game you're making, I want to play it. :)

For something as simple as adding a mask over an existing texture, I'd write a shader. You don't need to do any rendering... say for example you want 300x200 to be masked by a gradient regardless of the texture size -- 200x200, 300x300, 340x240, etc... Use a shader that will calculate this stuff in screen space or view space then calculate the UV2 within using that.