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 Uriel_96 · Jan 29, 2011 at 03:14 AM · javascriptmeshverticestriangles

How to find triangles using variables

Ok, I have some information to get the vertices that is something like this:

var p0 = vertices[triangles[j * 3 + 0]];
var p1 = vertices[triangles[j * 3 + 1]];
var p2 = vertices[triangles[j * 3 + 2]];

and some vertices(that means that some vertices wasn't added to this variable)are added to a variable

newvertices[triangles[j * 3 + 0]] = p0;
newvertices[triangles[j*3+0]].z=newvertices[triangles[j*3+0]].z/transform.localScale.z; 
newvertices[triangles[j*3+0]].x=newvertices[triangles[j*3+0]].x/transform.localScale.x;
//and the other points(p1 & p2)

and I have a variable(j) that increments(j++;). And when it ends to check all the triangles I put something like this: mesh1.vertices = newvertices;


this is my code:

var layermask : LayerMask;
var p0c = 0;
var p1c = 0;
var p2c = 0;
var j = 0;
var listriangles : Vector3[];
var newtriangles : Vector3[];
var g = 0;
var thismesh : Mesh;
var triangleslist : int[];
var verts : Vector3[];
var triangles : int[];
var tri : int[];
function Update(){
var mesh1 : Mesh = GetComponent(MeshFilter).mesh;
var vertices = mesh1.vertices;
triangles = mesh1.triangles;
if(j < 121){
var p0 = vertices[triangles[j * 3 + 0]];
var p1 = vertices[triangles[j * 3 + 1]];
var p2 = vertices[triangles[j * 3 + 2]];
tri[j] = triangles[j*3];
tri[j+1] = triangles[j*3+1];
tri[j+2] = triangles[j*3+2];
var hitTransform : Transform = collider.transform;
p0 = hitTransform.TransformPoint(p0);
p1 = hitTransform.TransformPoint(p1);
p2 = hitTransform.TransformPoint(p2);
if(Physics.Linecast(p0,p1,layermask)){
Debug.DrawLine(p0, p1,Color.red);
if(p0c < 2 && p1c < 2){
p0c++;
p1c++;
}
}else{
Debug.DrawLine(p0, p1);
}
if(Physics.Linecast(p0,p2,layermask)){
Debug.DrawLine(p0, p2,Color.red);
if(p0c < 2 && p2c < 2){
p0c++;
p2c++;
}
}else{
Debug.DrawLine(p0, p2);
}
if(Physics.Linecast(p1,p2,layermask)){
Debug.DrawLine(p1, p2,Color.red);
if(p1c < 2 && p2c < 2){
p1c++;
p2c++;
}
}else{
Debug.DrawLine(p1, p2);
}
if(p0c == 2){
p0c = 0;
}
if(p1c == 2){
p1c = 0;
}
if(p2c == 2){
p2c = 0;
}
if(p0c <= 1 && p1c <= 1 && p2c <= 1){
triangleslist[j] = tri;
triangleslist[j+1] = tri +1;
triangleslist[j+2] = tri+2;
}
if(p0c == 1){
newtriangles[triangles[j * 3 + 0]] = p0;
newtriangles[triangles[j * 3 + 0]].z = newtriangles[triangles[j * 3 + 0]].z/transform.localScale.z; 
newtriangles[triangles[j * 3 + 0]].x = newtriangles[triangles[j * 3 + 0]].x/transform.localScale.x; 
p0c = 0;
}
if(p1c == 1){
newtriangles[triangles[j * 3 + 1]] = p1;
newtriangles[triangles[j * 3 + 1]].z = newtriangles[triangles[j * 3 + 1]].z/transform.localScale.z; 
newtriangles[triangles[j * 3 + 1]].x = newtriangles[triangles[j * 3 + 1]].x/transform.localScale.x; 
p1c = 0;
}
if(p2c == 1){
newtriangles[triangles[j * 3 + 2]] = p2;
newtriangles[triangles[j * 3 + 2]].z = newtriangles[triangles[j * 3 + 2]].z/transform.localScale.z; 
newtriangles[triangles[j * 3 + 2]].x = newtriangles[triangles[j * 3 + 2]].x/transform.localScale.x; 
p2c = 0;
}
j++;
g += 2;
}
if(j >= 121){
mesh1.Clear();
mesh1.vertices = newtriangles;
mesh1.triangles = triangleslist * 3;
}
}

what I am trying to do with this script is to find the vertices that have to be remove and like that have, first a list of vertices(already done and works perfectly), and second build triangles, and this is the part I need help. Help that works :P

Comment
Add comment · Show 2
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 Bunny83 · Jan 30, 2011 at 11:13 AM 0
Share

I'm really sorry, but i won't continue on this. To describe your script with two words: a total mess. I've stopped searching for syntax errors after the tenth i've found. The whole script also contains a lot of logical errors even without the knowledge what the script should do. To use the Update function as a loop for a single task is just crazy. All your own arrays aren't created or initialized and you mixed up int arrays with ints... I don't see any light at the end of this tunnel. I would recommend to take some program$$anonymous$$g lessons or simply try to learn the syntax on your own. good luck

avatar image Uriel_96 · Jan 30, 2011 at 11:07 PM 0
Share

oh sorry, yeah I was wrong I did this bad, but I finally find a way, but thanks for answer, your answer really works.

1 Reply

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

Answer by Bunny83 · Jan 29, 2011 at 05:00 AM

I guess you don't understand what a vertexbuffer and a indexbuffer is?
A Mesh in Unity consists of triangles. Every triangle is made up of 3 vertices. I guess you already know that part. Now the important part. Mesh.vertices is the vertexbuffer and Mesh.triangles is the indexbuffer. The indexbuffer is also an array but it contains indices related to the vertexbuffer. Each number (int) in the indexbuffer is a index into the vertexbuffer. Always 3 indices made up a triangle. The indexbuffer just tell you which vertices you need.

A simple example for a plane:

var vertices : Vector3[] = new Vector3[4]; vertices[0] = Vector3(-1,-1,0); vertices[1] = Vector3(-1, 1,0); vertices[2] = Vector3( 1, 1,0); vertices[3] = Vector3( 1,-1,0);

var indices : int[] = new int[6]; // triangle 1 indices[0] = 0; // first vertex indices[1] = 1; // second vertex indices[2] = 2; // third vertex // triangle 2 indices[3] = 2; // third vertex indices[4] = 3; // forth vertex indices[5] = 0; // first vertex

mesh.vertices = vertices; mesh.triangles = indices;

Look carefully at the example of the indices. The first and the third vertex is shared between both triangled.

If you want remove some triangles you have to remove the 3 corresponding indices that made up the triangle. The resulting indices array will be smaller in the end. That's tricky because you can't change the size of builtin arrays dynamically. If you want to remove vertices somewhere in the middle of the vertices array it gets really difficult because the indices would be pointing to the wrong vertices.

I still don't know exactly what you want to do. I'll hope you understand everything i explained here. Maybe you can specify you question a bit and tell us what you want to archive.

I guess you've tried something similar in this post, right?


Ok, i still don't understand what exactly you want to archive :D
Do you want to remove some triangles completely or just move them outwards. If you want to move them outward you just need to shift the vertices like you've done already but it depends on how the mesh is built up. If there are shared vertex points you have to duplicate the vertex points to make them unique. That would require to build up a complete new vertex and index array.

You said how to "find" a triangle. The loop where you increment your "j", it already goes through all triangles. "j" is the current triangle and (j*3), (j*3+1) and (j*3+2) are the 3 points that define this triangle.

If you want to remove a single triangle you have to copy all triangle indices into a new array except those you don't want. You can't change the size of a builtin array. The new arraysize have to be reduced by 3.

Comment
Add comment · Show 2 · 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 Uriel_96 · Jan 29, 2011 at 03:25 PM 0
Share

yeah, the real problem is at the final of the question, please read it

avatar image Uriel_96 · Jan 29, 2011 at 09:47 PM 0
Share

O$$anonymous$$, sorry for not explaining very well. I remove some of the vertices, and the vertices is all correct(the order and position), the bad thing comes in building the triangles, I don't know how to order my triangles, if it helps I will put all my code and a explication.

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

No one has followed this question yet.

Related Questions

Create edges for vertices(JS) 1 Answer

Starting with simple pathfinding with mesh 4 Answers

Help me use Triangulator.cs (from wiki) with javascript 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 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