Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AdamBebko · Apr 07, 2016 at 09:08 PM · meshesprocedural meshcombined mesh

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: alt text

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

screen-shot-2016-04-07-at-44922-pm.png (24.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

alt text

alt text

 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();
     }
 }
 


screen-shot-2016-04-08-at-60901-pm.png (189.1 kB)
screen-shot-2016-04-08-at-60911-pm.png (202.3 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges