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 Joshua · May 05, 2011 at 03:04 AM · mesharrayproceduralgenerationbce0044

Procedurally generated mesh JS - explain the error please

Hey guys, I decided to try my luck at procedurally generating meshes. I've tried following this guide, but since it's in C# I wanted to translate it into JS. Since the general idea translates I thought I was managing quite well, but there are some compiler errors I keep getting that I cannot explain.

It's probably something extremely simple and stupid I'm doing wrong/overlooking and might very well have to do with me not understanding built in arrays very well (finding unity related documentation on them is proving difficult)

TBH I'm just a noob when it comes to scripting who helps out the even lesser noobs around here ^.^ Also, it's late, I'm tired and I need heeeelp :p I've added text explaining the code and pointing out the errors.

@MenuItem ("GameObject/Create Other/Tetrahedron") //add it to the menu

static function CreateTetrahedron () {

 var Tetrahedron : GameObject = new GameObject ("Tetrahedron");  //create an empty gameobject with that name

 Tetrahedron.AddComponent(MeshFilter);                           //add a meshfilter

 var p0 : Vector3 = Vector3(0,0,0);                              //the four points that make a tetrahedron
 var p1 : Vector3 = Vector3(1,0,0);
 var p2 : Vector3 = Vector3(0.5f,0,Mathf.Sqrt(0.75f));
 var p3 : Vector3 = Vector3(0.5f,Mathf.Sqrt(0.75f),Mathf.Sqrt(0.75f)/3);

 var newVertices : Vector3[] = new Vector3[] {                   //the non-shared vertices (line 14)
     p0,p1,p2,p0,                                                //Assets/Editor/Tetrahedron.js(14,50): BCE0044: expecting (, found '['.
     p2,p3,p2,p1,                                                //Assets/Editor/Tetrahedron.js(15,19): BCE0044: expecting :, found ','.
     p3,p0,p3,p1
 };

 var newTriangles : int[] = new int[]{                           //the triangle faces the points make up
     0,1,2,
     0,2,3,
     2,1,3,
     0,3,1
 };

 var newMesh : Mesh = new Mesh ();                               //create a new mesh, assign the vertices and triangles
     newMesh.vertices = newVertices;
     newMesh.triangles = newTriangles;
     newMesh.RecalculateNormals();                               //recalculate normals, bounds and optimize
     newMesh.RecalculateBounds();
     newMesh.Optimize();                             

 Tetrahedron.GetComponent(MeshFilter).mesh = newMesh;            //assign the created mesh as the used mesh

}

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 loramaru · May 05, 2011 at 03:42 AM

The problem is that you're using the C# array initialization. Here's what you want for JavaScript.

var newVertices : Vector3[] = [ p0,p1,p2,p0, p2,p3,p2,p1, p3,p0,p3,p1 ];

var newTriangles : int[] = [
0,1,2, 0,2,3, 2,1,3, 0,3,1 ];

Also you had one more error that crops up once those are fixed; so here's a bonus fix. :)

(Tetrahedron.GetComponent(MeshFilter) as MeshFilter).mesh = newMesh;
Comment
Add comment · Show 5 · 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 Joshua · May 05, 2011 at 03:44 AM 0
Share

Haha , proves how blind you become when you stare too long at something. Thanks a lot :-)

avatar image loramaru · May 05, 2011 at 03:50 AM 0
Share

Also you mixed up the vertices from the second example with the triangles from the first. Your code will generate a flat triangle. You really want... var newTriangles : int[] = [0,1,2,3,4,5,6,7,8,9,10,11];

avatar image Joshua · May 05, 2011 at 03:56 AM 0
Share

Yes! Thank you! I got it to work :D thank you very very very very much!

avatar image keenanwoodall · Jul 15, 2014 at 04:37 PM 0
Share

How would I make the mesh visible, i couldn't figure it out

avatar image keenanwoodall · Jul 15, 2014 at 05:28 PM 0
Share

How would I make the pyramid visible? I've tried adding a renderer but I couldn't get it to work

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

1 Person is following this question.

avatar image

Related Questions

Flat shading procedural generated mesh? 1 Answer

Help in understanding Mesh Generation 1 Answer

generate procedural floating island 1 Answer

Generated mesh loses lighting at certain angles 2 Answers

Why Unity freezes while creating Meshes procedurally? 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