How does blending between textures work?

Actually, I mean interpolating between textures. Specifically, on the terrain.

In the terrain shader, how do you interpolate between textures? I’m not talking about 25% grass and 75% sand in one tile. I mean between tiles…you have a solid dirt tile next to a solid grass tile, and they transition from one to the other.
I’ve looked at the shaders, and I don’t see where this happens or even how to control it. I’d be curious if it were possible to change the transition ‘width’.

Can anyone help me understand this?

You should be able to do this just by using the brushes and turning down the opacity a bit.

I believe what happens is the textures are all added together in multiple passes, multiplied by the “splatmap” mask. Each channel of the splatmap represents the varying weights for each texture across the terrain. The sum of each weight for each texture at any given point on the terrain should add up to 1.

I think Unity speeds up this process by adding together up to four textures per pass, the four textures corresponding to the four RGBA channels of the splatmap.

Pseudocode for this might look like:

for each pixel
{
    float4 pixel = 0
    for each texture
    {
        float4 textureSample = sample of this texture at (U,V)
        float splatmapSample = sample of splat map channel for this texture at (U,V)
        pixel += textureSample * splatmapSample
    }
}

@Zergling103, wouldn’t this be how it handles multiple textures per tile?
I mean, let’s say you have a solid terrain tile of dirt, and right next to it you have another solid tile of grass.

There appears to be a linear interpolation between them, from one tile to another.

You’re thinking about this in the wrong way; Unity doesn’t use tiles for it’s terrain. It uses textures for everything, such as a heightmap, which it computes geometry on the fly. The density of the terrain mesh locally varies with distance, depending the LOD settings you’ve used for the terrain.

I suppose you could say that each pixel of the heightmap and splatmap (if they are the same resolution) could represent a tile. The splatmaps have interpolation between pixels just like what you’d expect from any other texture map. So yes, there is interpolation but this interpolation comes from texture filtering.

If you want the interpolation between pixels of the splatmap to be tighter and brushing in sharper texture details doesn’t work, the only option left is to increase the splatmap’s resolution and try again. You might have to resize the terrain but this might erase all of your splatmap data - a better option might be to open the splatmap image in an image editor and resize it there, but I don’t know if this would work.

Interesting…I suppose i never thought of the terrain that way.

So…let’s say I decide to use a custom mesh instead, which is split into regular tiles like the terrain. Along the X and Z axis, each ‘tile’ is 1m. y controls the height.

I can texture this easily, by generating an atlas of the textures and my own UV coordinates. BUT: There will be no interpolation between the “tiles” if I understand right. The transition between one tile and another will be sharp.

Normally, people don’t want this…they want a grass “tile” to blend to the next one over.

So why does the Unity terrain get an extra level of filtering between tiles? I think I’m missing some basic thing here.

[Edit] Ok…I read back over your pseudocode, and I think the lighting is starting to come on. Filtering is happening at the splat map level, and then at the textures level for the terrain. Maybe. I still don’t have a clear picture of this, but maybe I’m on the right track.