Procedural Generation Tutorial Checker Board Texture - Part 2
The end result of the full tutorial.
At the end of Part 10 we will have produced this (See image 1).
Image 1
Before you start you should know these things:
You should have gone through and completed ‘Part 1’ of this tutorial 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.
https://joseph-easter.blogspot.com/2016/12/procedural-generation-tutorial-checker.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.
Links to Parts 1 - 3 of this tutorial series:
Part 1
Part 3
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.
This tutorial will be split up into several parts. In this part we will continue from part one by:
- Display our new texture variable on the Quad by assigning to the object material
Step 1:
Code:
void CreatePattern ()
{
...
mainTexture.Apply();
GetComponent<Renderer().material.mainTextur
= mainTexture;
}
You may be wondering why I have put this line here instead of create a separate function for it. I have done this for the time being because the function is fairly single purpose and straight forward to understand for now. Also the function it only about 20 lines long at the moment and we only have one texture to assign so it keeps things fairly compact. If we were creating multiple textures for a single game object, then I would create an array and a separate function for it.
{
...
mainTexture.Apply();
GetComponent<Renderer().material.mainTextur
= mainTexture;
}
You may be wondering why I have put this line here instead of create a separate function for it. I have done this for the time being because the function is fairly single purpose and straight forward to understand for now. Also the function it only about 20 lines long at the moment and we only have one texture to assign so it keeps things fairly compact. If we were creating multiple textures for a single game object, then I would create an array and a separate function for it.
Figure 1.0: far left is 2 by 2, middle left 4 by 4, middle right 8 by 8, far right, 16 by 16.
Step 2:
Code:
void CreatePattern ()
{
...
mainTexture.Apply();
GetComponent<Renderer().material.mainTextur
= mainTexture;
mainTexture.wrapMode = TextureWrapMode.Clamp;
}
{
...
mainTexture.Apply();
GetComponent<Renderer().material.mainTextur
= mainTexture;
mainTexture.wrapMode = TextureWrapMode.Clamp;
}
Figure 1.1
Step 3:
Code:
void CreatePattern ()
{
...
mainTexture.Apply();
GetComponent<Renderer().material.mainTextur
= mainTexture;
mainTexture.wrapMode =
TextureWrapMode.Clamp;
mainTexture.filterMode = FilterMode.Point;
}
Figure 1.2
- Assign a texture to our object’s material via code using it’s ‘mainTexture’ variable
- Prevent edges of a texture from repeating itself by setting it’s wrap mode to clamp
- Make the texture look sharper by changing its filter mode to ‘Point’ from bilinear
- Using the block variant of SetPixel called SetPixels32
- Have the block sizes change dynamically instead of colouring each pixel