Hi everyone,
This is a script to partition a geometry into isolated elements and tag these elements with an attribute.
This can be used to randomly assign textures to the various disconnected parts of a single mesh.
What does it do?
It goes through the input geometry and figures out which polygons are connected together. It then creates a vertex attribute and assigns a value to each vertex based on the geometry island the vertex belongs to.
Usage
For this example I will use this floor geometry created in 3DSMax with the FloorGenerator plugin. This is a single OBJ file.
1. Add a Scripted graph node and load the script "partition.lua" into it.
2. Plug the geometry node you want to partition into the Geometry input.
3. Choose a name for the attribute using the Attribute name input, for example "elementid".
We will use this later to control texturing.
Remember that there is a limit to 4 float attributes for Mesh nodes, you need to have at least one empty slot in your source mesh for this to work.
The attribute values will range from 1 to the number of islands found.
4. Texturing
Any texturing applied to the input geometry will also be applied to the modified copy.
4.1 Add a Greyscale vertex attribute texture node and set its Name input to the chosen attribute name.
4.2 Add a Random map texture node (If you don't have this node an OSL version is provided below.). Plug the Greyscale vertex attribute node into it and set the Noise function to Hash.
This will convert our attribute from integers into a number between 0 and 1 that we can use in a Gradient map.
4.3 Add a Gradient map texture node. Plug the Random map into it. Set the interpolation to Constant and add some control points.
You should get something like the following.
We have assigned colors randomly to the isolated elements of the geometry.
Instead of simple colors, you can use actual textures as the values of the Gradient map's control points, as in the first image.
The final graph looks like this:
Tip: The Input scale field in the Random map node can be used as a seed to change the result.
Note: It will work with Alembic scenes and more complex input trees, but if the input contains a scripted graph or a wrapped scripted graph (e.g: Scatter tools), it may not work. I am working on fixing this issue.
Feedback, bug reports and suggestions are highly appreciated.
Lua script:
Demo ORBX:
Random map OSL: