- Home /
is using OnPostRender to draw textures Pro only?
Im trying to draw a texture on screen using Graphics.DrawTexture, but I am getting no results. but if i were to use it in OnGUI it suddenly decides to draw the texture.
void OnPostRender()
{
DrawGUI();
}
IEnumaertaor DrawGUI() {
yeild return new WaitForEndOfFrame;
Graphics.DrawTexture(new Rect(0,0,128,128),MyTexture);
}
this does nothing...its currently unknown to me why but...
void OnGUI()
{
if (Event.current.type == EventType.repaint)
Graphics.DrawTexture(new Rect(0,0,128,128),MyTexture);
}
this does...works great infact, however it called many times a frame making it a waste to use. I tried Google only finding unrelated answers, tried adding the script to a camera, also tried removing the "if" block. can someone explain why is this happening? (or why unity needed to remove this from basic)
EDIT I've just realized my GUI was drawing but in the stupidest possible manner, it wasn't drawing on screen but based on the coordinated of the Game_manager(which manages GUI and scene transition and other things). lol, i watched my scene view to see the giant 128x128(level units) texture floating behind the player lol. I have no clue on fixing this.... would camera.WorldtoVeiwportPoint be a starter? ][2]][1]
[2]: /storage/temp/10475-unity+2013-04-27+23-42-48-020.jpg
tried that it wont work, I don't what Unity is drawing over but its happening in OnRenderImage (clearing image effects for non-pro users?).
Answer by Fornoreason1000 · May 13, 2013 at 06:04 PM
i stumbled upon a solution to use Graphics.DrawTexture in PostRender randomly through testing. simply put you need to setup Matrices to stop it from drawing in World space which only happens outside OnGUI, I'm not sure why that happens. Anyway here is how to fix it, some sample code:
GL.PushMatrix();
GL.LoadPixelMatrix();
Graphics.DrawTexture ( new Rect (0,0,300,300), MyTexture, 2, 2, 2, 2, null);
GL.PopMatrix();
this only works if its attached to the main camera or the camera with the upper depth. it also need to be redrawn after every frame. turns out it had nothing to do with co routines or using the wrong type of coordinates. whether this method is safe or not performance wise is to be determined. hope it helps other users
Answer by xortrox · Apr 26, 2013 at 11:56 PM
"OnPostRender is called after the camera renders all its objects. If you want to do something after all cameras and GUI is rendered, use WaitForEndOfFrame coroutine."
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnPostRender.html
Basically what could be happening is that something is getting drawn ontop of your texture for not using the WaitForEndOfFrame coroutine.
http://docs.unity3d.com/Documentation/ScriptReference/WaitForEndOfFrame.html
thanks... I would up vote, but this petty moderation thing just tells me im not allowed to. lol, not to mention it takes forever to help others. Unfortunately that doesn't seem to help...since the texture was there but in 3d space not screen space hang out behind the player
for future readers simply TIC$$anonymous$$ if you are a new user and the answer is correct
Answer by whydoidoit · Apr 27, 2013 at 07:08 AM
Putting event code in OnPostRender isn't possible - it only works in OnGUI and functions it calls. OnPostRender is called to let you do stuff with a render texture - so isn't much use on Free as you can't get one.
Graphics.DrawTexture and GUI.DrawTexture both work in screen coordinates. So if you want the screen coordinates of a world object you use camera.WorldToScreenPoint.
BTW if you only draw your texture in OnGUI when Event.current.type == EventType.repaint then the texture draw will only happen once per frame.
can i show a pic of whats happening? cause Graphics.DrawTexture is not screen space (not in this case anyway). it draws it in the world at the coordinate (0,0) and a scale of 128(don't know where the z goes) I know its not supposed to happen, but it is...
I think you might be wrong about how Graphics.DrawTexture works.
"Draw a texture in screen coordinates.
If you want to draw a texture from inside of OnGUI code, you should only do that from EventType.Repaint events. It's probably better to use GUI.DrawTexture for GUI code."
As for why there is no z.
I believe you are using world coordinates to screen coordinates to position the texture making you think it would "enter" the world space yet it is still in screen space, could you provide an image of the issue?
http://docs.unity3d.com/Documentation/ScriptReference/Graphics.DrawTexture.html
see the weird thing is if i put the same function in ONGUI the texture appears (0,0) on the screen A$$anonymous$$A top left corner, but OnPostRender it appears a giant plane.
Could this be a solution?
http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTexture.html
Your answer
Follow this Question
Related Questions
GUI Label "flash a Arrow" 1 Answer
C# GUI Button 2 Answers
Show texture when clicking left mouse button 0 Answers
IGUI IOS error 1 Answer
I'm trying to get a picture in game and display it on a plane 2 Answers