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 Teku-Studios · May 23, 2013 at 03:45 PM · shaderrendertexturepostprocess

Render to texture performance problems

Hi there!

We are just doing some cool effects with shaders and post process but there is something wrong for sure the way we are doing it because it's pretty slow (specially at Full Screen or Maximizing the editor window). We suspect could be the way assigning RenderTextures to the camera. Here it is the code in a camera script used:

  public void OnPreCull()
     
         {
     
            
     
             // Start from nothing
     
            CleanRenderTextures();
     
          
     
            // Reference to ppCamera’s camera
     
            Camera cam = GetPPCamera();
     
            
     
            // Set up camera
     
            cam.CopyFrom(this.camera);
     
            cam.cullingMask = 1 << 27; //Culling mask for rendering what we need to combine later
     
            cam.depth = 2;
     
            cam.clearFlags = CameraClearFlags.Color;
     
            cam.backgroundColor = new Color(0.2f,0.2f,0.2f,1.0f);
     
      
     
            lightTex = RenderTexture.GetTemporary (Screen.width, Screen.height, 16);
     
            //I could use new RenderTexture with Screen dimensions but not sure about using Create method
     
            //(it is called from Cam when   render?)
     
      
     
             cam.targetTexture = lightTex;
     
            
     
             //RenderTexture.active = lightTex; Is this needed?
     
            
     
            cam.Render ();
     
         }
     
      
     
         /**
     
         * Post Processing magic
     
         * @param source
     
         * @param destination
     
         */
     
         public void OnRenderImage(RenderTexture source, RenderTexture destination)
     
         {
     
            dimensionCompositeMaterial.SetTexture("_ForeTex", lightTex);
     
          
     
            Graphics.Blit (source,destination,compositeMaterial );
     
            
     
            CleanRenderTextures();
     
         }

And here it is the compositeMaterial shader code:

 Shader "Teku/TestComposite" {
        Properties {
           _MainTex ("Focus scene texture", 2D) = "white" {}
           _ForeTex ("ForeGround texture", 2D) = "white" {}
        }
        SubShader {
           Tags {"Queue"="Transparent+31"}
           Pass {
              ZWrite Off Lighting Off Cull Off Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha
      
             /* This was used to cobine layers as in photoshop but it's incredibly slow
      
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
      
              struct data
              {
                  float4 position : POSITION;
                  float2 uv: TEXCOORD0;
              };
              
              struct v2f
              {
                  float4 position : POSITION;
                  float2 uv : TEXCOORD;
              };
              
              sampler2D _Back0Tex;
              sampler2D _Back1Tex;
              sampler2D _ForeTex;
              sampler2D _MainTex;
      
              v2f vert(data v)
              {
                  v2f o;
                  o.position = mul(UNITY_MATRIX_MVP, v.position);
                  o.uv = v.uv;
                  return o;    
              
              }
              
              float4 frag(v2f i) : COLOR
              {
                  float4 pa = tex2D(_MainTex, i.uv);      
                  float4 pa = tex2D(_ForeTex, i.uv);
                  float4 final = pa + pb *(1.0-pa.a); //MainScene + Front
      
                  return final;
              }
             ENDCG
            
             */
      
            
              SetTexture [_MainTex] {
                 Combine texture
              }
              
              //Multiply time instead using macros for testing!!
              SetTexture [_ForeTex]
              {
                  Combine texture * previous
              }
              
           }
          
         }
     }

Well, as you can see, we'll try to find if the real problem was in the fragment shader testing SetTexture macros but the performance it's similar. I'm sure the problem is the way we are assigning Rendertextures to the camera or something similar. Sometimes we suffer from flickering problems too.

Thank you very much in advance for your 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

0 Replies

· Add your reply
  • Sort: 

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

14 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

Related Questions

sampler2d object has no methods 1 Answer

How to render a RenderTexture into a specific mipLevel 0 Answers

How to use Camera.RenderWithShader() to modify rendered image? 0 Answers

Read depth buffer on the cpu 1 Answer

Using Render Texture with Screen Overlay 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