c# - Getting a Texture2D from a RenderTarget2D object in XNA 4.0 -


im experimenting pixel shader. found nice blur effect , im trying create effect of blurring image on , over.

how want that: want render image hellokittytexture in rendertarget applying blur effect, replace hellokittytexture result of render , on , on again every draw iteration:

protected override void draw(gametime gametime)     {          graphicsdevice.clear(color.cornflowerblue);           graphicsdevice.setrendertarget(buffer1);          // begin sprite batch, using our custom effect.         spritebatch.begin(0, null, null, null, null, blur);         spritebatch.draw(hellokittytexture , vector2.zero, color.white);         spritebatch.end();         graphicsdevice.setrendertarget(null);          hellokittytexture = (texture2d) buffer1;          // draw texture in screen         spritebatch.begin(0, null, null, null, null, null);         spritebatch.draw(hellokittytexture , vector2.zero, color.white);         spritebatch.end();          base.draw(gametime);     } 

but error "the render target must not set on device when used texture." because hellokittytexture = (texture2d) buffer1; not copying texture reference rendertarget (basicly same object after asignation)

do know nice way texture inside rendertarget? or more elegant way im trying?

    spritebatch.draw(hellokittytexture , vector2.zero, color.white); 

in line, you're drawing texture... itself... can't happen.

assuming buffer1 , hellokittytexture have been initialized, replace line:

    hellokittytexture = (texture2d) buffer1; 

with this:

        color[] texdata = new color[hellokittytexture.width * hellokittytexture.height];         buffer1.getdata(texdata);         hellokittytexture.setdata(texdata); 

this way, hellokittytexture set copy of buffer1, rather pointer it.


Comments