//Return indexes of edges in the list which incident to the current vertex int[] GetEdgesForVertex(List <EdgeDataClass> edges, int vertex) { List <int> edgeIndexes = new List <int>(); for (int e = 0; e < edges.Count; e++) { EdgeDataClass edge = edges[e]; if (edge.IsContainsVertex(vertex)) { edgeIndexes.Add(e); } } return(edgeIndexes.ToArray()); }
//Return array of Vectir2-values for each vertex. This vector is zero for inner vertices and normal direction for boundary vertices public Vector2[] GetVertexNormals(Vector3[] _vertices, int[] _triangles) { //WG_Helper.PrintArray(_vertices); //WG_Helper.PrintArray(_triangles); //Collect edges List <EdgeDataClass> edges = new List <EdgeDataClass>(); // edge is a pair of vertices, int trianglesCount = _triangles.Length / 3; for (int i = 0; i < trianglesCount; i++) { int v1 = _triangles[3 * i]; int v2 = _triangles[3 * i + 1]; int v3 = _triangles[3 * i + 2]; CheckTheEdge(edges, v1, v2, i); CheckTheEdge(edges, v2, v3, i); CheckTheEdge(edges, v3, v1, i); } /*for (int i = 0; i < edges.Count; i++) * { * Debug.Log(i.ToString() + ": " + edges[i].ToString()); * }*/ //Iterate throw vertices and check is it boundary or not List <Vector2> vertexNormals = new List <Vector2>(); for (int i = 0; i < _vertices.Length; i++) { int[] edgeIndexes = GetEdgesForVertex(edges, i); Vector2 normal = new Vector2(0, 0); for (int j = 0; j < edgeIndexes.Length; j++) { EdgeDataClass edge = edges[edgeIndexes[j]]; if (edge.isBoundary) { Vector2 edgeNormal = GetEdgeNormal(edge, _vertices, _triangles); normal += edgeNormal; //Debug.Log("vertex " + i.ToString() + ", edge " + edgeIndexes[j].ToString() + ", normal: " + edgeNormal.ToString() + ", result: " + normal.ToString()); } } //Debug.Log(i.ToString() + ": " + WG_Helper.ArrayToString(edgeIndexes) + ", normal: " + normal.ToString()); vertexNormals.Add(normal.normalized); } return(vertexNormals.ToArray()); }
Vector2 GetEdgeNormal(EdgeDataClass edge, Vector3[] _vertices, int[] _triangles) { Vector3 pos1 = _vertices[edge.vertex01]; Vector3 pos2 = _vertices[edge.vertex02]; int v1 = _triangles[3 * edge.triangles[0]]; int v2 = _triangles[3 * edge.triangles[0] + 1]; int v3 = _triangles[3 * edge.triangles[0] + 2]; int v = edge.IsVertexPair(v1, v2) ? v3 : (edge.IsVertexPair(v1, v3) ? v2 : v1); Vector3 pos3 = _vertices[v]; Vector3 edgeDirection = pos2 - pos1; Vector3 innerDirection = pos3 - pos1; edgeDirection.Normalize(); innerDirection.Normalize(); Vector3 vec1 = Vector3.Cross(edgeDirection, innerDirection); Vector3 vec2 = Vector3.Cross(edgeDirection, vec1); Vector2 normal = new Vector2(vec2.x, vec2.z); return(normal.normalized); }