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 Muzz5 · Aug 16, 2011 at 10:45 AM · mesh

Procedural cube generation

I'm trying to make a cube appear by prcedurally creating the mesh. I have got all the triangles in the right place, except for the last one! I'm trying to create a simple 1x1x1 cube - I've attempted all possible combinations for the last triangle - and none of them work. All produce a triangle the wrong way round. Anyway, here is the code:

 var newVertices : Vector3[];
 var newUV : Vector2[];
 var newTriangles : int[];
 var material : Texture2D;
 
 function Start () {
 var mesh : Mesh = new Mesh ();
 GetComponent(MeshFilter).mesh = mesh;
 mesh.Clear();
 //Actual creation.
 var p1 = Vector3(0,0,0);
 var p2 = Vector3(1,0,0);
 var p3 = Vector3(0,1,0);
 var p4 = Vector3(0,0,1);
 var p5 = Vector3(1,0,1);
 var p6 = Vector3(1,1,0);
 var p7 = Vector3(0,1,1);
 var p8 = Vector3(1,1,1);
 
 newVertices = new Array (p1,p2,p3,p4,p5,p6,p7,p8);
 
 newTriangles = [
 0,1,2,
 1,2,5,
 
 3,0,1,
 1,4,3,
 
 0,3,2,
 2,3,6,
 
 1,4,5,
 5,7,4,
 
 6,3,4,
 6,4,7,
 
 6,5,2,
 //problem line below
 7,6,5
 ];
 
 mesh.vertices = newVertices;
 mesh.triangles = newTriangles;
 mesh.uv = newUV;
 //I could UV map it, but I can't be bothered right now. Maybe later?
 mesh.RecalculateNormals();  
 mesh.RecalculateBounds();  
 mesh.Optimize(); 
 renderer.material.color = Color.gray;
 }
Comment
Add comment · Show 1
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
avatar image Eric5h5 · Aug 16, 2011 at 05:53 PM 0
Share

You should be using a Vector3 array, not Array. It will work with Array, but relatively slowly.

1 Reply

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

Answer by Mortennobel · Aug 16, 2011 at 11:57 AM

Take a look at my blog about procedural generated mesh in Unity.

http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/

You are doing a few things wrong:

  • Most likely you need more vertices (since each normal are shared between all vertices in each triangle). If you don't do this, your cube will be shaded very odd.

  • Be careful about the winding order. The correct order of your vertices are:

    newTriangles = [ 0,2,1, 1,2,5,

    3,0,1, 1,4,3,

    0,3,2, 2,3,6,

    1,5,4, 5,7,4,

    6,3,4, 6,4,7,

    6,5,2, 7,5,6 ];

Comment
Add comment · Show 2 · 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
avatar image Muzz5 · Aug 16, 2011 at 03:57 PM 0
Share

7,5,6 isn't working for me. Your blog is interesting, but do you really need more verts? I thought in 3D modelling they are always shared?

avatar image Mortennobel · Aug 16, 2011 at 06:21 PM 0
Share

I have updated the answer - there were quite a few winding order issues. You are right that in 3D modelling vertices are usually shared, but in 3D rendering they are not (since that is usually the most efficient). There is one normal for each vertex (this is computed in recalculate normals - or can be set manually. But your cube actually needs three normals for each corner. The solution is to have three vertices in each corner.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Autoscale Shader with multiple random texture selection. 0 Answers

Triangulate a polygon 1 Answer

Frame Rate drops when creating mesh in mobile Voxel World? 0 Answers

Dynamic Vertex Welding 1 Answer

Closest collision point 1 Answer


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