Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by exerion · Oct 05, 2012 at 02:55 PM · c#rendertexturedrawtextureblit

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! :)

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SolidSnake · Oct 05, 2012 at 03:02 PM 0
Share

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

avatar image SolidSnake · Oct 05, 2012 at 03:03 PM 0
Share

http://docs.unity3d.com/Documentation/Components/class-RenderTexture.html

avatar image SolidSnake · Oct 05, 2012 at 03:10 PM 0
Share

and here is an example of using blit: http://docs.unity3d.com/Documentation/ScriptReference/Graphics.Blit.html

avatar image exerion · Oct 05, 2012 at 10:33 PM 0
Share

SolidSnake: Thank you for your replies :) I have updated my question to try make clearer what I'm trying to achieve.

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.
     }
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AgentFire · Aug 16, 2014 at 02:19 PM 0
Share

Is this only for Unity PRO?

avatar image
0

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:
working.jpg

This is what I get:
broken.jpg

Whats even worse is that the position and scale of my stamp changes depending on the size and aspect of my Unity window?!?!


broken.jpg (38.6 kB)
working.jpg (28.5 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Graphics.Blit() results in empty RenderTexture 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Prevent Render Texture clearing 1 Answer

Need Help Getting RenderTexture & Graphics.Blit Working: Boosting Mobile Performance 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges