Animation Nodes can be used to dynamically generate voronoi diagrams. Storing the color information of the diagram and accessing it from the render engine is a problem and our options are limited, one option is to use vertex colors, where the quality depends on the number of loops in the target mesh.
Vertex Color
First, you should study how vertex colors works in blender and how to assign it using Animation Nodes and Python. For this, you should see my answer here. I will be using the Vertex Sample and Vertex Colors subprograms defined there in my node trees, so make sure you implement them first.
A simple F1 vornoi diagram based on random points can be generated using a KD tree as follows:
If one wants to use custom points locations, then one should replace the random locations with the custom locations:
If you want to have a solid color for each voronoi cell, then the indices output can be used or a random number based on it:
You might notice that the quality starts to become rather low, and that is the downside of using vertex colors, they are dependent of the loop count.
The other options we have are not any nicer, but they might be suitable for some cased.
Screen Space Tracing
What we can do is make a small render engine of our own that renders the vornoi as a fragment shader and use the output in the more advanced render engine. The render engine will cast a number of rays on the mesh defined by a BVH Tree, get the location on the surface point they hit, use it in the KD Tree, and then write the output colors to an image. To write an image given the greyscale value of each pixel, its width, height and name, a script like this can be used:
if imageName in bpy.data.images:
image = bpy.data.images[imageName]
image.generated_width = width
image.generated_height = height
else:
image = bpy.data.images.new(imageName, width, height)
image.pixels = [v for color in colors for v in [color]*4]
The ray casting part is simple, align a grid with the camera orientation and location, scale it based on the orthographic scale (3 in my case), we will perform an extra 90
degree rotation around the local z axis because the grid node returns the locations in the vertically first order while blender expects a horizontally first order, the node tree is as follows:
You may see the rendered image in near realtime in the UV image editor
Now, if I used the Windows texture coordinates (Screen Space Coordinates) with a material like this:
We will get the render projected on our object, which we can then use to do what ever we want:
I created this to work in orthographic view, I will let you create the perspective version on your own.
Other methods include UV space projection, it can be done as following. BVH ray cast is used to get the index of the polygon the ray intersect, we get the barycentric coordinates of the surface point through a barycentric interpolation, we get the UV space locations of the polygon's corresponding UV polygon, perform a convex combination using the barycentric coordinates we computed before, get the pixel nearest to the result of the convex combination or use some kind of interpolation like bilinear or bicubic, write the color to it. The resulted image can be used in the UV objects space as a normal texture. I shall not explain this method, because it doesn't really have any advantages and is hard and slow to implement.