Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to TRANSFORM_TEXT(v.texcoord, _MainTex_ST) in Shader Graph?

Discussion in 'Shader Graph' started by jakesee, Aug 31, 2023.

  1. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81
    hello, I am trying to learn Shaders in Unity in general.

    I am trying to texture the extruded faces in a different texture but I don't know how to get the UV coords right. In my attempt, I am using the Triplanar node because it gives me the closest results, which is still incorrect, i.e. the texture is not aligned to the cliff face axis. I do not believe Triplanar node is required in this situation, I think I just do not understand the concepts of converting between different cartesian coordinate spaces.

    I read various shader code and found a common line in the vert() `TRANSFORM_TEXT(v.texcoord, _MainTex_ST)` that seems to be my answer, but I do not understand fully what this does and how to test this in the Shader Graph equivalent.

    In this example, I exported the model from Blender. The Blender's Cliff UVs are squished into a line (intentional), however, in below example, the cliff texture (grey) is not squished, which suggests to me that the Blender UVs are not used in Unity. So my question is, what is the default TEXTCOORD0 (UV0) used? How can I map this default UV0 using position data to the texture?

    My end goal is to generate this procedurally. In that case, do I also need to procedurally supply the UV0 coords, or can the Shader calculate it on the fly?

    Any advice please? Thanks.

    upload_2023-8-31_16-53-33.png
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,339
    If you are using triplanar, then it will not use UV as triplanar node uses world space coords to "stick" the texture from all axes.
    To be honest I am not sure I get what you want, how does blender uv looks like? Do you want to use UV from blender or generate UV from code? How it supposed to look like in the end?

    By the way I think TRANSFORM_TEX is macro for tiling and offset.
     
  3. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81
    @Qriva If you look at the rock texture (grey), I expect them to align to the face axis, i.e. it should look right-side-up the same as in the shader graph Triplanar node preview, but actually it is not. I don't know what it is currently aligned to, it is slanted in various direction.

    I don't want to use triplanar. I believe this can be achieved by simple mapping.

    If I am importing from Blender, I will just texture everything in Blender and will not have this problem. But I want to generate the model in Unity and procedurally texture it.

    Are you saying that I need to generate the UV in C# first, as it is not possible to map the texture in Shader alone?
     
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,339
    Well, triplanar takes world space axes by default (the red arrows), this is why the rock is aligned to top.

    I think you have two options, first one is to calculate new coords in shader. Never tried to spherize coords like this, so I can't tell you how exactly to do this, but you would probably need to find the light green tangent arrow and the blue one, but to do that you need both normal and tangent to be correctly set in mesh, then you could try polarize them. You could use sphere origin to calculate distance on blue arrow and autotile texture like that.

    The thing is if you generate this mesh anyway, then you can assign UV values directly during generation and this is the second option you have.

    upload_2023-8-31_20-16-43.png
     
  5. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    81