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 Jacky2611 · Jul 16, 2016 at 06:02 PM · c#android3dtriangulation

Need Help with 3D triangulation for Android

I just ran into the first big problem with my current Android project. I want to display a 3d Level that is generated from 2d data. (List of obstacles with defined corner points + a fix height stored in a json file.) I managed to draw them onto the floor using some code I found online and the triangulation code from the wiki. Now to make them 3d I would have to move the top part (which I already have) up (that is eays :P) and then draw a border/walls around it.

The Problem is that all the triangulation code I found online is 2d only. In order to draw my walls/ borders I need a 3rd axis(up). But whenever I tried to modify my code to add 3d support Unity stopped rendering any triangles at all :(

Would be great If anyone could help me with this.

This is what I currently use:

  private static Mesh CreateMesh(List<Vector3> verts)
     {
         var tris = new Triangulator(verts.Select(x => x.ToVector2xz()).ToArray());
         var mesh = new Mesh();
 
         var vertices = verts.Select(x => new Vector3(x.x, x.y, x.z)).ToList();
         var indices = tris.Triangulate().ToList();
         //var uv = new List<Vector2>();
 
         var n = vertices.Count;
         for (int index = 0; index < n; index++)
         {
             var v = vertices[index];
             vertices.Add(new Vector3(v.x, v.y, v.z));
         }
 
         for (int i = 0; i < n - 1; i++)
         {
             indices.Add(i);
             indices.Add(i + n);
             indices.Add(i + n + 1);
             indices.Add(i);
             indices.Add(i + n + 1);
             indices.Add(i + 1);
         }
 
         indices.Add(n - 1);
         indices.Add(n);
         indices.Add(0);
 
         indices.Add(n - 1);
         indices.Add(n + n - 1);
         indices.Add(n);
 
 
 
         mesh.vertices = vertices.ToArray();
         mesh.triangles = indices.ToArray();
 
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
 
         return 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
0
Best Answer

Answer by Jacky2611 · Jul 16, 2016 at 07:00 PM

Figured it out on my own. Sometimes a cup of hot choclat works wonders...

       private static Mesh CreateMesh(List<Vector3> verts, float height)
         {
             var tris = new Triangulator(verts.Select(x => x.ToVector2xz()).ToArray());
             var mesh = new Mesh();
 
             var vertices = verts.Select(x => new Vector3(x.x, height, x.z)).ToList();
             var indices = tris.Triangulate().ToList();
             //var uv = new List<Vector2>();
 
             var numberOfPointsPerRing = vertices.Count;
             for (int index = 0; index < numberOfPointsPerRing; index++)
             {
                 var v = vertices[index];
                 vertices.Add(new Vector3(v.x, 0, v.z));
             }
 
             for (int i = 0; i < numberOfPointsPerRing - 1; i++)
             {
                 indices.Add(i);
                 indices.Add(i + numberOfPointsPerRing);
                 indices.Add(i + numberOfPointsPerRing + 1);
                 indices.Add(i);
                 indices.Add(i + numberOfPointsPerRing + 1);
                 indices.Add(i + 1);
             }
 
             indices.Add(numberOfPointsPerRing - 1);
             indices.Add(numberOfPointsPerRing);
             indices.Add(0);
 
             indices.Add(numberOfPointsPerRing - 1);
             indices.Add(numberOfPointsPerRing + numberOfPointsPerRing - 1);
             indices.Add(numberOfPointsPerRing);
 
 
             //Lower pointposition hinzufügen
             foreach(Vector3 point in verts)
             {
                 vertices.Add(new Vector3(point.x, 0, point.z));
             }
 
 
             int pointAbove = numberOfPointsPerRing;
             for(int i=0; i<numberOfPointsPerRing; i++)
             {
                 //right upper triangle
                 indices.Add(i);             //right down
                 indices.Add(i+1+pointAbove);//left up
                 indices.Add(i+ pointAbove); //right up
 
                 //left lower triangle
                 indices.Add(i +1);             //right down
                 indices.Add(i + 1 + pointAbove);//left up
                 indices.Add(i); //right up
 
             }
 
 
 
             mesh.vertices = vertices.ToArray();
             mesh.triangles = indices.ToArray();
 
             mesh.RecalculateNormals();
             mesh.RecalculateBounds();
 
             return 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 pranavgadamsetty · Mar 17, 2017 at 10:51 AM 0
Share

Hi, Is this code specific to only your case of building walls or would it work in general with any gameobject? I'm new at this so I'm not able to figure this out.

Thanks, Pranav

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

195 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I rotate my player with a Joystick? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to make endless scene. (3D) 1 Answer

Count time between scene executions 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