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 g-augview · Mar 22, 2021 at 12:34 AM · graphicsrendertextureblitblitting

Where to Graphics.Blit()?

I have 2 cameras, one for a video background, one rendering objects. Both have a target render texture. I want to use a material as a post-processing effect to add a global transparency to the objects (each may already have an individual transparency), so I want to blit the result of a material to screen. However when I call graphics.blit in the built-in render pipeline (in OnRenderImage) I don't get the same result as in the material preview (see pictures).

Expected result (material preview) by setting the material's main texture: alt text

Graphics.Blit in OnRenderImage by blitting in OnImageRender: alt text

 private void OnPreRender()
         {
             material.SetFloat(variableName, Multiplier);
             material.SetTexture(backgroundTextName, backgroundRt);
         }
 
         private void OnRenderImage(RenderTexture source, RenderTexture destination)
         {
             Graphics.Blit(source, destination, material);
         }

The shader:

 Shader "PostProcessing/AlphaBlend" 
 {
     Properties
     {
         _MainTex("Main Texture", 2D) = "white" {}
         _BackgroundTex("Background Texture", 2D) = "white" {}
         _Alpha("Alpha Multiplier", Range(0,1)) = 0.5
     }
 
 
     SubShader{
 
         Tags{ "PreviewType" = "Plane" }
 
         Pass {
                     ZWrite Off
                     Lighting Off
                     ColorMask RGBA
 
                     CGPROGRAM
                     #pragma vertex vert
                     #pragma fragment frag
                     #include "UnityCG.cginc"
 
                 sampler2D _MainTex;
                 sampler2D _BackgroundTex;
                 float _Alpha;
 
                 struct v2f {
                     float4 pos : SV_POSITION;
                     float2 uv : TEXCOORD0;
                 };
 
                 v2f vert(appdata_base v)
                 {
                     v2f o;
                     o.pos = UnityObjectToClipPos(v.vertex);
                     o.uv = v.texcoord;
                     return o;
                 }
 
                 float4 frag(v2f_img i) : COLOR
                 {
                     float4 color = tex2D(_MainTex, i.uv);
                     color.a = color.a * _Alpha;
                     float4 backgroundColor = tex2D(_BackgroundTex, i.uv);
                     float4 output = (color.a * color + (1-color.a) * backgroundColor);
                     output.a = 1;
                     return output;
                 }
                 ENDCG
             }
         }
 }

As you can see the pipe colours are very dull in the second case. I have tried to blit to a different render texture, then blit that render texture in Update, LateUpdate, but in those cases I get a black screen.

Does someone understand why blitting in OnImageRender results in dull colors (more transparency than I'd expect)? Where/how can I use my material since it does what I want given the preview?

screen-shot-2021-03-22-at-122159-pm.png (123.1 kB)
screen-shot-2021-03-22-at-10040-pm.png (170.1 kB)
Comment
Add comment · Show 7
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 Eno-Khaon · Mar 22, 2021 at 05:58 AM 0
Share

For reference, it's not that you're getting "dull" colors. It's that the objects in the render have transparency applied to them. What are the attributes of the material/shader being used during the Blit() call?

avatar image g-augview Eno-Khaon · Mar 22, 2021 at 08:55 AM 0
Share

Compared to the expected result, the pipes do look dull to me :) I don't do any blending, and set alpha to 1 in the shader (I added the shader to the question).

avatar image Eno-Khaon g-augview · Mar 22, 2021 at 06:24 PM 0
Share

On first impression, that seems like it ought to be fine. Just for reference, does it still give the same result if you force _Alpha to be 1? (I ask simply because your second image looks like ~50% opacity layering)

 // Example: comment out the line...
 color.a = color.a * _Alpha;
Show more comments

1 Reply

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

Answer by g-augview · Apr 12, 2021 at 10:17 PM

I fixed this by modifying the pipe's transparent shader: I replaced

 Blend SrcAlpha OneMinuseSrcAlpha

with

 Blend One OneMinusSrcAlpha
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

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

Related Questions

How Blit internally works? 2 Answers

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

dynamic hole in layer / texture / camera 0 Answers

Blitting a Texture once 0 Answers

Change the camera RenderTexture before rendering it to screen? 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