Procedural Generation Tutorial Brick Noise Texture - Part 4
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_60.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 4 and the completed project file of Part 4.
In this tutorial series, we will be adding to the brick pattern texture using code by adding noise to the texture image.
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
- How to create 2D noise using the quads 'x' and 'y' position
- How to off set a hash value
- To add one int value to another when returning a result
Step 1: The theory
In this part we will cover how to make a 2D value noise pattern using the 1D approach and adding complexity to it. Thanks to this, this part will be relatively straight forward and quick.
Step 2: The basics
Code:
using UnityEngine;
public static class NoiseLibrary()
{
private static int [] hashValue =
{
8, 1, 3, 4, 14, 7, 5, 12, 11, 2, 10,
13, 0, 9, 6
};
public static float RandomValue1D (Vector3
point, float patternAlternationSpeed)
{
point *= patternAlternationSpeed;
int intX = Mathf.FloorToInt(point.x);
intX &= 15f;
return hashValue[intX] / 15f;
}
{
8, 1, 3, 4, 14, 7, 5, 12, 11, 2, 10,
13, 0, 9, 6
};
public static float RandomValue1D (Vector3
point, float patternAlternationSpeed)
{
point *= patternAlternationSpeed;
int intX = Mathf.FloorToInt(point.x);
intX &= 15f;
return hashValue[intX] / 15f;
}
}
Next we copy the function ‘RandomValue1D’ and paste it below, then change its name replacing the ‘1D’ on the end with ‘2D’.
using UnityEngine;
public static class NoiseLibrary()
Now we have done this in the ‘Noise’ script, in the for loop of the ‘CreateNoisePattern’ change the reference to ‘NoiseLibrary.RandomValue’ to ‘NoiseLibrary.RandomValue2D’ so we can see the changes to our code.
Next we copy the function ‘RandomValue1D’ and paste it below, then change its name replacing the ‘1D’ on the end with ‘2D’.
Code:
using UnityEngine;
public static class NoiseLibrary()
{
private static int [] hashValue =
{
8, 1, 3, 4, 14, 7, 5, 12, 11, 2, 10,
13, 0, 9, 6
};
public static float RandomValue1D (Vector3
point, float patternAlternationSpeed)
{
point *= patternAlternationSpeed;
int intX = Mathf.FloorToInt(point.x);
intX &= 15f;
return hashValue[intX] / 15f;
}
public static float RandomValue2D (Vector3
point, float patternAlternationSpeed)
{
point *= patternAlternationSpeed;
int intX = Mathf.FloorToInt(point.x);
intX &= 15f;
return hashValue[intX] / 15f;
}
}
{
8, 1, 3, 4, 14, 7, 5, 12, 11, 2, 10,
13, 0, 9, 6
};
public static float RandomValue1D (Vector3
point, float patternAlternationSpeed)
{
point *= patternAlternationSpeed;
int intX = Mathf.FloorToInt(point.x);
intX &= 15f;
return hashValue[intX] / 15f;
}
public static float RandomValue2D (Vector3
point, float patternAlternationSpeed)
{
point *= patternAlternationSpeed;
int intX = Mathf.FloorToInt(point.x);
intX &= 15f;
return hashValue[intX] / 15f;
}
}
Now we have done this in the ‘Noise’ script, in the for loop of the ‘CreateNoisePattern’ change the reference to ‘NoiseLibrary.RandomValue’ to ‘NoiseLibrary.RandomValue2D’ so we can see the changes to our code.
Code:
public void CreateNoisePattern()
{
...
for (int h = 0; h < resolutionH; h++)
{
CalculatePoint(leftSide, rightSide
h, normalizer);
noiseTexture.SetPixel(w, h, Color.white *
NoiseLibrary.RandomValue2D(point,
patternAlternationSpeed);
}
...
}
for (int h = 0; h < resolutionH; h++)
{
CalculatePoint(leftSide, rightSide
h, normalizer);
noiseTexture.SetPixel(w, h, Color.white *
NoiseLibrary.RandomValue2D(point,
patternAlternationSpeed);
}
...
}
Step 3: Introducing the 'Y' axis
Code:
using UnityEngine;
public static class NoiseLibrary()
{
...
public static float RandomValue2D (Vector3
point, float patternAlternationSpeed)
{
point *= patternAlternationSpeed;
int intX = Mathf.FloorToInt(point.x);
int intY = Mathf.FloorToInt(point.y);
intX &= 15f;
intY &= 15f;
return hashValue[intX] / 15f;
}
}
public static float RandomValue2D (Vector3
point, float patternAlternationSpeed)
{
point *= patternAlternationSpeed;
int intX = Mathf.FloorToInt(point.x);
int intY = Mathf.FloorToInt(point.y);
intX &= 15f;
intY &= 15f;
return hashValue[intX] / 15f;
}
}
Step 4: Returning the value and seeing the result
Code:
using UnityEngine;
public static class NoiseLibrary()
{
...
public static float RandomValue2D (Vector3
point, float patternAlternationSpeed)
{
point *= patternAlternationSpeed;
int intX = Mathf.FloorToInt(point.x);
int intY = Mathf.FloorToInt(point.y);
intX &= 15f;
intY &= 15f;
return hashValue[(hashValue[intX] + intY)
& 15f];
}
}
Code:
using UnityEngine;
public static class NoiseLibrary()
{
...
public static float RandomValue2D (Vector3
point, float patternAlternationSpeed)
{
point *= patternAlternationSpeed;
int intX = Mathf.FloorToInt(point.x);
int intY = Mathf.FloorToInt(point.y);
intX &= 15f;
intY &= 15f;
return hashValue[(hashValue[intX] + intY)
& 15f] * (1f / 15f);
}
}
Resolution 256, pattern alternation speed 32.
- Create 2D noise
- Add one hash value to another
- Use a normaliser when returning a hash value
- Use one has value to offset another in a hash array indexer
- Multiply a hash value by normaliser to get an apparently random value from a hash table
- Adding another dimension to ‘Random.Values’ merely requires adding another position value to what we already have
No comments:
Post a Comment