Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Stardog · Jan 29, 2012 at 07:27 AM · meshproceduralverticestriangles

Procedural cube code

I've been following this tutorial - http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/

My question is about the mesh.vertices and mesh.triangles arrays. Do I have to type them out in the way I have? It seems like a lot of typing.

Procedural cube mesh generation Unity

Here's the code. Blame UnityAnswers + Monodevelop for the spacing.

 using UnityEngine;
 using System.Collections;
 

 [RequireComponent (typeof (MeshCollider))]
 [RequireComponent (typeof (MeshFilter))]
 [RequireComponent (typeof (MeshRenderer))]
 public class MyCube : MonoBehaviour {
 
     public float cubeX = 1f;
     public float cubeY = 1f;
     public float cubeZ = 1f;
     public float sections = 1f;
 
     public void Rebuild()
     {
         MeshFilter meshFilter = GetComponent<MeshFilter>();
 
         // FACE 1 VERTS
         Vector3 p0 = new Vector3(0, 0, 0);
         Vector3 p1 = new Vector3(cubeX, 0, 0);
         Vector3 p2 = new Vector3(cubeX, cubeY, 0);
         Vector3 p3 = new Vector3(0, cubeY, 0);
 
         // FACE 2 VERTS
         Vector3 p4 = new Vector3(cubeX, 0, 0);
         Vector3 p5 = new Vector3(cubeX, 0, cubeZ);
         Vector3 p6 = new Vector3(cubeX, cubeY, cubeZ);
         Vector3 p7 = new Vector3(cubeX, cubeY, 0);
 
         Mesh mesh = meshFilter.sharedMesh;
 
         if (mesh == null){
             meshFilter.mesh = new Mesh();
             mesh = meshFilter.sharedMesh;
         }
 
         mesh.Clear();
 
         // Returns a copy of the vertex positions or assigns a new vertex positions array.
         mesh.vertices = new Vector3[]{
             p0,p1,p2,
             p0,p2,p3,
             p2,p1,p3,
             p0,p3,p1,
 
             p4,p5,p6,
             p4,p6,p5,
             p4,p6,p7,
             p4,p7,p6
 
         };
 
         // Array containing all triangles in the mesh.
         mesh.triangles = new int[]{
             0,1,2,
             3,4,5,
             6,7,8,
             9,10,11,
 
             12,13,14,
             15,16,17,
             18,19,20,
             21,22,23
         };
 
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
         mesh.Optimize();
     }
 }



Comment
Add comment · Show 18
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 syclamoth · Jan 29, 2012 at 08:55 AM 0
Share

No, I'm pretty sure I can blame you for the spacing. It's not that hard to fix. In fact, I'm doing it for you now.

avatar image Eric5h5 · Jan 29, 2012 at 07:56 PM 2
Share

Just to be clear, you need 24 entries in mesh.vertices for a cube (4 per side, because of UVs and normals), though you only need 8 unique values for the vertices. You also should have 36 entries in mesh.triangles.

avatar image Bunny83 · Nov 15, 2014 at 03:21 PM 2
Share

@JaredThomson: Uhm, what are you talking about? Have you actually seen the original post? It was unreadable since you had to scroll down 4 or 5 pages. The "bla$$anonymous$$g" wasn't about the code itself. You can even write your code in a single line. It was about posting code here for others. In case you haven't realised: The question you see at the moment has been fixed by syclamoth.

We're doing a lot of moderating on this Q&A site and you just woke a dead question (which actually has been answered in the comments above) for no reason.

avatar image tanoshimi · Nov 15, 2014 at 03:41 PM 2
Share

@JaredThomson - Nobody $$anonymous$$ds beginners. What I (and many other users) $$anonymous$$d is people that ask for (free) help with their code without having a bit of decency to learn and follow the rules regarding formatting their question, rather than expecting "moderators" (i.e. other users) to fix it for them.

avatar image tanoshimi · Nov 15, 2014 at 09:19 PM 1
Share

@JaredThomson - I'm aware that this thread has already gone off-topic somewhat, but I wanted only to touch on one interesting point you raised...

You tutor people in making Unity games "who sometimes don't even know how to use a computer.". Unity has a reputation for being very accessible, and creating games is a great way to introduce people to program$$anonymous$$g (especially to engage young people in computer science). However, the fact that Unity is often people's first experience of program$$anonymous$$g, and also that there are a great many "casual" Unity developers is somewhat of both a blessing and a curse. $$anonymous$$any of the more experienced developers have left the community in frustration at the "dumbing-down" of the quality of questions and answers here recently. It's not the "newbies" fault - everybody was a beginner once - it's just that there is a very diverse range of backgrounds and abilities between Unity users, and those more experienced programmers who rely on Unity to make a living often find their requests for very specific technical help (that might be critical in the release of their game, and hence their income) lost in a mass of, what are essentially basic program$$anonymous$$g questions.

I'd argue that one of the most important things you can learn as a rookie programmer these days is: "When you get stuck, how and where should you ask for help?". $$anonymous$$ost internet fora have fairly common standards regarding search before you post, include a clear description reproducing the problem, don't use CAPS or abusive language etc. Then, each forum will have its own standards on top of that. On Unity Answers there is a clear FAQ and even tutorial video linked in the sidebar which explain the standards expected here, and clear code formatting is one of them.

That's all - please do encourage your students to post their Unity questions here next time they get stuck. But make sure they read the FAQ first :)

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

11 People are following this question.

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

Related Questions

Procedural Mesh Generation - Split Arrays into sections 1 Answer

Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer

Mesh extrusion overlapping triangles problem 0 Answers

Highmap on a cube. 0 Answers

Vertices and Triangle in Unity - problem with counting. 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