Procedural Generation Tutorial Brick Noise Texture - Part 2
Before you start you should know these things:
- Create Texture variables
- Manipulate textures using SetPixels
You should have gone through and completed all the ‘Checker Board Texture’ and 'Brick Pattern Texture' tutorials before going any further. If you find yourself not understanding some of the terminology or code of the tutorial I would also recommend going through the previous tutorial to get up to speed. I will leave a link to it below to go through at your leisure.
http://joseph-easter.blogspot.com/2017/01/procedural-generation-tutorial-brick_26.html
If you follow this tutorial and find I am moving too fast or if you don’t know the things in the list above, I would recommend getting up to speed and then come back to this tutorial when you are ready.
With this tutorial if you want to convert this into JavaScript by all means do so but it may be easier for you to follow this in C# and stick with the language of the tutorial to make it easier.
PROJECT RESOURCES: At the bottom of the post there is a download link to a zip file. This files includes all files needed to follow Part 2 and the completed project file of Part 2.
In this tutorial series, we will be adding to the brick pattern texture using code by adding noise to the texture image.
We will:
- Explain the theory
- Declare local coordinates of a quad
- How to use use Lerp between two Vector3's to get one Vector3 point
- How to get a point between upper and lower side of a quad
- Find a point between the left or sight side of a quad
- Convert local coordinate space to world space
In Part one we created a way to generate noise using one of Unity’s built in functions and it created a random noise pattern every time. We said next time we will change it so we get psudo-random noise in case we need to replicate a pattern, based on the object’s location. In this part we will not see the results of our code until part 3 because if I were to put in how to get the objects location and turn it into a value and how to use the psudo-random noise, part 2 would have been par too long.
Step 1: The theory
Step 1: The theory
Step 2: Defining local coordinates
Code:
Vector3 pointLL;
Vector3 pointUL;
Vector3 pointLR;
Vector3 pointUR;
...
void CreateNoisePattern()
Vector3 pointUL;
Vector3 pointLR;
Vector3 pointUR;
...
void CreateNoisePattern()
{
//Random.seed = 29;
WorldCoordinates();
for (int w = 0; w < resolutionW; w++)
{
for (int h = 0; h < resolutionH; h++)
{
noiseTexture.SetPixel(w, h,
Color.grey * Random.Value);
}
}
noiseTexture.Apply();
WorldCoordinates();
for (int w = 0; w < resolutionW; w++)
{
for (int h = 0; h < resolutionH; h++)
{
noiseTexture.SetPixel(w, h,
Color.grey * Random.Value);
}
}
noiseTexture.Apply();
}
...
void WorldCoordinates()
{
pointLL = new Vector3(-0.5f, -0.5f);
pointUL = new Vector3( 0.5f, -0.5f);
pointLR = new Vector3(-0.5f, 0.5f);
pointLR = new Vector3( 0.5f, 0.5f);
}
Code:
Vector3 leftSide;
Vector3 rightSide;
..void CreateNoisePattern()
Vector3 rightSide;
..void CreateNoisePattern()
{
//Random.seed = 29;
float normalizer = 1f / resolutionW;
WorldCoordinates();
for (int w = 0; w < resolutionW; w++)
{
LeftAndRightSides(pointLL, pointLR,
pointUL, pointUR,
w, normalizer);
for (int h = 0; h < resolutionH; h++)
{
noiseTexture.SetPixel(w, h,
Color.grey * Random.Value);
}
}
noiseTexture.Apply();
float normalizer = 1f / resolutionW;
WorldCoordinates();
for (int w = 0; w < resolutionW; w++)
{
LeftAndRightSides(pointLL, pointLR,
pointUL, pointUR,
w, normalizer);
for (int h = 0; h < resolutionH; h++)
{
noiseTexture.SetPixel(w, h,
Color.grey * Random.Value);
}
}
noiseTexture.Apply();
}
...
void LeftAndRightSides(Vector3 pLL, Vector3 pUL, Vector3 pLR, Vector3 pUR, int w, float n)
{
leftSide = Vector3.Lerp(pointLL, pointUL,
(w + 0.5f) * n);
rightSide = Vector3.Lerp(pointLR, pointUR,
(w + 0.5f) * n);
}
Code:
Vector3 point;
..void CreateNoisePattern()
..void CreateNoisePattern()
{
//Random.seed = 29;
float normalizer = 1f / resolutionW;
WorldCoordinates();
for (int w = 0; w < resolutionW; w++)
{
LeftAndRightSides(pointLL, pointLR,
pointUL, pointUR,
w, normalizer);
for (int h = 0; h < resolutionH; h++)
{
CalculatePoint(leftSide, rightSide,
h, normalizer);
noiseTexture.SetPixel(w, h,
Color.grey * Random.Value);
}
}
noiseTexture.Apply();
float normalizer = 1f / resolutionW;
WorldCoordinates();
for (int w = 0; w < resolutionW; w++)
{
LeftAndRightSides(pointLL, pointLR,
pointUL, pointUR,
w, normalizer);
for (int h = 0; h < resolutionH; h++)
{
CalculatePoint(leftSide, rightSide,
h, normalizer);
noiseTexture.SetPixel(w, h,
Color.grey * Random.Value);
}
}
noiseTexture.Apply();
}
...
void CalculatePoint(Vector3 lSide, Vector3 rSide, int h, float n)
{
point = Vector3.Lerp(lSide, rSide,
(w + 0.5f) * n);
}
Step 3: Converting local coordinates to world space
Code:
void WorldCoordinates()
{
pointLL = transform.TransformPoint(new
Vector3(-0.5f, -0.5f));
pointUL = transform.TransformPoint(new
Vector3( 0.5f, -0.5f));
pointLR = transform.TransformPoint(new
Vector3(-0.5f, 0.5f));
pointLR = transform.TransformPoint(new
Vector3( 0.5f, 0.5f));
}
We have learnt how to:
- Declare local coordinates of the four points of the quad
- Interpolate between two vector3 variables to get one vector3
- Find a point between upper & lower left to get a point on the left side (same for right side)
- Find a point between the left and right side to get a point in the middle of the quad
- Convert the local point coordinates (or UV coordinates) to world space coordinates
Click here for Part 3.
No comments:
Post a Comment