public void Execute() { for (int index = 0; index < PointCount; ++index) { //so everything above this point wants to be factored out float randomSample = Rand.NextFloat(TotalAccum); int triIndex = -1; for (int i = 0; i < CumSizes.Length; i++) { if (randomSample <= CumSizes[i]) { triIndex = i; break; } } int vertIndex0 = Triangles[triIndex * 3 + 0]; int vertIndex1 = Triangles[triIndex * 3 + 1]; int vertIndex2 = Triangles[triIndex * 3 + 2]; float3 posA = Vertices[vertIndex0]; float3 posB = Vertices[vertIndex1]; float3 posC = Vertices[vertIndex2]; float3 normalA = Normals[vertIndex0]; float3 normalB = Normals[vertIndex1]; float3 normalC = Normals[vertIndex2]; float2 uvA = Uvs[vertIndex0]; float2 uvB = Uvs[vertIndex1]; float2 uvC = Uvs[vertIndex2]; //generate random barycentric coordinates float r = Rand.NextFloat(); float s = Rand.NextFloat(); if (r + s >= 1) { r = 1 - r; s = 1 - s; } //and then turn them back to a Vector3 float3 normalSample; float3 posSample = posA + r * (posB - posA) + s * (posC - posA); float2 uvSample = uvA + r * (uvB - uvA) + s * (uvC - uvA); // Use normal map instead hurray if (math.all(NormalTex.Size > 0)) { normalSample = NormalTex.Get(uvSample); } else { normalSample = normalA + r * (normalB - normalA) + s * (normalC - normalA); } posSample += Rand.NextGaussianSphere(NoiseLevel); MeshPoint point; point.Position = posSample; point.Normal = normalSample; point.Albedo = Albedo.Get(uvSample); MeshPoints[index] = point; } }