Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Alternative solution for nointerpolation in surface shaders?

Discussion in 'Shaders' started by richard-firehose, May 20, 2022.

  1. richard-firehose

    richard-firehose

    Joined:
    May 31, 2019
    Posts:
    15
    Hi all,

    I've seen a couple posts about this topic but I haven't seen any workarounds yet for this problem.

    I'm making a very expensive triplanar terrain shader with stochastic sampling that supports up to 40 materials using a texture array. I have I've found runs MUCH better when I cache the top 4 indices of the materials in my texture array in a texcoord (as opposed to finding the top 4 blend indices inside the surf function). I'm not particularly sure why, it seems to be that texture sampling works better when the indices are passed from the vertex shader rather than calculated in the fragment shader.

    However there is a fundamental issue to this technique which is the transitions between materials. Since I'm passing in the indices and caching all their relevant data (like their metallic values, normal strengths, emissionColors, etc), the v2f struct interpolates the indices and indexed material data giving me a nice little band of other materials before arriving at the next proper material. If I could stop interpolation from happening on all the values except for the blend values of each material, then I would be able to prevent this glitch from happening. However since this is a surface shader, the nointerpolation keyword is not supported.

    Does anyone know of a way to prevent the interpolation or a way to circumvent the issue?

    Thanks!

    upload_2022-5-20_10-12-31.png
    ^Image of the material banding in case its useful to see
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,255
    Unfortunately, not really no. There are a few ways around this problem, but literally none of them are possible with Surface Shaders, or requires custom mesh setup to bake special data in the vertices.
     
  3. richard-firehose

    richard-firehose

    Joined:
    May 31, 2019
    Posts:
    15
    Thanks for the reply! Do you know of any resources I can look into to either recreate the appearance of a surface shader in a vert frag shader or how to do this special vertex data system? With the surface shader conversion I’ve heard there’s a way to convert the surface shader into a vert frag shader before it gets compiled, how do I go about doing that? With the vertex data, I’m already caching lots of data into the texcoords of the vertices, but still have texcoords2 and 3 available so that could work too. I’m also using the interpolaters32 keyword as well so it’s not a problem to add more data into the v2f struct. I’m not opposed to trying either option. Whichever you think is less of a headache to set up!
     
  4. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
    Isn't that what Megasplat from @jbooth is doing?
     
    richard-firehose likes this.
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,255
    Yes, Megasplat is using triangle barycentrics to reconstruct the data from each vertex.
     
    richard-firehose likes this.
  6. richard-firehose

    richard-firehose

    Joined:
    May 31, 2019
    Posts:
    15
    Oh brilliant, I will buy that asset and look into how he does it then! Thank you so much!
     
  7. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,446
    This is kind of a dead end on Unity terrain, since the vertices are not stable and change resolutions based on view. Basically you have to do this work in the fragment shader.
     
  8. richard-firehose

    richard-firehose

    Joined:
    May 31, 2019
    Posts:
    15
    Oh I’m generating meshes at runtime, this is for a voxel based mesh system. So this should be no problem!