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 martygrof3708 · Jun 17, 2019 at 04:03 PM · camerashaderrenderinggraphicsbackground

How can I make a quad render below absolutely everything else?

I tried changing the render queue but no success. I have a quad parented to the main camera. I am trying to have a static background.

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

1 Reply

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

Answer by Bunny83 · Jun 17, 2019 at 04:23 PM

Well, the most efficient way would be to render the quad just after all opaque geometry has been rendered. You would render the quad at a far distance with depth testing on but with zwrite off just like most transparent geometry is rendered.


Another option is to render it before anything else with zwrite off. This way the distance from the camera doesn't matter. However it's less efficient since you will use quite a bit of fill rate due to overdraw.


edit

Ok here's an example. To use the second solution you can use the following shader for your plane. This plane can be just right in front of the camera if forward rendering is used. This does not work in deferred rendering. With deferred rendering you need to place it manually far away. Of course the plane would need to be a child of the camera so it always moves and rotates with the camera.

 Shader "Unlit/BackgroundPlane"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
     }
     SubShader
     {
         Tags {  "Queue" = "Background" }
         ZWrite Off
         Pass
         {
             ZWrite Off
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target
             {
                 // sample the texture
                 fixed4 col = tex2D(_MainTex, i.uv);
                 return col;
             }
             ENDCG
         }
     }
 }

The same shader can be used for the first solution, but you just need to replace

 Tags {  "Queue" = "Background" }

with

 Tags {  "Queue" = "Geometry+500" }

This will render the quad after all opaque geometry. Just as mentioned in this case the quad need to be far away and behind everything. So you might want to place it close to the far clipping plane and scale it up to fill the whole screen.


Instead of an actual plane object that is placed as child on the camera we can also draw a fullscreen quad procedurally with a script like this:

 //BackgroundPlane.cs
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class BackgroundPlane : MonoBehaviour
 {
     public Material mat;
 
     void Point(float x, float y)
     {
         GL.TexCoord2(x, y);
         GL.Vertex3(x, y, -1);
     }
 
     private void OnPostRender()
     {
         mat.SetPass(0);
         GL.PushMatrix();
         GL.LoadIdentity();
         GL.LoadProjectionMatrix(Matrix4x4.Ortho(0,1,0,1,0,1));
         GL.Begin(GL.QUADS);
         Point(0, 0);
         Point(0, 1);
         Point(1, 1);
         Point(1, 0);
         GL.End();
         GL.PopMatrix();
     }
 }

Just attach this script to the camera. It will draw a fullscreen quad at the far clipping plane using the provided material. This would work with both shaders mentioned above. Note that this script will always scale the texture to fit the screen. So it might be stretched depending on the screen aspect ratio.

Comment
Add comment · Show 2 · 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 martygrof3708 · Jun 19, 2019 at 11:08 PM 0
Share

I cannot thank you enough for this! Do you just know all this shader stuff off the top of your head? I'm still new to shaders and rednering.

avatar image martygrof3708 · Jun 21, 2019 at 02:59 AM 0
Share

If I wanted to make the procedural generated plane not rotate with the camera, is it possible?

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

242 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 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

Scissor test/early rejection 2 Answers

Render inside of mesh as black 2 Answers

Masking elements with non-rendered layers? Culling masks, camera, render textures 0 Answers

Rendering different shader when camera get near object 1 Answer

Do I have to turn on camera relative rendering in URP? 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