Cannot convert UnityTexture2D to Texture2D<float4> for custom function node

I keep getting this error but do not know how to fix it.

I have a function that takes in a Texture2D and a SampleState but it give my custom node an error about converting the two textures.

How do i fix this problem?

My code looks like this:

void RGBSplit_float(float Split, Texture2D Texture, SamplerState Sampler, float2 UV, out float3 Out)
{
    float2 offset = float2(Split,Split);
    float r = SAMPLE_TEXTURE2D(Texture, Sampler, UV - offset).r;
    float g = SAMPLE_TEXTURE2D(Texture, Sampler, UV).g;
    float b = SAMPLE_TEXTURE2D(Texture, Sampler, UV + offset).b;
    Out = float3(r,g,b);
}

I am passing in my Texture in Shader Graph linked like this:

8621370--1157940--upload_2022-11-29_4-36-8.png

So why does my custom node give this error ? And how do i fix it ? My Shader Graph version is 131.8 if that matters.

The full error:

Shader error in 'Master': 'RGBSplit_float': cannot convert from 'struct UnityTexture2D' to 'Texture2D<float4>' at line 200 (on d3d11)

Try replacing Texture2D with UnityTexture2D.

These SAMPLE_ macros take UnityTexture2D.

The other day I had the same problem when using SAMPLE_TEXTURE2D_LOD

void GetSampleLOD_float(UnityTexture2D tex, float2 uv, float offset, float lod, out float4 color)
{
    // Note: can't use Texture2D, much better to use UnityTexture2D
    // for tex's sampler state use "tex.samplerState"

    float o = offset;
    color = SAMPLE_TEXTURE2D_LOD(tex, tex.samplerstate, uv + float2( o,  0), lod);
}
4 Likes

How did you figure this out I would never have guessed this…

On a ShaderGraph’s asset, I clicked the button View Generated Shader and within the garbled trash that it spat out there was this magical line which really got the noggin joggin:

UnityTexture2D _Property_b3eaacee9c4946148e9c27166093c088_Out_0 = UnityBuildTexture2DStructNoScale(_MainTex);

Thank you, _Property_b3eaacee9c4946148e9c27166093c088 !!!

1 Like

Hmm I have no idea how to interpret that line - still new to shader code so this is very block box and hard to decipher at the moment. I was hoping URP/HDRP would make writing shaders easier than the old built in ways ! :frowning:

Such an example is still missing from the LTS Shadergraph documentation! I have to google UnityTexture2D to find out what the struct actually contains - then you can get an idea about using it!
Graphics/Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl at master · Unity-Technologies/Graphics (github.com)

As for how to pass in the screen’s colour texture or normal buffer to the hlsl for a full screen effect, I am still trying to work out…!