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.
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.
Assets/Textures
folder and import the image named spiralTex
, or any other texture of your choice.Materials
folder and name it mat_spiral
.mat_Spiral
as we did before.Sphere
in the scene by selecting GameObject | CreateOther | Sphere from the main menu.mat_spiral
onto the sphere in your scene. The following screenshot shows how the material should look:Assets
directory named Scripts
to store the new asset (you should remember how to do that from previous recipes).Scripts
folder, create a new C# script
folder, as shown in the following screenshot:AnimateUV
and double-click on it to open it in Monodevelop, the default Unity script editor.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); } }
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!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.
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.
3.147.58.194