Procedural Mesh Building. [SOLVED]

My eyes is soaply now. I cannot find an error in any way. My errors or in checking vertices in a triangle or in vertices array sortng. There can be somebody will help me?
Sorry for not so clearly code. This is port from other programming language.

#pragma strict
enum selectMethod
{
	XY,
	XZ,
//	WALL	
}

var Name : String = "TriangleObject";
var material : Material = new Material (Shader.Find(" Diffuse"));

var tsv : Vector3[];
var tsvi : int[];
var UV : Vector2 = Vector2(1,1);

var selectUvMethod : selectMethod = selectMethod.XZ;
var Flat : boolean = true;
var scale : float = 1.0;
var magOLD : float;
private var step : Vector3 = Vector3(0,0,0);//position

private var tessNx : float;
private var tessNy : float;
private var tessNz : float;
private var mE0x : float;
private var mE0y : float;
private var mE0z : float;
private var mE1x : float;
private var mE1y : float;
private var mE1z : float;
private var mNx : float;
private var mNy : float;
private var mNz : float;
private var mA : float;

private var ent : GameObject;
private var myMesh : Mesh;
private var faceIndex : int = 0;
private var center : Vector3;

private var vertices : Vector3[];
private var triangles : int[];
private var uv : Vector2[]; 
private var myFilter : MeshFilter;// = plane.AddComponent(MeshFilter);
private var myRenderer : MeshRenderer;

function tess_IsPointInside(A : Vector3, B : Vector3, C : Vector3, P : Vector3) : boolean
{
	var v0 : Vector3 = C - A;
	var v1 : Vector3 = B - A;
	var v2 : Vector3 = P - A;

	var isin : boolean; 

// Compute dot products
	var dot00 : float = Vector3.Dot(v0, v0);
	var dot01 : float = Vector3.Dot(v0, v1);
	var dot02 : float = Vector3.Dot(v0, v2);
	var dot11 : float = Vector3.Dot(v1, v1);
	var dot12 : float = Vector3.Dot(v1, v2);

// Compute barycentric coordinates
	var invDenom : float = 1 / (dot00 * dot11 - dot01 * dot01);
	var u : float = (dot11 * dot02 - dot01 * dot12) * invDenom;
	var v : float = (dot00 * dot12 - dot01 * dot02) * invDenom;

// Check if point is in triangle
	isin = (u > 0)  (v > 0)  (u + v < 1);

	return isin;	
}

function calculateCenter() : Vector3
{
	var vec : Vector3;	
	var length : int = tsv.length;  
	for (var i : int = 0; i<length; i++)
	{
		vec = vec+tsv[i];
	}
	return vec/length;
}

function tess_ComputeNormal()
{
	tessNx = 0.0;
	tessNy = 0.0;
	tessNz = 0.0;
	var rowCount : int = tsv.length;
	var a : Vector3;
	var b : Vector3;
	for (var i : int = 0; i<rowCount; ++i)
	{
		a = tsv[i];
		if(i<rowCount+1)
		{
			b = tsv[i];
			tessNx = tessNx + ((a.y - b.y ) * ( a.z + b.z));
       			tessNy = tessNy + ((a.z - b.z ) * ( a.x + b.x));
        			tessNz = tessNz + ((a.x - b.x ) * ( a.y + b.y));
		}
	}
	//Normalize it, not really nescessary
	// just nicer to look at :)
	var d : float = Mathf.Sqrt(tessNx*tessNx + tessNy*tessNy + tessNz*tessNz);
	if( d>0.0 )
	{
		tessNx = tessNx/d;
		tessNy = tessNy/d;
		tessNz = tessNz/d;
	}
}

function tess_IsAnyPointInside(ii : int, jj : int, kk : int, n_verts : int) : boolean
{
	var v0 : Vector3;
	var v1 : Vector3;
	var v2 : Vector3;
	var point : Vector3;
	for (var ip : int = 0; ip<n_verts; ++ip)
	{
		if (ip<ii || ip>kk)
		{
			v0 = tsv[ii];
			v1 = tsv[jj];
			v2 = tsv[kk];
			point = tsv[ip];
			if(tess_IsPointInside(v0,v1,v2,point))return true;
		}
	}
	return false;
}

function Mod(a : int, b : int) : int
{
	var i : int = a;
	if (b == 0) return 0;
	while (i>=0)
	{
		i = i-b;
	}
	if (i<0) i=i+b;
	return i;
}

function tess_TriangleArea( i : int, j : int, k : int) : int
{
	var v0 : Vector3 = tsv[i];
	var v1 : Vector3 = tsv[j];
	var v2 : Vector3 = tsv[k];
	
	mE0x = v0.x-v2.x;
	mE0y = v0.y-v2.y;
	mE0z = v0.z-v2.z;
			
	mE1x = v1.x-v2.x;
	mE1y = v1.y-v2.y;
	mE1z = v1.z-v2.z;				
		
	mNx = mE0y * mE1z - mE0z * mE1y;
	mNy = mE0z * mE1x - mE0x * mE1z;
	mNz = mE0x * mE1y - mE0y * mE1x;
				
	var mA : float = (mNx*mNx + mNy*mNy + mNz*mNz);
			
	if (Mathf.Abs(mA) < 0.000001)
	{		
//		DEGENERATE = TRUE;
		return 2;
	}		
	if ((mNx*tessNx + mNy*tessNy + mNz*tessNz) < 0.0)
	{
//		CONCAVE = TRUE;
		return 1;
	}
//	CONVEX = TRUE;
	return 0; 
}

function tess_RemoveVertex( j : int, n_verts : int )
{
	var temparr = new Array (tsv);
	temparr.RemoveAt(j);
	tsv = temparr.ToBuiltin(Vector3);
	
	var temparri = new Array (tsvi);
	temparri.RemoveAt(j);
	tsvi = temparri.ToBuiltin(int);
	
	return n_verts - 1;
}

function tess_AddTriangle(i : int,j : int,k : int)
{
	var v0 : Vector3 = tsv[i];
	var v1 : Vector3 = tsv[j];
	var v2 : Vector3 = tsv[k];
	
	var vi : int = tsvi[i];
	var vj : int = tsvi[j];
	var vk : int = tsvi[k];

	vertices[vi] = v0-center;
	vertices[vj] = v1-center;
	vertices[vk] = v2-center;
	triangles[faceIndex++] = vi;
	triangles[faceIndex++] = vj;
	triangles[faceIndex++] = vk;
}

function Build() // Main function
{
	var n_verts : int = tsv.length;
	if(n_verts < 2)
	{
//		Error = TRUE;
		return;
	} 
	myMesh = new Mesh ();//bc.CreateMesh(nam.Str(),dynamic);
//	myMesh.name = Name + "_mesh";
	ent = new GameObject();//bc.CreateEntity3D(name.Str(),dynamic);
	var noErrors : boolean = true;
	
	myMesh.vertices = new Vector3[n_verts];//myMesh.SetVertexCount(n_verts);
	myMesh.uv = new Vector2[n_verts];
	myMesh.triangles = new int[(n_verts-2)*3];//myMesh.SetFaceCount(n_verts-2);
	vertices = new Vector3[n_verts];//myMesh.SetVertexCount(n_verts);
	uv = new Vector2[n_verts];
	triangles = new int[(n_verts-2)*3];
	var dir : Vector3 = Vector3 (0.0,0.0,1.0);// Vector dir(0.0,0.0,1.0),up(0.0,1.0,0.0),right(1.0,0.0,0.0);
	var up : Vector3 = Vector3 (0.0,1.0,0.0);
	var right : Vector3 = Vector3 (1.0,0.0,0.0);

	center = calculateCenter();
	ent.transform.position = center;

	
	tess_ComputeNormal() ;
	
	if (n_verts == 3)
	{
		tess_AddTriangle(0,1,2);	
		Finish(); 
		return;
	}

	while (n_verts>=3  noErrors)
	{
		noErrors = false;
		var i : int = 0;
		var j : int = 1;
		var k : int = 2;
		var ib : int;
		var jb : int;
		var kb : int;
		
		while (k<n_verts+3)
		{
			if(n_verts == 0)
			{
				Finish();
				return;
			}
			
			ib = Mod(i, n_verts);
			jb = Mod(j, n_verts);
			kb = Mod(k, n_verts);
			

			var test = tess_TriangleArea(ib,jb,kb);
			if (test == 0)
			{
				if(tess_IsAnyPointInside(ib,jb,kb,n_verts)){
						// Triangle is ok, but it cross another part of the polygon
							i = j;
							j = k;
							k = k + 1;
						}else{
						// Triangle is ok, so build it
							tess_AddTriangle(ib,jb,kb);
							n_verts  = tess_RemoveVertex(jb,n_verts);
							noErrors = true;	
						}
			}else if (test == 1){
				//Triangle faces the wrong way
						i = j;
						j = k;
						k = k + 1;	
			}else if (test == 2){
				// Bad triangle (zero area)
						n_verts  = tess_RemoveVertex(jb,n_verts);
						noErrors = true;

			}
		}
	}
	Finish();
	return;
}

function Finish()
{
//	Debug.Log("Finish()");
	
	myFilter = ent.AddComponent(MeshFilter);
	myRenderer = ent.AddComponent(MeshRenderer);
	myMesh.vertices = vertices;
	myMesh.uv = uv;
	myMesh.triangles = triangles;
	myMesh.RecalculateNormals();
	myFilter.mesh = myMesh;
	myRenderer.material = material;
	ent.transform.localScale = Vector3 (1,1,1); 
	myFilter.name = Name;
	myFilter.mesh.name = Name + "_mesh";
	DestroyImmediate(myMesh);
}

I think my errors is in “vertex in triangle” (function IsPointInside) checkng. Any idia?
Screens :



108129–4135–$procedural_861.zip (186 KB)

I just wondered if you managed to fix this… its just what I am looking for :wink:

Br,
Tore

I’ll share this code soon.

Hi Neodrop,

Hey man, great work so far.
Any news about sharing it?

Thanks,
R.

Not 100% sure this is actually about the same thing, but you might consider this if what you need is to triangulate a polygon:

Rune

This is not what I need.
My work is finished a long time ago. Sorry, what I do not share it yet.
Here we go :

187005–6615–$cstriangulator_509.cs (10.8 KB)