#How to sample my global texture in shader graph custom node

1 messages · Page 1 of 1 (latest)

mint bison
#

I am trying to put more of my shader graph logic into code to get rid of the spaghetti hellscape that its become.

I set a global test in C#:

Shader.SetGlobalTexture("_MyTexture", _myTexture);

Then in my custom node i call this function:

UnityTexture2D _MyTexture;
UnitySamplerState sampler_MyTexture;
void Test_float(float4 screenPos, out float3 color)
{
    float2 uv = screenPos.xy / screenPos.w; 
    color = _MyTexture.Sample(sampler_MyTexture, uv).rgb;
}

But i get this error:

ps_4_0 does not allow textures or samplers to be members of compound types

What is the correct way to set this up for globally set textures? How does Unity do it for sampling Scene Depth for example?

ionic relic
#

In URP for example, scene depth is declared as a TEXTURE2D_X_FlOAT

mint bison
#

I'm not 100% sure i understand how to write the syntax based on these files. A bit confusing, so do i just do:

_MyTexture.Sample(uv); ?

is that also the case for the global textures like depth/depth normals?

plush brook
#

You would use _MyTexture.Sample(_MyTexture.samplerstate, uv)
Or SAMPLE_TEXTURE2D(_MyTexture, _MyTexture.samplerstate, uv);

#

Though I usually only use the UnityTexture2D if passing textures as a param and haven't tested as a global reference. SG might declare differently & pack into the UnityTexture2D structure behind the scenes.

If it isn't needed as a param, could probably just use the usual macros : TEXTURE2D(_MyTexture) and SAMPLER(sampler_MyTexture), SAMPLE_TEXTURE2D(_MyTexture, sampler_MyTexture, uv);
.

#

For depth/normal textures I think they need to support stereo rendering for VR so use the TEXTURE2D_X macros instead.
But usually better to just #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl" (the file Remy linked), or DeclareNormalsTexture.hlsl for normals texture, then call a sample function inside those. (But likely need to wrap in #ifndef SHADERGRAPH_PREVIEW blocks to prevent conflicts with shadergraph's shaderlibrary used for previews)

mint bison
#

i see , damn thats a lot random things to take into account along the way lol

mint bison
#

@plush brookwhen i tried _MyTexture.samplerstate i get this error:

Shader error in 'Shader Graphs/Master': 'UnityTexture2D::Sample': cannot implicitly convert from 'const float2' to 'struct UnitySamplerState'```
---
#
depth.Normal = depthNormalTexture.Sample(depthNormalTexture.samplerstate, uvSS).xyz;```

i passed it in as a parameter in my custom node
plush brook
#

Maybe try the macro version instead SAMPLE_TEXTURE2D(_MyTexture, _MyTexture.samplerstate, uv);

mint bison
#

now the error has changed though similar kind of error:
cannot implicitly convert from 'struct UnityTexture2D' to 'int' at /(312) (on d3d11)

my custom script doesn't even reach line 312 so i don't know what line it's referring to

#

strangely, my shader graph compiles fine but console log has the error anyway