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

Question How to sample a texture in HDRP custom post process

Discussion in 'Shaders' started by KrisDevelopment, May 11, 2021.

  1. KrisDevelopment

    KrisDevelopment

    Joined:
    Jul 26, 2015
    Posts:
    36
    Hi, I'm building a post process that takes in an input image and another texture that I need to sample. Now the input image sampling is alright (1 to 1 with the grayscale post-process example in HDRP scripting docs), but I can't figure out how am I supposed to sample a normal texture.

    This is what I've got so far:
    Shader:
    Code (CSharp):
    1.  
    2.         TEXTURE2D_X(_InputTexture);
    3.         TEXTURE2D_X(_WorldMask);
    4. ......
    5.         float4 maskTexture = SAMPLE_TEXTURE2D_X(_WorldMask, s_linear_clamp_sampler, uv);
    Post Process:

    Code (CSharp):
    1.  
    2. public BoolParameter UseDebug = new BoolParameter(false);
    3. public TextureParameter DebugTexture = new TextureParameter(null);
    4.  
    5. public override void Render(CommandBuffer cmd, HDCamera hdCamera, RTHandle source, RTHandle destination)
    6. {
    7.     var cam = hdCamera.camera;
    8.     var material = GetMaterial();
    9.     material.SetTexture(_MaskProperty, GetMask());
    10.     material.SetTexture(_InputTextureProperty, source);
    11.     HDUtils.DrawFullScreen(cmd, material, destination);
    12. }
    13.  
    14. private Texture GetMask()
    15. {
    16. #if UNITY_EDITOR
    17.     return UseDebug.value ? DebugTexture.value : WorldMask;
    18. #else
    19.     return WorldMask;
    20. #endif
    21. }
    When I render the image it is just gray-ish, regardless of what texture I put in. Does it have something to do with that RTHandle type?
    There aren't many resources on HDRP shader scripting I could find.
     
  2. KrisDevelopment

    KrisDevelopment

    Joined:
    Jul 26, 2015
    Posts:
    36
    Update: replacing SAMPLE_TEXTURE2D_X with SAMPLE_TEXTURE2D and TEXTURE2D_X with TEXTURE2D makes it work.
    Now anyone know why that is?
     
    Last edited: May 11, 2021
  3. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,896
    SAMPLE_TEXTURE2D_X is meant to be used with texture arrays which _WorldMask isn't. Regular textures should just use SAMPLE_TEXTURE2D (ie. mask textures).
     
    KrisDevelopment likes this.