Author Topic: Create Temporary UITexture Instead Of UISprite  (Read 3365 times)

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Create Temporary UITexture Instead Of UISprite
« on: March 06, 2016, 12:15:51 PM »
Hi everyone and ArenMook,

I'm using some custom shaders to add life to my sprites. I need the effect to be working per 1 sprite at a single given time only.

However when my sprites are packed tightly into the atlas, of course there are problems: the shader effect applies to the whole atlas (all of the sprites), it leads to the slow execution and inconsistency of the effect across different sprites.

So I want to avoid the problems by making a temporary UITexture, displaying it right over the top of existing UISprite with the required shader for N seconds and then just disposing of it.

I would really appreciate if someone could help me with the code. I can't wrap my head around this seemingly simple task!

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Create Temporary UITexture Instead Of UISprite
« Reply #1 on: March 06, 2016, 01:14:40 PM »
Eh... why not just create a temporary clone of the atlas and its material, and changing the material's shader? You can then use this cloned atlas instead of the original one for your sprite in question. You will be reusing the atlas texture, so no memory will be wasted.

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Create Temporary UITexture Instead Of UISprite
« Reply #2 on: March 06, 2016, 04:32:56 PM »
The shader applies a certain effect over all 20+ sprites that are inside the atlas, and uses atlas size and dimensions to generate effects.

It leads to the slow execution and inconsistency of the effect across different sprites.

I need the shader effect to work only on certain sprite, with certain dimensions.

Therefore I need to take out a single sprite out of the atlas to make a UItexture out of it, apply shader, display, and then destroy.

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Create Temporary UITexture Instead Of UISprite
« Reply #3 on: March 08, 2016, 03:43:30 PM »
I would really aprecciate some reply!

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Create Temporary UITexture Instead Of UISprite
« Reply #4 on: March 11, 2016, 10:48:15 AM »
You don't need to cut it out into a texture, you just need to calculate the offset to the given sprite you need and do what mook said above.

I mean it obviously depends on what kind of effect you're thinking of, but generally, you could offset the drawing of a secondary texture blended in some way with the atlas texture and just do that.

Otherwise, your option is to use Texture2D's SetPixels and GetPixels and create a new texture from a "cutout" of the atlas. It will just be costly.

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Create Temporary UITexture Instead Of UISprite
« Reply #5 on: March 11, 2016, 02:31:34 PM »

Otherwise, your option is to use Texture2D's SetPixels and GetPixels and create a new texture from a "cutout" of the atlas. It will just be costly.

Are there any other options apart from Get/SetPixels?

Can you share some code that would accomplish the task this way? I understand it's costly, but I need to test this before trying out any other possibilities.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Create Temporary UITexture Instead Of UISprite
« Reply #6 on: March 12, 2016, 08:05:03 AM »
An atlas texture is just that -- a texture. An atlas in itself simply knows where sprites are within the big atlas texture. You can get the sprite from the atlas, calculate its UV coordinates from the sprite's position, width and height, and then draw it using a UITexture with whatever shader you want to apply on it.