Search Unity

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

Discussion in 'Shader Graph' started by TheCelt, Nov 29, 2022.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    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:

    Code (CSharp):
    1. void RGBSplit_float(float Split, Texture2D Texture, SamplerState Sampler, float2 UV, out float3 Out)
    2. {
    3.     float2 offset = float2(Split,Split);
    4.     float r = SAMPLE_TEXTURE2D(Texture, Sampler, UV - offset).r;
    5.     float g = SAMPLE_TEXTURE2D(Texture, Sampler, UV).g;
    6.     float b = SAMPLE_TEXTURE2D(Texture, Sampler, UV + offset).b;
    7.     Out = float3(r,g,b);
    8. }
    I am passing in my Texture in Shader Graph linked like this:

    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:

    Code (CSharp):
    1. Shader error in 'Master': 'RGBSplit_float': cannot convert from 'struct UnityTexture2D' to 'Texture2D<float4>' at line 200 (on d3d11)
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    Try replacing
    Texture2D
    with
    UnityTexture2D
    .

    These SAMPLE_ macros take UnityTexture2D.

    The other day I had the same problem when using SAMPLE_TEXTURE2D_LOD
    Code (CSharp):
    1. void GetSampleLOD_float(UnityTexture2D tex, float2 uv, float offset, float lod, out float4 color)
    2. {
    3.     // Note: can't use Texture2D, much better to use UnityTexture2D
    4.     // for tex's sampler state use "tex.samplerState"
    5.  
    6.     float o = offset;
    7.     color = SAMPLE_TEXTURE2D_LOD(tex, tex.samplerstate, uv + float2( o,  0), lod);
    8. }
     
    aniline888, Kokowolo and TheCelt like this.
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    How did you figure this out I would never have guessed this...
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    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:
    Code (CSharp):
    1. UnityTexture2D _Property_b3eaacee9c4946148e9c27166093c088_Out_0 = UnityBuildTexture2DStructNoScale(_MainTex);
    Thank you, _Property_b3eaacee9c4946148e9c27166093c088 !!!
     
    Anhorse likes this.
  5. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742

    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 ! :(