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
0
Question by Ebil · Feb 22, 2012 at 01:04 AM · meshproceduralcubeprocedural mesh

Simple plane generation

Hello everyone,

Im just trying to procedurally generate meshes and got some problems just at the beginning. I just want to create a cube, but to look if i understand everything correct I wanted to test a plane.

So I wrote that code:

 function Start () {
 var block : GameObject = new GameObject("Block");
 var mesh : Mesh = new Mesh ();
 block.AddComponent(MeshFilter); 
 block.AddComponent(MeshRenderer); 
 
 var vertices : Vector3[];
 var triangles : int[];
 vertices[0] = Vector3(0,0,0);
 vertices[1] = Vector3(1,0,0);
 vertices[2] = Vector3(1,0,1);
 vertices[3] = Vector3(0,0,1);
 
 
 
 var uvs = new Vector2[mesh.vertices.length];
 for (var i=0; i<uvs.Length; i++) {
 uvs[i] = Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
 }
 
 triangles = [0,1,2,3,2,1];
 mesh.vertices = vertices;
 mesh.uv = uvs;
 mesh.triangles = triangles;
 }

But I dont see anything , doesnt matter how and where i move the camera (so maybe if just one side of the plane is visible), but no, there is no mesh on the screen.

So any idea what I did wrong? Thanks in advance.

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
2
Best Answer

Answer by aldonaletto · Feb 22, 2012 at 02:12 AM

There are several errors:
1- the arrays vertices and triangles were just declared - you must allocate them with the new keyword specifying the desired size (they are built-in arrays, not Array class instances);
2- the triangles are wrong: the two triangles overlap, and have different winding orders, what makes only one triangle visible from each side;
3- you must assign the created mesh to meshFilter.mesh;
4- you're calculating uv coordinates based on mesh.vertices before assigning the vertices to mesh.vertices;
5- you must assign some material to the mesh renderer, or the triangles will be rendered in that lovely pink color;

function Start () { var block : GameObject = new GameObject("Block"); var mesh : Mesh = new Mesh (); // save a reference to the MeshFilter (you will need it): var meshFilter = block.AddComponent(MeshFilter); block.AddComponent(MeshRenderer);

var vertices = new Vector3[4]; // allocate the arrays with new! var triangles = new int[6]; vertices[0] = Vector3(0,0,0); // I changed the vertices order to clockwise just vertices[1] = Vector3(0,0,1); // to help understanding the winding order (the vertices[2] = Vector3(1,0,1); // triangles winding order is what really matters) vertices[3] = Vector3(1,0,0); triangles = [0,1,2,0,2,3]; // the triangles now are in clockwise order! mesh.vertices = vertices; // assign mesh.vertices and mesh.triangles... mesh.triangles = triangles; // prior to use them in the uv calculation! var uvs = new Vector2[mesh.vertices.length]; for (var i=0; i < uvs.Length; i++) { uvs[i] = Vector2(mesh.vertices[i].x, mesh.vertices[i].z); } mesh.uv = uvs; // assign the new uv coordinates... meshFilter.mesh = mesh; // and assign the mesh to MeshFilter.mesh }

Comment
Add comment · Show 1 · 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 Ebil · Feb 22, 2012 at 09:38 PM 0
Share

Thanks, helped me alot! Cube is done :)

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

Sphere made of cubes algorithm 4 Answers

C# Proceducal Mesh terrain 2 Answers

How to change type of shading? 1 Answer

Mesh Collider Edge Position. 0 Answers

Procedurally Generated Cube Mesh 3 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