This site uses strictly necessary cookies. More Information

X- Home /

# Need help with procedurally generated tunnel mesh

I have been trying to make a procedurally generated tunnel with a mesh. The tunnel is based on a regular polygon with 12 sides. I have successfully created a path the tunnel to follow with center points for each face, and also a single part - two connected faces - separately, but I really can't figure out why this isn't working. I know it's probably something very obvious, but I just can't spot it.

Here's the code:

```
using UnityEngine;
using System.Collections;
public class LevelController : MonoBehaviour {
public float minimumLength;
public float maximumLength;
public float maximumAngle;
public Level currentLevel;
[HideInInspector] public GameObject gameObj;
[HideInInspector] new public Transform transform;
void Start () {
gameObj = gameObject;
transform = gameObj.GetComponent<Transform> ();
currentLevel = new Level (50, 100, maximumAngle, minimumLength, maximumLength);
transform.GetComponent<MeshFilter> ().mesh = currentLevel.mesh;
}
public struct Level {
public Mesh mesh;
public Face[] face;
public Vector3[] faceCenterPoint;
public Quaternion[] faceRotation;
public Vector3[] vertices;
public Vector2[] uv;
public int[] triangles;
public Level (int partCount, float firstPartLength, float maximumAngle, float minimumLength, float maximumLength) {
mesh = new Mesh ();
face = new Face[partCount + 1];
faceCenterPoint = new Vector3[partCount + 1];
faceRotation = new Quaternion[partCount + 1];
vertices = new Vector3[(partCount + 1) * 12];
uv = new Vector2[vertices.Length];
triangles = new int[24 * partCount];
faceCenterPoint[0] = new Vector3 (0, 0, 0);
faceRotation[0] = Quaternion.identity;
face[0] = new Face (faceRotation[0], faceCenterPoint[0]);
faceCenterPoint[1] = new Vector3 (0, 0, firstPartLength);
faceRotation[1] = Quaternion.Euler (faceRotation[0].eulerAngles.x + Random.Range (-maximumAngle, maximumAngle), faceRotation[0].eulerAngles.y + Random.Range (-maximumAngle, maximumAngle), 0);
face[1] = new Face (faceRotation[1], faceCenterPoint[1]);
Debug.DrawLine(faceCenterPoint[0], faceCenterPoint[1], Color.red, 600, true);
for (int n = 2; n < partCount; n++) {
float partLength = Random.Range (minimumLength, maximumLength);
Vector3 centerPoint = Vector3.zero;
Debug.Log (faceCenterPoint[n - 1]);
centerPoint.x = partLength * Mathf.Sin (faceRotation[n - 1].eulerAngles.y) * Mathf.Cos (faceRotation[n - 1].eulerAngles.x) + faceCenterPoint[n - 1].x;
centerPoint.y = partLength * Mathf.Cos (faceRotation[n - 1].eulerAngles.y) * Mathf.Sin (faceRotation[n - 1].eulerAngles.x) + faceCenterPoint[n - 1].y;
centerPoint.z = partLength * Mathf.Cos (faceRotation[n - 1].eulerAngles.x) * Mathf.Cos (faceRotation[n - 1].eulerAngles.y) + faceCenterPoint[n - 1].z;
faceCenterPoint[n] = centerPoint;
faceRotation[n] = Quaternion.Euler (faceRotation[n - 1].eulerAngles.x + Random.Range (-maximumAngle, maximumAngle), faceRotation[n - 1].eulerAngles.y + Random.Range (-maximumAngle, maximumAngle), 0);
Debug.DrawLine(faceCenterPoint[n - 1], faceCenterPoint[n], Color.red, 600, true);
face[n] = new Face (faceRotation[n], faceCenterPoint[n]);
}
for (int n = 0; n < face.Length - 1; n++) {
vertices[n * 12] = face[n].vertices[0];
vertices[n * 12 + 1] = face[n].vertices[1];
vertices[n * 12 + 2] = face[n].vertices[2];
vertices[n * 12 + 3] = face[n].vertices[3];
vertices[n * 12 + 4] = face[n].vertices[4];
vertices[n * 12 + 5] = face[n].vertices[5];
vertices[n * 12 + 6] = face[n].vertices[6];
vertices[n * 12 + 7] = face[n].vertices[7];
vertices[n * 12 + 8] = face[n].vertices[8];
vertices[n * 12 + 9] = face[n].vertices[9];
vertices[n * 12 + 10] = face[n].vertices[10];
vertices[n * 12 + 11] = face[n].vertices[11];
}
for (int n = 0; n < partCount - 1; n++) {
for (int j = 0; j < 12; j++) {
if (j == 11) {
triangles[j * n] = j + (n * 12);
triangles[j * n + 1] = (n + 1) * 12;
triangles[j * n + 2] = j + (n + 1) * 12;
triangles[j * n + 3] = j + (n * 12);
triangles[j * n + 4] = n * 12;
triangles[j * n + 5] = (n + 1) * 12;
} else {
triangles[j * n] = j + (n * 12);
triangles[j * n + 1] = j + 13 + (n * 12);
triangles[j * n + 2] = (n + 1) * 12;
triangles[j * n + 3] = j + (n * 12);
triangles[j * n + 4] = j + 1 + (n * 12);
triangles[j * n + 5] = j + 13 + (n * 12);
}
}
}
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
mesh.Optimize ();
mesh.RecalculateNormals ();
}
}
public struct Face {
public Vector3 centerPoint;
public Vector3[] vertices;
public Vector2[] uv;
public Face (Quaternion rotation, Vector3 centerP) {
centerPoint = centerP;
vertices = new Vector3[12];
uv = new Vector2[12];
vertices[0] = new Vector3 (-3, -11, centerP.z);
vertices[1] = new Vector3 (-8, 8, centerP.z);
vertices[2] = new Vector3 (-11, -3, centerP.z);
vertices[3] = new Vector3 (-11, 3, centerP.z);
vertices[4] = new Vector3 (-8, 8, centerP.z);
vertices[5] = new Vector3 (-3, 11, centerP.z);
vertices[6] = new Vector3 (3, 11, centerP.z);
vertices[7] = new Vector3 (8, 8, centerP.z);
vertices[8] = new Vector3 (11, 3, centerP.z);
vertices[9] = new Vector3 (11, -3, centerP.z);
vertices[10] = new Vector3 (8, -8, centerP.z);
vertices[11] = new Vector3 (3, -11, centerP.z);
for (int n = 0; n < vertices.Length; n++) {
vertices[n] = rotation * (vertices[n] - centerPoint) + centerPoint;
}
}
}
}
```

The weird part is that the triangles[] ends up being filled with seemingly random numbers in the range from 0 to ~700. If someone can explain to what I'm doing wrong or where the obvious mistake is, it'd be great.

**Answer** by The Pirate Duck
·
Sep 20, 2014 at 08:44 PM

I figured it out my self. If anyone is interested in almost working code, here's a link - https://gist.github.com/ThePirateDuck/7b95397733a09bf9e593

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Procedural generated mesh problem 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to snap a Prefab to the normal of an irregular mesh 1 Answer