Skip to content

Instantly share code, notes, and snippets.

@ChrisHinde
Last active August 21, 2023 15:25
Show Gist options
  • Save ChrisHinde/86e3db54888afdb1e040293335b6dc34 to your computer and use it in GitHub Desktop.
Save ChrisHinde/86e3db54888afdb1e040293335b6dc34 to your computer and use it in GitHub Desktop.
Calculate an "Instance ID" based on UV coordinates
shader InstanceGrid(
point proj = point(u, v, 0) [[string label = "Projection"]],
int cols = 10 [[string label = "Columns", int min=0, int max=1000]],
int rows = 10 [[string label = "Rows", int min=0, int max=1000]],
int fulluv = 0 [[string label = "Use full UV", string widget = "boolean"]],
int invert_u = 0 [[string label = "Invert U", string widget = "boolean"]],
int invert_v = 0 [[string label = "Invert V", string widget = "boolean"]],
output color c = 0 [[string label = "Instance ID"]]
)
{
// Prepare variables needed later
float u_ = proj[0];
float v_ = proj[1];
float total = rows * cols;
float u_sz = 1 / (float)cols; // "Size" of patches in U direction (width)
float v_sz = 1 / (float)rows; // "Size" of patches in V direction (height)
// "Round" the coordinates, based on the number of patches (needed to get distinct patches)
// eg. 0.123456789 with 10x10 patches will become 0.12
u_ = floor(u_ * total) / total;
v_ = floor(v_ * total) / total;
if (fulluv) {
// Scale the coordinates
u_ = u_ / cols;
v_ = v_ / rows;
}
// Flip the coordinates (if asked for)
if (invert_u)
u_ = 1 - u_;
if (invert_v)
v_ = 1 - v_;
// Turn the prepared coordinates (between 0.00.. to 1.00..) to A,B (or X,Y) integer coordinates
float a = floor(u_ / u_sz);
float b = floor(v_ / v_sz);
// Calculate the index (0 - 'total'), and then turn it to a fraction of the total number of patches
float i = (b * cols + a) / (total);
// Create the color (greyscale) for the output
c = color(i, i, i);
}
/*
Calculate an "Instance ID" (0.000 - 1.000) based on UV coordinates.
This was made to work in conjunction with the Array Modifier in Blender,
but can be useful in other circumstances as well
* * * * * *
USAGE:
Inputs:
o Projection - UV projection/Texture coordinates to be used
o Column - Number of columns that you have
o Rows - Number of rows that you have
o Scale - Scale the texture/output
(If you use a "Full UV" set this to the number of Rows)
o Invert U - Flip the U coordinates/output Horizontally
o Invert V - Flip the V coordinates/output Vertically
Output:
Outputs a greyscale texture with values from 0.0000 to 1.0000
divided to (Rows * Columns) number of unique values (patches)
Setup:
How to use this script is described in this article:
https://chris.hindefjord.se/resources/how-tos/instance-mapping-with-array-modifier-blender/
* * * * * *
CC-BY - Chris Hindefjord - 2023-08
chris.hindefjord.se - chris@hindefjord.se
You are free to use this in any project, without creditation.
But if you repost/include this in a project file, etc,
please do so with this information intact!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment