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 alexander11 · Aug 25, 2016 at 12:37 AM · c#unity 5mesh3dtriangles

How does the Triangles work on Mesh?

Hello i having been searching about Triangles on mesh but i have found "0" things about them there is no tutorial for triangles for mesh in Unity, so i have some code below and if you can tell me how it works, that would be helpful.

So i have this Triangle code from here

     int ti = 0;
     for( int i = 0; i < segments; i++ ) {
         int offset = i * vertsInShape;
         for ( int l = 0; l < lines.Length; l += 2 ) {
             int a = offset + lines[l] + vertsInShape;
             int b = offset + lines[l];
             int c = offset + lines[l+1];
             int d = offset + lines[l+1] + vertsInShape;
             triangleIndices[ti] = a;     ti++;
             triangleIndices[ti] = b;     ti++;
             triangleIndices[ti] = c;     ti++;
             triangleIndices[ti] = c;     ti++;
             triangleIndices[ti] = d;     ti++;
             triangleIndices[ti] = a;     ti++;
         }
     }

And i am trying to change it to this, but obviously it does not work

       int ti = 0;
         for (int i = 0; i < segments; i++)
         {
             int offset = i * vertsInShape;
             for (int l = 0; l < origVerts.Length; l += 6)
             {
                 int a = offset + (stretch * l) + vertsInShape;
                 int b = offset + (stretch * l);
                 int c = offset + (stretch * l + 1);
                 int d = offset + (stretch * l + 1) + vertsInShape;
                 triangleIndices[ti] = c; ti++;
                 triangleIndices[ti] = b; ti++;
                 triangleIndices[ti] = a; ti++;
                 triangleIndices[ti] = a; ti++;
                 triangleIndices[ti] = d; ti++;
                 triangleIndices[ti] = c; ti++;
             }
         }

And this is the error i get with my code. "Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 48, VertexCount: 20 UnityEngine.Mesh:set_triangles(Int32[]) Extruder:Generate() (at Assets/Extruder.cs:73) Extruder:Start() (at Assets/Extruder.cs:23)"

But if you need the full code to know what i am doing with the code just comment below.

@Fattie

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 DiegoSLTS · Aug 25, 2016 at 01:08 AM

Well, the error is pretty clear, you've set a triangle vertex index greater than the actual vertex count for that mesh. In your case, you're setting one of the indices of some triangle to the vertex 48, but your mesh only has 20 vertices.

The problem is that you're incrementing "l" by 6, but the code inside the for only works with some index and the next. This means that you're ignoring a lot of vertices between each for iteration, and you're moving outside the valid vertices index at some point.

I'm not sure what you're trying to do, but as the error message says, you must use valid vertex indices for your triangles.

A triangle in the context of meshes is just a group of 3 vertices, but instead of having each vertex position for each triangle, a mesh has a list of vertices, and the triangles just store the index for the 3 vertices in that vertices array. This is to avoid vertex position to be replicated for each triangle that shares that vertex. Note that this is not some Unity thing, it's the most common method to represent meshes as data. I'd say it's the standard method, but I'm not familiar with all the 3D formats.

Comment
Add comment · Show 5 · 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 alexander11 · Aug 25, 2016 at 03:55 AM 0
Share

Ok... Anyway does not make much sense, but i am looking into it.

avatar image NoseKills alexander11 · Aug 25, 2016 at 06:18 AM 0
Share

What doesn't make much sense?

Btw. here's some info on triangles

Unity $$anonymous$$anual

API docs

avatar image alexander11 NoseKills · Aug 25, 2016 at 08:21 AM 0
Share

Thanks @Nose$$anonymous$$ills that did make an explanation for some things.

The first code(box) above where it says lines[](Which is an int[]) how would i be able to convert that to Vector3[] or something that can calculate all the triangles on every vertice?

Show more comments

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

236 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 do i create a Mesh in the middle of a Catmull spline? 0 Answers

How do i access the MeshExtrusion.cs from Unity's PE? 1 Answer

How do i have a Vector3[] position be in another Vector3[] position (That Updates)?? 1 Answer

How do i cast mesh like a linerenderer? 0 Answers

How do i modify a mesh Filter(C#)? 2 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