Skip to content

Instantly share code, notes, and snippets.

@Szaq
Last active February 21, 2019 10:16
Show Gist options
  • Save Szaq/8baf64e7d0bab605b9542f9022832f94 to your computer and use it in GitHub Desktop.
Save Szaq/8baf64e7d0bab605b9542f9022832f94 to your computer and use it in GitHub Desktop.
vector custom_random(vector Seed, vector Pixelator, float RandomSeed, float Division) {
return hashnoise(Pixelator * 2343249.2 + Seed * RandomSeed);
}
float count_treshold(float RandomizedCount, float Count) {
float Mix = Count < 0 ? (1 - RandomizedCount) : RandomizedCount;
return Mix < abs(Count);
}
float clipping_mask(vector Vector, float CountTreshold) {
return (Vector[0] > 0 && Vector[0] < 1 && Vector[1] > 0 && Vector[1] < 1) ? CountTreshold : 0;
}
vector randomize_location(vector Sawtooth, float OffsetPhaseShiftRad, float RandomizedOffset, float RandomizedOutput, float RandomOffset, int WrapTextureCoordinates) {
float SinOffsetPhaseShift = sin(OffsetPhaseShiftRad);
float CosOffsetPhaseShift = cos(OffsetPhaseShiftRad);
float NormalizedOffset = (RandomizedOffset- 0.5) * 2;
float NormalizedOutput = (RandomizedOutput- 0.5) * 2;
vector location = vector(NormalizedOffset * CosOffsetPhaseShift - NormalizedOutput * SinOffsetPhaseShift,
NormalizedOffset * SinOffsetPhaseShift + NormalizedOutput * CosOffsetPhaseShift,
0) * RandomOffset + Sawtooth;
if (WrapTextureCoordinates > 0) {
return fmod(location + 1, vector(1));
} else {
return location;
}
}
vector cell_rotation(vector RandomizedLocation, float RandomAngle, float RandomSize, float XYRatio) {
float SquareRootXYRatio = sqrt(XYRatio);
vector CenteredLocation = RandomizedLocation - 0.5;
vector SinLocation = CenteredLocation * sin(RandomAngle);
vector CosLocation = CenteredLocation * cos(RandomAngle);
return vector(CosLocation[0] - SinLocation[1], SinLocation[0] + CosLocation[1], 0.0)
* vector(1.0 / SquareRootXYRatio, SquareRootXYRatio, 0.0)
/ RandomSize
+ vector(0.5, 0.5, 0);
}
shader scatter_material(
vector InputMap = 0,
float Count = 1,
float Division = 10,
float IndividualSize = 0.5,
float RandomSize = 0.0,
float Rotation = 0.0,
float RandomRotation = 0.0,
float RotationSteps = 0.0,
float RandomOffset = 0.0,
float OffsetPhaseShift = 0.0,
float RandomSeed = 0.0,
float RandomProfile = 0.0,
float XYRatio = 1.0,
int WrapTextureCoordinates = 0
[[ string help = "Should coordinates of single image be wrapped",
string widget = "mapper",
string options = "Disabled:0|Enabled:1" ]],
output vector Vector = 0.0,
output float ClippingMask = 0.0,
output float OutRandomSize = 0.0,
output float OutRandomRotation = 0.0,
output float Random1 = 0.0,
output float Random2 = 0.0,
output float Random3 = 0.0,
output float Random4 = 0.0)
{
vector ModBase = vector(1 / Division);
vector AbsBase = InputMap/ModBase;
vector Sawtooth = (AbsBase - floor(AbsBase)) * ModBase;//fmod(abs(InputMap), vector(1 / Division));
vector Pixelator = InputMap - Sawtooth;
//Generating randomness
vector AngleSizeCount = custom_random(vector(9.7, 6.9, 13), Pixelator, RandomSeed, Division);
vector OffsetOutputRandom = custom_random(vector(12.2, 1.3, 2.70), Pixelator, RandomSeed, Division);
vector ExtraRandomOutput = custom_random(vector(-4.5, 0.3, 16.900), Pixelator, RandomSeed, Division);
float RandomizedSize = mix(IndividualSize, AngleSizeCount[1] * IndividualSize, RandomSize);
// Location
vector RandomizedLocation = randomize_location(Sawtooth * Division, radians(OffsetPhaseShift), OffsetOutputRandom[0], OffsetOutputRandom[1], RandomOffset / 200, WrapTextureCoordinates);
// Rotation & Scale
float RandomRotationRad = radians(RandomRotation);
float ScaledRandomAngle = AngleSizeCount[0] * RandomRotationRad;
float RandomAngle = ScaledRandomAngle + radians(Rotation) - (abs(RandomRotationRad) > 0 ? fmod(ScaledRandomAngle, radians(RotationSteps)) : 0.0);
//Generating Output
Vector = cell_rotation(RandomizedLocation, RandomAngle, RandomizedSize, XYRatio);
ClippingMask = clipping_mask(Vector, count_treshold(AngleSizeCount[2], Count));
Random1 = OffsetOutputRandom[2];
Random2 = ExtraRandomOutput[0];
Random3 = ExtraRandomOutput[1];
Random4 = ExtraRandomOutput[2];
OutRandomSize = mix(1, RandomizedSize, RandomSize);
OutRandomRotation = AngleSizeCount[0] * RandomRotation / 360;
}
vector custom_random(vector Seed, vector Pixelator, float RandomSeed, float Division) {
return hashnoise(Pixelator * 2343249.2 + Seed * RandomSeed);
}
float count_treshold(float RandomizedCount, float Count) {
float Mix = Count < 0 ? (1 - RandomizedCount) : RandomizedCount;
return Mix < abs(Count);
}
float clipping_mask(vector Vector, float CountTreshold) {
return (Vector[0] > 0 && Vector[0] < 1 && Vector[1] > 0 && Vector[1] < 1) ? CountTreshold : 0;
}
vector randomize_location(vector Sawtooth, float OffsetPhaseShiftRad, float RandomizedOffset, float RandomizedOutput, float RandomOffset, int WrapTextureCoordinates) {
float SinOffsetPhaseShift = sin(OffsetPhaseShiftRad);
float CosOffsetPhaseShift = cos(OffsetPhaseShiftRad);
float NormalizedOffset = (RandomizedOffset- 0.5) * 2;
float NormalizedOutput = (RandomizedOutput- 0.5) * 2;
vector location = vector(NormalizedOffset * CosOffsetPhaseShift - NormalizedOutput * SinOffsetPhaseShift,
NormalizedOffset * SinOffsetPhaseShift + NormalizedOutput * CosOffsetPhaseShift,
0) * RandomOffset + Sawtooth;
if (WrapTextureCoordinates > 0) {
return fmod(location + 1, vector(1));
} else {
return location;
}
}
vector cell_rotation(vector RandomizedLocation, float RandomAngle, float RandomSize, float XYRatio) {
float SquareRootXYRatio = sqrt(XYRatio);
vector CenteredLocation = RandomizedLocation - 0.5;
float SinLocationX = CenteredLocation[0] * sin(RandomAngle);
float SinLocationY = CenteredLocation[1] * sin(RandomAngle);
float CosLocationX = CenteredLocation[0] * cos(RandomAngle);
float CosLocationY = CenteredLocation[1] * cos(RandomAngle);
return vector((CosLocationX - SinLocationY) / SquareRootXYRatio / RandomSize + 0.5,
(SinLocationX + CosLocationY) * SquareRootXYRatio / RandomSize + 0.5,
0.0);
}
shader scatter_material(
vector InputMap = 0,
float Count = 1,
float Division = 10,
float IndividualSize = 0.5,
float RandomSize = 0.0,
float Rotation = 0.0,
float RandomRotation = 0.0,
float RotationSteps = 0.0,
float RandomOffset = 0.0,
float OffsetPhaseShift = 0.0,
float RandomSeed = 0.0,
float XYRatio = 1.0,
int WrapTextureCoordinates = 0
[[ string help = "Should coordinates of single image be wrapped",
string widget = "mapper",
string options = "Disabled:0|Enabled:1" ]],
output vector Vector = 0.0,
output float ClippingMask = 0.0,
output float OutRandomSize = 0.0,
output float OutRandomRotation = 0.0,
output float Random1 = 0.0,
output float Random2 = 0.0,
output float Random3 = 0.0,
output float Random4 = 0.0)
{
vector Sawtooth = fmod(abs(InputMap), vector(1 / Division));
vector Pixelator = InputMap - Sawtooth;
//Generating randomness
vector AngleSizeCount = custom_random(vector(9.7, 6.9, 13), Pixelator, RandomSeed, Division);
vector OffsetOutputRandom = custom_random(vector(12.2, 1.3, 2.70), Pixelator, RandomSeed, Division);
vector ExtraRandomOutput = custom_random(vector(-4.5, 0.3, 16.900), Pixelator, RandomSeed, Division);
float RandomizedSize = mix(IndividualSize, AngleSizeCount[1] * IndividualSize, RandomSize);
// Location
vector RandomizedLocation = randomize_location(Sawtooth * Division, radians(OffsetPhaseShift), OffsetOutputRandom[0], OffsetOutputRandom[1], RandomOffset / 200, WrapTextureCoordinates);
// Rotation & Scale
float RandomRotationRad = radians(RandomRotation);
float ScaledRandomAngle = AngleSizeCount[0] * RandomRotationRad;
float RandomAngle = ScaledRandomAngle + radians(Rotation) - (abs(RandomRotationRad) > 0 ? fmod(ScaledRandomAngle, radians(RotationSteps)) : 0.0);
//Generating Output
Vector = cell_rotation(RandomizedLocation, RandomAngle, RandomizedSize, XYRatio);
ClippingMask = clipping_mask(Vector, count_treshold(AngleSizeCount[2], Count));
Random1 = OffsetOutputRandom[2];
Random2 = ExtraRandomOutput[0];
Random3 = ExtraRandomOutput[1];
Random4 = ExtraRandomOutput[2];
OutRandomSize = mix(1, RandomizedSize, RandomSize);
OutRandomRotation = AngleSizeCount[0] * RandomRotation / 360;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment