- Home /
Question by
RobAnthem · Jan 14, 2017 at 07:36 AM ·
texturecolorproceduralprocedural-generationprocedural texturing
Can't find a way to simplify this code... any help appreciated...
So I'm writing an extension for texture generation, and when I ran into bricks I quickly realized that some simple fractal or perlin noise was not enough, I needed to calculate the grout lines accurately. So after a few hours this is what I came up with... I spent the rest of the day combing over it and simplifying and fixing things where I saw it needed it, but I still think I am MASSIVELY overlooking some easy steps...
else if (textureType == TextureType.Bricks)
{
//Calculate the noise scale.
float xCoord = xOrg + x / (float)noiseTex.width * scale;
float yCoord = yOrg + y / (float)noiseTex.height * scale;
//Calculate the linear scale of noise.
float xyValue = xCoord + addNoise(x / (float)noiseTex.width * noiseScaleX * scale, y / (float)noiseTex.height * noiseScaleY * scale, noiseSize) * noiseStrength;
float sample = Mathf.Sin(xyValue);
//Create our ratio sample.
if (cutoff > 0.0f)
sample = (sample - cutoff) / (1.0f - cutoff);
//Declare the noise we are coming from.
float fromRatio = sample;
//Declare the noise we are going towards.
float toRatio = 1.0f - sample;
//Calculate the height of a brick.
int bHeight = noiseTex.height / brickCount;
//Calculate the width of the brick.
int bWidth = noiseTex.width / (brickCount / 2);
//Calculate the row of bricks.
int moveCount = y / bHeight;
//Prevent the divison of zero.
if (y > 0)
{
//Check if the remainder of the y coordinate is less than or equal to our brick grout lines.
//Within the confined space of 0 - brickSpacing.
if (y % bHeight <= brickSpacing && y % bHeight >= 0)
{
//Run the method of obtaining the next color from the color generator.
pix[i] = getNextColor(fromRatio, toRatio, bWidth - x % bWidth, y % bHeight, true);
//If the current pixel is center of the grout line.
if (y % bHeight <= brickSpacing / 2)
{
//Darken the pixel by 10%.
pix[i] -= Color.white * .1f;
}
//Our pixel is not center of the grout line.
else
{
//Darken the pixel by 5%.
pix[i] -= Color.white * 0.05f;
}
}
//If our x coordinate is not on the far left side, and our number of the remainder of our brick rows divided by 2 is 0.
//And our Width is offset by half the width of a normal brick, then offset again, so that it draws equal size brick lines centered above old bricks
//Then checking if we are within the confined space of our normally generated grout lines.
else if (x > brickSpacing && moveCount % 2 == 0 && (x + bWidth / 2) / 2 % (bWidth / 2) <= brickSpacing / 2
&& (x + bWidth / 2) / 2 % (bWidth / 2) >= 0 && y >= bHeight * moveCount && y <= bHeight * (moveCount + 1))
{
//Run the method of obtaining the next color from the color generator.
pix[i] = getNextColor(fromRatio, toRatio, bWidth - x % bWidth, y % bHeight, true);
//If the current pixel is center of the grout line.
if ((x + bWidth / 2) / 2 % (bWidth / 2) <= brickSpacing / 2 / 2)
{
//Darken the pixel by 10%.
pix[i] -= Color.white * .1f;
}
//It is not center of our grout line.
else
{
//Darken the pixel by 5%.
pix[i] -= Color.white * 0.05f;
}
}
//Is this pixel a normal grout line, not offset. Calculated by our normal width remainders and height constraints.
else if (moveCount % 2 != 0 && x % bWidth <= brickSpacing && x % bWidth >= 0 && y >= bHeight * moveCount && y <= bHeight * (moveCount + 1))
{
//Run the method of obtaining the next color from the color generator.
pix[i] = getNextColor(fromRatio, toRatio, bWidth - x % bWidth, y % bHeight, true);
if (x % bWidth <= brickSpacing / 2)
{
//Darken the pixel by 10%.
pix[i] -= Color.white * .1f;
}
else
{
//Darken the pixel by 5%.
pix[i] -= Color.white * 0.05f;
}
}
else
{
//Run the method of obtaining the next color from the color generator.
pix[i] = getNextColor(fromRatio, toRatio, x, y);
}
//Set the alpha back to max.
pix[i].a = 1f;
}
//This is the first row of pixels.
else
{
//Run the method of obtaining the next color from the color generator.
pix[i] = getNextColor(fromRatio, toRatio, x, y, true);
//Darken the pixel by 5%.
pix[i] -= Color.white * .05f;
//Set the alpha back to max.
pix[i].a = 1f;
}
Comment