• 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 luckruns0ut · Nov 23, 2013 at 05:11 PM · meshmapgenerationtile

Get quads from mesh? [SOLVED]

I'm trying to make a tile-based game which uses pre-made map meshes. I need to find all of the center point of each quad in the mesh so that I can set up a node graph from it. Unity treats this mesh as triangles, however, so it becomes harder for me to do this. I've attached an image of a basic map which I am trying to use.

How can I find each quad in this mesh? Speed isn't an issue as I'm doing this once in the editor.

EDIT: I've fixed my issue, though I didn't actually find the quads, I used the sides of the triangles to get the midpoint of the quads. I'll post the code below for anyone who has a similar issue to me. The code is probably very inefficient but that doesn't matter to me since it's just for building the nodegraphs.

 using UnityEngine;

using System.Collections; using System.Threading; using System.Collections.Generic;

 using UnityEngine;
 using System.Collections;
 using System.Threading;
 using System.Collections.Generic;
 
 public class MeshTest : MonoBehaviour {
     
     Mesh mesh;
     
     void Start() 
     {
         Vector3[] mapNodes = GenerateMesh();
     }
     
     private Vector3[] GenerateMesh()
     {
         mesh = gameObject.GetComponent<MeshFilter>().mesh;
         
         Vector3[] vertices = mesh.vertices;
         int[] triangles = mesh.triangles;
 
         List<Vector3> finalVerts = new List<Vector3>();
         
         for (int i = 0; i < triangles.Length; i+=3)
         {
             Vector3 p1 = mesh.vertices[triangles[i+0]];
             Vector3 p2 = mesh.vertices[triangles[i+1]];
             Vector3 p3 = mesh.vertices[triangles[i+2]];
             
             float d1 = Vector3.Distance(p1,p2);
             float d2 = Vector3.Distance(p2,p3);
             float d3 = Vector3.Distance(p3,p1);
             
             Vector3 point = Vector3.zero;
             
             if(d1 > d2 && d1 > d3)
                 point = (p1+p2)/2;
             
             if(d2 > d1 && d2 > d3)
                 point = (p2+p3)/2;
             
             if(d3 > d1 && d3 > d2)
                 point = (p3+p1)/2;
 
             if(point != Vector3.zero)
             {
                 if(!finalVerts.Contains(point))
                     finalVerts.Add(point);
             }
         }
 
         return finalVerts.ToArray();
     }
 }
 

 
map2.jpg (328.4 kB)
Comment
Add comment · Show 6
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 robertbu · Nov 23, 2013 at 05:46 PM 1
Share

Untested ideas:

See if the triangles are always defined in pairs. If so, you can get the four points by processing each triangle pair.

Another idea. The middle of each quad will be the midpoint of the longest side of each triangle. Find the midpoint of each triangle and then remove duplicates.

avatar image luckruns0ut · Nov 23, 2013 at 05:50 PM 0
Share

Good ideas, though I just found out that GetTriangles() doesn't actually return the vertices of the triangles so it's still an issue.

avatar image robertbu · Nov 23, 2013 at 05:55 PM 0
Share

It returns the index into the $$anonymous$$esh.vertices array, so it is just one indirection. So you will have something like:

 var vertices = mesh.vertices;
 var triangles = mesh.triangles;
 /...
 var vertex = vertices[triangles[someIndex]];

 
avatar image luckruns0ut · Nov 23, 2013 at 06:01 PM 0
Share

I see, I'll try using that then.

avatar image robertbu · Nov 23, 2013 at 06:02 PM 0
Share

It is more efficient to find the middle point and then convert it to world space rather than to convert all the vertices to world coordinates. Plus you will have to assume some sort of order of the vertices in order to use just the vertices to find the middle of each quad.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Connecting flatshaded vertices 0 Answers

Creating a mesh and assigning a material during edit time. 1 Answer

can 5 verts and 4 triangles be done on a square texture? 1 Answer

Triangulate a polygon 1 Answer

Problem with Winding Generated mesh, Material and edges appear wrong 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