- Home /
Setting mesh.vertices to a 3-dimensional array
Ello everyone! this is my first time posting in this forum.
Anyways, I have a code that I wrote, of which I plan to create a voxel terrain from.
I have some 3d arrays set up, so that it can be simpler to generate a "sponge" of points. In other words, I will not only have points on the outside of the mesh, but filling the inside as well, so as to later be able to just switch them on and off. My plan is that this will be used to decide which vertices are triangulated with other adjacent vertices.
Alright, to my point.
I have a code here, and I have assigned the vertices, uvs and tris to the mesh like so -
using UnityEngine;
using System.Collections;
public class Voxel : MonoBehaviour {
//initializes the size changing values
public int WidthX;
public int WidthY;
public int WidthZ;
//initializes the arrays
public Vector3[,,] Verts;
public Vector2[,,] UVs;
public int[,,] Tris;
void Start () {
//sets the voxel size
WidthX = 5;
WidthY = 10;
WidthZ = 5;
//applies the voxelmesh to the game object
MeshFilter mf = GetComponent<MeshFilter>();
Mesh vox = new Mesh();
mf.mesh = vox;
vox = GetComponent<MeshFilter>().mesh;
vox.Clear();
//set vertices, uvs, and tris here
Verts = new Vector3[WidthX,WidthY,WidthZ];
UVs = new Vector2[WidthX,WidthY,WidthZ];
Tris = new int[WidthX*3,WidthY*3,WidthZ*3];
//apply values to mesh here
vox.vertices = Verts;
vox.uv = UVs;
vox.triangles = Tris;
vox.RecalculateNormals();
vox.RecalculateBounds();
}
}
So, when I hit F8 to build it, it says "Cannot convert Vector3[] to Vector3[,,]". No errors before I assign them, so I know that the 3d arrays exist and work. My problem is the compatibility.
Any suggestions?
Answer by robertbu · Nov 19, 2014 at 05:42 PM
Mesh.vertices, Mesh.uv, and Mesh.triangles are all one dimensional arrays, so you are going going to have to unwind your 3D arrays somehow. Iterating over them and building the 1D array is probably the most efficient, but you can have simpler code using LinQ:
In addition, if I understand what you are doing correctly, you've sized your triangles array incorrectly. You will have two triangles (three entries each) for every four vertices. So the number of entries will be:
no. vertices / 4 * 2 * 3
I see that I have sized the triangles wrong, but just to reiterate, this is a voxel terrain, so the triangles to vertices ratio may change.
And please note, there are Vector3's and Vector2's, with xyz values, so that unwinder may give an unwanted result.
The way you describe your problem you are using cube voxels. Due to uv mapping and/or vertex colors, you will not share vertices. The result is that for every quad you want to show, you will have two triangles.
I cannot see how unwinding will give unwanted results. If it were me,for performance reasons, I'd use a 1D array and map to it mathematically.
I simply have a 3d array for the purpose of readability, considering that the chunk will usually be massive. However, if I find the program too slow afterwards, I'll change it.
And I think I see your point with not getting unwanted results :P
Thanks for your time.
oh, and you messed up a bit on sizing the array xD
((no. vertices) / 4)*2*3;
Your answer
Follow this Question
Related Questions
how to make a planet 1 Answer
Get all vertices in gameObject to array. 1 Answer
Generating a 3D Mesh from Audio data 0 Answers
Procedurally generated mesh acting weird. 0 Answers
Multiple Cars not working 1 Answer