Created
December 25, 2015 08:22
-
-
Save mattatz/ae5cf97d24198ac47658 to your computer and use it in GitHub Desktop.
Sample a point randomly on a given mesh script for Unity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello under what license is this snippet released under ? Thank-you.