0
\$\begingroup\$

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);
}
\$\endgroup\$
  • 1
    \$\begingroup\$ There are lots of ways a normal could be "based on" a Voronoi pattern. To know which one is right for your application, it would help to know what your application is. What would you like the surface appearance to look like? Is there a real-world material or structure you're trying to imitate? \$\endgroup\$ – DMGregory Nov 15 at 16:19
  • \$\begingroup\$ Thanks for your answer @DMGregory. No real-world material or structure imitation. I am just using this procedural pattern as a mask for the color of a stylized shader and I also want to avoid have smooth mesh normal and have flat normals based on this pattern. \$\endgroup\$ – MaT Nov 15 at 16:23
  • 1
    \$\begingroup\$ Can you then at least describe which way you'd like the normal to point? Would you like the polygonal voronoi regions to look like facets, where each cell's normals all point in exactly the same direction, and adjacent cells point in different directions? Or should each polygon look domed, with normals pointing straight out near the middle and leaning into the crack between cells at the edges? Or maybe the opposite, concave like a slice through an open-cell foam? Maybe ridges along the cell borders? What surface bumps are right for your needs? \$\endgroup\$ – DMGregory Nov 15 at 16:40
  • \$\begingroup\$ Thanks again @DMGregory for you help. No smooth normal gradient from the center to the edges. I am looking for a facet look, like if the mesh normals were not smoothed. Each cell's normal 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 normal and have normals points in random directions. Hope it makes sense. \$\endgroup\$ – MaT Nov 15 at 16:48
  • 2
    \$\begingroup\$ It does! Can you put that detail in your question so folks don't have to read the back-and-forth comments to understand what you need? \$\endgroup\$ – DMGregory Nov 15 at 17:04

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.