Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 markefus · Jan 27, 2019 at 12:22 AM · renderinggraphicsprogrammingblit

Manual Graphics.Blit

I'm having some issues preserving stencil buffer data with Graphics.Blit.

In the Unity documentation, it says if you want to preserve stencil buffer data:

"Note that if you want to use depth or stencil buffer that is part of the source (Render)texture, you'll have to do equivalent of Blit functionality manually - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin)."

Source: https://docs.unity3d.com/ScriptReference/Graphics.Blit.html
Could anyone provide example code for how to do this? It's not listed in the documentation.
I figured out how to do the second part, (and can confirm this preserves stencil buffer data). But haven't used render targets before and documentation is sparse.
Here's what I have so far:
    public Shader shader;
    private Material mat;

    // Will be called from camera after regular rendering is done.
    public void OnPostRender()
    {
        if (!mat)
        {
            mat = new Material(shader);
            mat.hideFlags = HideFlags.HideAndDontSave;
        }

        GL.PushMatrix();
        GL.LoadOrtho();

        // activate the first shader pass (in this case we know it is the only pass)
        mat.SetPass(0);
        // draw a quad over whole screen
        GL.Begin(GL.QUADS);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(1, 0, 0);
        GL.Vertex3(1, 1, 0);
        GL.Vertex3(0, 1, 0);
        GL.End();

        GL.PopMatrix();
    }

I essentially want to create an image effect, but have to do it manually because of the stencil buffer issue. I want to preserve the camera's texture data (presumably in a buffer, as described) and feed it into the shader as I would in a normal blit operation.
Thanks!
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

3 Replies

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

Answer by markefus · Jan 27, 2019 at 06:16 AM

For any lost soul struggling with preserving stencil buffer data in image effects.... It just works.

  public Shader shader;
  private Material mat;
   
  void OnEnable()
  {
      mat = new Material(shader);
  }
 
  //'It Just Works' -- Todd Howard
  void OnRenderImage(RenderTexture source, RenderTexture destination)
  {
      RenderTexture buffer = RenderTexture.GetTemporary(source.width, source.height, 24);
      Graphics.Blit(source, buffer, mat);  
      Graphics.Blit(buffer, destination);
      RenderTexture.ReleaseTemporary(buffer);
  }
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 markefus · Jan 27, 2019 at 06:17 AM 0
Share

https://answers.unity.com/questions/621279/using-the-stencil-buffer-in-a-post-process.html

avatar image
2

Answer by ArminRigo · Feb 10, 2020 at 10:12 AM

FWIW, I'm trying to use a similar CustomBlit() function and eventually found out the correct tweak. For the next person's usage, I'm copying here the complete thing:

 /* Sometimes you need to save and restore the active rendertexture. */
 var original = RenderTexture.active;
 RenderTexture.active = destination;   /* or Graphics.SetRenderTarget(..) */
 
 // Set the '_MainTex' variable to the texture given by 'source'
 mat.SetTexture("_MainTex", source);
 GL.PushMatrix();
 GL.LoadOrtho();
 // activate the first shader pass (in this case we know it is the only pass)
 mat.SetPass(0);
 // draw a quad over whole screen
 GL.Begin(GL.QUADS);
 GL.TexCoord2(0f, 0f); GL.Vertex3(0f, 0f, 0f);    /* note the order! */
 GL.TexCoord2(0f, 1f); GL.Vertex3(0f, 1f, 0f);    /* also, we need TexCoord2 */
 GL.TexCoord2(1f, 1f); GL.Vertex3(1f, 1f, 0f);
 GL.TexCoord2(1f, 0f); GL.Vertex3(1f, 0f, 0f);
 GL.End();
 GL.PopMatrix();
 RenderTexture.active = original;    /* restore */

The shader itself:

 Shader "Unlit/MyCustomBlit"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 100
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
             };
 
             sampler _MainTex;
 
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;     /* just copy the coordinates here */
                 return o;
             }
 
             float4 frag (v2f i) : SV_Target
             {
                 float4 input_col = tex2D(_MainTex, i.uv);
                 /* post-processing here */
                 return input_col;
             }
             ENDCG
         }
     }
 }
 
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
avatar image
0

Answer by markefus · Jan 27, 2019 at 02:21 AM

So have some updates, but it still isn't working. Any ideas??

 public Shader shader;
 private Material mat;
 private Camera cam;

 void OnEnable()
 {
     // Create a material that uses the desired shader
     mat = new Material(shader);

     // Get the camera object (this script must be assigned to a camera)
     cam = GetComponent<Camera>();
 }

 [ImageEffectOpaque] // Draw after opaque, but before transparent geometry
 void OnRenderImage(RenderTexture source, RenderTexture destination)
 {
     // Call custom Blit function
     // (usually Graphics.Blit is used)
     CustomBlit(source, destination, mat);
 }


 void CustomBlit(RenderTexture source, RenderTexture destination, Material mat)
 {
     // Custom Blit
     // ===========

     // Set the given destination texture as the active render texture
     Graphics.SetRenderTarget(destination.colorBuffer, source.depthBuffer);
    
     // Set the '_MainTex' variable to the texture given by 'source'
     mat.SetTexture("_MainTex", source);

     GL.PushMatrix();
     GL.LoadOrtho();

     // activate the first shader pass (in this case we know it is the only pass)
     mat.SetPass(0);
     // draw a quad over whole screen
     GL.Begin(GL.QUADS);
     GL.Vertex3(0, 0, 0);
     GL.Vertex3(1, 0, 0);
     GL.Vertex3(1, 1, 0);
     GL.Vertex3(0, 1, 0);
     GL.End();

     GL.PopMatrix();
 }
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

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

Related Questions

Multiple Cars not working 1 Answer

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

OpenGL ES 2.0 support breaks simple shader 0 Answers

Using stencil buffer with OnRenderImage()/Blit 0 Answers

URP Blit Material Renderer Feature - Breaks the Inspector Asset Preview Window 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