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
3
Question by tbugrara · Jun 29, 2012 at 11:57 PM · shadertexturematerialtexture2drendertexture

Converting a RenderTexture to a Texture2D for use in a shader

I'm getting a Render Texture from a second camera and using it in a shader to find some alpha values in the current camera view. In OnRenderImage, I convert the RenderTexture to a simple Texture2D and set the material's texture to this newly created Texture2D. This works perfectly and I get the effect I want.

However, its really expensive to convert a RenderTexture to a Texture2D every single frame so I would like to just set the material's texture to the RenderTexture itself. This however, does not give the effect I want and I have no idea why.

What is so different between the RenderTexture's pixels and the Texture2D's pixels? Is there an better way to convert the RenderTexture into a Texture2D?

EDIT: Here's how I'm converting it now:

 void OnRenderImage(RenderTexture source, RenderTexture dest)
 {
     if (mat.HasProperty("_RenderPaintTexture"))
     {
         RenderTexture.active = source;
         Texture2D texture = new Texture2D(source.width, source.height, TextureFormat.ARGB32, false);
         texture.ReadPixels(new Rect(0, 0, source.width, source.height), 0, 0);
         texture.Apply();
         mat.SetTexture("_RenderPaintTexture", texture);
     }
 }
Comment
Add comment · Show 3
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 whydoidoit · Jun 29, 2012 at 11:58 PM 0
Share

How are you converting it at the moment?

avatar image tbugrara · Jun 30, 2012 at 12:17 AM 0
Share

Just updated the question with some code.

avatar image misichi · Aug 26, 2014 at 05:26 AM 0
Share

Ah, i am having the same roadblock now. I wonder if you've solved this already? :O

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tbergelt · Apr 19, 2018 at 07:41 PM

I'm not sure of the different between RenderTexture and Texture2D but Texture2D.Apply suggests that if you only need the texture on the GPU and not on the CPU then Graphics.CopyTexture would be faster.

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
avatar image
-2

Answer by BigBlob · Jun 30, 2012 at 12:19 AM

Just try setting the camera to Orthographic to set everything in 2D

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 tbugrara · Jun 30, 2012 at 03:58 PM 1
Share

I tried this and it did not work. Let me know if you need more information about the problem.

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

8 People are following this question.

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

Related Questions

How to update texture during runtime to display damage? 0 Answers

Get Texture 2D after all shader passes are applied 0 Answers

How can i get mixed texture from one shader and set it for another object with another shader? 0 Answers

Rendering a G-Buffer for a cubemap 0 Answers

(c#) Transparency Problem - Toggle not Gradient 1 Answer


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