Search Unity

Resolved Triplanar UV mapping against untransformed mesh coordinates

Discussion in 'Shader Graph' started by lcompton, Mar 14, 2021.

  1. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
    I'm using HDRP/Lit materials with a triplanar base UV mapping for consistent tiling of large objects in an interior space, which I'm very happy with. The technique does a great job of making separate objects appear to be cut from the same large block of material, which is the look I'm going for.

    I'd like to apply this technique to objects that move or rotate. I realize that world coordinates are used for the triplanar base UV mapping in HDRP/Lit materials and in the corresponding Triplanar node in shader graph. What I'd like to be able to do is apply the triplanar UV mapping to the object without the application of the object's transform. I've done some experimentation with shader graph, but haven't yet found the right way to do this. How do you access an object's untransformed mesh coordinates in shader graph?
     
  2. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
    I came up with a solution using transformation matrices. Here's the shader graph. The results are good overall, but the texture exhibits a bit more noise than in my corresponding HDRP/Lit material applied to static objects. I'm guessing that's caused by some precision loss somewhere in the computation chain.

    I'm still new to shaders and shader graph, so I don't claim to fully understand why this works. Applying the inverse model matrix to the position makes sense, but multiplying the position with the normal doesn't. That was just a lucky accident and is probably not the right manipulation of the normal vector. Feel free to suggest improvements and/or provide explanations. I'm interested in gaining a better understanding.

    DynamicTriplanar.png
     
    Last edited: Mar 16, 2021
  3. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
    I did a bit more fiddling and came up with something slightly more intuitive...

    DynamicTriplanarV2.png

    Anyway, hopefully this will be useful to someone. Cheers!
     
  4. JJRivers

    JJRivers

    Joined:
    Oct 16, 2018
    Posts:
    137
    This was Exactly what i was looking for and i was amazed no one else had run into it or atleast hadnt shared it anywhere. I had my own solution to it but it ate 20x the nodes and was horrible as heck with performance. Thank you a lot!
     
  5. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
    That's great! I agree. I was surprised there weren't other posts about this. I'm also surprised that there aren't options for different triplanar coordinate spaces on the HDRP materials. I might create a feature request for that. If so, I'll post the link here.
     
    JJRivers likes this.