Skip to content

Instantly share code, notes, and snippets.

@ChrisHinde
Last active August 21, 2023 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisHinde/785dcf4c93b4d8cf9b688e9c889dc6a6 to your computer and use it in GitHub Desktop.
Save ChrisHinde/785dcf4c93b4d8cf9b688e9c889dc6a6 to your computer and use it in GitHub Desktop.
OSL-script that generates a random (greyscale) value per “instance”
shader InstanceGrid_Random(
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 utime=1 [[string label = "Time", int min=0, float max=1000, float sliderexponent=2]],
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);
// Generate a random value based on the current column and row
float i = cellnoise(b * cols + a, utime);
// Create the color (greyscale) for the output
c = color(i, i, i);
}
/*
Calculate a random "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 random greyscale texture with values from 0.0000 to 1.0000
based on the UV position
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