My custom function accepts a variable of type UnityTexture2D and I would like to check if it is initialized. Since there are no null in hlsl, so I would like to check the value of the parameters of the texture itself or something. However, the problem is that I cannot find anything similar anywhere, in particular, the GetTextureDimensions function is also not available for UnityTexture2D.
I’ve tried adding a similar texel check, but it still returns true, even when no texture is set, I’m assuming that Unity defaults to some “empty” texture, since my function returns white, even though it shouldn’t be)
![]()
I looked through github, but I didn’t see what I could do to solve the problems here
Graphics/Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl at master · Unity-Technologies/Graphics
in shaders, there exists an easy way to find the dimensions of a given texture. say you have _MainTex, add a float4 _MainTex_TexelSize before the vert frag programs, and it will automatically fill the variable with the texel size of whichever texture you choose to name it after. it is a float4, and xy contains 1 / texel size, z and w contain normal texel size (texel size being pixel number).
by the way, unity does initialize the shaders with very small textures if there is no input. you can actually choose between them, though i don’t know if you can in urp. i think there’s only white grey and black.
I don’t think this is a very good solution because I’m using a Shader Graph with a Custom Function node, so I’m a little confused about how to do it, except to try to set the variable you’re talking about before the function, since it seems possible with file functions, I’ll try now.
I tried using variable (Texture_TexelSize) declaration before the function, but it seems the automatic initialization of variables by name doesn’t work with function parameters (my param is UnityTexture2D Texture).
hahah. if i had known you were using shader graph i would have just linked you this.
Texel Size Node | Shader Graph | 6.9.2
something interesting though, the generated code seems to be exactly the same as what you wrote, but i’m not really experienced with shader graph, so it could be that if the node isn’t in the graph it doesn’t pass the variable… who really knows.
I already found a solution (using certain ranges from the ZW texel parameter) so I think this post can be closed, I found the answer here () but thank you for your time as well
It just sounds wild, but the problem that caused me to think that the texel was unclear how it was defined was that my use of the uv offset (shown in the picture) seemed to override the condition (I thought so). but it seems that the problem was that my picture had a sampler of the clamp mode, which is why it was not painted
![]()