- Home /
How do I texture this voxel cube?
I am still trying to understand voxel programming and just starting to understand it. Right now I have it so it generates a cube. I decided I would try to get it to generate a texture on the cube but looking at other peoples code I didn't understand it enough to put it in the script. So if you could put it in and explain a little I would like that:). This is the texture I am using for now
this is the script so far
var newVertices : Vector3[];
var newUV : Vector2[];
var newTriangles : int[];
var material : Texture2D;
function Start () {
var mesh : Mesh = new Mesh ();
GetComponent(MeshFilter).mesh = mesh;
mesh.Clear();
var p1 = Vector3(0,0,0);
var p2 = Vector3(1,0,0);
var p3 = Vector3(0,1,0);
var p4 = Vector3(0,0,1);
var p5 = Vector3(1,0,1);
var p6 = Vector3(1,1,0);
var p7 = Vector3(0,1,1);
var p8 = Vector3(1,1,1);
newVertices = new Array (p1,p2,p3,p4,p5,p6,p7,p8);
newTriangles = [0,2,1, 1,2,5, 3,0,1, 1,4,3, 0,3,2, 2,3,6, 1,5,4, 5,7,4, 6,3,4, 6,4,7, 6,5,2, 7,5,6];
mesh.vertices = newVertices;
mesh.triangles = newTriangles;
mesh.uv = newUV;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
}
what this dose
Answer by Cherno · Sep 28, 2014 at 06:54 PM
Take a look at this tutorial, it includes code for UVs:
http://unitycoder.com/blog/2012/02/15/dummy-craft-chunk-renderer-js/
You just imagine each face of the cube as lying on a 2-dimensional plane, and then you just give each vertice you defined a xy coordinate on this plane from 0 to 1. I would suggest using lists for vertices, triangles and uvs so you can just add new elements on the fly instead of having to deicde the array sizes beforehand.
If you want to learn more, take a look at this thread:
http://forum.unity3d.com/threads/after-playing-minecraft.63149/
Your answer
![](https://koobas.hobune.stream/wayback/20220613163953im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to assign texture to a specific mesh in a chunk for a minecraft game? 1 Answer
Dynamic Runtime Decals 0 Answers
Render voxels with RGB data passed to them 1 Answer
Save a procedurally generated mesh and load it after? 0 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers