- Home /
Trying to use Graphics.DrawTexture() to modify my texture
I'm trying to build a texture at runtime by doing the following in C#:
- Create a GameObject
- Add a camera component to the GameObject
- Create a RenderTexture and assign it to camera.targetTexture
- Blit a background image into the RenderTexture
- Add a script to the GameObject (more on script below)
- Call camera.Render()
My script.OnRenderObject() does a Graphics.DrawTexture() to draw a texture into a small area of the render texture.
My script.OnPostRender() does the following:
- Texture2D.ReadPixels() to copy the RenderTexture into a new texture I will use later.
- Texture2D.Apply()
The result is that I see the background image from the Blit call, but the DrawTexture image never shows up. Does DrawTexture not work within a Camera.Render() call?
Some other possibilities that I've tried (unsuccessfully):
- Tried WaitForEndOfFrame in case the DrawTexture calls were still queued up and hadn't executed before OnPostRender() gets called.
- I'm outputting log messages in OnObjectRender, so I know it's getting in there.
Any ideas or better solutions to my problem? I would post code, but it's a bit long.
Thanks.
Answer by Brian Stringham · Oct 18, 2010 at 10:26 PM
Well, I never found a proper solution for this, so I ended up having to use Graphics.Blit to create a scaled version of the 'decal' texture, and then manually copy and alpha-blend it into the destination texture using SetPixels.
It's a lot less elegant and I can't do arbritrary rotations, but it does the bare minimum functionality I need. (sigh)
Your answer
Follow this Question
Related Questions
How to make foot to leave a print on snow? 1 Answer
GUI.DrawTexture shows in Unity but not in Build 1 Answer
GUITexture makes my image to low quality 1 Answer
How can I trace Alphabet 0 Answers