Procedural Generation Tutorial Basic Cell Pattern Texture - Part 1
The end result of the full tutorial.
At the end of Part 1 we will have produced this (See image 1.0).
At the end of Part 1 we will have produced this (See image 1.0).
Image 1.0
Before you start you should know these things:
- Create Texture variables
- Manipulate textures using SetPixels
- Create a basic texture pattern
- What noise is and hot to use it with a texture pattern
You should have gone through these tutorials before going further:
- Checker Board Texture
- Brick Pattern Texture
- Brick Pattern Noise Texture
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.
Checker Board Pattern Texture http://joseph-easter.blogspot.nl/2016/12/procedural-generation-tutorial-checker.html
Brick Texture Tutorial http://joseph-easter.blogspot.co.uk/2017/01/procedural-generation-tutorial-brick.html
Brick Noise Texture Tutorial http://joseph-easter.blogspot.nl/2017/02/procedural-generation-tutorial-brick_8.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 1 and the completed project file of Part 1.
In this tutorial series, we will be adding a pattern to the texture using code by adding noise to the texture image.
In this tutorial series, we will be creating a very simple pattern using a cellular texture using code. This tutorial will use and build upon what we have learnt from the previous texture tutorials, by creating two textures, one for the background, and one for the foreground, dividing the foreground into cells and adding an image into those cells then combining those textures. Later we will add variation to it by deciding whether to show a cell based on it noise value in the texture space, then using that noise to position it with in the cell itself using bombing.
We will:
- Explain the theory
- Add the Noise script to a quad
- Bootstrap some of our code from the BrickNoisePattern class
- Create the background texture (one block colour)
- Import the PNG image file
- Add the background colour to it
Step 1: The theory
Theory for the project:
However, in unity this is not a problem because it will set the pixels of the image if it has enough pixels to set in the texture. This causes another problem though. One image may overlap another one from another cell or two, this can be fixed by making the sure image is set between two predefined points within its cell.
Theory for Part 1:
Step 2: Bootstrapping code from the Brick Noise Texture and Setup
Image 1.1: This is the order they should be in.
Image 1.2: Make sure the Quad is set to
the default values in the inspector.
Now we go into our script and prepare it buy bootstrapping some code from ‘BrickNoiseTextureCreator’, essentially copy and pasting code we have already done before. First, we copy and paste a few variables ‘mainTexture’. Then we copy over the variables ‘mainTexWidth’, ‘mainTexHeight’, ‘blockWidth’, ‘blockHeight’, ‘colour0’, ‘colour1’, ‘colour0Arr’, ‘colour1Arr’ and ‘NoiseScr’ of type ‘Noise’. After this we copy and paste the functions ‘SetMainTextureSize’, ‘CreateMortarColour’, ‘ConvertColourToArray’, ‘SetBlockSize’, ‘CreatePattern’ and ‘AddTexturesTogether’. (Also below ‘mainTexture’ add ‘combinedTexture’).
Code:
public Texture2D mainTexture;
public Texture2D combinedTexture;
public int mainTexWidth;
public int mainTexHeight;
int squaresX;
int squaresY;
int blockWidth;
int blockHeight;
public Color32 color0;
public Color32 color1;
public Color32[] color0Arr;
public Color32[] color1Arr;
public Noise noiseScr;
void Start()
public Texture2D combinedTexture;
public int mainTexWidth;
public int mainTexHeight;
int squaresX;
int squaresY;
int blockWidth;
int blockHeight;
public Color32 color0;
public Color32 color1;
public Color32[] color0Arr;
public Color32[] color1Arr;
public Noise noiseScr;
void Start()
{
SetMaintextureSize();
CreateMortarColour();
ConvertColourToArray(blockWidth *
blockHeight);
CreateMortarColour();
ConvertColourToArray(blockWidth *
blockHeight);
CreatePattern();
AddTexturesTogether();
AddTexturesTogether();
}
void SetMaintextureSize()
{
...
}
void CreateMortarColour(Texture2D tex)
{
...
}
void ConvertColourToArray()
{
...
}
void CreatePattern()
{
...
}
void AddTexturesTogether()
{
...
}
Step 3: Creating and Setting the Background Colour
Code:
...
void Start()
void Start()
{
SetMaintextureSize();
CreateBackgroundColour(mainTexture);
ConvertColourToArray(blockWidth *
blockHeight);
CreateBackgroundColour(mainTexture);
ConvertColourToArray(blockWidth *
blockHeight);
CreatePattern();
AddTexturesTogether();
AddTexturesTogether();
}
...
void CreateBackgroundColour(Texture2D tex)
{
...
}
Image 1.3
Code:
...
void CreateBackgroundColour(Texture2D tex)
{
ConvertColourToArray(tex.width *
tex.height);
tex.SetPixels32(0, 0, tex.width,
tex.height, colour0Arr);
tex.Apply();
GetComponent<Renderer>().material.
mainTexture = tex;
}
Image 1.4
Image 1.5
Image 1.6: Resolution 256 by 256.
Great, part one is finished. We have laid down the foundations.
We have learnt how to:
- Make a simple texture variable
- Colour our texture with one block colour
Go to Part 2, click here.
If you enjoyed this tutorial and would like me to add some extra content to it, like and share this tutorial on here and social media and leave a comment below. If you didn’t like this tutorial please leave a comment below saying why.
If you enjoyed this tutorial and would like me to add some extra content to it, like and share this tutorial on here and social media and leave a comment below. If you didn’t like this tutorial please leave a comment below saying why.