# Procedurally Texturing Marching Cubes

This topic is 767 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Using the Cubes algorithm, I am generating planets for my game. It currently works fairly well, but now I have to work on texturing it. What would be the best way going around setting up the UVs for marching cubes meshes? This needs to support different material types, and later on have some sort of blending. Any resources or code help would be greatly appreciated!

##### Share on other sites

This GPU gem chapter mentions a textureing technique:
http://http.developer.nvidia.com/GPUGems3/gpugems3_ch01.html

"A simple way to resolve this is to use triplanar texturing, or three different planar projections, one along each of the three primary axes (x, y, and z). At any given point, we use the projection that offers the least distortion (stretching) at that point—with some projections blending in the in-between areas, as in Figure 1-22. For example, a surface point whose normal vector pointed mostly in the +x or -x direction would use the yz planar projection. A blending range of roughly 10 to 20 degrees tends to work well; see Figure 1-23 for an illustration of how wide the blending range is."

Its mentioned towards the bottom.

##### Share on other sites

Since that example (1-3) deliberately eliminates axes with low weights, it might be best to test and not sample if weight is 0. I don't know why they allude to this and not do it in the sample code.

##### Share on other sites

Okay, I've done some research and made a tri-planar shader. It works wonderfully to not stretch textures - but here comes the hard part. How do I communicate which material each vertex of the mesh should be? I'm going to attempt using Vertex Colors that correspond to different textures - I'll post back if successful.

##### Share on other sites

Use whatever vertex attribute that suits you. I think I used ivec3 for material id and vec3 for alphas (to blend between 1-3 possible materials per triangle), which exploited barycentric coordinates to allow blending between different materials in all vertices of a triangle. Material indices were always the same for each vertex (so quite a data redundancy) to prevent interpolation. Mind that it was 5 years ago, so things may have changed or there is some better way to do this.

Say, you had triangle that had 2 vertices with material A and 1 vertex with material B, so this is a case of 2 verts sharing same material and 1 having different one:

v1 (has material A, uses 1st position in material vector)

material ivec3(A, A, B)

alpha vec3(1.0, 0.0, 0.0)

v2 (has material B, uses 2nd position in material vector)

material ivec3(A, B, A)

alpha vec3(0.0, 0.0, 1.0)

v3 (has material A, uses 1st position in material vector)

material ivec3 (A, B, A)

alpha(1.0, 0.0, 0.0)

Now case where each vertex has different material that has to be blended with others: A, B, C:

v1 (has material A, uses 1st position in material vector)

material ivec3(A, B, C)

alpha vec3(1.0, 0.0, 0.0)

v2 (has material B, uses 2rd position in material vector)

material ivec3(A, B, C)

alpha vec3(0.0, 1.0, 0.0)

v3 (has material C, uses 3st position in material vector)

material ivec3 (A, B, C)

alpha(0.0, 0.0, 1.0)

Hope this makes sense. One problem with this approach is that it's very costly, because triplanar means 3x sampling, and this solutions means another 3x sampling (for each vertex, even if they all share the same material). Add to it some normal/detail map and you're getting texture-sample bound :/

If I were to do this again, I'd go for more draw calls, but batched by number of unique materials. So if all vertices share the same material, it's one batch, another for 2 different materials, and another for 3 different materials (which will be ~5% of all draw calls). Triplanar can be very expensive in terms of samples made, so you will have to sacrifice draw call number for this. I remember my solution from years ago was really on the border of my GPU sampling capabilities with triplanar.

Actually, I will be bringing this back really soon, and there is no other way than triplanar for this - parametric generation of texcoords for such mesh is not easy - if anyone has done that I'd love to hear how :)

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013547
×