Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Isan · Oct 11, 2017 at 10:00 PM · shadertexture2drendertextureshader programmingpingpong

Most Efficient way to save shader result as texture and feed it back in next frame (Ping Pong Buffer)

I've done some fun work with Shaders in Open Frameworks by taking the output of the shader as a texture and feeding it back into the shader on the next frame as a sampler2D. This allows for interesting effects involving devices like cellular automata.

Now I am trying to replicate this effect in Unity, but wanted to check if my current method is good or if there's some glaring flaw in efficiency people more experienced in graphics can see. I'd also just like to post this code since I had a hard time finding this exact problem. I am specifically doing this on the camera for the purpose of screen effects and took the foundations from Zucconi and Lammers's book.

Specifically, I am after every rendering reading the pixels of the current active texture (which appears to be destTexture, though I am not certain) onto a global Texture2D variable. I am then feeding this in as a texture to the shader on the next pass.

 Texture2D samp;
 void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture){
         if (curShader != null) {
             if (samp == null) {
                 samp = new Texture2D(sourceTexture.width, sourceTexture.height);
                 RenderTexture.active = sourceTexture;
                 samp.ReadPixels(new Rect(0, 0, sourceTexture.width, sourceTexture.height), 0, 0);
                 samp.Apply ();
             }
             material.SetTexture("_CurTex", samp);
             Graphics.Blit (sourceTexture, destTexture, material);
             samp.ReadPixels(new Rect(0, 0, sourceTexture.width, sourceTexture.height), 0, 0);
             samp.Apply ();
         } else {
             Graphics.Blit (sourceTexture, destTexture);
         }
     }

Thanks in advance for any help.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

In reading about Texture2D.Apply it 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

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

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

Related Questions

Converting c# function to shader. 0 Answers

How do I make a paint trail effect in ShaderGraph? Are there better ways? 0 Answers

Using rendertextures in multiple shader passes 0 Answers

Use RenderTexture as ShaderResource in DX11 native plug-in 1 Answer

Converting a RenderTexture to a Texture2D for use in a shader 2 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