Skip to content

Instantly share code, notes, and snippets.

@mattatz
Created December 25, 2015 08:22
Show Gist options
  • Save mattatz/ae5cf97d24198ac47658 to your computer and use it in GitHub Desktop.
Save mattatz/ae5cf97d24198ac47658 to your computer and use it in GitHub Desktop.
Sample a point randomly on a given mesh script for Unity.
using UnityEngine;
using System.Collections;
namespace Utils {
public class RandomPointOnMesh {
public static Vector3 Sample (Mesh mesh) {
int[] triangles = mesh.triangles;
int index = Mathf.FloorToInt(Random.value * (triangles.Length / 3));
Vector3 v0 = mesh.vertices[triangles[index * 3 + 0]];
Vector3 v1 = mesh.vertices[triangles[index * 3 + 1]];
Vector3 v2 = mesh.vertices[triangles[index * 3 + 2]];
return Vector3.Lerp(v0, Vector3.Lerp(v1, v2, Random.value), Random.value);
}
}
}
@suprafun
Copy link

Hello under what license is this snippet released under ? Thank-you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment