Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 AlexFlint · Jun 27, 2012 at 06:57 PM · shaderblit

Graphics.Blit with custom shader gives black screen under iOS

I have the following

 public Material shaderMaterial;
 void OnRenderImage (RenderTexture source, RenderTexture destination) {
     //rotoscopeMaterial.color = Color.white;
     Graphics.Blit(source, destination, shaderMaterial);
 }

where shaderMaterial is associated with a simple GLSL shader. This works fine in the development environment but when I build the app for iOS and run it on a device see nothing but a black screen. However, when I Blit() the image directly with no shader I do see the correct output. Any ideas?

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 badescuga9 · Feb 06, 2013 at 11:57 AM 0
Share

i am having the same issue, have you found a way to fix it?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ecschiel · Oct 19, 2017 at 02:05 AM

It's probably late for an answer, but maybe it helps someone. I had this same issue using a custom shader for some post-processing effects and the reason was the custom shader was not being included in the build. You have to:

1 - reference it in some material

2 - add it under "Allways included shaders", or

3 - put shader or something that reference it on "Resources" folder

as stated in Shader.Find doc

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 HooksForFeet · Apr 04, 2020 at 09:31 AM 0
Share

Never too late for an answer, because this really saved my bacon! I was doing some postprocessing in OnRenderImage and while it worked fine in the editor, the build remained black. Turns out that one of the shaders I used was only used by Shader.Find and not referenced anywhere else, so it didn't get included in the build. Adding it to "Always included" seemed the cleanest solution. Thanks!

avatar image
0

Answer by schulkinator · Mar 13, 2013 at 07:20 PM

There seems to be a bug in Unity (as of 4.0.1) under iOS where in OnRenderImage you cannot directly Blit to the destination with a Material. Doing so effectively renders nothing (whatever was previously in the backbuffer, which may be black, or garbage, or the previously rendered frames). You can however, as a workaround, render to an intermediate buffer and then Blit to the destination without a material. For example:

 public Material shaderMaterial;
 public RenderTexture intermediateRT;
 void OnRenderImage (RenderTexture source, RenderTexture destination) {
     //rotoscopeMaterial.color = Color.white;
     Graphics.Blit(source, intermediateRT, shaderMaterial);
     Graphics.Blit(intermediateRT, destination);
 }

This of course costs the additional overhead of another RenderTexture and draw call, but it at least works. Hopefully Unity will fix this in future versions.

The way I isolated this was by using different materials of varying complexity, some that just do diffuse texturing, and some that just output a flat color, none of them worked. They work in the editor but not on a device. Also, all my RenderTexture formats were identical RGBA32s (save some resolution differences). But once I Blit without a Material to destination it renders as expected. My conclusion was that it must be the presence of a Material that is causing the issue.

Comment
Add comment · Show 4 · 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 badescuga9 · Mar 14, 2013 at 01:16 PM 0
Share

i don't think this is a Unity 4.0.1+ problem. i had this issue in unity 3.5 and your solution still isn't working. i get no effect whatsoever applied. (now i have unity 4.0)

avatar image schulkinator · Mar 14, 2013 at 08:17 PM 0
Share

@badescuga9 I wasn't implying it was exclusively a Unity 4.0.1 problem, I was merely identifying that as the version I have known this issue to exist on (it's the version I use). As for your continuing issues, are you sure you don't have a material applied on the final Blit? Is the shader you're using working correctly and does it have all the proper inputs? Perhaps you need to set the specific pass you want by using the fourth parameter to Blit()? Also what error are you getting, if any? Could you possibly post the relevant code? Oh and to cover the obvious, I have omitted the actual creation of the intermediate rendertexture, so you would obviously need to do that to have functioning code (it would be the same format and dimensions as your screen).

avatar image badescuga9 · Mar 15, 2013 at 09:58 AM 0
Share

i get a black screen when i try to render. All the code works in the simulator. I don't get any errors. If i apply the material on a plane, it renders properly(so it can't be the shader). i am doing a nightvision effect. I set the intermediate RT in Start() :

 intermediateRT = new RenderTexture(Screen.width,Screen.height,0,RenderTextureFormat.Default);

and after that in the OnRenderImage() i use:

 Graphics.Blit(source, intermediateRT, overlay$$anonymous$$aterial,0);
 Graphics.Blit(intermediateRT, destination);

As for the shader:

 Shader "Custom/nightvisionShader" {
     Properties {
         _$$anonymous$$ainTex ("Texture ", 2D) = "white" {}
         _noiseTex ("noiseTex ", 2D) = "white" {}  
         _maskTex ("mask tex",2D) = "white" {} 
         _elapsedTime("elapsed time",Float) = 0 // seconds
         _lu$$anonymous$$anceThreshold("lu$$anonymous$$ance threshold",Float) = .2 // 0.2
         _colorAmplification("color amplification",Float) = 4 // 4.0
         _effectCoverage("effect coverage",Float) = .5 // 0.5
     }
     SubShader { 
     
     
         Tags { "RenderType"="Transparent" }
         //LOD 200
     
     
         CGPROGRA$$anonymous$$
         #pragma surface surf Lambert
 
         sampler2D _$$anonymous$$ainTex;
         sampler2D _noiseTex;
         sampler2D _maskTex;
         float _elapsedTime;
         float _lu$$anonymous$$anceThreshold;
         float _colorAmplification;
         float _effectCoverage;
 
         struct Input {
             float2 uv_$$anonymous$$ainTex;
             float2 uv_noiseTex;
             float2 uv_maskTex;
             
         };
 
         void surf (Input IN, inout SurfaceOutput o)
          {
              half4 finalColor;         
             if (IN.uv_$$anonymous$$ainTex.x < _effectCoverage) 
                   {
                     float2 uv;  
                    
                     uv.x = 0.4*sin(_elapsedTime*50.0);                                 
                     uv.y = 0.4*cos(_elapsedTime*50.0);                                 
                     float m = tex2D(_maskTex, IN.uv_maskTex).r;
                     half3 n = tex2D(_noiseTex,(IN.uv_noiseTex*3.5) + uv).rgb;
                     half3 c = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex + (n.xy*0.005)).rgb;
               
                     float lum = dot(float3(0.30, 0.59, 0.11), c);
                     if (lum < _lu$$anonymous$$anceThreshold)
                       c *= _colorAmplification; 
   
                     half3 visionColor = float3(0.1, 0.95, 0.2);
                     finalColor.rgb = (c + (n*0.2)) * visionColor * m;
                   o.Albedo = finalColor.rgb;
                    }
            else
                {
                 o.Albedo = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex).rgb;
               //  o.Emission = half4(.5,.5,.5,1);
                }
             
         }
         ENDCG
         
     } 
     FallBack "Diffuse"
 }
avatar image AndySum · Jan 12, 2014 at 03:39 AM 0
Share

This fixed my issues with a desktop build. I was getting a black screen when I did a standalone build, but in the editor my shader worked fine. With this intermediary RT it fixed the problem completely. Thank you!

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

9 People are following this question.

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

Related Questions

Effects of Graphics.Blit to rendertexture are temporary 2 Answers

How to use Graphics.Blit with custom input textures 3 Answers

Copy frame buffer into Texture 1 Answer

I have a problem with updating a RenderTexture being used in combination with a shader. 0 Answers

How to (efficiently) make an image pixelated? 3 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