Skip to content

Instantly share code, notes, and snippets.

@cabbibo
Created December 24, 2017 20:05
Show Gist options
  • Save cabbibo/6d9a4c34a9e4bf6ce45e10d2c98178f7 to your computer and use it in GitHub Desktop.
Save cabbibo/6d9a4c34a9e4bf6ce45e10d2c98178f7 to your computer and use it in GitHub Desktop.
float totalArea = 0;
for( int i = 0; i < triBuffer.triangles.Length/3; i++ ){
int tri0 = i * 3;
int tri1 = tri0 + 1;
int tri2 = tri0 + 2;
tri0 = triBuffer.triangles[tri0];
tri1 = triBuffer.triangles[tri1];
tri2 = triBuffer.triangles[tri2];
float area = AreaOfTriangle( vertBuffer.vertices[tri0] , vertBuffer.vertices[tri1] , vertBuffer.vertices[tri2] );
triAreas[i] = area;
totalArea += area;
}
print( totalArea );
for( int i = 0; i < triAreas.Length; i++ ){
triAreas[i] /= totalArea;
}
// Then later
getTri( Random.value );
Where:
int getTri(float randomVal){
int triID = 0;
float totalTest = 0;
for( int i = 0; i < triAreas.Length; i++ ){
totalTest += triAreas[i];
if( randomVal <= totalTest){
triID = i;
break;
}
}
return triID;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment