Render Object After Everything & Before Skybox In Shader

UPDATE: See THIS POST for more info and sample scene download.

How to render object after everything and just before skybox?

Here’s the shader so far:

Shader "Custom/Background"
{
    Properties
    {
       _MainTex ("Base (RGB) Trans (A)", 2D) = "white"{}
    }
   
    SubShader
    {
        Tags { "Queue" = "Background+1" "RenderType" = "Background" }
        Cull Off ZWrite Off
   
        CGPROGRAM
        #pragma surface surf Lambert
       
        sampler2D _MainTex;
       
        struct Input
        {
            float2 uv_MainTex;
        };
   
        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
        }
        ENDCG
    }
}

But seems like the skybox overwrites any background shaders and the object is not shown.

Multiple cameras implementation is not possible. Also tried renderQueue for its material and it has no effects at all!

Use the renderQueue on the material instead of the one in the shader. (The Queue from the shaders doesn’t always get applied correctly.)

Since you don’t write to the z-buffer, you are pretty much inviting the skybox to be drawn on top. So either draw it on top of the skybox (after the skybox) or enable z writing.

1 Like

@jvo3dc Thanks for the reply. Actually I do want to have the ZWriting enabled to receive fog and other effects so don’t have to turn it off in the shader.

As I said in the first post, the material’s renderQueue has no effects at all and doesn’t do the rendering order!

I’m trying to change default terrain’s shader so maybe that’s what makes it more complicated. The scene is consisting of some near terrains around the player and a bigger far terrain underneath the near terrains. I want the far terrains to act as background mountains and being rendered just after the skybox. As this bigger terrain is located under the near terrains, sometimes parts of it pops out of the near terrains and this is where I want these parts to be hidden. So near terrains always have to be drawn on top of the far terrain regardless of far terrain’s distance to the camera.

I’ve set lower value in far terrain’s material renderQueue (Geometry-101 = 1899) and a higher value for near terrains’ material renderQueue (Geometry-100 = 1900) and it doesn’t do the job. Checked different lower/higher values and it’s still the same.

And why is there shader queue tags when most of the times they don’t work as you said?

Also tried different ZTest options based on what answered here: Setting renderQueue doesn't appear to change draw order - Questions & Answers - Unity Discussions

But again with no luck! Still looking for a simple magical combination of shader tags to completely draw/render a terrain on top of another one!

Attached I setup a simple scene with 2 overlapping terrains to illustrate what I’m asking. The scripts are commented out and there are 2 built-in standard terrain shaders assigned to the scene terrains for tweaking.

As you see, the renderQueue can be controlled both for terrain materials and object materials in scene but there are no effects at all neither. Wondering why not as there are many people out there who were succeeded in this!

TLDR!: Open the scene in Unity, you will see 2 terrains sitting on each other. The small one has to be rendered always on top of the other one.

2857075–208937–RenderingOrder.unitypackage (2.95 MB)

Right, but it is disabled now, which is why the skybox overwrites things. So if you want to have it enabled, enable it. (ZWrite Off = ZWriting disabled)

Well, they are supposed to work and in typical situations, they do. If a new material is created, the queue value is assigned from the shader. If you change the shader to have a different queue value however, the material is not updated. This actually does make sense. Still, if you really want to be specific about the queue order, assign it directly to the material.

1 Like

Thanks, I already knew this but didn’t want to update the first post as I found I need z-writing later. See included shaders in the sample scene.

In the example scene, the terrain materials will be generated in runtime and will get different render queues in their custom shaders in project by script. Turning on the Debug mode in Unity editor actually shows correct assigned custom render queues in materials. I even tried to change the values manually in the inspector but to no avail.

I guess you mean the materials have to be manually generated and then set custom render queue from inspector in Debug mode? Or has to be set from a script? Either way, I have to check this procedure too.

Did you have time to check the sample scene provided in my previous post? It’s straight to the point, so it would be great if you and others could check it out and tweak the shaders.