#Trying to sample the GBuffer.

1 messages · Page 1 of 1 (latest)

winged thorn
#

Title, basically. Writing a HLSL shader (despise shader graph in it's entirety) and I can't seem to sample the GBuffer from a shader. I can see that my objects are rendering to _GBuffer0 according to the frame debugger, but when I try to sample _GBuffer0, it just returns a grey color.

I define the GBuffer like so:

TEXTURE2D(_GBuffer0);
SAMPLER(sampler_GBuffer0);

and sample it like so:

float3 n = SAMPLE_TEXTURE2D(_GBuffer0, sampler_GBuffer0, IN.screenPosition/IN.screenPosition.w);

Screen position is calculated in the vertex shader like so:

//Irrelevant parts omitted
VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);
VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS);
OUT.screenPosition = positionInputs.positionNDC;

It's worth noting I'm not trying to sample this from a fullscreen quad, but specific objects in the scene (the usecase being to sample the GBuffer for drawing edge highlights on objects)

I'm using Unity 6000.3.6f1 and URP version 17.3.0.

#

This is the entire UniversalGBuffer pass I use to render to the GBuffer.

{
    Name "UniversalGBuffer"
    Tags { "LightMode" ="UniversalGBuffer" }
    HLSLPROGRAM
    #pragma vertex LitGBufferPassVertex
    #pragma fragment LitGBufferPassFragment
    struct Attributes
    {
        float4 positionOS   : POSITION;
        float3 normalOS     : NORMAL;
        float4 tangentOS    : TANGENT;
        float2 texcoord     : TEXCOORD0;
        float2 staticLightmapUV   : TEXCOORD1;
        float2 dynamicLightmapUV  : TEXCOORD2;
    };

    struct Varyings
    {
        float2 uv                       : TEXCOORD0;
        half3 normalWS                  : TEXCOORD2;
        float4 positionCS               : SV_POSITION;
    };


    //Vertex shader
    Varyings LitGBufferPassVertex(Attributes IN)
    {
         Varyings OUT;
        
        VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);
        VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS);



        OUT.positionCS = positionInputs.positionCS;
        OUT.uv = IN.texcoord;
        OUT.normalWS = normalInputs.normalWS;

        return OUT;
    }

    float4 LitGBufferPassFragment(Varyings IN) : SV_Target {
        float3 normal = normalize(IN.normalWS);

        normal = normal * 0.5 + 0.5;
        
        return float4(normal,1);
    }
    
    ENDHLSL
}
fierce sequoia
#

If you just need normal data I'd try the built-in _CameraNormalsTexture first (see DeclareNormalsTexture.hlsl include file under the URP ShaderLibrary)

winged thorn
#

tried both of those, couldn't get anything to work.

winged thorn
#

I've also tried sampling all the other _GBufferX textures (i.e. _GBuffer1, _GBuffer2, etc). Can only get grey.

#

also triple checked that I'm using the correct pipeline asset, which I am.

#

It's maybe worth noting I can sample depth fine as long as the shader is using the transparent queue. It otherwise uses the standard geometry queue

tired pasture
#

This may be because these textures are still writable pre this stage or a copy is made only post opaque

winged thorn
#

I've got them to be green.

#
float3 normals = SampleSceneNormals(IN.screenPosition.xy/IN.screenPosition.w);
return float4(normals.xyz,1);