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
0
Question by irshad0karim · Mar 04, 2017 at 12:05 AM · c#rendertextureimage effectsblit

Graphics.Blit() results in empty RenderTexture

So I'm trying to use Graphics.Blit as cross-platform alternative to using Compute Shaders, specifically to merge a bunch of different images together and store them into a Texture2D. I've set up a shader that does this successfully, and a utility function as follows:

 public void LayerMerge(Texture2D destination, Texture2D over, Texture2D region = null, int regionIndex = 0, Texture2D under = null)
         {
             
             avatarLayerMergeMaterial.SetTexture("_MainTex", destination);
             avatarLayerMergeMaterial.SetTexture("_Over", over);
             avatarLayerMergeMaterial.SetTexture("_Region",region);
             avatarLayerMergeMaterial.SetInt("_RegionIndex",regionIndex);
             avatarLayerMergeMaterial.SetTexture("_Under", under == null ? nullTexture : under);
 
             Graphics.Blit(destination,avatarLayerMergeRenderTexture,avatarLayerMergeMaterial,-1);
             Graphics.CopyTexture(avatarLayerMergeRenderTexture,destination);
         }

The region/region index stuff is unimportant, and for now under is as well. My function call looks like this, using only the first two parameters:

 foreach (AvatarLayer layer in layers)
 {
        textureUtility.LayerMerge(displayTexture,layer.Texture);
 }

The problem is that the render texture is completely empty and seems to be completely unaffected by the Blit call. I'm able to look at the actual material instance (avatarLayerMergeMaterial) and it's got a little preview window in the inspector at runtime that displays the correct results. For some reason it's simply not being copied over into the render texture.

Thanks.

Comment
Add comment · Show 1
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 irshad0karim · Mar 02, 2017 at 06:39 PM 0
Share

Looks like my question is still awaiting moderation for whatever reason, but good news! I solved the issue myself, and unfortunately the problem was related to something I did not mention in the original question: I was using ShaderForge to create the shader in question.

ShaderForge included a "Forward" and a "$$anonymous$$eta" pass. Now my understanding of shaders is fairly limited (hence my using ShaderForge) but removing the meta pass outright seemed to solve the problem completely. At least, as far as I can tell.

This is all based on a test I was running during my lunch break at work, where I was able to reproduce the problem with the same shader, and then solve it - so I have every confidence that it will fix the problem in the actual project in question.

Probably.

1 Reply

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

Answer by irshad0karim · Mar 04, 2017 at 08:39 AM

(Pasted from the comment I posted earlier while the post was still awaiting moderator approval so the answer is not missed by those struggling with the same problem)

Looks like my question is still awaiting moderation for whatever reason, but good news! I solved the issue myself, and unfortunately the problem was related to something I did not mention in the original question: I was using ShaderForge to create the shader in question.

ShaderForge included a "Forward" and a "Meta" pass. Now my understanding of shaders is fairly limited (hence my using ShaderForge) but removing the meta pass outright seemed to solve the problem completely. At least, as far as I can tell.

This is all based on a test I was running during my lunch break at work, where I was able to reproduce the problem with the same shader, and then solve it - so I have every confidence that it will fix the problem in the actual project in question.

Probably.

Edit:

So this definitely did solve my problem. Instead of just deleting that pass from the shader however, I ended up using the fourth parameter in the Blit() call (pass). It defaults to -1 which means that all passes in the shader should be used. By setting it to 0, only the first pass is used, which had the same effect in fixing my problem. This is beneficial because now I can edit my shader in ShaderForge without having to go back in and manually remove the Meta pass that would inevitably be added every time the shader is compiled.

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 Darkgaze · Sep 25, 2018 at 05:02 PM 1
Share

In case you don't want to use the -1 parameter and you want to specifically give numbers to run several passes, you can copy the source texture parameters (so that they are the same in a new RenderTexture) and pass it. Otherwise, it will pass "destination" which is generally NULL.

For example, run just pass 0 and then 5 and then return to destination the result.

 RenderTexture intermediate = new RenderTexture(source);
 Graphics.Blit(source, intermediate, bloom$$anonymous$$aterial, 0);
 Graphics.Blit(intermediate, destination, bloom$$anonymous$$aterial, 5);

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using Graphics.Blit with the same source and destination 1 Answer

Graphics.DrawTexture on RenderTexture not working...? 2 Answers

Blur Effect 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