Search Unity

Question Writing UI shaders for URP

Discussion in 'Shaders' started by Yamski, Apr 17, 2023.

  1. Yamski

    Yamski

    Joined:
    Sep 7, 2019
    Posts:
    5
    Hello
    I work on a UI based, mobile game and we're using URP. I'm not used to writing URP shaders and want to ask a few questions it.

    1. Can I use normal syntax using this shader as a base?
    2. What is the difference between using tex2D and and SAMPLE_TEXTURE2D, we're building for both android and iPhone.
    3. Do I need to #include "HLSLSupport.cginc" to our shaders?
    4. I'm getting compile errors when trying to use the new macro for sampling
    upload_2023-4-17_15-31-38.png

    Thanks!
     
  2. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    1. Yes. UI shaders are (mostly) independent of the render pipeline; you can use the same shader in built-in, URP, and HDRP for UGUI.
    2. In DirectX9 (early 2000s), textures and samplers were kind of combined. You should prefer the new macros like
    SAMPLE_TEXTURE2D
    , but the old
    tex2D
    syntax is not going away.
    3. No. If your shader has a
    .cg
    file exension, that will be included automatically.
    4. You need to add a sampler variable. When using the macros, instead of having
    sampler2D _MainTex;
    , you want
    Texture2D _MainTex; SamplerState sampler_MainTex;
    .
     
    Last edited: Apr 17, 2023
    Yamski likes this.
  3. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,016
    You probably mean "if the code in your shader is inside a
    CGPROGRAM / ENDCG
    pair"?
    .cg
    is not a for shaders, but we still recognise it as a valid include extension.
     
    burningmime likes this.
  4. Yamski

    Yamski

    Joined:
    Sep 7, 2019
    Posts:
    5
    Thanks for answering!
    I've noticed you changed your second answer after some time :)
    I will try to add the sampler var.