Convert texture2d to rendertexture?

Hi all, The game I’m working on needs to take screenshots of the in-game envirnment in a script that are then mapped to Objects like the world like billboards etc. I’m converting my camera rendertextures to texture2d and then doing some color shifting effects. Is there a way to use this texture2d data on a model like a side of a truck or billboard(in a shader) or would this have to be done as a GUI element? Thanks!

You don’t need to convert it.

About all materials etc take Texture objects not Texture2D.

Texture2D like RenderTexture both extend from Texture

But you can’t do Get and SetPixels and other texture operations over an RenderTexture.
How you can actually do a copy of the RenderTexture to another texture?

Now you can assign a RenderTexture to a material, but I don’t know how to take a snapshot or thumbnail and to transfer it to another material’s texture.

simple, you make the rendertexture active and use readpixels.

see the screenshot function on the unifycommunity.com for example

question is: assuming you want to copy it over every time, why the heck would you want to copy it around if you can just use it
you would need a very very special usage of it to even consider copy out (basically you want to write it to disk or network or want to keep an old version of its look around, thats it)

Hi Dreamora.
I want to take a series of miniatures of things that do not appear on the screen, and I need to transfer it to an array for use it with GUI.SelectionGrid, which requires a Texture array as argument, so I can not directly use the RenderTexture.

Because the things I want to capture are not displayed on screen, I think ReadPixels does not apply.

EDIT: Error: Seems Texture2D.ReadPixels actually reads pixels ‘from the currently active RenderTexture’, so I’ll go to try it now.

Thanks a lot.

It seems that to use Texture2D.ReadPixels would have to manage the process of rendering to texture via script.

In my first test seems just to declare the camera.targetTexture in another camera, it becomes active in the viewport (which I do not whant…)

This gets too complicated for one thing that I think should and could be easier…

Is there a simple way to transfer the current frame of a given camera’s RenderTexture Target material’s main texture to another texture?

did you checkout the script on the unifycommunity I mentioned?

The only I can find is this one http://www.unifycommunity.com/wiki/index.php?title=TakeScreenshotInEditor, that it has noting to do with what I need…

you are right, the corersponding one seems to be gone, someone replaced it with a capture screenshot based one.

this one should help

http://answers.unity3d.com/questions/16012/how-to-save-a-picture-take-screenshot-from-a-camera-in-game

1 Like

No way.
Almost working but just declaring the (intended hidden) camera.targetTexture auxiliary camera, and it becomes active in the viewport…

this method does not use anything like hidden camera etc. it uses your active camera, grabs the screen and does not otherwise touch anyhting.

if it does not work, then you have interfering code or a setup within which this can not work.

But this is not what I need.

As I said:

In short, I do not want to make screenshots, but to create a series of thumbnails for display in the GUI via GUI.SelectionGrid in a process invisible for the user.

(And by the way, I reiterate my appreciation for your interest.)

Which is exactly creating screenshots, cause a screenshot is taking a static image of something thats being rendered.
Nobody said that this “screen” technically must be visible to the user for example :slight_smile:

The only real difference is just that you don’t use the function to write it to disk but the rest is 1:1 “take a screenshot” if you grab rendertexture to texture2d

I also want to read the color of a RenderTexture but I get:

MissingMethodException: Method not found: ‘UnityEngine.RenderTexture.GetPixel’.

Any solution?

(Any way to convert a RenderTexture to a Texture2D?
Any way to read the color of a screenpixel?
At least, any way to read the pixelcolor of a raycasthit on an object with a rendertexture?)

The simplest way Ive found to do this is with Graphics.Blt

1 Like

You just necro’d a 3 year old thread.

Since those threads keed appearing in google, I think it’s actually a good thing to answer old threads for future reference.

1 Like

Indeed! I found it very useful today! thanks for answering!

4 Likes

→ 3 years Later!.. so what is the best way to copy Texture2D into RenderTexture?

to clarify: I’m looking for the best way to stream my scene (2 cameras and overlay canvas)
I already have a good UDP streamer mechanism which sends render texture quite good.
currently, I use texture2D.ReadPixels() to capture all rendered objects in the scene
I know… readpixel() is slow and I should use render texture that feeds on the camera… but as I mentioned before - I have 2 cameras and overlay canvas… and I didn’t find a good way to generate one render texture that feeds from all of the 3.
hence readPixel().

Eventually, I do what to convert my full-screen Texture2D and push it as RenderTexture… so how can I convert Texture2D to RenderTextue… or if you have a better Idea, please… appreciate any advice you can give.
Thanks

1 year later… Graphics.Blit (texture2d, rt)

5 Likes