x


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

more ▼

asked Jan 29 '11 at 03:14 AM

Uriel_96 gravatar image

Uriel_96
945 61 70 81

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 programming lessons or simply try to learn the syntax on your own. good luck

Jan 30 '11 at 11:13 AM Bunny83

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

Jan 30 '11 at 11:07 PM Uriel_96
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jan 29 '11 at 05:00 AM

Bunny83 gravatar image

Bunny83
45.4k 11 49 207

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

Jan 29 '11 at 03:25 PM Uriel_96

OK, 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.

Jan 29 '11 at 09:47 PM Uriel_96
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5085
x3460
x1364
x139
x61

asked: Jan 29 '11 at 03:14 AM

Seen: 1519 times

Last Updated: Jan 29 '11 at 09:59 PM