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
1
Question by peterhoglund · Feb 17, 2019 at 11:57 PM · meshgridverticesprocedural mesh

Set vertex positions aligned to a grid

I am creating a mesh and I need my vertices to line up exactly with the grid. How do I find the positions to place the vertices, including the "edge" vertices, so they intersect with any of the grid lines? I suppose there is a mathematical solution to this, but I don't even know where to start looking for an answer. Hope anyone can help.

Here is an explanation of what I have and what I am hoping for:

alt text Image 1 I have the grid. The grid is actually just all the whole number positions, for example Vector2(1,1). I also have the position of the mesh corners (black dots).

alt text Image 2 I want to build the mesh so that all the outer vertices is positioned where the green line intersects with the grid (pink dots). I also need to find all the inner vertices (black dots) and have them positioned according to the grid.

Thank you!

grid1.png (18.1 kB)
grid2.png (28.8 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Ymrasu · Feb 18, 2019 at 05:33 AM

I made your pictures out of gizmos that update in real-time.

Just attach this script to an empty gameobject and hit play! Have fun moving the child and parent gameobjects around. Maybe the maths I used will help you out. This only works for 2 Dimensions!

 using System.Collections.Generic;
 using System.Linq;
 using UnityEditor;
 using UnityEngine;
 
 public class MeshCreationHelper : MonoBehaviour
 {
     GameObject[] dots;
 
     private void Start()
     {
         dots = new GameObject[4];
 
         for (int i = 0; i < dots.Length; i++) {
             dots[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
             dots[i].transform.SetParent(transform);
         }
 
         dots[0].transform.position = new Vector3(2.5f, 0, 4.5f);
         dots[1].transform.position = new Vector3(5.5f, 0, 14.5f);
         dots[2].transform.position = new Vector3(12.5f, 0, 10.5f);
         dots[3].transform.position = new Vector3(6.5f, 0, 2.5f);
         
     }
 
     private void OnDrawGizmos()
     {
         if (EditorApplication.isPlaying) {
 
             List<Vector3> points = new List<Vector3>();
 
             // lines
             for (int i = 0; i < dots.Length; i++) {
                 Vector3 A = dots[i].transform.position;
                 Vector3 B = dots[(i + 1) % dots.Length].transform.position;
 
                 Gizmos.color = Color.cyan;
                 Gizmos.DrawLine(A, B);
 
                 var m = (B.z - A.z) / (B.x - A.x);
                 var b = A.z + (-m * A.x);
 
                 // X aligned points
                 int startX = (A.x < B.x) ? Mathf.CeilToInt(A.x) : Mathf.CeilToInt(B.x);
                 int finishX = (A.x < B.x) ? Mathf.FloorToInt(B.x) : Mathf.FloorToInt(A.x);
 
                 for (int x = startX; x <= finishX; x++) {
                     float z = (m * x) + b;
 
                     Gizmos.color = Color.red;
                     Gizmos.DrawSphere(new Vector3(x, 0, z), 0.1f);
 
                     points.Add(new Vector3(x, 0, z)); // add point for use with grid
                 }
 
                 // Z aligned points
                 int startZ = (A.z < B.z) ? Mathf.CeilToInt(A.z) : Mathf.CeilToInt(B.z);
                 int finishZ = (A.z < B.z) ? Mathf.FloorToInt(B.z) : Mathf.FloorToInt(A.z);
 
                 for (int z = startZ; z <= finishZ; z++) {
                     float x = (z - b) / m;
 
                     Gizmos.color = Color.red;
                     Gizmos.DrawSphere(new Vector3(x, 0, z), 0.1f);
                 }
             }
 
             // magic sort... sort vector3's by x pos then y pos
             List<Vector3> gridPoints = points.OrderBy(v => v.x).ThenBy(v => v.z).ToList();
 
             for (int i = 0; i < gridPoints.Count; i += 2) {
                 var x = gridPoints[i].x;
 
                 for (int z = Mathf.CeilToInt(gridPoints[i].z); z <= Mathf.FloorToInt(gridPoints[i + 1].z); z++) {
 
                     Gizmos.color = Color.black;
                     Gizmos.DrawSphere(new Vector3(x, 0, z), 0.1f);
                 }
             }
 
         }
     }
 }
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 peterhoglund · Feb 19, 2019 at 07:47 PM 0
Share

Thank you so much for this! It truly helped me out a lot!

avatar image
1

Answer by toddisarockstar · Feb 18, 2019 at 04:14 AM

here is the math you need. this gives the points along the x of your grid. just switch the x's and y's to get the points along the y.

obviosly you run this for every line in your shape

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class linedots : MonoBehaviour {
 
     void Start (){
         Vector2[] p = LinePoints (new Vector2 (-5.5f, 8f), new Vector2 (-11.5f, -15));
 
         foreach (Vector2 pp in p) {
 
             print("Point: "+pp);        
         }
         print (p.Length);
     }
     public Vector2[] LinePoints(Vector2 a,Vector2 b){
         float[] x = EvenNumbersBetween (a.x, b.x).ToArray();
         float distx = Mathf.Abs (a.x - b.x);
         float disty = Mathf.Abs (a.y - b.y);
         print ("str" + disty);
         Vector2[] r = new Vector2[x.Length];
         int i = x.Length;
         while(i>0){i--;
             r[i].x = x[i];
             r[i].y = x[i] - a.x;
             Mathf.Abs(r[i].y = r[i].y/distx);
             print ("per:"+r[i].y);
 
             Mathf.Abs(r[i].y = disty * r[i].y);
             print ("dis:"+r[i].y);
 
             r[i].y += a.y;
         }return r;}
 
 public List<float> EvenNumbersBetween (float a, float b) {
         float n=0;
     
         if(a>b){n = a; a=b; b=n;}
         List<float> r = new List<float> ();
         if(n<0){    n = Mathf.Ceil (a);}else{n = Mathf.Floor (a);}
         
     
         while (n<=b) {
         
             r.Add(n);n++;}
     
         return r;}
     
 
 
 }
 



so to explain the logic behind this... first of all we kinda allready know what all the x coordnates should be in our final vector list. and how many results we should have. the x coordanates are all going to whole numbers. to get the length of our return we just find the amount of whole numbers between the x of point a and the x of point b.

once you have all your x coordinates in your vecors, to get the corresponding y cordinates its a bit tricky. you figure the percentage each new x coordinate is between point a's x and point b's x. then you take that percentage and use it to multiply to the distance between point a's y and point b'y.

in other words if your x coordinate is a quarter of the way between the two black points x's. then the y should also be a quarter of the way between the the black point's y's.

after you have all your lines, and you are looking to output the middle you would use a flood algorithm. if you are doing shapes with only 4 sides there is a shortcuted method you could use. if you have more than 4 sides then you would need another step to for setting up a full flood algo. but this is really another question.

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 peterhoglund · Feb 19, 2019 at 07:51 PM 0
Share

Thank you for this explanation and example! I will use this and try to make my own solution! Great help!

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

113 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

Related Questions

Procedural Mesh Editing Performance 1 Answer

Getting mesh length (x,z) 1 Answer

How can I remove redundant vertices in custom mesh? 0 Answers

Holes in procedural mesh 0 Answers

What's wrong with this simple mesh manipulation? 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