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 can to handle RGBA(4) output from Texture2DArray when node requires a Texture2D input?

Discussion in 'Shader Graph' started by fomafomitch, Aug 1, 2023.

  1. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    89
    Hello,

    I have a triplanar node for my procedural map that works great. The problem is that this triplanar node only take an input of a Texture2D.

    In my map, I store Texture2D index in UV map for vertices and I change Texture2DArray using this index cells.

    The problem is that I can't output a RGBA into Triplanar node.

    upload_2023-8-1_23-18-34.png


    What solution I can see :
    - (blue) Export the UV map from triplanar and input it to Texture2D array (this need to edit triplanar node).
    - (red) Convert RGBA to Texture2D
    - (green) Change input of Triplanar node to Texture2D array
    - Use a if/else expression from my TextureIndex and output the corresponding texture2D (I know if/else on shader is bad practice, iI need to do it for like 20 textures).

    Do you think it's possible ? If yes how ?
     
  2. BenCloward

    BenCloward

    Unity Technologies

    Joined:
    Jul 15, 2021
    Posts:
    145
    What you're asking for is pretty specialized. You need an Array version of the Triplanar node. What I'd recommend is that you use the source code for the Triplanar node that's available in the documentation here: https://docs.unity3d.com/Packages/com.unity.shadergraph@16.0/manual/Triplanar-Node.html Put that into a custom code node and make your own new version of the Triplanar node that samples array textures instead of standard 2d textures. That can be done by replacing the instances of SAMPLE_TEXTURE2D in the code with SAMPLE_TEXTURE2DARRAY instead. If you don't know how to do that, ask and engineer on your team for help. If you don't have an available engineer, let me know here and I'll do it.
     
  3. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    89
    Thanks you !
    I didn't know that this code on the documentation was the .HSLS code. First I tried to edit TiplanarNode.cs on the package but it was protected of course...

    So I made a custom node function taking Texture2Darray and index as input and it's working !

    upload_2023-8-2_21-1-26.png
     
  4. BenCloward

    BenCloward

    Unity Technologies

    Joined:
    Jul 15, 2021
    Posts:
    145
    The looks perfect from what I can see in your screen shot - except you'll want to create a Normal Vector node and plug that into the Normal input port instead of the Position node as it is now. Nice work with that!
     
  5. BenCloward

    BenCloward

    Unity Technologies

    Joined:
    Jul 15, 2021
    Posts:
    145
    Also, if you make a new Subgraph and put your Custom Function node in that, you'll be able to save it out and reuse it in other graphs if needed.