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
I read part 1 of this tutorial that was easy to follow and was looking forward to reading this one. I am not disappointed. I have been putting off using multiple textures on a 3D model and swapping them around because it looked quite daunting from examples on various forums. With this tutorial being easy to follow, using good sample code, I understand how to use this and tweak how it is shown. It provides good building blocks to experiment with. It is good that the tutorial provides a link to download the source project files. This way if I have gone wrong I can compare my project to the source. Thanks! I am looking forward to reading part three soon.
ReplyDeleteThis is good to read. A subject that is quite difficult is explained in simple terms. Before reading this I didn't know how to change or assign a texture to a material through scripting. Now I have a working knowledge of how to do so. Tweaking how the texture is presented is explained well without going into too much detail as well.
ReplyDeleteI have been avoiding accessing the material properties through scripting because I didn't want to mess up the shader somehow. I'm glad you wrote this. I understand that through a MonoBehavior I can assign a texture variable to a material (I might try this with normal and bump maps as well). I'll use this to hot swap textures during runtime in a project. The other building blocks provided are good, like texture filtering and wrap modes (if a texture repeats itself at the edge). I would like to know why it does this though but that wasn't the scope of this post. I always wondered why small textures looked blurry on 3D models.
ReplyDelete