- Home /
Apply texture to a mesh on all 3 sides
I am working on a marching cubes terrain, and I need to apply a texture to it. The problem is, I don't know how to apply the texture so the mesh looks "normal" from any angle. Right now, Ive been doing this:
uvs = new Vector2[mesh.vertices.length];
for (var i=0; i < uvs.Length; i+=1) {
uvs[i] = Vector2(mesh.vertices[i].x,mesh.vertices[i].z);
}
mesh.uv = uvs;
This code works, but the mesh doesn't draw right when a part of the mesh is completely straight up. That is because my code only applys the texture onto the x and z plane. I've been looking online for solutions, and couldn't find any source code. But I have learned that there is a way to apply texture on all 3 of the xyz planes, and simply blend the result together. Unfortunately, I have no idea how to do this.
If anyone out there knows how to apply a texture onto a mesh, please let me know. ( I know it will involve UVs and normals)
After doing further research, ive learned that what im trying to do is called triplanar texturing.
I would recommend getting some/all of the existing voxel add-ons from the Asset Store and see how they did it. VoxelForm is my favorite.
I don't have the money to buy any voxel add-ons, and im pretty sure someone out there knows how to do this. Does anyone have any sample code, or a website that explains how to apply a texture onto a mesh from all 3 sides.
O$$anonymous$$, after doing further thinking, I think I figured out how to do something simaler to triplanar textureing. First off, I create a 3 UVs for all 3 sides by doing this:
uvsx = new Vector2[mesh.vertices.length];
for (var i=0; i < uvsx.Length; i+=1) {
uvsx[i] = Vector2(mesh.vertices[i].y,mesh.vertices[i].z);
}
uvsy = new Vector2[mesh.vertices.length];
for (var i=0; i < uvsy.Length; i+=1) {
uvsy[i] = Vector2(mesh.vertices[i].z,mesh.vertices[i].x);
}
uvsz = new Vector2[mesh.vertices.length];
for (var i=0; i < uvsz.Length; i+=1) {
uvsz[i] = Vector2(mesh.vertices[i].x,mesh.vertices[i].y);
}
All I need to do now is blend them together as one. Unfortunately, I don't know how. So does anyone know how to blend these 3 UV together?
How about looking at some open source voxel engines and seeing how they did it?
O$$anonymous$$, I have looked all over the internet, and I have found a few shaders. But I have no idea what I would use a shader for. Also, when I transfer them into my game, they always give parsing error's that I don't know how to fix because I usually work with java script. Does anyone now how to finish up the code I posted above, or at least show me a website with a working triplanar texturing code?
Answer by DavidWilliams · Jul 01, 2013 at 08:23 AM
You do indeed want to use triplanar texturing. Have a look at these resources:
Generating Complex Procedural Terrains Using the GPU (Section 1.5)
Volumetric Representation of Virtual Environments (Section 3.5.1)
Note that you are on the right track, but you don't want to 'combine the UVs'. Instead you sample the texture three times (once with each set of UVs) and then combine the results. You will need some good shader programming skills to do this - I don't believe you can just use C#/JavaScript.
Your answer
Follow this Question
Related Questions
UV Mapping for 2D Meshes with 3 Vertices Only 1 Answer
Split a mesh while using same UVs and texture position. 0 Answers
Assigning UV Map to model at runtime 0 Answers
Same material renders differently on a mesh generated from script and normal cubes. 1 Answer
Ways to texture very large mesh 0 Answers