The code that you found and translated to C# essentially converts a NavMesh into a regular [Mesh][1], and puts that Mesh in any MeshFilter. Whichever MeshFilter this object holds as a reference will then display the converted NavMesh (assuming it has a MeshFilterer).

Converting this to a Mesh could still be a useful first step, as you can use this to calculate a random point on the mesh. This would also be a random point on the NavMesh.

There is a thread on gamedev.net about [evenly distributing points on a triangle mesh][2]. This thread is fairly relevant for you, since you have a triangle mesh and want to generate a random point. It’s not perfectly relevant, since the Katachi’s question is actually about generating something close to a regular structure. For a random point I’d recommend following osmanb’s advice:

- Compute the area of each triangle in the mesh, sum them to get the complete surface area.
- Construct a list of normalized area weights for each triangle: (AreaOfTriangle / TotalSurfaceArea).
- For each point you generate
- Generate a random number ‘r’ (float) in [0,1]
- Walk through the list of weights, accumulating weight until the total is <= r
- Generate a random point on the surface of the selected triangle. (Via randomized barycentric coordinates or similar).

Here’s a quick code sample I threw together that does will generate a random point on a given Mesh that follows osmanb’s approach. I’ve written the code here focused especially on simplicity and understandability, rather than performance. If you’re going to be doing this a lot every frame, you’ll probably want to think a little bit about the performance (there are, for example, a lot of float array copies that aren’t needed).

```
private Vector3 GenerateRandomPoint(Mesh mesh)
{
// 1 - Calculate Surface Areas
float[] triangleSurfaceAreas = CalculateSurfaceAreas(mesh);
// 2 - Normalize area weights
float[] normalizedAreaWeights = NormalizeAreaWeights(triangleSurfaceAreas);
// 3 - Generate 'triangle selection' random #
float triangleSelectionValue = Random.value;
// 4 - Walk through the list of weights to select the proper triangle
int triangleIndex = SelectRandomTriangle(normalizedAreaWeights, triangleSelectionValue);
// 5 - Generate a random barycentric coordinate
Vector3 randomBarycentricCoordinates = GenerateRandomBarycentricCoordinates();
// 6 - Using the selected barycentric coordinate and the selected mesh triangle, convert
// this point to world space.
return ConvertToLocalSpace(randomBarycentricCoordinates, triangleIndex, mesh);
}
private float[] CalculateSurfaceAreas(Mesh mesh)
{
int triangleCount = mesh.triangles.Length / 3;
float[] surfaceAreas = new float[triangleCount];
for (int triangleIndex = 0; triangleIndex < triangleCount; triangleIndex++)
{
Vector3[] points = new Vector3[3];
points[0] = mesh.vertices[mesh.triangles[triangleIndex * 3 + 0]];
points[1] = mesh.vertices[mesh.triangles[triangleIndex * 3 + 1]];
points[2] = mesh.vertices[mesh.triangles[triangleIndex * 3 + 2]];
// calculate the three sidelengths and use those to determine the area of the triangle
// http://www.wikihow.com/Sample/Area-of-a-Triangle-Side-Length
float a = (points[0] - points[1]).magnitude;
float b = (points[0] - points[2]).magnitude;
float c = (points[1] - points[2]).magnitude;
float s = (a + b + c) / 2;
surfaceAreas[triangleIndex] = Mathf.Sqrt(s*(s - a)*(s - b)*(s - c));
}
return surfaceAreas;
}
private float[] NormalizeAreaWeights(float[] surfaceAreas)
{
float[] normalizedAreaWeights = new float[surfaceAreas.Length];
float totalSurfaceArea = 0;
foreach (float surfaceArea in surfaceAreas)
{
totalSurfaceArea += surfaceArea;
}
for (int i = 0; i < normalizedAreaWeights.Length; i++)
{
normalizedAreaWeights _= surfaceAreas */ totalSurfaceArea;*_
```

** }**

** return normalizedAreaWeights;**

** }**

** private int SelectRandomTriangle(float[] normalizedAreaWeights, float triangleSelectionValue)**

** {**

** float accumulated = 0;**

** for (int i = 0; i < normalizedAreaWeights.Length; i++)**

** {**

*_ accumulated += normalizedAreaWeights*;*_*

** if (accumulated >= triangleSelectionValue)**

** {**

** return i;**

** }**

** }**

** // unless we were handed malformed normalizedAreaWeights, we should have returned from this already.**

** throw new System.ArgumentException(“Normalized Area Weights were not normalized properly, or triangle selection value was not [0, 1]”);**

** }**

** private Vector3 GenerateRandomBarycentricCoordinates()**

** {**

** Vector3 barycentric = new Vector3(Random.value, Random.value, Random.value);**

** while (barycentric == Vector3.zero)**

** {**

** // seems unlikely, but just in case…**

** barycentric = new Vector3(Random.value, Random.value, Random.value);**

** }**

** // normalize the barycentric coordinates. These are normalized such that x + y + z = 1, as opposed to**

** // normal vectors which are normalized such that Sqrt(x^2 + y^2 + z^2) = 1. See:**

** // Barycentric coordinate system - Wikipedia**

** float sum = barycentric.x + barycentric.y + barycentric.z;**

** return barycentric / sum;**

** }**

** private Vector3 ConvertToLocalSpace(Vector3 barycentric, int triangleIndex, Mesh mesh)**

** {**

** Vector3[] points = new Vector3[3];**

*_ points[0] = mesh.vertices[mesh.triangles[triangleIndex * 3 + 0]];*

* points[1] = mesh.vertices[mesh.triangles[triangleIndex * 3 + 1]];*

* points[2] = mesh.vertices[mesh.triangles[triangleIndex * 3 + 2]];_*

*_ return (points[0] * barycentric.x + points[1] * barycentric.y + points[2] * barycentric.z);*

* }

[1]: http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html*_

*_*[2]: http://www.gamedev.net/topic/602405-evenly-distribute-points-on-a-triangle-mesh/*_*