[UE4] Distance Based Texture Tiling

polycounter lvl 3
Offline / Send Message
Molokotof polycounter lvl 3
Hi guys!

I made a material function a couple of months ago to overcome the issue of terrain textures visibly being tiled at a distance.

It basically has multiple world-aligned texture nodes with different texture size inputs (could be done with standard texture samplers as well), and lerps between them depending on distance to the camera.

I suspect something like this has been done before as it seemed pretty straight-forward to me.

Anyway if there's any interest in this I'll have a look at making it more efficient and parameterised and put it up online eventually.

C&C very much welcome.


  • LTS3D
    Offline / Send Message
    LTS3D polycounter lvl 2
    I'd be interested, I've messed with doing similar things in the past but never done a full fledged function. You aren't lerping between two WorldAlignedTexture functions for one TextureObject right? You're just Lerping the sizes?
  • Obscura
    Online / Send Message
    Obscura ngon master
    Based on what he said, not. If I'm not mistaken. Thats a really good idea though.

    @Molokotof - can you upload a less compressed gif, or a video please?
  • Molokotof
    Offline / Send Message
    Molokotof polycounter lvl 3
    @Obscura Yeah, I'll make a less compressed video and upload it to youtube later today. Was really trying to keep the GIF size down.
    I'll also but up a couple of screenshots of the material graph.

    And @LTS3D ; I'm actually lerping between the WorldAlignedTexture functions.

    I tried lerping between sizes, but that gave me the issue that the texture was actually deforming (warping) in really strange ways as the size changed.

    Atm it's using several (I think 5) WorldAlignedTexture functions, all with different texture sizes, for 5 different levels of zoom (highly inefficient).
    But your comment just gave me an idea:

    I might be able to have only 2 WorldAlignedTexture functions and actually alternate between lerping between those two functions and lerping the actual size of the texture. That way i should be able to cover up the warping of the texture.

  • Molokotof
    Offline / Send Message
    Molokotof polycounter lvl 3
    Here's a video, the quality isn't much better as it seams my screen recording skills aren't that great :p
    but you can put the video in fullscreen and play it at .25 or .5 playback speed and it should be clear what the material is doing.

    I also had a second look at the function (as it's been some time ago that i actually made it). And it seems some more rework will be required to get it working the way i described at the end of my last post.

    So I will focus on finishing my other gun project before focussing more on this.

    Meanwhile here's a link to download the uasset

    It's still messy / unoptimized, but the easiest way to give feedback crit is if you can look at the graph yourself.

  • Mant1k0re
    Offline / Send Message
    Mant1k0re polycounter lvl 3
    Forgive my ignorance but how is this new or special? I'm not saying it's not I just don't understand how it's different from all the terrain shaders (some from the marketplace, some from the /Learn projects from Epic that are doing the same thing - and by this I mean hide the tiling of the texture at a distance, not that it does exactly what you're doing. I'm quite okay with what those existing solutions are doing to address this issue, and it's not obvious from the video how it's better or different.

    If you could spare the time to put it in layman's term for my education, I'd be grateful :)
  • Molokotof
    Offline / Send Message
    Molokotof polycounter lvl 3
    Oh no problem, I indeed doubt myself that this is anything new (as I mentioned in my original post). And as such I'm not claiming it to be new or original. 
    I don't know how all the other solutions work (apart from wang tiling which is pretty labour intensive to set up) 
    I just needed a solution for this tiling problem and I decided to make something myself. 
    Looking into the content examples (or google it for that matter) might have saved me a lot of time. 
    I'll try and explain it, but I suggest you take a look at the material graph (download link is in my previous post) if you want to follow along visually. As the graph is a bit too big too post online: 
    1. I calculate the distance between the camera and the object I'm looking at. 
    2 This distance is used together with 2 parameters (1 to tell the material where to start the distance fade initially, the other determines the size of the distance intervals). These basically drive some math to calculate the output of 5 integers which always have a sum of 1. 
    3.  All those integers correspond to a different distance interval, let's say for illustrative purposes:  
       - Distance object to camera = 250 units. 
       - 250 Units falls within the 3rd distance interval => int1 = 0, int2= 0 int3 = 1, int4=0, int5=0. 
    4. These integers then feed into lerps between 5 different WorldAlignedTexture nodes, all with different texture sizes. 
    5. The nodes then feed into the material inputs. 
    Issues that need fixing: 
    - Amount of distance intervals cannot be parameterized in instances (it's always 5 different intervals hard coded into the material) 
    - There are a lot of WorldAlignedTexture nodes used (lot's of shader instructions) as: 5 (distance intervals) x 3 (Albedo,Rough,Normal) = 15 WorldAlignedTexture nodes. 
    Don't know how to parameterise the amount of intervals, 
    But the WorldAlignedTexture nodes used will be lowered by greypacking + a system that switches between two nodes a I change their texture size. 
    I hope my explanation + the graph cleared it up a bit. If you still have any questions don't hesitate to ask.
  • Mant1k0re
    Offline / Send Message
    Mant1k0re polycounter lvl 3
    Yeah I totally get how it's different from the solution I had in mind now, thanks for the explanation. If I understand what you said correctly based on the distance from camera the texture you are seeing is swapped for the same one but at a different tiling setting? The solution from epic that I know best is simply to have the texture transition to a unique uniform color when you're getting real far, for instance your grass texture eventually is only a shade of green and that's it. You can also swap to a few different uniform color with a macro variation mask to have something a bit more elaborate, it's not very pretty pretty but with a little bit of fog and other effect you don't really notice. I can see how your solution is a lot better though.

    I'll have a look at your graph, I wonder how expansive it is in engine.

    Again thanks for the explanation :)
  • Molokotof
    Offline / Send Message
    Molokotof polycounter lvl 3
    Aha yeah, the uniform colour is definitely a more performant solution, but on cliff faces like in the video it would be pretty visible until you get really far. 

    And it's pretty expensive in engine, on one of my materials (that has other nodes as well) I'm getting 500+ instructions.
    Will try to make this more performant!
Sign In or Register to comment.