Skip to content

Instantly share code, notes, and snippets.

@sugi-cho
Created December 17, 2013 01:38
Show Gist options
  • Save sugi-cho/7998451 to your computer and use it in GitHub Desktop.
Save sugi-cho/7998451 to your computer and use it in GitHub Desktop.
get random points on mesh
using UnityEngine;
using System.Collections;
public class RandomOnMesh : MonoBehaviour {
void OnDrawGizmos(){
Gizmos.color = Color.red;
for (int i = 0; i < 100; i++) {
Gizmos.DrawSphere(
GetRandomPointOnMesh(GetComponent<MeshFilter>().sharedMesh),
0.1f
);
}
}
Vector3 GetRandomPointInTriangle(Vector3 v0, Vector3 v1, Vector3 v2){
Vector3
p1 = Vector3.Lerp (v0, v1, Mathf.Sqrt(Random.value)),
p2 = Vector3.Lerp (v0, v2, Mathf.Sqrt(Random.value));
return Vector3.Lerp (p1, p2, Random.value);
}
Vector3 GetRandomPointOnMesh(Mesh mesh){
int rand = Random.Range(0, mesh.triangles.Length/3);
Vector3[] triangle = new Vector3[3]{
mesh.vertices[mesh.triangles[rand*3]],
mesh.vertices[mesh.triangles[rand*3+1]],
mesh.vertices[mesh.triangles[rand*3+2]]
};
return GetRandomPointInTriangle (triangle [0], triangle [1], triangle [2]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment