Search Unity

Resolved Shadergraph Sample Texture 2D anisotrophic filtering

Discussion in 'Shader Graph' started by Srokaaa, Nov 2, 2019.

  1. Srokaaa

    Srokaaa

    Joined:
    Sep 18, 2018
    Posts:
    169
    Hello,

    I have a problem with mimicking a UPR/Unlit shader texture sampling in custom ShaderGraph shader. How can I make ShaderGraph generated shader handle mipmaps the same as Unlit shader? In the picture below you can see Unlit shader result on the left and my ShaderGraph shader on the right. As you can see on the right the texture is "cut off" above some ditance.

    upload_2019-11-2_9-52-20.png
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    My guess is you have a Sample Texture 2D node that you’ve attached a Sampler State node to. The solution is to delete the Sampler State node and leave that input without anything connected. That’ll use the texture’s default sampler state, which allows for anisotropic filtering.
     
  3. Srokaaa

    Srokaaa

    Joined:
    Sep 18, 2018
    Posts:
    169
    @bgolus I tried both, with and without, SampleState connected and the result is the same :/ Here is how my graph looks like:
    upload_2019-11-3_9-57-32.png

    I also tried using Sample Texture 2D LOD and the results are different but still quite unsatisfactory compared to default Unlit node:
    upload_2019-11-3_10-4-50.png

    I tried figuring out what is the difference between Unlit shader code and to code generated by ShaderGraph nad the only difference I could figure out is that Unlit shader uses such methods TRANSFORM_TEX in vertex shader:
    Code (CSharp):
    1. output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
    2.  
    and SAMPLE_TEXTURE2D in fragment shader:

    Code (CSharp):
    1.                 half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
    2.                 half3 color = texColor.rgb * _BaseColor.rgb;
    3.                 half alpha = texColor.a * _BaseColor.a;
    4.  
    whereas shader generated from Shadergraph uses a texture delivered straight to fragment shader as argument:
    Code (CSharp):
    1. fragment Mtl_FragmentOut xlatMtlMain(
    2.     sampler sampler_BaseMap [[ sampler (0) ]],
    3.     texture2d<half, access::sample > _BaseMap [[ texture(0) ]] ,
    4.     Mtl_FragmentIn input [[ stage_in ]])
    5.  
    and this method to do sampling:

    Code (CSharp):
    1.     u_xlat16_0 = _BaseMap.sample(sampler_BaseMap, input.TEXCOORD0.xy);
    2.  
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Those two lines are identical. The above
    SAMPLE_TEXTURE2D
    macro just expands out the parameters you pass in to exactly the same line of code as the second example (though, obviously with
    uv
    instead of
    input.TEXCOORD0.xy
    ). The TRANSFORM_TEX isn't really doing anything either. If you've got a scale and offset on the texture property, it'll apply that scale and offset. If not (or the scale is 1 and the offset is 0) it doesn't change anything at all.

    And they're both for sure using the same texture?

    And is the Unlit node's Surface type set to Opaque or Transparent? I was assuming the blue glow was the same texture, but I realize that might not be true, and I'm now wondering if you have your shader set to Opaque and it's using alpha testing rather than alpha blending.
     
  5. Srokaaa

    Srokaaa

    Joined:
    Sep 18, 2018
    Posts:
    169
    @bgolus Thanks for explanation :)

    Yep, I double checked and they are using the same texture.

    Blue glow is a separate Game Object. I should have mentioned it in the first post. Surface type is set to Transparent:
    upload_2019-11-5_12-34-16.png

    I ended up just copying the Unlit shader source code and adding the functionality I wanted in shaderlab. In general I find it really annoying that ShaderGraph doesn't provide few default nodes that would work exactly like default shaders for given render pipeline - eg. Unlit, Simple Lit, Lit. Plenty of the times people don't want to writhe whole shader themselves, they just want to add some functionality to a default shader. Enough of this rant :)
    Thanks for help, if you have any more ideas what could be wrong I would like to hear them because it seems like something potentially useful in the future
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    As a last ditch guess, what happens if you set the AlphaClipThreshold to 0.0?

    Also, what version of Unity & what type & version of SRP are you using? I've never seen an Unlit master node with the vertex normal & tangent exposed.
     
  7. Srokaaa

    Srokaaa

    Joined:
    Sep 18, 2018
    Posts:
    169
    @bgolus You, sir, are a wizard :) It was AlphaClipThreshold all along. No idea how could I not tinker with that in the first place. Thank you once again

    It's 7.1.2 btw