- Home /
Graphics.DrawTexture on RenderTexture not working...?
Hello :) I have just started learning Unity and C# so please be gentle.
I'm trying to make changes to a RenderTexture which I have set to be the "_MainTex" of my object. I want to use my RenderTexture "rt" as a buffer to hold a Texture2D "texture" and use Graphics.DrawTexture to draw my Texture2D "stampTexture" on my RenderTexture "rt". I thought I'd be able to do it like this...
using UnityEngine;
using System.Collections;
public class Test: MonoBehaviour {
public Texture2D texture; //Starting image.
public Texture2D stampTexture; //Texture to Graphics.Drawtexture on my RenderTexture.
public RenderTexture rt; //RenderTexture to use as buffer.
public int posX; //Position the DrawTexture command while testing.
public int posY; //Position the DrawTexture command while testing.
void Start() {
renderer.material.SetTexture("_MainTex", rt); //Assign my RenderTexure to be the main texture of my object.
RenderTexture.active = rt; //Set my RenderTexture active so DrawTexture will draw to it.
Graphics.Blit(texture, rt); //Blit my starting texture to my RenderTexture.
}
void Update() {
//Draw my stampTexture on my RenderTexture.
Graphics.DrawTexture (new Rect (posX,posY,stampTexture.width,stampTexture.height),stampTexture);
}
}
...but either the Graphics.DrawTexture is not working or the RenderTexture is not displaying the changes. Can someone please help me out.
Thanks! :)
if you simply want to use rendertexture on an object then make sure to attach rendertexture to a camera then create material that uses that and attach the material to your object and everything is done for you...unless i misunderstood what you are trying to achieve
and here is an example of using blit: http://docs.unity3d.com/Documentation/ScriptReference/Graphics.Blit.html
SolidSnake: Thank you for your replies :) I have updated my question to try make clearer what I'm trying to achieve.
Answer by exerion · Oct 22, 2012 at 08:59 PM
I got it working thanks to this post. Now my DrawTexture works and is positioned as expected. Moving RenderTexture.active into Update() and the GL functions are what made it work.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public Texture2D texture; //Starting image.
public Texture2D stampTexture; //Texture to Graphics.Drawtexture on my RenderTexture.
public float posX = 256f; //Position the DrawTexture command while testing.
public float posY = 256f; //Position the DrawTexture command while testing.
RenderTexture rt; //RenderTexture to use as buffer.
void Start ()
{
rt = new RenderTexture (512, 512, 32); //Create RenderTexture 512x512 pixels in size.
renderer.material.SetTexture ("_MainTex", rt); //Assign my RenderTexure to be the main texture of my object.
Graphics.Blit (texture, rt); //Blit my starting texture to my RenderTexture.
}
void Update ()
{
RenderTexture.active = rt; //Set my RenderTexture active so DrawTexture will draw to it.
GL.PushMatrix (); //Saves both projection and modelview matrices to the matrix stack.
GL.LoadPixelMatrix (0, 512, 512, 0); //Setup a matrix for pixel-correct rendering.
//Draw my stampTexture on my RenderTexture positioned by posX and posY.
Graphics.DrawTexture (new Rect (posX - stampTexture.width / 2, (rt.height - posY) - stampTexture.height / 2, stampTexture.width, stampTexture.height), stampTexture);
GL.PopMatrix (); //Restores both projection and modelview matrices off the top of the matrix stack.
RenderTexture.active = null; //De-activate my RenderTexture.
}
}
Answer by exerion · Oct 20, 2012 at 09:56 AM
Moving the RenderTexture.active = rt; line into Update() made this script partially work...
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public Texture2D texture; //Starting image.
public Texture2D stampTexture; //Texture to Graphics.Drawtexture on my RenderTexture.
public RenderTexture rt; //RenderTexture to use as buffer.
public int posX = 256; //Position the DrawTexture command while testing.
public int posY = 256; //Position the DrawTexture command while testing.
void Start()
{
renderer.material.SetTexture("_MainTex", rt); //Assign my RenderTexure to be the main texture of my object.
Graphics.Blit(texture, rt); //Blit my starting texture to my RenderTexture.
}
void Update()
{
RenderTexture.active = rt; //Set my RenderTexture active so DrawTexture will draw to it.
//Draw my stampTexture on my RenderTexture.
Graphics.DrawTexture(new Rect(posX - (stampTexture.width / 2), posY - (stampTexture.height / 2), stampTexture.width, stampTexture.height), stampTexture);
}
}
I still have two problems. DrawTexture draws my stampTexture on my RenderTexture but it is squashed and I can't make any sense of the coordinates required to position my stampTexture.
This is what I would expect my RenderTexture to like if my RenderTexture was 512x512 and I set posX and poxY to 256:
This is what I get:
Whats even worse is that the position and scale of my stamp changes depending on the size and aspect of my Unity window?!?!
Your answer
