- Home /
How do I make a series of primitive planes and roll them into a tube?
So I want to create any number of primitive planes and roll them into a tube. Currently I get here from doing this:
for(int i = 0; i < 10; i++)
{
GameObject symbol = GameObject.CreatePrimitive(PrimitiveType.Plane);
symbol.name = "Symbol_" + i;
symbol.transform.parent = reel.transform;
Vector3 tran = new Vector3(PLANE_SIZE * i, 0.0f, 0.0f);
Quaternion rot = Quaternion.Euler(0.0f, 0.0f, 0.0f);
Vector3 scale = new Vector3(1.0f, 1.0f, 1.0f);
Matrix4x4 mat = new Matrix4x4();
mat.SetTRS(tran, rot, scale);
List<Vector3> updatedVerts = new List<Vector3>();
foreach(Vector3 vert in symbol.GetComponent<MeshFilter>().mesh.vertices)
{
updatedVerts.Add(mat.MultiplyPoint3x4(vert));
}
symbol.GetComponent<MeshFilter>().mesh.vertices = updatedVerts.ToArray();
symbol.GetComponent<MeshCollider>().sharedMesh = symbol.GetComponent<MeshFilter>().mesh;
symbol.GetComponent<MeshFilter>().mesh.RecalculateBounds();
symbol.GetComponent<MeshFilter>().mesh.RecalculateNormals();
}
Things start to go wrong when I try and roll it up into a tube.
int vertCount = 0;
foreach(MeshFilter meshFilter in reel.GetComponentsInChildren<MeshFilter>())
{
List<Vector3> updatedVerts = new List<Vector3>();
foreach(Vector3 vert in meshFilter.mesh.vertices)
{
int multiplier = vertCount / 11;
float angle = 3.0f * multiplier;
float xPos = Mathf.Cos(angle) * 5.0f;
float yPos = Mathf.Sin(angle) * 5.0f;
Vector3 tran = new Vector3(xPos, yPos, 0.0f);
Quaternion rot = Quaternion.Euler(0.0f, 0.0f, 0.0f);
Vector3 scale = new Vector3(1.0f, 1.0f, 1.0f);
Matrix4x4 mat = new Matrix4x4();
mat.SetTRS(tran, rot, scale);
updatedVerts.Add(mat.MultiplyPoint3x4(vert));
vertCount++;
}
meshFilter.mesh.vertices = updatedVerts.ToArray();
}
I was thinking I could just run through each row and apply the cos and sin to the x and y respectively. I pass 3.0f as an angle into both just as a place holder. Eventually I'll calculate the length of the original strip(circumference) to figure out my theta for cos and sin. Please and thank you for any help.
What is your goal here? Why are you modifying the vertices rather than simply rotating/positioning the Transform.
$$anonymous$$y goal is to have a smooth tube with x number of individual texture meshes. If I alter the transform and rotation of the the individual planes, I think the final product would look a little blocky. Hope that clears things up.
Answer by UnityUser666 · Oct 01, 2014 at 07:49 PM
Figured it out. I'm sure this isn't optimized, but it works. Hopefully this helps someone.
//This is where we make the cylinder
MeshFilter[] totalSymbolMesh = reel.GetComponentsInChildren<MeshFilter>();
List<Vector3> totalMeshVertList = new List<Vector3>();
foreach (MeshFilter symbolMeshFilter in totalSymbolMesh)
{
foreach (Vector3 symbolMeshVert in symbolMeshFilter.mesh.vertices)
{
totalMeshVertList.Add(symbolMeshVert);
}
}
float reelCircumference = getStripLength(totalMeshVertList);
float reelDiameter = reelCircumference / Mathf.PI;
float reelRadius = reelDiameter / 2;
int numOfStripHorizontalVerts = NumberOfSymbolsOnReel * NUM_VERTS_HEIGHT;
float incrimentalAngle = 360.0f / (NumberOfSymbolsOnReel * DEFAULT_PLANE_HEIGHT_SEGMENTS);
float angle = 0.0f;
float angleStep = 0.0f;
for (int meshCount = 0; meshCount < totalSymbolMesh.Length; meshCount++)
{
List<Vector3> updatedVerts = new List<Vector3>();
for (int vertIndex = 0; vertIndex < DEFAULT_NUMBER_PLANE_VERTS; vertIndex++)
{
angle = incrimentalAngle * (vertIndex / NUM_VERTS_HEIGHT);
angle = angle + angleStep;
float xPos = totalSymbolMesh[meshCount].mesh.vertices[vertIndex].x;
float yPos = reelRadius * Mathf.Sin(angle * Mathf.Deg2Rad);
float zPos = reelRadius * Mathf.Cos(angle * Mathf.Deg2Rad);
updatedVerts.Add(new Vector3(xPos, yPos, zPos));
}
angleStep = angle;
totalSymbolMesh[meshCount].mesh.vertices = updatedVerts.ToArray();
totalSymbolMesh[meshCount].mesh.RecalculateBounds();
totalSymbolMesh[meshCount].mesh.RecalculateNormals();
}