I am applying a 3D Voronoi pattern on a mesh. Using those loops, I am able to compute the cell position, an id and the distance.

But I would like to compute a normal based on the generated pattern. How can I generate a normal or reorient the current normal based on this pattern and associated cells ?

The aim is to provide a faced look for the mesh. Each cell's normal should point in the same direction and adjacent cells point in different directions. Those directions should be based on the original mesh normals, I don't want to totally break mesh normals and have those points in random directions.

Here's how I generate the Voronoi pattern.

float3 p = floor(position);
float3 f = frac(position);
float id = 0.0;
float distance = 10.0;

for (int k = -1; k <= 1; k++)
    for (int j = -1; j <= 1; j++)
        for (int i = -1; i <= 1; i++)
            float3 cell = float3(float(i), float(j), float(k));
            float3 random = hash3(p + cell);
            float3 r = cell - f + random * angleOffset;
            float d = dot(r, r);

            if (d < distance)
                id = random;
                distance = d;
                cellPosition = cell + p;
                normal = ?

And here's the hash function :

float3 hash3(float3 x)
    x = float3(dot(x, float3(127.1, 311.7, 74.7)),
        dot(x, float3(269.5, 183.3, 246.1)),
        dot(x, float3(113.5, 271.9, 124.6)));

    return frac(sin(x)*43758.5453123);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.