- Home /
Create Mesh that encapsulates two different sized spheres (perhaps a distorted cylinder?)
Hello, I'm relatively new to unity. I'm trying to create a new game object procedurally from a script . I'm building objects from two separate spheres of differing size. I can Instantiate the spheres with no issues, but I don't know how i can create a mesh that surrounds both. And I also don't know how to combine the meshes into one.
Perhaps a diagram of what i'm trying to accomplish is warranted:
The above is the two spheres, and the below is the combined mesh I am looking for.
I've thought about using a cylinder that draws between each sphere's centre, but then one end of the cylinder needs to be somehow made larger than the other to make the smooth connection. I've also thought about using a capsule and making one end larger than the other, but I don't know how to do that either.
EDIT: I'm also eventually going to need to put a (capsule??) collider around the new mesh, so ideas on that would be great as well
Any ideas would be appreciated! Adam
Answer by AdamBebko · Apr 08, 2016 at 10:27 PM
Well since no one answered I spent hours trying to come up with a solution myself and succeeded ! I still need to figure out how to get the meshes joined together and fix the collider, but the basic functionally is working now. Will update when I solve collider problem. I based my solution on the cone from http://wiki.unity3d.com/index.php/ProceduralPrimitives.
Hopefully this will help out someone else!
using UnityEngine;
using System.Collections;
public class DistrotedCylinder : MonoBehaviour {
float height;
float bottomRadius = 1;
float topRadius = 1;
public int nbSides = 18;
public GameObject sphere1;
public GameObject sphere2;
void Awake () {
CreateMesh ();
}
public void CreateMesh () {
MeshFilter filter = GetComponent<MeshFilter>();
Mesh mesh = filter.mesh;
mesh.Clear();
float scaleBottom = sphere1.transform.localScale.x;
bottomRadius *= scaleBottom/2f;
Debug.Log (bottomRadius.ToString ());
float scaleTop = sphere2.transform.localScale.x;
topRadius *= scaleTop/2f;
Debug.Log (topRadius.ToString ());
height = Vector3.Distance(sphere1.transform.localPosition, sphere2.transform.localPosition);
Debug.Log (height.ToString ());
float bottomPos = -height / 2;
float topPos = height/2;
Vector3 vectorBetweenSpheres = sphere2.transform.localPosition - sphere1.transform.localPosition;
transform.localPosition = sphere1.transform.localPosition + (vectorBetweenSpheres) / 2;
transform.rotation = Quaternion.FromToRotation(transform.up, vectorBetweenSpheres);
Debug.Log(transform.rotation.eulerAngles.ToString());
int nbVerticesCap = nbSides + 1;
#region Vertices
// bottom + top + sides
Vector3[] vertices = new Vector3[nbVerticesCap + nbVerticesCap + nbSides * 2 + 2];
int vert = 0;
float _2pi = Mathf.PI * 2f;
// Bottom cap
vertices[vert++] = new Vector3(0f, bottomPos, 0f);
while( vert <= nbSides )
{
float rad = (float)vert / nbSides * _2pi;
vertices[vert] = new Vector3(Mathf.Cos(rad) * bottomRadius, bottomPos, Mathf.Sin(rad) * bottomRadius);
vert++;
}
// Top cap
vertices[vert++] = new Vector3(0f, topPos, 0f);
while (vert <= nbSides * 2 + 1)
{
float rad = (float)(vert - nbSides - 1) / nbSides * _2pi;
vertices[vert] = new Vector3(Mathf.Cos(rad) * topRadius, topPos, Mathf.Sin(rad) * topRadius);
vert++;
}
// Sides
int v = 0;
while (vert <= vertices.Length - 4 )
{
float rad = (float)v / nbSides * _2pi;
vertices[vert] = new Vector3(Mathf.Cos(rad) * topRadius, topPos, Mathf.Sin(rad) * topRadius);
vertices[vert + 1] = new Vector3(Mathf.Cos(rad) * bottomRadius, bottomPos, Mathf.Sin(rad) * bottomRadius);
vert+=2;
v++;
}
vertices[vert] = vertices[ nbSides * 2 + 2 ];
vertices[vert + 1] = vertices[nbSides * 2 + 3 ];
#endregion
#region Normales
// bottom + top + sides
Vector3[] normales = new Vector3[vertices.Length];
vert = 0;
// Bottom cap
while( vert <= nbSides )
{
normales[vert++] = Vector3.down;
}
// Top cap
while( vert <= nbSides * 2 + 1 )
{
normales[vert++] = Vector3.up;
}
// Sides
v = 0;
while (vert <= vertices.Length - 4 )
{
float rad = (float)v / nbSides * _2pi;
float cos = Mathf.Cos(rad);
float sin = Mathf.Sin(rad);
normales[vert] = new Vector3(cos, 0f, sin);
normales[vert+1] = normales[vert];
vert+=2;
v++;
}
normales[vert] = normales[ nbSides * 2 + 2 ];
normales[vert + 1] = normales[nbSides * 2 + 3 ];
#endregion
#region UVs
Vector2[] uvs = new Vector2[vertices.Length];
// Bottom cap
int u = 0;
uvs[u++] = new Vector2(0.5f, 0.5f);
while (u <= nbSides)
{
float rad = (float)u / nbSides * _2pi;
uvs[u] = new Vector2(Mathf.Cos(rad) * .5f + .5f, Mathf.Sin(rad) * .5f + .5f);
u++;
}
// Top cap
uvs[u++] = new Vector2(0.5f, 0.5f);
while (u <= nbSides * 2 + 1)
{
float rad = (float)u / nbSides * _2pi;
uvs[u] = new Vector2(Mathf.Cos(rad) * .5f + .5f, Mathf.Sin(rad) * .5f + .5f);
u++;
}
// Sides
int u_sides = 0;
while (u <= uvs.Length - 4 )
{
float t = (float)u_sides / nbSides;
uvs[u] = new Vector3(t, 1f);
uvs[u + 1] = new Vector3(t, 0f);
u += 2;
u_sides++;
}
uvs[u] = new Vector2(1f, 1f);
uvs[u + 1] = new Vector2(1f, 0f);
#endregion
#region Triangles
int nbTriangles = nbSides + nbSides + nbSides*2;
int[] triangles = new int[nbTriangles * 3 + 3];
// Bottom cap
int tri = 0;
int i = 0;
while (tri < nbSides - 1)
{
triangles[ i ] = 0;
triangles[ i+1 ] = tri + 1;
triangles[ i+2 ] = tri + 2;
tri++;
i += 3;
}
triangles[i] = 0;
triangles[i + 1] = tri + 1;
triangles[i + 2] = 1;
tri++;
i += 3;
// Top cap
//tri++;
while (tri < nbSides*2)
{
triangles[ i ] = tri + 2;
triangles[i + 1] = tri + 1;
triangles[i + 2] = nbVerticesCap;
tri++;
i += 3;
}
triangles[i] = nbVerticesCap + 1;
triangles[i + 1] = tri + 1;
triangles[i + 2] = nbVerticesCap;
tri++;
i += 3;
tri++;
// Sides
while( tri <= nbTriangles )
{
triangles[ i ] = tri + 2;
triangles[ i+1 ] = tri + 1;
triangles[ i+2 ] = tri + 0;
tri++;
i += 3;
triangles[ i ] = tri + 1;
triangles[ i+1 ] = tri + 2;
triangles[ i+2 ] = tri + 0;
tri++;
i += 3;
}
#endregion
mesh.vertices = vertices;
mesh.normals = normales;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.Optimize();
}
}
Your answer
Follow this Question
Related Questions
Dynamically move vertices of a mesh 2 Answers
Does CombineMesh always remove unused vertecies or not? 0 Answers
How can I remove redundant vertices in custom mesh? 0 Answers
Mesh generation issue (C#) 1 Answer
Creating a convex MESH (not collider) 2 Answers