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 wolga2 · May 07, 2014 at 11:22 PM · meshplaneverticesquadgenerate

Build plane on quad of mesh with same vertices

Hi! I want to build a plane by code on a quad of a existing mesh. So to simply generate a plane i use this code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlaneGenerator : MonoBehaviour
 {
     public List<Vector3> newVertices = new List<Vector3>();
     public List<int> newTriangles = new List<int>();
     public List<Vector2> newUV = new List<Vector2>();
     private Mesh mesh;
 
     void Start ()
     {
         mesh = GetComponent<MeshFilter> ().mesh;
         
         float x = transform.position.x;
         float y = transform.position.y;
         float z = transform.position.z;
         
         
         newVertices.Add( new Vector3 (x  , y  , z ));
         newVertices.Add( new Vector3 (x + 1 , y  , z ));
         newVertices.Add( new Vector3 (x + 1 , y-1  , z ));
         newVertices.Add( new Vector3 (x  , y-1  , z ));
 
         newTriangles.Add(0);
         newTriangles.Add(1);
         newTriangles.Add(3);
         newTriangles.Add(1);
         newTriangles.Add(2);
         newTriangles.Add(3);
 
         newUV.Add(new Vector2 (0, 1));
         newUV.Add(new Vector2 (1, 1));
         newUV.Add(new Vector2 (1, 0));
         newUV.Add(new Vector2 (0, 0));
     }
 
     void Update ()
     {
         mesh.Clear ();
         mesh.vertices = newVertices.ToArray();
         mesh.triangles = newTriangles.ToArray();
         mesh.uv = newUV.ToArray();
         mesh.Optimize ();
         mesh.RecalculateNormals ();
     }
 }

The code obviously puts the vertices in the positions (0, 0, 0), (1, 0, 0), (1, -1, 0), (0, -1, 0). What I want is to generate the plane on a quad on a existing mesh. So the planes vertices will be at the position of the vertices of a random quad in the mesh. Therefore I think I need to get the vertices positions of two triangles (that build a quad thogether) of the mesh and put it's positions for the positions of the vertices of the plane. How could I code this?

Thank you so much!

Comment
Add comment · Show 3
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 · May 08, 2014 at 01:22 AM 0
Share

So the planes vertices will be at the position of the vertices of a random quad

How are you finding this quad? Are all four vertices of this quad on the same plane?

avatar image wolga2 · May 08, 2014 at 02:34 AM 0
Share

No, they are not. I think finding the three vertices of a triangle is possible, but I don't know how to find the forth vertex for the quad. It certainly isn't the next one in the mesh.vertices-array. Would there be a way?

avatar image robertbu · May 08, 2014 at 03:39 AM 0
Share

A triangle has three sides. You indicate the triangle is arbitrary. Any other triangle that shares one of these sides (two vertices the same or at least to Vector3 positions the same) is a potential triangle that forms quad with the original. So in order to solve this problem, you need to take advantage of some unique property of your mesh. If you can provide a description of how you are identifying the original triangle and any properties of the mesh, I might be able to narrow your selection process beyond sharing a side.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wolga2 · May 08, 2014 at 01:15 PM

I thought I could find every triangle and then it's vertices. I don't know, if that's even possible. I tried something like that, it was meant to build a triangle for each triangle of the mesh. It actually builds a triangle on the mesh, but only one:

 using UnityEngine;
 using System.Collections;
 
 public class ExampleSpawn : MonoBehaviour
 {
     public GameObject spwanGO;
     private Mesh spawnMesh;
     public int[] spawnTris;
     public Vector3[] spawnVecs;
 
     void Start()
     {
         gameObject.AddComponent("MeshFilter");
         gameObject.AddComponent("MeshRenderer");
         Mesh mesh = GetComponent<MeshFilter>().mesh;
         mesh.Clear();
 
         spawnMesh = spwanGO.GetComponent<MeshFilter>().mesh;
         spawnTris = spawnMesh.triangles;
         spawnVecs = spawnMesh.vertices;
 
         for (int i = 0; i < spawnTris.Length; i += 3)
         {
             Vector3 p1 = spawnVecs[spawnTris[i + 0]];
             Vector3 p2 = spawnVecs[spawnTris[i + 1]];
             Vector3 p3 = spawnVecs[spawnTris[i + 2]];
 
             mesh.vertices = new Vector3[] {transform.TransformPoint(p1), transform.TransformPoint(p2), transform.TransformPoint(p3)};
             mesh.triangles = new int[] {i + 0, i + 1, i + 2};
         }
     }
 }
Comment
Add comment · 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

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

21 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

Related Questions

Unidirectional slicing algorithm of a mesh 0 Answers

Plane: vertices and triangles 2 Answers

Generate terrain tutorial by brackeys not working 0 Answers

Mesh's corners are not where it's supposed to be 0 Answers

Creating a mesh out of a linerenderer - how do you convert revolved vertices set to triangles and uv's 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