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 zhihewan · Nov 04, 2019 at 03:30 AM · scripting problemmeshpolygon

Failed creating polygon mesh from script

I am trying to create a new gameobject with polygon mesh from a set of points.

pointList is a list of points in clockwise order.

My script doesn't work and I couldn't tell why. Could anyone tell me what's wrong?

Many thanks!!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class CreateSurface : MonoBehaviour
 {
     public GameObject surfaceVisualizer;
 
     private int[] meshIndices;
     private Vector3[] pointLocation;
     private GameObject surface;
 
     public void Start()
     {
         surface = new GameObject("New Surface");
         surface.AddComponent<MeshRenderer>();
         surface.AddComponent<MeshFilter>();
     }
 
     public GameObject creatSurface(List<GameObject> pointList, string meshName)
     {
         surface.name = meshName;
         surface.GetComponent<MeshRenderer>().material = surfaceVisualizer.GetComponent<MeshRenderer>().material;
 
         // Generate Mesh.
         Mesh mesh = surface.GetComponent<MeshFilter>().mesh;
         mesh.name = meshName;
         mesh.Clear();
 
         int pointCount = pointList.Count;
         for (int i = 0; i < pointCount; i++)
         {
             pointLocation[i] = pointList[i].transform.position;
         }
 
         for (int i = 0; i < pointCount - 1; i++)
         {
             meshIndices[3 * i] = i;
             meshIndices[3 * i + 1] = i + 1;
             meshIndices[3 * i + 2] = pointCount - i - 1;       
         }
 
         mesh.vertices = pointLocation;
         mesh.triangles = meshIndices;
         mesh.Optimize();
         mesh.RecalculateNormals();
 
         return Instantiate(surface, pointList[0].transform.position, pointList[0].transform.rotation); 
     }
 }
 
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
1

Answer by Bunny83 · Nov 04, 2019 at 11:51 AM

Well your triangle list doesn't really seem to make much sense. Also note that you can not simply create a polygon. Unity only supports the topologies: Triangles or Quads for "filled" geometry. So if you have a point loop you have to triangulate it yourself. Note that if your points form a convex polygon it is quite trivial since you can simply create a triangle fan around one point (usually the first one). If that is the case all you need to do is this:

      for (int i = 0; i < pointCount - 2; i++)
      {
          meshIndices[3 * i] = 0;
          meshIndices[3 * i + 1] = i + 1;
          meshIndices[3 * i + 2] = i + 2;
      }

However if your points do not resemble a convex polygon this will not work in many cases. Now you need to actually triangulate the polygon. Note that polygons are 2 dimensional shapes. If you have different heights (the points do not lie in the same plane) it's not really a polygon and there might be several ways how to create a surface between the points. There is the Triangulator script on the wiki which can triangulate a 2d polygon.

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

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

TextMesh/3DText modify shape 0 Answers

How to create a mesh with coordinates given from an Arduino? 1 Answer

Drawing new mesh not working 1 Answer

Runtime mesh decimation 1 Answer

Export game objects with mesh paths 0 Answers


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