Generate Mesh (464940)

Hi I am trying to make a cube in script but i can’t get all the sides to work

using UnityEngine;
using System.Collections;

public class Cube : MonoBehaviour {
	public Material mat;

	// Use this for initialization
	void Start () {
		MeshFilter meshFilter = GetComponent<MeshFilter>();
		if (meshFilter==null){
			Debug.LogError("MeshFilter not found!");
			return;
		}
		
		Vector3 p1 = new Vector3(0, 0, 0);
		Vector3 p2 = new Vector3(0, 1, 0);
		Vector3 p3 = new Vector3(1, 1, 0);
		Vector3 p4 = new Vector3(1, 0, 0);
		Vector3 p5 = new Vector3(1 , 0, 1);
		Vector3 p6 = new Vector3(1, 1, 1);
		Vector3 p7 = new Vector3(0, 0, 1);
		Vector3 p8 = new Vector3(0, 1, 1);
		
		Mesh mesh = meshFilter.sharedMesh;
		if (mesh == null) {
			meshFilter.mesh = new Mesh();
			mesh = meshFilter.sharedMesh;
		}
		
		mesh.Clear();

		mesh.vertices = new Vector3[]{
			p1, p2, p3, p3, p4, p1,
			p2, p8, p6, p6, p2, p2,
			p4, p3, p6, p6, p5, p4,
			p5, p6, p8, p8, p7, p5,
			p7, p8, p2, p2, p1, p7,
			p7, p1, p4, p4, p5, p7,
		};
		mesh.triangles = new int[]{
			0, 1, 2, 3,
			4, 5, 6, 7,
			8, 9, 10, 11,
			12, 13, 14, 15,
			16, 17, 18, 19,
			20, 21, 22, 23
		};
	
		mesh.RecalculateNormals();
		mesh.RecalculateBounds();
		mesh.Optimize();
		
		//Materials
		renderer.material = mat;
	
	}
}

The vertices of one of your triangles is in the wrong order I guess. Find out which one it is, and swap two of the vertices… (want them to be either all clockwise or all anti-clockwise, can’t remember which)

edit : wait, how many triangles do you have defined there? shouldn’t you need some more? why do you have them aligned in rows of 4 rather than 3?

You’re mixing up the vertices/triangles arrays…you only need 24 vertices in the vertices array, not 36, and you need 36 triangles in the triangle array, not 24.

–Eric

How to set the vertices up. I can’t get it to work

using UnityEngine;
using System.Collections;

public class Cube : MonoBehaviour {
	public Material mat;

	// Use this for initialization
	void Start () {
		MeshFilter meshFilter = GetComponent<MeshFilter>();
		if (meshFilter==null){
			Debug.LogError("MeshFilter not found!");
			return;
		}
		
		Vector3 p1 = new Vector3(0, 0, 0);
		Vector3 p2 = new Vector3(0, 1, 0);
		Vector3 p3 = new Vector3(1, 1, 0);
		Vector3 p4 = new Vector3(1, 0, 0);
		Vector3 p5 = new Vector3(1 , 0, 1);
		Vector3 p6 = new Vector3(1, 1, 1);
		Vector3 p7 = new Vector3(0, 0, 1);
		Vector3 p8 = new Vector3(0, 1, 1);
		
		Mesh mesh = meshFilter.sharedMesh;
		if (mesh == null) {
			meshFilter.mesh = new Mesh();
			mesh = meshFilter.sharedMesh;
		}
		
		mesh.Clear();

		mesh.vertices = new Vector3[]{
			p1, p2, p3, p4,
			p4, p3, p6, p5,
			p5, p6, p8, p7,
			p7, p8, p2, p1,
			p2, p8, p6, p3,
			p7, p1, p4, p5
		};
		
		mesh.triangles = new int[]{
			0, 1, 2, 3,
			4, 5, 6, 7,
			8, 9, 10, 11,
			12, 13, 14, 15,
			16, 17, 18, 19,
			29, 21, 22, 23,
			24, 25, 26, 27,
			28, 29, 30, 31,
			32, 33, 34, 35
		};
	
		mesh.RecalculateNormals();
		mesh.RecalculateBounds();
		mesh.Optimize();
		
		//Materials
		renderer.material = mat;
	
	}
}

Anyone?

Try this: You need to call out your eight verts (the corners of the cube), and you will have twelve triangles (two per side).

mesh.vertices = new Vector3[]{
p1, p2, p3, p4, p5, p6, p7, p8
};
mesh.triangles = new int[]{
0,1,2,
0,2,4,
3,2,5,
3,5,4,
4,5,7,
4,7,6,
6,7,2,
6,2,0,
0,6,4,
0,4,3,
1,7,5,
1,5,2
};

That didn’t work the cube were twisted

I did it in my head, but that’s the basic idea.

You need 8 verts and 12 tris.

Sorry I don’t have the time to spell it out, but you’re halfway there.

I have 24 vertices and 36 triangles but now i get an error

Error:

Failed setting triangles. Some indices are referencing out of bounds vertices.
UnityEngine.Mesh:set_triangles(Int32[])
Cube:Start() (at Assets/Scripts/Mesh Testing/Cube.cs:36)

Code:

using UnityEngine;
using System.Collections;

public class Cube : MonoBehaviour {
	public Material mat;

	// Use this for initialization
	void Start () {
		MeshFilter meshFilter = GetComponent<MeshFilter>();
		if (meshFilter==null){
			Debug.LogError("MeshFilter not found!");
			return;
		}
		
		Vector3 p1 = new Vector3(0, 0, 0);
		Vector3 p2 = new Vector3(0, 1, 0);
		Vector3 p3 = new Vector3(1, 1, 0);
		Vector3 p4 = new Vector3(1, 0, 0);
		Vector3 p5 = new Vector3(1 , 0, 1);
		Vector3 p6 = new Vector3(1, 1, 1);
		Vector3 p7 = new Vector3(0, 0, 1);
		Vector3 p8 = new Vector3(0, 1, 1);
		
		Mesh mesh = meshFilter.sharedMesh;
		if (mesh == null) {
			meshFilter.mesh = new Mesh();
			mesh = meshFilter.sharedMesh;
		}
		
		mesh.Clear();

		mesh.vertices = new Vector3[]{
			p1, p2, p3, p4, p5, p6, p7, p8
		};
		
		mesh.triangles = new int[]{
			0, 1, 2,
			3, 4, 5,
			6, 7, 8,
			9, 10, 11,
			12, 13, 14,
			15, 16, 17,
			18, 19, 29,
			21, 22, 23,
			24, 25, 26, 
			27, 28, 29,
			30, 31, 32,
			33, 34, 35
		};
	
		mesh.RecalculateNormals();
		mesh.RecalculateBounds();
		mesh.Optimize();
		
		//Materials
		renderer.material = mat;
	
	}
}

I have read this Tutorial: http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/

OK, the real problem you are having there is that the mesh.triangles array refers back to the mesh.vertices array.

In other words, if you put a 1 in Triangle[ ], you would be referring to (in your code) the vertex ‘p2’.

Your triangle array should have no numbers higher than 7.
The reason you are getting the out of bounds error, is because when you have
triangles[×] = 8; (and on up to 35)
you are referencing back to vertices[8-35], which don’t exist, because you only assigned values for vertices[0] to vertices[7]. (Which is correct)
When you make the triangles[ ] array, it is OK to re-use vertices, in fact, it is unavoidable.
Think of it as connect the dots. The vertices array are the numbers on the dots.
The triangles array shows how they are all connected.

This is explained in that tutorial you linked, which does explain the concept pretty well. The only confusing thing in that is that he used base 1 arrays, so his numbers were 1-4, where in Unity, they would really be numbered 0-3.

You should really follow that tutorial and build those tetrahedrons yourself, then move on to cubes.
There is a major issue that is the next layer to a cube construct… Shared versus non-shared verts.