Animated materials

Another very useful thing that can be done with materials is to animate them by manipulating their so called UVs to get interesting rendering effects.

U and V are the letters used to refer to the x and y axes of a 2D image that is going to be used to texture a 3D mesh. As the X, Y, and Z letters are already in use for the model, the x and y axes for the image are named with the letters U and V.

The process of putting a 2D image on a 3D model is consequently called UV mapping, and it is a very basic activity for any 3D artist.

As a complete tour of UV mapping would go beyond the scope of this chapter, we suggest you checkout this Wikipedia link to begin learning more about it: http://en.wikipedia.org/wiki/UV_mappingt.

The next recipe describes how textures can be animated through code.

Getting ready

For this recipe, we need a texture, a game object, and a piece of code. We provided a nice spiral texture for you to use, in case you don't already have one.

How to do it...

  1. Start a new Unity scene.
  2. Access the Assets/Textures folder and import the image named spiralTex, or any other texture of your choice.
  3. Create a new material in the Materials folder and name it mat_spiral.
  4. Drag spiralTex onto mat_Spiral as we did before.
  5. Create a Sphere in the scene by selecting GameObject | CreateOther | Sphere from the main menu.
  6. Now drag mat_spiral onto the sphere in your scene. The following screenshot shows how the material should look:
    How to do it...
  7. For the next step, we need a script to be added to our sphere. Let's first create a new folder in our Assets directory named Scripts to store the new asset (you should remember how to do that from previous recipes).
  8. In the Scripts folder, create a new C# script folder, as shown in the following screenshot:
    How to do it...
  9. Name this script AnimateUV and double-click on it to open it in Monodevelop, the default Unity script editor.
  10. Add the following code to the script:
    using UnityEngine;
    using System.Collections;
    public class AnimateUV : MonoBehaviour {
      public float scrollSpeed = 0.5f;
      // Use this for initialization
      
      // Update is called once per frame
      void Update () {
        float offSetY=scrollSpeed*Time.time;
        myVector = new Vector2(0, offsetY);
        renderer.material.SetTextureOffset("_MainTex", myVector);
      }
    }
  11. Drag the animateUV script onto the sphere in the scene and click on the Play button. What you should get is the spiral texture rotating around the y axis on the game object. Don't fall asleep!

How it works...

With this script, we set a speed for the rotation of the texture (the scrollSpeed float variable) and then use time passing by to make the texture rotate on the y axis of the model (by multiplying scrollSpeed for time).

The instruction to actually rotate the texture UVs is renderer.material.SetTextureOffset, which asks for a material component to operate on (in our case, the diffuse component of the material, specified with the _MainTex conventional name) and a Vector2 variable to define rotation on the x or y axis (or both, if you like). As a matter of fact, in this recipe, we acted on the y axis.

There's more...

Though scripting is planned to be the topic of another chapter, we decided to add this recipe here to show you an interesting example of how materials can be manipulated through scripting. To learn more about advanced usage of textures and materials, we recommend you check out the online Unity reference manual at http://docs.unity3d.com/Manual/Textures.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.58.194