Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SamplerState in custom Shader Graph node

Discussion in 'Shaders' started by NumT, Mar 19, 2019.

  1. NumT

    NumT

    Joined:
    Feb 13, 2014
    Posts:
    22
    I'm trying to produce my own custom alternative to the shader graph triplanar node so that I can use different texture inputs for the different world axes. So my approach initially is to simply reproduce the existing Triplanar node as a starting points. Problem is I can't seem to work out how to properly declare and use a sampler state in my custom shader graph node so I get an "assertion failed on expression: 'success'" error message.

    Any idea what I'm doing wrong? Here is the code I am using:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor.ShaderGraph;
    3. using System.Reflection;
    4.  
    5. [Title("Custom", "My Custom Node")]
    6. public class MyCustomNode : CodeFunctionNode
    7. {
    8.     public MyCustomNode()
    9.     {
    10.         name = "My Custom Node";
    11.     }
    12.  
    13.     static string MyCustomFunction(
    14.         [Slot(0, Binding.None)] Texture2D Texture,
    15.         [Slot(1, Binding.WorldSpacePosition)] Vector3 Position,
    16.         [Slot(2, Binding.WorldSpaceNormal)] Vector3 Normal,
    17.         [Slot(3, Binding.None)] DynamicDimensionVector Tile,
    18.         [Slot(4, Binding.None)] DynamicDimensionVector Blend,
    19.         [Slot(5, Binding.None)] SamplerState Sampler,
    20.         [Slot(6, Binding.None)] out Vector4 Out)
    21.     {
    22.         Out = Vector4.zero;
    23.         return
    24.             @"
    25. {
    26.  
    27.    float3 Node_UV = Position * Tile;
    28.    float3 Node_Blend = pow(abs(Normal), Blend);
    29.    Node_Blend /= dot(Node_Blend, 1.0);
    30.    float4 Node_X = SAMPLE_TEXTURE2D(Texture, Sampler, Node_UV.zy);
    31.    float4 Node_Y = SAMPLE_TEXTURE2D(Texture, Sampler, Node_UV.xz);
    32.    float4 Node_Z = SAMPLE_TEXTURE2D(Texture, Sampler, Node_UV.xy);
    33.    float4 Out = Node_X * Node_Blend.x + Node_Y * Node_Blend.y + Node_Z * Node_Blend.z;
    34. }
    35. ;
    36. ";
    37.     }
    38.  
    39.     protected override MethodInfo GetFunctionToConvert()
    40.     {
    41.         return GetType().GetMethod("MyCustomFunction", BindingFlags.Static | BindingFlags.NonPublic);
    42.     }
    43. }
    44.  
     
  2. NumT

    NumT

    Joined:
    Feb 13, 2014
    Posts:
    22
    Ok so so far I've found out that even having a slot for the SamplerState Sampler throws an error regardless of anything else so for now I've had to remove it. So it begs the question - how are we supposed to sample our textures in Custom Shader Graph Nodes???
     
  3. NumT

    NumT

    Joined:
    Feb 13, 2014
    Posts:
    22
    Oh.... the API for custom nodes is being reworked. Ok well ignore this thread for now then :/
     
  4. andrzej_

    andrzej_

    Joined:
    Dec 2, 2016
    Posts:
    81
    Sorry to dig this up after a year, but I had a similar issue and just added the sampler state and pass the 'ss' variable in the SAMPLE_TEXTURE2D but don't provide any input for it and it works surprisingly.

    upload_2020-1-7_19-0-3.png