views:

34

answers:

2

I am attempting to use 3D textures to texture my randomly generated terrain. I want to be able to have many different terrain textures that blend between different areas. I can't seem to figure out what part of the texture the R coordinate will pick up. If I set it to 0.0 then it gives a blend of the texture from before 1.0 and after 0.0. It seems to have a few points where it is not blending at all. How does the R coordinate(float) work with the depth of the 3D texture? (IE. depth divided by something multiplied by something etc.)

An example of what I need to figure this out for: I have a 3D texture that is a 128x512 image. The first 128x256 is the grass texture. The 2nd half is the dirt texture. I am treating the texture as if it is 4 128x128 tiles, so the depth is 4. What math do I do to find out the float R coordinate for a solid grass texture? What math do I do to find the start/end float R coordinates for blending from dirt to grass?

Any help is appreciated!

A: 

I realized that I did something wrong with my code that made me think it wasn't simply getting texture surrounding R, when it actually is. So the first texture is at 1.0/(depth*2).

Barakat
+1  A: 

You figured out that to sample from a layer 0, you have to use r = 1 / (depth * 2). In general, to get layer N, you'd use r = (N + 1/2) / depth

This result is the same for the first 2 coordinates, by the way. If you want to have a triangle that has exactly the value of the first texel of your 2/d texture, then you need to pass the coordinates (0.5/width, 0.5/height) to each of the vertices.

But, but... what you're trying to do is what texture arrays have been invented for (DX10 era feature). 3D textures are a poor fit for a number of reasons, the biggest issue being that mip-mapping does not work like you would want it to (mipmap between the 2D levels independently of the third dimension).

Mip-mapping is critical to reduce aliasing artifacts (especially on terrain, where it spans all the depth range). When you start turning on mip-mapping on your layers, you'll see that the different material layer will start blending regardless of your texture coordinates when the terrain is further away. That's what mipmapping is for after all... your 128x128x4 texture becomes 64x64x2, 32x32x1, 16x16x1 as you use them on geometries that are further away. When you reach down the 32x32x1 level, you only have 1 layer left, that's likely going to be a bland mixture of your dirt and grass. Again, not what you want.

In contrast, texture arrays will give 128x128[4], 64x64[4], 32x32[4], 16x16[4], where you keep the 4 layers you want to select the various materials regardless of mipmapping.

Bahbar
I'm not sure if texture arrays will also let me do this easily, but the main reason I am using 3d textures is so that I can blend between different textures easily. To blend between tile 1 and tile 2 all I have to do is interpolate between 1/(depth*2) - 2/(depth*2). I have not noticed any artifacts so far, but if I see any that I can't fix I will definitely keep your suggestion in mind. Thanks!
Barakat