Reputation: 21
I wrote a custom shader in Unity URP 2022.2 and otherwise it works fine, but the editor grid lines are being drawn on top of the object in the editor window.
The shader code I used was like this:
Shader "Custom/SurfaceShaderExample2"
{
Properties
{
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags {"RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline" "Queue"="Geometry"}
Pass {
Name "ForwardLit"
Tags { "LightMode" = "UniversalForward" }
Cull Back
Blend One Zero
ZTest LEqual
ZWrite On
HLSLPROGRAM
#pragma vertex Vertex
#pragma fragment Fragment
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct VertexInput {
float3 positionOS : POSITION;
};
struct Varyings {
float4 positionHCS : SV_POSITION;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
CBUFFER_END
Varyings Vertex(VertexInput IN) {
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS);
return OUT;
}
float4 Fragment(Varyings IN) : SV_TARGET {
return _BaseColor;
}
ENDHLSL
}
}
}
The behaviour looks like the object isn't writing to depth, but it works fine with other objects. The only problem is with the editor grid.
Upvotes: 1
Views: 176
Reputation: 1176
I couldn't get an object using a custom shader in URP to render on top of the grid in the scene view.
Ultimately, I copy pasted the shader code for URP's default lit shader - which, out of the box, does render on top of the scene view grid - and started removing passes one by one, to see which was required specifically.
It turned out to be the DepthNormals
pass, and not DepthOnly
, which could be removed without surfacing the issue.
This is what it looks like in the default shader code:
// This pass is used when drawing to a _CameraNormalsTexture texture
Pass
{
Name "DepthNormals"
Tags
{
"LightMode" = "DepthNormals"
}
// -------------------------------------
// Render State Commands
ZWrite On
Cull[_Cull]
HLSLPROGRAM
#pragma target 2.0
// -------------------------------------
// Shader Stages
#pragma vertex DepthNormalsVertex
#pragma fragment DepthNormalsFragment
// -------------------------------------
// Material Keywords
#pragma shader_feature_local _NORMALMAP
#pragma shader_feature_local _PARALLAXMAP
#pragma shader_feature_local _ _DETAIL_MULX2 _DETAIL_SCALED
#pragma shader_feature_local _ALPHATEST_ON
#pragma shader_feature_local_fragment _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
// -------------------------------------
// Unity defined keywords
#pragma multi_compile _ LOD_FADE_CROSSFADE
// -------------------------------------
// Universal Pipeline keywords
#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RenderingLayers.hlsl"
//--------------------------------------
// GPU Instancing
#pragma multi_compile_instancing
#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"
// -------------------------------------
// Includes
#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
#include "Packages/com.unity.render-pipelines.universal/Shaders/LitDepthNormalsPass.hlsl"
ENDHLSL
}
Upvotes: 0
Reputation: 21
The problem seems to have been that I didn't include a depth only pass in the shader. When I added the depth pass it works even in the editor window.
Pass {
Name "Depth"
Tags { "LightMode" = "DepthOnly" }
Cull Back
ZTest LEqual
ZWrite On
ColorMask R
HLSLPROGRAM
#pragma vertex Vertex
#pragma fragment Fragment
#include "../Common/SurfaceShaderExampleDepthProgram.hlsl"
ENDHLSL
}
The pass just outputs the clipspace z coordinate.
Upvotes: 1