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
0
Question by TeoL · Aug 05, 2013 at 05:35 PM · shadersurface shaderrefractionbackbuffer

Accessing camera "backbuffer" in surface shader

Hi Everyone - this is my first question here, so I hope I'm following all the rules. Checked the existings, but couldn't find anything, so I'm creating my own.

Essentially, I'm trying to execute a refraction shader that utilizes output from a camera (based on this gpu gem: http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter19.html)

What is the best (most performant/efficient) way to pass camera render data to a surface shader as a texture?

I've got a really rough experimental start using C#, with the following script attached to a camera:

 using UnityEngine;
 using System.Collections;
 
 public class camPostRenderScript : MonoBehaviour {
     
     public Renderer display;
     Texture2D tex;
     
     void Start () {
         tex = new Texture2D(Screen.width,Screen.height);
     }

     void OnPostRender() {
         tex.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0);
         tex.Apply(false);
         display.material.mainTexture = tex;
     }
 }

However, this has drawbacks in that it only references one screen object at a time, and using C# instead of shader code feels like it's taking work off of the gpu and putting it onto the gpu which I think has performance implications (if any experts want to speak on that, I'm all ears).

Is there a way to create a shared texture that all of the refraction shaders will be able to access? I suppose I could make that "tex" var accessible, but is there a way to pull that in with a surface shader? Is it super heavy to write per frame like this? I'd imagine it's cheaper than using RenderToTexture() - is that correct? I'd prefer to avoid using RenderToTexture as I get the impression that it's pretty heavy. Is this method faster/lighter?

Alternately, is there another, "shader native" way to access a camera "backbuffer" like this that might be more efficient/live on the gpu?

Any input would be greatly appreciated :)

Thanks Everyone!

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
Best Answer

Answer by Dave-Carlile · Aug 05, 2013 at 05:47 PM

You're likely not going to have much luck with ReadPixels. Copying data from the GPU isn't going to perform well, especially a full screen of it.

Unless I'm missing something, Render Textures are the way to go. Render the scene to the render texture, then use it like you would a normal texture. The data remains in GPU RAM so there's never an issue with moving it around.

Comment
Add comment · Show 6 · 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 TeoL · Aug 05, 2013 at 05:59 PM 0
Share

Thanks Dave :) I was hoping to avoid using Pro features, but that does sound exactly like what I need. Stays on the GPU and everything. I suppose there's no clean alternative in Standard / self-code solution?

avatar image Dave-Carlile · Aug 05, 2013 at 06:01 PM 0
Share

I'm not sure if there's a way to roll your own render textures or not. Seems like it might be possible, but I'm not knowledgable enough with Unity to know if it is.

avatar image TeoL · Aug 05, 2013 at 10:40 PM 0
Share

Sounds about right. Thanks again. Now have more tools for research :)

avatar image Dave-Carlile · Aug 06, 2013 at 01:09 PM 0
Share

If my answer was helpful, please click the "accepted" checkmark.

avatar image Cleroth · Oct 15, 2014 at 04:04 AM 0
Share

As an additional note, you don't need to render everything on a render texture, just what you need to blend.

Show more comments

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

16 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

Related Questions

Simple Shader Syntax ... or so I thought 0 Answers

Shader that renders fragment behind 0 Answers

Use Shader Motion Vectors pass in Surface Shader 2 Answers

tangent space viewDir in a SurfaceShader 0 Answers

Spherical Hamonics - ShadeSH9 and Surface Shader issue 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